1 /* 2 * Copyright (c) 2010, 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 import java.io.File; 24 import java.nio.charset.Charset; 25 import java.nio.file.Files; 26 import java.util.ArrayList; 27 import java.util.Arrays; 28 import java.util.List; 29 import static java.nio.file.StandardOpenOption.*; 30 /* 31 * @test 32 * @bug 6746111 8005252 33 * @summary tests various classfile format and attribute handling by pack200 34 * @compile -XDignore.symbol.file Utils.java AttributeTests.java 35 * @run main AttributeTests 36 * @author ksrini 37 */ 38 public class AttributeTests { 39 40 public static void main(String... args) throws Exception { 41 test6746111(); 42 testMethodParameters(); 43 } 44 45 /* 46 * this tests ensure that MethodParameters produces by javac is packed 47 * correctly. Usually this is not the case as new attributes are available 48 * in the sdk jars, since MethodParameters happens to be an optional 49 * attribute, thus this test. 50 */ 51 static void testMethodParameters() throws Exception { 52 List<String> scratch = new ArrayList<>(); 53 final String fname = "MP"; 54 String javaFileName = fname + Utils.JAVA_FILE_EXT; 55 String javaClassName = fname + Utils.CLASS_FILE_EXT; 56 scratch.add("class " + fname + " {"); 57 scratch.add("void foo2(int j, final int k){}"); 58 scratch.add("}"); 59 File cwd = new File("."); 60 File javaFile = new File(cwd, javaFileName); 61 Files.write(javaFile.toPath(), scratch, Charset.defaultCharset(), 62 CREATE, TRUNCATE_EXISTING); 63 64 Utils.compiler(javaFile.getName(), "-parameters"); 65 66 // jar the file up 67 File testjarFile = new File(cwd, "test" + Utils.JAR_FILE_EXT); 68 Utils.jar("cvf", testjarFile.getName(), javaClassName); 69 70 // pack using --repack 71 File outjarFile = new File(cwd, "out" + Utils.JAR_FILE_EXT); 72 scratch.clear(); 73 scratch.add(Utils.getPack200Cmd()); 74 scratch.add("--repack"); 75 scratch.add("--unknown-attribute=error"); 76 scratch.add(outjarFile.getName()); 77 scratch.add(testjarFile.getName()); 78 Utils.runExec(scratch); 79 80 Utils.doCompareVerify(testjarFile, outjarFile); 81 } 82 /* 83 * this test checks to see if we get the expected strings for output 84 */ 85 static void test6746111() throws Exception { 86 String pack200Cmd = Utils.getPack200Cmd(); 87 File badAttrJar = new File(".", "badattr.jar"); 88 Utils.copyFile(new File(Utils.TEST_SRC_DIR, "badattr.jar"), badAttrJar); 89 File testJar = new File(".", "test.jar"); 90 List<String> cmds = new ArrayList<String>(); 91 cmds.add(pack200Cmd); 92 cmds.add("--repack"); 93 cmds.add("-v"); 94 cmds.add(testJar.getAbsolutePath()); 95 cmds.add(badAttrJar.getAbsolutePath()); 96 List<String> output = Utils.runExec(cmds); 97 /* 98 * compare the repacked jar bit-wise, as all the files 99 * should be transmitted "as-is". 100 */ 101 Utils.doCompareBitWise(badAttrJar.getAbsoluteFile(), testJar.getAbsoluteFile()); 102 String[] expectedStrings = { 103 "WARNING: Passing class file uncompressed due to unrecognized" + 104 " attribute: Foo.class", 105 "INFO: com.sun.java.util.jar.pack.Attribute$FormatException: " + 106 "class attribute \"XourceFile\": is unknown attribute " + 107 "in class Foo", 108 "INFO: com.sun.java.util.jar.pack.ClassReader$ClassFormatException: " + 109 "AnnotationDefault: attribute length cannot be zero, in Test.message()", 110 "WARNING: Passing class file uncompressed due to unknown class format: Test.class" 111 }; 112 List<String> notfoundList = new ArrayList<String>(); 113 notfoundList.addAll(Arrays.asList(expectedStrings)); 114 // make sure the expected messages are emitted 115 for (String x : output) { 116 findString(x, notfoundList, expectedStrings); 117 } 118 if (!notfoundList.isEmpty()) { 119 System.out.println("Not found:"); 120 for (String x : notfoundList) { 121 System.out.println(x); 122 } 123 throw new Exception("Test fails: " + notfoundList.size() + 124 " expected strings not found"); 125 } 126 testJar.delete(); 127 badAttrJar.delete(); 128 } 129 130 private static void findString(String outputStr, List<String> notfoundList, 131 String[] expectedStrings) { 132 for (String y : expectedStrings) { 133 if (outputStr.contains(y)) { 134 notfoundList.remove(y); 135 return; 136 } 137 } 138 } 139 }