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  package org.apache.commons.compress.archivers.zip;
19  
20  import static org.junit.jupiter.api.Assertions.assertEquals;
21  import static org.junit.jupiter.api.Assertions.assertNotEquals;
22  
23  import java.math.BigInteger;
24  
25  import org.junit.jupiter.api.Test;
26  
27  /**
28   * JUnit tests for org.apache.commons.compress.archivers.zip.ZipEightByteInteger.
29   */
30  public class ZipEightByteIntegerTest {
31  
32      /**
33       * Test conversion from bytes.
34       */
35      @Test
36      public void testBIFromBytes() {
37          final byte[] val = { (byte) 0xFE, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF };
38          final ZipEightByteInteger zl = new ZipEightByteInteger(val);
39          assertEquals(BigInteger.valueOf(Long.MAX_VALUE).shiftLeft(1), zl.getValue(), "value from bytes");
40      }
41  
42      /**
43       * Test conversion to bytes.
44       */
45      @Test
46      public void testBIToBytes() {
47          final ZipEightByteInteger zl = new ZipEightByteInteger(BigInteger.valueOf(Long.MAX_VALUE).shiftLeft(1));
48          final byte[] result = zl.getBytes();
49          assertEquals(8, result.length, "length getBytes");
50          assertEquals((byte) 0xFE, result[0], "first byte getBytes");
51          assertEquals((byte) 0xFF, result[1], "second byte getBytes");
52          assertEquals((byte) 0xFF, result[2], "third byte getBytes");
53          assertEquals((byte) 0xFF, result[3], "fourth byte getBytes");
54          assertEquals((byte) 0xFF, result[4], "fifth byte getBytes");
55          assertEquals((byte) 0xFF, result[5], "sixth byte getBytes");
56          assertEquals((byte) 0xFF, result[6], "seventh byte getBytes");
57          assertEquals((byte) 0xFF, result[7], "eighth byte getBytes");
58      }
59  
60      /**
61       * Test the contract of the equals method.
62       */
63      @Test
64      public void testEquals() {
65          final ZipEightByteInteger zl = new ZipEightByteInteger(0x12345678);
66          final ZipEightByteInteger zl2 = new ZipEightByteInteger(0x12345678);
67          final ZipEightByteInteger zl3 = new ZipEightByteInteger(0x87654321);
68  
69          assertEquals(zl, zl, "reflexive");
70  
71          assertEquals(zl, zl2, "works");
72          assertNotEquals(zl, zl3, "works, part two");
73  
74          assertEquals(zl2, zl, "symmetric");
75  
76          assertNotEquals(null, zl, "null handling");
77          assertNotEquals(zl, Integer.valueOf(0x1234), "non ZipEightByteInteger handling");
78      }
79  
80      /**
81       * Test conversion from bytes.
82       */
83      @Test
84      public void testLongFromBytes() {
85          final byte[] val = { 0x78, 0x56, 0x34, 0x12, (byte) 0xAB, 0x00, 0x00, 0x00 };
86          final ZipEightByteInteger zl = new ZipEightByteInteger(val);
87          assertEquals(0xAB12345678L, zl.getLongValue(), "longValue from bytes");
88      }
89  
90      /**
91       * Test conversion to bytes.
92       */
93      @Test
94      public void testLongToBytes() {
95          final ZipEightByteInteger zl = new ZipEightByteInteger(0xAB12345678L);
96          final byte[] result = zl.getBytes();
97          assertEquals(8, result.length, "length getBytes");
98          assertEquals(0x78, result[0], "first byte getBytes");
99          assertEquals(0x56, result[1], "second byte getBytes");
100         assertEquals(0x34, result[2], "third byte getBytes");
101         assertEquals(0x12, result[3], "fourth byte getBytes");
102         assertEquals((byte) 0xAB, result[4], "fifth byte getBytes");
103         assertEquals(0, result[5], "sixth byte getBytes");
104         assertEquals(0, result[6], "seventh byte getBytes");
105         assertEquals(0, result[7], "eighth byte getBytes");
106     }
107 
108     /**
109      * Test sign handling.
110      */
111     @Test
112     public void testSign() {
113         final ZipEightByteInteger zl = new ZipEightByteInteger(
114                 new byte[] { (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF });
115         assertEquals(BigInteger.valueOf(Long.MAX_VALUE).shiftLeft(1).setBit(0), zl.getValue());
116     }
117 }