author | sherman |
Thu, 17 Jun 2010 13:21:46 -0700 | |
changeset 5805 | 87c7dd70572a |
parent 5506 | 202f599c92aa |
child 8553 | 46c2babb1e44 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
5805
87c7dd70572a
6962067: TEST_BUG: Tests in java/util/zip/ZipFile leave file open
sherman
parents:
5506
diff
changeset
|
2 |
* Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 22 |
*/ |
23 |
||
24 |
/* @test |
|
25 |
* @bug 4770745 6218846 6218848 6237956 |
|
26 |
* @summary test for correct detection and reporting of corrupted zip files |
|
27 |
* @author Martin Buchholz |
|
28 |
*/ |
|
29 |
||
30 |
import java.util.*; |
|
31 |
import java.util.zip.*; |
|
32 |
import java.io.*; |
|
33 |
import static java.lang.System.*; |
|
34 |
import static java.util.zip.ZipFile.*; |
|
35 |
||
36 |
public class CorruptedZipFiles { |
|
37 |
static int passed = 0, failed = 0; |
|
38 |
||
39 |
static void fail(String msg) { |
|
40 |
failed++; |
|
41 |
err.println(msg); |
|
42 |
} |
|
43 |
||
44 |
static void unexpected(Throwable t) { |
|
45 |
failed++; |
|
46 |
t.printStackTrace(); |
|
47 |
} |
|
48 |
||
49 |
public static void main(String[] args) throws Exception { |
|
5805
87c7dd70572a
6962067: TEST_BUG: Tests in java/util/zip/ZipFile leave file open
sherman
parents:
5506
diff
changeset
|
50 |
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream("x.zip")); |
87c7dd70572a
6962067: TEST_BUG: Tests in java/util/zip/ZipFile leave file open
sherman
parents:
5506
diff
changeset
|
51 |
try { |
87c7dd70572a
6962067: TEST_BUG: Tests in java/util/zip/ZipFile leave file open
sherman
parents:
5506
diff
changeset
|
52 |
ZipEntry e = new ZipEntry("x"); |
87c7dd70572a
6962067: TEST_BUG: Tests in java/util/zip/ZipFile leave file open
sherman
parents:
5506
diff
changeset
|
53 |
zos.putNextEntry(e); |
87c7dd70572a
6962067: TEST_BUG: Tests in java/util/zip/ZipFile leave file open
sherman
parents:
5506
diff
changeset
|
54 |
zos.write((int)'x'); |
87c7dd70572a
6962067: TEST_BUG: Tests in java/util/zip/ZipFile leave file open
sherman
parents:
5506
diff
changeset
|
55 |
} finally { |
87c7dd70572a
6962067: TEST_BUG: Tests in java/util/zip/ZipFile leave file open
sherman
parents:
5506
diff
changeset
|
56 |
zos.close(); |
87c7dd70572a
6962067: TEST_BUG: Tests in java/util/zip/ZipFile leave file open
sherman
parents:
5506
diff
changeset
|
57 |
} |
2 | 58 |
|
59 |
int len = (int)(new File("x.zip").length()); |
|
60 |
byte[] good = new byte[len]; |
|
61 |
FileInputStream fis = new FileInputStream("x.zip"); |
|
62 |
fis.read(good); |
|
63 |
fis.close(); |
|
64 |
fis = null; |
|
65 |
new File("x.zip").delete(); |
|
66 |
||
67 |
int endpos = len - ENDHDR; |
|
68 |
int cenpos = u16(good, endpos+ENDOFF); |
|
69 |
int locpos = u16(good, cenpos+CENOFF); |
|
70 |
if (u32(good, endpos) != ENDSIG) fail("Where's ENDSIG?"); |
|
71 |
if (u32(good, cenpos) != CENSIG) fail("Where's CENSIG?"); |
|
72 |
if (u32(good, locpos) != LOCSIG) fail("Where's LOCSIG?"); |
|
73 |
if (u16(good, locpos+LOCNAM) != u16(good,cenpos+CENNAM)) |
|
74 |
fail("Name field length mismatch"); |
|
75 |
if (u16(good, locpos+LOCEXT) != u16(good,cenpos+CENEXT)) |
|
76 |
fail("Extra field length mismatch"); |
|
77 |
||
78 |
byte[] bad; |
|
79 |
||
80 |
err.println("corrupted ENDSIZ"); |
|
81 |
bad = good.clone(); |
|
82 |
bad[endpos+ENDSIZ]=(byte)0xff; |
|
83 |
checkZipException(bad, ".*bad central directory size.*"); |
|
84 |
||
85 |
err.println("corrupted ENDOFF"); |
|
86 |
bad = good.clone(); |
|
87 |
bad[endpos+ENDOFF]=(byte)0xff; |
|
88 |
checkZipException(bad, ".*bad central directory offset.*"); |
|
89 |
||
90 |
err.println("corrupted CENSIG"); |
|
91 |
bad = good.clone(); |
|
92 |
bad[cenpos]++; |
|
93 |
checkZipException(bad, ".*bad signature.*"); |
|
94 |
||
95 |
err.println("corrupted CENFLG"); |
|
96 |
bad = good.clone(); |
|
97 |
bad[cenpos+CENFLG] |= 1; |
|
98 |
checkZipException(bad, ".*encrypted entry.*"); |
|
99 |
||
100 |
err.println("corrupted CENNAM 1"); |
|
101 |
bad = good.clone(); |
|
102 |
bad[cenpos+CENNAM]++; |
|
103 |
checkZipException(bad, ".*bad header size.*"); |
|
104 |
||
105 |
err.println("corrupted CENNAM 2"); |
|
106 |
bad = good.clone(); |
|
107 |
bad[cenpos+CENNAM]--; |
|
108 |
checkZipException(bad, ".*bad header size.*"); |
|
109 |
||
110 |
err.println("corrupted CENNAM 3"); |
|
111 |
bad = good.clone(); |
|
112 |
bad[cenpos+CENNAM] = (byte)0xfd; |
|
113 |
bad[cenpos+CENNAM+1] = (byte)0xfd; |
|
114 |
checkZipException(bad, ".*bad header size.*"); |
|
115 |
||
116 |
err.println("corrupted CENEXT 1"); |
|
117 |
bad = good.clone(); |
|
118 |
bad[cenpos+CENEXT]++; |
|
119 |
checkZipException(bad, ".*bad header size.*"); |
|
120 |
||
121 |
err.println("corrupted CENEXT 2"); |
|
122 |
bad = good.clone(); |
|
123 |
bad[cenpos+CENEXT] = (byte)0xfd; |
|
124 |
bad[cenpos+CENEXT+1] = (byte)0xfd; |
|
125 |
checkZipException(bad, ".*bad header size.*"); |
|
126 |
||
127 |
err.println("corrupted CENCOM"); |
|
128 |
bad = good.clone(); |
|
129 |
bad[cenpos+CENCOM]++; |
|
130 |
checkZipException(bad, ".*bad header size.*"); |
|
131 |
||
132 |
err.println("corrupted CENHOW"); |
|
133 |
bad = good.clone(); |
|
134 |
bad[cenpos+CENHOW] = 2; |
|
135 |
checkZipException(bad, ".*bad compression method.*"); |
|
136 |
||
137 |
err.println("corrupted LOCSIG"); |
|
138 |
bad = good.clone(); |
|
139 |
bad[locpos]++; |
|
140 |
checkZipExceptionInGetInputStream(bad, ".*bad signature.*"); |
|
141 |
||
142 |
out.printf("passed = %d, failed = %d%n", passed, failed); |
|
143 |
if (failed > 0) throw new Exception("Some tests failed"); |
|
144 |
} |
|
145 |
||
146 |
static int uniquifier = 432; |
|
147 |
||
148 |
static void checkZipExceptionImpl(byte[] data, |
|
149 |
String msgPattern, |
|
150 |
boolean getInputStream) { |
|
151 |
String zipName = "bad" + (uniquifier++) + ".zip"; |
|
152 |
try { |
|
153 |
FileOutputStream fos = new FileOutputStream(zipName); |
|
154 |
fos.write(data); |
|
155 |
fos.close(); |
|
156 |
ZipFile zf = new ZipFile(zipName); |
|
5805
87c7dd70572a
6962067: TEST_BUG: Tests in java/util/zip/ZipFile leave file open
sherman
parents:
5506
diff
changeset
|
157 |
try { |
87c7dd70572a
6962067: TEST_BUG: Tests in java/util/zip/ZipFile leave file open
sherman
parents:
5506
diff
changeset
|
158 |
if (getInputStream) { |
87c7dd70572a
6962067: TEST_BUG: Tests in java/util/zip/ZipFile leave file open
sherman
parents:
5506
diff
changeset
|
159 |
InputStream is = zf.getInputStream(new ZipEntry("x")); |
87c7dd70572a
6962067: TEST_BUG: Tests in java/util/zip/ZipFile leave file open
sherman
parents:
5506
diff
changeset
|
160 |
is.read(); |
87c7dd70572a
6962067: TEST_BUG: Tests in java/util/zip/ZipFile leave file open
sherman
parents:
5506
diff
changeset
|
161 |
} |
87c7dd70572a
6962067: TEST_BUG: Tests in java/util/zip/ZipFile leave file open
sherman
parents:
5506
diff
changeset
|
162 |
} finally { |
87c7dd70572a
6962067: TEST_BUG: Tests in java/util/zip/ZipFile leave file open
sherman
parents:
5506
diff
changeset
|
163 |
zf.close(); |
2 | 164 |
} |
165 |
fail("Failed to throw expected ZipException"); |
|
166 |
} catch (ZipException e) { |
|
167 |
if (e.getMessage().matches(msgPattern)) |
|
168 |
passed++; |
|
169 |
else |
|
170 |
unexpected(e); |
|
171 |
} catch (Throwable t) { |
|
172 |
unexpected(t); |
|
173 |
} |
|
174 |
finally { |
|
175 |
new File(zipName).delete(); |
|
176 |
} |
|
177 |
} |
|
178 |
||
179 |
static void checkZipException(byte[] data, String msgPattern) { |
|
180 |
checkZipExceptionImpl(data, msgPattern, false); |
|
181 |
} |
|
182 |
||
183 |
static void checkZipExceptionInGetInputStream(byte[] data, String msgPattern) { |
|
184 |
checkZipExceptionImpl(data, msgPattern, true); |
|
185 |
} |
|
186 |
||
187 |
static int u8(byte[] data, int offset) { |
|
188 |
return data[offset]&0xff; |
|
189 |
} |
|
190 |
||
191 |
static int u16(byte[] data, int offset) { |
|
192 |
return u8(data,offset) + (u8(data,offset+1)<<8); |
|
193 |
} |
|
194 |
||
195 |
static int u32(byte[] data, int offset) { |
|
196 |
return u16(data,offset) + (u16(data,offset+2)<<16); |
|
197 |
} |
|
198 |
||
199 |
// The following can be deleted once this bug is fixed: |
|
200 |
// 6225935: "import static" accessibility rules for symbols different for no reason |
|
201 |
static final long LOCSIG = ZipFile.LOCSIG; |
|
202 |
static final long EXTSIG = ZipFile.EXTSIG; |
|
203 |
static final long CENSIG = ZipFile.CENSIG; |
|
204 |
static final long ENDSIG = ZipFile.ENDSIG; |
|
205 |
||
206 |
static final int LOCHDR = ZipFile.LOCHDR; |
|
207 |
static final int EXTHDR = ZipFile.EXTHDR; |
|
208 |
static final int CENHDR = ZipFile.CENHDR; |
|
209 |
static final int ENDHDR = ZipFile.ENDHDR; |
|
210 |
||
211 |
static final int LOCVER = ZipFile.LOCVER; |
|
212 |
static final int LOCFLG = ZipFile.LOCFLG; |
|
213 |
static final int LOCHOW = ZipFile.LOCHOW; |
|
214 |
static final int LOCTIM = ZipFile.LOCTIM; |
|
215 |
static final int LOCCRC = ZipFile.LOCCRC; |
|
216 |
static final int LOCSIZ = ZipFile.LOCSIZ; |
|
217 |
static final int LOCLEN = ZipFile.LOCLEN; |
|
218 |
static final int LOCNAM = ZipFile.LOCNAM; |
|
219 |
static final int LOCEXT = ZipFile.LOCEXT; |
|
220 |
||
221 |
static final int CENVEM = ZipFile.CENVEM; |
|
222 |
static final int CENVER = ZipFile.CENVER; |
|
223 |
static final int CENFLG = ZipFile.CENFLG; |
|
224 |
static final int CENHOW = ZipFile.CENHOW; |
|
225 |
static final int CENTIM = ZipFile.CENTIM; |
|
226 |
static final int CENCRC = ZipFile.CENCRC; |
|
227 |
static final int CENSIZ = ZipFile.CENSIZ; |
|
228 |
static final int CENLEN = ZipFile.CENLEN; |
|
229 |
static final int CENNAM = ZipFile.CENNAM; |
|
230 |
static final int CENEXT = ZipFile.CENEXT; |
|
231 |
static final int CENCOM = ZipFile.CENCOM; |
|
232 |
static final int CENDSK = ZipFile.CENDSK; |
|
233 |
static final int CENATT = ZipFile.CENATT; |
|
234 |
static final int CENATX = ZipFile.CENATX; |
|
235 |
static final int CENOFF = ZipFile.CENOFF; |
|
236 |
||
237 |
static final int ENDSUB = ZipFile.ENDSUB; |
|
238 |
static final int ENDTOT = ZipFile.ENDTOT; |
|
239 |
static final int ENDSIZ = ZipFile.ENDSIZ; |
|
240 |
static final int ENDOFF = ZipFile.ENDOFF; |
|
241 |
static final int ENDCOM = ZipFile.ENDCOM; |
|
242 |
} |