1/* 2 * Copyright (c) 2008, 2015, 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 TestBootNativeLibraryPath.java 26 * @bug 6819213 27 * @modules java.compiler 28 * @compile -XDignore.symbol.file TestBootNativeLibraryPath.java 29 * @summary verify sun.boot.native.library.path is expandable on 32 bit systems 30 * @run main TestBootNativeLibraryPath 31 * @author ksrini 32*/ 33 34import java.io.BufferedReader; 35import java.io.File; 36import java.io.FileOutputStream; 37import java.io.IOException; 38import java.io.InputStreamReader; 39import java.io.PrintStream; 40import java.util.ArrayList; 41import java.util.List; 42import java.util.Map; 43import java.util.logging.Level; 44import java.util.logging.Logger; 45import javax.tools.JavaCompiler; 46import javax.tools.ToolProvider; 47 48public class TestBootNativeLibraryPath { 49 50 private static final String TESTFILE = "Test6"; 51 52 static void createTestClass() throws IOException { 53 FileOutputStream fos = new FileOutputStream(TESTFILE + ".java"); 54 PrintStream ps = new PrintStream(fos); 55 ps.println("public class " + TESTFILE + "{"); 56 ps.println("public static void main(String[] args) {\n"); 57 ps.println("System.out.println(System.getProperty(\"sun.boot.library.path\"));\n"); 58 ps.println("}}\n"); 59 ps.close(); 60 fos.close(); 61 62 JavaCompiler javac = ToolProvider.getSystemJavaCompiler(); 63 String javacOpts[] = {TESTFILE + ".java"}; 64 if (javac.run(null, null, null, javacOpts) != 0) { 65 throw new RuntimeException("compilation of " + TESTFILE + ".java Failed"); 66 } 67 } 68 69 static List<String> doExec(String... args) { 70 String javaCmd = System.getProperty("java.home") + "/bin/java"; 71 if (!new File(javaCmd).exists()) { 72 javaCmd = System.getProperty("java.home") + "/bin/java.exe"; 73 } 74 75 ArrayList<String> cmds = new ArrayList<String>(); 76 cmds.add(javaCmd); 77 for (String x : args) { 78 cmds.add(x); 79 } 80 System.out.println("cmds=" + cmds); 81 ProcessBuilder pb = new ProcessBuilder(cmds); 82 83 Map<String, String> env = pb.environment(); 84 pb.directory(new File(".")); 85 86 List<String> out = new ArrayList<String>(); 87 try { 88 pb.redirectErrorStream(true); 89 Process p = pb.start(); 90 BufferedReader rd = new BufferedReader(new InputStreamReader(p.getInputStream()),8192); 91 String in = rd.readLine(); 92 while (in != null) { 93 out.add(in); 94 System.out.println(in); 95 in = rd.readLine(); 96 } 97 int retval = p.waitFor(); 98 p.destroy(); 99 if (retval != 0) { 100 throw new RuntimeException("Error: test returned non-zero value"); 101 } 102 return out; 103 } catch (Exception ex) { 104 ex.printStackTrace(); 105 throw new RuntimeException(ex.getMessage()); 106 } 107 } 108 109 public static void main(String[] args) { 110 try { 111 if (!System.getProperty("sun.arch.data.model").equals("32")) { 112 System.out.println("Warning: test skipped for 64-bit systems\n"); 113 return; 114 } 115 String osname = System.getProperty("os.name"); 116 if (osname.startsWith("Windows")) { 117 osname = "Windows"; 118 } 119 120 createTestClass(); 121 122 // Test a simple path 123 String libpath = File.pathSeparator + "tmp" + File.pathSeparator + "foobar"; 124 List<String> processOut = null; 125 String sunbootlibrarypath = "-Dsun.boot.library.path=" + libpath; 126 processOut = doExec(sunbootlibrarypath, "-cp", ".", TESTFILE); 127 if (processOut == null || !processOut.get(0).endsWith(libpath)) { 128 throw new RuntimeException("Error: did not get expected error string"); 129 } 130 } catch (IOException ex) { 131 throw new RuntimeException("Unexpected error " + ex); 132 } 133 } 134} 135