MockCnxn() throws IOException { super(new ZKClientConfig()); }
@Test public void testClientCanonicalization() throws IOException, InterruptedException { SaslServerPrincipal.WrapperInetSocketAddress addr = mock(SaslServerPrincipal.WrapperInetSocketAddress.class); SaslServerPrincipal.WrapperInetAddress ia = mock(SaslServerPrincipal.WrapperInetAddress.class); when(addr.getHostName()).thenReturn("zookeeper.apache.org"); when(addr.getAddress()).thenReturn(ia); when(ia.getCanonicalHostName()).thenReturn("zk1.apache.org"); when(ia.getHostAddress()).thenReturn("127.0.0.1"); ZKClientConfig conf = new ZKClientConfig(); String principal = SaslServerPrincipal.getServerPrincipal(addr, conf); Assert.assertEquals("The computed principal does not appear to have been canonicalized", "zookeeper/zk1.apache.org", principal); }
@Test public void testSaslClientDisabled() { System.clearProperty(ZKClientConfig.ENABLE_CLIENT_SASL_KEY); Assert.assertTrue("SASL client disabled", new ZKClientConfig().isSaslClientEnabled()); for (String value : Arrays.asList("true", "TRUE")) { System.setProperty(ZKClientConfig.ENABLE_CLIENT_SASL_KEY, value); Assert.assertTrue("SASL client disabled", new ZKClientConfig().isSaslClientEnabled()); } for (String value : Arrays.asList("false", "FALSE")) { System.setProperty(ZKClientConfig.ENABLE_CLIENT_SASL_KEY, value); Assert.assertFalse("SASL client disabled", new ZKClientConfig().isSaslClientEnabled()); } } }
@Test public void testClientNoCanonicalization() throws IOException, InterruptedException { SaslServerPrincipal.WrapperInetSocketAddress addr = mock(SaslServerPrincipal.WrapperInetSocketAddress.class); SaslServerPrincipal.WrapperInetAddress ia = mock(SaslServerPrincipal.WrapperInetAddress.class); when(addr.getHostName()).thenReturn("zookeeper.apache.org"); when(addr.getAddress()).thenReturn(ia); when(ia.getCanonicalHostName()).thenReturn("zk1.apache.org"); when(ia.getHostAddress()).thenReturn("127.0.0.1"); ZKClientConfig conf = new ZKClientConfig(); conf.setProperty(ZKClientConfig.ZK_SASL_CLIENT_CANONICALIZE_HOSTNAME, "false"); String principal = SaslServerPrincipal.getServerPrincipal(addr, conf); Assert.assertEquals("The computed principal does appears to have been canonicalized incorrectly", "zookeeper/zookeeper.apache.org", principal); }
@Test public void testClientCanonicalizationToIp() throws IOException, InterruptedException { SaslServerPrincipal.WrapperInetSocketAddress addr = mock(SaslServerPrincipal.WrapperInetSocketAddress.class); SaslServerPrincipal.WrapperInetAddress ia = mock(SaslServerPrincipal.WrapperInetAddress.class); when(addr.getHostName()).thenReturn("zookeeper.apache.org"); when(addr.getAddress()).thenReturn(ia); when(ia.getCanonicalHostName()).thenReturn("127.0.0.1"); when(ia.getHostAddress()).thenReturn("127.0.0.1"); ZKClientConfig conf = new ZKClientConfig(); String principal = SaslServerPrincipal.getServerPrincipal(addr, conf); Assert.assertEquals("The computed principal does appear to have falled back to the original host name", "zookeeper/zookeeper.apache.org", principal); } }
ZKClientConfig conf = new ZKClientConfig(); for (Map.Entry<String, String> e : properties.entrySet()) { assertEquals(e.getValue(), conf.getProperty(e.getKey())); conf = new ZKClientConfig();
@Test public void testClientReconnect() throws IOException, InterruptedException { HostProvider hostProvider = mock(HostProvider.class); when(hostProvider.size()).thenReturn(1); InetSocketAddress inaddr = new InetSocketAddress("127.0.0.1", 1111); when(hostProvider.next(anyLong())).thenReturn(inaddr); ZooKeeper zk = mock(ZooKeeper.class); when(zk.getClientConfig()).thenReturn(new ZKClientConfig()); sc = SocketChannel.open(); ClientCnxnSocketNIO nioCnxn = new MockCnxn(); ClientWatchManager watcher = mock(ClientWatchManager.class); ClientCnxn clientCnxn = new ClientCnxn( "tmp", hostProvider, 5000, zk, watcher, nioCnxn, false); clientCnxn.start(); countDownLatch.await(5000, TimeUnit.MILLISECONDS); Assert.assertTrue(countDownLatch.getCount() == 0); clientCnxn.close(); } }
+ (sessionPasswd == null ? "<null>" : "<hidden>")); this.clientConfig = new ZKClientConfig(); watchManager = defaultWatchManager(); watchManager.defaultWatcher = watcher;
@Test public void testReadConfigurationFile() throws IOException, ConfigException { File file = File.createTempFile("clientConfig", ".conf", testData); file.deleteOnExit(); Properties clientConfProp = new Properties(); clientConfProp.setProperty(ENABLE_CLIENT_SASL_KEY, "true"); clientConfProp.setProperty(ZK_SASL_CLIENT_USERNAME, "ZK"); clientConfProp.setProperty(LOGIN_CONTEXT_NAME_KEY, "MyClient"); clientConfProp.setProperty(ZOOKEEPER_SERVER_REALM, "HADOOP.COM"); clientConfProp.setProperty("dummyProperty", "dummyValue"); OutputStream io = new FileOutputStream(file); try { clientConfProp.store(io, "Client Configurations"); } finally { io.close(); } ZKClientConfig conf = new ZKClientConfig(); conf.addConfiguration(file.getAbsolutePath()); assertEquals(conf.getProperty(ENABLE_CLIENT_SASL_KEY), "true"); assertEquals(conf.getProperty(ZK_SASL_CLIENT_USERNAME), "ZK"); assertEquals(conf.getProperty(LOGIN_CONTEXT_NAME_KEY), "MyClient"); assertEquals(conf.getProperty(ZOOKEEPER_SERVER_REALM), "HADOOP.COM"); assertEquals(conf.getProperty("dummyProperty"), "dummyValue"); // try to delete it now as we have done with the created file, why to // wait for deleteOnExit() deletion file.delete(); }
clientConfig = new ZKClientConfig();
@Test public void testSystemPropertyValue() { String clientName = "zookeeper1"; System.setProperty(ZK_SASL_CLIENT_USERNAME, clientName); ZKClientConfig conf = new ZKClientConfig(); assertEquals(conf.getProperty(ZK_SASL_CLIENT_USERNAME), clientName); String newClientName = "zookeeper2"; conf.setProperty(ZK_SASL_CLIENT_USERNAME, newClientName); assertEquals(conf.getProperty(ZK_SASL_CLIENT_USERNAME), newClientName); }
@Test public void testWhenInvalidJuteMaxBufferIsConfiguredIOExceptionIsThrown() { ZKClientConfig clientConfig = new ZKClientConfig(); String value = "SomeInvalidInt"; clientConfig.setProperty(ZKConfig.JUTE_MAXBUFFER, value); // verify ClientCnxnSocketNIO creation try { new ClientCnxnSocketNIO(clientConfig); fail("IOException is expected."); } catch (IOException e) { assertTrue(e.getMessage().contains(value)); } // verify ClientCnxnSocketNetty creation try { new ClientCnxnSocketNetty(clientConfig); fail("IOException is expected."); } catch (IOException e) { assertTrue(e.getMessage().contains(value)); } }
@Test public void testSetConfiguration() { ZKClientConfig conf = new ZKClientConfig(); String defaultValue = conf.getProperty(ZKClientConfig.ENABLE_CLIENT_SASL_KEY, ZKClientConfig.ENABLE_CLIENT_SASL_DEFAULT); if (defaultValue.equals("true")) { conf.setProperty(ENABLE_CLIENT_SASL_KEY, "false"); } else { conf.setProperty(ENABLE_CLIENT_SASL_KEY, "true"); } assertTrue(conf.getProperty(ENABLE_CLIENT_SASL_KEY) != defaultValue); }
@Test public void testIntegerRetrievalFromProperty() { ZKClientConfig conf = new ZKClientConfig(); String prop = "UnSetProperty" + System.currentTimeMillis(); int defaultValue = 100; // property is not set we should get the default value int result = conf.getInt(prop, defaultValue); assertEquals(defaultValue, result); // property is set but can not be parsed to int, we should get the // NumberFormatException conf.setProperty(ZKConfig.JUTE_MAXBUFFER, "InvlaidIntValue123"); try { result = conf.getInt(ZKConfig.JUTE_MAXBUFFER, defaultValue); fail("NumberFormatException is expected"); } catch (NumberFormatException exception) { // do nothing } assertEquals(defaultValue, result); // property is set to an valid int, we should get the set value int value = ZKClientConfig.CLIENT_MAX_PACKET_LENGTH_DEFAULT; conf.setProperty(ZKConfig.JUTE_MAXBUFFER, Integer.toString(value)); result = conf.getInt(ZKConfig.JUTE_MAXBUFFER, defaultValue); assertEquals(value, result); // property is set but with white spaces value = 12345; conf.setProperty(ZKConfig.JUTE_MAXBUFFER, " " + Integer.toString(value) + " "); result = conf.getInt(ZKConfig.JUTE_MAXBUFFER, defaultValue); assertEquals(value, result); }