http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/d1a0748b/geode-assembly/src/test/java/com/gemstone/gemfire/rest/internal/web/controllers/RestAPIsWithSSLDUnitTest.java ---------------------------------------------------------------------- diff --git a/geode-assembly/src/test/java/com/gemstone/gemfire/rest/internal/web/controllers/RestAPIsWithSSLDUnitTest.java b/geode-assembly/src/test/java/com/gemstone/gemfire/rest/internal/web/controllers/RestAPIsWithSSLDUnitTest.java index 692ff19..2e80ebf 100644 --- a/geode-assembly/src/test/java/com/gemstone/gemfire/rest/internal/web/controllers/RestAPIsWithSSLDUnitTest.java +++ b/geode-assembly/src/test/java/com/gemstone/gemfire/rest/internal/web/controllers/RestAPIsWithSSLDUnitTest.java @@ -19,6 +19,7 @@ package com.gemstone.gemfire.rest.internal.web.controllers; import com.gemstone.gemfire.cache.*; import com.gemstone.gemfire.cache.client.ClientCache; import com.gemstone.gemfire.cache.client.ClientCacheFactory; +import com.gemstone.gemfire.cache.client.ClientRegionFactory; import com.gemstone.gemfire.cache.client.ClientRegionShortcut; import com.gemstone.gemfire.cache.client.internal.LocatorTestBase; import com.gemstone.gemfire.cache.server.CacheServer; @@ -30,10 +31,7 @@ import com.gemstone.gemfire.internal.AvailablePortHelper; import com.gemstone.gemfire.internal.cache.GemFireCacheImpl; import com.gemstone.gemfire.management.ManagementException; import com.gemstone.gemfire.management.ManagementTestBase; -import com.gemstone.gemfire.test.dunit.Host; -import com.gemstone.gemfire.test.dunit.IgnoredException; -import com.gemstone.gemfire.test.dunit.NetworkUtils; -import com.gemstone.gemfire.test.dunit.VM; +import com.gemstone.gemfire.test.dunit.*; import com.gemstone.gemfire.util.test.TestUtil; import org.apache.http.HttpEntity; import org.apache.http.client.methods.CloseableHttpResponse; @@ -49,12 +47,13 @@ import javax.net.ssl.SSLContext; import java.io.*; import java.net.BindException; import java.security.KeyStore; -import java.util.Date; +import java.util.Calendar; import java.util.HashMap; import java.util.Map; import java.util.Properties; /** + * * @since 8.0 */ public class RestAPIsWithSSLDUnitTest extends LocatorTestBase { @@ -64,13 +63,13 @@ public class RestAPIsWithSSLDUnitTest extends LocatorTestBase { private ManagementTestBase helper; private final String PEOPLE_REGION_NAME = "People"; - + private File jks; public RestAPIsWithSSLDUnitTest(String name) { super(name); this.helper = new ManagementTestBase(name); - this.jks = findTrustedJKS(); + this.jks = findTrustedJKS(); } @@ -86,22 +85,32 @@ public class RestAPIsWithSSLDUnitTest extends LocatorTestBase { } private File findTrustedJKS() { - if (jks == null) { + if(jks == null){ jks = new File(TestUtil.getResourcePath(RestAPIsWithSSLDUnitTest.class, "/ssl/trusted.keystore")); } return jks; } + public String startBridgeServerWithRestServiceOnInVM(final VM vm, final String locators, final String[] regions, + final Properties sslProperties, final boolean clusterLevel) { + + final String hostName = vm.getHost().getHostName(); + final int serverPort = AvailablePortHelper.getRandomAvailableTCPPort(); + vm.invoke("startBridge", () -> startBridgeServer(hostName,serverPort,locators,regions,sslProperties,clusterLevel)); + return "https://" + hostName + ":" + serverPort + "/gemfire-api/v1"; + + } + @SuppressWarnings("deprecation") - protected int startBridgeServer(String hostName, int restServicePort, final String locators, final String[] regions, + protected int startBridgeServer(String hostName, int restServicerPort, final String locators, final String[] regions, final Properties sslProperties, boolean clusterLevel) { Properties props = new Properties(); - props.setProperty(DistributionConfig.MCAST_PORT_NAME, "0"); + props.setProperty(DistributionConfig.MCAST_PORT_NAME, String.valueOf(0)); props.setProperty(DistributionConfig.LOCATORS_NAME, locators); props.setProperty(DistributionConfig.START_DEV_REST_API_NAME, "true"); props.setProperty(DistributionConfig.HTTP_SERVICE_BIND_ADDRESS_NAME, hostName); - props.setProperty(DistributionConfig.HTTP_SERVICE_PORT_NAME, String.valueOf(restServicePort)); + props.setProperty(DistributionConfig.HTTP_SERVICE_PORT_NAME, String.valueOf(restServicerPort)); System.setProperty("javax.net.debug", "ssl,handshake"); configureSSL(props, sslProperties, clusterLevel); @@ -109,8 +118,8 @@ public class RestAPIsWithSSLDUnitTest extends LocatorTestBase { DistributedSystem ds = getSystem(props); Cache cache = CacheFactory.create(ds); ((GemFireCacheImpl) cache).setReadSerialized(true); - AttributesFactory factory = new AttributesFactory(); + factory.setEnableBridgeConflation(true); factory.setDataPolicy(DataPolicy.REPLICATE); RegionAttributes attrs = factory.create(); @@ -119,45 +128,81 @@ public class RestAPIsWithSSLDUnitTest extends LocatorTestBase { } CacheServer server = cache.addCacheServer(); - server.setPort(0); + final int serverPort = AvailablePortHelper.getRandomAvailableTCPPort(); + server.setPort(serverPort); try { server.start(); } catch (IOException e) { e.printStackTrace(); } remoteObjects.put(CACHE_KEY, cache); - return new Integer(server.getPort()); + return new Integer(serverPort); } public void doPutsInClientCache() { - ClientCache clientCache = ClientCacheFactory.getAnyInstance(); - assertNotNull(clientCache); - Region<String, Object> region = clientCache.getRegion(PEOPLE_REGION_NAME); + ClientCache cache = GemFireCacheImpl.getInstance(); + assertNotNull(cache); + Region<String, Object> region = cache.getRegion(PEOPLE_REGION_NAME); // put person object - region.put("1", new Person(101L, "Mithali", "Dorai", "Raj", new Date(), Gender.FEMALE)); - region.put("2", new Person(102L, "Sachin", "Ramesh", "Tendulkar", new Date(), Gender.MALE)); - region.put("3", new Person(103L, "Saurabh", "Baburav", "Ganguly", new Date(), Gender.MALE)); - region.put("4", new Person(104L, "Rahul", "subrymanyam", "Dravid", new Date(), Gender.MALE)); - region.put("5", new Person(105L, "Jhulan", "Chidambaram", "Goswami", new Date(), Gender.FEMALE)); + final Person person1 = new Person(101L, "Mithali", "Dorai", "Raj", DateTimeUtils.createDate(1982, + Calendar.DECEMBER, 4), Gender.FEMALE); + final Person person2 = new Person(102L, "Sachin", "Ramesh", "Tendulkar", DateTimeUtils.createDate(1975, + Calendar.DECEMBER, 14), Gender.MALE); + final Person person3 = new Person(103L, "Saurabh", "Baburav", "Ganguly", DateTimeUtils.createDate(1972, + Calendar.AUGUST, 29), Gender.MALE); + final Person person4 = new Person(104L, "Rahul", "subrymanyam", "Dravid", DateTimeUtils.createDate(1979, + Calendar.MARCH, 17), Gender.MALE); + final Person person5 = new Person(105L, "Jhulan", "Chidambaram", "Goswami", DateTimeUtils.createDate(1983, + Calendar.NOVEMBER, 25), Gender.FEMALE); + + region.put("1", person1); + region.put("2", person2); + region.put("3", person3); + region.put("4", person4); + region.put("5", person5); + + final Person person6 = new Person(101L, "Rahul", "Rajiv", "Gndhi", + DateTimeUtils.createDate(1970, Calendar.MAY, 14), Gender.MALE); + final Person person7 = new Person(102L, "Narendra", "Damodar", "Modi", DateTimeUtils.createDate(1945, + Calendar.DECEMBER, 24), Gender.MALE); + final Person person8 = new Person(103L, "Atal", "Bihari", "Vajpayee", DateTimeUtils.createDate(1920, + Calendar.AUGUST, 9), Gender.MALE); + final Person person9 = new Person(104L, "Soniya", "Rajiv", "Gandhi", DateTimeUtils.createDate(1929, Calendar.MARCH, + 27), Gender.FEMALE); + final Person person10 = new Person(104L, "Priyanka", "Robert", "Gandhi", DateTimeUtils.createDate(1973, + Calendar.APRIL, 15), Gender.FEMALE); + + final Person person11 = new Person(104L, "Murali", "Manohar", "Joshi", DateTimeUtils.createDate(1923, + Calendar.APRIL, 25), Gender.MALE); + final Person person12 = new Person(104L, "Lalkrishna", "Parmhansh", "Advani", DateTimeUtils.createDate(1910, + Calendar.JANUARY, 01), Gender.MALE); + final Person person13 = new Person(104L, "Shushma", "kumari", "Swaraj", DateTimeUtils.createDate(1943, + Calendar.AUGUST, 10), Gender.FEMALE); + final Person person14 = new Person(104L, "Arun", "raman", "jetly", DateTimeUtils.createDate(1942, Calendar.OCTOBER, + 27), Gender.MALE); + final Person person15 = new Person(104L, "Amit", "kumar", "shah", DateTimeUtils.createDate(1958, Calendar.DECEMBER, + 21), Gender.MALE); + final Person person16 = new Person(104L, "Shila", "kumari", "Dixit", DateTimeUtils.createDate(1927, + Calendar.FEBRUARY, 15), Gender.FEMALE); Map<String, Object> userMap = new HashMap<String, Object>(); - userMap.put("6", new Person(101L, "Rahul", "Rajiv", "Gndhi", new Date(), Gender.MALE)); - userMap.put("7", new Person(102L, "Narendra", "Damodar", "Modi", new Date(), Gender.MALE)); - userMap.put("8", new Person(103L, "Atal", "Bihari", "Vajpayee", new Date(), Gender.MALE)); - userMap.put("9", new Person(104L, "Soniya", "Rajiv", "Gandhi", new Date(), Gender.FEMALE)); - userMap.put("10", new Person(104L, "Priyanka", "Robert", "Gandhi", new Date(), Gender.FEMALE)); - userMap.put("11", new Person(104L, "Murali", "Manohar", "Joshi", new Date(), Gender.MALE)); - userMap.put("12", new Person(104L, "Lalkrishna", "Parmhansh", "Advani", new Date(), Gender.MALE)); - userMap.put("13", new Person(104L, "Shushma", "kumari", "Swaraj", new Date(), Gender.FEMALE)); - userMap.put("14", new Person(104L, "Arun", "raman", "jetly", new Date(), Gender.MALE)); - userMap.put("15", new Person(104L, "Amit", "kumar", "shah", new Date(), Gender.MALE)); - userMap.put("16", new Person(104L, "Shila", "kumari", "Dixit", new Date(), Gender.FEMALE)); + userMap.put("6", person6); + userMap.put("7", person7); + userMap.put("8", person8); + userMap.put("9", person9); + userMap.put("10", person10); + userMap.put("11", person11); + userMap.put("12", person12); + userMap.put("13", person13); + userMap.put("14", person14); + userMap.put("15", person15); + userMap.put("16", person16); region.putAll(userMap); - if (clientCache != null) - clientCache.getLogger().info("Gemfire Cache Client: Puts successfully done"); + if (cache != null) + cache.getLogger().info("Gemfire Cache Client: Puts successfully done"); } @@ -172,30 +217,24 @@ public class RestAPIsWithSSLDUnitTest extends LocatorTestBase { // start locator int locatorPort = AvailablePort.getRandomAvailablePort(AvailablePort.SOCKET); - locator.invoke("Start Locator", () -> startLocator(locator.getHost(), locatorPort, "")); + startLocatorInVM(locator, locatorPort, ""); // find locators String locators = NetworkUtils.getServerHostName(locator.getHost()) + "[" + locatorPort + "]"; // start manager (peer cache) - manager.invoke("StartManager", () -> startManager(locators, new String[] { REGION_NAME }, sslProperties)); + startManagerInVM(manager, locators, new String[] { REGION_NAME }, sslProperties); // start startBridgeServer With RestService enabled - String restEndpoint = server.invoke("startBridgeServerWithRestServiceOnInVM", () -> { - final String hostName = server.getHost().getHostName(); - final int restServicePort = AvailablePortHelper.getRandomAvailableTCPPort(); - startBridgeServer(hostName, restServicePort, locators, new String[] { REGION_NAME }, sslProperties, clusterLevel); - return "https://" + hostName + ":" + restServicePort + "/gemfire-api/v1"; - }); + String restEndpoint = startBridgeServerWithRestServiceOnInVM(server, locators, new String[] { REGION_NAME }, + sslProperties, clusterLevel); // create a client cache - client.invoke("Create ClientCache", () -> new ClientCacheFactory() - .setPdxReadSerialized(true) - .addPoolLocator(NetworkUtils.getServerHostName(locator.getHost()), locatorPort).create()); + createClientCacheInVM(client, NetworkUtils.getServerHostName(locator.getHost()), locatorPort); // create region in Manager, peer cache and Client cache nodes - manager.invoke("createRegionInManager", () -> createRegionInCache()); - server.invoke("createRegionInPeerServer", () -> createRegionInCache()); + manager.invoke("createRegionInManager",() -> createRegionInManager()); + server.invoke("createRegionInPeerServer", () -> createRegionInPeerServer()); client.invoke("createRegionInClientCache", () -> createRegionInClientCache()); // do some person puts from clientcache @@ -221,114 +260,191 @@ public class RestAPIsWithSSLDUnitTest extends LocatorTestBase { helper.closeCache(client); } - private void sslPropertyConverter(Properties properties, Properties newProperties, String propertyName, String newPropertyName) { - String property = properties.getProperty(propertyName); - if (property != null) { - newProperties.setProperty((newPropertyName != null ? newPropertyName : propertyName), property); + private void createClientCacheInVM(VM vm, final String host, final int port) throws Exception { + SerializableRunnable connect = new SerializableRunnable("Start Cache client") { + public void run() { + // Connect using the GemFire locator and create a Caching_Proxy cache + ClientCache clientCache = new ClientCacheFactory().setPdxReadSerialized(true).addPoolLocator(host, port).create(); + clientCache.createClientRegionFactory(ClientRegionShortcut.PROXY).create(REGION_NAME); + } + }; + + if (vm == null) { + connect.run(); + } else { + vm.invoke(connect); } } private void configureSSL(Properties props, Properties sslProperties, boolean clusterLevel) { - if (clusterLevel) { - sslPropertyConverter(sslProperties, props, DistributionConfig.HTTP_SERVICE_SSL_ENABLED_NAME, DistributionConfig.CLUSTER_SSL_ENABLED_NAME); - sslPropertyConverter(sslProperties, props, DistributionConfig.HTTP_SERVICE_SSL_KEYSTORE_NAME, DistributionConfig.CLUSTER_SSL_KEYSTORE_NAME); - sslPropertyConverter(sslProperties, props, DistributionConfig.HTTP_SERVICE_SSL_KEYSTORE_PASSWORD_NAME, - DistributionConfig.CLUSTER_SSL_KEYSTORE_PASSWORD_NAME); - sslPropertyConverter(sslProperties, props, DistributionConfig.HTTP_SERVICE_SSL_KEYSTORE_TYPE_NAME, DistributionConfig.CLUSTER_SSL_KEYSTORE_TYPE_NAME); - sslPropertyConverter(sslProperties, props, DistributionConfig.HTTP_SERVICE_SSL_PROTOCOLS_NAME, DistributionConfig.CLUSTER_SSL_PROTOCOLS_NAME); - sslPropertyConverter(sslProperties, props, DistributionConfig.HTTP_SERVICE_SSL_REQUIRE_AUTHENTICATION_NAME, - DistributionConfig.CLUSTER_SSL_REQUIRE_AUTHENTICATION_NAME); - sslPropertyConverter(sslProperties, props, DistributionConfig.HTTP_SERVICE_SSL_TRUSTSTORE_NAME, DistributionConfig.CLUSTER_SSL_TRUSTSTORE_NAME); - sslPropertyConverter(sslProperties, props, DistributionConfig.HTTP_SERVICE_SSL_TRUSTSTORE_PASSWORD_NAME, - DistributionConfig.CLUSTER_SSL_TRUSTSTORE_PASSWORD_NAME); - } else { - sslPropertyConverter(sslProperties, props, DistributionConfig.HTTP_SERVICE_SSL_ENABLED_NAME, null); - sslPropertyConverter(sslProperties, props, DistributionConfig.HTTP_SERVICE_SSL_KEYSTORE_NAME, null); - sslPropertyConverter(sslProperties, props, DistributionConfig.HTTP_SERVICE_SSL_KEYSTORE_PASSWORD_NAME, null); - sslPropertyConverter(sslProperties, props, DistributionConfig.HTTP_SERVICE_SSL_KEYSTORE_TYPE_NAME, null); - sslPropertyConverter(sslProperties, props, DistributionConfig.HTTP_SERVICE_SSL_PROTOCOLS_NAME, null); - sslPropertyConverter(sslProperties, props, DistributionConfig.HTTP_SERVICE_SSL_REQUIRE_AUTHENTICATION_NAME, null); - sslPropertyConverter(sslProperties, props, DistributionConfig.HTTP_SERVICE_SSL_TRUSTSTORE_NAME, null); - sslPropertyConverter(sslProperties, props, DistributionConfig.HTTP_SERVICE_SSL_TRUSTSTORE_PASSWORD_NAME, null); + if(clusterLevel){ + if (sslProperties.getProperty(DistributionConfig.HTTP_SERVICE_SSL_ENABLED_NAME) != null) { + props.setProperty(DistributionConfig.CLUSTER_SSL_ENABLED_NAME, + sslProperties.getProperty(DistributionConfig.HTTP_SERVICE_SSL_ENABLED_NAME)); + } + if (sslProperties.getProperty(DistributionConfig.HTTP_SERVICE_SSL_KEYSTORE_NAME) != null) { + props.setProperty(DistributionConfig.CLUSTER_SSL_KEYSTORE_NAME, + sslProperties.getProperty(DistributionConfig.HTTP_SERVICE_SSL_KEYSTORE_NAME)); + } + if (sslProperties.getProperty(DistributionConfig.HTTP_SERVICE_SSL_KEYSTORE_PASSWORD_NAME) != null) { + props.setProperty(DistributionConfig.CLUSTER_SSL_KEYSTORE_PASSWORD_NAME, + sslProperties.getProperty(DistributionConfig.HTTP_SERVICE_SSL_KEYSTORE_PASSWORD_NAME)); + } + if (sslProperties.getProperty(DistributionConfig.HTTP_SERVICE_SSL_KEYSTORE_TYPE_NAME) != null) { + props.setProperty(DistributionConfig.CLUSTER_SSL_KEYSTORE_TYPE_NAME, + sslProperties.getProperty(DistributionConfig.HTTP_SERVICE_SSL_KEYSTORE_TYPE_NAME)); + } + if (sslProperties.getProperty(DistributionConfig.HTTP_SERVICE_SSL_PROTOCOLS_NAME) != null) { + props.setProperty(DistributionConfig.CLUSTER_SSL_PROTOCOLS_NAME, + sslProperties.getProperty(DistributionConfig.HTTP_SERVICE_SSL_PROTOCOLS_NAME)); + } + if (sslProperties.getProperty(DistributionConfig.HTTP_SERVICE_SSL_REQUIRE_AUTHENTICATION_NAME) != null) { + props.setProperty(DistributionConfig.CLUSTER_SSL_REQUIRE_AUTHENTICATION_NAME, + sslProperties.getProperty(DistributionConfig.HTTP_SERVICE_SSL_REQUIRE_AUTHENTICATION_NAME)); + } + if (sslProperties.getProperty(DistributionConfig.HTTP_SERVICE_SSL_TRUSTSTORE_NAME) != null) { + props.setProperty(DistributionConfig.CLUSTER_SSL_TRUSTSTORE_NAME, + sslProperties.getProperty(DistributionConfig.HTTP_SERVICE_SSL_TRUSTSTORE_NAME)); + } + if (sslProperties.getProperty(DistributionConfig.HTTP_SERVICE_SSL_TRUSTSTORE_PASSWORD_NAME) != null) { + props.setProperty(DistributionConfig.CLUSTER_SSL_TRUSTSTORE_PASSWORD_NAME, + sslProperties.getProperty(DistributionConfig.HTTP_SERVICE_SSL_TRUSTSTORE_PASSWORD_NAME)); + } + + }else{ + if (sslProperties.getProperty(DistributionConfig.HTTP_SERVICE_SSL_ENABLED_NAME) != null) { + props.setProperty(DistributionConfig.HTTP_SERVICE_SSL_ENABLED_NAME, + sslProperties.getProperty(DistributionConfig.HTTP_SERVICE_SSL_ENABLED_NAME)); + } + if (sslProperties.getProperty(DistributionConfig.HTTP_SERVICE_SSL_KEYSTORE_NAME) != null) { + props.setProperty(DistributionConfig.HTTP_SERVICE_SSL_KEYSTORE_NAME, + sslProperties.getProperty(DistributionConfig.HTTP_SERVICE_SSL_KEYSTORE_NAME)); + } + if (sslProperties.getProperty(DistributionConfig.HTTP_SERVICE_SSL_KEYSTORE_PASSWORD_NAME) != null) { + props.setProperty(DistributionConfig.HTTP_SERVICE_SSL_KEYSTORE_PASSWORD_NAME, + sslProperties.getProperty(DistributionConfig.HTTP_SERVICE_SSL_KEYSTORE_PASSWORD_NAME)); + } + if (sslProperties.getProperty(DistributionConfig.HTTP_SERVICE_SSL_KEYSTORE_TYPE_NAME) != null) { + props.setProperty(DistributionConfig.HTTP_SERVICE_SSL_KEYSTORE_TYPE_NAME, + sslProperties.getProperty(DistributionConfig.HTTP_SERVICE_SSL_KEYSTORE_TYPE_NAME)); + } + if (sslProperties.getProperty(DistributionConfig.HTTP_SERVICE_SSL_PROTOCOLS_NAME) != null) { + props.setProperty(DistributionConfig.HTTP_SERVICE_SSL_PROTOCOLS_NAME, + sslProperties.getProperty(DistributionConfig.HTTP_SERVICE_SSL_PROTOCOLS_NAME)); + } + if (sslProperties.getProperty(DistributionConfig.HTTP_SERVICE_SSL_REQUIRE_AUTHENTICATION_NAME) != null) { + props.setProperty(DistributionConfig.HTTP_SERVICE_SSL_REQUIRE_AUTHENTICATION_NAME, + sslProperties.getProperty(DistributionConfig.HTTP_SERVICE_SSL_REQUIRE_AUTHENTICATION_NAME)); + } + if (sslProperties.getProperty(DistributionConfig.HTTP_SERVICE_SSL_TRUSTSTORE_NAME) != null) { + props.setProperty(DistributionConfig.HTTP_SERVICE_SSL_TRUSTSTORE_NAME, + sslProperties.getProperty(DistributionConfig.HTTP_SERVICE_SSL_TRUSTSTORE_NAME)); + } + if (sslProperties.getProperty(DistributionConfig.HTTP_SERVICE_SSL_TRUSTSTORE_PASSWORD_NAME) != null) { + props.setProperty(DistributionConfig.HTTP_SERVICE_SSL_TRUSTSTORE_PASSWORD_NAME, + sslProperties.getProperty(DistributionConfig.HTTP_SERVICE_SSL_TRUSTSTORE_PASSWORD_NAME)); + } + } + + } - private void startManager(final String locators, final String[] regions, final Properties sslProperties) throws IOException { - + private int startManagerInVM(VM vm, final String locators, final String[] regions, final Properties sslProperties) { + IgnoredException.addIgnoredException("java.net.BindException"); IgnoredException.addIgnoredException("java.rmi.server.ExportException"); IgnoredException.addIgnoredException("com.gemstone.gemfire.management.ManagementException"); - - Properties props = new Properties(); - props.setProperty(DistributionConfig.MCAST_PORT_NAME, "0"); - props.setProperty(DistributionConfig.LOCATORS_NAME, locators); - props.setProperty("jmx-manager", "true"); - props.setProperty("jmx-manager-start", "true"); - - Cache cache = null; - configureSSL(props, sslProperties, false); - while (true) { - try { - DistributedSystem ds = getSystem(props); - System.out.println("Creating cache with http-service-port " + props.getProperty("http-service-port", "7070") - + " and jmx-manager-port " + props.getProperty("jmx-manager-port", "1099")); - cache = CacheFactory.create(ds); - System.out.println("Successfully created cache."); - break; - } catch (ManagementException ex) { - if ((ex.getCause() instanceof BindException) - || (ex.getCause() != null && ex.getCause().getCause() instanceof BindException)) { - //close cache and disconnect - GemFireCacheImpl existingInstance = GemFireCacheImpl.getInstance(); - if (existingInstance != null) { - existingInstance.close(); + + SerializableCallable connect = new SerializableCallable("Start Manager ") { + public Object call() throws IOException { + Properties props = new Properties(); + props.setProperty(DistributionConfig.MCAST_PORT_NAME, String.valueOf(0)); + props.setProperty(DistributionConfig.LOCATORS_NAME, locators); + props.setProperty("jmx-manager", "true"); + props.setProperty("jmx-manager-start", "true"); + + Cache cache = null; + while (true) { + try { + configureSSL(props, sslProperties, false); + DistributedSystem ds = getSystem(props); + System.out.println("Creating cache with http-service-port " + props.getProperty("http-service-port", "7070") + + " and jmx-manager-port " + props.getProperty("jmx-manager-port", "1099")); + cache = CacheFactory.create(ds); + System.out.println("Successfully created cache."); + break; } - InternalDistributedSystem ids = InternalDistributedSystem - .getConnectedInstance(); - if (ids != null) { - ids.disconnect(); + catch (ManagementException ex) { + if ((ex.getCause() instanceof BindException) + || (ex.getCause() != null && ex.getCause().getCause() instanceof BindException)) { + //close cache and disconnect + GemFireCacheImpl existingInstance = GemFireCacheImpl.getInstance(); + if (existingInstance != null) { + existingInstance.close(); + } + InternalDistributedSystem ids = InternalDistributedSystem + .getConnectedInstance(); + if (ids != null) { + ids.disconnect(); + } + //try a different port + int httpServicePort = AvailablePortHelper.getRandomAvailableTCPPort(); + int jmxManagerPort = AvailablePortHelper.getRandomAvailableTCPPort(); + props.setProperty("http-service-port", Integer.toString(httpServicePort)); + props.setProperty("jmx-manager-port", Integer.toString(jmxManagerPort)); + System.out.println("Try a different http-service-port " + httpServicePort); + System.out.println("Try a different jmx-manager-port " + jmxManagerPort); + } + else { + throw ex; + } } - //try a different port - int httpServicePort = AvailablePortHelper.getRandomAvailableTCPPort(); - int jmxManagerPort = AvailablePortHelper.getRandomAvailableTCPPort(); - props.setProperty("http-service-port", Integer.toString(httpServicePort)); - props.setProperty("jmx-manager-port", Integer.toString(jmxManagerPort)); - System.out.println("Try a different http-service-port " + httpServicePort); - System.out.println("Try a different jmx-manager-port " + jmxManagerPort); - } else { - throw ex; + } + AttributesFactory factory = new AttributesFactory(); + + factory.setEnableBridgeConflation(true); + factory.setDataPolicy(DataPolicy.REPLICATE); + RegionAttributes attrs = factory.create(); + for (int i = 0; i < regions.length; i++) { + cache.createRegion(regions[i], attrs); } - } - } - AttributesFactory factory = new AttributesFactory(); + CacheServer server = cache.addCacheServer(); + final int serverPort = AvailablePortHelper.getRandomAvailableTCPPort(); + server.setPort(serverPort); + server.start(); - factory.setEnableBridgeConflation(true); - factory.setDataPolicy(DataPolicy.REPLICATE); - RegionAttributes attrs = factory.create(); - for (int i = 0; i < regions.length; i++) { - cache.createRegion(regions[i], attrs); - } - CacheServer server = cache.addCacheServer(); - server.setPort(0); - server.start(); + return new Integer(serverPort); + } + }; + Integer port = (Integer) vm.invoke(connect); + return port.intValue(); } private void createRegionInClientCache() { - ClientCache clientCache = ClientCacheFactory.getAnyInstance(); - assertNotNull(clientCache); - clientCache.createClientRegionFactory(ClientRegionShortcut.PROXY).create(PEOPLE_REGION_NAME); - clientCache.createClientRegionFactory(ClientRegionShortcut.PROXY).create(REGION_NAME); + ClientCache cache = GemFireCacheImpl.getInstance(); + assertNotNull(cache); + ClientRegionFactory<String, Object> crf = cache.createClientRegionFactory(ClientRegionShortcut.PROXY); + crf.create(PEOPLE_REGION_NAME); } - private void createRegionInCache() { + private void createRegionInManager() { Cache cache = GemFireCacheImpl.getInstance(); assertNotNull(cache); - RegionFactory<String, Object> regionFactory = cache.createRegionFactory(RegionShortcut.REPLICATE); - regionFactory.create(PEOPLE_REGION_NAME); + RegionFactory<String, Object> rf = cache.createRegionFactory(RegionShortcut.REPLICATE); + rf.create(PEOPLE_REGION_NAME); } - private CloseableHttpClient getSSLBasedHTTPClient() throws Exception { + private void createRegionInPeerServer() { + Cache cache = GemFireCacheImpl.getInstance(); + assertNotNull(cache); + RegionFactory<String, Object> rf = cache.createRegionFactory(RegionShortcut.REPLICATE); + rf.create(PEOPLE_REGION_NAME); + } + + private CloseableHttpClient getSSLBasedHTTPClient(String algo) throws Exception { + File jks = findTrustedJKS(); KeyStore clientKeys = KeyStore.getInstance("JKS"); @@ -338,7 +454,7 @@ public class RestAPIsWithSSLDUnitTest extends LocatorTestBase { SSLContext sslcontext = SSLContexts.custom() .loadTrustMaterial(clientKeys, new TrustSelfSignedStrategy()) .loadKeyMaterial(clientKeys, "password".toCharArray()) - .build(); + .build(); // Host checking is disabled here , as tests might run on multiple hosts and // host entries can not be assumed @@ -350,7 +466,7 @@ public class RestAPIsWithSSLDUnitTest extends LocatorTestBase { return httpclient; } - private void validateConnection(String restEndpoint) { + private void validateConnection(String restEndpoint, String algo) { try { // 1. Get on key="1" and validate result. @@ -359,7 +475,8 @@ public class RestAPIsWithSSLDUnitTest extends LocatorTestBase { get.addHeader("Content-Type", "application/json"); get.addHeader("Accept", "application/json"); - CloseableHttpClient httpclient = getSSLBasedHTTPClient(); + + CloseableHttpClient httpclient = getSSLBasedHTTPClient(algo); CloseableHttpResponse response = httpclient.execute(get); HttpEntity entity = response.getEntity(); @@ -384,7 +501,7 @@ public class RestAPIsWithSSLDUnitTest extends LocatorTestBase { throw new RuntimeException("unexpected exception", e); } } - + // Actual Tests starts here. public void testSimpleSSL() throws Exception { @@ -394,80 +511,90 @@ public class RestAPIsWithSSLDUnitTest extends LocatorTestBase { props.setProperty(DistributionConfig.HTTP_SERVICE_SSL_KEYSTORE_NAME, jks.getCanonicalPath()); props.setProperty(DistributionConfig.HTTP_SERVICE_SSL_KEYSTORE_PASSWORD_NAME, "password"); props.setProperty(DistributionConfig.HTTP_SERVICE_SSL_KEYSTORE_TYPE_NAME, "JKS"); - validateConnection(startInfraWithSSL(props, false)); + String restEndpoint = startInfraWithSSL(props,false); + validateConnection(restEndpoint, "SSL"); } - + public void testSSLWithoutKeyStoreType() throws Exception { + + Properties props = new Properties(); props.setProperty(DistributionConfig.HTTP_SERVICE_SSL_ENABLED_NAME, "true"); props.setProperty(DistributionConfig.HTTP_SERVICE_SSL_KEYSTORE_NAME, jks.getCanonicalPath()); props.setProperty(DistributionConfig.HTTP_SERVICE_SSL_KEYSTORE_PASSWORD_NAME, "password"); + + String restEndpoint = startInfraWithSSL(props, false); + validateConnection(restEndpoint, "SSL"); - validateConnection(startInfraWithSSL(props, false)); } - + public void testSSLWithSSLProtocol() throws Exception { Properties props = new Properties(); props.setProperty(DistributionConfig.HTTP_SERVICE_SSL_ENABLED_NAME, "true"); props.setProperty(DistributionConfig.HTTP_SERVICE_SSL_KEYSTORE_NAME, jks.getCanonicalPath()); props.setProperty(DistributionConfig.HTTP_SERVICE_SSL_KEYSTORE_PASSWORD_NAME, "password"); - props.setProperty(DistributionConfig.HTTP_SERVICE_SSL_PROTOCOLS_NAME, "SSL"); - - validateConnection(startInfraWithSSL(props, false)); + props.setProperty(DistributionConfig.HTTP_SERVICE_SSL_PROTOCOLS_NAME,"SSL"); + + String restEndpoint = startInfraWithSSL(props, false); + validateConnection(restEndpoint, "SSL"); } - + public void testSSLWithTLSProtocol() throws Exception { Properties props = new Properties(); props.setProperty(DistributionConfig.HTTP_SERVICE_SSL_ENABLED_NAME, "true"); props.setProperty(DistributionConfig.HTTP_SERVICE_SSL_KEYSTORE_NAME, jks.getCanonicalPath()); props.setProperty(DistributionConfig.HTTP_SERVICE_SSL_KEYSTORE_PASSWORD_NAME, "password"); - props.setProperty(DistributionConfig.HTTP_SERVICE_SSL_PROTOCOLS_NAME, "TLS"); - - validateConnection(startInfraWithSSL(props, false)); + props.setProperty(DistributionConfig.HTTP_SERVICE_SSL_PROTOCOLS_NAME,"TLS"); + + String restEndpoint = startInfraWithSSL(props, false); + validateConnection(restEndpoint, "TLS"); } - + public void testSSLWithTLSv11Protocol() throws Exception { Properties props = new Properties(); props.setProperty(DistributionConfig.HTTP_SERVICE_SSL_ENABLED_NAME, "true"); props.setProperty(DistributionConfig.HTTP_SERVICE_SSL_KEYSTORE_NAME, jks.getCanonicalPath()); props.setProperty(DistributionConfig.HTTP_SERVICE_SSL_KEYSTORE_PASSWORD_NAME, "password"); - props.setProperty(DistributionConfig.HTTP_SERVICE_SSL_PROTOCOLS_NAME, "TLSv1.1"); - - validateConnection(startInfraWithSSL(props, false)); + props.setProperty(DistributionConfig.HTTP_SERVICE_SSL_PROTOCOLS_NAME,"TLSv1.1"); + + String restEndpoint = startInfraWithSSL(props, false); + validateConnection(restEndpoint, "TLSv1.1"); } - + public void testSSLWithTLSv12Protocol() throws Exception { Properties props = new Properties(); props.setProperty(DistributionConfig.HTTP_SERVICE_SSL_ENABLED_NAME, "true"); props.setProperty(DistributionConfig.HTTP_SERVICE_SSL_KEYSTORE_NAME, jks.getCanonicalPath()); props.setProperty(DistributionConfig.HTTP_SERVICE_SSL_KEYSTORE_PASSWORD_NAME, "password"); - props.setProperty(DistributionConfig.HTTP_SERVICE_SSL_PROTOCOLS_NAME, "TLSv1.2"); - - validateConnection(startInfraWithSSL(props, false)); + props.setProperty(DistributionConfig.HTTP_SERVICE_SSL_PROTOCOLS_NAME,"TLSv1.2"); + + String restEndpoint = startInfraWithSSL(props, false); + validateConnection(restEndpoint, "TLSv1.2"); } - + public void testWithMultipleProtocol() throws Exception { Properties props = new Properties(); props.setProperty(DistributionConfig.HTTP_SERVICE_SSL_ENABLED_NAME, "true"); props.setProperty(DistributionConfig.HTTP_SERVICE_SSL_KEYSTORE_NAME, jks.getCanonicalPath()); props.setProperty(DistributionConfig.HTTP_SERVICE_SSL_KEYSTORE_PASSWORD_NAME, "password"); - props.setProperty(DistributionConfig.HTTP_SERVICE_SSL_PROTOCOLS_NAME, "SSL,TLSv1.2"); - - validateConnection(startInfraWithSSL(props, false)); + props.setProperty(DistributionConfig.HTTP_SERVICE_SSL_PROTOCOLS_NAME,"SSL,TLSv1.2"); + + String restEndpoint = startInfraWithSSL(props, false); + validateConnection(restEndpoint, "TLSv1.2"); } - + public void testSSLWithCipherSuite() throws Exception { System.setProperty("javax.net.debug", "ssl"); @@ -475,51 +602,57 @@ public class RestAPIsWithSSLDUnitTest extends LocatorTestBase { props.setProperty(DistributionConfig.HTTP_SERVICE_SSL_ENABLED_NAME, "true"); props.setProperty(DistributionConfig.HTTP_SERVICE_SSL_KEYSTORE_NAME, jks.getCanonicalPath()); props.setProperty(DistributionConfig.HTTP_SERVICE_SSL_KEYSTORE_PASSWORD_NAME, "password"); - props.setProperty(DistributionConfig.HTTP_SERVICE_SSL_PROTOCOLS_NAME, "TLSv1.2"); - + props.setProperty(DistributionConfig.HTTP_SERVICE_SSL_PROTOCOLS_NAME,"TLSv1.2"); + SSLContext ssl = SSLContext.getInstance("TLSv1.2"); - + ssl.init(null, null, new java.security.SecureRandom()); String[] cipherSuites = ssl.getSocketFactory().getSupportedCipherSuites(); - - props.setProperty(DistributionConfig.HTTP_SERVICE_SSL_CIPHERS_NAME, cipherSuites[0]); - - validateConnection(startInfraWithSSL(props, false)); + + props.setProperty(DistributionConfig.HTTP_SERVICE_SSL_CIPHERS_NAME,cipherSuites[0]); + + String restEndpoint = startInfraWithSSL(props, false); + validateConnection(restEndpoint, "TLSv1.2"); } - + public void testSSLWithMultipleCipherSuite() throws Exception { Properties props = new Properties(); props.setProperty(DistributionConfig.HTTP_SERVICE_SSL_ENABLED_NAME, "true"); props.setProperty(DistributionConfig.HTTP_SERVICE_SSL_KEYSTORE_NAME, jks.getCanonicalPath()); props.setProperty(DistributionConfig.HTTP_SERVICE_SSL_KEYSTORE_PASSWORD_NAME, "password"); - props.setProperty(DistributionConfig.HTTP_SERVICE_SSL_PROTOCOLS_NAME, "TLSv1.2"); - + props.setProperty(DistributionConfig.HTTP_SERVICE_SSL_PROTOCOLS_NAME,"TLSv1.2"); + SSLContext ssl = SSLContext.getInstance("TLSv1.2"); - + ssl.init(null, null, new java.security.SecureRandom()); String[] cipherSuites = ssl.getSocketFactory().getSupportedCipherSuites(); - - props.setProperty(DistributionConfig.HTTP_SERVICE_SSL_CIPHERS_NAME, cipherSuites[0] + "," + cipherSuites[1]); - - validateConnection(startInfraWithSSL(props, false)); + + props.setProperty(DistributionConfig.HTTP_SERVICE_SSL_CIPHERS_NAME,cipherSuites[0]+","+cipherSuites[1]); + + String restEndpoint = startInfraWithSSL(props, false); + validateConnection(restEndpoint, "TLSv1.2"); } - + + public void testMutualAuthentication() throws Exception { Properties props = new Properties(); props.setProperty(DistributionConfig.HTTP_SERVICE_SSL_ENABLED_NAME, "true"); props.setProperty(DistributionConfig.HTTP_SERVICE_SSL_KEYSTORE_NAME, jks.getCanonicalPath()); props.setProperty(DistributionConfig.HTTP_SERVICE_SSL_KEYSTORE_PASSWORD_NAME, "password"); - props.setProperty(DistributionConfig.HTTP_SERVICE_SSL_PROTOCOLS_NAME, "SSL"); - props.setProperty(DistributionConfig.HTTP_SERVICE_SSL_REQUIRE_AUTHENTICATION_NAME, "true"); + props.setProperty(DistributionConfig.HTTP_SERVICE_SSL_PROTOCOLS_NAME,"SSL"); + props.setProperty(DistributionConfig.HTTP_SERVICE_SSL_REQUIRE_AUTHENTICATION_NAME,"true"); - props.setProperty(DistributionConfig.HTTP_SERVICE_SSL_TRUSTSTORE_NAME, jks.getCanonicalPath()); + props.setProperty(DistributionConfig.HTTP_SERVICE_SSL_TRUSTSTORE_NAME,jks.getCanonicalPath()); - props.setProperty(DistributionConfig.HTTP_SERVICE_SSL_TRUSTSTORE_PASSWORD_NAME, "password"); - validateConnection(startInfraWithSSL(props, false)); + props.setProperty(DistributionConfig.HTTP_SERVICE_SSL_TRUSTSTORE_PASSWORD_NAME,"password"); + + String restEndpoint = startInfraWithSSL(props, false); + validateConnection(restEndpoint, "SSL"); } + }
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/d1a0748b/geode-core/src/test/java/com/gemstone/gemfire/cache/client/internal/AutoConnectionSourceDUnitTest.java ---------------------------------------------------------------------- diff --git a/geode-core/src/test/java/com/gemstone/gemfire/cache/client/internal/AutoConnectionSourceDUnitTest.java b/geode-core/src/test/java/com/gemstone/gemfire/cache/client/internal/AutoConnectionSourceDUnitTest.java index ad578c5..cfcff5e 100644 --- a/geode-core/src/test/java/com/gemstone/gemfire/cache/client/internal/AutoConnectionSourceDUnitTest.java +++ b/geode-core/src/test/java/com/gemstone/gemfire/cache/client/internal/AutoConnectionSourceDUnitTest.java @@ -16,6 +16,15 @@ */ package com.gemstone.gemfire.cache.client.internal; +import java.io.Serializable; +import java.net.BindException; +import java.net.InetSocketAddress; +import java.util.HashSet; +import java.util.Iterator; +import java.util.List; + +import junit.framework.Assert; + import com.gemstone.gemfire.cache.Cache; import com.gemstone.gemfire.cache.Region; import com.gemstone.gemfire.cache.client.NoAvailableLocatorsException; @@ -28,23 +37,22 @@ import com.gemstone.gemfire.internal.AvailablePortHelper; import com.gemstone.gemfire.management.membership.ClientMembership; import com.gemstone.gemfire.management.membership.ClientMembershipEvent; import com.gemstone.gemfire.management.membership.ClientMembershipListenerAdapter; -import com.gemstone.gemfire.test.dunit.*; -import org.junit.Assert; - -import java.io.Serializable; -import java.net.BindException; -import java.net.InetSocketAddress; -import java.util.HashSet; -import java.util.Iterator; -import java.util.List; +import com.gemstone.gemfire.test.dunit.Host; +import com.gemstone.gemfire.test.dunit.IgnoredException; +import com.gemstone.gemfire.test.dunit.NetworkUtils; +import com.gemstone.gemfire.test.dunit.SerializableCallable; +import com.gemstone.gemfire.test.dunit.SerializableRunnable; +import com.gemstone.gemfire.test.dunit.VM; +import com.gemstone.gemfire.test.dunit.Wait; /** * Tests cases that are particular for the auto connection source - * - dynamically discovering servers, locators, handling + * - dynamically discovering servers, locators, handling * locator disappearance, etc. + * */ public class AutoConnectionSourceDUnitTest extends LocatorTestBase { - + protected static final Object BRIDGE_LISTENER = "BRIDGE_LISTENER"; private static final long MAX_WAIT = 60000; @@ -56,85 +64,84 @@ public class AutoConnectionSourceDUnitTest extends LocatorTestBase { public AutoConnectionSourceDUnitTest(String name) { super(name); } - + public void testDiscoverBridgeServers() throws Exception { final Host host = Host.getHost(0); VM vm0 = host.getVM(0); VM vm1 = host.getVM(1); VM vm2 = host.getVM(2); - + int locatorPort = AvailablePort.getRandomAvailablePort(AvailablePort.SOCKET); - vm0.invoke("Start Locator", () -> startLocator(vm0.getHost(), locatorPort, "")); - - String locators = NetworkUtils.getServerHostName(vm0.getHost()) + "[" + locatorPort + "]"; + startLocatorInVM(vm0, locatorPort, ""); + + String locators = NetworkUtils.getServerHostName(vm0.getHost())+ "[" + locatorPort + "]"; + + startBridgeServerInVM(vm1, null, locators); - vm1.invoke("Start BridgeServer", () -> startBridgeServer(null, locators)); - - vm2.invoke("StartBridgeClient", () -> startBridgeClient(null, NetworkUtils.getServerHostName(vm0.getHost()), locatorPort)); + startBridgeClientInVM(vm2, null, NetworkUtils.getServerHostName(vm0.getHost()), locatorPort); putAndWaitForSuccess(vm2, REGION_NAME, "key", "value"); - + Assert.assertEquals("value", getInVM(vm1, "key")); } public void testNoLocators() { - + final Host host = Host.getHost(0); VM vm0 = host.getVM(0); - + try { - vm0.invoke("StartBridgeClient", () -> startBridgeClient(null, NetworkUtils.getServerHostName(vm0.getHost()) - , AvailablePort.getRandomAvailablePort(AvailablePort.SOCKET))); + startBridgeClientInVM(vm0, null, NetworkUtils.getServerHostName(vm0.getHost()), AvailablePort.getRandomAvailablePort(AvailablePort.SOCKET)); putInVM(vm0, "key", "value"); fail("Client cache should not have been able to start"); - } catch (Exception e) { + } catch(Exception e) { //expected an exception } } - + public void testNoBridgeServer() { final Host host = Host.getHost(0); VM vm0 = host.getVM(0); VM vm1 = host.getVM(1); - + int locatorPort = AvailablePort.getRandomAvailablePort(AvailablePort.SOCKET); - vm0.invoke("Start Locator", () -> startLocator(vm0.getHost(), locatorPort, "")); - try { - vm1.invoke("StartBridgeClient", () -> startBridgeClient(null, NetworkUtils.getServerHostName(vm0.getHost()), locatorPort)); + startLocatorInVM(vm0, locatorPort, ""); + try { + startBridgeClientInVM(vm1, null, NetworkUtils.getServerHostName(vm0.getHost()), locatorPort); putInVM(vm0, "key", "value"); fail("Client cache should not have been able to start"); - } catch (Exception e) { + } catch(Exception e) { //expected an exception } } - - public void testDynamicallyFindBridgeServer() throws Exception { + + public void testDynamicallyFindBridgeServer() throws Exception { final Host host = Host.getHost(0); VM vm0 = host.getVM(0); VM vm1 = host.getVM(1); VM vm2 = host.getVM(2); VM vm3 = host.getVM(3); - + int locatorPort = AvailablePort.getRandomAvailablePort(AvailablePort.SOCKET); - vm0.invoke("Start Locator", () -> startLocator(vm0.getHost(), locatorPort, "")); - + startLocatorInVM(vm0, locatorPort, ""); + String locators = NetworkUtils.getServerHostName(vm0.getHost()) + "[" + locatorPort + "]"; - - vm1.invoke("Start BridgeServer", () -> startBridgeServer(null, locators)); - - vm2.invoke("StartBridgeClient", () -> startBridgeClient(null, NetworkUtils.getServerHostName(vm0.getHost()), locatorPort)); - + + startBridgeServerInVM(vm1, null, locators); + + startBridgeClientInVM(vm2, null, NetworkUtils.getServerHostName(vm0.getHost()), locatorPort); + putAndWaitForSuccess(vm2, REGION_NAME, "key", "value"); - - vm3.invoke("Start BridgeServer", () -> startBridgeServer(null, locators)); - + + startBridgeServerInVM(vm3, null, locators); + stopBridgeMemberVM(vm1); - + putAndWaitForSuccess(vm2, REGION_NAME, "key2", "value2"); - + Assert.assertEquals("value2", getInVM(vm3, "key2")); } - + public void testDynamicallyFindLocators() throws Exception { try { final Host host = Host.getHost(0); @@ -143,60 +150,59 @@ public class AutoConnectionSourceDUnitTest extends LocatorTestBase { VM vm1 = host.getVM(1); VM vm2 = host.getVM(2); VM vm3 = host.getVM(3); - + int[] ports = AvailablePortHelper.getRandomAvailableTCPPorts(3); - + final int locatorPort0 = ports[0]; final int locatorPort1 = ports[1]; final int locatorPort3 = ports[2]; - String locators = getLocatorString(host, new int[] { locatorPort0, locatorPort1, locatorPort3 }); - vm0.invoke("Start Locator", () -> startLocator(vm0.getHost(), locatorPort0, locators)); - vm1.invoke("Start Locator", () -> startLocator(vm1.getHost(), locatorPort1, locators)); - - vm2.invoke("StartBridgeClient", () -> startBridgeClient(null, NetworkUtils.getServerHostName(vm0.getHost()), locatorPort0)); - - InetSocketAddress locatorToWaitFor = new InetSocketAddress(hostName, locatorPort1); + String locators = getLocatorString(host, new int[] { locatorPort0, locatorPort1, locatorPort3}); + startLocatorInVM(vm0, locatorPort0, locators); + + startLocatorInVM(vm1, locatorPort1, locators); + startBridgeClientInVM(vm2, null, NetworkUtils.getServerHostName(vm0.getHost()), locatorPort0); + + InetSocketAddress locatorToWaitFor= new InetSocketAddress(hostName, locatorPort1); waitForLocatorDiscovery(vm2, locatorToWaitFor); - - vm0.invoke("Stop Locator", () -> stopLocator()); - vm0.invoke("Start BridgeServer", () -> startBridgeServer(null, locators)); - + + stopLocatorInVM(vm0); + startBridgeServerInVM(vm0, null, locators); + putAndWaitForSuccess(vm2, REGION_NAME, "key", "value"); Assert.assertEquals("value", getInVM(vm0, "key")); - - vm3.invoke("Start Locator", () -> startLocator(vm3.getHost(), locatorPort3, locators)); + + startLocatorInVM(vm3, locatorPort3, locators); stopBridgeMemberVM(vm0); - locatorToWaitFor = new InetSocketAddress(hostName, locatorPort3); + locatorToWaitFor= new InetSocketAddress(hostName, locatorPort3); waitForLocatorDiscovery(vm2, locatorToWaitFor); - vm1.invoke("Stop Locator", () -> stopLocator()); - vm1.invoke("Start BridgeServer", () -> startBridgeServer(null, locators)); + stopLocatorInVM(vm1); + startBridgeServerInVM(vm1, null, locators); putAndWaitForSuccess(vm2, REGION_NAME, "key2", "value2"); Assert.assertEquals("value2", getInVM(vm1, "key2")); - } catch (Exception ec) { - if (ec.getCause() != null && (ec.getCause().getCause() instanceof BindException)) + }catch(Exception ec) { + if(ec.getCause() != null && (ec.getCause().getCause() instanceof BindException)) return;//BindException let it pass throw ec; } } - - public void testEmbeddedLocator() throws Exception { + + public void testEmbeddedLocator() throws Exception { final Host host = Host.getHost(0); VM vm0 = host.getVM(0); VM vm1 = host.getVM(1); VM vm2 = host.getVM(2); VM vm3 = host.getVM(3); - + int locatorPort = AvailablePort.getRandomAvailablePort(AvailablePort.SOCKET); - + String locators = NetworkUtils.getServerHostName(vm0.getHost()) + "[" + locatorPort + "]"; - - vm0.invoke("Start BridgeServer", () -> startBridgeServerWithEmbeddedLocator(null, locators, new String[] { REGION_NAME } - , CacheServer.DEFAULT_LOAD_PROBE)); - - vm2.invoke("StartBridgeClient", () -> startBridgeClient(null, NetworkUtils.getServerHostName(vm0.getHost()), locatorPort)); - + + startBridgeServerWithEmbeddedLocator(vm0, null, locators, new String[] {REGION_NAME}, CacheServer.DEFAULT_LOAD_PROBE); + + startBridgeClientInVM(vm2, null, NetworkUtils.getServerHostName(vm0.getHost()), locatorPort); + putAndWaitForSuccess(vm2, REGION_NAME, "key", "value"); - + Assert.assertEquals("value", getInVM(vm2, "key")); } @@ -205,7 +211,7 @@ public class AutoConnectionSourceDUnitTest extends LocatorTestBase { vm.invoke(new SerializableCallable() { public Object call() throws InterruptedException { MyLocatorCallback callback = (MyLocatorCallback) remoteObjects.get(CALLBACK_KEY); - + boolean discovered = callback.waitForDiscovery(locatorToWaitFor, MAX_WAIT); Assert.assertTrue("Waited " + MAX_WAIT + " for " + locatorToWaitFor + " to be discovered on client. List is now: " @@ -214,89 +220,85 @@ public class AutoConnectionSourceDUnitTest extends LocatorTestBase { } }); } - + public void testServerGroups() throws Exception { final Host host = Host.getHost(0); VM vm0 = host.getVM(0); VM vm1 = host.getVM(1); VM vm2 = host.getVM(2); VM vm3 = host.getVM(3); - + int locatorPort = AvailablePort.getRandomAvailablePort(AvailablePort.SOCKET); - vm0.invoke("Start Locator", () -> startLocator(vm0.getHost(), locatorPort, "")); - + startLocatorInVM(vm0, locatorPort, ""); + String locators = NetworkUtils.getServerHostName(vm0.getHost()) + "[" + locatorPort + "]"; + + startBridgeServerInVM(vm1, new String[] {"group1", "group2"} , locators, new String[] {"A", "B"}); + startBridgeServerInVM(vm2, new String[] {"group2", "group3"}, locators, new String[] {"B", "C"}); - vm1.invoke("Start BridgeServer", () -> startBridgeServer(new String[] { "group1", "group2" }, locators, new String[] { "A", "B" })); - vm2.invoke("Start BridgeServer", () -> startBridgeServer(new String[] { "group2", "group3" }, locators, new String[] { "B", "C" })); - - vm3.invoke("StartBridgeClient", () -> startBridgeClient("group1", NetworkUtils.getServerHostName(vm0.getHost()) - , locatorPort, new String[] { "A", "B", "C" })); + + startBridgeClientInVM(vm3, "group1", NetworkUtils.getServerHostName(vm0.getHost()), locatorPort, new String [] {"A", "B", "C"}); putAndWaitForSuccess(vm3, "A", "key", "value"); Assert.assertEquals("value", getInVM(vm1, "A", "key")); try { putInVM(vm3, "C", "key2", "value2"); fail("Should not have been able to find Region C on the server"); - } catch (Exception expected) { - } - + } catch(Exception expected) {} + stopBridgeMemberVM(vm3); - - vm3.invoke("StartBridgeClient", () -> startBridgeClient("group3", NetworkUtils.getServerHostName(vm0.getHost()), - locatorPort, new String[] { "A", "B", "C" })); + + startBridgeClientInVM(vm3, "group3", NetworkUtils.getServerHostName(vm0.getHost()), locatorPort, new String [] {"A", "B", "C"}); try { putInVM(vm3, "A", "key3", "value"); fail("Should not have been able to find Region A on the server"); - } catch (Exception expected) { - } + } catch(Exception expected) {} putInVM(vm3, "C", "key4", "value"); Assert.assertEquals("value", getInVM(vm2, "C", "key4")); - + stopBridgeMemberVM(vm3); - - vm3.invoke("StartBridgeClient", () -> startBridgeClient("group2", NetworkUtils.getServerHostName(vm0.getHost()), - locatorPort, new String[] { "A", "B", "C" })); + + startBridgeClientInVM(vm3, "group2", NetworkUtils.getServerHostName(vm0.getHost()), locatorPort, new String [] {"A", "B", "C"}); putInVM(vm3, "B", "key5", "value"); Assert.assertEquals("value", getInVM(vm1, "B", "key5")); Assert.assertEquals("value", getInVM(vm2, "B", "key5")); - + stopBridgeMemberVM(vm1); putInVM(vm3, "B", "key6", "value"); Assert.assertEquals("value", getInVM(vm2, "B", "key6")); - vm1.invoke("Start BridgeServer", () -> startBridgeServer(new String[] { "group1", "group2" }, locators, new String[] { "A", "B" })); + startBridgeServerInVM(vm1, new String[] {"group1", "group2"} , locators, new String[] {"A", "B"}); stopBridgeMemberVM(vm2); - + putInVM(vm3, "B", "key7", "value"); Assert.assertEquals("value", getInVM(vm1, "B", "key7")); } - + public void testTwoServersInSameVM() throws Exception { final Host host = Host.getHost(0); VM vm0 = host.getVM(0); VM vm1 = host.getVM(1); VM vm2 = host.getVM(2); - // VM vm3 = host.getVM(3); - +// VM vm3 = host.getVM(3); + int locatorPort = AvailablePort.getRandomAvailablePort(AvailablePort.SOCKET); - - vm0.invoke("Start Locator", () -> startLocator(vm0.getHost(), locatorPort, "")); - + + startLocatorInVM(vm0, locatorPort, ""); + final String locators = NetworkUtils.getServerHostName(vm0.getHost()) + "[" + locatorPort + "]"; - - final int serverPort1 = vm1.invoke("Start BridgeServer", () -> startBridgeServer(new String[] { "group1" }, locators)); - final int serverPort2 = vm1.invoke("Start CacheServer", () -> addCacheServer(new String[] { "group2" })); - - vm2.invoke("StartBridgeClient", () -> startBridgeClient("group2", NetworkUtils.getServerHostName(vm0.getHost()), locatorPort)); - - checkEndpoints(vm2, new int[] { serverPort2 }); - + + final int serverPort1 =startBridgeServerInVM(vm1, new String[] {"group1"}, locators); + final int serverPort2 =addCacheServerInVM(vm1, new String[] {"group2"}); + + startBridgeClientInVM(vm2, "group2", NetworkUtils.getServerHostName(vm0.getHost()), locatorPort); + + checkEndpoints(vm2, new int[] {serverPort2}); + stopBridgeMemberVM(vm2); - vm2.invoke("StartBridgeClient", () -> startBridgeClient("group1", NetworkUtils.getServerHostName(vm0.getHost()), locatorPort)); - - checkEndpoints(vm2, new int[] { serverPort1 }); + startBridgeClientInVM(vm2, "group1", NetworkUtils.getServerHostName(vm0.getHost()), locatorPort); + + checkEndpoints(vm2, new int[] {serverPort1}); } - + public void testClientMembershipListener() throws Exception { final Host host = Host.getHost(0); VM locatorVM = host.getVM(0); @@ -304,20 +306,19 @@ public class AutoConnectionSourceDUnitTest extends LocatorTestBase { VM bridge2VM = host.getVM(2); VM clientVM = host.getVM(3); int locatorPort = AvailablePort.getRandomAvailablePort(AvailablePort.SOCKET); - locatorVM.invoke("Start Locator", () -> startLocator(locatorVM.getHost(), locatorPort, "")); - + startLocatorInVM(locatorVM, locatorPort, ""); String locators = NetworkUtils.getServerHostName(locatorVM.getHost()) + "[" + locatorPort + "]"; //start a bridge server with a listener addBridgeListener(bridge1VM); - int serverPort1 = bridge1VM.invoke("Start BridgeServer", () -> startBridgeServer(null, locators)); + int serverPort1 = startBridgeServerInVM(bridge1VM, null, locators); //start a bridge client with a listener addBridgeListener(clientVM); - clientVM.invoke("StartBridgeClient", () -> startBridgeClient(null, NetworkUtils.getServerHostName(locatorVM.getHost()), locatorPort)); + startBridgeClientInVM(clientVM, null, NetworkUtils.getServerHostName(locatorVM.getHost()), locatorPort); // wait for client to connect - checkEndpoints(clientVM, new int[] { serverPort1 }); - + checkEndpoints(clientVM, new int[] {serverPort1}); + //make sure the client and bridge server both noticed each other waitForJoin(bridge1VM); MyListener serverListener = getBridgeListener(bridge1VM); @@ -325,48 +326,48 @@ public class AutoConnectionSourceDUnitTest extends LocatorTestBase { Assert.assertEquals(0, serverListener.getDepartures()); Assert.assertEquals(1, serverListener.getJoins()); resetBridgeListener(bridge1VM); - + waitForJoin(clientVM); - MyListener clientListener = getBridgeListener(clientVM); + MyListener clientListener= getBridgeListener(clientVM); Assert.assertEquals(0, clientListener.getCrashes()); Assert.assertEquals(0, clientListener.getDepartures()); Assert.assertEquals(1, clientListener.getJoins()); resetBridgeListener(clientVM); - - checkEndpoints(clientVM, new int[] { serverPort1 }); + + checkEndpoints(clientVM, new int[] {serverPort1}); //start another bridge server and make sure it is detected by the client - int serverPort2 = bridge2VM.invoke("Start BridgeServer", () -> startBridgeServer(null, locators)); - - checkEndpoints(clientVM, new int[] { serverPort1, serverPort2 }); + int serverPort2 = startBridgeServerInVM(bridge2VM, null, locators); + + checkEndpoints(clientVM, new int[] {serverPort1, serverPort2}); serverListener = getBridgeListener(bridge1VM); Assert.assertEquals(0, serverListener.getCrashes()); Assert.assertEquals(0, serverListener.getDepartures()); Assert.assertEquals(0, serverListener.getJoins()); resetBridgeListener(bridge1VM); waitForJoin(clientVM); - clientListener = getBridgeListener(clientVM); + clientListener= getBridgeListener(clientVM); Assert.assertEquals(0, clientListener.getCrashes()); Assert.assertEquals(0, clientListener.getDepartures()); Assert.assertEquals(1, clientListener.getJoins()); resetBridgeListener(clientVM); - + //stop the second bridge server and make sure it is detected by the client stopBridgeMemberVM(bridge2VM); - - checkEndpoints(clientVM, new int[] { serverPort1 }); + + checkEndpoints(clientVM, new int[] {serverPort1}); serverListener = getBridgeListener(bridge1VM); Assert.assertEquals(0, serverListener.getCrashes()); Assert.assertEquals(0, serverListener.getDepartures()); Assert.assertEquals(0, serverListener.getJoins()); resetBridgeListener(bridge1VM); waitForCrash(clientVM); - clientListener = getBridgeListener(clientVM); + clientListener= getBridgeListener(clientVM); Assert.assertEquals(1, clientListener.getCrashes()); Assert.assertEquals(0, clientListener.getDepartures()); Assert.assertEquals(0, clientListener.getJoins()); resetBridgeListener(clientVM); - + //stop the client and make sure the bridge server notices stopBridgeMemberVM(clientVM); waitForDeparture(bridge1VM); @@ -379,7 +380,7 @@ public class AutoConnectionSourceDUnitTest extends LocatorTestBase { protected Object getInVM(VM vm, final Serializable key) { return getInVM(vm, REGION_NAME, key); } - + protected Object getInVM(VM vm, final String regionName, final Serializable key) { return vm.invoke(new SerializableCallable("Get in VM") { public Object call() throws Exception { @@ -389,22 +390,23 @@ public class AutoConnectionSourceDUnitTest extends LocatorTestBase { } }); } - - protected void putAndWaitForSuccess(VM vm, final String regionName, final Serializable key, final Serializable value) throws InterruptedException { + + protected void putAndWaitForSuccess(VM vm, final String regionName, final Serializable key, final Serializable value) throws InterruptedException + { long endTime = System.currentTimeMillis() + MAX_WAIT; long remaining = MAX_WAIT; int i = 0; - while (true) { + while(true) { try { System.err.println("Attempt: " + (i++)); putInVM(vm, regionName, key, value); break; } catch (NoAvailableLocatorsException | com.gemstone.gemfire.test.dunit.RMIException e) { - if (!(e instanceof NoAvailableLocatorsException) + if( !(e instanceof NoAvailableLocatorsException) && !(e.getCause() instanceof NoAvailableServersException)) { throw e; } - if (remaining <= 0) { + if(remaining <= 0) { throw e; } Wait.pause(100); @@ -416,8 +418,10 @@ public class AutoConnectionSourceDUnitTest extends LocatorTestBase { protected void putInVM(VM vm, final Serializable key, final Serializable value) { putInVM(vm, REGION_NAME, key, value); } - - protected void putInVM(VM vm, final String regionName, final Serializable key, final Serializable value) { + + + + protected void putInVM(VM vm, final String regionName, final Serializable key, final Serializable value) { vm.invoke(new SerializableCallable("Put in VM") { public Object call() throws Exception { Cache cache = (Cache) remoteObjects.get(CACHE_KEY); @@ -426,12 +430,11 @@ public class AutoConnectionSourceDUnitTest extends LocatorTestBase { } }); } - + /** * Assert that there is one endpoint with the given host in port * on the client vm. - * - * @param vm - the vm the client is running in + * @param vm - the vm the client is running in * @param expectedPorts - The server ports we expect the client to be connected to. */ protected void checkEndpoints(VM vm, final int[] expectedPorts) { @@ -442,14 +445,14 @@ public class AutoConnectionSourceDUnitTest extends LocatorTestBase { List/*<ServerLocation>*/ endpoints; HashSet actualEndpointPorts; HashSet expectedEndpointPorts = new HashSet(); - for (int i = 0; i < expectedPorts.length; i++) { + for(int i = 0; i < expectedPorts.length; i++) { expectedEndpointPorts.add(new Integer(expectedPorts[i])); } do { endpoints = pool.getCurrentServers(); actualEndpointPorts = new HashSet(); - for (Iterator itr = endpoints.iterator(); itr.hasNext(); ) { - ServerLocation sl = (ServerLocation) itr.next(); + for(Iterator itr = endpoints.iterator(); itr.hasNext();) { + ServerLocation sl = (ServerLocation)itr.next(); actualEndpointPorts.add(new Integer(sl.getPort())); } if (expectedEndpointPorts.size() == actualEndpointPorts.size()) { @@ -457,12 +460,12 @@ public class AutoConnectionSourceDUnitTest extends LocatorTestBase { } else { Wait.pause(100); } - } while (retryCount-- > 0); + } while(retryCount-- > 0); Assert.assertEquals(expectedEndpointPorts, actualEndpointPorts); } }); } - + protected void addBridgeListener(VM vm) { vm.invoke(new SerializableRunnable("Add membership listener") { public void run() { @@ -472,7 +475,7 @@ public class AutoConnectionSourceDUnitTest extends LocatorTestBase { } }); } - + protected void resetBridgeListener(VM vm) { vm.invoke(new SerializableRunnable("Add membership listener") { public void run() { @@ -481,7 +484,7 @@ public class AutoConnectionSourceDUnitTest extends LocatorTestBase { } }); } - + private MyListener getBridgeListener(VM vm) { return (MyListener) vm.invoke(new SerializableCallable("Add membership listener") { public Object call() { @@ -489,17 +492,17 @@ public class AutoConnectionSourceDUnitTest extends LocatorTestBase { } }); } - + private void waitForJoin(VM vm) { vm.invoke(new SerializableRunnable() { public void run() { MyListener listener = (MyListener) remoteObjects.get(BRIDGE_LISTENER); - synchronized (listener) { + synchronized(listener) { long end = System.currentTimeMillis() + 10000; while (listener.joins == 0) { try { long remaining = end - System.currentTimeMillis(); - if (remaining < 0) { + if(remaining < 0) { break; } listener.wait(remaining); @@ -511,17 +514,17 @@ public class AutoConnectionSourceDUnitTest extends LocatorTestBase { } }); } - + private void waitForCrash(VM vm) { vm.invoke(new SerializableRunnable() { public void run() { MyListener listener = (MyListener) remoteObjects.get(BRIDGE_LISTENER); - synchronized (listener) { + synchronized(listener) { long end = System.currentTimeMillis() + 10000; - while (listener.crashes == 0) { + while (listener.crashes== 0) { try { long remaining = end - System.currentTimeMillis(); - if (remaining < 0) { + if(remaining < 0) { break; } listener.wait(remaining); @@ -533,17 +536,17 @@ public class AutoConnectionSourceDUnitTest extends LocatorTestBase { } }); } - + private void waitForDeparture(VM vm) { vm.invoke(new SerializableRunnable() { public void run() { MyListener listener = (MyListener) remoteObjects.get(BRIDGE_LISTENER); - synchronized (listener) { + synchronized(listener) { long end = System.currentTimeMillis() + 10000; while (listener.departures == 0) { try { long remaining = end - System.currentTimeMillis(); - if (remaining < 0) { + if(remaining < 0) { break; } listener.wait(remaining); @@ -555,11 +558,11 @@ public class AutoConnectionSourceDUnitTest extends LocatorTestBase { } }); } - + public static class MyListener extends ClientMembershipListenerAdapter implements Serializable { protected int crashes = 0; protected int joins = 0; - protected int departures = 0; + protected int departures= 0; @Override public synchronized void memberCrashed(ClientMembershipEvent event) { @@ -593,7 +596,7 @@ public class AutoConnectionSourceDUnitTest extends LocatorTestBase { return joins; } - public synchronized int getDepartures() { + public synchronized int getDepartures() { return departures; } } http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/d1a0748b/geode-core/src/test/java/com/gemstone/gemfire/cache/client/internal/AutoConnectionSourceImplJUnitTest.java ---------------------------------------------------------------------- diff --git a/geode-core/src/test/java/com/gemstone/gemfire/cache/client/internal/AutoConnectionSourceImplJUnitTest.java b/geode-core/src/test/java/com/gemstone/gemfire/cache/client/internal/AutoConnectionSourceImplJUnitTest.java index 667af89..8deedc1 100644 --- a/geode-core/src/test/java/com/gemstone/gemfire/cache/client/internal/AutoConnectionSourceImplJUnitTest.java +++ b/geode-core/src/test/java/com/gemstone/gemfire/cache/client/internal/AutoConnectionSourceImplJUnitTest.java @@ -144,22 +144,27 @@ public class AutoConnectionSourceImplJUnitTest { @Test public void testNoServers() throws Exception { + startFakeLocator(); handler.nextConnectionResponse = new ClientConnectionResponse(null); + assertEquals(null, source.findServer(null)); } @Test public void testDiscoverServers() throws Exception { startFakeLocator(); + ServerLocation loc1 = new ServerLocation("localhost", 4423); handler.nextConnectionResponse = new ClientConnectionResponse(loc1); + assertEquals(loc1, source.findServer(null)); } @Test public void testDiscoverLocators() throws Exception { startFakeLocator(); + int secondPort = AvailablePortHelper.getRandomAvailableTCPPort(); TcpServer server2 = new TcpServer(secondPort, InetAddress.getLocalHost(), null, null, handler, new FakeHelper(), Thread.currentThread().getThreadGroup(), "tcp server"); server2.start();