2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 package org.apache.harmony.archive.tests.java.util.jar;
20 import dalvik.annotation.KnownFailure;
21 import dalvik.annotation.TestLevel;
22 import dalvik.annotation.TestTargetClass;
23 import dalvik.annotation.TestTargetNew;
24 import tests.support.resource.Support_Resources;
27 import java.io.IOException;
28 import java.io.InputStream;
30 import java.util.Arrays;
31 import java.util.HashSet;
33 import java.util.jar.JarEntry;
34 import java.util.jar.JarInputStream;
35 import java.util.jar.Manifest;
36 import java.util.zip.ZipEntry;
37 import java.util.zip.ZipException;
39 @TestTargetClass(JarInputStream.class)
40 public class JarInputStreamTest extends junit.framework.TestCase {
41 // a 'normal' jar file
42 private String jarName;
44 // same as patch.jar but without a manifest file
45 private String jarName2;
47 private final String entryName = "foo/bar/A.class";
49 private static final int indexofDSA = 2;
51 private static final int indexofTESTCLASS = 4;
53 private static final int totalEntries = 4;
56 protected void setUp() {
57 jarName = Support_Resources.getURL("morestuff/hyts_patch.jar");
58 jarName2 = Support_Resources.getURL("morestuff/hyts_patch2.jar");
62 * @tests java.util.jar.JarInputStream#JarInputStream(java.io.InputStream)
65 level = TestLevel.COMPLETE,
67 method = "JarInputStream",
68 args = {java.io.InputStream.class}
70 public void test_ConstructorLjava_io_InputStream() {
71 // Test for method java.util.jar.JarInputStream(java.io.InputStream)
72 InputStream is = null;
73 JarInputStream jis = null;
75 is = new URL(jarName).openConnection().getInputStream();
76 boolean hasCorrectEntry = false;
77 jis = new JarInputStream(is);
78 assertNotNull("The jar input stream should have a manifest", jis
80 JarEntry je = jis.getNextJarEntry();
82 if (je.getName().equals(entryName)) {
83 hasCorrectEntry = true;
85 je = jis.getNextJarEntry();
88 "The jar input stream does not contain the correct entries",
90 } catch (Exception e) {
91 fail("Exception during test: " + e.toString());
96 jis = new JarInputStream(is);
97 fail("IOException expected");
98 } catch (IOException ee) {
104 * @tests java.util.jar.JarInputStream#getManifest()
107 level = TestLevel.COMPLETE,
109 method = "getManifest",
112 public void test_getManifest() {
113 // Test for method java.util.jar.Manifest
114 // java.util.jar.JarInputStream.getManifest()
118 InputStream is = new URL(jarName2).openConnection()
120 JarInputStream jis = new JarInputStream(is);
121 m = jis.getManifest();
122 assertNull("The jar input stream should not have a manifest", m);
124 is = new URL(jarName).openConnection().getInputStream();
125 jis = new JarInputStream(is);
126 m = jis.getManifest();
127 assertNotNull("The jar input stream should have a manifest", m);
128 } catch (Exception e) {
129 fail("Exception during test: " + e.toString());
135 * @tests java.util.jar.JarInputStream#getNextJarEntry()
138 level = TestLevel.PARTIAL_COMPLETE,
139 method = "getNextJarEntry",
142 public void test_getNextJarEntry() throws Exception {
143 final Set<String> desired = new HashSet<String>(Arrays
144 .asList(new String[] {
145 "foo/", "foo/bar/", "foo/bar/A.class", "Blah.txt"}));
146 Set<String> actual = new HashSet<String>();
147 InputStream is = new URL(jarName).openConnection().getInputStream();
148 JarInputStream jis = new JarInputStream(is);
149 JarEntry je = jis.getNextJarEntry();
151 actual.add(je.toString());
152 je = jis.getNextJarEntry();
154 assertEquals(actual, desired);
158 jis.getNextJarEntry();
159 fail("IOException expected");
160 } catch (IOException ee) {
164 File resources = Support_Resources.createTempFolder();
165 Support_Resources.copyFile(resources, null, "Broken_entry.jar");
166 is = Support_Resources.getStream("Broken_entry.jar");
167 jis = new JarInputStream(is, false);
168 jis.getNextJarEntry();
170 jis.getNextJarEntry();
171 fail("ZipException expected");
172 } catch (ZipException ee) {
178 level = TestLevel.PARTIAL_COMPLETE,
179 method = "getNextJarEntry",
182 public void test_getNextJarEntry_Ex() throws Exception {
183 final Set<String> desired = new HashSet<String>(Arrays
184 .asList("foo/", "foo/bar/", "foo/bar/A.class", "Blah.txt"));
185 Set<String> actual = new HashSet<String>();
186 InputStream is = new URL(jarName).openConnection().getInputStream();
187 JarInputStream jis = new JarInputStream(is);
188 JarEntry je = jis.getNextJarEntry();
190 actual.add(je.toString());
191 je = jis.getNextJarEntry();
193 assertEquals(actual, desired);
197 jis.getNextJarEntry();
198 fail("IOException expected");
199 } catch (IOException ee) {
203 File resources = Support_Resources.createTempFolder();
204 Support_Resources.copyFile(resources, null, "Broken_entry.jar");
205 is = Support_Resources.getStream("Broken_entry.jar");
206 jis = new JarInputStream(is, false);
207 jis.getNextJarEntry();
209 jis.getNextJarEntry();
210 fail("ZipException expected");
211 } catch (ZipException ee) {
217 level = TestLevel.PARTIAL_COMPLETE,
218 notes = "Exceptions checking missed. Case2",
219 method = "getNextJarEntry",
222 public void test_JarInputStream_Integrate_Jar_getNextEntry()
224 String intJarName = Support_Resources.getURL("Integrate.jar");
225 InputStream is = new URL(intJarName).openConnection().getInputStream();
226 JarInputStream jin = new JarInputStream(is, true);
227 ZipEntry entry = null;
229 while (count == 0 || entry != null) {
231 entry = jin.getNextEntry();
233 assertEquals(totalEntries + 1, count);
238 level = TestLevel.PARTIAL_COMPLETE,
239 notes = "IOException & ZipException checking missed.",
240 method = "getNextEntry",
243 public void test_JarInputStream_Modified_Class_getNextEntry()
245 String modJarName = Support_Resources.getURL("Modified_Class.jar");
246 InputStream is = new URL(modJarName).openConnection().getInputStream();
247 JarInputStream jin = new JarInputStream(is, true);
248 ZipEntry zipEntry = null;
251 while (count == 0 || zipEntry != null) {
254 zipEntry = jin.getNextEntry();
255 if (count == indexofTESTCLASS + 1) {
256 fail("Should throw Security Exception");
258 } catch (SecurityException e) {
259 if (count != indexofTESTCLASS + 1) {
265 assertEquals(totalEntries + 2, count);
270 level = TestLevel.PARTIAL_COMPLETE,
271 notes = "IOException & ZipException checking missed.",
272 method = "getNextEntry",
275 public void test_JarInputStream_Modified_Manifest_MainAttributes_getNextEntry()
277 String modJarName = Support_Resources.getURL("Modified_Manifest_MainAttributes.jar");
278 InputStream is = new URL(modJarName).openConnection()
280 JarInputStream jin = new JarInputStream(is, true);
282 assertEquals("META-INF/TESTROOT.SF", jin.getNextEntry().getName());
283 assertEquals("META-INF/TESTROOT.DSA", jin.getNextEntry().getName());
287 } catch (SecurityException expected) {
289 assertEquals("META-INF/", jin.getNextEntry().getName());
290 assertEquals("Test.class", jin.getNextEntry().getName());
291 assertNull(jin.getNextEntry());
296 level = TestLevel.PARTIAL_COMPLETE,
297 notes = "IOException & ZipException checking missed.",
298 method = "getNextEntry",
301 public void test_JarInputStream_Modified_Manifest_EntryAttributes_getNextEntry()
303 String modJarName = Support_Resources
304 .getURL("Modified_Manifest_EntryAttributes.jar");
305 InputStream is = new URL(modJarName).openConnection().getInputStream();
306 JarInputStream jin = new JarInputStream(is, true);
307 ZipEntry zipEntry = null;
310 while (count == 0 || zipEntry != null) {
313 zipEntry = jin.getNextEntry();
314 if (count == indexofDSA + 1) {
315 fail("Should throw Security Exception");
317 } catch (SecurityException e) {
318 if (count != indexofDSA + 1) {
323 assertEquals(totalEntries + 2, count);
328 level = TestLevel.PARTIAL_COMPLETE,
329 notes = "IOException & ZipException checking missed.",
330 method = "getNextEntry",
333 public void test_JarInputStream_Modified_SF_EntryAttributes_getNextEntry()
335 String modJarName = Support_Resources
336 .getURL("Modified_SF_EntryAttributes.jar");
337 InputStream is = new URL(modJarName).openConnection().getInputStream();
338 JarInputStream jin = new JarInputStream(is, true);
339 ZipEntry zipEntry = null;
342 while (count == 0 || zipEntry != null) {
345 zipEntry = jin.getNextEntry();
346 if (count == indexofDSA + 1) {
347 fail("Should throw Security Exception");
349 } catch (SecurityException e) {
350 if (count != indexofDSA + 1) {
355 assertEquals(totalEntries + 2, count);
360 level = TestLevel.PARTIAL_COMPLETE,
361 notes = "IOException & ZipException checking missed.",
363 args = {byte[].class}
365 public void test_JarInputStream_Modified_Class_read() throws IOException {
366 String modJarName = Support_Resources.getURL("Modified_Class.jar");
367 InputStream is = new URL(modJarName).openConnection().getInputStream();
368 JarInputStream jin = new JarInputStream(is, true);
370 ZipEntry zipEntry = null;
371 while (count == 0 || zipEntry != null) {
373 zipEntry = jin.getNextEntry();
374 byte[] buffer = new byte[1024];
377 while (length >= 0) {
378 length = jin.read(buffer);
380 if (count == indexofTESTCLASS) {
381 fail("Should throw Security Exception");
383 } catch (SecurityException e) {
384 if (count < indexofTESTCLASS) {
389 assertEquals(totalEntries + 1, count);
394 level = TestLevel.PARTIAL,
395 notes = "Exception checking missed.",
397 args = {byte[].class}
399 public void test_Integrate_Jar_read() throws IOException {
400 String intJarName = Support_Resources.getURL("Integrate.jar");
401 InputStream is = new URL(intJarName).openConnection().getInputStream();
402 JarInputStream jin = new JarInputStream(is, true);
404 ZipEntry zipEntry = null;
405 while (count == 0 || zipEntry != null) {
407 zipEntry = jin.getNextEntry();
408 byte[] buffer = new byte[1024];
410 while (length >= 0) {
411 length = jin.read(buffer);
415 assertEquals(totalEntries + 1, count);
420 level = TestLevel.PARTIAL_COMPLETE,
421 notes = "IOException & ZipException checking missed.",
423 args = {byte[].class}
425 public void test_JarInputStream_Modified_Manifest_MainAttributes_read()
427 String modJarName = Support_Resources
428 .getURL("Modified_Manifest_MainAttributes.jar");
429 InputStream is = new URL(modJarName).openConnection().getInputStream();
430 JarInputStream jin = new JarInputStream(is, true);
432 ZipEntry zipEntry = null;
433 while (count == 0 || zipEntry != null) {
435 zipEntry = jin.getNextEntry();
436 byte[] buffer = new byte[1024];
439 while (length >= 0) {
440 length = jin.read(buffer);
442 if (count == indexofDSA) {
443 fail("Should throw Security Exception");
445 } catch (SecurityException e) {
446 if (count != indexofDSA) {
451 assertEquals(totalEntries + 1, count);
456 level = TestLevel.PARTIAL_COMPLETE,
457 notes = "IOException & ZipException checking missed.",
459 args = {byte[].class}
461 public void test_JarInputStream_Modified_SF_EntryAttributes_read()
463 String modJarName = Support_Resources
464 .getURL("Modified_SF_EntryAttributes.jar");
465 InputStream is = new URL(modJarName).openConnection().getInputStream();
466 JarInputStream jin = new JarInputStream(is, true);
468 ZipEntry zipEntry = null;
469 while (count == 0 || zipEntry != null) {
471 zipEntry = jin.getNextEntry();
472 byte[] buffer = new byte[1024];
475 while (length >= 0) {
476 length = jin.read(buffer);
478 if (count == indexofDSA) {
479 fail("Should throw Security Exception");
481 } catch (SecurityException e) {
482 if (count != indexofDSA) {
487 assertEquals(totalEntries + 1, count);
492 level = TestLevel.COMPLETE,
494 method = "JarInputStream",
495 args = {java.io.InputStream.class, boolean.class}
497 public void test_ConstructorLjava_io_InputStreamZ() {
498 // Test for method java.util.jar.JarInputStream(java.io.InputStream)
499 InputStream is = null;
500 JarInputStream jis = null;
502 is = new URL(jarName).openConnection().getInputStream();
503 boolean hasCorrectEntry = false;
504 jis = new JarInputStream(is, true);
505 assertNotNull("The jar input stream should have a manifest", jis
507 JarEntry je = jis.getNextJarEntry();
509 if (je.getName().equals(entryName)) {
510 hasCorrectEntry = true;
512 je = jis.getNextJarEntry();
515 "The jar input stream does not contain the correct entries",
517 } catch (Exception e) {
518 fail("Exception during test: " + e.toString());
523 jis = new JarInputStream(is, false);
524 fail("IOException expected");
525 } catch (IOException ee) {
531 level = TestLevel.COMPLETE,
535 public void test_closeAfterException() throws Exception {
536 File resources = Support_Resources.createTempFolder();
537 Support_Resources.copyFile(resources, null, "Broken_entry.jar");
538 InputStream is = Support_Resources.getStream("Broken_entry.jar");
539 JarInputStream jis = new JarInputStream(is, false);
543 fail("ZipException expected");
544 } catch (ZipException ee) {
550 fail("IOException expected");
551 } catch (IOException ee) {
557 level = TestLevel.COMPLETE,
558 method = "getNextEntry",
561 public void test_getNextEntry() throws Exception {
562 File resources = Support_Resources.createTempFolder();
563 Support_Resources.copyFile(resources, null, "Broken_entry.jar");
564 InputStream is = Support_Resources.getStream("Broken_entry.jar");
565 JarInputStream jis = new JarInputStream(is, false);
569 fail("ZipException expected");
570 } catch (ZipException ee) {
575 jis.close(); // Android throws exception here, already!
576 jis.getNextEntry(); // But RI here, only!
577 fail("IOException expected");
578 } catch (IOException ee) {
583 class Mock_JarInputStream extends JarInputStream {
585 public Mock_JarInputStream(InputStream in) throws IOException {
589 public ZipEntry createZipEntry(String str) {
590 return super.createZipEntry(str);
595 level = TestLevel.COMPLETE,
597 method = "createZipEntry",
598 args = {java.lang.String.class}
600 public void test_createZipEntryLjava_lang_String() throws Exception {
601 File resources = Support_Resources.createTempFolder();
602 Support_Resources.copyFile(resources, null, "Broken_entry.jar");
603 InputStream is = Support_Resources.getStream("Broken_entry.jar");
604 Mock_JarInputStream mjis = new Mock_JarInputStream(is);
605 assertNotNull(mjis.createZipEntry("New entry"));
609 level = TestLevel.COMPLETE,
611 args = {byte[].class, int.class, int.class}
613 public void test_read$ZII() throws Exception {
614 File resources = Support_Resources.createTempFolder();
615 Support_Resources.copyFile(resources, null, "Broken_entry_data.jar");
616 InputStream is = Support_Resources.getStream("Broken_entry_data.jar");
617 JarInputStream jis = new JarInputStream(is, true);
618 byte b[] = new byte[100];
628 fail("ZipException expected");
629 } catch (ZipException ee) {
634 jis.close(); // Android throws exception here, already!
635 jis.read(b, 0, 100); // But RI here, only!
636 fail("IOException expected");
637 } catch (IOException ee) {