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 import java.nio.file.Path;
  25 import java.io.BufferedReader;
  26 import java.io.ByteArrayOutputStream;
  27 import java.io.Closeable;
  28 import java.io.File;
  29 import java.io.FileFilter;
  30 import java.io.FileOutputStream;
  31 import java.io.IOException;
  32 import java.io.InputStream;
  33 import java.io.InputStreamReader;
  34 import java.io.PrintStream;
  35 import java.nio.charset.Charset;
  36 import java.nio.file.Files;
  37 import java.util.ArrayList;
  38 import java.util.Arrays;
  39 import java.util.Collections;
  40 import java.util.List;
  41 import java.util.Map;
  42 import java.util.jar.JarFile;
  43 import java.util.jar.JarOutputStream;
  44 import java.util.jar.Pack200;
  45 import java.util.zip.ZipEntry;
  46 import java.util.zip.ZipFile;
  47 
  48 import static java.nio.file.StandardCopyOption.*;
  49 import static java.nio.file.StandardOpenOption.*;
  50 
  51 /**
  52  *
  53  * @author ksrini
  54  */
  55 
  56 /*
  57  * This class contains the commonly used utilities.
  58  */
  59 class Utils {
  60     static final String JavaHome = System.getProperty("test.java",
  61             System.getProperty("java.home"));
  62     static final boolean IsWindows =
  63             System.getProperty("os.name").startsWith("Windows");
  64     static final boolean Is64Bit =
  65             System.getProperty("sun.arch.data.model", "32").equals("64");
  66     static final File   JavaSDK =  new File(JavaHome);
  67 
  68     static final String PACK_FILE_EXT   = ".pack";
  69     static final String JAVA_FILE_EXT   = ".java";
  70     static final String CLASS_FILE_EXT  = ".class";
  71     static final String JAR_FILE_EXT    = ".jar";
  72 
  73     static final File   TEST_SRC_DIR = new File(System.getProperty("test.src"));
  74     static final File   TEST_CLS_DIR = new File(System.getProperty("test.classes"));
  75     static final String VERIFIER_DIR_NAME = "pack200-verifier";
  76     static final File   VerifierJar = new File(VERIFIER_DIR_NAME + JAR_FILE_EXT);
  77     static final File   XCLASSES = new File("xclasses");
  78 
  79     private Utils() {} // all static
  80 
  81     private static void init() throws IOException {
  82         if (VerifierJar.exists()) {
  83             return;
  84         }
  85         File srcDir = new File(getVerifierDir(), "src");
  86         List<File> javaFileList = findFiles(srcDir, createFilter(JAVA_FILE_EXT));
  87         File tmpFile = File.createTempFile("javac", ".tmp");
  88         XCLASSES.mkdirs();
  89         FileOutputStream fos = null;
  90         PrintStream ps = null;
  91         try {
  92             fos = new FileOutputStream(tmpFile);
  93             ps = new PrintStream(fos);
  94             for (File f : javaFileList) {
  95                 ps.println(f.getAbsolutePath());
  96             }
  97         } finally {
  98             close(ps);
  99             close(fos);
 100         }
 101 
 102         compiler("-d",
 103                 XCLASSES.getName(),
 104                 "@" + tmpFile.getAbsolutePath());
 105 
 106         jar("cvfe",
 107             VerifierJar.getName(),
 108             "sun.tools.pack.verify.Main",
 109             "-C",
 110             XCLASSES.getName(),
 111             ".");
 112     }
 113 
 114     private static File getVerifierDir() {
 115         File srcDir = new File(TEST_SRC_DIR, VERIFIER_DIR_NAME);
 116         if (!srcDir.exists()) {
 117             // if not available try one level above
 118             srcDir = new File(TEST_SRC_DIR.getParentFile(), VERIFIER_DIR_NAME);
 119         }
 120         return srcDir;
 121     }
 122 
 123     static File getGoldenJar() {
 124         return new File(new File(getVerifierDir(), "data"), "golden.jar");
 125     }
 126     static void dirlist(File dir) {
 127         File[] files = dir.listFiles();
 128         System.out.println("--listing " + dir.getAbsolutePath() + "---");
 129         for (File f : files) {
 130             StringBuffer sb = new StringBuffer();
 131             sb.append(f.isDirectory() ? "d " : "- ");
 132             sb.append(f.getName());
 133             System.out.println(sb);
 134         }
 135     }
 136     static void doCompareVerify(File reference, File specimen) throws IOException {
 137         init();
 138         List<String> cmds = new ArrayList<String>();
 139         cmds.add(getJavaCmd());
 140         cmds.add("-cp");
 141         cmds.add(VerifierJar.getName());
 142         cmds.add("sun.tools.pack.verify.Main");
 143         cmds.add(reference.getAbsolutePath());
 144         cmds.add(specimen.getAbsolutePath());
 145         cmds.add("-O");
 146         runExec(cmds);
 147     }
 148 
 149     static void doCompareBitWise(File reference, File specimen)
 150             throws IOException {
 151         init();
 152         List<String> cmds = new ArrayList<String>();
 153         cmds.add(getJavaCmd());
 154         cmds.add("-cp");
 155         cmds.add(VerifierJar.getName());
 156         cmds.add("sun.tools.pack.verify.Main");
 157         cmds.add(reference.getName());
 158         cmds.add(specimen.getName());
 159         cmds.add("-O");
 160         cmds.add("-b");
 161         runExec(cmds);
 162     }
 163 
 164     static FileFilter createFilter(final String extension) {
 165         return new FileFilter() {
 166             @Override
 167             public boolean accept(File pathname) {
 168                 String name = pathname.getName();
 169                 if (name.endsWith(extension)) {
 170                     return true;
 171                 }
 172                 return false;
 173             }
 174         };
 175     }
 176 
 177     /*
 178      * clean up all the usual suspects
 179      */
 180     static void cleanup() throws IOException {
 181         recursiveDelete(XCLASSES);
 182         List<File> toDelete = new ArrayList<>();
 183         toDelete.addAll(Utils.findFiles(new File("."),
 184                 Utils.createFilter(".out")));
 185         toDelete.addAll(Utils.findFiles(new File("."),
 186                 Utils.createFilter(".bak")));
 187         toDelete.addAll(Utils.findFiles(new File("."),
 188                 Utils.createFilter(".jar")));
 189         toDelete.addAll(Utils.findFiles(new File("."),
 190                 Utils.createFilter(".pack")));
 191         toDelete.addAll(Utils.findFiles(new File("."),
 192                 Utils.createFilter(".bnd")));
 193         toDelete.addAll(Utils.findFiles(new File("."),
 194                 Utils.createFilter(".txt")));
 195         toDelete.addAll(Utils.findFiles(new File("."),
 196                 Utils.createFilter(".idx")));
 197         toDelete.addAll(Utils.findFiles(new File("."),
 198                 Utils.createFilter(".gidx")));
 199         for (File f : toDelete) {
 200             f.delete();
 201         }
 202     }
 203 
 204     static final FileFilter DIR_FILTER = new FileFilter() {
 205         public boolean accept(File pathname) {
 206             if (pathname.isDirectory()) {
 207                 return true;
 208             }
 209             return false;
 210         }
 211     };
 212 
 213     static final FileFilter FILE_FILTER = new FileFilter() {
 214         public boolean accept(File pathname) {
 215             if (pathname.isFile()) {
 216                 return true;
 217             }
 218             return false;
 219         }
 220     };
 221 
 222     static void copyFile(File src, File dst) throws IOException {
 223         Path parent = dst.toPath().getParent();
 224         if (parent != null) {
 225             Files.createDirectories(parent);
 226         }
 227         Files.copy(src.toPath(), dst.toPath(), COPY_ATTRIBUTES, REPLACE_EXISTING);
 228         if (dst.isDirectory() && !dst.canWrite()) {
 229             dst.setWritable(true);
 230         }
 231     }
 232 
 233     static String baseName(File file, String extension) {
 234         return baseName(file.getAbsolutePath(), extension);
 235     }
 236 
 237     static String baseName(String name, String extension) {
 238         int cut = name.length() - extension.length();
 239         return name.lastIndexOf(extension) == cut
 240                 ? name.substring(0, cut)
 241                 : name;
 242 
 243     }
 244    static void createFile(File outFile, List<String> content) throws IOException {
 245         Files.write(outFile.getAbsoluteFile().toPath(), content,
 246                 Charset.defaultCharset(), CREATE_NEW, TRUNCATE_EXISTING);
 247     }
 248 
 249     /*
 250      * Suppose a path is provided which consists of a full path
 251      * this method returns the sub path for a full path ex: /foo/bar/baz/foobar.z
 252      * and the base path is /foo/bar it will will return baz/foobar.z.
 253      */
 254     private static String getEntryPath(String basePath, String fullPath) {
 255         if (!fullPath.startsWith(basePath)) {
 256             return null;
 257         }
 258         return fullPath.substring(basePath.length());
 259     }
 260 
 261     static String getEntryPath(File basePathFile, File fullPathFile) {
 262         return getEntryPath(basePathFile.toString(), fullPathFile.toString());
 263     }
 264 
 265     public static void recursiveCopy(File src, File dest) throws IOException {
 266         if (!src.exists() || !src.canRead()) {
 267             throw new IOException("file not found or readable: " + src);
 268         }
 269         if (dest.exists() && !dest.isDirectory() && !dest.canWrite()) {
 270             throw new IOException("file not found or writeable: " + dest);
 271         }
 272         if (!dest.exists()) {
 273             dest.mkdirs();
 274         }
 275         List<File> a = directoryList(src);
 276         for (File f : a) {
 277             copyFile(f, new File(dest, getEntryPath(src, f)));
 278         }
 279     }
 280 
 281     static List<File> directoryList(File dirname) {
 282         List<File>  dirList = new ArrayList<File>();
 283         return directoryList(dirname, dirList, null);
 284     }
 285 
 286     private static List<File> directoryList(File dirname, List<File> dirList,
 287             File[] dirs) {
 288         dirList.addAll(Arrays.asList(dirname.listFiles(FILE_FILTER)));
 289         dirs = dirname.listFiles(DIR_FILTER);
 290         for (File f : dirs) {
 291             if (f.isDirectory() && !f.equals(dirname)) {
 292                 dirList.add(f);
 293                 directoryList(f, dirList, dirs);
 294             }
 295         }
 296         return dirList;
 297     }
 298 
 299     static void recursiveDelete(File dir) throws IOException {
 300         if (dir.isFile()) {
 301             dir.delete();
 302         } else if (dir.isDirectory()) {
 303             File[] entries = dir.listFiles();
 304             for (int i = 0; i < entries.length; i++) {
 305                 if (entries[i].isDirectory()) {
 306                     recursiveDelete(entries[i]);
 307                 }
 308                 entries[i].delete();
 309             }
 310             dir.delete();
 311         }
 312     }
 313 
 314     static List<File> findFiles(File startDir, FileFilter filter)
 315             throws IOException {
 316         List<File> list = new ArrayList<File>();
 317         findFiles0(startDir, list, filter);
 318         return list;
 319     }
 320     /*
 321      * finds files in the start directory using the the filter, appends
 322      * the files to the dirList.
 323      */
 324     private static void findFiles0(File startDir, List<File> list,
 325                                     FileFilter filter) throws IOException {
 326         File[] foundFiles = startDir.listFiles(filter);
 327         list.addAll(Arrays.asList(foundFiles));
 328         File[] dirs = startDir.listFiles(DIR_FILTER);
 329         for (File dir : dirs) {
 330             findFiles0(dir, list, filter);
 331         }
 332     }
 333 
 334     static void close(Closeable c) {
 335         if (c == null) {
 336             return;
 337         }
 338         try {
 339             c.close();
 340         } catch (IOException ignore) {
 341         }
 342     }
 343 
 344     static void compiler(String... javacCmds) {
 345         List<String> cmdList = new ArrayList<>();
 346         cmdList.add(getJavacCmd());
 347         for (String x : javacCmds) {
 348             cmdList.add(x);
 349         }
 350         runExec(cmdList);
 351     }
 352 
 353     static void jar(String... jargs) {
 354         List<String> cmdList = new ArrayList<>();
 355         cmdList.add(getJarCmd());
 356         for (String x : jargs) {
 357             cmdList.add(x);
 358         }
 359         runExec(cmdList);
 360     }
 361 
 362     static void testWithRepack(File inFile, String... repackOpts) throws IOException {
 363         File cwd = new File(".");
 364         // pack using --repack in native mode
 365         File nativejarFile = new File(cwd, "out-n" + Utils.JAR_FILE_EXT);
 366         repack(inFile, nativejarFile, false, repackOpts);
 367         doCompareVerify(inFile, nativejarFile);
 368 
 369         // ensure bit compatibility between the unpacker variants
 370         File javajarFile = new File(cwd, "out-j" + Utils.JAR_FILE_EXT);
 371         repack(inFile, javajarFile, true, repackOpts);
 372         doCompareBitWise(javajarFile, nativejarFile);
 373     }
 374 
 375     static List<String> repack(File inFile, File outFile,
 376             boolean disableNative, String... extraOpts) {
 377         List<String> cmdList = new ArrayList<>();
 378         cmdList.clear();
 379         cmdList.add(Utils.getJavaCmd());
 380         cmdList.add("-ea");
 381         cmdList.add("-esa");
 382         if (disableNative) {
 383             cmdList.add("-Dcom.sun.java.util.jar.pack.disable.native=true");
 384         }
 385         cmdList.add("com.sun.java.util.jar.pack.Driver");
 386         cmdList.add("--repack");
 387         if (extraOpts != null) {
 388            for (String opt: extraOpts) {
 389                cmdList.add(opt);
 390            }
 391         }
 392         cmdList.add(outFile.getName());
 393         cmdList.add(inFile.getName());
 394         return Utils.runExec(cmdList);
 395     }
 396 
 397     // given a jar file foo.jar will write to foo.pack
 398     static void pack(JarFile jarFile, File packFile) throws IOException {
 399         Pack200.Packer packer = Pack200.newPacker();
 400         Map<String, String> p = packer.properties();
 401         // Take the time optimization vs. space
 402         p.put(packer.EFFORT, "1");  // CAUTION: do not use 0.
 403         // Make the memory consumption as effective as possible
 404         p.put(packer.SEGMENT_LIMIT, "10000");
 405         // ignore all JAR deflation requests to save time
 406         p.put(packer.DEFLATE_HINT, packer.FALSE);
 407         // save the file ordering of the original JAR
 408         p.put(packer.KEEP_FILE_ORDER, packer.TRUE);
 409         FileOutputStream fos = null;
 410         try {
 411             // Write out to a jtreg scratch area
 412             fos = new FileOutputStream(packFile);
 413             // Call the packer
 414             packer.pack(jarFile, fos);
 415         } finally {
 416             close(fos);
 417         }
 418     }
 419 
 420     // uses java unpacker, slow but useful to discover issues with the packer
 421     static void unpackj(File inFile, JarOutputStream jarStream)
 422             throws IOException {
 423         unpack0(inFile, jarStream, true);
 424 
 425     }
 426 
 427     // uses native unpacker using the java APIs
 428     static void unpackn(File inFile, JarOutputStream jarStream)
 429             throws IOException {
 430         unpack0(inFile, jarStream, false);
 431     }
 432 
 433     // given a packed file, create the jar file in the current directory.
 434     private static void unpack0(File inFile, JarOutputStream jarStream,
 435             boolean useJavaUnpack) throws IOException {
 436         // Unpack the files
 437         Pack200.Unpacker unpacker = Pack200.newUnpacker();
 438         Map<String, String> props = unpacker.properties();
 439         if (useJavaUnpack) {
 440             props.put("com.sun.java.util.jar.pack.disable.native", "true");
 441         }
 442         // Call the unpacker
 443         unpacker.unpack(inFile, jarStream);
 444     }
 445 
 446     static byte[] getBuffer(ZipFile zf, ZipEntry ze) throws IOException {
 447         ByteArrayOutputStream baos = new ByteArrayOutputStream();
 448         byte buf[] = new byte[8192];
 449         InputStream is = null;
 450         try {
 451             is = zf.getInputStream(ze);
 452             int n = is.read(buf);
 453             while (n > 0) {
 454                 baos.write(buf, 0, n);
 455                 n = is.read(buf);
 456             }
 457             return baos.toByteArray();
 458         } finally {
 459             close(is);
 460         }
 461     }
 462 
 463     static ArrayList<String> getZipFileEntryNames(ZipFile z) {
 464         ArrayList<String> out = new ArrayList<String>();
 465         for (ZipEntry ze : Collections.list(z.entries())) {
 466             out.add(ze.getName());
 467         }
 468         return out;
 469     }
 470     static List<String> runExec(List<String> cmdsList) {
 471         return runExec(cmdsList, null);
 472     }
 473     static List<String> runExec(List<String> cmdsList, Map<String, String> penv) {
 474         ArrayList<String> alist = new ArrayList<String>();
 475         ProcessBuilder pb =
 476                 new ProcessBuilder(cmdsList);
 477         Map<String, String> env = pb.environment();
 478         if (penv != null && !penv.isEmpty()) {
 479             env.putAll(penv);
 480         }
 481         pb.directory(new File("."));
 482         dirlist(new File("."));
 483         for (String x : cmdsList) {
 484             System.out.print(x + " ");
 485         }
 486         System.out.println("");
 487         int retval = 0;
 488         Process p = null;
 489         InputStreamReader ir = null;
 490         BufferedReader rd = null;
 491         InputStream is = null;
 492         try {
 493             pb.redirectErrorStream(true);
 494             p = pb.start();
 495             is = p.getInputStream();
 496             ir = new InputStreamReader(is);
 497             rd = new BufferedReader(ir, 8192);
 498 
 499             String in = rd.readLine();
 500             while (in != null) {
 501                 alist.add(in);
 502                 System.out.println(in);
 503                 in = rd.readLine();
 504             }
 505             retval = p.waitFor();
 506             if (retval != 0) {
 507                 throw new RuntimeException("process failed with non-zero exit");
 508             }
 509         } catch (Exception ex) {
 510             throw new RuntimeException(ex.getMessage());
 511         } finally {
 512             close(rd);
 513             close(ir);
 514             close(is);
 515             if (p != null) {
 516                 p.destroy();
 517             }
 518         }
 519         return alist;
 520     }
 521 
 522     static String getUnpack200Cmd() {
 523         return getAjavaCmd("unpack200");
 524     }
 525 
 526     static String getPack200Cmd() {
 527         return getAjavaCmd("pack200");
 528     }
 529 
 530     static String getJavaCmd() {
 531         return getAjavaCmd("java");
 532     }
 533 
 534     static String getJavacCmd() {
 535         return getAjavaCmd("javac");
 536     }
 537 
 538     static String getJarCmd() {
 539         return getAjavaCmd("jar");
 540     }
 541 
 542     static String getJimageCmd() {
 543         return getAjavaCmd("jimage");
 544     }
 545 
 546     static String getAjavaCmd(String cmdStr) {
 547         File binDir = new File(JavaHome, "bin");
 548         File unpack200File = IsWindows
 549                 ? new File(binDir, cmdStr + ".exe")
 550                 : new File(binDir, cmdStr);
 551 
 552         String cmd = unpack200File.getAbsolutePath();
 553         if (!unpack200File.canExecute()) {
 554             throw new RuntimeException("please check" +
 555                     cmd + " exists and is executable");
 556         }
 557         return cmd;
 558     }
 559 
 560     static File createRtJar() throws IOException {
 561         File LibDir = new File(JavaHome, "lib");
 562         File ModuleDir = new File(LibDir, "modules");
 563         File BootModules = new File(ModuleDir, "bootmodules.jimage");
 564         List<String> cmdList = new ArrayList<>();
 565         cmdList.add(getJimageCmd());
 566         cmdList.add("extract");
 567         cmdList.add(BootModules.getAbsolutePath());
 568         cmdList.add("--dir");
 569         cmdList.add("out");
 570         runExec(cmdList);
 571 
 572         File rtJar = new File("rt.jar");
 573         cmdList.clear();
 574         cmdList.add(getJarCmd());
 575         // cmdList.add("cvf"); too noisy
 576         cmdList.add("cf");
 577         cmdList.add(rtJar.getName());
 578         cmdList.add("-C");
 579         cmdList.add("out");
 580         cmdList.add(".");
 581         runExec(cmdList);
 582 
 583         recursiveDelete(new File("out"));
 584         return rtJar;
 585     }
 586 }