1 /*
   2  * Copyright (c) 2007, 2016, 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         if (foundFiles == null) {
 328             return;
 329         }
 330         list.addAll(Arrays.asList(foundFiles));
 331         File[] dirs = startDir.listFiles(DIR_FILTER);
 332         for (File dir : dirs) {
 333             findFiles0(dir, list, filter);
 334         }
 335     }
 336 
 337     static void close(Closeable c) {
 338         if (c == null) {
 339             return;
 340         }
 341         try {
 342             c.close();
 343         } catch (IOException ignore) {
 344         }
 345     }
 346 
 347     static void compiler(String... javacCmds) {
 348         List<String> cmdList = new ArrayList<>();
 349         cmdList.add(getJavacCmd());
 350         for (String x : javacCmds) {
 351             cmdList.add(x);
 352         }
 353         runExec(cmdList);
 354     }
 355 
 356     static void jar(String... jargs) {
 357         List<String> cmdList = new ArrayList<>();
 358         cmdList.add(getJarCmd());
 359         for (String x : jargs) {
 360             cmdList.add(x);
 361         }
 362         runExec(cmdList);
 363     }
 364 
 365     static void testWithRepack(File inFile, String... repackOpts) throws IOException {
 366         File cwd = new File(".");
 367         // pack using --repack in native mode
 368         File nativejarFile = new File(cwd, "out-n" + Utils.JAR_FILE_EXT);
 369         repack(inFile, nativejarFile, false, repackOpts);
 370         doCompareVerify(inFile, nativejarFile);
 371 
 372         // ensure bit compatibility between the unpacker variants
 373         File javajarFile = new File(cwd, "out-j" + Utils.JAR_FILE_EXT);
 374         repack(inFile, javajarFile, true, repackOpts);
 375         doCompareBitWise(javajarFile, nativejarFile);
 376     }
 377 
 378     static List<String> repack(File inFile, File outFile,
 379             boolean disableNative, String... extraOpts) {
 380         List<String> cmdList = new ArrayList<>();
 381         cmdList.clear();
 382         cmdList.add(Utils.getJavaCmd());
 383         cmdList.add("-ea");
 384         cmdList.add("-esa");
 385         if (disableNative) {
 386             cmdList.add("-Dcom.sun.java.util.jar.pack.disable.native=true");
 387         }
 388         cmdList.add("com.sun.java.util.jar.pack.Driver");
 389         cmdList.add("--repack");
 390         if (extraOpts != null) {
 391            for (String opt: extraOpts) {
 392                cmdList.add(opt);
 393            }
 394         }
 395         cmdList.add(outFile.getName());
 396         cmdList.add(inFile.getName());
 397         return Utils.runExec(cmdList);
 398     }
 399 
 400     // given a jar file foo.jar will write to foo.pack
 401     static void pack(JarFile jarFile, File packFile) throws IOException {
 402         Pack200.Packer packer = Pack200.newPacker();
 403         Map<String, String> p = packer.properties();
 404         // Take the time optimization vs. space
 405         p.put(packer.EFFORT, "1");  // CAUTION: do not use 0.
 406         // Make the memory consumption as effective as possible
 407         p.put(packer.SEGMENT_LIMIT, "10000");
 408         // ignore all JAR deflation requests to save time
 409         p.put(packer.DEFLATE_HINT, packer.FALSE);
 410         // save the file ordering of the original JAR
 411         p.put(packer.KEEP_FILE_ORDER, packer.TRUE);
 412         FileOutputStream fos = null;
 413         try {
 414             // Write out to a jtreg scratch area
 415             fos = new FileOutputStream(packFile);
 416             // Call the packer
 417             packer.pack(jarFile, fos);
 418         } finally {
 419             close(fos);
 420         }
 421     }
 422 
 423     // uses java unpacker, slow but useful to discover issues with the packer
 424     static void unpackj(File inFile, JarOutputStream jarStream)
 425             throws IOException {
 426         unpack0(inFile, jarStream, true);
 427 
 428     }
 429 
 430     // uses native unpacker using the java APIs
 431     static void unpackn(File inFile, JarOutputStream jarStream)
 432             throws IOException {
 433         unpack0(inFile, jarStream, false);
 434     }
 435 
 436     // given a packed file, create the jar file in the current directory.
 437     private static void unpack0(File inFile, JarOutputStream jarStream,
 438             boolean useJavaUnpack) throws IOException {
 439         // Unpack the files
 440         Pack200.Unpacker unpacker = Pack200.newUnpacker();
 441         Map<String, String> props = unpacker.properties();
 442         if (useJavaUnpack) {
 443             props.put("com.sun.java.util.jar.pack.disable.native", "true");
 444         }
 445         // Call the unpacker
 446         unpacker.unpack(inFile, jarStream);
 447     }
 448 
 449     static byte[] getBuffer(ZipFile zf, ZipEntry ze) throws IOException {
 450         ByteArrayOutputStream baos = new ByteArrayOutputStream();
 451         byte buf[] = new byte[8192];
 452         InputStream is = null;
 453         try {
 454             is = zf.getInputStream(ze);
 455             int n = is.read(buf);
 456             while (n > 0) {
 457                 baos.write(buf, 0, n);
 458                 n = is.read(buf);
 459             }
 460             return baos.toByteArray();
 461         } finally {
 462             close(is);
 463         }
 464     }
 465 
 466     static ArrayList<String> getZipFileEntryNames(ZipFile z) {
 467         ArrayList<String> out = new ArrayList<String>();
 468         for (ZipEntry ze : Collections.list(z.entries())) {
 469             out.add(ze.getName());
 470         }
 471         return out;
 472     }
 473     static List<String> runExec(List<String> cmdsList) {
 474         return runExec(cmdsList, null);
 475     }
 476     static List<String> runExec(List<String> cmdsList, Map<String, String> penv) {
 477         ArrayList<String> alist = new ArrayList<String>();
 478         ProcessBuilder pb =
 479                 new ProcessBuilder(cmdsList);
 480         Map<String, String> env = pb.environment();
 481         if (penv != null && !penv.isEmpty()) {
 482             env.putAll(penv);
 483         }
 484         pb.directory(new File("."));
 485         dirlist(new File("."));
 486         for (String x : cmdsList) {
 487             System.out.print(x + " ");
 488         }
 489         System.out.println("");
 490         int retval = 0;
 491         Process p = null;
 492         InputStreamReader ir = null;
 493         BufferedReader rd = null;
 494         InputStream is = null;
 495         try {
 496             pb.redirectErrorStream(true);
 497             p = pb.start();
 498             is = p.getInputStream();
 499             ir = new InputStreamReader(is);
 500             rd = new BufferedReader(ir, 8192);
 501 
 502             String in = rd.readLine();
 503             while (in != null) {
 504                 alist.add(in);
 505                 System.out.println(in);
 506                 in = rd.readLine();
 507             }
 508             retval = p.waitFor();
 509             if (retval != 0) {
 510                 throw new RuntimeException("process failed with non-zero exit");
 511             }
 512         } catch (Exception ex) {
 513             throw new RuntimeException(ex.getMessage());
 514         } finally {
 515             close(rd);
 516             close(ir);
 517             close(is);
 518             if (p != null) {
 519                 p.destroy();
 520             }
 521         }
 522         return alist;
 523     }
 524 
 525     static String getUnpack200Cmd() {
 526         return getAjavaCmd("unpack200");
 527     }
 528 
 529     static String getPack200Cmd() {
 530         return getAjavaCmd("pack200");
 531     }
 532 
 533     static String getJavaCmd() {
 534         return getAjavaCmd("java");
 535     }
 536 
 537     static String getJavacCmd() {
 538         return getAjavaCmd("javac");
 539     }
 540 
 541     static String getJarCmd() {
 542         return getAjavaCmd("jar");
 543     }
 544 
 545     static String getJimageCmd() {
 546         return getAjavaCmd("jimage");
 547     }
 548 
 549     static String getAjavaCmd(String cmdStr) {
 550         File binDir = new File(JavaHome, "bin");
 551         File unpack200File = IsWindows
 552                 ? new File(binDir, cmdStr + ".exe")
 553                 : new File(binDir, cmdStr);
 554 
 555         String cmd = unpack200File.getAbsolutePath();
 556         if (!unpack200File.canExecute()) {
 557             throw new RuntimeException("please check" +
 558                     cmd + " exists and is executable");
 559         }
 560         return cmd;
 561     }
 562 
 563     static File createRtJar() throws IOException {
 564         File LibDir = new File(JavaHome, "lib");
 565         File ModuleDir = new File(LibDir, "modules");
 566         File BootModules = new File(ModuleDir, "bootmodules.jimage");
 567         List<String> cmdList = new ArrayList<>();
 568         cmdList.add(getJimageCmd());
 569         cmdList.add("extract");
 570         cmdList.add(BootModules.getAbsolutePath());
 571         cmdList.add("--dir");
 572         cmdList.add("out");
 573         runExec(cmdList);
 574 
 575         File rtJar = new File("rt.jar");
 576         cmdList.clear();
 577         cmdList.add(getJarCmd());
 578         // cmdList.add("cvf"); too noisy
 579         cmdList.add("cf");
 580         cmdList.add(rtJar.getName());
 581         cmdList.add("-C");
 582         cmdList.add("out");
 583         cmdList.add(".");
 584         runExec(cmdList);
 585 
 586         recursiveDelete(new File("out"));
 587         return rtJar;
 588     }
 589 }