author | lancea |
Thu, 10 Jan 2019 14:32:56 -0500 | |
changeset 53243 | 8bea4144b21c |
permissions | -rw-r--r-- |
53243
8bea4144b21c
8216362: Better error message handling when there is an invalid Manifest
lancea
parents:
diff
changeset
|
1 |
/* |
8bea4144b21c
8216362: Better error message handling when there is an invalid Manifest
lancea
parents:
diff
changeset
|
2 |
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. |
8bea4144b21c
8216362: Better error message handling when there is an invalid Manifest
lancea
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
8bea4144b21c
8216362: Better error message handling when there is an invalid Manifest
lancea
parents:
diff
changeset
|
4 |
* |
8bea4144b21c
8216362: Better error message handling when there is an invalid Manifest
lancea
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
8bea4144b21c
8216362: Better error message handling when there is an invalid Manifest
lancea
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
8bea4144b21c
8216362: Better error message handling when there is an invalid Manifest
lancea
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. |
8bea4144b21c
8216362: Better error message handling when there is an invalid Manifest
lancea
parents:
diff
changeset
|
8 |
* |
8bea4144b21c
8216362: Better error message handling when there is an invalid Manifest
lancea
parents:
diff
changeset
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
8bea4144b21c
8216362: Better error message handling when there is an invalid Manifest
lancea
parents:
diff
changeset
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
8bea4144b21c
8216362: Better error message handling when there is an invalid Manifest
lancea
parents:
diff
changeset
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
8bea4144b21c
8216362: Better error message handling when there is an invalid Manifest
lancea
parents:
diff
changeset
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
8bea4144b21c
8216362: Better error message handling when there is an invalid Manifest
lancea
parents:
diff
changeset
|
13 |
* accompanied this code). |
8bea4144b21c
8216362: Better error message handling when there is an invalid Manifest
lancea
parents:
diff
changeset
|
14 |
* |
8bea4144b21c
8216362: Better error message handling when there is an invalid Manifest
lancea
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License version |
8bea4144b21c
8216362: Better error message handling when there is an invalid Manifest
lancea
parents:
diff
changeset
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
8bea4144b21c
8216362: Better error message handling when there is an invalid Manifest
lancea
parents:
diff
changeset
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
8bea4144b21c
8216362: Better error message handling when there is an invalid Manifest
lancea
parents:
diff
changeset
|
18 |
* |
8bea4144b21c
8216362: Better error message handling when there is an invalid Manifest
lancea
parents:
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
8bea4144b21c
8216362: Better error message handling when there is an invalid Manifest
lancea
parents:
diff
changeset
|
20 |
* or visit www.oracle.com if you need additional information or have any |
8bea4144b21c
8216362: Better error message handling when there is an invalid Manifest
lancea
parents:
diff
changeset
|
21 |
* questions. |
8bea4144b21c
8216362: Better error message handling when there is an invalid Manifest
lancea
parents:
diff
changeset
|
22 |
*/ |
8bea4144b21c
8216362: Better error message handling when there is an invalid Manifest
lancea
parents:
diff
changeset
|
23 |
|
8bea4144b21c
8216362: Better error message handling when there is an invalid Manifest
lancea
parents:
diff
changeset
|
24 |
import java.io.File; |
8bea4144b21c
8216362: Better error message handling when there is an invalid Manifest
lancea
parents:
diff
changeset
|
25 |
import java.io.IOException; |
8bea4144b21c
8216362: Better error message handling when there is an invalid Manifest
lancea
parents:
diff
changeset
|
26 |
import java.io.OutputStream; |
8bea4144b21c
8216362: Better error message handling when there is an invalid Manifest
lancea
parents:
diff
changeset
|
27 |
import java.nio.file.Files; |
8bea4144b21c
8216362: Better error message handling when there is an invalid Manifest
lancea
parents:
diff
changeset
|
28 |
import java.nio.file.Paths; |
8bea4144b21c
8216362: Better error message handling when there is an invalid Manifest
lancea
parents:
diff
changeset
|
29 |
import java.util.concurrent.Callable; |
8bea4144b21c
8216362: Better error message handling when there is an invalid Manifest
lancea
parents:
diff
changeset
|
30 |
import java.util.jar.JarEntry; |
8bea4144b21c
8216362: Better error message handling when there is an invalid Manifest
lancea
parents:
diff
changeset
|
31 |
import java.util.jar.JarFile; |
8bea4144b21c
8216362: Better error message handling when there is an invalid Manifest
lancea
parents:
diff
changeset
|
32 |
import java.util.jar.JarOutputStream; |
8bea4144b21c
8216362: Better error message handling when there is an invalid Manifest
lancea
parents:
diff
changeset
|
33 |
|
8bea4144b21c
8216362: Better error message handling when there is an invalid Manifest
lancea
parents:
diff
changeset
|
34 |
import static java.nio.charset.StandardCharsets.UTF_8; |
8bea4144b21c
8216362: Better error message handling when there is an invalid Manifest
lancea
parents:
diff
changeset
|
35 |
|
8bea4144b21c
8216362: Better error message handling when there is an invalid Manifest
lancea
parents:
diff
changeset
|
36 |
/** |
8bea4144b21c
8216362: Better error message handling when there is an invalid Manifest
lancea
parents:
diff
changeset
|
37 |
* @test |
8bea4144b21c
8216362: Better error message handling when there is an invalid Manifest
lancea
parents:
diff
changeset
|
38 |
* @bug 8216362 |
8bea4144b21c
8216362: Better error message handling when there is an invalid Manifest
lancea
parents:
diff
changeset
|
39 |
* @run main/othervm -Djdk.includeInExceptions=jar IncludeInExceptionsTest yes |
8bea4144b21c
8216362: Better error message handling when there is an invalid Manifest
lancea
parents:
diff
changeset
|
40 |
* @run main/othervm IncludeInExceptionsTest |
8bea4144b21c
8216362: Better error message handling when there is an invalid Manifest
lancea
parents:
diff
changeset
|
41 |
* @summary Verify that the property jdk.net.includeInExceptions works as expected |
8bea4144b21c
8216362: Better error message handling when there is an invalid Manifest
lancea
parents:
diff
changeset
|
42 |
* when an error occurs while reading an invalid Manifest file. |
8bea4144b21c
8216362: Better error message handling when there is an invalid Manifest
lancea
parents:
diff
changeset
|
43 |
*/ |
8bea4144b21c
8216362: Better error message handling when there is an invalid Manifest
lancea
parents:
diff
changeset
|
44 |
/* |
8bea4144b21c
8216362: Better error message handling when there is an invalid Manifest
lancea
parents:
diff
changeset
|
45 |
* @see Manifest#Manifest(JarVerifier,InputStream,String) |
8bea4144b21c
8216362: Better error message handling when there is an invalid Manifest
lancea
parents:
diff
changeset
|
46 |
* @see Manifest#getErrorPosition |
8bea4144b21c
8216362: Better error message handling when there is an invalid Manifest
lancea
parents:
diff
changeset
|
47 |
*/ |
8bea4144b21c
8216362: Better error message handling when there is an invalid Manifest
lancea
parents:
diff
changeset
|
48 |
public class IncludeInExceptionsTest { |
8bea4144b21c
8216362: Better error message handling when there is an invalid Manifest
lancea
parents:
diff
changeset
|
49 |
|
8bea4144b21c
8216362: Better error message handling when there is an invalid Manifest
lancea
parents:
diff
changeset
|
50 |
static final String FILENAME = "Unique-Filename-Expected-In_Msg.jar"; |
8bea4144b21c
8216362: Better error message handling when there is an invalid Manifest
lancea
parents:
diff
changeset
|
51 |
|
8bea4144b21c
8216362: Better error message handling when there is an invalid Manifest
lancea
parents:
diff
changeset
|
52 |
static final byte[] INVALID_MANIFEST = ( |
8bea4144b21c
8216362: Better error message handling when there is an invalid Manifest
lancea
parents:
diff
changeset
|
53 |
"Manifest-Version: 1.0\r\n" + |
8bea4144b21c
8216362: Better error message handling when there is an invalid Manifest
lancea
parents:
diff
changeset
|
54 |
"\r\n" + |
8bea4144b21c
8216362: Better error message handling when there is an invalid Manifest
lancea
parents:
diff
changeset
|
55 |
"Illegal\r\n" + |
8bea4144b21c
8216362: Better error message handling when there is an invalid Manifest
lancea
parents:
diff
changeset
|
56 |
"\r\n").getBytes(UTF_8); |
8bea4144b21c
8216362: Better error message handling when there is an invalid Manifest
lancea
parents:
diff
changeset
|
57 |
|
8bea4144b21c
8216362: Better error message handling when there is an invalid Manifest
lancea
parents:
diff
changeset
|
58 |
static String createJarInvalidManifest(String jar) throws IOException { |
8bea4144b21c
8216362: Better error message handling when there is an invalid Manifest
lancea
parents:
diff
changeset
|
59 |
try (OutputStream out = Files.newOutputStream(Paths.get(jar)); |
8bea4144b21c
8216362: Better error message handling when there is an invalid Manifest
lancea
parents:
diff
changeset
|
60 |
JarOutputStream jos = new JarOutputStream(out)) { |
8bea4144b21c
8216362: Better error message handling when there is an invalid Manifest
lancea
parents:
diff
changeset
|
61 |
JarEntry je = new JarEntry(JarFile.MANIFEST_NAME); |
8bea4144b21c
8216362: Better error message handling when there is an invalid Manifest
lancea
parents:
diff
changeset
|
62 |
jos.putNextEntry(je); |
8bea4144b21c
8216362: Better error message handling when there is an invalid Manifest
lancea
parents:
diff
changeset
|
63 |
jos.write(INVALID_MANIFEST); |
8bea4144b21c
8216362: Better error message handling when there is an invalid Manifest
lancea
parents:
diff
changeset
|
64 |
jos.closeEntry(); |
8bea4144b21c
8216362: Better error message handling when there is an invalid Manifest
lancea
parents:
diff
changeset
|
65 |
} |
8bea4144b21c
8216362: Better error message handling when there is an invalid Manifest
lancea
parents:
diff
changeset
|
66 |
return jar; |
8bea4144b21c
8216362: Better error message handling when there is an invalid Manifest
lancea
parents:
diff
changeset
|
67 |
} |
8bea4144b21c
8216362: Better error message handling when there is an invalid Manifest
lancea
parents:
diff
changeset
|
68 |
|
8bea4144b21c
8216362: Better error message handling when there is an invalid Manifest
lancea
parents:
diff
changeset
|
69 |
static void test(Callable<?> attempt, boolean includeInExceptions) throws Exception { |
8bea4144b21c
8216362: Better error message handling when there is an invalid Manifest
lancea
parents:
diff
changeset
|
70 |
try { |
8bea4144b21c
8216362: Better error message handling when there is an invalid Manifest
lancea
parents:
diff
changeset
|
71 |
attempt.call(); |
8bea4144b21c
8216362: Better error message handling when there is an invalid Manifest
lancea
parents:
diff
changeset
|
72 |
throw new AssertionError("Excpected Exception not thrown"); |
8bea4144b21c
8216362: Better error message handling when there is an invalid Manifest
lancea
parents:
diff
changeset
|
73 |
} catch (IOException e) { |
8bea4144b21c
8216362: Better error message handling when there is an invalid Manifest
lancea
parents:
diff
changeset
|
74 |
boolean foundFileName = e.getMessage().contains(FILENAME); |
8bea4144b21c
8216362: Better error message handling when there is an invalid Manifest
lancea
parents:
diff
changeset
|
75 |
if(includeInExceptions && !foundFileName) { |
8bea4144b21c
8216362: Better error message handling when there is an invalid Manifest
lancea
parents:
diff
changeset
|
76 |
throw new AssertionError("JAR file name expected but not found in error message"); |
8bea4144b21c
8216362: Better error message handling when there is an invalid Manifest
lancea
parents:
diff
changeset
|
77 |
} else if (foundFileName && !includeInExceptions) { |
8bea4144b21c
8216362: Better error message handling when there is an invalid Manifest
lancea
parents:
diff
changeset
|
78 |
throw new AssertionError("JAR file name found but should not be in error message"); |
8bea4144b21c
8216362: Better error message handling when there is an invalid Manifest
lancea
parents:
diff
changeset
|
79 |
} |
8bea4144b21c
8216362: Better error message handling when there is an invalid Manifest
lancea
parents:
diff
changeset
|
80 |
} |
8bea4144b21c
8216362: Better error message handling when there is an invalid Manifest
lancea
parents:
diff
changeset
|
81 |
} |
8bea4144b21c
8216362: Better error message handling when there is an invalid Manifest
lancea
parents:
diff
changeset
|
82 |
|
8bea4144b21c
8216362: Better error message handling when there is an invalid Manifest
lancea
parents:
diff
changeset
|
83 |
public static void main(String[] args) throws Exception { |
8bea4144b21c
8216362: Better error message handling when there is an invalid Manifest
lancea
parents:
diff
changeset
|
84 |
|
8bea4144b21c
8216362: Better error message handling when there is an invalid Manifest
lancea
parents:
diff
changeset
|
85 |
boolean includeInExceptions; |
8bea4144b21c
8216362: Better error message handling when there is an invalid Manifest
lancea
parents:
diff
changeset
|
86 |
if(args.length > 0) { |
8bea4144b21c
8216362: Better error message handling when there is an invalid Manifest
lancea
parents:
diff
changeset
|
87 |
includeInExceptions = true; |
8bea4144b21c
8216362: Better error message handling when there is an invalid Manifest
lancea
parents:
diff
changeset
|
88 |
System.out.println("**** Running test WITH -Djdk.includeInExceptions=jar"); |
8bea4144b21c
8216362: Better error message handling when there is an invalid Manifest
lancea
parents:
diff
changeset
|
89 |
} else { |
8bea4144b21c
8216362: Better error message handling when there is an invalid Manifest
lancea
parents:
diff
changeset
|
90 |
includeInExceptions = false; |
8bea4144b21c
8216362: Better error message handling when there is an invalid Manifest
lancea
parents:
diff
changeset
|
91 |
System.out.println("**** Running test WITHOUT -Djdk.includeInExceptions=jar"); |
8bea4144b21c
8216362: Better error message handling when there is an invalid Manifest
lancea
parents:
diff
changeset
|
92 |
} |
8bea4144b21c
8216362: Better error message handling when there is an invalid Manifest
lancea
parents:
diff
changeset
|
93 |
|
8bea4144b21c
8216362: Better error message handling when there is an invalid Manifest
lancea
parents:
diff
changeset
|
94 |
test(() -> new JarFile(createJarInvalidManifest(FILENAME)).getManifest(), |
8bea4144b21c
8216362: Better error message handling when there is an invalid Manifest
lancea
parents:
diff
changeset
|
95 |
includeInExceptions); |
8bea4144b21c
8216362: Better error message handling when there is an invalid Manifest
lancea
parents:
diff
changeset
|
96 |
test(() -> new JarFile(createJarInvalidManifest("Verifying-" + FILENAME), |
8bea4144b21c
8216362: Better error message handling when there is an invalid Manifest
lancea
parents:
diff
changeset
|
97 |
true).getManifest(), includeInExceptions); |
8bea4144b21c
8216362: Better error message handling when there is an invalid Manifest
lancea
parents:
diff
changeset
|
98 |
} |
8bea4144b21c
8216362: Better error message handling when there is an invalid Manifest
lancea
parents:
diff
changeset
|
99 |
|
8bea4144b21c
8216362: Better error message handling when there is an invalid Manifest
lancea
parents:
diff
changeset
|
100 |
} |
8bea4144b21c
8216362: Better error message handling when there is an invalid Manifest
lancea
parents:
diff
changeset
|
101 |