Skip to content

Commit

Permalink
Android IceSSL testing fixes - fix #3288 (#3323)
Browse files Browse the repository at this point in the history
  • Loading branch information
pepone authored Jan 9, 2025
1 parent b4ca088 commit 5332063
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion java/test/android/controller/gradle.properties
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}

Expand Down
26 changes: 15 additions & 11 deletions java/test/src/main/java/test/Ice/idleTimeout/AllTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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();
}
Expand Down Expand Up @@ -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();
Expand All @@ -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.
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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();

Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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());

Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion java/test/src/main/java/test/Ice/timeout/AllTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
25 changes: 25 additions & 0 deletions java/test/src/main/java/test/TestHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
1 change: 0 additions & 1 deletion scripts/Util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 5332063

Please sign in to comment.