View Javadoc
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  
19  package org.apache.commons.compress.archivers.zip;
20  
21  import junit.framework.TestCase;
22  
23  import java.math.BigInteger;
24  import java.util.Arrays;
25  import java.util.Calendar;
26  import java.util.Date;
27  
28  public class ZipUtilTest extends TestCase {
29  
30      private Date time;
31      private ZipLong zl;
32  
33      /**
34       * Constructor
35       */
36      public ZipUtilTest(String name) {
37          super(name);
38      }
39  
40      @Override
41      protected void setUp() throws Exception {
42          time = new Date();
43          Calendar cal = Calendar.getInstance();
44          cal.setTime(time);
45          int year = cal.get(Calendar.YEAR);
46          int month = cal.get(Calendar.MONTH) + 1;
47          long value =  ((year - 1980) << 25)
48              |         (month << 21)
49              |         (cal.get(Calendar.DAY_OF_MONTH) << 16)
50              |         (cal.get(Calendar.HOUR_OF_DAY) << 11)
51              |         (cal.get(Calendar.MINUTE) << 5)
52              |         (cal.get(Calendar.SECOND) >> 1);
53  
54          byte[] result = new byte[4];
55          result[0] = (byte) ((value & 0xFF));
56          result[1] = (byte) ((value & 0xFF00) >> 8);
57          result[2] = (byte) ((value & 0xFF0000) >> 16);
58          result[3] = (byte) ((value & 0xFF000000L) >> 24);
59          zl = new ZipLong(result);
60      }
61  
62      @Override
63      protected void tearDown() throws Exception {
64          super.tearDown();
65      }
66  
67      public void testZipLong() throws Exception {
68          ZipLong test = ZipUtil.toDosTime(time);
69          assertEquals(test.getValue(), zl.getValue());
70      }
71  
72      public void testAdjustToLong() {
73          assertEquals(Integer.MAX_VALUE,
74                       ZipUtil.adjustToLong(Integer.MAX_VALUE));
75          assertEquals(((long) Integer.MAX_VALUE) + 1,
76                       ZipUtil.adjustToLong(Integer.MAX_VALUE + 1));
77          assertEquals(2 * ((long) Integer.MAX_VALUE),
78                       ZipUtil.adjustToLong(2 * Integer.MAX_VALUE));
79      }
80  
81      public void testMinTime(){
82          byte[] b1 = ZipUtil.toDosTime(0);
83          byte b10 = b1[0]; // Save the first byte
84          b1[0]++; // change it
85          byte[] b2 = ZipUtil.toDosTime(0); // get the same time
86          assertEquals(b10,b2[0]); // first byte should still be the same
87      }
88  
89      public void testReverse() {
90          byte[][] bTest = new byte[6][];
91          bTest[0] = new byte[]{};
92          bTest[1] = new byte[]{1};
93          bTest[2] = new byte[]{1, 2};
94          bTest[3] = new byte[]{1, 2, 3};
95          bTest[4] = new byte[]{1, 2, 3, 4};
96          bTest[5] = new byte[]{1, 2, 3, 4, 5};
97  
98          byte[][] rTest = new byte[6][];
99          rTest[0] = new byte[]{};
100         rTest[1] = new byte[]{1};
101         rTest[2] = new byte[]{2, 1};
102         rTest[3] = new byte[]{3, 2, 1};
103         rTest[4] = new byte[]{4, 3, 2, 1};
104         rTest[5] = new byte[]{5, 4, 3, 2, 1};
105 
106         assertEquals("test and result arrays are same length", bTest.length, rTest.length);
107 
108         for (int i = 0; i < bTest.length; i++) {
109             byte[] result = ZipUtil.reverse(bTest[i]);
110             assertTrue("reverse mutates in-place", bTest[i] == result);
111             assertTrue("reverse actually reverses", Arrays.equals(rTest[i], result));
112         }
113     }
114 
115     public void testBigToLong() {
116         BigInteger big1 = BigInteger.valueOf(1);
117         BigInteger big2 = BigInteger.valueOf(Long.MAX_VALUE);
118         BigInteger big3 = BigInteger.valueOf(Long.MIN_VALUE);
119 
120         assertEquals(1L, ZipUtil.bigToLong(big1));
121         assertEquals(Long.MAX_VALUE, ZipUtil.bigToLong(big2));
122         assertEquals(Long.MIN_VALUE, ZipUtil.bigToLong(big3));
123 
124         BigInteger big4 = big2.add(big1);
125         try {
126             ZipUtil.bigToLong(big4);
127             fail("Should have thrown IllegalArgumentException");
128         } catch (IllegalArgumentException iae) {
129             // All is good.
130         }
131 
132         BigInteger big5 = big3.subtract(big1);
133         try {
134             ZipUtil.bigToLong(big5);
135             fail("ZipUtil.bigToLong(BigInteger) should have thrown IllegalArgumentException");
136         } catch (IllegalArgumentException iae) {
137             // All is good.
138         }
139     }
140 
141     public void testLongToBig() {
142         long l0 = 0;
143         long l1 = 1;
144         long l2 = -1;
145         long l3 = Integer.MIN_VALUE;
146         long l4 = Long.MAX_VALUE;
147         long l5 = Long.MIN_VALUE;
148 
149         BigInteger big0 = ZipUtil.longToBig(l0);
150         BigInteger big1 = ZipUtil.longToBig(l1);
151         BigInteger big2 = ZipUtil.longToBig(l2);
152         BigInteger big3 = ZipUtil.longToBig(l3);
153         BigInteger big4 = ZipUtil.longToBig(l4);
154 
155         assertEquals(0, big0.longValue());
156         assertEquals(1, big1.longValue());
157         assertEquals(0xFFFFFFFFL, big2.longValue());
158         assertEquals(0x80000000L, big3.longValue());
159         assertEquals(Long.MAX_VALUE, big4.longValue());
160 
161         try {
162             ZipUtil.longToBig(l5);
163             fail("ZipUtil.longToBig(long) should have thrown IllegalArgumentException");
164         } catch (IllegalArgumentException iae) {
165 
166         }
167     }
168 
169     public void testSignedByteToUnsignedInt() {
170         // Yay, we can completely test all possible input values in this case!
171         int expectedVal = 128;
172         for (int i = Byte.MIN_VALUE; i <= Byte.MAX_VALUE; i++) {
173             byte b = (byte) i;
174             assertEquals(expectedVal, ZipUtil.signedByteToUnsignedInt(b));
175             expectedVal++;
176             if (expectedVal == 256) {
177                 expectedVal = 0;
178             }
179         }
180     }
181 
182     public void testUnsignedIntToSignedByte() {
183         int unsignedVal = 128;
184         for (int i = Byte.MIN_VALUE; i <= Byte.MAX_VALUE; i++) {
185             byte expectedVal = (byte) i;
186             assertEquals(expectedVal, ZipUtil.unsignedIntToSignedByte(unsignedVal));
187             unsignedVal++;
188             if (unsignedVal == 256) {
189                 unsignedVal = 0;
190             }
191         }
192 
193         try {
194             ZipUtil.unsignedIntToSignedByte(-1);
195             fail("ZipUtil.unsignedIntToSignedByte(-1) should have thrown IllegalArgumentException");
196         } catch (IllegalArgumentException iae) {
197             // All is good.
198         }
199 
200         try {
201             ZipUtil.unsignedIntToSignedByte(256);
202             fail("ZipUtil.unsignedIntToSignedByte(256) should have thrown IllegalArgumentException");
203         } catch (IllegalArgumentException iae) {
204             // All is good.
205         }
206 
207     }
208 
209 
210 }