author | chegar |
Wed, 03 Dec 2014 14:22:58 +0000 | |
changeset 27565 | 729f9700483a |
parent 23031 | 995cf5c2eb55 |
child 31715 | 4e942fbc0bf6 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
21408
6468b29c9877
8027058: sun/management/jmxremote/bootstrap/RmiBootstrapTest.sh Failed to initialize connector
jbachorik
parents:
5506
diff
changeset
|
2 |
* Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. |
|
8 |
* |
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
13 |
* accompanied this code). |
|
14 |
* |
|
15 |
* You should have received a copy of the GNU General Public License version |
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 |
* |
|
5506 | 19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
20 |
* or visit www.oracle.com if you need additional information or have any |
|
21 |
* questions. |
|
2 | 22 |
*/ |
23 |
import sun.management.jmxremote.ConnectorBootstrap; |
|
24 |
||
25 |
import java.io.File; |
|
26 |
import java.io.FileInputStream; |
|
27 |
import java.io.FilenameFilter; |
|
28 |
import java.io.IOException; |
|
21408
6468b29c9877
8027058: sun/management/jmxremote/bootstrap/RmiBootstrapTest.sh Failed to initialize connector
jbachorik
parents:
5506
diff
changeset
|
29 |
import java.net.BindException; |
6468b29c9877
8027058: sun/management/jmxremote/bootstrap/RmiBootstrapTest.sh Failed to initialize connector
jbachorik
parents:
5506
diff
changeset
|
30 |
import java.rmi.server.ExportException; |
2 | 31 |
|
32 |
import java.util.Properties; |
|
33 |
import java.util.Iterator; |
|
34 |
import java.util.Set; |
|
35 |
import java.util.ArrayList; |
|
36 |
import java.util.HashMap; |
|
37 |
import java.util.Map; |
|
38 |
import java.util.Enumeration; |
|
39 |
||
40 |
import javax.management.remote.*; |
|
41 |
import javax.management.*; |
|
42 |
||
43 |
import sun.management.AgentConfigurationError; |
|
44 |
||
45 |
/** |
|
46 |
* <p>This class implements unit test for RMI Bootstrap. |
|
47 |
* When called with no arguments main() looks in the directory indicated |
|
48 |
* by the "test.src" system property for files called management*ok.properties |
|
49 |
* or management*ko.properties. The *ok.properties files are assumed to be |
|
50 |
* valid Java M&M config files for which the bootstrap should succeed. |
|
51 |
* The *ko.properties files are assumed to be configurations for which the |
|
52 |
* bootstrap & connection test will fail.</p> |
|
53 |
* |
|
54 |
* <p>The rmi port number can be specified with the "rmi.port" system property. |
|
21408
6468b29c9877
8027058: sun/management/jmxremote/bootstrap/RmiBootstrapTest.sh Failed to initialize connector
jbachorik
parents:
5506
diff
changeset
|
55 |
* If not, this test will use the first available port</p> |
2 | 56 |
* |
57 |
* <p>When called with some argument, the main() will interprete its args to |
|
58 |
* be Java M&M configuration file names. The filenames are expected to end |
|
59 |
* with ok.properties or ko.properties - and are interpreted as above.</p> |
|
60 |
* |
|
61 |
* <p>Note that a limitation of the RMI registry (bug 4267864) prevent |
|
62 |
* this test from succeeding if more than 1 configuration is used. |
|
63 |
* As long as 4267864 isn't fix, this test must be called as many times |
|
64 |
* as needed but with a single argument (no arguments, or several arguments |
|
65 |
* will fail).</p> |
|
66 |
* |
|
67 |
* <p>Debug traces are logged in "sun.management.test"</p> |
|
68 |
**/ |
|
69 |
public class RmiBootstrapTest { |
|
21408
6468b29c9877
8027058: sun/management/jmxremote/bootstrap/RmiBootstrapTest.sh Failed to initialize connector
jbachorik
parents:
5506
diff
changeset
|
70 |
// the number of consecutive ports to test for availability |
23031 | 71 |
private static int MAX_GET_FREE_PORT_TRIES = 10; |
2 | 72 |
static TestLogger log = |
73 |
new TestLogger("RmiBootstrapTest"); |
|
74 |
||
75 |
/** |
|
76 |
* Default values for RMI configuration properties. |
|
77 |
**/ |
|
78 |
public static interface DefaultValues { |
|
79 |
public static final String PORT="0"; |
|
80 |
public static final String CONFIG_FILE_NAME="management.properties"; |
|
81 |
public static final String USE_SSL="true"; |
|
82 |
public static final String USE_AUTHENTICATION="true"; |
|
83 |
public static final String PASSWORD_FILE_NAME="jmxremote.password"; |
|
84 |
public static final String ACCESS_FILE_NAME="jmxremote.access"; |
|
85 |
public static final String KEYSTORE="keystore"; |
|
86 |
public static final String KEYSTORE_PASSWD="password"; |
|
87 |
public static final String TRUSTSTORE="truststore"; |
|
88 |
public static final String TRUSTSTORE_PASSWD="trustword"; |
|
89 |
public static final String SSL_NEED_CLIENT_AUTH="false"; |
|
90 |
} |
|
91 |
||
92 |
/** |
|
93 |
* Names of RMI configuration properties. |
|
94 |
**/ |
|
95 |
public static interface PropertyNames { |
|
96 |
public static final String PORT= |
|
97 |
"com.sun.management.jmxremote.port"; |
|
98 |
public static final String CONFIG_FILE_NAME= |
|
99 |
"com.sun.management.config.file"; |
|
100 |
public static final String USE_SSL= |
|
101 |
"com.sun.management.jmxremote.ssl"; |
|
102 |
public static final String USE_AUTHENTICATION= |
|
103 |
"com.sun.management.jmxremote.authenticate"; |
|
104 |
public static final String PASSWORD_FILE_NAME= |
|
105 |
"com.sun.management.jmxremote.password.file"; |
|
106 |
public static final String ACCESS_FILE_NAME= |
|
107 |
"com.sun.management.jmxremote.access.file"; |
|
108 |
public static final String INSTRUMENT_ALL= |
|
109 |
"com.sun.management.instrumentall"; |
|
110 |
public static final String CREDENTIALS = |
|
111 |
"jmx.remote.credentials"; |
|
112 |
public static final String KEYSTORE= |
|
113 |
"javax.net.ssl.keyStore"; |
|
114 |
public static final String KEYSTORE_PASSWD= |
|
115 |
"javax.net.ssl.keyStorePassword"; |
|
116 |
public static final String TRUSTSTORE= |
|
117 |
"javax.net.ssl.trustStore"; |
|
118 |
public static final String TRUSTSTORE_PASSWD= |
|
119 |
"javax.net.ssl.trustStorePassword"; |
|
120 |
public static final String SSL_ENABLED_CIPHER_SUITES = |
|
121 |
"com.sun.management.jmxremote.ssl.enabled.cipher.suites"; |
|
122 |
public static final String SSL_ENABLED_PROTOCOLS = |
|
123 |
"com.sun.management.jmxremote.ssl.enabled.protocols"; |
|
124 |
public static final String SSL_NEED_CLIENT_AUTH = |
|
125 |
"com.sun.management.jmxremote.ssl.need.client.auth"; |
|
126 |
} |
|
127 |
||
128 |
/** |
|
129 |
* A filter to find all filenames who match <prefix>*<suffix>. |
|
130 |
* Note that <prefix> and <suffix> can overlap. |
|
131 |
**/ |
|
132 |
private static class ConfigFilenameFilter implements FilenameFilter { |
|
133 |
final String suffix; |
|
134 |
final String prefix; |
|
135 |
ConfigFilenameFilter(String prefix, String suffix) { |
|
136 |
this.suffix=suffix; |
|
137 |
this.prefix=prefix; |
|
138 |
} |
|
139 |
public boolean accept(File dir, String name) { |
|
140 |
return (name.startsWith(prefix) && name.endsWith(suffix)); |
|
141 |
} |
|
142 |
} |
|
143 |
||
144 |
/** |
|
145 |
* Get all "management*ok.properties" files in the directory |
|
146 |
* indicated by the "test.src" management property. |
|
147 |
**/ |
|
148 |
private static File[] findConfigurationFilesOk() { |
|
149 |
final String testSrc = System.getProperty("test.src"); |
|
150 |
final File dir = new File(testSrc); |
|
151 |
final FilenameFilter filter = |
|
152 |
new ConfigFilenameFilter("management_test","ok.properties"); |
|
153 |
return dir.listFiles(filter); |
|
154 |
} |
|
155 |
||
156 |
/** |
|
157 |
* Get all "management*ko.properties" files in the directory |
|
158 |
* indicated by the "test.src" management property. |
|
159 |
**/ |
|
160 |
private static File[] findConfigurationFilesKo() { |
|
161 |
final String testSrc = System.getProperty("test.src"); |
|
162 |
final File dir = new File(testSrc); |
|
163 |
final FilenameFilter filter = |
|
164 |
new ConfigFilenameFilter("management_test","ko.properties"); |
|
165 |
return dir.listFiles(filter); |
|
166 |
} |
|
167 |
||
168 |
/** |
|
169 |
* List all MBeans and their attributes. Used to test communication |
|
170 |
* with the Java M&M MBean Server. |
|
171 |
* @return the number of queried MBeans. |
|
172 |
*/ |
|
173 |
public static int listMBeans(MBeanServerConnection server) |
|
174 |
throws IOException { |
|
175 |
return listMBeans(server,null,null); |
|
176 |
} |
|
177 |
||
178 |
/** |
|
179 |
* List all matching MBeans and their attributes. |
|
180 |
* Used to test communication with the Java M&M MBean Server. |
|
181 |
* @return the number of matching MBeans. |
|
182 |
*/ |
|
183 |
public static int listMBeans(MBeanServerConnection server, |
|
184 |
ObjectName pattern, QueryExp query) |
|
185 |
throws IOException { |
|
186 |
||
187 |
final Set names = server.queryNames(pattern,query); |
|
188 |
for (final Iterator i=names.iterator(); i.hasNext(); ) { |
|
189 |
ObjectName name = (ObjectName)i.next(); |
|
190 |
log.trace("listMBeans","Got MBean: "+name); |
|
191 |
try { |
|
192 |
MBeanInfo info = |
|
193 |
server.getMBeanInfo((ObjectName)name); |
|
194 |
MBeanAttributeInfo[] attrs = info.getAttributes(); |
|
195 |
if (attrs == null) continue; |
|
196 |
for (int j=0; j<attrs.length; j++) { |
|
197 |
if (attrs[j].isReadable()) { |
|
198 |
try { |
|
199 |
Object o = |
|
200 |
server.getAttribute(name,attrs[j].getName()); |
|
201 |
if (log.isDebugOn()) |
|
202 |
log.debug("listMBeans","\t\t" + |
|
203 |
attrs[j].getName() + |
|
204 |
" = "+o); |
|
205 |
} catch (Exception x) { |
|
206 |
log.trace("listMBeans","JmxClient failed to get " + |
|
207 |
attrs[j].getName() + ": " + x); |
|
208 |
final IOException io = |
|
209 |
new IOException("JmxClient failed to get " + |
|
210 |
attrs[j].getName()); |
|
211 |
io.initCause(x); |
|
212 |
throw io; |
|
213 |
} |
|
214 |
} |
|
215 |
} |
|
216 |
} catch (Exception x) { |
|
217 |
log.trace("listMBeans", |
|
218 |
"JmxClient failed to get MBeanInfo: " + x); |
|
219 |
final IOException io = |
|
220 |
new IOException("JmxClient failed to get MBeanInfo: "+x); |
|
221 |
io.initCause(x); |
|
222 |
throw io; |
|
223 |
} |
|
224 |
} |
|
225 |
return names.size(); |
|
226 |
} |
|
227 |
||
228 |
/** |
|
229 |
* Compute the full path name for a default file. |
|
230 |
* @param basename basename (with extension) of the default file. |
|
27565 | 231 |
* @return ${JRE}/conf/management/${basename} |
2 | 232 |
**/ |
233 |
private static String getDefaultFileName(String basename) { |
|
234 |
final String fileSeparator = File.separator; |
|
235 |
final StringBuffer defaultFileName = |
|
236 |
new StringBuffer(System.getProperty("java.home")). |
|
27565 | 237 |
append(fileSeparator).append("conf").append(fileSeparator). |
2 | 238 |
append("management").append(fileSeparator). |
239 |
append(basename); |
|
240 |
return defaultFileName.toString(); |
|
241 |
} |
|
242 |
||
243 |
/** |
|
244 |
* Compute the full path name for a default file. |
|
245 |
* @param basename basename (with extension) of the default file. |
|
27565 | 246 |
* @return ${JRE}/conf/management/${basename} |
2 | 247 |
**/ |
248 |
private static String getDefaultStoreName(String basename) { |
|
249 |
final String fileSeparator = File.separator; |
|
250 |
final StringBuffer defaultFileName = |
|
251 |
new StringBuffer(System.getProperty("test.src")). |
|
252 |
append(fileSeparator).append("ssl").append(fileSeparator). |
|
253 |
append(basename); |
|
254 |
return defaultFileName.toString(); |
|
255 |
} |
|
256 |
||
257 |
||
258 |
/** |
|
259 |
* Parses the password file to read the credentials. |
|
260 |
* Returns an ArrayList of arrays of 2 string: |
|
261 |
* {<subject>, <password>}. |
|
262 |
* If the password file does not exists, return an empty list. |
|
263 |
* (File not found = empty file). |
|
264 |
**/ |
|
265 |
private ArrayList readCredentials(String passwordFileName) |
|
266 |
throws IOException { |
|
267 |
final Properties pws = new Properties(); |
|
268 |
final ArrayList result = new ArrayList(); |
|
269 |
final File f = new File(passwordFileName); |
|
270 |
if (!f.exists()) return result; |
|
271 |
FileInputStream fin = new FileInputStream(passwordFileName); |
|
272 |
try {pws.load(fin);}finally{fin.close();} |
|
273 |
for (Enumeration en=pws.propertyNames();en.hasMoreElements();) { |
|
274 |
final String[] cred = new String[2]; |
|
275 |
cred[0]=(String)en.nextElement(); |
|
276 |
cred[1]=pws.getProperty(cred[0]); |
|
277 |
result.add(cred); |
|
278 |
} |
|
279 |
return result; |
|
280 |
} |
|
281 |
||
282 |
||
283 |
/** |
|
284 |
* Connect with the given url, using all given credentials in turn. |
|
285 |
* A null entry in the useCredentials arrays indicate a connection |
|
286 |
* where no credentials are used. |
|
287 |
* @param url JMXServiceURL of the server. |
|
288 |
* @param useCredentials An array of credentials (a credential |
|
289 |
* is a two String array, so this is an array of arrays |
|
290 |
* of strings: |
|
291 |
* useCredentials[i][0]=subject |
|
292 |
* useCredentials[i][1]=password |
|
293 |
* if useCredentials[i] == null means no credentials. |
|
294 |
* @param expectConnectOk true if connection is expected to succeed |
|
295 |
* Note: if expectConnectOk=false and the test fails to connect |
|
296 |
* the number of failure is not incremented. Conversely, |
|
297 |
* if expectConnectOk=false and the test does not fail to |
|
298 |
* connect the number of failure is incremented. |
|
299 |
* @param expectReadOk true if communication (listMBeans) is expected |
|
300 |
* to succeed. |
|
301 |
* Note: if expectReadOk=false and the test fails to read MBeans |
|
302 |
* the number of failure is not incremented. Conversely, |
|
303 |
* if expectReadOk=false and the test does not fail to |
|
304 |
* read MBeans the number of failure is incremented. |
|
305 |
* @return number of failure. |
|
306 |
**/ |
|
307 |
public int connectAndRead(JMXServiceURL url, |
|
308 |
Object[] useCredentials, |
|
309 |
boolean expectConnectOk, |
|
310 |
boolean expectReadOk) |
|
311 |
throws IOException { |
|
312 |
||
313 |
int errorCount = 0; |
|
314 |
||
315 |
for (int i=0 ; i<useCredentials.length ; i++) { |
|
316 |
final Map m = new HashMap(); |
|
317 |
final String[] credentials = (String[])useCredentials[i]; |
|
318 |
final String crinfo; |
|
319 |
if (credentials != null) { |
|
320 |
crinfo = "{"+credentials[0] + ", " + credentials[1] + "}"; |
|
321 |
m.put(PropertyNames.CREDENTIALS,credentials); |
|
322 |
} else { |
|
323 |
crinfo="no credentials"; |
|
324 |
} |
|
325 |
log.trace("testCommunication","using credentials: " + crinfo); |
|
326 |
||
327 |
final JMXConnector c; |
|
328 |
try { |
|
329 |
c = JMXConnectorFactory.connect(url,m); |
|
330 |
} catch (IOException x ) { |
|
331 |
if (expectConnectOk) { |
|
332 |
final String err = "Connection failed for " + crinfo + |
|
333 |
": " + x; |
|
334 |
System.out.println(err); |
|
335 |
log.trace("testCommunication",err); |
|
336 |
log.debug("testCommunication",x); |
|
337 |
errorCount++; |
|
338 |
continue; |
|
339 |
} else { |
|
340 |
System.out.println("Connection failed as expected for " + |
|
341 |
crinfo + ": " + x); |
|
342 |
continue; |
|
343 |
} |
|
344 |
} catch (RuntimeException x ) { |
|
345 |
if (expectConnectOk) { |
|
346 |
final String err = "Connection failed for " + crinfo + |
|
347 |
": " + x; |
|
348 |
System.out.println(err); |
|
349 |
log.trace("testCommunication",err); |
|
350 |
log.debug("testCommunication",x); |
|
351 |
errorCount++; |
|
352 |
continue; |
|
353 |
} else { |
|
354 |
System.out.println("Connection failed as expected for " + |
|
355 |
crinfo + ": " + x); |
|
356 |
continue; |
|
357 |
} |
|
358 |
} |
|
359 |
try { |
|
360 |
MBeanServerConnection conn = |
|
361 |
c.getMBeanServerConnection(); |
|
362 |
if (log.isDebugOn()) { |
|
363 |
log.debug("testCommunication","Connection is:" + conn); |
|
364 |
log.debug("testCommunication","Server domain is: " + |
|
365 |
conn.getDefaultDomain()); |
|
366 |
} |
|
367 |
final ObjectName pattern = |
|
368 |
new ObjectName("java.lang:type=Memory,*"); |
|
369 |
final int count = listMBeans(conn,pattern,null); |
|
370 |
if (count == 0) |
|
371 |
throw new Exception("Expected at least one matching "+ |
|
372 |
"MBean for "+pattern); |
|
373 |
if (expectReadOk) { |
|
374 |
System.out.println("Communication succeeded " + |
|
375 |
"as expected for "+ |
|
376 |
crinfo + ": found " + count |
|
377 |
+ ((count<2)?"MBean":"MBeans")); |
|
378 |
} else { |
|
379 |
final String err = "Expected failure didn't occur for " + |
|
380 |
crinfo; |
|
381 |
System.out.println(err); |
|
382 |
errorCount++; |
|
383 |
} |
|
384 |
} catch (IOException x ) { |
|
385 |
if (expectReadOk) { |
|
386 |
final String err = "Communication failed with " + crinfo + |
|
387 |
": " + x; |
|
388 |
System.out.println(err); |
|
389 |
log.trace("testCommunication",err); |
|
390 |
log.debug("testCommunication",x); |
|
391 |
errorCount++; |
|
392 |
continue; |
|
393 |
} else { |
|
394 |
System.out.println("Communication failed as expected for "+ |
|
395 |
crinfo + ": " + x); |
|
396 |
continue; |
|
397 |
} |
|
398 |
} catch (RuntimeException x ) { |
|
399 |
if (expectReadOk) { |
|
400 |
final String err = "Communication failed with " + crinfo + |
|
401 |
": " + x; |
|
402 |
System.out.println(err); |
|
403 |
log.trace("testCommunication",err); |
|
404 |
log.debug("testCommunication",x); |
|
405 |
errorCount++; |
|
406 |
continue; |
|
407 |
} else { |
|
408 |
System.out.println("Communication failed as expected for "+ |
|
409 |
crinfo + ": " + x); |
|
410 |
} |
|
411 |
} catch (Exception x) { |
|
412 |
final String err = "Failed to read MBeans with " + crinfo + |
|
413 |
": " + x; |
|
414 |
System.out.println(err); |
|
415 |
log.trace("testCommunication",err); |
|
416 |
log.debug("testCommunication",x); |
|
417 |
errorCount++; |
|
418 |
continue; |
|
419 |
} finally { |
|
420 |
c.close(); |
|
421 |
} |
|
422 |
} |
|
423 |
return errorCount; |
|
424 |
} |
|
425 |
||
426 |
||
427 |
private void setSslProperties() { |
|
428 |
final String defaultKeyStore = |
|
429 |
getDefaultStoreName(DefaultValues.KEYSTORE); |
|
430 |
final String defaultTrustStore = |
|
431 |
getDefaultStoreName(DefaultValues.TRUSTSTORE); |
|
432 |
||
433 |
final String keyStore = |
|
434 |
System.getProperty(PropertyNames.KEYSTORE, defaultKeyStore); |
|
435 |
System.setProperty(PropertyNames.KEYSTORE,keyStore); |
|
436 |
log.trace("setSslProperties",PropertyNames.KEYSTORE+"="+keyStore); |
|
437 |
||
438 |
final String password = |
|
439 |
System.getProperty(PropertyNames.KEYSTORE_PASSWD, |
|
440 |
DefaultValues.KEYSTORE_PASSWD); |
|
441 |
System.setProperty(PropertyNames.KEYSTORE_PASSWD,password); |
|
442 |
log.trace("setSslProperties", |
|
443 |
PropertyNames.KEYSTORE_PASSWD+"="+password); |
|
444 |
||
445 |
final String trustStore = |
|
446 |
System.getProperty(PropertyNames.TRUSTSTORE, |
|
447 |
defaultTrustStore); |
|
448 |
System.setProperty(PropertyNames.TRUSTSTORE,trustStore); |
|
449 |
log.trace("setSslProperties", |
|
450 |
PropertyNames.TRUSTSTORE+"="+trustStore); |
|
451 |
||
452 |
final String trustword = |
|
453 |
System.getProperty(PropertyNames.TRUSTSTORE_PASSWD, |
|
454 |
DefaultValues.TRUSTSTORE_PASSWD); |
|
455 |
System.setProperty(PropertyNames.TRUSTSTORE_PASSWD,trustword); |
|
456 |
log.trace("setSslProperties", |
|
457 |
PropertyNames.TRUSTSTORE_PASSWD+"="+trustword); |
|
458 |
} |
|
459 |
||
460 |
private void checkSslConfiguration() { |
|
461 |
try { |
|
462 |
final String defaultConf = |
|
463 |
getDefaultFileName(DefaultValues.CONFIG_FILE_NAME); |
|
464 |
final String confname = |
|
465 |
System.getProperty(PropertyNames.CONFIG_FILE_NAME,defaultConf); |
|
466 |
||
467 |
final Properties props = new Properties(); |
|
468 |
final File conf = new File(confname); |
|
469 |
if (conf.exists()) { |
|
470 |
FileInputStream fin = new FileInputStream(conf); |
|
471 |
try {props.load(fin);} finally {fin.close();} |
|
472 |
} |
|
473 |
||
474 |
// Do we use SSL? |
|
475 |
final String useSslStr = |
|
476 |
props.getProperty(PropertyNames.USE_SSL, |
|
477 |
DefaultValues.USE_SSL); |
|
478 |
final boolean useSsl = |
|
479 |
Boolean.valueOf(useSslStr).booleanValue(); |
|
480 |
||
481 |
log.debug("checkSslConfiguration", |
|
482 |
PropertyNames.USE_SSL+"="+useSsl+ |
|
483 |
": setting SSL"); |
|
484 |
// Do we use SSL client authentication? |
|
485 |
final String useSslClientAuthStr = |
|
486 |
props.getProperty(PropertyNames.SSL_NEED_CLIENT_AUTH, |
|
487 |
DefaultValues.SSL_NEED_CLIENT_AUTH); |
|
488 |
final boolean useSslClientAuth = |
|
489 |
Boolean.valueOf(useSslClientAuthStr).booleanValue(); |
|
490 |
||
491 |
log.debug("checkSslConfiguration", |
|
492 |
PropertyNames.SSL_NEED_CLIENT_AUTH+"="+useSslClientAuth); |
|
493 |
||
494 |
// Do we use customized SSL cipher suites? |
|
495 |
final String sslCipherSuites = |
|
496 |
props.getProperty(PropertyNames.SSL_ENABLED_CIPHER_SUITES); |
|
497 |
||
498 |
log.debug("checkSslConfiguration", |
|
499 |
PropertyNames.SSL_ENABLED_CIPHER_SUITES + "=" + |
|
500 |
sslCipherSuites); |
|
501 |
||
502 |
// Do we use customized SSL protocols? |
|
503 |
final String sslProtocols = |
|
504 |
props.getProperty(PropertyNames.SSL_ENABLED_PROTOCOLS); |
|
505 |
||
506 |
log.debug("checkSslConfiguration", |
|
507 |
PropertyNames.SSL_ENABLED_PROTOCOLS + "=" + |
|
508 |
sslProtocols); |
|
509 |
||
510 |
if (useSsl) setSslProperties(); |
|
511 |
} catch (Exception x) { |
|
512 |
System.out.println("Failed to setup SSL configuration: " + x); |
|
513 |
log.debug("checkSslConfiguration",x); |
|
514 |
} |
|
515 |
} |
|
516 |
||
517 |
/** |
|
518 |
* Tests the server bootstraped at the given URL. |
|
519 |
* Uses the system properties to determine which config file is used. |
|
520 |
* Loads the config file to determine which password file is used. |
|
521 |
* Loads the password file to find out wich credentials to use. |
|
522 |
* Also checks that unregistered user/passwords are not allowed to |
|
523 |
* connect when a password file is used. |
|
524 |
* |
|
525 |
* This method calls connectAndRead(). |
|
526 |
**/ |
|
527 |
public void testCommunication(JMXServiceURL url) |
|
528 |
throws IOException { |
|
529 |
||
530 |
final String defaultConf = |
|
531 |
getDefaultFileName(DefaultValues.CONFIG_FILE_NAME); |
|
532 |
final String confname = |
|
533 |
System.getProperty(PropertyNames.CONFIG_FILE_NAME,defaultConf); |
|
534 |
||
535 |
final Properties props = new Properties(); |
|
536 |
final File conf = new File(confname); |
|
537 |
if (conf.exists()) { |
|
538 |
FileInputStream fin = new FileInputStream(conf); |
|
539 |
try {props.load(fin);} finally {fin.close();} |
|
540 |
} |
|
541 |
||
542 |
// Do we use authentication? |
|
543 |
final String useAuthenticationStr = |
|
544 |
props.getProperty(PropertyNames.USE_AUTHENTICATION, |
|
545 |
DefaultValues.USE_AUTHENTICATION); |
|
546 |
final boolean useAuthentication = |
|
547 |
Boolean.valueOf(useAuthenticationStr).booleanValue(); |
|
548 |
||
549 |
// Get Password File |
|
550 |
final String defaultPasswordFileName = Utils.convertPath( |
|
551 |
getDefaultFileName(DefaultValues.PASSWORD_FILE_NAME)); |
|
552 |
final String passwordFileName = Utils.convertPath( |
|
553 |
props.getProperty(PropertyNames.PASSWORD_FILE_NAME, |
|
554 |
defaultPasswordFileName)); |
|
555 |
||
556 |
// Get Access File |
|
557 |
final String defaultAccessFileName = Utils.convertPath( |
|
558 |
getDefaultFileName(DefaultValues.ACCESS_FILE_NAME)); |
|
559 |
final String accessFileName = Utils.convertPath( |
|
560 |
props.getProperty(PropertyNames.ACCESS_FILE_NAME, |
|
561 |
defaultAccessFileName)); |
|
562 |
||
563 |
if (useAuthentication) { |
|
564 |
System.out.println("PasswordFileName: " + passwordFileName); |
|
565 |
System.out.println("accessFileName: " + accessFileName); |
|
566 |
} |
|
567 |
||
568 |
final Object[] allCredentials; |
|
569 |
final Object[] noCredentials = { null }; |
|
570 |
if (useAuthentication) { |
|
571 |
final ArrayList l = readCredentials(passwordFileName); |
|
572 |
if (l.size() == 0) allCredentials = null; |
|
573 |
else allCredentials = l.toArray(); |
|
574 |
} else allCredentials = noCredentials; |
|
575 |
||
576 |
int errorCount = 0; |
|
577 |
if (allCredentials!=null) { |
|
578 |
// Tests that the registered user/passwords are allowed to |
|
579 |
// connect & read |
|
580 |
// |
|
581 |
errorCount += connectAndRead(url,allCredentials,true,true); |
|
582 |
} else { |
|
583 |
// Tests that no one is allowed |
|
584 |
// connect & read |
|
585 |
// |
|
586 |
final String[][] someCredentials = { |
|
587 |
null, |
|
588 |
{ "modify", "R&D" }, |
|
589 |
{ "measure", "QED" } |
|
590 |
}; |
|
591 |
errorCount += connectAndRead(url,someCredentials,false,false); |
|
592 |
} |
|
593 |
||
594 |
if (useAuthentication && allCredentials != noCredentials) { |
|
595 |
// Tests that the registered user/passwords are not allowed to |
|
596 |
// connect & read |
|
597 |
// |
|
598 |
final String[][] badCredentials = { |
|
599 |
{ "bad.user", "R&D" }, |
|
600 |
{ "measure", "bad.password" } |
|
601 |
}; |
|
602 |
errorCount += connectAndRead(url,badCredentials,false,false); |
|
603 |
} |
|
604 |
if (errorCount > 0) { |
|
605 |
final String err = "Test " + confname + " failed with " + |
|
606 |
errorCount + " error(s)"; |
|
607 |
log.debug("testCommunication",err); |
|
608 |
throw new RuntimeException(err); |
|
609 |
} |
|
610 |
} |
|
611 |
||
612 |
||
613 |
/** |
|
614 |
* Test the configuration indicated by `file'. |
|
615 |
* Sets the appropriate System properties for config file and |
|
616 |
* port and then calls ConnectorBootstrap.initialize(). |
|
617 |
* eventually cleans up by calling ConnectorBootstrap.terminate(). |
|
618 |
* @return null if the test succeeds, an error message otherwise. |
|
619 |
**/ |
|
23031 | 620 |
private String testConfiguration(File file) throws IOException, InterruptedException { |
2 | 621 |
|
23031 | 622 |
for (int i = 0; i < MAX_GET_FREE_PORT_TRIES; i++) { |
623 |
try { |
|
624 |
int port = jdk.testlibrary.Utils.getFreePort(); |
|
625 |
final String path; |
|
626 |
try { |
|
627 |
path=(file==null)?null:file.getCanonicalPath(); |
|
628 |
} catch(IOException x) { |
|
629 |
final String err = "Failed to test configuration " + file + |
|
630 |
": " + x; |
|
631 |
log.trace("testConfiguration",err); |
|
632 |
log.debug("testConfiguration",x); |
|
633 |
return err; |
|
21408
6468b29c9877
8027058: sun/management/jmxremote/bootstrap/RmiBootstrapTest.sh Failed to initialize connector
jbachorik
parents:
5506
diff
changeset
|
634 |
} |
23031 | 635 |
final String config = (path==null)?"Default config file":path; |
636 |
||
637 |
System.out.println("***"); |
|
638 |
System.out.println("*** Testing configuration (port=" + port + "): " |
|
639 |
+ path); |
|
640 |
System.out.println("***"); |
|
2 | 641 |
|
23031 | 642 |
System.setProperty("com.sun.management.jmxremote.port", |
643 |
Integer.toString(port)); |
|
644 |
if (path != null) |
|
645 |
System.setProperty("com.sun.management.config.file", path); |
|
646 |
else |
|
647 |
System.getProperties().remove("com.sun.management.config.file"); |
|
648 |
||
649 |
log.trace("testConfiguration","com.sun.management.jmxremote.port="+port); |
|
650 |
if (path != null && log.isDebugOn()) |
|
651 |
log.trace("testConfiguration", |
|
652 |
"com.sun.management.config.file="+path); |
|
653 |
||
654 |
checkSslConfiguration(); |
|
2 | 655 |
|
23031 | 656 |
final JMXConnectorServer cs; |
657 |
try { |
|
658 |
cs = ConnectorBootstrap.initialize(); |
|
659 |
} catch (AgentConfigurationError x) { |
|
660 |
if (x.getCause() instanceof ExportException) { |
|
661 |
if (x.getCause().getCause() instanceof BindException) { |
|
662 |
throw (BindException)x.getCause().getCause(); |
|
663 |
} |
|
664 |
} |
|
665 |
final String err = "Failed to initialize connector:" + |
|
666 |
"\n\tcom.sun.management.jmxremote.port=" + port + |
|
667 |
((path!=null)?"\n\tcom.sun.management.config.file="+path: |
|
668 |
"\n\t"+config) + |
|
669 |
"\n\tError is: " + x; |
|
670 |
log.trace("testConfiguration",err); |
|
671 |
log.debug("testConfiguration",x); |
|
672 |
return err; |
|
673 |
} catch (Exception x) { |
|
674 |
log.debug("testConfiguration",x); |
|
675 |
return x.toString(); |
|
676 |
} |
|
677 |
||
678 |
try { |
|
679 |
JMXServiceURL url = |
|
680 |
new JMXServiceURL("rmi",null,0,"/jndi/rmi://localhost:"+ |
|
681 |
port+"/jmxrmi"); |
|
682 |
||
683 |
try { |
|
684 |
testCommunication(url); |
|
685 |
} catch (Exception x) { |
|
686 |
final String err = "Failed to connect to agent {url="+url+ |
|
687 |
"}: " + x; |
|
688 |
log.trace("testConfiguration",err); |
|
689 |
log.debug("testConfiguration",x); |
|
690 |
return err; |
|
691 |
} |
|
692 |
} catch (Exception x) { |
|
693 |
final String err = "Failed to test configuration "+config+ |
|
694 |
": "+x; |
|
695 |
log.trace("testConfiguration",err); |
|
696 |
log.debug("testConfiguration",x); |
|
697 |
return err; |
|
698 |
} finally { |
|
699 |
try { |
|
700 |
cs.stop(); |
|
701 |
} catch (Exception x) { |
|
702 |
final String err = "Failed to terminate: "+x; |
|
703 |
log.trace("testConfiguration",err); |
|
704 |
log.debug("testConfiguration",x); |
|
705 |
} |
|
706 |
} |
|
707 |
System.out.println("Configuration " + config + " successfully tested"); |
|
708 |
return null; |
|
709 |
} catch(BindException ex) { |
|
2 | 710 |
} |
711 |
} |
|
23031 | 712 |
System.err.println("Cannot find a free port after " + MAX_GET_FREE_PORT_TRIES + " tries"); |
713 |
return "Failed: cannot find a free port after " + MAX_GET_FREE_PORT_TRIES + " tries"; |
|
2 | 714 |
} |
715 |
||
716 |
/** |
|
717 |
* Test a configuration file which should make the bootstrap fail. |
|
718 |
* The test is assumed to have succeeded if the bootstrap fails. |
|
719 |
* @return null if the test succeeds, an error message otherwise. |
|
720 |
**/ |
|
23031 | 721 |
private String testConfigurationKo(File conf) throws InterruptedException, IOException { |
21408
6468b29c9877
8027058: sun/management/jmxremote/bootstrap/RmiBootstrapTest.sh Failed to initialize connector
jbachorik
parents:
5506
diff
changeset
|
722 |
String errStr = null; |
23031 | 723 |
errStr = testConfiguration(conf); |
2 | 724 |
if (errStr == null) { |
725 |
return "Configuration " + |
|
726 |
conf + " should have failed!"; |
|
727 |
} |
|
728 |
System.out.println("Configuration " + |
|
729 |
conf + " failed as expected"); |
|
23031 | 730 |
log.debug("runko", "Error was: " + errStr); |
2 | 731 |
return null; |
732 |
} |
|
733 |
||
734 |
/** |
|
735 |
* Test a configuration file. Determines whether the bootstrap |
|
736 |
* should succeed or fail depending on the file name: |
|
737 |
* *ok.properties: bootstrap should succeed. |
|
738 |
* *ko.properties: bootstrap or connection should fail. |
|
739 |
* @return null if the test succeeds, an error message otherwise. |
|
740 |
**/ |
|
23031 | 741 |
private String testConfigurationFile(String fileName) throws InterruptedException, IOException { |
2 | 742 |
File file = new File(fileName); |
743 |
||
744 |
if (fileName.endsWith("ok.properties")) { |
|
21408
6468b29c9877
8027058: sun/management/jmxremote/bootstrap/RmiBootstrapTest.sh Failed to initialize connector
jbachorik
parents:
5506
diff
changeset
|
745 |
String errStr = null; |
23031 | 746 |
errStr = testConfiguration(file); |
747 |
return errStr; |
|
2 | 748 |
} |
749 |
if (fileName.endsWith("ko.properties")) { |
|
23031 | 750 |
return testConfigurationKo(file); |
2 | 751 |
} |
752 |
return fileName + |
|
753 |
": test file suffix must be one of [ko|ok].properties"; |
|
754 |
} |
|
755 |
||
756 |
/** |
|
757 |
* Find all *ko.property files and test them. |
|
758 |
* (see findConfigurationFilesKo() and testConfigurationKo()) |
|
759 |
* @throws RuntimeException if the test fails. |
|
760 |
**/ |
|
23031 | 761 |
public void runko() throws InterruptedException, IOException { |
2 | 762 |
final File[] conf = findConfigurationFilesKo(); |
763 |
if ((conf == null)||(conf.length == 0)) |
|
764 |
throw new RuntimeException("No configuration found"); |
|
765 |
||
766 |
String errStr; |
|
767 |
for (int i=0;i<conf.length;i++) { |
|
23031 | 768 |
errStr = testConfigurationKo(conf[i]); |
2 | 769 |
if (errStr != null) { |
770 |
throw new RuntimeException(errStr); |
|
771 |
} |
|
772 |
} |
|
773 |
||
774 |
} |
|
775 |
||
776 |
/** |
|
777 |
* Find all *ok.property files and test them. |
|
778 |
* (see findConfigurationFilesOk() and testConfiguration()) |
|
779 |
* @throws RuntimeException if the test fails. |
|
780 |
**/ |
|
23031 | 781 |
public void runok() throws InterruptedException, IOException { |
2 | 782 |
final File[] conf = findConfigurationFilesOk(); |
783 |
if ((conf == null)||(conf.length == 0)) |
|
784 |
throw new RuntimeException("No configuration found"); |
|
785 |
||
21408
6468b29c9877
8027058: sun/management/jmxremote/bootstrap/RmiBootstrapTest.sh Failed to initialize connector
jbachorik
parents:
5506
diff
changeset
|
786 |
String errStr = null; |
2 | 787 |
for (int i=0;i<conf.length;i++) { |
23031 | 788 |
errStr = testConfiguration(conf[i]); |
2 | 789 |
if (errStr != null) { |
790 |
throw new RuntimeException(errStr); |
|
791 |
} |
|
792 |
} |
|
793 |
||
794 |
// FIXME: No jmxremote.password is not installed in JRE by default. |
|
795 |
// - disable the following test case. |
|
796 |
// |
|
797 |
// Test default config |
|
798 |
// |
|
799 |
// errStr = testConfiguration(null,port+testPort++); |
|
800 |
// if (errStr != null) { |
|
801 |
// throw new RuntimeException(errStr); |
|
802 |
// } |
|
803 |
} |
|
804 |
||
805 |
/** |
|
806 |
* Finds all configuration files (*ok.properties and *ko.properties) |
|
807 |
* and tests them. |
|
808 |
* (see runko() and runok()). |
|
809 |
* @throws RuntimeException if the test fails. |
|
810 |
**/ |
|
23031 | 811 |
public void run() throws InterruptedException, IOException { |
2 | 812 |
runok(); |
813 |
runko(); |
|
814 |
} |
|
815 |
||
816 |
/** |
|
817 |
* Tests the specified configuration files. |
|
818 |
* If args[] is not empty, each element in args[] is expected to be |
|
819 |
* a filename ending either by ok.properties or ko.properties. |
|
820 |
* Otherwise, the configuration files will be automatically determined |
|
821 |
* by looking at all *.properties files located in the directory |
|
822 |
* indicated by the System property "test.src". |
|
823 |
* @throws RuntimeException if the test fails. |
|
824 |
**/ |
|
23031 | 825 |
public void run(String args[]) throws InterruptedException, IOException { |
2 | 826 |
if (args.length == 0) { |
827 |
run() ; return; |
|
828 |
} |
|
829 |
for (int i=0; i<args.length; i++) { |
|
830 |
final String errStr =testConfigurationFile(args[i]); |
|
831 |
if (errStr != null) { |
|
832 |
throw new RuntimeException(errStr); |
|
833 |
} |
|
834 |
} |
|
835 |
} |
|
836 |
||
837 |
/** |
|
838 |
* Calls run(args[]). |
|
839 |
* exit(1) if the test fails. |
|
840 |
**/ |
|
21408
6468b29c9877
8027058: sun/management/jmxremote/bootstrap/RmiBootstrapTest.sh Failed to initialize connector
jbachorik
parents:
5506
diff
changeset
|
841 |
public static void main(String args[]) throws Exception { |
23031 | 842 |
try { |
843 |
MAX_GET_FREE_PORT_TRIES = Integer.parseInt(System.getProperty("test.getfreeport.max.tries", "10")); |
|
844 |
} catch (NumberFormatException ex) { |
|
845 |
} |
|
846 |
||
2 | 847 |
RmiBootstrapTest manager = new RmiBootstrapTest(); |
848 |
try { |
|
849 |
manager.run(args); |
|
850 |
} catch (RuntimeException r) { |
|
851 |
System.out.println("Test Failed: "+ r.getMessage()); |
|
852 |
System.exit(1); |
|
853 |
} catch (Throwable t) { |
|
854 |
System.out.println("Test Failed: "+ t); |
|
855 |
t.printStackTrace(); |
|
856 |
System.exit(2); |
|
857 |
} |
|
858 |
System.out.println("**** Test RmiBootstrap Passed ****"); |
|
859 |
} |
|
860 |
||
861 |
} |