Skip to content

Commit be7138b

Browse files
committed
Refactor temp directory setup for JmxTest.
1 parent 8c78daa commit be7138b

File tree

1 file changed

+29
-29
lines changed
  • substratevm/src/com.oracle.svm.test/src/com/oracle/svm/test/jmx

1 file changed

+29
-29
lines changed

substratevm/src/com.oracle.svm.test/src/com/oracle/svm/test/jmx/JmxTest.java

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,14 @@ public class JmxTest {
8181
static final String ACCESS_PROPERTY = "com.sun.management.jmxremote.access.file";
8282
static final String PASSWORD_PROPERTY = "com.sun.management.jmxremote.password.file";
8383
static final String SSL_PROPERTY = "com.sun.management.jmxremote.ssl";
84-
static final String KEYSTORE_PROPERTY = "javax.net.ssl.keyStore";
84+
static final String KEYSTORE_FILENAME = "clientkeystore";
85+
static final String KEYSTORE_PASSWORD = "clientpass";
8586
static final String KEYSTORE_PASSWORD_PROPERTY = "javax.net.ssl.keyStorePassword";
86-
static final String TRUSTSTORE_PROPERTY = "javax.net.ssl.trustStore";
87+
static final String KEYSTORE_PROPERTY = "javax.net.ssl.keyStore";
88+
static final String TRUSTSTORE_FILENAME = "servertruststore";
89+
static final String TRUSTSTORE_PASSWORD = "servertrustpass";
8790
static final String TRUSTSTORE_PASSWORD_PROPERTY = "javax.net.ssl.trustStorePassword";
91+
static final String TRUSTSTORE_PROPERTY = "javax.net.ssl.trustStore";
8892
static final String REGISTRY_SSL_PROPERTY = "com.sun.management.jmxremote.registry.ssl";
8993
static final String SOCKET_FACTORY_PROPERTY = "com.sun.jndi.rmi.factory.socket";
9094
static final String TEST_PORT = "12345";
@@ -104,34 +108,30 @@ public static void setup() throws IOException {
104108
System.setProperty(SSL_PROPERTY, TRUE);
105109
System.setProperty(REGISTRY_SSL_PROPERTY, TRUE);
106110

111+
// Prepare temp directory with files required for testing authentification.
107112
Path tempDirectory = Files.createTempDirectory("jmxtest");
113+
Path jmxRemoteAccess = tempDirectory.resolve("jmxremote.access");
114+
Path jmxRemotePassword = tempDirectory.resolve("jmxremote.password");
115+
Path clientKeyStore = tempDirectory.resolve(KEYSTORE_FILENAME);
116+
Path serverTrustStore = tempDirectory.resolve(TRUSTSTORE_FILENAME);
108117

109-
// Generate SSL keystore, client cert, and truststore
118+
// Generate SSL keystore, client cert, and truststore for testing SSL connection.
110119
createClientKey(tempDirectory);
111120
createClientCert(tempDirectory);
121+
assertTrue("Failed to create " + KEYSTORE_FILENAME, Files.exists(clientKeyStore));
122+
System.setProperty(KEYSTORE_PROPERTY, clientKeyStore.toString());
123+
System.setProperty(KEYSTORE_PASSWORD_PROPERTY, KEYSTORE_PASSWORD);
112124
createServerTrustStore(tempDirectory);
113-
// Copy resources into tempDirectory
114-
Path jmxRemoteAccess = tempDirectory.resolve("jmxremote.access");
115-
Path jmxRemotePassword = tempDirectory.resolve("jmxremote.password");
116-
Path clientkeystore = tempDirectory.resolve("clientkeystore");
117-
Path servertruststore = tempDirectory.resolve("servertruststore");
118-
// Note: full paths are used to ensure analysis includes the resources automatically
119-
Files.writeString(jmxRemoteAccess, TEST_ROLE + " readwrite");
120-
Files.writeString(jmxRemotePassword, TEST_ROLE + " " + TEST_ROLE_PASSWORD);
125+
assertTrue("Failed to create " + TRUSTSTORE_FILENAME, Files.exists(serverTrustStore));
126+
System.setProperty(TRUSTSTORE_PROPERTY, serverTrustStore.toString());
127+
System.setProperty(TRUSTSTORE_PASSWORD_PROPERTY, TRUSTSTORE_PASSWORD);
121128

122-
// The following are dummy password and access files required for testing authentication.
129+
// The following are dummy access and password files required for testing authentication.
130+
Files.writeString(jmxRemoteAccess, TEST_ROLE + " readwrite");
123131
System.setProperty(ACCESS_PROPERTY, jmxRemoteAccess.toString());
132+
Files.writeString(jmxRemotePassword, TEST_ROLE + " " + TEST_ROLE_PASSWORD);
124133
System.setProperty(PASSWORD_PROPERTY, jmxRemotePassword.toString());
125134

126-
/*
127-
* The following are dummy SSL keystore and truststore files required for testing connection
128-
* using SSL. See resources/jmxremote/README.md for more information.
129-
*/
130-
System.setProperty(KEYSTORE_PROPERTY, clientkeystore.toString());
131-
System.setProperty(KEYSTORE_PASSWORD_PROPERTY, "clientpass");
132-
System.setProperty(TRUSTSTORE_PROPERTY, servertruststore.toString());
133-
System.setProperty(TRUSTSTORE_PASSWORD_PROPERTY, "servertrustpass");
134-
135135
// Password file must have restricted access.
136136
Files.setPosixFilePermissions(jmxRemotePassword, Set.of(PosixFilePermission.OWNER_READ, PosixFilePermission.OWNER_WRITE));
137137

@@ -140,35 +140,35 @@ public static void setup() throws IOException {
140140
ManagementAgentStartupHook startupHook = new ManagementAgentStartupHook();
141141
startupHook.execute(false);
142142
} catch (Exception e) {
143-
Assert.fail("Failed to start server Cause: " + e.getMessage());
143+
Assert.fail("Failed to start server. Cause: " + e.getMessage());
144144
}
145145
}
146146

147147
private static void createClientKey(Path tempDirectory) throws IOException {
148148
runCommand(tempDirectory, List.of("keytool", "-genkey",
149-
"-keystore", "clientkeystore",
149+
"-keystore", KEYSTORE_FILENAME,
150150
"-alias", "clientkey",
151-
"-storepass", "clientpass",
152-
"-keypass", "clientpass",
151+
"-storepass", KEYSTORE_PASSWORD,
152+
"-keypass", KEYSTORE_PASSWORD,
153153
"-dname", "CN=test, OU=test, O=test, L=test, ST=test, C=test, EMAILADDRESS=test",
154154
"-validity", "99999",
155155
"-keyalg", "rsa"));
156156
}
157157

158158
private static void createClientCert(Path tempDirectory) throws IOException {
159159
runCommand(tempDirectory, List.of("keytool", "-exportcert",
160-
"-keystore", "clientkeystore",
160+
"-keystore", KEYSTORE_FILENAME,
161161
"-alias", "clientkey",
162-
"-storepass", "clientpass",
162+
"-storepass", KEYSTORE_PASSWORD,
163163
"-file", "client.cer"));
164164
}
165165

166166
private static void createServerTrustStore(Path tempDirectory) throws IOException {
167167
runCommand(tempDirectory, List.of("keytool", "-importcert",
168168
"-noprompt",
169169
"-file", "client.cer",
170-
"-keystore", "servertruststore",
171-
"-storepass", "servertrustpass"));
170+
"-keystore", TRUSTSTORE_FILENAME,
171+
"-storepass", TRUSTSTORE_PASSWORD));
172172
}
173173

174174
private static void runCommand(Path tempDirectory, List<String> command) throws IOException {

0 commit comments

Comments
 (0)