42
42
import java .lang .management .OperatingSystemMXBean ;
43
43
import java .lang .management .RuntimeMXBean ;
44
44
import java .lang .management .ThreadMXBean ;
45
+ import java .nio .file .DirectoryStream ;
45
46
import java .nio .file .Files ;
46
47
import java .nio .file .Path ;
47
48
import java .nio .file .attribute .PosixFilePermission ;
61
62
import javax .rmi .ssl .SslRMIClientSocketFactory ;
62
63
63
64
import org .graalvm .nativeimage .ImageInfo ;
65
+ import org .junit .AfterClass ;
64
66
import org .junit .Assert ;
65
67
import org .junit .BeforeClass ;
66
68
import org .junit .Test ;
@@ -96,6 +98,8 @@ public class JmxTest {
96
98
static final String TEST_ROLE_PASSWORD = "MYTESTP@SSWORD" ;
97
99
static final String TRUE = "true" ;
98
100
101
+ private static Path tempDirectory ;
102
+
99
103
@ BeforeClass
100
104
public static void setup () throws IOException {
101
105
assumeTrue ("skipping JMX tests" , !ImageInfo .inImageCode () ||
@@ -109,19 +113,19 @@ public static void setup() throws IOException {
109
113
System .setProperty (REGISTRY_SSL_PROPERTY , TRUE );
110
114
111
115
// Prepare temp directory with files required for testing authentication.
112
- Path tempDirectory = Files .createTempDirectory ("jmxtest" );
116
+ tempDirectory = Files .createTempDirectory ("jmxtest" );
113
117
Path jmxRemoteAccess = tempDirectory .resolve ("jmxremote.access" );
114
118
Path jmxRemotePassword = tempDirectory .resolve ("jmxremote.password" );
115
119
Path clientKeyStore = tempDirectory .resolve (KEYSTORE_FILENAME );
116
120
Path serverTrustStore = tempDirectory .resolve (TRUSTSTORE_FILENAME );
117
121
118
122
// Generate SSL keystore, client cert, and truststore for testing SSL connection.
119
- createClientKey (tempDirectory );
120
- createClientCert (tempDirectory );
123
+ createClientKey ();
124
+ createClientCert ();
121
125
assertTrue ("Failed to create " + KEYSTORE_FILENAME , Files .exists (clientKeyStore ));
122
126
System .setProperty (KEYSTORE_PROPERTY , clientKeyStore .toString ());
123
127
System .setProperty (KEYSTORE_PASSWORD_PROPERTY , KEYSTORE_PASSWORD );
124
- createServerTrustStore (tempDirectory );
128
+ createServerTrustStore ();
125
129
assertTrue ("Failed to create " + TRUSTSTORE_FILENAME , Files .exists (serverTrustStore ));
126
130
System .setProperty (TRUSTSTORE_PROPERTY , serverTrustStore .toString ());
127
131
System .setProperty (TRUSTSTORE_PASSWORD_PROPERTY , TRUSTSTORE_PASSWORD );
@@ -144,8 +148,26 @@ public static void setup() throws IOException {
144
148
}
145
149
}
146
150
147
- private static void createClientKey (Path tempDirectory ) throws IOException {
148
- runCommand (tempDirectory , List .of ("keytool" , "-genkey" ,
151
+ @ AfterClass
152
+ public static void teardown () throws IOException {
153
+ if (tempDirectory != null ) {
154
+ delete (tempDirectory );
155
+ }
156
+ }
157
+
158
+ private static void delete (Path file ) throws IOException {
159
+ if (Files .isDirectory (file )) {
160
+ try (DirectoryStream <Path > children = Files .newDirectoryStream (file )) {
161
+ for (Path child : children ) {
162
+ delete (child );
163
+ }
164
+ }
165
+ }
166
+ Files .deleteIfExists (file );
167
+ }
168
+
169
+ private static void createClientKey () throws IOException {
170
+ runCommand (List .of ("keytool" , "-genkey" ,
149
171
"-keystore" , KEYSTORE_FILENAME ,
150
172
"-alias" , "clientkey" ,
151
173
"-storepass" , KEYSTORE_PASSWORD ,
@@ -155,23 +177,23 @@ private static void createClientKey(Path tempDirectory) throws IOException {
155
177
"-keyalg" , "rsa" ));
156
178
}
157
179
158
- private static void createClientCert (Path tempDirectory ) throws IOException {
159
- runCommand (tempDirectory , List .of ("keytool" , "-exportcert" ,
180
+ private static void createClientCert () throws IOException {
181
+ runCommand (List .of ("keytool" , "-exportcert" ,
160
182
"-keystore" , KEYSTORE_FILENAME ,
161
183
"-alias" , "clientkey" ,
162
184
"-storepass" , KEYSTORE_PASSWORD ,
163
185
"-file" , "client.cer" ));
164
186
}
165
187
166
- private static void createServerTrustStore (Path tempDirectory ) throws IOException {
167
- runCommand (tempDirectory , List .of ("keytool" , "-importcert" ,
188
+ private static void createServerTrustStore () throws IOException {
189
+ runCommand (List .of ("keytool" , "-importcert" ,
168
190
"-noprompt" ,
169
191
"-file" , "client.cer" ,
170
192
"-keystore" , TRUSTSTORE_FILENAME ,
171
193
"-storepass" , TRUSTSTORE_PASSWORD ));
172
194
}
173
195
174
- private static void runCommand (Path tempDirectory , List <String > command ) throws IOException {
196
+ private static void runCommand (List <String > command ) throws IOException {
175
197
ProcessBuilder pb = new ProcessBuilder ().command (command );
176
198
pb .directory (tempDirectory .toFile ());
177
199
final Process process = pb .start ();
0 commit comments