1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
29
30 public class ZipEightByteIntegerTest {
31
32
33
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
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
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
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
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
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 }