1 /* TestOfManifest.java |
|
2 Copyright (C) 2006 Free Software Foundation, Inc. |
|
3 This file is part of Mauve. |
|
4 |
|
5 Mauve is free software; you can redistribute it and/or modify |
|
6 it under the terms of the GNU General Public License as published by |
|
7 the Free Software Foundation; either version 2, or (at your option) |
|
8 any later version. |
|
9 |
|
10 Mauve is distributed in the hope that it will be useful, but |
|
11 WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
13 General Public License for more details. |
|
14 |
|
15 You should have received a copy of the GNU General Public License |
|
16 along with Mauve; see the file COPYING. If not, write to the |
|
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
18 02110-1301 USA. |
|
19 |
|
20 */ |
|
21 |
|
22 // Tags: JDK1.4 |
|
23 |
|
24 package gnu.testlet.java.util.jar.JarFile; |
|
25 |
|
26 import gnu.testlet.TestHarness; |
|
27 import gnu.testlet.Testlet; |
|
28 |
|
29 import java.io.File; |
|
30 import java.io.IOException; |
|
31 import java.io.InputStream; |
|
32 import java.security.cert.Certificate; |
|
33 import java.util.Enumeration; |
|
34 import java.util.jar.JarEntry; |
|
35 import java.util.jar.JarFile; |
|
36 |
|
37 /** |
|
38 * Simple test for validating Marco Trudel's patch for parsing long file names |
|
39 * in a Jar file's manifest. |
|
40 */ |
|
41 public class TestOfManifest |
|
42 implements Testlet |
|
43 { |
|
44 private static final String FILENAME = "jfaceSmall.jar"; |
|
45 private static final String FILEPATH = "gnu#testlet#java#util#jar#JarFile#" |
|
46 + FILENAME; |
|
47 private static final String ENTRYNAME = |
|
48 "org/eclipse/jface/viewers/TreeViewer$TreeColorAndFontCollector.class"; |
|
49 |
|
50 /* (non-Javadoc) |
|
51 * @see gnu.testlet.Testlet#test(gnu.testlet.TestHarness) |
|
52 */ |
|
53 public void test(TestHarness harness) |
|
54 { |
|
55 checkManifestEntries(harness); |
|
56 checkCertificates(harness); |
|
57 } |
|
58 |
|
59 private void checkManifestEntries(TestHarness harness) |
|
60 { |
|
61 harness.checkPoint("checkManifestEntries"); |
|
62 try |
|
63 { |
|
64 File file = harness.getResourceFile(FILEPATH); |
|
65 JarFile jarFile = new JarFile(file); |
|
66 readEntries(jarFile); // will parse the signatures |
|
67 boolean ok = readCertificates(harness, jarFile); |
|
68 harness.check(ok, "Jar entry MUST be signed"); |
|
69 } |
|
70 catch (Exception x) |
|
71 { |
|
72 harness.debug(x); |
|
73 harness.fail("checkManifestEntries: " + x); |
|
74 } |
|
75 } |
|
76 |
|
77 /** |
|
78 * @param harness this test-harness. |
|
79 */ |
|
80 private void checkCertificates(TestHarness harness) |
|
81 { |
|
82 harness.checkPoint("checkCertificates"); |
|
83 try |
|
84 { |
|
85 File file = harness.getResourceFile(FILEPATH); |
|
86 JarFile jarFile = new JarFile(file, true); |
|
87 JarEntry je = jarFile.getJarEntry(ENTRYNAME); |
|
88 |
|
89 Certificate[] certsBefore = je.getCertificates(); |
|
90 int certsBeforeCount = certsBefore == null ? 0 : certsBefore.length; |
|
91 harness.verbose("*** before: " + certsBeforeCount); |
|
92 harness.check(certsBeforeCount == 0, "Certificate count MUST be 0"); |
|
93 |
|
94 read1Entry(jarFile, je); |
|
95 |
|
96 Certificate[] certsAfter = je.getCertificates(); |
|
97 int certsAfterCount = certsAfter == null ? 0 : certsAfter.length; |
|
98 harness.verbose("*** after: " + certsAfterCount); |
|
99 harness.check(certsAfterCount == 1, "Certificate count MUST be 1"); |
|
100 harness.check(certsBeforeCount != certsAfterCount, |
|
101 "Certificate counts MUST NOT be the same"); |
|
102 |
|
103 JarEntry je_ = jarFile.getJarEntry(ENTRYNAME); |
|
104 Certificate[] sameCerts = je_.getCertificates(); |
|
105 int sameCertsCount = sameCerts == null ? 0 : sameCerts.length; |
|
106 harness.verbose("*** w/ new entry: " + sameCertsCount); |
|
107 harness.check(sameCertsCount == 1, |
|
108 "Certificate count (w/ new entry) MUST be 1"); |
|
109 harness.check(certsAfterCount == sameCertsCount, |
|
110 "Certificate counts (w/ new entry) MUST be the same"); |
|
111 } |
|
112 catch (Exception x) |
|
113 { |
|
114 harness.debug(x); |
|
115 harness.fail("checkCertificates: " + x); |
|
116 } |
|
117 } |
|
118 |
|
119 private static void readEntries(JarFile jarFile) throws Exception |
|
120 { |
|
121 for (Enumeration entries = jarFile.entries(); entries.hasMoreElements();) |
|
122 read1Entry(jarFile, (JarEntry) entries.nextElement()); |
|
123 } |
|
124 |
|
125 private static void read1Entry(JarFile jar, JarEntry entry) throws Exception |
|
126 { |
|
127 InputStream stream = null; |
|
128 try |
|
129 { |
|
130 stream = jar.getInputStream(entry); |
|
131 byte[] ba = new byte[8192]; |
|
132 int n; |
|
133 while ((n = stream.read(ba)) >= 0) |
|
134 /* keep reading */; |
|
135 } |
|
136 finally |
|
137 { |
|
138 if (stream != null) |
|
139 try |
|
140 { |
|
141 stream.close(); |
|
142 } |
|
143 catch (IOException ignored) |
|
144 { |
|
145 } |
|
146 } |
|
147 } |
|
148 |
|
149 private boolean readCertificates(TestHarness harness, JarFile jarFile) |
|
150 { |
|
151 for (Enumeration entries = jarFile.entries(); entries.hasMoreElements();) |
|
152 { |
|
153 JarEntry entry = (JarEntry) entries.nextElement(); |
|
154 if (entry.isDirectory()) |
|
155 continue; |
|
156 Certificate[] certs = entry.getCertificates(); |
|
157 if (certs == null || certs.length == 0) // No certificate |
|
158 { |
|
159 if (! entry.getName().startsWith("META-INF")) |
|
160 { |
|
161 harness.verbose("Entry " + entry.getName() + " in jar file " |
|
162 + FILENAME + " does not have a certificate"); |
|
163 return false; |
|
164 } |
|
165 } |
|
166 } |
|
167 return true; |
|
168 } |
|
169 } |
|