View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   https://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.commons.compress.archivers.zip;
21  
22  import static org.junit.jupiter.api.Assertions.assertEquals;
23  import static org.junit.jupiter.api.Assertions.assertNotEquals;
24  
25  import java.math.BigInteger;
26  
27  import org.junit.jupiter.api.Test;
28  
29  /**
30   * Tests {@link ZipEightByteInteger}.
31   */
32  class ZipEightByteIntegerTest {
33  
34      private byte[] getBytes(final ZipEightByteInteger zl) {
35          final byte[] result = zl.getBytes();
36          assertEquals(8, result.length, "length getBytes");
37          return result;
38      }
39  
40      private byte[] newMaxByteArrayValue() {
41          return new byte[] { (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF };
42      }
43  
44      private ZipEightByteInteger newMaxValue() {
45          return new ZipEightByteInteger(newMaxByteArrayValue());
46      }
47  
48      /**
49       * Tests conversion from bytes.
50       */
51      @Test
52      void testBIFromBytes() {
53          final byte[] val = { (byte) 0xFE, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF };
54          final ZipEightByteInteger zl = new ZipEightByteInteger(val);
55          assertEquals(BigInteger.valueOf(Long.MAX_VALUE).shiftLeft(1), zl.getValue(), "value from bytes");
56      }
57  
58      /**
59       * Tests conversion from max value.
60       */
61      @Test
62      void testBIFromMaxValue() {
63          // https://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT
64          // 4.4.1.1 All fields unless otherwise noted are unsigned and stored in Intel low-byte:high-byte, low-word:high-word order.
65          final ZipEightByteInteger zipEightByteInteger = newMaxValue();
66          assertEquals("18446744073709551615", zipEightByteInteger.getValue().toString());
67      }
68  
69      /**
70       * Tests conversion from bytes.
71       */
72      @Test
73      void testBILongFromBytes() {
74          final ZipEightByteInteger zl = newMaxValue();
75          assertEquals(0XFFFFFFFFFFFFFFFFL, zl.getLongValue(), "longValue from bytes");
76      }
77  
78      /**
79       * Tests conversion to bytes.
80       */
81      @Test
82      void testBIToBytes() {
83          final byte[] result = getBytes(new ZipEightByteInteger(BigInteger.valueOf(Long.MAX_VALUE).shiftLeft(1)));
84          assertEquals((byte) 0xFE, result[0], "first byte getBytes");
85          assertEquals((byte) 0xFF, result[1], "second byte getBytes");
86          assertEquals((byte) 0xFF, result[2], "third byte getBytes");
87          assertEquals((byte) 0xFF, result[3], "fourth byte getBytes");
88          assertEquals((byte) 0xFF, result[4], "fifth byte getBytes");
89          assertEquals((byte) 0xFF, result[5], "sixth byte getBytes");
90          assertEquals((byte) 0xFF, result[6], "seventh byte getBytes");
91          assertEquals((byte) 0xFF, result[7], "eighth byte getBytes");
92      }
93  
94      /**
95       * Tests the contract of the equals method.
96       */
97      @Test
98      void testEquals() {
99          final ZipEightByteInteger zl = new ZipEightByteInteger(0x12345678);
100         final ZipEightByteInteger zl2 = new ZipEightByteInteger(0x12345678);
101         final ZipEightByteInteger zl3 = new ZipEightByteInteger(0x87654321);
102 
103         assertEquals(zl, zl, "reflexive");
104 
105         assertEquals(zl, zl2, "works");
106         assertNotEquals(zl, zl3, "works, part two");
107 
108         assertEquals(zl2, zl, "symmetric");
109 
110         assertNotEquals(null, zl, "null handling");
111         assertNotEquals(zl, Integer.valueOf(0x1234), "non ZipEightByteInteger handling");
112     }
113 
114     /**
115      * Tests conversion from bytes.
116      */
117     @Test
118     void testLongFromBytes() {
119         final byte[] val = { 0x78, 0x56, 0x34, 0x12, (byte) 0xAB, 0x00, 0x00, 0x00 };
120         final ZipEightByteInteger zl = new ZipEightByteInteger(val);
121         assertEquals(0xAB12345678L, zl.getLongValue(), "longValue from bytes");
122     }
123 
124     /**
125      * Tests conversion to bytes.
126      */
127     @Test
128     void testLongToBytes() {
129         final byte[] result = getBytes(new ZipEightByteInteger(0xAB12345678L));
130         assertEquals(8, result.length, "length getBytes");
131         assertEquals(0x78, result[0], "first byte getBytes");
132         assertEquals(0x56, result[1], "second byte getBytes");
133         assertEquals(0x34, result[2], "third byte getBytes");
134         assertEquals(0x12, result[3], "fourth byte getBytes");
135         assertEquals((byte) 0xAB, result[4], "fifth byte getBytes");
136         assertEquals(0, result[5], "sixth byte getBytes");
137         assertEquals(0, result[6], "seventh byte getBytes");
138         assertEquals(0, result[7], "eighth byte getBytes");
139     }
140 
141     /**
142      * Tests sign handling.
143      */
144     @Test
145     void testSign() {
146         assertEquals(BigInteger.valueOf(Long.MAX_VALUE).shiftLeft(1).setBit(0), newMaxValue().getValue());
147     }
148 
149     /**
150      * Tests {@link ZipEightByteInteger#toString()}.
151      */
152     @Test
153     void testToString() {
154         assertEquals("0", ZipEightByteInteger.ZERO.toString());
155         assertEquals("0", ZipEightByteInteger.getValue(new byte[ZipEightByteInteger.BYTES]).toString());
156         assertEquals(Long.toString(Long.MAX_VALUE), new ZipEightByteInteger(BigInteger.valueOf(Long.MAX_VALUE)).toString());
157         assertEquals("18446744073709551615", newMaxValue().toString());
158     }
159 
160     /**
161      * Tests {@link ZipEightByteInteger#toUnsignedBigInteger(long)}.
162      */
163     @Test
164     void testToUnsignedBigInteger() {
165         assertEquals(BigInteger.valueOf(Long.MAX_VALUE), ZipEightByteInteger.toUnsignedBigInteger(Long.MAX_VALUE));
166         assertEquals(BigInteger.valueOf(Long.MAX_VALUE).shiftLeft(1), ZipEightByteInteger.toUnsignedBigInteger(0XFFFFFFFFFFFFFFFEL));
167     }
168 }