OSDN Git Service

Fixing available() and close() for archive streams.
[android-x86/dalvik.git] / libcore / archive / src / test / java / org / apache / harmony / archive / tests / java / util / jar / JarInputStreamTest.java
1 /* 
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
8  * 
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  * 
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.
16  */
17
18 package org.apache.harmony.archive.tests.java.util.jar;
19
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;
25
26 import java.io.File;
27 import java.io.IOException;
28 import java.io.InputStream;
29 import java.net.URL;
30 import java.util.Arrays;
31 import java.util.HashSet;
32 import java.util.Set;
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;
38
39 @TestTargetClass(JarInputStream.class)
40 public class JarInputStreamTest extends junit.framework.TestCase {
41     // a 'normal' jar file
42     private String jarName;
43
44     // same as patch.jar but without a manifest file
45     private String jarName2;
46
47     private final String entryName = "foo/bar/A.class";
48
49     private static final int indexofDSA = 2;
50
51     private static final int indexofTESTCLASS = 4;
52
53     private static final int totalEntries = 4;
54
55     @Override
56     protected void setUp() {
57         jarName = Support_Resources.getURL("morestuff/hyts_patch.jar");
58         jarName2 = Support_Resources.getURL("morestuff/hyts_patch2.jar");
59     }
60
61     /**
62      * @tests java.util.jar.JarInputStream#JarInputStream(java.io.InputStream)
63      */
64     @TestTargetNew(
65         level = TestLevel.COMPLETE,
66         notes = "",
67         method = "JarInputStream",
68         args = {java.io.InputStream.class}
69     )
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;
74         try {
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
79                     .getManifest());
80             JarEntry je = jis.getNextJarEntry();
81             while (je != null) {
82                 if (je.getName().equals(entryName)) {
83                     hasCorrectEntry = true;
84                 }
85                 je = jis.getNextJarEntry();
86             }
87             assertTrue(
88                     "The jar input stream does not contain the correct entries",
89                     hasCorrectEntry);
90         } catch (Exception e) {
91             fail("Exception during test: " + e.toString());
92         }
93
94         try {
95             is.close();
96             jis = new JarInputStream(is);
97             fail("IOException expected");
98         } catch (IOException ee) {
99             // expected
100         }
101     }
102
103     /**
104      * @tests java.util.jar.JarInputStream#getManifest()
105      */
106     @TestTargetNew(
107         level = TestLevel.COMPLETE,
108         notes = "",
109         method = "getManifest",
110         args = {}
111     )
112     public void test_getManifest() {
113         // Test for method java.util.jar.Manifest
114         // java.util.jar.JarInputStream.getManifest()
115         try {
116             Manifest m;
117
118             InputStream is = new URL(jarName2).openConnection()
119                     .getInputStream();
120             JarInputStream jis = new JarInputStream(is);
121             m = jis.getManifest();
122             assertNull("The jar input stream should not have a manifest", m);
123
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());
130         }
131
132     }
133
134     /**
135      * @tests java.util.jar.JarInputStream#getNextJarEntry()
136      */
137     @TestTargetNew(
138         level = TestLevel.PARTIAL_COMPLETE,
139         method = "getNextJarEntry",
140         args = {}
141     )
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();
150         while (je != null) {
151             actual.add(je.toString());
152             je = jis.getNextJarEntry();
153         }
154         assertEquals(actual, desired);
155         jis.close();
156
157         try {
158             jis.getNextJarEntry();
159             fail("IOException expected");
160         } catch (IOException ee) {
161             // expected
162         }
163
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();
169         try {
170             jis.getNextJarEntry();
171             fail("ZipException expected");
172         } catch (ZipException ee) {
173             // expected
174         }
175     }
176
177     @TestTargetNew(
178         level = TestLevel.PARTIAL_COMPLETE,
179         method = "getNextJarEntry",
180         args = {}
181     )
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();
189         while (je != null) {
190             actual.add(je.toString());
191             je = jis.getNextJarEntry();
192         }
193         assertEquals(actual, desired);
194         jis.close();
195
196         try {
197             jis.getNextJarEntry();
198             fail("IOException expected");
199         } catch (IOException ee) {
200             // expected
201         }
202
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();
208         try {
209             jis.getNextJarEntry();
210             fail("ZipException expected");
211         } catch (ZipException ee) {
212             // expected
213         }
214     }
215
216     @TestTargetNew(
217         level = TestLevel.PARTIAL_COMPLETE,
218         notes = "Exceptions checking missed. Case2",
219         method = "getNextJarEntry",
220         args = {}
221     )
222     public void test_JarInputStream_Integrate_Jar_getNextEntry()
223             throws IOException {
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;
228         int count = 0;
229         while (count == 0 || entry != null) {
230             count++;
231             entry = jin.getNextEntry();
232         }
233         assertEquals(totalEntries + 1, count);
234         jin.close();
235     }
236
237     @TestTargetNew(
238         level = TestLevel.PARTIAL_COMPLETE,
239         notes = "IOException & ZipException checking missed.",
240         method = "getNextEntry",
241         args = {}
242     )
243     public void test_JarInputStream_Modified_Class_getNextEntry()
244             throws IOException {
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;
249
250         int count = 0;
251         while (count == 0 || zipEntry != null) {
252             count++;
253             try {
254                 zipEntry = jin.getNextEntry();
255                 if (count == indexofTESTCLASS + 1) {
256                     fail("Should throw Security Exception");
257                 }
258             } catch (SecurityException e) {
259                 if (count != indexofTESTCLASS + 1) {
260                     throw e;
261                 }
262
263             }
264         }
265         assertEquals(totalEntries + 2, count);
266         jin.close();
267     }
268
269     @TestTargetNew(
270         level = TestLevel.PARTIAL_COMPLETE,
271         notes = "IOException & ZipException checking missed.",
272         method = "getNextEntry",
273         args = {}
274     )
275     public void test_JarInputStream_Modified_Manifest_MainAttributes_getNextEntry()
276             throws IOException {
277         String modJarName = Support_Resources.getURL("Modified_Manifest_MainAttributes.jar");
278         InputStream is = new URL(modJarName).openConnection()
279                 .getInputStream();
280         JarInputStream jin = new JarInputStream(is, true);
281
282         assertEquals("META-INF/TESTROOT.SF", jin.getNextEntry().getName());
283         assertEquals("META-INF/TESTROOT.DSA", jin.getNextEntry().getName());
284         try {
285             jin.getNextEntry();
286             fail();
287         } catch (SecurityException expected) {
288         }
289         assertEquals("META-INF/", jin.getNextEntry().getName());
290         assertEquals("Test.class", jin.getNextEntry().getName());
291         assertNull(jin.getNextEntry());
292         jin.close();
293     }
294
295     @TestTargetNew(
296         level = TestLevel.PARTIAL_COMPLETE,
297         notes = "IOException & ZipException checking missed.",
298         method = "getNextEntry",
299         args = {}
300     )
301     public void test_JarInputStream_Modified_Manifest_EntryAttributes_getNextEntry()
302             throws IOException {
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;
308
309         int count = 0;
310         while (count == 0 || zipEntry != null) {
311             count++;
312             try {
313                 zipEntry = jin.getNextEntry();
314                 if (count == indexofDSA + 1) {
315                     fail("Should throw Security Exception");
316                 }
317             } catch (SecurityException e) {
318                 if (count != indexofDSA + 1) {
319                     throw e;
320                 }
321             }
322         }
323         assertEquals(totalEntries + 2, count);
324         jin.close();
325     }
326
327     @TestTargetNew(
328         level = TestLevel.PARTIAL_COMPLETE,
329         notes = "IOException & ZipException checking missed.",
330         method = "getNextEntry",
331         args = {}
332     )
333     public void test_JarInputStream_Modified_SF_EntryAttributes_getNextEntry()
334             throws IOException {
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;
340
341         int count = 0;
342         while (count == 0 || zipEntry != null) {
343             count++;
344             try {
345                 zipEntry = jin.getNextEntry();
346                 if (count == indexofDSA + 1) {
347                     fail("Should throw Security Exception");
348                 }
349             } catch (SecurityException e) {
350                 if (count != indexofDSA + 1) {
351                     throw e;
352                 }
353             }
354         }
355         assertEquals(totalEntries + 2, count);
356         jin.close();
357     }
358
359     @TestTargetNew(
360         level = TestLevel.PARTIAL_COMPLETE,
361         notes = "IOException & ZipException checking missed.",
362         method = "read",
363         args = {byte[].class}
364     )
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);
369         int count = 0;
370         ZipEntry zipEntry = null;
371         while (count == 0 || zipEntry != null) {
372             count++;
373             zipEntry = jin.getNextEntry();
374             byte[] buffer = new byte[1024];
375             try {
376                 int length = 0;
377                 while (length >= 0) {
378                     length = jin.read(buffer);
379                 }
380                 if (count == indexofTESTCLASS) {
381                     fail("Should throw Security Exception");
382                 }
383             } catch (SecurityException e) {
384                 if (count < indexofTESTCLASS) {
385                     throw e;
386                 }
387             }
388         }
389         assertEquals(totalEntries + 1, count);
390         jin.close();
391     }
392
393     @TestTargetNew(
394         level = TestLevel.PARTIAL,
395         notes = "Exception checking missed.",
396         method = "read",
397         args = {byte[].class}
398     )
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);
403         int count = 0;
404         ZipEntry zipEntry = null;
405         while (count == 0 || zipEntry != null) {
406             count++;
407             zipEntry = jin.getNextEntry();
408             byte[] buffer = new byte[1024];
409             int length = 0;
410             while (length >= 0) {
411                 length = jin.read(buffer);
412             }
413
414         }
415         assertEquals(totalEntries + 1, count);
416         jin.close();
417     }
418
419     @TestTargetNew(
420         level = TestLevel.PARTIAL_COMPLETE,
421         notes = "IOException & ZipException checking missed.",
422         method = "read",
423         args = {byte[].class}
424     )
425     public void test_JarInputStream_Modified_Manifest_MainAttributes_read()
426             throws IOException {
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);
431         int count = 0;
432         ZipEntry zipEntry = null;
433         while (count == 0 || zipEntry != null) {
434             count++;
435             zipEntry = jin.getNextEntry();
436             byte[] buffer = new byte[1024];
437             try {
438                 int length = 0;
439                 while (length >= 0) {
440                     length = jin.read(buffer);
441                 }
442                 if (count == indexofDSA) {
443                     fail("Should throw Security Exception");
444                 }
445             } catch (SecurityException e) {
446                 if (count != indexofDSA) {
447                     throw e;
448                 }
449             }
450         }
451         assertEquals(totalEntries + 1, count);
452         jin.close();
453     }
454
455     @TestTargetNew(
456         level = TestLevel.PARTIAL_COMPLETE,
457         notes = "IOException & ZipException checking missed.",
458         method = "read",
459         args = {byte[].class}
460     )
461     public void test_JarInputStream_Modified_SF_EntryAttributes_read()
462             throws IOException {
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);
467         int count = 0;
468         ZipEntry zipEntry = null;
469         while (count == 0 || zipEntry != null) {
470             count++;
471             zipEntry = jin.getNextEntry();
472             byte[] buffer = new byte[1024];
473             try {
474                 int length = 0;
475                 while (length >= 0) {
476                     length = jin.read(buffer);
477                 }
478                 if (count == indexofDSA) {
479                     fail("Should throw Security Exception");
480                 }
481             } catch (SecurityException e) {
482                 if (count != indexofDSA) {
483                     throw e;
484                 }
485             }
486         }
487         assertEquals(totalEntries + 1, count);
488         jin.close();
489     }
490
491     @TestTargetNew(
492         level = TestLevel.COMPLETE,
493         notes = "",
494         method = "JarInputStream",
495         args = {java.io.InputStream.class, boolean.class}
496     )
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;
501         try {
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
506                     .getManifest());
507             JarEntry je = jis.getNextJarEntry();
508             while (je != null) {
509                 if (je.getName().equals(entryName)) {
510                     hasCorrectEntry = true;
511                 }
512                 je = jis.getNextJarEntry();
513             }
514             assertTrue(
515                     "The jar input stream does not contain the correct entries",
516                     hasCorrectEntry);
517         } catch (Exception e) {
518             fail("Exception during test: " + e.toString());
519         }
520
521         try {
522             is.close();
523             jis = new JarInputStream(is, false);
524             fail("IOException expected");
525         } catch (IOException ee) {
526             // expected
527         }
528     }
529
530     @TestTargetNew(
531         level = TestLevel.COMPLETE,
532         method = "close",
533         args = {}
534     )
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);
540         jis.getNextEntry();
541         try {
542             jis.getNextEntry();
543             fail("ZipException expected");
544         } catch (ZipException ee) {
545             // expected
546         }
547         jis.close();
548         try {
549             jis.getNextEntry();
550             fail("IOException expected");
551         } catch (IOException ee) {
552             // expected
553         }
554     }
555
556     @TestTargetNew(
557         level = TestLevel.COMPLETE,
558         method = "getNextEntry",
559         args = {}
560     )
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);
566         jis.getNextEntry();
567         try {
568             jis.getNextEntry();
569             fail("ZipException expected");
570         } catch (ZipException ee) {
571             // expected
572         }
573
574         try {
575             jis.close();  // Android throws exception here, already!
576             jis.getNextEntry();  // But RI here, only!
577             fail("IOException expected");
578         } catch (IOException ee) {
579             // expected
580         }
581     }
582
583     class Mock_JarInputStream extends JarInputStream {
584
585         public Mock_JarInputStream(InputStream in) throws IOException {
586             super(in);
587         }
588
589         public ZipEntry createZipEntry(String str) {
590             return super.createZipEntry(str);
591         }
592     }
593
594     @TestTargetNew(
595         level = TestLevel.COMPLETE,
596         notes = "",
597         method = "createZipEntry",
598         args = {java.lang.String.class}
599     )
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"));
606     }
607
608     @TestTargetNew(
609         level = TestLevel.COMPLETE,
610         method = "read",
611         args = {byte[].class, int.class, int.class}
612     )
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];
619
620         jis.getNextEntry();
621         jis.read(b, 0, 100);
622         jis.getNextEntry();
623         jis.getNextEntry();
624         jis.getNextEntry();
625
626         try {
627             jis.read(b, 0, 100);
628             fail("ZipException expected");
629         } catch (ZipException ee) {
630             // expected
631         }
632
633         try {
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) {
638             // expected
639         }
640     }
641 }