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
18package libcore.java.util.zip;
19
20import tests.support.Support_PlatformFile;
21import tests.support.resource.Support_Resources;
22
23import java.io.ByteArrayOutputStream;
24import java.io.File;
25import java.io.FileOutputStream;
26import java.io.FilePermission;
27import java.io.IOException;
28import java.io.InputStream;
29import java.io.OutputStream;
30import java.security.Permission;
31import java.util.Enumeration;
32import java.util.zip.ZipEntry;
33import java.util.zip.ZipException;
34import java.util.zip.ZipFile;
35
36public class OldZipFileTest extends junit.framework.TestCase {
37
38    public byte[] getAllBytesFromStream(InputStream is) throws IOException {
39        ByteArrayOutputStream bs = new ByteArrayOutputStream();
40        byte[] buf = new byte[512];
41        int iRead;
42        while ((iRead = is.read(buf, 0, buf.length)) != -1) {
43            bs.write(buf, 0, iRead);
44        }
45        return bs.toByteArray();
46    }
47
48    public void test_size() throws IOException {
49        zfile.close();
50        try {
51            zfile.size();
52            fail("IllegalStateException expected");
53        } catch (IllegalStateException expected) {
54        }
55    }
56
57    public void test_getEntryLjava_lang_String_AndroidOnly() throws IOException {
58        java.util.zip.ZipEntry zentry = zfile.getEntry("File1.txt");
59        assertNotNull("Could not obtain ZipEntry", zentry);
60        int r;
61        InputStream in;
62
63        zentry = zfile.getEntry("testdir1");
64        assertNotNull("Must be able to obtain ZipEntry: testdir1", zentry);
65        in = zfile.getInputStream(zentry);
66        /*
67         * Android delivers empty InputStream, RI no InputStream at all. The
68         * spec doesn't clarify this, so we need to deal with both situations.
69         */
70        int data = -1;
71        if (in != null) {
72            data = in.read();
73            in.close();
74        }
75        assertEquals("Must not be able to read directory data", -1, data);
76    }
77
78    // the file hyts_zipFile.zip in setup must be included as a resource
79    private String tempFileName;
80
81    private ZipFile zfile;
82
83    /**
84     * @throws IOException
85     * java.util.zip.ZipFile#close()
86     */
87    public void test_close() throws IOException {
88        // Test for method void java.util.zip.ZipFile.close()
89        File fl = new File(tempFileName);
90        ZipFile zf = new ZipFile(fl);
91        InputStream is1 = zf.getInputStream(zf.getEntry("File1.txt"));
92        InputStream is2 = zf.getInputStream(zf.getEntry("File2.txt"));
93
94        is1.read();
95        is2.read();
96
97        zf.close();
98
99        try {
100            is1.read();
101            fail("IOException expected");
102        } catch (IOException ee) {
103            // expected
104        }
105
106        try {
107            is2.read();
108            fail("IOException expected");
109        } catch (IOException ee) {
110            // expected
111        }
112    }
113
114    public void test_getEntryLjava_lang_String_Ex() throws IOException {
115        java.util.zip.ZipEntry zentry = zfile.getEntry("File1.txt");
116        assertNotNull("Could not obtain ZipEntry", zentry);
117
118        zfile.close();
119        try {
120            zfile.getEntry("File2.txt");
121            fail("IllegalStateException expected");
122        } catch (IllegalStateException ee) {
123        }
124    }
125
126    /**
127     * @throws IOException
128     * java.util.zip.ZipFile#getInputStream(java.util.zip.ZipEntry)
129     */
130    public void test_getInputStreamLjava_util_zip_ZipEntry() throws IOException {
131        // Test for method java.io.InputStream
132        // java.util.zip.ZipFile.getInputStream(java.util.zip.ZipEntry)
133        ZipEntry zentry = null;
134        InputStream is = null;
135
136        zentry = zfile.getEntry("File2.txt");
137        zfile.close();
138        try {
139            is = zfile.getInputStream(zentry);
140            fail("IllegalStateException expected");
141        } catch (IllegalStateException ee) {
142            // expected
143        }
144    }
145
146    /**
147     * Sets up the fixture, for example, open a network connection. This method
148     * is called before a test is executed.
149     */
150    @Override
151    protected void setUp() throws IOException {
152        // Create a local copy of the file since some tests want to alter information.
153        tempFileName = System.getProperty("java.io.tmpdir");
154        String separator = System.getProperty("file.separator");
155        if (tempFileName.charAt(tempFileName.length() - 1) == separator.charAt(0)) {
156            tempFileName = Support_PlatformFile.getNewPlatformFile(tempFileName, "gabba.zip");
157        } else {
158            tempFileName = Support_PlatformFile.getNewPlatformFile(
159                    tempFileName + separator, "gabba.zip");
160        }
161
162        File f = new File(tempFileName);
163        f.delete();
164        InputStream is = Support_Resources.getStream("hyts_ZipFile.zip");
165        FileOutputStream fos = new FileOutputStream(f);
166        byte[] rbuf = getAllBytesFromStream(is);
167        fos.write(rbuf, 0, rbuf.length);
168        is.close();
169        fos.close();
170        zfile = new ZipFile(f);
171    }
172
173    /**
174     * Tears down the fixture, for example, close a network connection. This
175     * method is called after a test is executed.
176     */
177    @Override
178    protected void tearDown() throws IOException {
179        // Note zfile is a user-defined zip file used by other tests and
180        // should not be deleted
181        zfile.close();
182        tempFileName = System.getProperty("java.io.tmpdir");
183        String separator = System.getProperty("file.separator");
184        if (tempFileName.charAt(tempFileName.length() - 1) == separator
185                .charAt(0)) {
186            tempFileName = Support_PlatformFile.getNewPlatformFile(
187                    tempFileName, "gabba.zip");
188        } else {
189            tempFileName = Support_PlatformFile.getNewPlatformFile(
190                    tempFileName + separator, "gabba.zip");
191        }
192
193        File f = new File(tempFileName);
194        f.delete();
195    }
196}
197