1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.commons.compress.archivers.zip;
20
21 import static org.apache.commons.compress.AbstractTest.getFile;
22 import static org.junit.jupiter.api.Assertions.assertArrayEquals;
23 import static org.junit.jupiter.api.Assertions.assertEquals;
24 import static org.junit.jupiter.api.Assertions.assertNotEquals;
25 import static org.junit.jupiter.api.Assertions.assertSame;
26 import static org.junit.jupiter.api.Assertions.assertTrue;
27
28 import java.util.zip.ZipException;
29
30 import org.apache.commons.compress.utils.ByteUtils;
31 import org.junit.jupiter.api.BeforeEach;
32 import org.junit.jupiter.api.Test;
33
34 class X7875_NewUnixTest {
35
36 private static final ZipShort X7875 = new ZipShort(0x7875);
37
38 private static byte[] trimTest(final byte[] b) {
39 return X7875_NewUnix.trimLeadingZeroesForceMinLength(b);
40 }
41
42 private X7875_NewUnix xf;
43
44 @BeforeEach
45 public void before() {
46 xf = new X7875_NewUnix();
47 }
48
49 private void parseReparse(final long uid, final long gid, final byte[] expected, final long expectedUID, final long expectedGID) throws ZipException {
50
51
52 xf.setUID(54321);
53 xf.setGID(12345);
54 xf.parseFromLocalFileData(expected, 0, expected.length);
55 assertEquals(expectedUID, xf.getUID());
56 assertEquals(expectedGID, xf.getGID());
57
58 xf.setUID(uid);
59 xf.setGID(gid);
60 if (expected.length < 5) {
61
62 assertEquals(5, xf.getLocalFileDataLength().getValue());
63 } else {
64 assertEquals(expected.length, xf.getLocalFileDataLength().getValue());
65 }
66 byte[] result = xf.getLocalFileDataData();
67 if (expected.length < 5) {
68
69 assertArrayEquals(new byte[] { 1, 1, 0, 1, 0 }, result);
70 } else {
71 assertArrayEquals(expected, result);
72 }
73
74
75 xf.parseFromLocalFileData(result, 0, result.length);
76
77
78 assertEquals(expectedUID, xf.getUID());
79 assertEquals(expectedGID, xf.getGID());
80
81 assertEquals(0, xf.getCentralDirectoryLength().getValue());
82 result = xf.getCentralDirectoryData();
83 assertArrayEquals(ByteUtils.EMPTY_BYTE_ARRAY, result);
84
85
86 xf.parseFromCentralDirectoryData(result, 0, result.length);
87
88
89 assertEquals(expectedUID, xf.getUID());
90 assertEquals(expectedGID, xf.getGID());
91 }
92
93 @Test
94 void testGetHeaderId() {
95 assertEquals(X7875, xf.getHeaderId());
96 }
97
98 @Test
99 void testMisc() throws Exception {
100 assertNotEquals(xf, new Object());
101 assertTrue(xf.toString().startsWith("0x7875 Zip Extra Field"));
102 final Object o = xf.clone();
103 assertEquals(o.hashCode(), xf.hashCode());
104 assertEquals(xf, o);
105 xf.setUID(12345);
106 assertNotEquals(xf, o);
107 }
108
109 @Test
110 void testParseReparse() throws ZipException {
111
112
113 final byte[] ZERO_LEN = { 1, 0, 0 };
114
115
116 final byte[] ZERO_UID_GID = { 1, 1, 0, 1, 0 };
117
118
119 final byte[] ONE_UID_GID = { 1, 1, 1, 1, 1 };
120
121
122 final byte[] ONE_THOUSAND_UID_GID = { 1, 2, -24, 3, 2, -24, 3 };
123
124
125
126 final byte[] UNIX_MAX_UID_GID = { 1, 4, -2, -1, -1, -1, 4, -2, -1, -1, -1 };
127
128
129
130 final byte[] LENGTH_5 = { 1, 5, 0, 0, 0, 0, 1, 5, 1, 0, 0, 0, 1 };
131
132
133
134 final byte[] LENGTH_8 = { 1, 8, -2, -1, -1, -1, -1, -1, -1, 127, 8, -1, -1, -1, -1, -1, -1, -1, 127 };
135
136 final long TWO_TO_32 = 0x100000000L;
137 final long MAX = TWO_TO_32 - 2;
138
139 parseReparse(0, 0, ZERO_LEN, 0, 0);
140 parseReparse(0, 0, ZERO_UID_GID, 0, 0);
141 parseReparse(1, 1, ONE_UID_GID, 1, 1);
142 parseReparse(1000, 1000, ONE_THOUSAND_UID_GID, 1000, 1000);
143 parseReparse(MAX, MAX, UNIX_MAX_UID_GID, MAX, MAX);
144 parseReparse(-2, -2, UNIX_MAX_UID_GID, MAX, MAX);
145 parseReparse(TWO_TO_32, TWO_TO_32 + 1, LENGTH_5, TWO_TO_32, TWO_TO_32 + 1);
146 parseReparse(Long.MAX_VALUE - 1, Long.MAX_VALUE, LENGTH_8, Long.MAX_VALUE - 1, Long.MAX_VALUE);
147
148
149 final byte[] SPURIOUS_ZEROES_1 = { 1, 4, -1, 0, 0, 0, 4, -128, 0, 0, 0 };
150 final byte[] EXPECTED_1 = { 1, 1, -1, 1, -128 };
151 xf.parseFromLocalFileData(SPURIOUS_ZEROES_1, 0, SPURIOUS_ZEROES_1.length);
152
153 assertEquals(255, xf.getUID());
154 assertEquals(128, xf.getGID());
155 assertArrayEquals(EXPECTED_1, xf.getLocalFileDataData());
156
157 final byte[] SPURIOUS_ZEROES_2 = { 1, 4, -1, -1, 0, 0, 4, 1, 2, 0, 0 };
158 final byte[] EXPECTED_2 = { 1, 2, -1, -1, 2, 1, 2 };
159 xf.parseFromLocalFileData(SPURIOUS_ZEROES_2, 0, SPURIOUS_ZEROES_2.length);
160
161 assertEquals(65535, xf.getUID());
162 assertEquals(513, xf.getGID());
163 assertArrayEquals(EXPECTED_2, xf.getLocalFileDataData());
164 }
165
166 @Test
167 void testSampleFile() throws Exception {
168 try (ZipFile zipFile = ZipFile.builder().setFile(getFile("COMPRESS-211_uid_gid_zip_test.zip")).get()) {
169
170
171 zipFile.stream().forEach(zae -> {
172 final String name = zae.getName();
173 final X7875_NewUnix xf = (X7875_NewUnix) zae.getExtraField(X7875);
174
175 long expected = 1000;
176 if (name.contains("uid555_gid555")) {
177 expected = 555;
178 } else if (name.contains("uid5555_gid5555")) {
179 expected = 5555;
180 } else if (name.contains("uid55555_gid55555")) {
181 expected = 55555;
182 } else if (name.contains("uid555555_gid555555")) {
183 expected = 555555;
184 } else if (name.contains("min_unix")) {
185 expected = 0;
186 } else if (name.contains("max_unix")) {
187
188
189 expected = 0x100000000L - 2;
190 }
191 assertEquals(expected, xf.getUID());
192 assertEquals(expected, xf.getGID());
193 });
194 }
195 }
196
197 @Test
198 void testTrimLeadingZeroesForceMinLength4() {
199 final byte[] NULL = null;
200 final byte[] EMPTY = ByteUtils.EMPTY_BYTE_ARRAY;
201 final byte[] ONE_ZERO = { 0 };
202 final byte[] TWO_ZEROES = { 0, 0 };
203 final byte[] FOUR_ZEROES = { 0, 0, 0, 0 };
204 final byte[] SEQUENCE = { 1, 2, 3 };
205 final byte[] SEQUENCE_LEADING_ZERO = { 0, 1, 2, 3 };
206 final byte[] SEQUENCE_LEADING_ZEROES = { 0, 0, 0, 0, 0, 0, 0, 1, 2, 3 };
207 final byte[] TRAILING_ZERO = { 1, 2, 3, 0 };
208 final byte[] PADDING_ZERO = { 0, 1, 2, 3, 0 };
209 final byte[] SEQUENCE6 = { 1, 2, 3, 4, 5, 6 };
210 final byte[] SEQUENCE6_LEADING_ZERO = { 0, 1, 2, 3, 4, 5, 6 };
211
212 assertSame(NULL, trimTest(NULL));
213 assertArrayEquals(ONE_ZERO, trimTest(EMPTY));
214 assertArrayEquals(ONE_ZERO, trimTest(ONE_ZERO));
215 assertArrayEquals(ONE_ZERO, trimTest(TWO_ZEROES));
216 assertArrayEquals(ONE_ZERO, trimTest(FOUR_ZEROES));
217 assertArrayEquals(SEQUENCE, trimTest(SEQUENCE));
218 assertArrayEquals(SEQUENCE, trimTest(SEQUENCE_LEADING_ZERO));
219 assertArrayEquals(SEQUENCE, trimTest(SEQUENCE_LEADING_ZEROES));
220 assertArrayEquals(TRAILING_ZERO, trimTest(TRAILING_ZERO));
221 assertArrayEquals(TRAILING_ZERO, trimTest(PADDING_ZERO));
222 assertArrayEquals(SEQUENCE6, trimTest(SEQUENCE6));
223 assertArrayEquals(SEQUENCE6, trimTest(SEQUENCE6_LEADING_ZERO));
224 }
225 }