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 */
17package org.apache.harmony.archive.tests.java.util.zip;
18
19import java.util.TimeZone;
20import java.util.zip.ZipEntry;
21import java.io.ByteArrayOutputStream;
22import java.io.IOException;
23import java.io.InputStream;
24
25import tests.support.resource.Support_Resources;
26
27public class ZipEntryTest extends junit.framework.TestCase {
28
29    public byte[] getAllBytesFromStream(InputStream is) throws IOException {
30        ByteArrayOutputStream bs = new ByteArrayOutputStream();
31        byte[] buf = new byte[512];
32        int iRead;
33        int off;
34        while (is.available() > 0) {
35            iRead = is.read(buf, 0, buf.length);
36            if (iRead > 0) bs.write(buf, 0, iRead);
37        }
38        return bs.toByteArray();
39    }
40
41	// zip file hyts_ZipFile.zip must be included as a resource
42	java.util.zip.ZipEntry zentry;
43
44	java.util.zip.ZipFile zfile;
45
46	private static final String platformId = System.getProperty(
47			"com.ibm.oti.configuration", "JDK")
48			+ System.getProperty("java.vm.version");
49
50	static final String tempFileName = platformId + "zfzezi.zip";
51
52	long orgSize;
53
54	long orgCompressedSize;
55
56	long orgCrc;
57
58	long orgTime;
59
60	String orgComment;
61
62	/**
63	 * @tests java.util.zip.ZipEntry#ZipEntry(java.lang.String)
64	 */
65	public void test_ConstructorLjava_lang_String() {
66		// Test for method java.util.zip.ZipEntry(java.lang.String)
67		zentry = zfile.getEntry("File3.txt");
68		assertNotNull("Failed to create ZipEntry", zentry);
69		try {
70			zentry = zfile.getEntry(null);
71			fail("NullPointerException not thrown");
72		} catch (NullPointerException e) {
73		}
74		StringBuffer s = new StringBuffer();
75		for (int i = 0; i < 65535; i++) {
76            s.append('a');
77        }
78		try {
79			zentry = new ZipEntry(s.toString());
80		} catch (IllegalArgumentException e) {
81			fail("Unexpected IllegalArgumentException During Test.");
82		}
83		try {
84			s.append('a');
85			zentry = new ZipEntry(s.toString());
86			fail("IllegalArgumentException not thrown");
87		} catch (IllegalArgumentException e) {
88		}
89		try {
90			String n = null;
91			zentry = new ZipEntry(n);
92			fail("NullPointerException not thrown");
93		} catch (NullPointerException e) {
94		}
95	}
96
97	/**
98	 * @tests java.util.zip.ZipEntry#getComment()
99	 */
100	public void test_getComment() {
101		// Test for method java.lang.String java.util.zip.ZipEntry.getComment()
102		ZipEntry zipEntry = new ZipEntry("zippy.zip");
103		assertNull("Incorrect Comment Returned.", zipEntry.getComment());
104		zipEntry.setComment("This Is A Comment");
105		assertEquals("Incorrect Comment Returned.",
106				"This Is A Comment", zipEntry.getComment());
107	}
108
109	/**
110	 * @tests java.util.zip.ZipEntry#getCompressedSize()
111	 */
112	public void test_getCompressedSize() {
113		// Test for method long java.util.zip.ZipEntry.getCompressedSize()
114		assertTrue("Incorrect compressed size returned", zentry
115				.getCompressedSize() == orgCompressedSize);
116	}
117
118	/**
119	 * @tests java.util.zip.ZipEntry#getCrc()
120	 */
121	public void test_getCrc() {
122		// Test for method long java.util.zip.ZipEntry.getCrc()
123		assertEquals("Failed to get Crc", orgCrc, zentry.getCrc());
124	}
125
126	/**
127	 * @tests java.util.zip.ZipEntry#getExtra()
128	 */
129	public void test_getExtra() {
130		// Test for method byte [] java.util.zip.ZipEntry.getExtra()
131		assertNull("Incorrect extra information returned",
132				zentry.getExtra());
133		byte[] ba = { 'T', 'E', 'S', 'T' };
134		zentry = new ZipEntry("test.tst");
135		zentry.setExtra(ba);
136		assertEquals("Incorrect Extra Information Returned.",
137				ba, zentry.getExtra());
138	}
139
140	/**
141	 * @tests java.util.zip.ZipEntry#getMethod()
142	 */
143	public void test_getMethod() {
144		// Test for method int java.util.zip.ZipEntry.getMethod()
145		zentry = zfile.getEntry("File1.txt");
146		assertEquals("Incorrect compression method returned",
147				java.util.zip.ZipEntry.STORED, zentry.getMethod());
148		zentry = zfile.getEntry("File3.txt");
149		assertEquals("Incorrect compression method returned",
150				java.util.zip.ZipEntry.DEFLATED, zentry.getMethod());
151		zentry = new ZipEntry("test.tst");
152		assertEquals("Incorrect Method Returned.", -1, zentry.getMethod());
153	}
154
155	/**
156	 * @tests java.util.zip.ZipEntry#getName()
157	 */
158	public void test_getName() {
159		// Test for method java.lang.String java.util.zip.ZipEntry.getName()
160		assertEquals("Incorrect name returned - Note return result somewhat ambiguous in spec",
161				"File1.txt", zentry.getName());
162	}
163
164	/**
165	 * @tests java.util.zip.ZipEntry#getSize()
166	 */
167	public void test_getSize() {
168		// Test for method long java.util.zip.ZipEntry.getSize()
169		assertEquals("Incorrect size returned", orgSize, zentry.getSize());
170	}
171
172	/**
173	 * @tests java.util.zip.ZipEntry#getTime()
174	 */
175	public void test_getTime() {
176		// Test for method long java.util.zip.ZipEntry.getTime()
177		assertEquals("Failed to get time", orgTime, zentry.getTime());
178	}
179
180	/**
181	 * @tests java.util.zip.ZipEntry#isDirectory()
182	 */
183	public void test_isDirectory() {
184		// Test for method boolean java.util.zip.ZipEntry.isDirectory()
185		assertTrue("Entry should not answer true to isDirectory", !zentry
186				.isDirectory());
187		zentry = new ZipEntry("Directory/");
188		assertTrue("Entry should answer true to isDirectory", zentry
189				.isDirectory());
190	}
191
192	/**
193	 * @tests java.util.zip.ZipEntry#setComment(java.lang.String)
194	 */
195	public void test_setCommentLjava_lang_String() {
196		// Test for method void
197		// java.util.zip.ZipEntry.setComment(java.lang.String)
198		zentry = zfile.getEntry("File1.txt");
199		zentry.setComment("Set comment using api");
200		assertEquals("Comment not correctly set",
201				"Set comment using api", zentry.getComment());
202		String n = null;
203		zentry.setComment(n);
204		assertNull("Comment not correctly set", zentry.getComment());
205		StringBuffer s = new StringBuffer();
206		for (int i = 0; i < 0xFFFF; i++) {
207            s.append('a');
208        }
209		try {
210			zentry.setComment(s.toString());
211		} catch (IllegalArgumentException e) {
212			fail("Unexpected IllegalArgumentException During Test.");
213		}
214		try {
215			s.append('a');
216			zentry.setComment(s.toString());
217			fail("IllegalArgumentException not thrown");
218		} catch (IllegalArgumentException e) {
219		}
220	}
221
222	/**
223	 * @tests java.util.zip.ZipEntry#setCompressedSize(long)
224	 */
225	public void test_setCompressedSizeJ() {
226		// Test for method void java.util.zip.ZipEntry.setCompressedSize(long)
227		zentry.setCompressedSize(orgCompressedSize + 10);
228		assertEquals("Set compressed size failed",
229				(orgCompressedSize + 10), zentry.getCompressedSize());
230		zentry.setCompressedSize(0);
231		assertEquals("Set compressed size failed",
232				0, zentry.getCompressedSize());
233		zentry.setCompressedSize(-25);
234		assertEquals("Set compressed size failed",
235				-25, zentry.getCompressedSize());
236		zentry.setCompressedSize(4294967296l);
237		assertEquals("Set compressed size failed",
238				4294967296l, zentry.getCompressedSize());
239	}
240
241	/**
242	 * @tests java.util.zip.ZipEntry#setCrc(long)
243	 */
244	public void test_setCrcJ() {
245		// Test for method void java.util.zip.ZipEntry.setCrc(long)
246		zentry.setCrc(orgCrc + 100);
247		assertEquals("Failed to set Crc", (orgCrc + 100), zentry.getCrc());
248		zentry.setCrc(0);
249		assertEquals("Failed to set Crc", 0, zentry.getCrc());
250		try {
251			zentry.setCrc(-25);
252			fail("IllegalArgumentException not thrown");
253		} catch (IllegalArgumentException e) {
254		}
255		try {
256			zentry.setCrc(4294967295l);
257		} catch (IllegalArgumentException e) {
258			fail("Unexpected IllegalArgumentException during test");
259		}
260		try {
261			zentry.setCrc(4294967296l);
262			fail("IllegalArgumentException not thrown");
263		} catch (IllegalArgumentException e) {
264		}
265	}
266
267	/**
268	 * @tests java.util.zip.ZipEntry#setExtra(byte[])
269	 */
270	public void test_setExtra$B() {
271		// Test for method void java.util.zip.ZipEntry.setExtra(byte [])
272		zentry = zfile.getEntry("File1.txt");
273		zentry.setExtra("Test setting extra information".getBytes());
274		assertEquals("Extra information not written properly", "Test setting extra information", new String(zentry
275				.getExtra(), 0, zentry.getExtra().length)
276				);
277		zentry = new ZipEntry("test.tst");
278		byte[] ba = new byte[0xFFFF];
279		try {
280			zentry.setExtra(ba);
281		} catch (IllegalArgumentException e) {
282			fail("Unexpected IllegalArgumentException during test");
283		}
284		try {
285			ba = new byte[0xFFFF + 1];
286			zentry.setExtra(ba);
287			fail("IllegalArgumentException not thrown");
288		} catch (IllegalArgumentException e) {
289		}
290
291		// One constructor
292		ZipEntry zeInput = new ZipEntry("InputZIP");
293		byte[] extraB = { 'a', 'b', 'd', 'e' };
294		zeInput.setExtra(extraB);
295		assertEquals(extraB, zeInput.getExtra());
296		assertEquals(extraB[3], zeInput.getExtra()[3]);
297		assertEquals(extraB.length, zeInput.getExtra().length);
298
299		// test another constructor
300		ZipEntry zeOutput = new ZipEntry(zeInput);
301		assertEquals(zeInput.getExtra()[3], zeOutput.getExtra()[3]);
302		assertEquals(zeInput.getExtra().length, zeOutput.getExtra().length);
303		assertEquals(extraB[3], zeOutput.getExtra()[3]);
304		assertEquals(extraB.length, zeOutput.getExtra().length);
305	}
306
307	/**
308	 * @tests java.util.zip.ZipEntry#setMethod(int)
309	 */
310	public void test_setMethodI() {
311		// Test for method void java.util.zip.ZipEntry.setMethod(int)
312		zentry = zfile.getEntry("File3.txt");
313		zentry.setMethod(ZipEntry.STORED);
314		assertEquals("Failed to set compression method",
315				ZipEntry.STORED, zentry.getMethod());
316		zentry.setMethod(ZipEntry.DEFLATED);
317		assertEquals("Failed to set compression method",
318				ZipEntry.DEFLATED, zentry.getMethod());
319		try {
320			int error = 1;
321			zentry = new ZipEntry("test.tst");
322			zentry.setMethod(error);
323			fail("IllegalArgumentException not thrown");
324		} catch (IllegalArgumentException e) {
325		}
326	}
327
328	/**
329	 * @tests java.util.zip.ZipEntry#setSize(long)
330	 */
331	public void test_setSizeJ() {
332		// Test for method void java.util.zip.ZipEntry.setSize(long)
333		zentry.setSize(orgSize + 10);
334		assertEquals("Set size failed", (orgSize + 10), zentry.getSize());
335		zentry.setSize(0);
336		assertEquals("Set size failed", 0, zentry.getSize());
337		try {
338			zentry.setSize(-25);
339			fail("IllegalArgumentException not thrown");
340		} catch (IllegalArgumentException e) {
341		}
342		try {
343			zentry.setCrc(4294967295l);
344		} catch (IllegalArgumentException e) {
345			fail("Unexpected IllegalArgumentException during test");
346		}
347		try {
348			zentry.setCrc(4294967296l);
349			fail("IllegalArgumentException not thrown");
350		} catch (IllegalArgumentException e) {
351		}
352	}
353
354	/**
355	 * @tests java.util.zip.ZipEntry#setTime(long)
356	 */
357	public void test_setTimeJ() {
358		// Test for method void java.util.zip.ZipEntry.setTime(long)
359		zentry.setTime(orgTime + 10000);
360		assertEquals("Test 1: Failed to set time: " + zentry.getTime(), (orgTime + 10000),
361		        zentry.getTime());
362		zentry.setTime(orgTime - 10000);
363		assertEquals("Test 2: Failed to set time: " + zentry.getTime(), (orgTime - 10000),
364		        zentry.getTime());
365		TimeZone zone = TimeZone.getDefault();
366		try {
367			TimeZone.setDefault(TimeZone.getTimeZone("EST"));
368			zentry.setTime(0);
369			assertEquals("Test 3: Failed to set time: " + zentry.getTime(),
370					315550800000L, zentry.getTime());
371			TimeZone.setDefault(TimeZone.getTimeZone("GMT"));
372			assertEquals("Test 3a: Failed to set time: " + zentry.getTime(),
373					315532800000L, zentry.getTime());
374			zentry.setTime(0);
375			TimeZone.setDefault(TimeZone.getTimeZone("EST"));
376			assertEquals("Test 3b: Failed to set time: " + zentry.getTime(),
377					315550800000L, zentry.getTime());
378
379			zentry.setTime(-25);
380			assertEquals("Test 4: Failed to set time: " + zentry.getTime(),
381					315550800000L, zentry.getTime());
382			zentry.setTime(4354837200000L);
383			assertEquals("Test 5: Failed to set time: " + zentry.getTime(),
384					315550800000L, zentry.getTime());
385		} finally {
386			TimeZone.setDefault(zone);
387		}
388	}
389
390	/**
391	 * @tests java.util.zip.ZipEntry#toString()
392	 */
393	public void test_toString() {
394		// Test for method java.lang.String java.util.zip.ZipEntry.toString()
395		assertTrue("Returned incorrect entry name", zentry.toString().indexOf(
396				"File1.txt") >= 0);
397	}
398
399	/**
400	 * @tests java.util.zip.ZipEntry#ZipEntry(java.util.zip.ZipEntry)
401	 */
402	public void test_ConstructorLjava_util_zip_ZipEntry() {
403		// Test for method java.util.zip.ZipEntry(util.zip.ZipEntry)
404		zentry.setSize(2);
405		zentry.setCompressedSize(4);
406		zentry.setComment("Testing");
407		ZipEntry zentry2 = new ZipEntry(zentry);
408		assertEquals("ZipEntry Created With Incorrect Size.",
409				2, zentry2.getSize());
410		assertEquals("ZipEntry Created With Incorrect Compressed Size.", 4, zentry2
411				.getCompressedSize());
412		assertEquals("ZipEntry Created With Incorrect Comment.", "Testing", zentry2
413				.getComment());
414		assertEquals("ZipEntry Created With Incorrect Crc.",
415				orgCrc, zentry2.getCrc());
416		assertEquals("ZipEntry Created With Incorrect Time.",
417				orgTime, zentry2.getTime());
418	}
419
420	/**
421	 * @tests java.util.zip.ZipEntry#clone()
422	 */
423	public void test_clone() {
424		// Test for method java.util.zip.ZipEntry.clone()
425		Object obj = zentry.clone();
426		assertEquals("toString()", zentry.toString(), obj.toString());
427		assertEquals("hashCode()", zentry.hashCode(), obj.hashCode());
428
429		// One constructor
430		ZipEntry zeInput = new ZipEntry("InputZIP");
431		byte[] extraB = { 'a', 'b', 'd', 'e' };
432		zeInput.setExtra(extraB);
433		assertEquals(extraB, zeInput.getExtra());
434		assertEquals(extraB[3], zeInput.getExtra()[3]);
435		assertEquals(extraB.length, zeInput.getExtra().length);
436
437		// test Clone()
438		ZipEntry zeOutput = (ZipEntry) zeInput.clone();
439		assertEquals(zeInput.getExtra()[3], zeOutput.getExtra()[3]);
440		assertEquals(zeInput.getExtra().length, zeOutput.getExtra().length);
441		assertEquals(extraB[3], zeOutput.getExtra()[3]);
442		assertEquals(extraB.length, zeOutput.getExtra().length);
443	}
444
445	/**
446	 * Sets up the fixture, for example, open a network connection. This method
447	 * is called before a test is executed.
448	 */
449
450	@Override
451    protected void setUp() {
452		java.io.File f = null;
453		try {
454			// Create a local copy of the file since some tests want to alter
455			// information.
456			f = new java.io.File(tempFileName);
457			// Create absolute filename as ZipFile does not resolve using
458			// user.dir
459			f = new java.io.File(f.getAbsolutePath());
460			f.delete();
461			java.io.InputStream is = Support_Resources
462					.getStream("hyts_ZipFile.zip");
463			java.io.FileOutputStream fos = new java.io.FileOutputStream(f);
464            byte[] rbuf = getAllBytesFromStream(is);
465			fos.write(rbuf, 0, rbuf.length);
466			is.close();
467			fos.close();
468			zfile = new java.util.zip.ZipFile(f);
469			zentry = zfile.getEntry("File1.txt");
470			orgSize = zentry.getSize();
471			orgCompressedSize = zentry.getCompressedSize();
472			orgCrc = zentry.getCrc();
473			orgTime = zentry.getTime();
474			orgComment = zentry.getComment();
475		} catch (Exception e) {
476			System.out.println("Exception during ZipFile setup <"
477					+ f.getAbsolutePath() + ">: ");
478			e.printStackTrace();
479		}
480	}
481
482	/**
483	 * Tears down the fixture, for example, close a network connection. This
484	 * method is called after a test is executed.
485	 */
486
487	@Override
488    protected void tearDown() {
489		try {
490			if (zfile != null) {
491                zfile.close();
492            }
493			java.io.File f = new java.io.File(tempFileName);
494			f.delete();
495		} catch (java.io.IOException e) {
496			System.out.println("Exception during tearDown");
497		}
498	}
499
500}
501