1 /*
2  * Copyright (c) 2005, 2019, 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.Platform;
37 import jdk.test.lib.Utils;
38 import jdk.test.lib.process.ProcessTools;
39 
40 import java.io.IOException;
41 import java.io.File;
42 import java.nio.file.Files;
43 import java.nio.file.Path;
44 import java.nio.file.Paths;
45 import java.util.Map;
46 import java.util.Optional;
47 import java.util.stream.Stream;
48 
49 public class PrivateTransportTest {
50 
main(String argv[])51     public static void main(String argv[]) throws Throwable {
52         new PrivateTransportTest()
53                 .test();
54     }
55 
56     private final Path transportLib;
57     private final String pathEnvVar;
58     private final String pathSep;
59 
isTransportLib(Path path)60     private static boolean isTransportLib(Path path) {
61         String fileName = path.getFileName().toString().toLowerCase();
62         return fileName.equals("dt_socket.dll")
63                 || fileName.equals("libdt_socket.so")
64                 || fileName.equals("libdt_socket.dylib");
65     }
66 
PrivateTransportTest()67     private PrivateTransportTest() throws IOException {
68         Path jdkRoot = Paths.get(Utils.TEST_JDK);
69         try (Stream<Path> files = Files.find(jdkRoot, 5, (path, attr) -> isTransportLib(path.getFileName()))) {
70             Optional<Path> foundLib = files.findAny();
71             if (!foundLib.isPresent()) {
72                 throw new RuntimeException("cannot find dt_socket lib");
73             }
74             transportLib = foundLib.get();
75         }
76         pathEnvVar = Platform.sharedLibraryPathVariableName();
77         pathSep    = File.pathSeparator;
78     }
79 
test()80     private void test() throws Throwable {
81         // copy existing dt_socket lib to <CLASSES>/private_dt_socket
82         String libName = transportLib.getFileName().toString().replace("dt_socket", "private_dt_socket");
83         Files.copy(transportLib, Paths.get(Utils.TEST_CLASSES).resolve(libName));
84 
85         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(true,
86                 "-agentlib:jdwp=transport=private_dt_socket,server=y,suspend=n",
87                 "-classpath", Utils.TEST_CLASSES,
88                 "HelloWorld");
89         Map<String, String> env = pb.environment();
90         String pathValue = env.get(pathEnvVar);
91         if (pathValue == null) {
92             pathValue = Utils.TEST_CLASSES;
93         } else {
94             pathValue = pathValue + pathSep + Utils.TEST_CLASSES;
95         }
96         env.put(pathEnvVar, pathValue);
97 
98         ProcessTools.executeCommand(pb)
99                 .shouldHaveExitValue(0);
100     }
101 }
102