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