1 /*
2 * Copyright (c) 2014, 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 */
28 * @library /lib/testlibrary
29 * @modules java.base/sun.security.tools.keytool
30 * jdk.jartool/sun.security.tools.jarsigner
31 * jdk.jartool/sun.tools.jar
32 * @build jdk.testlibrary.IOUtils
33 * @run main EntriesOrder
34 */
35
36 import java.io.FileInputStream;
37 import java.io.FileOutputStream;
38 import java.nio.file.Files;
39 import java.nio.file.Paths;
40 import java.security.cert.Certificate;
41 import java.util.*;
42 import java.util.jar.JarEntry;
43 import java.util.jar.JarFile;
44 import java.util.jar.JarInputStream;
45 import java.util.zip.ZipEntry;
46 import java.util.zip.ZipOutputStream;
47
48 import jdk.testlibrary.IOUtils;
49
50 public class EntriesOrder {
51
52 public static void main(String[] args) throws Exception {
53
54 String[] entries = {
55 "META-INF/",
56 "META-INF/MANIFEST.MF",
57 "META-INF/A.RSA",
58 "META-INF/A.SF",
59 "META-INF/inf",
60 "a"};
61
62 Map<String,byte[]> content = new HashMap<>();
63
64 // We will create a jar containing entries above. Try all permutations
65 // and confirm 1) When opened as a JarFile, we can always get 3 signed
66 // ones (MANIFEST, inf, a), and 2) When opened as a JarInputStream,
67 // when the order is correct (MANIFEST at beginning, followed by RSA/SF,
68 // directory ignored), we can get 2 signed ones (inf, a).
69
97
98 // Test
99 for (List<String> perm: Permute(entries)) {
100
101 // Recreate a jar
102 try (ZipOutputStream zos
103 = new ZipOutputStream(new FileOutputStream("x.jar"))) {
104 for (String e: perm) {
105 zos.putNextEntry(new ZipEntry(e));
106 if (Paths.get(e).toFile().isDirectory()) continue;
107 zos.write(content.get(e));
108 }
109 }
110
111 // Open with JarFile, number of signed entries should be 3.
112 int cc = 0;
113 try (JarFile jf = new JarFile("x.jar")) {
114 Enumeration<JarEntry> jes = jf.entries();
115 while (jes.hasMoreElements()) {
116 JarEntry je = jes.nextElement();
117 IOUtils.readFully(jf.getInputStream(je));
118 Certificate[] certs = je.getCertificates();
119 if (certs != null && certs.length > 0) {
120 cc++;
121 }
122 }
123 }
124
125 if (cc != 3) {
126 System.out.println(perm + " - jf - " + cc);
127 throw new Exception();
128 }
129
130 // Open with JarInputStream
131 int signed;
132
133 perm.remove("META-INF/");
134 if (perm.get(0).equals("META-INF/MANIFEST.MF") &&
135 perm.get(1).contains("/A.") &&
136 perm.get(2).contains("/A.")) {
137 signed = 2; // Good order
138 } else {
139 signed = 0; // Bad order. In this case, the number of signed
140 // entries is not documented. Just test impl.
141 }
142
143 cc = 0;
144 try (JarInputStream jis
145 = new JarInputStream(new FileInputStream("x.jar"))) {
146 while (true) {
147 JarEntry je = jis.getNextJarEntry();
148 if (je == null) break;
149 IOUtils.readFully(jis);
150 Certificate[] certs = je.getCertificates();
151 if (certs != null && certs.length > 0) {
152 cc++;
153 }
154 }
155 }
156
157 if (cc != signed) {
158 System.out.println(perm + " - jis - " + cc + " " + signed);
159 throw new Exception();
160 }
161 }
162 }
163
164 // Helper method to return all permutations of an array. Each output can
165 // be altered without damaging the iteration process.
166 static Iterable<List<String>> Permute(String[] entries) {
167 return new Iterable<List<String>>() {
168
169 int s = entries.length;
|
1 /*
2 * Copyright (c) 2014, 2017, 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 */
28 * @library /lib/testlibrary
29 * @modules java.base/sun.security.tools.keytool
30 * jdk.jartool/sun.security.tools.jarsigner
31 * jdk.jartool/sun.tools.jar
32 * @build jdk.testlibrary.IOUtils
33 * @run main EntriesOrder
34 */
35
36 import java.io.FileInputStream;
37 import java.io.FileOutputStream;
38 import java.nio.file.Files;
39 import java.nio.file.Paths;
40 import java.security.cert.Certificate;
41 import java.util.*;
42 import java.util.jar.JarEntry;
43 import java.util.jar.JarFile;
44 import java.util.jar.JarInputStream;
45 import java.util.zip.ZipEntry;
46 import java.util.zip.ZipOutputStream;
47
48 public class EntriesOrder {
49
50 public static void main(String[] args) throws Exception {
51
52 String[] entries = {
53 "META-INF/",
54 "META-INF/MANIFEST.MF",
55 "META-INF/A.RSA",
56 "META-INF/A.SF",
57 "META-INF/inf",
58 "a"};
59
60 Map<String,byte[]> content = new HashMap<>();
61
62 // We will create a jar containing entries above. Try all permutations
63 // and confirm 1) When opened as a JarFile, we can always get 3 signed
64 // ones (MANIFEST, inf, a), and 2) When opened as a JarInputStream,
65 // when the order is correct (MANIFEST at beginning, followed by RSA/SF,
66 // directory ignored), we can get 2 signed ones (inf, a).
67
95
96 // Test
97 for (List<String> perm: Permute(entries)) {
98
99 // Recreate a jar
100 try (ZipOutputStream zos
101 = new ZipOutputStream(new FileOutputStream("x.jar"))) {
102 for (String e: perm) {
103 zos.putNextEntry(new ZipEntry(e));
104 if (Paths.get(e).toFile().isDirectory()) continue;
105 zos.write(content.get(e));
106 }
107 }
108
109 // Open with JarFile, number of signed entries should be 3.
110 int cc = 0;
111 try (JarFile jf = new JarFile("x.jar")) {
112 Enumeration<JarEntry> jes = jf.entries();
113 while (jes.hasMoreElements()) {
114 JarEntry je = jes.nextElement();
115 jf.getInputStream(je).readAllBytes();
116 Certificate[] certs = je.getCertificates();
117 if (certs != null && certs.length > 0) {
118 cc++;
119 }
120 }
121 }
122
123 if (cc != 3) {
124 System.out.println(perm + " - jf - " + cc);
125 throw new Exception();
126 }
127
128 // Open with JarInputStream
129 int signed;
130
131 perm.remove("META-INF/");
132 if (perm.get(0).equals("META-INF/MANIFEST.MF") &&
133 perm.get(1).contains("/A.") &&
134 perm.get(2).contains("/A.")) {
135 signed = 2; // Good order
136 } else {
137 signed = 0; // Bad order. In this case, the number of signed
138 // entries is not documented. Just test impl.
139 }
140
141 cc = 0;
142 try (JarInputStream jis
143 = new JarInputStream(new FileInputStream("x.jar"))) {
144 while (true) {
145 JarEntry je = jis.getNextJarEntry();
146 if (je == null) break;
147 jis.readAllBytes();
148 Certificate[] certs = je.getCertificates();
149 if (certs != null && certs.length > 0) {
150 cc++;
151 }
152 }
153 }
154
155 if (cc != signed) {
156 System.out.println(perm + " - jis - " + cc + " " + signed);
157 throw new Exception();
158 }
159 }
160 }
161
162 // Helper method to return all permutations of an array. Each output can
163 // be altered without damaging the iteration process.
164 static Iterable<List<String>> Permute(String[] entries) {
165 return new Iterable<List<String>>() {
166
167 int s = entries.length;
|