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