jdk/test/com/sun/jdi/OptionTest.java
author iignatyev
Wed, 15 Mar 2017 22:48:59 -0700
changeset 44423 306c020eb154
parent 30376 2ccf2cf7ea48
permissions -rw-r--r--
8176176: fix @modules in jdk_svc tests Reviewed-by: shurailine, sspitsyn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
30376
2ccf2cf7ea48 8078896: Add @modules as needed to the jdk_svc tests
ykantser
parents: 27296
diff changeset
     2
 * Copyright (c) 2004, 2015, 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: 4514
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4514
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4514
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
44423
306c020eb154 8176176: fix @modules in jdk_svc tests
iignatyev
parents: 30376
diff changeset
    25
 * @test       OptionTest
306c020eb154 8176176: fix @modules in jdk_svc tests
iignatyev
parents: 30376
diff changeset
    26
 * @bug        5095072
306c020eb154 8176176: fix @modules in jdk_svc tests
iignatyev
parents: 30376
diff changeset
    27
 * @summary    Test for misc jdwp options, just that the option is parsed
306c020eb154 8176176: fix @modules in jdk_svc tests
iignatyev
parents: 30376
diff changeset
    28
 * @author     Kelly O'Hair (copied from Tim Bell's NoLaunchOptionTest)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 *
44423
306c020eb154 8176176: fix @modules in jdk_svc tests
iignatyev
parents: 30376
diff changeset
    30
 * @run compile -g OptionTest.java
306c020eb154 8176176: fix @modules in jdk_svc tests
iignatyev
parents: 30376
diff changeset
    31
 * @run compile -g HelloWorld.java
306c020eb154 8176176: fix @modules in jdk_svc tests
iignatyev
parents: 30376
diff changeset
    32
 * @run compile -g VMConnection.java
306c020eb154 8176176: fix @modules in jdk_svc tests
iignatyev
parents: 30376
diff changeset
    33
 * @run driver OptionTest
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 */
4514
7758bdd54629 6906210: Fix another minor typo in test/Makefile
ohair
parents: 2
diff changeset
    35
7758bdd54629 6906210: Fix another minor typo in test/Makefile
ohair
parents: 2
diff changeset
    36
import java.net.ServerSocket;
27296
43273a116265 8060707: jdwp accept invalid address ':'
iignatyev
parents: 24973
diff changeset
    37
import java.util.regex.Matcher;
43273a116265 8060707: jdwp accept invalid address ':'
iignatyev
parents: 24973
diff changeset
    38
