1 /*
   2  * Copyright (c) 2016, 2017, 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  * @library /test/lib /
  27  * @requires vm.bits == "64" & (os.arch == "amd64" | os.arch == "x86_64") & !(os.family == "windows")
  28  * @modules java.base/jdk.internal.misc
  29  * @build compiler.aot.verification.ClassAndLibraryNotMatchTest
  30  * @run driver compiler.aot.verification.ClassAndLibraryNotMatchTest
  31  * @summary check if class and aot library are properly bound to each other
  32  */
  33 
  34 package compiler.aot.verification;
  35 
  36 import compiler.aot.AotCompiler;
  37 import java.io.File;
  38 import java.io.IOException;
  39 import java.nio.file.Files;
  40 import java.nio.file.Paths;
  41 import java.nio.file.StandardOpenOption;
  42 import java.util.Arrays;
  43 import jdk.test.lib.JDKToolFinder;
  44 import jdk.test.lib.Utils;
  45 import jdk.test.lib.process.OutputAnalyzer;
  46 import jdk.test.lib.process.ProcessTools;
  47 
  48 public class ClassAndLibraryNotMatchTest {
  49     private static final String HELLO_WORLD_CLASS_NAME = "HelloWorld";
  50     private static final String LIB_NAME = "lib" + HELLO_WORLD_CLASS_NAME + ".so";
  51     private static final String HELLO_WORLD_MSG1 = "HelloWorld1";
  52     private static final String HELLO_WORLD_MSG2 = "HelloWorld2";
  53     private static final String HELLO_WORLD_FILE = "./" + HELLO_WORLD_CLASS_NAME + ".java";
  54     private static final String HELLO_WORLD_PRE = "public class "
  55             + HELLO_WORLD_CLASS_NAME + " {\n"
  56             + "    public static void main(String args[]) {\n"
  57             + "        System.out.println(\"";
  58     private static final String HELLO_WORLD_POST = "\");\n"
  59             + "    }\n"
  60             + "}\n";
  61 
  62     public static void main(String args[]) {
  63         new ClassAndLibraryNotMatchTest().runTest();
  64     }
  65 
  66     private void writeHelloWorld(String message) {
  67         String src = HELLO_WORLD_PRE + message + HELLO_WORLD_POST;
  68         try{
  69             Files.write(Paths.get(HELLO_WORLD_FILE), src.getBytes(), StandardOpenOption.CREATE);
  70         } catch (IOException e) {
  71             throw new Error("Can't write HelloWorld " + e, e);
  72         }
  73     }
  74 
  75     private void compileHelloWorld() {
  76         String javac = JDKToolFinder.getCompileJDKTool("javac");
  77         ProcessBuilder pb = new ProcessBuilder(javac, HELLO_WORLD_FILE);
  78         OutputAnalyzer oa;
  79         try {
  80             oa = ProcessTools.executeProcess(pb);
  81         } catch (Exception e) {
  82             throw new Error("Can't compile class " + e, e);
  83         }
  84         oa.shouldHaveExitValue(0);
  85     }
  86 
  87     private void compileAotLibrary() {
  88         AotCompiler.launchCompiler(LIB_NAME, HELLO_WORLD_CLASS_NAME + ".class",
  89                 Arrays.asList("-classpath", Utils.TEST_CLASS_PATH + File.pathSeparator
  90                         + Utils.TEST_SRC), null);
  91     }
  92 
  93     private void runAndCheckHelloWorld(String checkString) {
  94         ProcessBuilder pb;
  95         try {
  96             pb = ProcessTools.createJavaProcessBuilder(true, "-cp", ".",
  97                     "-XX:+UseAOT", "-XX:AOTLibrary=./" + LIB_NAME,
  98                     HELLO_WORLD_CLASS_NAME);
  99         } catch (Exception e) {
 100             throw new Error("Can't create ProcessBuilder to run "
 101                     + HELLO_WORLD_CLASS_NAME + " " + e, e);
 102         }
 103         OutputAnalyzer oa;
 104         try {
 105             oa = ProcessTools.executeProcess(pb);
 106         } catch (Exception e) {
 107             throw new Error("Can't execute " + HELLO_WORLD_CLASS_NAME + " " + e, e);
 108         }
 109         oa.shouldHaveExitValue(0);
 110         oa.shouldContain(checkString);
 111     }
 112 
 113     private void createHelloWorld(String msg) {
 114         writeHelloWorld(msg);
 115         compileHelloWorld();
 116     }
 117 
 118     private void runTest() {
 119         createHelloWorld(HELLO_WORLD_MSG1);
 120         compileAotLibrary();
 121         runAndCheckHelloWorld(HELLO_WORLD_MSG1);
 122         createHelloWorld(HELLO_WORLD_MSG2);
 123         runAndCheckHelloWorld(HELLO_WORLD_MSG2);
 124     }
 125 }