Skip to content

Commit

Permalink
Fix android IceSSL testing - #3288
Browse files Browse the repository at this point in the history
  • Loading branch information
pepone committed Dec 23, 2024
1 parent 075234d commit 1d83b9a
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,14 @@ public InitializationData clone() {
* the SSLEngineFactory.
*/
public SSLEngineFactory clientSSLEngineFactory;

/**
* A user-supplied function used to loads resources (e.g., key stores, trust stores) given a
* resource identifier or path. If this field is non-null, the provided function is used to load
* resources. Otherwise, the default loader (using the classpath and file system) is used.
*
* <p>If the function itself returns {@code null} when attempting to load a particular resource,
* this class will automatically fall back to the default resource loader.
*/
public java.util.function.Function<String, java.io.InputStream> resourceLoader;
}
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,7 @@ public void load(String file) {
} else {
java.io.PushbackInputStream is = null;
try {
java.io.InputStream f = Util.openResource(getClass().getClassLoader(), file);
java.io.InputStream f = Util.openResource(null, getClass().getClassLoader(), file);
if (f == null) {
throw new FileException("failed to open '" + file + "'");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,11 @@ void trustManagerFailure(boolean incoming, CertificateException ex)
}

private java.io.InputStream openResource(String path) throws java.io.IOException {
java.io.InputStream stream;

com.zeroc.Ice.InitializationData initData =
_communicator.getInstance().initializationData();

boolean isAbsolute = false;
try {
new java.net.URL(path);
Expand All @@ -485,8 +490,9 @@ 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);
stream =
com.zeroc.Ice.Util.openResource(
initData.resourceLoader, getClass().getClassLoader(), path);

//
// If the first attempt fails and IceSSL.DefaultDir is defined and the original
Expand All @@ -497,6 +503,7 @@ private java.io.InputStream openResource(String path) throws java.io.IOException
if (stream == null && !_defaultDir.isEmpty() && !isAbsolute) {
stream =
com.zeroc.Ice.Util.openResource(
initData.resourceLoader,
getClass().getClassLoader(),
_defaultDir + java.io.File.separator + path);
}
Expand Down
20 changes: 17 additions & 3 deletions java/src/com.zeroc.ice/src/main/java/com/zeroc/Ice/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -569,22 +569,35 @@ public static ProtocolPluginFacade getProtocolPluginFacade(Communicator communic
}

/**
* Given a path name, first try to open it as a class path resource (the path is treated as
* Given a path name, first try to open it using the provided resource loader. If the resource
* loader is null or if it returns null try as a class path resource (the path is treated as
* absolute). If that fails, fall back to the file system. Returns null if the file does not
* exist and raises IOException if an error occurs.
*
* @hidden Public because it's used by SSL.
*/
public static java.io.InputStream openResource(ClassLoader cl, String path)
public static java.io.InputStream openResource(
java.util.function.Function<String, java.io.InputStream> resourceLoader,
ClassLoader cl,
String path)
throws java.io.IOException {

java.io.InputStream stream = null;
// Try the resource loader first if one is provided.
if (resourceLoader != null) {
stream = resourceLoader.apply(path);
if (stream != null) {
return stream;
}
}

//
// Calling getResourceAsStream on the class loader means all paths are absolute,
// whereas calling it on the class means all paths are relative to the class
// unless the path has a leading forward slash. We call it on the class loader.
//
// getResourceAsStream returns null if the resource can't be found.
//
java.io.InputStream stream = null;
try {
stream = cl.getResourceAsStream(path);
} catch (IllegalArgumentException ex) {
Expand All @@ -598,6 +611,7 @@ public static java.io.InputStream openResource(ClassLoader cl, String path)
// java.io.InputStream in = Util.openResource(cl, "c:\\foo.txt");
//
}

if (stream == null) {
try {
java.io.File f = new java.io.File(path);
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
11 changes: 11 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,15 @@ public Communicator initialize(InitializationData initData) {
initData.classLoader = _classLoader;
}

if (isAndroid()) {
initData.resourceLoader =
(String path) -> {
return _controllerHelper != null
? _controllerHelper.loadResource(path)
: null;
};
}

Communicator communicator = Util.initialize(initData);
if (_communicator == null) {
_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 1d83b9a

Please sign in to comment.