import java.util.regex.Pattern;
4514
7758bdd54629 6906210: Fix another minor typo in test/Makefile
ohair
parents: 2
diff changeset
    39
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
public class OptionTest extends Object {
27296
43273a116265 8060707: jdwp accept invalid address ':'
iignatyev
parents: 24973
diff changeset
    41
    private static final Pattern TRANSPORT_ERROR_PTRN = Pattern.compile("^ERROR: transport error .+$", Pattern.MULTILINE);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    private int subprocessStatus;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    private static final String CR = System.getProperty("line.separator");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    private static final int BUFFERSIZE = 4096;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    public static final int RETSTAT = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    public static final int STDOUT = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    public static final int STDERR = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     * Run an arbitrary command and return the results to caller.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * @param an array of String containing the command
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     *        to run and any flags or parameters to the command.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * @return completion status, stderr and stdout as array of String
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     *  Look for:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     *    return status in result[OptionTest.RETSTAT]
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     *    standard out in result[OptionTest.STDOUT]
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     *    standard err in result[OptionTest.STDERR]
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    public String[] run (String[] cmdStrings) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        StringBuffer stdoutBuffer = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        StringBuffer stderrBuffer = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        System.out.print(CR + "runCommand method about to execute: ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        for (int iNdx = 0; iNdx < cmdStrings.length; iNdx++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            System.out.print(" ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            System.out.print(cmdStrings[iNdx]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        System.out.println(CR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            Process process = Runtime.getRuntime().exec(cmdStrings);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
             * Gather up the output of the subprocess using non-blocking
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
             * reads so we can get both the subprocess stdout and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
             * subprocess stderr without overfilling any buffers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            java.io.BufferedInputStream is =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                new java.io.BufferedInputStream(process.getInputStream());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            int isLen = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            byte[] isBuf = new byte[BUFFERSIZE];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            java.io.BufferedInputStream es =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                new java.io.BufferedInputStream(process.getErrorStream());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            int esLen = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            byte[] esBuf = new byte[BUFFERSIZE];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                isLen = is.read(isBuf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                if (isLen > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                    stdoutBuffer.append(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                                        new String(isBuf, 0, isLen));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                esLen = es.read(esBuf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                if (esLen > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                    stderrBuffer.append(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                                        new String(esBuf, 0, esLen));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            } while ((isLen > -1) || (esLen > -1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                process.waitFor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                subprocessStatus = process.exitValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                process = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            } catch(java.lang.InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                System.err.println("InterruptedException: " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        } catch(java.io.IOException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            System.err.println("IO error: " + ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        String[] result =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            new String[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                Integer.toString(subprocessStatus),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                stdoutBuffer.toString(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                stderrBuffer.toString()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        System.out.println(CR + "--- Return code was: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                           CR + result[RETSTAT]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        System.out.println(CR + "--- Return stdout was: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                           CR + result[STDOUT]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        System.out.println(CR + "--- Return stderr was: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                           CR + result[STDERR]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    public static void main(String[] args) throws Exception {
4514
7758bdd54629 6906210: Fix another minor typo in test/Makefile
ohair
parents: 2
diff changeset
   130
        // find a free port
7758bdd54629 6906210: Fix another minor typo in test/Makefile
ohair
parents: 2
diff changeset
   131
        ServerSocket ss = new ServerSocket(0);
7758bdd54629 6906210: Fix another minor typo in test/Makefile
ohair
parents: 2
diff changeset
   132
        int port = ss.getLocalPort();
7758bdd54629 6906210: Fix another minor typo in test/Makefile
ohair
parents: 2
diff changeset
   133
        ss.close();
7758bdd54629 6906210: Fix another minor typo in test/Makefile
ohair
parents: 2
diff changeset
   134
        String address = String.valueOf(port);
7758bdd54629 6906210: Fix another minor typo in test/Makefile
ohair
parents: 2
diff changeset
   135
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        String javaExe = System.getProperty("java.home") +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            java.io.File.separator + "bin" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            java.io.File.separator + "java";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        String targetClass = "HelloWorld";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        String baseOptions = "transport=dt_socket" +
4514
7758bdd54629 6906210: Fix another minor typo in test/Makefile
ohair
parents: 2
diff changeset
   141
                              ",address=" + address +
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                              ",server=y" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                              ",suspend=n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        /* Option combinations to try (combos faster, fewer exec's) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        String options[] =  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                "timeout=0,mutf8=y,quiet=y,stdalloc=y,strict=n",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                "timeout=200000,mutf8=n,quiet=n,stdalloc=n,strict=y"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        for ( String option : options) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            String cmds [] = {javaExe,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                              "-agentlib:jdwp=" + baseOptions + "," + option,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                              targetClass};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            OptionTest myTest = new OptionTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            String results [] = myTest.run(VMConnection.insertDebuggeeVMOptions(cmds));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            if (!(results[RETSTAT].equals("0")) ||
27296
43273a116265 8060707: jdwp accept invalid address ':'
iignatyev
parents: 24973
diff changeset
   158
                (TRANSPORT_ERROR_PTRN.matcher(results[STDERR]).find())) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                throw new Exception("Test failed: jdwp doesn't like " + cmds[1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        }
24503
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 5506
diff changeset
   162
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 5506
diff changeset
   163
        System.out.println("Testing invalid address string");
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 5506
diff changeset
   164
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 5506
diff changeset
   165
        // Test invalid addresses
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 5506
diff changeset
   166
        String badAddresses[] = {
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 5506
diff changeset
   167
            ":",
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 5506
diff changeset
   168
            "localhost:",
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 5506
diff changeset
   169
            "localhost:abc",
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 5506
diff changeset
   170
            "localhost:65536",
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 5506
diff changeset
   171
            "localhost:65F"
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 5506
diff changeset
   172
        };
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 5506
diff changeset
   173
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 5506
diff changeset
   174
        for (String badAddress : badAddresses) {
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 5506
diff changeset
   175
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 5506
diff changeset
   176
            String badOptions = "transport=dt_socket" +
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 5506
diff changeset
   177
                              ",address=" + badAddress +
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 5506
diff changeset
   178
                              ",server=y" +
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 5506
diff changeset
   179
                              ",suspend=n";
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 5506
diff changeset
   180
            String cmds[] = {javaExe, "-agentlib:jdwp=" + badOptions, targetClass};
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 5506
diff changeset
   181
            OptionTest myTest = new OptionTest();
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 5506
diff changeset
   182
            String results[] = myTest.run(VMConnection.insertDebuggeeVMOptions(cmds));
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 5506
diff changeset
   183
27296
43273a116265 8060707: jdwp accept invalid address ':'
iignatyev
parents: 24973
diff changeset
   184
            if (!results[RETSTAT].equals("0") && TRANSPORT_ERROR_PTRN.matcher(results[STDERR]).find()) {
24503
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 5506
diff changeset
   185
                // We got expected error, test passed
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 5506
diff changeset
   186
            }
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 5506
diff changeset
   187
            else {
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 5506
diff changeset
   188
                throw new Exception("Test failed: jdwp accept invalid address '" + badAddress + "'");
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 5506
diff changeset
   189
            }
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 5506
diff changeset
   190
        }
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 5506
diff changeset
   191
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        System.out.println("Test passed: status = 0");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
}
24503
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 5506
diff changeset
   195