test/jdk/javax/management/security/HashedPasswordFileTest.java
changeset 50760 8e296de294e7
parent 48198 bf64ff40f4eb
equal deleted inserted replaced
50759:00c4edaf2017 50760:8e296de294e7
     1 /*
     1 /*
     2  * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    20  * or visit www.oracle.com if you need additional information or have any
    20  * or visit www.oracle.com if you need additional information or have any
    21  * questions.
    21  * questions.
    22  */
    22  */
    23 
    23 
    24  /* @test
    24  /* @test
    25  * @bug 5016517
    25  * @bug 5016517 8204661
    26  * @summary Test Hashed passwords
    26  * @summary Test Hashed passwords
    27  * @library /test/lib
    27  * @library /test/lib
    28  * @modules java.management
    28  * @modules java.management
       
    29  *          jdk.management.agent/jdk.internal.agent
    29  * @build HashedPasswordFileTest
    30  * @build HashedPasswordFileTest
    30  * @run testng/othervm  HashedPasswordFileTest
    31  * @run testng/othervm  HashedPasswordFileTest
    31  *
    32  *
    32  */
    33  */
    33 
    34 
       
    35 import jdk.internal.agent.ConnectorAddressLink;
    34 import jdk.test.lib.Utils;
    36 import jdk.test.lib.Utils;
    35 import jdk.test.lib.process.ProcessTools;
    37 import jdk.test.lib.process.ProcessTools;
    36 import org.testng.Assert;
    38 import org.testng.Assert;
    37 import org.testng.annotations.AfterClass;
    39 import org.testng.annotations.AfterClass;
    38 import org.testng.annotations.Test;
    40 import org.testng.annotations.Test;
   403     }
   405     }
   404 
   406 
   405     @Test
   407     @Test
   406     public void testDefaultAgent() throws Exception {
   408     public void testDefaultAgent() throws Exception {
   407         List<String> pbArgs = new ArrayList<>();
   409         List<String> pbArgs = new ArrayList<>();
   408         int port = Utils.getFreePort();
       
   409         generateClearTextPasswordFile();
   410         generateClearTextPasswordFile();
   410 
   411 
   411         // This will run only on a POSIX compliant system
   412         // This will run only on a POSIX compliant system
   412         if (!FileSystems.getDefault().supportedFileAttributeViews().contains("posix")) {
   413         if (!FileSystems.getDefault().supportedFileAttributeViews().contains("posix")) {
   413             return;
   414             return;
   422         Files.setPosixFilePermissions(file.toPath(), perms);
   423         Files.setPosixFilePermissions(file.toPath(), perms);
   423 
   424 
   424         pbArgs.add("-cp");
   425         pbArgs.add("-cp");
   425         pbArgs.add(System.getProperty("test.class.path"));
   426         pbArgs.add(System.getProperty("test.class.path"));
   426 
   427 
   427         pbArgs.add("-Dcom.sun.management.jmxremote.port=" + port);
   428         pbArgs.add("-Dcom.sun.management.jmxremote.port=0");
   428         pbArgs.add("-Dcom.sun.management.jmxremote.authenticate=true");
   429         pbArgs.add("-Dcom.sun.management.jmxremote.authenticate=true");
   429         pbArgs.add("-Dcom.sun.management.jmxremote.password.file=" + file.getAbsolutePath());
   430         pbArgs.add("-Dcom.sun.management.jmxremote.password.file=" + file.getAbsolutePath());
   430         pbArgs.add("-Dcom.sun.management.jmxremote.ssl=false");
   431         pbArgs.add("-Dcom.sun.management.jmxremote.ssl=false");
       
   432         pbArgs.add("--add-exports");
       
   433         pbArgs.add("jdk.management.agent/jdk.internal.agent=ALL-UNNAMED");
   431         pbArgs.add(TestApp.class.getSimpleName());
   434         pbArgs.add(TestApp.class.getSimpleName());
   432 
   435 
   433         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
   436         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
   434                 pbArgs.toArray(new String[0]));
   437                 pbArgs.toArray(new String[0]));
   435         Process process = ProcessTools.startProcess(
   438         Process process = ProcessTools.startProcess(
   443     }
   446     }
   444 
   447 
   445     @Test
   448     @Test
   446     public void testDefaultAgentNoHash() throws Exception {
   449     public void testDefaultAgentNoHash() throws Exception {
   447         List<String> pbArgs = new ArrayList<>();
   450         List<String> pbArgs = new ArrayList<>();
   448         int port = Utils.getFreePort();
       
   449         generateClearTextPasswordFile();
   451         generateClearTextPasswordFile();
   450 
   452 
   451         // This will run only on a POSIX compliant system
   453         // This will run only on a POSIX compliant system
   452         if (!FileSystems.getDefault().supportedFileAttributeViews().contains("posix")) {
   454         if (!FileSystems.getDefault().supportedFileAttributeViews().contains("posix")) {
   453             return;
   455             return;
   462         Files.setPosixFilePermissions(file.toPath(), perms);
   464         Files.setPosixFilePermissions(file.toPath(), perms);
   463 
   465 
   464         pbArgs.add("-cp");
   466         pbArgs.add("-cp");
   465         pbArgs.add(System.getProperty("test.class.path"));
   467         pbArgs.add(System.getProperty("test.class.path"));
   466 
   468 
   467         pbArgs.add("-Dcom.sun.management.jmxremote.port=" + port);
   469         pbArgs.add("-Dcom.sun.management.jmxremote.port=0");
   468         pbArgs.add("-Dcom.sun.management.jmxremote.authenticate=true");
   470         pbArgs.add("-Dcom.sun.management.jmxremote.authenticate=true");
   469         pbArgs.add("-Dcom.sun.management.jmxremote.password.file=" + file.getAbsolutePath());
   471         pbArgs.add("-Dcom.sun.management.jmxremote.password.file=" + file.getAbsolutePath());
   470         pbArgs.add("-Dcom.sun.management.jmxremote.password.toHashes=false");
   472         pbArgs.add("-Dcom.sun.management.jmxremote.password.toHashes=false");
   471         pbArgs.add("-Dcom.sun.management.jmxremote.ssl=false");
   473         pbArgs.add("-Dcom.sun.management.jmxremote.ssl=false");
       
   474         pbArgs.add("--add-exports");
       
   475         pbArgs.add("jdk.management.agent/jdk.internal.agent=ALL-UNNAMED");
   472         pbArgs.add(TestApp.class.getSimpleName());
   476         pbArgs.add(TestApp.class.getSimpleName());
   473 
   477 
   474         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
   478         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
   475                 pbArgs.toArray(new String[0]));
   479                 pbArgs.toArray(new String[0]));
   476         Process process = ProcessTools.startProcess(
   480         Process process = ProcessTools.startProcess(
   494 
   498 
   495 class TestApp {
   499 class TestApp {
   496 
   500 
   497     public static void main(String[] args) throws IOException {
   501     public static void main(String[] args) throws IOException {
   498         try {
   502         try {
   499             JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:"
   503             Map<String, String> propsMap = ConnectorAddressLink.importRemoteFrom(0);
   500                     + System.getProperty("com.sun.management.jmxremote.port") + "/jmxrmi");
   504             String jmxServiceUrl = propsMap.get("sun.management.JMXConnectorServer.0.remoteAddress");
   501             Map<String, Object> env = new HashMap<>(1);
   505             Map<String, Object> env = new HashMap<>(1);
   502             // any dummy credentials will do. We just have to trigger password hashing
   506             // any dummy credentials will do. We just have to trigger password hashing
   503             env.put("jmx.remote.credentials", new String[]{"a", "a"});
   507             env.put("jmx.remote.credentials", new String[]{"a", "a"});
   504             try (JMXConnector cc = JMXConnectorFactory.connect(url, env)) {
   508             try (JMXConnector cc = JMXConnectorFactory.connect(new JMXServiceURL(jmxServiceUrl), env)) {
   505                 cc.getMBeanServerConnection();
   509                 cc.getMBeanServerConnection();
   506             }
   510             }
   507         } catch (SecurityException ex) {
   511         } catch (SecurityException ex) {
   508             // Catch authentication failure here
   512             // Catch authentication failure here
   509         }
   513         }