The Android Open Source Project | b5de22c | 2012-04-01 00:00:00 -0700 | [diff] [blame] | 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 libcore.java.util.zip; |
| 19 | |
| 20 | import java.io.ByteArrayOutputStream; |
| 21 | import java.io.File; |
| 22 | import java.io.FileInputStream; |
| 23 | import java.io.IOException; |
| 24 | import java.io.InputStream; |
| 25 | import java.util.zip.ZipEntry; |
| 26 | import java.util.zip.ZipException; |
| 27 | import java.util.zip.ZipInputStream; |
| 28 | import java.util.zip.ZipOutputStream; |
| 29 | import junit.framework.TestCase; |
| 30 | import tests.support.resource.Support_Resources; |
| 31 | |
| 32 | public class OldZipInputStreamTest extends TestCase { |
| 33 | private ZipInputStream zis; |
| 34 | private byte[] dataBytes = "Some data in my file".getBytes(); |
| 35 | |
| 36 | @Override |
| 37 | protected void setUp() throws IOException { |
| 38 | InputStream is = Support_Resources.getStream("hyts_ZipFile.zip"); |
| 39 | if (is == null) { |
| 40 | System.out.println("file hyts_ZipFile.zip can not be found"); |
| 41 | } |
| 42 | zis = new ZipInputStream(is); |
| 43 | |
| 44 | ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
| 45 | ZipOutputStream zos = new ZipOutputStream(bos); |
| 46 | ZipEntry entry = new ZipEntry("myFile"); |
| 47 | zos.putNextEntry(entry); |
| 48 | zos.write(dataBytes); |
| 49 | zos.closeEntry(); |
| 50 | zos.close(); |
| 51 | } |
| 52 | |
| 53 | @Override |
| 54 | protected void tearDown() throws IOException { |
| 55 | if (zis != null) { |
| 56 | zis.close(); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | public void test_skipJ() throws Exception { |
| 61 | File resources = Support_Resources.createTempFolder(); |
| 62 | Support_Resources.copyFile(resources, null, "Broken_manifest.jar"); |
| 63 | FileInputStream fis = new FileInputStream(new File(resources, |
| 64 | "Broken_manifest.jar")); |
| 65 | |
| 66 | ZipInputStream zis1 = new ZipInputStream(fis); |
| 67 | |
| 68 | zis1.getNextEntry(); |
| 69 | zis1.getNextEntry(); |
| 70 | |
| 71 | try { |
| 72 | zis1.skip(10); |
| 73 | fail("ZipException expected"); |
| 74 | } catch (ZipException ee) { |
| 75 | // expected |
| 76 | } |
| 77 | |
| 78 | try { |
| 79 | zis1.close(); // Android throws exception here, already! |
| 80 | zis1.skip(10); // But RI here, only! |
| 81 | fail("IOException expected"); |
| 82 | } catch (IOException ee) { |
| 83 | // expected |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | public void test_read$BII() throws Exception { |
| 88 | byte[] rbuf; |
| 89 | |
| 90 | File resources = Support_Resources.createTempFolder(); |
| 91 | Support_Resources.copyFile(resources, null, "Broken_manifest.jar"); |
| 92 | FileInputStream fis = new FileInputStream(new File(resources, |
| 93 | "Broken_manifest.jar")); |
| 94 | |
| 95 | ZipInputStream zis1 = new ZipInputStream(fis); |
| 96 | |
| 97 | zis1.getNextEntry(); |
| 98 | zis1.getNextEntry(); |
| 99 | |
| 100 | rbuf = new byte[100]; |
| 101 | |
| 102 | try { |
| 103 | zis1.read(rbuf, 10, 90); |
| 104 | fail("ZipException expected"); |
| 105 | } catch (ZipException ee) { |
| 106 | // expected |
| 107 | } |
| 108 | |
| 109 | try { |
| 110 | zis1.close(); // Android throws exception here, already! |
| 111 | zis1.read(rbuf, 10, 90); // But RI here, only! |
| 112 | fail("IOException expected"); |
| 113 | } catch (IOException ee) { |
| 114 | // expected |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | public void test_closeEntry() throws Exception { |
| 119 | zis.getNextEntry(); |
| 120 | zis.closeEntry(); |
| 121 | zis.getNextEntry(); |
| 122 | zis.close(); |
| 123 | try { |
| 124 | zis.closeEntry(); |
| 125 | fail("IOException expected"); |
| 126 | } catch (IOException ee) { |
| 127 | // expected |
| 128 | } |
| 129 | File resources = Support_Resources.createTempFolder(); |
| 130 | Support_Resources.copyFile(resources, null, "Broken_manifest.jar"); |
| 131 | FileInputStream fis = new FileInputStream(new File(resources, |
| 132 | "Broken_manifest.jar")); |
| 133 | |
| 134 | ZipInputStream zis1 = new ZipInputStream(fis); |
| 135 | |
| 136 | try { |
| 137 | for (int i = 0; i < 6; i++) { |
| 138 | zis1.getNextEntry(); |
| 139 | zis1.closeEntry(); |
| 140 | } |
| 141 | fail("ZipException expected"); |
| 142 | } catch (ZipException ee) { |
| 143 | // expected |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | class Mock_ZipInputStream extends ZipInputStream { |
| 148 | boolean createFlag = false; |
| 149 | |
| 150 | public Mock_ZipInputStream(InputStream arg0) { |
| 151 | super(arg0); |
| 152 | } |
| 153 | |
| 154 | boolean getCreateFlag() { |
| 155 | return createFlag; |
| 156 | } |
| 157 | |
| 158 | protected ZipEntry createZipEntry(String name) { |
| 159 | createFlag = true; |
| 160 | return super.createZipEntry(name); |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | public void test_createZipEntryLjava_lang_String() throws Exception { |
| 165 | |
| 166 | File resources = Support_Resources.createTempFolder(); |
| 167 | Support_Resources.copyFile(resources, null, "Broken_manifest.jar"); |
| 168 | File fl = new File(resources, "Broken_manifest.jar"); |
| 169 | FileInputStream fis = new FileInputStream(fl); |
| 170 | |
| 171 | Mock_ZipInputStream zis1 = new Mock_ZipInputStream(fis); |
| 172 | assertFalse(zis1.getCreateFlag()); |
| 173 | zis1.getNextEntry(); |
| 174 | assertTrue(zis1.getCreateFlag()); |
| 175 | } |
| 176 | } |