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 /* 25 * @test 26 * @bug 6521334 6712743 8007902 8151901 27 * @requires (sun.arch.data.model == "64" & os.maxMemory >= 4g) 28 * @summary test general packer/unpacker functionality 29 * using native and java unpackers 30 * @compile -XDignore.symbol.file Utils.java Pack200Test.java 31 * @run main/othervm/timeout=1200 -Xmx1280m -Xshare:off Pack200Test 32 */ 33 34 import java.util.*; 35 import java.io.*; 36 import java.lang.management.ManagementFactory; 37 import java.lang.management.MemoryMXBean; 38 import java.util.jar.*; 39 40 /** 41 * Tests the packing/unpacking via the APIs. 42 */ 43 public class Pack200Test { 44 45 private static ArrayList <File> jarList = new ArrayList<File>(); 46 static final MemoryMXBean mmxbean = ManagementFactory.getMemoryMXBean(); 47 static final long m0 = getUsedMemory(); 48 static final int LEAK_TOLERANCE = 21000; // OS and GC related variations. 49 // enable leak checks only if required, GC charecteristics vary on 50 // platforms and this may not yield consistent results 51 static final boolean LEAK_CHECK = Boolean.getBoolean("Pack200Test.enableLeakCheck"); 52 53 /** Creates a new instance of Pack200Test */ 54 private Pack200Test() {} 55 56 static long getUsedMemory() { 57 mmxbean.gc(); 58 mmxbean.gc(); 59 mmxbean.gc(); 60 return mmxbean.getHeapMemoryUsage().getUsed()/1024; 61 } 62 63 private static void leakCheck() throws Exception { 64 if (!LEAK_CHECK) 65 return; 66 long diff = getUsedMemory() - m0; 67 System.out.println(" Info: memory diff = " + diff + "K"); 68 if (diff > LEAK_TOLERANCE) { 69 throw new Exception("memory leak detected " + diff); 70 } 71 } 72 73 private static void doPackUnpack() throws IOException { 74 for (File in : jarList) { 75 JarOutputStream javaUnpackerStream = null; 76 JarOutputStream nativeUnpackerStream = null; 77 JarFile jarFile = null; 78 try { 79 jarFile = new JarFile(in); 80 81 // Write out to a jtreg scratch area 82 File packFile = new File(in.getName() + Utils.PACK_FILE_EXT); 83 84 System.out.println("Packing [" + in.toString() + "]"); 85 // Call the packer 86 Utils.pack(jarFile, packFile); 87 System.out.println("Done Packing [" + in.toString() + "]"); 88 jarFile.close(); 89 System.out.println("Start leak check"); 90 leakCheck(); 91 92 System.out.println(" Unpacking using java unpacker"); 93 File javaUnpackedJar = new File("java-" + in.getName()); 94 // Write out to current directory, jtreg will setup a scratch area 95 javaUnpackerStream = new JarOutputStream( 96 new FileOutputStream(javaUnpackedJar)); 97 Utils.unpackj(packFile, javaUnpackerStream); 98 javaUnpackerStream.close(); 99 System.out.println(" Testing...java unpacker"); 100 leakCheck(); 101 // Ok we have unpacked the file, lets test it. 102 Utils.doCompareVerify(in.getAbsoluteFile(), javaUnpackedJar); 103 104 System.out.println(" Unpacking using native unpacker"); 105 // Write out to current directory 106 File nativeUnpackedJar = new File("native-" + in.getName()); 107 nativeUnpackerStream = new JarOutputStream( 108 new FileOutputStream(nativeUnpackedJar)); 109 Utils.unpackn(packFile, nativeUnpackerStream); 110 nativeUnpackerStream.close(); 111 System.out.println(" Testing...native unpacker"); 112 leakCheck(); 113 // the unpackers (native and java) should produce identical bits 114 // so we use use bit wise compare, the verification compare is 115 // very expensive wrt. time. 116 Utils.doCompareBitWise(javaUnpackedJar, nativeUnpackedJar); 117 System.out.println("Done."); 118 } catch (Exception e) { 119 throw new RuntimeException(e); 120 } finally { 121 Utils.close(nativeUnpackerStream); 122 Utils.close(javaUnpackerStream); 123 Utils.close((Closeable) jarFile); 124 } 125 } 126 Utils.cleanup(); // cleanup artifacts, if successful run 127 } 128 129 /** 130 * @param args the command line arguments 131 */ 132 public static void main(String[] args) throws Exception { 133 // select the jars carefully, adding more jars will increase the 134 // testing time, especially for jprt. 135 jarList.add(Utils.createRtJar()); 136 jarList.add(Utils.getGoldenJar()); 137 System.out.println(jarList); 138 doPackUnpack(); 139 } 140 }