1 /*
2  * Copyright (c) 2005, 2018, Oracle and/or its affiliates. All rights reserved.
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  *
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.
22  */
23 
24 /*
25  * @test
26  * @bug 6225664 6220618
27  * @summary Test for when private transport library outside jdk
28  * @comment converted from test/jdk/com/sun/jdi/PrivateTransportTest.sh
29  *
30  * @library /test/lib
31  * @run compile -g HelloWorld.java
32  * @build PrivateTransportTest
33  * @run main/othervm PrivateTransportTest
34  */
35 
36 import jdk.test.lib.Utils;
37 import jdk.test.lib.process.ProcessTools;
38 
39 import java.io.IOException;
40 import java.nio.file.Files;
41 import java.nio.file.Path;
42 import java.nio.file.Paths;
43 import java.util.Map;
44 import java.util.Optional;
45 import java.util.stream.Stream;
46 
47 public class PrivateTransportTest {
48 
main(String argv[])49     public static void main(String argv[]) throws Throwable {
50         new PrivateTransportTest()
51                 .test();
52     }
53 
54     private final Path transportLib;
55     private final String pathEnvVar;
56     private final String pathSep;
57 
isTransportLib(Path path)58     private static boolean isTransportLib(Path path) {
59         String fileName = path.getFileName().toString().toLowerCase();
60         return fileName.equals("dt_socket.dll")
61                 || fileName.equals("libdt_socket.so")
62                 || fileName.equals("libdt_socket.dylib");
63     }
64 
PrivateTransportTest()65     private PrivateTransportTest() throws IOException {
66         Path jdkRoot = Paths.get(Utils.TEST_JDK);
67         try (Stream<Path> files = Files.find(jdkRoot, 5, (path, attr) -> isTransportLib(path.getFileName()))) {
68             Optional<Path> foundLib = files.findAny();
69             if (!foundLib.isPresent()) {
70                 throw new RuntimeException("cannot find dt_socket lib");
71             }
72             transportLib = foundLib.get();
73         }
74         if (System.getProperty("os.name").startsWith("Windows")) {
75             pathSep = ";";
76             pathEnvVar = "PATH";
77         } else {
78             pathSep = ":";
79             if (System.getProperty("os.name").equals("AIX")) {
80                 pathEnvVar = "LIBPATH";
81             } else {
82                 pathEnvVar = "LD_LIBRARY_PATH";
83             }
84         }
85     }
86 
test()87     private void test() throws Throwable {
88         // copy existing dt_socket lib to <CLASSES>/private_dt_socket
89         String libName = transportLib.getFileName().toString().replace("dt_socket", "private_dt_socket");
90         Files.copy(transportLib, Paths.get(Utils.TEST_CLASSES).resolve(libName));
91 
92         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(true,
93                 "-agentlib:jdwp=transport=private_dt_socket,server=y,suspend=n",
94                 "-classpath", Utils.TEST_CLASSES,
95                 "HelloWorld");
96         Map<String, String> env = pb.environment();
97         String pathValue = env.get(pathEnvVar);
98         if (pathValue == null) {
99             pathValue = Utils.TEST_CLASSES;
100         } else {
101             pathValue = pathValue + pathSep + Utils.TEST_CLASSES;
102         }
103         env.put(pathEnvVar, pathValue);
104 
105         ProcessTools.executeCommand(pb)
106                 .shouldHaveExitValue(0);
107     }
108 }
109