1 /*
   2  * Copyright (c) 2014, 2015, 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 import java.io.*;
  24 import java.util.ArrayList;
  25 import java.util.Collections;
  26 import java.util.Enumeration;
  27 import java.util.List;
  28 import java.util.jar.JarEntry;
  29 import java.util.jar.JarFile;
  30 import java.util.jar.JarInputStream;
  31 import java.util.jar.JarOutputStream;
  32 import java.util.zip.ZipEntry;
  33 /*
  34  * @test
  35  * @bug 8029646
  36  * @summary tests that native unpacker produces the same result as Java one
  37  * @compile -XDignore.symbol.file Utils.java PackTestZip64.java
  38  * @run main PackTestZip64
  39  * @author kizune
  40  * @key intermittent
  41  */
  42 
  43 public class PackTestZip64 {
  44     public static void main(String... args) throws Exception {
  45         testPacking();
  46         Utils.cleanup();
  47     }
  48 
  49     // 1KB buffer is enough to copy jar content
  50     private static final byte[] BUFFER = new byte[1024];
  51 
  52     static void testPacking() throws IOException {
  53         // make a copy of the test specimen to local directory
  54         File testFile = new File("tools_java.jar");
  55         // Add a large number of small files to the golden jar
  56         generateLargeJar(testFile, Utils.getGoldenJar());
  57 
  58         List<String> cmdsList = new ArrayList<>();
  59 
  60         // Repack file to get the Java-based result
  61         cmdsList.add(Utils.getPack200Cmd());
  62         cmdsList.add("--repack");
  63         cmdsList.add(testFile.getName());
  64         Utils.runExec(cmdsList);
  65         cmdsList.clear();
  66 
  67         // Pack file with pack200 and unpack in with unpack200
  68         File packedFile = new File("tools.pack.gz");
  69         cmdsList.add(Utils.getPack200Cmd());
  70         cmdsList.add(packedFile.getName());
  71         cmdsList.add(testFile.getName());
  72         Utils.runExec(cmdsList);
  73         cmdsList.clear();
  74 
  75         File unpackedFile = new File("tools_native.jar");
  76         cmdsList.add(Utils.getUnpack200Cmd());
  77         cmdsList.add(packedFile.getName());
  78         cmdsList.add(unpackedFile.getName());
  79         Utils.runExec(cmdsList);
  80 
  81         // Compare files binary
  82         compareTwoFiles(testFile, unpackedFile);
  83 
  84         // Cleaning up generated files
  85         testFile.delete();
  86         packedFile.delete();
  87         unpackedFile.delete();
  88     }
  89 
  90     static void compareTwoFiles(File src, File dst) throws IOException {
  91         if (!src.exists()) {
  92             throw new IOException("File " + src.getName() + " does not exist!");
  93         }
  94 
  95         if(!dst.exists()) {
  96             throw new IOException("File " + dst.getName() + " does not exist!");
  97         }
  98 
  99         BufferedInputStream srcis, dstis;
 100         srcis = new BufferedInputStream(new FileInputStream(src));
 101         dstis = new BufferedInputStream(new FileInputStream(dst));
 102 
 103         int s = 0, d, pos = 0;
 104         while (s != -1) { // Checking of just one result for EOF is enough
 105             s = srcis.read();
 106             d = dstis.read();
 107 
 108             if (s != d) {
 109                 throw new IOException("Files are differ starting at position: "
 110                 + Integer.toHexString(pos));
 111             }
 112 
 113             pos++;
 114         }
 115 
 116         srcis.close();
 117         dstis.close();
 118     }
 119 
 120     static void generateLargeJar(File result, File source) throws IOException {
 121         if (result.exists()) {
 122             result.delete();
 123         }
 124 
 125         try (JarOutputStream copyTo = new JarOutputStream(new FileOutputStream(result));
 126              JarFile srcJar = new JarFile(source)) {
 127 
 128             for (JarEntry je : Collections.list(srcJar.entries())) {
 129                 copyTo.putNextEntry(je);
 130                 if (!je.isDirectory()) {
 131                     copyStream(srcJar.getInputStream(je), copyTo);
 132                 }
 133                 copyTo.closeEntry();
 134             }
 135 
 136             int many = Short.MAX_VALUE * 2 + 2;
 137 
 138             for (int i = 0 ; i < many ; i++) {
 139                 JarEntry e = new JarEntry("F-" + i + ".txt");
 140                 copyTo.putNextEntry(e);
 141             }
 142             copyTo.flush();
 143             copyTo.close();
 144         }
 145     }
 146 
 147     static void copyStream(InputStream in, OutputStream out) throws IOException {
 148         int bytesRead;
 149         while ((bytesRead = in.read(BUFFER))!= -1) {
 150             out.write(BUFFER, 0, bytesRead);
 151         }
 152     }
 153 }