1 /*
   2  * Copyright (c) 2007, 2014, 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 CommandLineTests.sh
  26  * @bug  6521334 6965836 6965836
  27  * @ignore 8059906
  28  * @compile -XDignore.symbol.file CommandLineTests.java Pack200Test.java
  29  * @run main/timeout=1200 CommandLineTests
  30  * @summary An ad hoc test to verify the behavior of pack200/unpack200 CLIs,
  31  *           and a simulation of pack/unpacking in the install repo.
  32  * @author ksrini
  33  */
  34 
  35 import java.io.File;
  36 import java.io.FileOutputStream;
  37 import java.io.IOException;
  38 import java.io.PrintStream;
  39 import java.util.ArrayList;
  40 import java.util.List;
  41 /*
  42  * We try a potpouri of things ie. we have pack.conf to setup some
  43  * options as well as a couple of command line options. We also test
  44  * the packing and unpacking mechanism using the Java APIs. This also
  45  * simulates pack200 the install workspace, noting that this is a simulation
  46  * and can only test jars that are guaranteed to be available, also the
  47  * configuration may not be in sync with the installer workspace.
  48  */
  49 
  50 public class CommandLineTests {
  51     private static final File CWD = new File(".");
  52     private static final File EXP_SDK = new File(CWD, "exp-sdk-image");
  53     private static final File EXP_SDK_LIB_DIR = new File(EXP_SDK, "lib");
  54     private static final File EXP_SDK_BIN_DIR = new File(EXP_SDK, "bin");
  55     private static final File EXP_JRE_DIR = new File(EXP_SDK, "jre");
  56     private static final File EXP_JRE_LIB_DIR = new File(EXP_JRE_DIR, "lib");
  57     private static final File RtJar = new File(EXP_JRE_LIB_DIR, "rt.jar");
  58     private static final File CharsetsJar = new File(EXP_JRE_LIB_DIR, "charsets.jar");
  59     private static final File JsseJar = new File(EXP_JRE_LIB_DIR, "jsse.jar");
  60     private static final File ToolsJar = new File(EXP_SDK_LIB_DIR, "tools.jar");
  61     private static final File javaCmd;
  62     private static final File javacCmd;
  63     private static final File ConfigFile = new File("pack.conf");
  64     private static final List<File> jarList;
  65 
  66     static {
  67         javaCmd = Utils.IsWindows
  68                     ? new File(EXP_SDK_BIN_DIR, "java.exe")
  69                     : new File(EXP_SDK_BIN_DIR, "java");
  70 
  71         javacCmd = Utils.IsWindows
  72                     ? new File(EXP_SDK_BIN_DIR, "javac.exe")
  73                     : new File(EXP_SDK_BIN_DIR, "javac");
  74 
  75         jarList = new ArrayList<File>();
  76         jarList.add(RtJar);
  77         jarList.add(CharsetsJar);
  78         jarList.add(JsseJar);
  79         jarList.add(ToolsJar);
  80     }
  81 
  82     // init test area with a copy of the sdk
  83     static void init() throws IOException {
  84             Utils.recursiveCopy(Utils.JavaSDK, EXP_SDK);
  85             creatConfigFile();
  86     }
  87     // cleanup the test area
  88     static void cleanup() throws IOException {
  89         Utils.recursiveDelete(EXP_SDK);
  90         Utils.cleanup();
  91     }
  92 
  93     // Hopefully, this should be kept in sync with what the installer does.
  94     static void creatConfigFile() throws IOException {
  95         FileOutputStream fos = null;
  96         PrintStream ps = null;
  97         try {
  98             fos = new FileOutputStream(ConfigFile);
  99             ps = new PrintStream(fos);
 100             ps.println("com.sun.java.util.jar.pack.debug.verbose=0");
 101             ps.println("pack.modification.time=keep");
 102             ps.println("pack.keep.class.order=true");
 103             ps.println("pack.deflate.hint=false");
 104             // Fail the build, if new or unknown attributes are introduced.
 105             ps.println("pack.unknown.attribute=error");
 106             ps.println("pack.segment.limit=-1");
 107             // BugId: 6328502,  These files will be passed-through as-is.
 108             ps.println("pack.pass.file.0=java/lang/Error.class");
 109             ps.println("pack.pass.file.1=java/lang/LinkageError.class");
 110             ps.println("pack.pass.file.2=java/lang/Object.class");
 111             ps.println("pack.pass.file.3=java/lang/Throwable.class");
 112             ps.println("pack.pass.file.4=java/lang/VerifyError.class");
 113             ps.println("pack.pass.file.5=com/sun/demo/jvmti/hprof/Tracker.class");
 114         } finally {
 115             Utils.close(ps);
 116             Utils.close(fos);
 117         }
 118     }
 119 
 120     static void runPack200(boolean jre) throws IOException {
 121         List<String> cmdsList = new ArrayList<String>();
 122         for (File f : jarList) {
 123             if (jre && f.getName().equals("tools.jar")) {
 124                 continue;  // need not worry about tools.jar for JRE
 125             }
 126             // make a backup copy for re-use
 127             File bakFile = new File(f.getName() + ".bak");
 128             if (!bakFile.exists()) {  // backup
 129                 Utils.copyFile(f, bakFile);
 130             } else {  // restore
 131                 Utils.copyFile(bakFile, f);
 132             }
 133             cmdsList.clear();
 134             cmdsList.add(Utils.getPack200Cmd());
 135             cmdsList.add("-J-esa");
 136             cmdsList.add("-J-ea");
 137             cmdsList.add(Utils.Is64Bit ? "-J-Xmx1g" : "-J-Xmx512m");
 138             cmdsList.add("--repack");
 139             cmdsList.add("--config-file=" + ConfigFile.getAbsolutePath());
 140             if (jre) {
 141                 cmdsList.add("--strip-debug");
 142             }
 143             // NOTE: commented until 6965836 is fixed
 144             // cmdsList.add("--code-attribute=StackMapTable=strip");
 145             cmdsList.add(f.getAbsolutePath());
 146             Utils.runExec(cmdsList);
 147         }
 148     }
 149 
 150     static void testJRE() throws IOException {
 151         runPack200(true);
 152         // the speciment JRE
 153         List<String> cmdsList = new ArrayList<String>();
 154         cmdsList.add(javaCmd.getAbsolutePath());
 155         cmdsList.add("-verify");
 156         cmdsList.add("-version");
 157         Utils.runExec(cmdsList);
 158     }
 159 
 160     static void testJDK() throws IOException {
 161         runPack200(false);
 162         // test the specimen JDK
 163         List<String> cmdsList = new ArrayList<String>();
 164         cmdsList.add(javaCmd.getAbsolutePath());
 165         cmdsList.add("-verify");
 166         cmdsList.add("-version");
 167         Utils.runExec(cmdsList);
 168 
 169         // invoke javac to test the tools.jar
 170         cmdsList.clear();
 171         cmdsList.add(javacCmd.getAbsolutePath());
 172         cmdsList.add("-J-verify");
 173         cmdsList.add("-help");
 174         Utils.runExec(cmdsList);
 175     }
 176     public static void main(String... args) {
 177         try {
 178             init();
 179             testJRE();
 180             testJDK();
 181             cleanup(); // cleanup only if we pass successfully
 182         } catch (IOException ioe) {
 183             throw new RuntimeException(ioe);
 184         }
 185     }
 186 }