1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
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
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
60
61 @Test
62 void testBIFromMaxValue() {
63
64
65 final ZipEightByteInteger zipEightByteInteger = newMaxValue();
66 assertEquals("18446744073709551615", zipEightByteInteger.getValue().toString());
67 }
68
69
70
71
72 @Test
73 void testBILongFromBytes() {
74 final ZipEightByteInteger zl = newMaxValue();
75 assertEquals(0XFFFFFFFFFFFFFFFFL, zl.getLongValue(), "longValue from bytes");
76 }
77
78
79
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
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
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
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
143
144 @Test
145 void testSign() {
146 assertEquals(BigInteger.valueOf(Long.MAX_VALUE).shiftLeft(1).setBit(0), newMaxValue().getValue());
147 }
148
149
150
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
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 }