1 | /*
|
---|
2 | * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
|
---|
3 | * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
|
---|
4 | */
|
---|
5 | import java.io.BufferedReader;
|
---|
6 | import java.io.File;
|
---|
7 | import java.io.InputStreamReader;
|
---|
8 | import java.io.PrintStream;
|
---|
9 | import java.util.ArrayList;
|
---|
10 | import java.util.HashMap;
|
---|
11 | import java.util.List;
|
---|
12 | import java.util.Map;
|
---|
13 | /*
|
---|
14 | * @test LibraryPath
|
---|
15 | * @bug 6983554
|
---|
16 | * @build LibraryPath
|
---|
17 | * @run main LibraryPath
|
---|
18 | * @summary Verify that an empty LD_LIBRARY_PATH is ignored on Unix.
|
---|
19 | * @author ksrini
|
---|
20 | */
|
---|
21 | public class LibraryPath {
|
---|
22 | static final String JAVAHOME = System.getProperty("java.home");
|
---|
23 | static final boolean isSDK = JAVAHOME.endsWith("jre");
|
---|
24 | static final String javaCmd;
|
---|
25 | static final String java64Cmd;
|
---|
26 | static final String javacCmd;
|
---|
27 | static final boolean isWindows =
|
---|
28 | System.getProperty("os.name", "unknown").startsWith("Windows");
|
---|
29 | static final boolean is64Bit =
|
---|
30 | System.getProperty("sun.arch.data.model").equals("64");
|
---|
31 | static final boolean is32Bit =
|
---|
32 | System.getProperty("sun.arch.data.model").equals("32");
|
---|
33 | static final boolean isSolaris =
|
---|
34 | System.getProperty("os.name", "unknown").startsWith("SunOS");
|
---|
35 | static final boolean isLinux =
|
---|
36 | System.getProperty("os.name", "unknown").startsWith("Linux");
|
---|
37 | static final boolean isDualMode = isSolaris;
|
---|
38 | static final boolean isSparc = System.getProperty("os.arch").startsWith("sparc");
|
---|
39 |
|
---|
40 | static final String LLP = "LD_LIBRARY_PATH";
|
---|
41 | static final String LLP32 = LLP + "_32";
|
---|
42 | static final String LLP64 = LLP + "_64";
|
---|
43 | static final String JLP = "java.library.path";
|
---|
44 |
|
---|
45 | static {
|
---|
46 | assert is64Bit ^ is32Bit;
|
---|
47 |
|
---|
48 | File binDir = (isSDK) ? new File((new File(JAVAHOME)).getParentFile(), "bin")
|
---|
49 | : new File(JAVAHOME, "bin");
|
---|
50 | File javaCmdFile = (isWindows)
|
---|
51 | ? new File(binDir, "java.exe")
|
---|
52 | : new File(binDir, "java");
|
---|
53 | javaCmd = javaCmdFile.getAbsolutePath();
|
---|
54 | if (!javaCmdFile.canExecute()) {
|
---|
55 | throw new RuntimeException("java <" + javaCmd + "> must exist");
|
---|
56 | }
|
---|
57 |
|
---|
58 | File javacCmdFile = (isWindows)
|
---|
59 | ? new File(binDir, "javac.exe")
|
---|
60 | : new File(binDir, "javac");
|
---|
61 | javacCmd = javacCmdFile.getAbsolutePath();
|
---|
62 | if (!javacCmdFile.canExecute()) {
|
---|
63 | throw new RuntimeException("java <" + javacCmd + "> must exist");
|
---|
64 | }
|
---|
65 | if (isSolaris) {
|
---|
66 | File sparc64BinDir = new File(binDir, isSparc ? "sparcv9" : "amd64");
|
---|
67 | File java64CmdFile = new File(sparc64BinDir, "java");
|
---|
68 | if (java64CmdFile.exists() && java64CmdFile.canExecute()) {
|
---|
69 | java64Cmd = java64CmdFile.getAbsolutePath();
|
---|
70 | } else {
|
---|
71 | java64Cmd = null;
|
---|
72 | }
|
---|
73 | } else {
|
---|
74 | java64Cmd = null;
|
---|
75 | }
|
---|
76 | }
|
---|
77 |
|
---|
78 | /*
|
---|
79 | * usually the jre/lib/arch-name is the same as os.arch, except for x86.
|
---|
80 | */
|
---|
81 | static String getJreArch() {
|
---|
82 | String arch = System.getProperty("os.arch");
|
---|
83 | return arch.equals("x86") ? "i386" : arch;
|
---|
84 | }
|
---|
85 | /*
|
---|
86 | * A method which executes a java cmd and returns the results in a container
|
---|
87 | */
|
---|
88 | static TestResult doExec(Map<String, String> envToSet, String... cmds) {
|
---|
89 | String cmdStr = "";
|
---|
90 | for (String x : cmds) {
|
---|
91 | cmdStr = cmdStr.concat(x + " ");
|
---|
92 | }
|
---|
93 | System.out.println(cmdStr);
|
---|
94 | ProcessBuilder pb = new ProcessBuilder(cmds);
|
---|
95 | Map<String, String> env = pb.environment();
|
---|
96 | if (envToSet != null) {
|
---|
97 | env.putAll(envToSet);
|
---|
98 | }
|
---|
99 |
|
---|
100 | BufferedReader rdr = null;
|
---|
101 | Process p = null;
|
---|
102 | try {
|
---|
103 | List<String> outputList = new ArrayList<String>();
|
---|
104 | pb.redirectErrorStream(true);
|
---|
105 | p = pb.start();
|
---|
106 | rdr = new BufferedReader(new InputStreamReader(p.getInputStream()));
|
---|
107 | String in = rdr.readLine();
|
---|
108 | while (in != null) {
|
---|
109 | outputList.add(in);
|
---|
110 | in = rdr.readLine();
|
---|
111 | }
|
---|
112 | p.waitFor();
|
---|
113 | p.destroy();
|
---|
114 | return new TestResult(p.exitValue(), outputList);
|
---|
115 | } catch (Exception ex) {
|
---|
116 | ex.printStackTrace();
|
---|
117 | throw new RuntimeException(ex.getMessage());
|
---|
118 | } finally {
|
---|
119 | p.destroy();
|
---|
120 | }
|
---|
121 | }
|
---|
122 | /*
|
---|
123 | * test if all the LLP* enviroment variables and java.library.path are set
|
---|
124 | * correctly in the child, that is any empty value should not be trickled
|
---|
125 | * through, and should not be translated to a "." either.
|
---|
126 | */
|
---|
127 | static void checkStrings(String name, String value) {
|
---|
128 | if (value != null) {
|
---|
129 | System.out.println(name + "=" + value);
|
---|
130 | String s[] = value.split(":");
|
---|
131 | for (String x : s) {
|
---|
132 | if (x.equals(".")) {
|
---|
133 | throw new Error("found a . in " + name);
|
---|
134 | }
|
---|
135 | if (x.trim().equals("")) {
|
---|
136 | throw new Error("found an empty string in " + name);
|
---|
137 | }
|
---|
138 | }
|
---|
139 | System.out.println("OK");
|
---|
140 | }
|
---|
141 | }
|
---|
142 |
|
---|
143 | static void doTest(Map<String, String> envMap, String javaCmd) {
|
---|
144 | TestResult tr = doExec(envMap, javaCmd, "LibraryPath", "run");
|
---|
145 | tr.print(System.out);
|
---|
146 | if (tr.exitCode != 0) {
|
---|
147 | throw new Error("Test fails");
|
---|
148 | }
|
---|
149 | }
|
---|
150 |
|
---|
151 | /*
|
---|
152 | * This test behaves like a fork/exec but synchronously, ie. with no
|
---|
153 | * arguments, the parent java process sets the various environmental
|
---|
154 | * variables required for a given platform and re-execs this main with
|
---|
155 | * a "run" argument, and the child tests if the required variables
|
---|
156 | * and properties are sane.
|
---|
157 | */
|
---|
158 | public static void main(String[] args) {
|
---|
159 | // no windows please
|
---|
160 | if (isWindows) {
|
---|
161 | return;
|
---|
162 | }
|
---|
163 | if (args != null && args.length > 0 && args[0].equals("run")) {
|
---|
164 | checkStrings(LLP, System.getenv(LLP));
|
---|
165 | checkStrings(JLP, System.getProperty(JLP, null));
|
---|
166 | checkStrings(LLP32, System.getenv(LLP32));
|
---|
167 | checkStrings(LLP64, System.getenv(LLP64));
|
---|
168 | } else {
|
---|
169 | Map<String, String> envMap = new HashMap<String, String>();
|
---|
170 |
|
---|
171 | // test with a null string
|
---|
172 | envMap.put(LLP, "");
|
---|
173 | doTest(envMap, javaCmd);
|
---|
174 |
|
---|
175 | // test the Solaris variants now
|
---|
176 | if (isSolaris && is32Bit) {
|
---|
177 | envMap.put(LLP32, "");
|
---|
178 | doTest(envMap, javaCmd);
|
---|
179 | }
|
---|
180 |
|
---|
181 | // if we have 64-bit variant, ie. dual-mode jre, then test that too.
|
---|
182 | if (isDualMode && java64Cmd != null) {
|
---|
183 | envMap.clear(); // get rid of 32-bit'isms
|
---|
184 | envMap.put(LLP, "");
|
---|
185 | envMap.put(LLP64, "");
|
---|
186 | doTest(envMap, java64Cmd);
|
---|
187 | }
|
---|
188 | }
|
---|
189 | }
|
---|
190 | }
|
---|
191 |
|
---|
192 | class TestResult {
|
---|
193 | int exitCode = 0;
|
---|
194 | List<String> outputList = null;
|
---|
195 | public TestResult(int exitCode, List<String> outputList) {
|
---|
196 | this.exitCode = exitCode;
|
---|
197 | this.outputList = outputList;
|
---|
198 | }
|
---|
199 | public void print(PrintStream ps) {
|
---|
200 | for (String x : outputList) {
|
---|
201 | ps.println(x);
|
---|
202 | }
|
---|
203 | }
|
---|
204 | }
|
---|