jdk/test/sun/management/jmxremote/bootstrap/RmiSslNoKeyStoreTest.java
author jbachorik
Mon, 18 Nov 2013 15:25:15 +0100
changeset 21802 b36494ddf19e
parent 5506 202f599c92aa
child 23031 995cf5c2eb55
permissions -rw-r--r--
8027163: sun/management/jmxremote/bootstrap/CustomLauncherTest.java should be updated for jdk8 removal of solaris-32bit support Reviewed-by: sla
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2003, 2004, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
import sun.management.jmxremote.ConnectorBootstrap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
import java.io.File;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
import java.io.FileInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.io.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.FilenameFilter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.GeneralSecurityException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.KeyStore;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.Properties;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.Iterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.Set;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.util.Arrays;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.util.HashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.util.Enumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import javax.management.remote.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import javax.management.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import sun.management.AgentConfigurationError;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import util.TestLogger;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * <p>This class implements unit test for RMI Bootstrap.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * When called with no arguments main() looks in the directory indicated
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * by the "test.src" system property for files called management*ok.properties
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * or management*ko.properties. The *ok.properties files are assumed to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * valid Java M&M config files for which the bootstrap should succeed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * The *ko.properties files are assumed to be configurations for which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * bootstrap & connection test will fail.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * <p>The rmi port number can be specified with the "rmi.port" system property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * If not, this test will use 12424</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * <p>When called with some argument, the main() will interprete its args to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * be Java M&M configuration file names. The filenames are expected to end
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * with ok.properties or ko.properties - and are interpreted as above.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * <p>Note that a limitation of the RMI registry (bug 4267864) prevent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * this test from succeeding if more than 1 configuration is used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * As long as 4267864 isn't fix, this test must be called as many times
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * as needed but with a single argument (no arguments, or several arguments
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * will fail).</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * <p>Debug traces are logged in "sun.management.test"</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
public class RmiSslNoKeyStoreTest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    static TestLogger log =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        new TestLogger("RmiSslNoKeyStoreTest");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * When launching several registries, we increment the port number
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * to avoid falling into "port number already in use" problems.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    static int testPort = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * Default values for RMI configuration properties.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    public static interface DefaultValues {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        public static final String PORT="0";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        public static final String CONFIG_FILE_NAME="management.properties";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        public static final String USE_SSL="true";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        public static final String USE_AUTHENTICATION="true";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        public static final String PASSWORD_FILE_NAME="jmxremote.password";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        public static final String ACCESS_FILE_NAME="jmxremote.access";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        public static final String KEYSTORE="keystore";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        public static final String KEYSTORE_PASSWD="password";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        public static final String TRUSTSTORE="truststore";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        public static final String TRUSTSTORE_PASSWD="trustword";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * Names of RMI configuration properties.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    public static interface PropertyNames {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        public static final String PORT="com.sun.management.jmxremote.port";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        public static final String CONFIG_FILE_NAME=
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            "com.sun.management.config.file";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        public static final String USE_SSL="com.sun.management.jmxremote.ssl";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        public static final String USE_AUTHENTICATION=
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            "com.sun.management.jmxremote.authenticate";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        public static final String PASSWORD_FILE_NAME=
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            "com.sun.management.jmxremote.password.file";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        public static final String ACCESS_FILE_NAME=
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            "com.sun.management.jmxremote.access.file";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        public static final String INSTRUMENT_ALL=
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            "com.sun.management.instrumentall";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        public static final String CREDENTIALS =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            "jmx.remote.credentials";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        public static final String KEYSTORE="javax.net.ssl.keyStore";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        public static final String KEYSTORE_PASSWD=
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            "javax.net.ssl.keyStorePassword";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        public static final String KEYSTORE_TYPE="javax.net.ssl.keyStoreType";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        public static final String TRUSTSTORE="javax.net.ssl.trustStore";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        public static final String TRUSTSTORE_PASSWD=
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            "javax.net.ssl.trustStorePassword";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * Compute the full path name for a default file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * @param basename basename (with extension) of the default file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * @return ${JRE}/lib/management/${basename}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    private static String getDefaultFileName(String basename) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        final String fileSeparator = File.separator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        final StringBuffer defaultFileName =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            new StringBuffer(System.getProperty("java.home")).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            append(fileSeparator).append("lib").append(fileSeparator).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            append("management").append(fileSeparator).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            append(basename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        return defaultFileName.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * Compute the full path name for a default file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * @param basename basename (with extension) of the default file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * @return ${JRE}/lib/management/${basename}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    private static String getDefaultStoreName(String basename) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        final String fileSeparator = File.separator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        final StringBuffer defaultFileName =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            new StringBuffer(System.getProperty("test.src")).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            append(fileSeparator).append("ssl").append(fileSeparator).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            append(basename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        return defaultFileName.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    private static void checkKeystore(Properties props)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        throws IOException, GeneralSecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        if (log.isDebugOn())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            log.debug("checkKeystore","Checking Keystore configuration");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        final String keyStore =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            System.getProperty(PropertyNames.KEYSTORE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        if (keyStore == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            throw new IllegalArgumentException("System property " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                                               PropertyNames.KEYSTORE +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                                               " not specified");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        final String keyStorePass =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            System.getProperty(PropertyNames.KEYSTORE_PASSWD);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        if (keyStorePass == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            // We don't have the password, we can only check whether the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            // file exists...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            final File ksf = new File(keyStore);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            if (! ksf.canRead())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                throw new IOException(keyStore + ": not readable");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            if (log.isDebugOn())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                log.debug("checkSSL", "No password.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            throw new IllegalArgumentException("System property " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                                               PropertyNames.KEYSTORE_PASSWD +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                                               " not specified");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        // Now we're going to load the keyStore - just to check it's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        // correct.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        final String keyStoreType =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            System.getProperty(PropertyNames.KEYSTORE_TYPE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                               KeyStore.getDefaultType());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        final KeyStore ks         = KeyStore.getInstance(keyStoreType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        final FileInputStream fin = new FileInputStream(keyStore);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        final char keypassword[]  = keyStorePass.toCharArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            ks.load(fin,keypassword);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            Arrays.fill(keypassword,' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            fin.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        if (log.isDebugOn())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            log.debug("checkSSL","SSL configuration successfully checked");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    private void checkSslConfiguration() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        final String defaultConf =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            getDefaultFileName(DefaultValues.CONFIG_FILE_NAME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        final String confname =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            System.getProperty(PropertyNames.CONFIG_FILE_NAME,defaultConf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        final Properties props = new Properties();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        final File conf = new File(confname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        if (conf.exists()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            FileInputStream fin = new FileInputStream(conf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            try {props.load(fin);} finally {fin.close();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        // Do we use SSL?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        final String  useSslStr =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            props.getProperty(PropertyNames.USE_SSL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                              DefaultValues.USE_SSL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        final boolean useSsl =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            Boolean.valueOf(useSslStr).booleanValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        log.debug("checkSslConfiguration",PropertyNames.USE_SSL+"="+useSsl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        if (useSsl == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            final String msg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                PropertyNames.USE_SSL+"="+useSsl+", can't run test";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            throw new IllegalArgumentException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            checkKeystore(props);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        } catch (Exception x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            // Ok!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            log.debug("checkSslConfiguration","Test configuration OK: " + x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        final String msg = "KeyStore properly configured, can't run test";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        throw new IllegalArgumentException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * Test the configuration indicated by `file'.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * Sets the appropriate System properties for config file and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * port and then calls ConnectorBootstrap.initialize().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * eventually cleans up by calling ConnectorBootstrap.terminate().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * @return null if the test succeeds, an error message otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    private String testConfiguration(File file,int port) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        final String path = (file==null)?null:file.getAbsolutePath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        final String config = (path==null)?"Default config file":path;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            System.out.println("***");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            System.out.println("*** Testing configuration (port="+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                               port + "): "+ path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            System.out.println("***");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            System.setProperty("com.sun.management.jmxremote.port",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                               Integer.toString(port));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            if (path != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                System.setProperty("com.sun.management.config.file", path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                System.getProperties().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                    remove("com.sun.management.config.file");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            log.trace("testConfiguration","com.sun.management.jmxremote.port="+port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            if (path != null && log.isDebugOn())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                log.trace("testConfiguration",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                          "com.sun.management.config.file="+path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            checkSslConfiguration();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            final JMXConnectorServer cs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                cs = ConnectorBootstrap.initialize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            } catch (AgentConfigurationError x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                final String err = "Failed to initialize connector:" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                    "\n\tcom.sun.management.jmxremote.port=" + port +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                    ((path!=null)?"\n\tcom.sun.management.config.file="+path:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                     "\n\t"+config) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                    "\n\tError is: " + x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                log.trace("testConfiguration","Expected failure: " + err);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                log.debug("testConfiguration",x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                System.out.println("Got expected failure: " + x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            } catch (Exception x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                log.debug("testConfiguration",x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                return x.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                JMXConnector cc =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                    JMXConnectorFactory.connect(cs.getAddress(), null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                cc.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            } catch (IOException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                final String err = "Failed to initialize connector:" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                    "\n\tcom.sun.management.jmxremote.port=" + port +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                    ((path!=null)?"\n\tcom.sun.management.config.file="+path:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                     "\n\t"+config) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                    "\n\tError is: " + x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                log.trace("testConfiguration","Expected failure: " + err);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                log.debug("testConfiguration",x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                System.out.println("Got expected failure: " + x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            } catch (Exception x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                log.debug("testConfiguration",x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                return x.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                cs.stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            } catch (Exception x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                final String err = "Failed to terminate: "+x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                log.trace("testConfiguration",err);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                log.debug("testConfiguration",x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            final String err = "Bootstrap should have failed:" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                "\n\tcom.sun.management.jmxremote.port=" + port +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                ((path!=null)?"\n\tcom.sun.management.config.file="+path:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                 "\n\t"+config);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            log.trace("testConfiguration",err);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            return err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        } catch (Exception x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            final String err = "Failed to test bootstrap for:" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                "\n\tcom.sun.management.jmxremote.port=" + port +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                ((path!=null)?"\n\tcom.sun.management.config.file="+path:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                 "\n\t"+config)+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                "\n\tError is: " + x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            log.trace("testConfiguration",err);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            log.debug("testConfiguration",x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            return err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * Test a configuration file. Determines whether the bootstrap
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * should succeed or fail depending on the file name:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     *     *ok.properties: bootstrap should succeed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     *     *ko.properties: bootstrap or connection should fail.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * @return null if the test succeeds, an error message otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    private String testConfigurationFile(String fileName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        File file = new File(fileName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        final String portStr = System.getProperty("rmi.port","12424");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        final int port       = Integer.parseInt(portStr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        return testConfiguration(file,port+testPort++);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * Tests the specified configuration files.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * If args[] is not empty, each element in args[] is expected to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * a filename ending either by ok.properties or ko.properties.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * Otherwise, the configuration files will be automatically determined
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * by looking at all *.properties files located in the directory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * indicated by the System property "test.src".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * @throws RuntimeException if the test fails.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    public void run(String args[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        final String defaultKeyStore =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            getDefaultStoreName(DefaultValues.KEYSTORE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        final String keyStore =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            System.getProperty(PropertyNames.KEYSTORE, defaultKeyStore);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        for (int i=0; i<args.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            String errStr =testConfigurationFile(args[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            if (errStr != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                throw new RuntimeException(errStr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            if ((System.getProperty(PropertyNames.KEYSTORE) == null) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                (System.getProperty(PropertyNames.KEYSTORE_PASSWD) == null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                    // Specify the keystore, but don't specify the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                    // password.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                    System.setProperty(PropertyNames.KEYSTORE,keyStore);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                    log.trace("run",PropertyNames.KEYSTORE+"="+keyStore);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                    errStr =testConfigurationFile(args[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                    if (errStr != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                        throw new RuntimeException(errStr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                    System.getProperties().remove(PropertyNames.KEYSTORE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * Calls run(args[]).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * exit(1) if the test fails.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
    public static void main(String args[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        RmiSslNoKeyStoreTest manager = new RmiSslNoKeyStoreTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            manager.run(args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        } catch (RuntimeException r) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            System.err.println("Test Failed: "+ r.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            System.exit(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        } catch (Throwable t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            System.err.println("Test Failed: "+ t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            t.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            System.exit(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        System.out.println("**** Test RmiSslNoKeyStoreTest Passed ****");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
}