From 5332063e29516fff501ea4bc54eb9c9ec9f772cf Mon Sep 17 00:00:00 2001 From: Jose Date: Thu, 9 Jan 2025 16:45:21 +0100 Subject: [PATCH] Android IceSSL testing fixes - fix #3288 (#3323) --- .../java/com/zeroc/Ice/SSL/SSLEngine.java | 16 ++++++----- .../test/android/controller/gradle.properties | 2 +- .../testcontroller/ControllerActivity.java | 2 +- .../zeroc/testcontroller/ControllerApp.java | 28 +++++++++---------- .../java/test/Ice/idleTimeout/AllTests.java | 26 +++++++++-------- .../test/Ice/inactivityTimeout/AllTests.java | 7 ++--- .../main/java/test/Ice/timeout/AllTests.java | 2 +- java/test/src/main/java/test/TestHelper.java | 25 +++++++++++++++++ scripts/Util.py | 1 - 9 files changed, 68 insertions(+), 41 deletions(-) diff --git a/java/src/com.zeroc.ice/src/main/java/com/zeroc/Ice/SSL/SSLEngine.java b/java/src/com.zeroc.ice/src/main/java/com/zeroc/Ice/SSL/SSLEngine.java index 806144c9575..5dab6467efd 100644 --- a/java/src/com.zeroc.ice/src/main/java/com/zeroc/Ice/SSL/SSLEngine.java +++ b/java/src/com.zeroc.ice/src/main/java/com/zeroc/Ice/SSL/SSLEngine.java @@ -485,20 +485,22 @@ private java.io.InputStream openResource(String path) throws java.io.IOException isAbsolute = f.isAbsolute(); } - java.io.InputStream stream = - com.zeroc.Ice.Util.openResource(getClass().getClassLoader(), path); + com.zeroc.Ice.InitializationData initData = + _communicator.getInstance().initializationData(); + + ClassLoader classLoader = + initData.classLoader != null ? initData.classLoader : getClass().getClassLoader(); + + java.io.InputStream stream = com.zeroc.Ice.Util.openResource(classLoader, path); // // If the first attempt fails and IceSSL.DefaultDir is defined and the original - // path is - // relative, - // we prepend the default directory and try again. + // path is relative, we prepend the default directory and try again. // if (stream == null && !_defaultDir.isEmpty() && !isAbsolute) { stream = com.zeroc.Ice.Util.openResource( - getClass().getClassLoader(), - _defaultDir + java.io.File.separator + path); + classLoader, _defaultDir + java.io.File.separator + path); } if (stream != null) { diff --git a/java/test/android/controller/gradle.properties b/java/test/android/controller/gradle.properties index fc6cf34985f..8f57b81092b 100644 --- a/java/test/android/controller/gradle.properties +++ b/java/test/android/controller/gradle.properties @@ -1,7 +1,7 @@ # Copyright (c) ZeroC, Inc. All rights reserved. # Version used in JAR files -iceVersion = 3.8.0-alpha0 +iceVersion = 3.8.0-alpha.0 # Set iceHome to location of Ice installation if Ice was installed in a # non-standard location. You must use forward slashes in the path, even on diff --git a/java/test/android/controller/src/main/java/com/zeroc/testcontroller/ControllerActivity.java b/java/test/android/controller/src/main/java/com/zeroc/testcontroller/ControllerActivity.java index 825b8dae4ec..78192357aab 100644 --- a/java/test/android/controller/src/main/java/com/zeroc/testcontroller/ControllerActivity.java +++ b/java/test/android/controller/src/main/java/com/zeroc/testcontroller/ControllerActivity.java @@ -131,7 +131,7 @@ public void onNothingSelected(AdapterView arg0) // Start the controller in a background thread. Starting the controller creates the ObjectAdapter which makes // IO calls. Android doesn't allow making IO calls from the main thread. - Executor executor = Executors.newSingleThreadExecutor(); + ExecutorService executor = Executors.newSingleThreadExecutor(); executor.submit(() -> { try { app.startController(this, bluetooth); diff --git a/java/test/android/controller/src/main/java/com/zeroc/testcontroller/ControllerApp.java b/java/test/android/controller/src/main/java/com/zeroc/testcontroller/ControllerApp.java index 7d0f4d0986b..634eb560886 100644 --- a/java/test/android/controller/src/main/java/com/zeroc/testcontroller/ControllerApp.java +++ b/java/test/android/controller/src/main/java/com/zeroc/testcontroller/ControllerApp.java @@ -281,21 +281,19 @@ public ControllerHelperI(TestSuiteBundle bundle, String[] args) { _args = args; } - public void communicatorInitialized(Communicator communicator) { - com.zeroc.Ice.Properties properties = communicator.getProperties(); - if (properties - .getIceProperty("Ice.Plugin.IceSSL") - .equals("com.zeroc.IceSSL.PluginFactory")) { - com.zeroc.Ice.SSL.Plugin plugin = - (com.zeroc.Ice.SSL.Plugin) - communicator.getPluginManager().getPlugin("IceSSL"); - String keystore = communicator.getProperties().getIceProperty("IceSSL.Keystore"); - properties.setProperty("IceSSL.Keystore", ""); - int resource = keystore.equals("client.bks") ? R.raw.client : R.raw.server; - java.io.InputStream certs = getResources().openRawResource(resource); - plugin.setKeystoreStream(certs); - plugin.setTruststoreStream(certs); - communicator.getPluginManager().initializePlugins(); + public void communicatorInitialized(Communicator communicator) {} + + public java.io.InputStream loadResource(String path) { + switch (path) { + case "server.bks" -> { + return getResources().openRawResource(R.raw.server); + } + case "client.bks" -> { + return getResources().openRawResource(R.raw.client); + } + default -> { + return null; + } } } diff --git a/java/test/src/main/java/test/Ice/idleTimeout/AllTests.java b/java/test/src/main/java/test/Ice/idleTimeout/AllTests.java index 982cc80473f..9ff5e0bc2a6 100644 --- a/java/test/src/main/java/test/Ice/idleTimeout/AllTests.java +++ b/java/test/src/main/java/test/Ice/idleTimeout/AllTests.java @@ -7,9 +7,9 @@ import com.zeroc.Ice.ConnectionLostException; import com.zeroc.Ice.InitializationData; import com.zeroc.Ice.Properties; -import com.zeroc.Ice.Util; import test.Ice.idleTimeout.Test.TestIntfPrx; +import test.TestHelper; import java.io.PrintWriter; @@ -25,13 +25,13 @@ static void allTests(test.TestHelper helper) { testIdleCheckDoesNotAbortBackPressuredConnection(p, helper.getWriter()); testConnectionAbortedByIdleCheck( - proxyStringDefaultMax, communicator.getProperties(), helper.getWriter()); + helper, proxyStringDefaultMax, communicator.getProperties(), helper.getWriter()); testEnableDisableIdleCheck( - true, proxyString3s, communicator.getProperties(), helper.getWriter()); + helper, true, proxyString3s, communicator.getProperties(), helper.getWriter()); testEnableDisableIdleCheck( - false, proxyString3s, communicator.getProperties(), helper.getWriter()); + helper, false, proxyString3s, communicator.getProperties(), helper.getWriter()); testNoIdleTimeout( - proxyStringNoIdleTimeout, communicator.getProperties(), helper.getWriter()); + helper, proxyStringNoIdleTimeout, communicator.getProperties(), helper.getWriter()); p.shutdown(); } @@ -66,7 +66,7 @@ private static void testIdleCheckDoesNotAbortBackPressuredConnection( // We intentionally misconfigure the client with an idle timeout of 3s to send heartbeats every // 1.5s, which is too long to prevent the server from aborting the connection. private static void testConnectionAbortedByIdleCheck( - String proxyString, Properties properties, PrintWriter output) { + TestHelper helper, String proxyString, Properties properties, PrintWriter output) { output.write( "testing that the idle check aborts a connection that does not receive anything for 1s... "); output.flush(); @@ -77,7 +77,7 @@ private static void testConnectionAbortedByIdleCheck( properties.setProperty("Ice.Warn.Connections", "0"); var initData = new InitializationData(); initData.properties = properties; - try (var communicator = Util.initialize(initData)) { + try (var communicator = helper.initialize(initData)) { var p = TestIntfPrx.createProxy(communicator, proxyString); // Establish connection. @@ -100,7 +100,11 @@ private static void testConnectionAbortedByIdleCheck( // Verifies the behavior with the idle check enabled or disabled when the client and the server // have mismatched idle timeouts (here: 3s on the server side and 1s on the client side). private static void testEnableDisableIdleCheck( - boolean enabled, String proxyString, Properties properties, PrintWriter output) { + TestHelper helper, + boolean enabled, + String proxyString, + Properties properties, + PrintWriter output) { String enabledString = enabled ? "enabled" : "disabled"; output.write("testing connection with idle check " + enabledString + "... "); output.flush(); @@ -112,7 +116,7 @@ private static void testEnableDisableIdleCheck( properties.setProperty("Ice.Warn.Connections", "0"); var initData = new InitializationData(); initData.properties = properties; - try (var communicator = Util.initialize(initData)) { + try (var communicator = helper.initialize(initData)) { var p = TestIntfPrx.createProxy(communicator, proxyString); var connection = p.ice_getConnection(); @@ -130,7 +134,7 @@ private static void testEnableDisableIdleCheck( // Verifies the idle check is disabled when the idle timeout is set to 0. private static void testNoIdleTimeout( - String proxyString, Properties properties, PrintWriter output) { + TestHelper helper, String proxyString, Properties properties, PrintWriter output) { output.write("testing connection with idle timeout set to 0... "); output.flush(); @@ -139,7 +143,7 @@ private static void testNoIdleTimeout( properties.setProperty("Ice.Connection.Client.IdleTimeout", "0"); var initData = new InitializationData(); initData.properties = properties; - try (var communicator = Util.initialize(initData)) { + try (var communicator = helper.initialize(initData)) { var p = TestIntfPrx.createProxy(communicator, proxyString); var connection = p.ice_getConnection(); test(connection != null); diff --git a/java/test/src/main/java/test/Ice/inactivityTimeout/AllTests.java b/java/test/src/main/java/test/Ice/inactivityTimeout/AllTests.java index de61f3f9d75..7927cc9951d 100644 --- a/java/test/src/main/java/test/Ice/inactivityTimeout/AllTests.java +++ b/java/test/src/main/java/test/Ice/inactivityTimeout/AllTests.java @@ -6,7 +6,6 @@ import com.zeroc.Ice.Connection; import com.zeroc.Ice.InitializationData; import com.zeroc.Ice.Properties; -import com.zeroc.Ice.Util; import test.Ice.inactivityTimeout.Test.TestIntfPrx; @@ -22,7 +21,7 @@ static void allTests(test.TestHelper helper) { testClientInactivityTimeout(p, helper.getWriter()); testServerInactivityTimeout( - proxyString3s, communicator.getProperties(), helper.getWriter()); + helper, proxyString3s, communicator.getProperties(), helper.getWriter()); testWithOutstandingRequest(p, false, helper.getWriter()); testWithOutstandingRequest(p, true, helper.getWriter()); @@ -53,7 +52,7 @@ private static void testClientInactivityTimeout(TestIntfPrx p, PrintWriter outpu } private static void testServerInactivityTimeout( - String proxyString, Properties properties, PrintWriter output) { + test.TestHelper helper, String proxyString, Properties properties, PrintWriter output) { output.write( "testing that the server side inactivity timeout shuts down the connection... "); output.flush(); @@ -63,7 +62,7 @@ private static void testServerInactivityTimeout( properties.setProperty("Ice.Connection.Client.InactivityTimeout", "5"); var initData = new InitializationData(); initData.properties = properties; - try (var communicator = Util.initialize(initData)) { + try (var communicator = helper.initialize(initData)) { TestIntfPrx p = TestIntfPrx.uncheckedCast(communicator.stringToProxy(proxyString)); p.ice_ping(); diff --git a/java/test/src/main/java/test/Ice/timeout/AllTests.java b/java/test/src/main/java/test/Ice/timeout/AllTests.java index d399d3af7d2..31ca123f6bb 100644 --- a/java/test/src/main/java/test/Ice/timeout/AllTests.java +++ b/java/test/src/main/java/test/Ice/timeout/AllTests.java @@ -91,7 +91,7 @@ public static void allTestsWithController(test.TestHelper helper, ControllerPrx properties.setProperty("Ice.Connection.Client.ConnectTimeout", "-1"); var initData = new InitializationData(); initData.properties = properties; - try (var communicator2 = com.zeroc.Ice.Util.initialize(initData)) { + try (var communicator2 = helper.initialize(initData)) { TimeoutPrx to = TimeoutPrx.uncheckedCast(communicator2.stringToProxy(sref)); controller.holdAdapter(100); try { diff --git a/java/test/src/main/java/test/TestHelper.java b/java/test/src/main/java/test/TestHelper.java index bd1feaace0c..0f7ad4014a7 100644 --- a/java/test/src/main/java/test/TestHelper.java +++ b/java/test/src/main/java/test/TestHelper.java @@ -16,6 +16,8 @@ public static void test(boolean b) { public interface ControllerHelper { void communicatorInitialized(Communicator c); + java.io.InputStream loadResource(String name); + void serverReady(); } @@ -127,6 +129,14 @@ public Communicator initialize(InitializationData initData) { initData.classLoader = _classLoader; } + if (isAndroid()) { + if (initData.classLoader == null) { + initData.classLoader = new AndroidClassLoader(getClass().getClassLoader()); + } else { + initData.classLoader = new AndroidClassLoader(initData.classLoader); + } + } + Communicator communicator = Util.initialize(initData); if (_communicator == null) { _communicator = communicator; @@ -179,6 +189,21 @@ public void shutdown() { } } + public class AndroidClassLoader extends ClassLoader { + public AndroidClassLoader(ClassLoader parent) { + super(parent); + } + + @Override + public java.io.InputStream getResourceAsStream(String name) { + java.io.InputStream resource = _controllerHelper.loadResource(name); + if (resource == null) { + resource = super.getResourceAsStream(name); + } + return resource; + } + } + private ControllerHelper _controllerHelper; private ClassLoader _classLoader; private Communicator _communicator; diff --git a/scripts/Util.py b/scripts/Util.py index 2b3b30c9707..c2ec6f0ed04 100644 --- a/scripts/Util.py +++ b/scripts/Util.py @@ -3780,7 +3780,6 @@ def getSSLProps(self, process, current): { "IceSSL.KeystoreType": "BKS", "IceSSL.TruststoreType": "BKS", - "Ice.InitPlugins": "0", "IceSSL.Keystore": "server.bks" if isinstance(process, Server) else "client.bks",