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;
21
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.assertFalse;
25 import static org.junit.jupiter.api.Assertions.assertNotEquals;
26 import static org.junit.jupiter.api.Assertions.assertThrows;
27 import static org.junit.jupiter.api.Assertions.assertTrue;
28
29 import org.apache.commons.compress.archivers.sevenz.SevenZArchiveEntry;
30 import org.apache.commons.compress.utils.ArchiveUtils;
31 import org.junit.jupiter.api.Test;
32
33 class ArchiveUtilsTest extends AbstractTest {
34
35 private static final int bytesToTest = 50;
36 private static final byte[] byteTest = new byte[bytesToTest];
37
38 static {
39 for (int i = 0; i < byteTest.length;) {
40 byteTest[i] = (byte) i;
41 byteTest[i + 1] = (byte) -i;
42 i += 2;
43 }
44 }
45
46 private void asciiToByteAndBackFail(final String inputString) {
47 assertNotEquals(inputString, ArchiveUtils.toAsciiString(ArchiveUtils.toAsciiBytes(inputString)));
48 }
49
50 private void asciiToByteAndBackOK(final String inputString) {
51 assertEquals(inputString, ArchiveUtils.toAsciiString(ArchiveUtils.toAsciiBytes(inputString)));
52 }
53
54 @Test
55 void testAsciiConversions() {
56 asciiToByteAndBackOK("");
57 asciiToByteAndBackOK("abcd");
58 asciiToByteAndBackFail("\u8025");
59 }
60
61 @Test
62 void testCompareAscii() {
63 final byte[] buffer1 = { 'a', 'b', 'c' };
64 final byte[] buffer2 = { 'd', 'e', 'f', 0 };
65 assertTrue(ArchiveUtils.matchAsciiBuffer("abc", buffer1));
66 assertFalse(ArchiveUtils.matchAsciiBuffer("abc\0", buffer1));
67 assertTrue(ArchiveUtils.matchAsciiBuffer("def\0", buffer2));
68 assertFalse(ArchiveUtils.matchAsciiBuffer("def", buffer2));
69 }
70
71 @Test
72 void testCompareBA() {
73 final byte[] buffer1 = { 1, 2, 3 };
74 final byte[] buffer2 = { 1, 2, 3, 0 };
75 final byte[] buffer3 = { 1, 2, 3 };
76 assertTrue(ArchiveUtils.isEqual(buffer1, buffer2, true));
77 assertFalse(ArchiveUtils.isEqual(buffer1, buffer2, false));
78 assertFalse(ArchiveUtils.isEqual(buffer1, buffer2));
79 assertTrue(ArchiveUtils.isEqual(buffer2, buffer1, true));
80 assertFalse(ArchiveUtils.isEqual(buffer2, buffer1, false));
81 assertFalse(ArchiveUtils.isEqual(buffer2, buffer1));
82 assertTrue(ArchiveUtils.isEqual(buffer1, buffer3));
83 assertTrue(ArchiveUtils.isEqual(buffer3, buffer1));
84 }
85
86 @Test
87 void testIsEqual() {
88 assertTrue(ArchiveUtils.isEqual(null, 0, 0, null, 0, 0));
89 }
90
91 @Test
92 void testIsEqualWithNullWithPositive() {
93 final byte[] byteArray = new byte[8];
94 byteArray[1] = (byte) -77;
95 assertFalse(ArchiveUtils.isEqualWithNull(byteArray, 0, (byte) 0, byteArray, (byte) 0, (byte) 80));
96 }
97
98 @Test
99 void testSanitizeLeavesShortStringsAlone() {
100 final String input = "012345678901234567890123456789012345678901234567890123456789";
101 assertEquals(input, ArchiveUtils.sanitize(input));
102 }
103
104 @Test
105 void testSanitizeRemovesUnprintableCharacters() {
106 final String input = "\b12345678901234567890123456789012345678901234567890123456789";
107 final String expected = "?12345678901234567890123456789012345678901234567890123456789";
108 assertEquals(expected, ArchiveUtils.sanitize(input));
109 }
110
111 @Test
112 void testSanitizeShortensString() {
113
114 final String input = "012345678901234567890123456789012345678901234567890123456789"
115 + "012345678901234567890123456789012345678901234567890123456789"
116 + "012345678901234567890123456789012345678901234567890123456789"
117 + "012345678901234567890123456789012345678901234567890123456789"
118 + "012345678901234567890123456789012345678901234567890123456789";
119 final String expected = "012345678901234567890123456789012345678901234567890123456789"
120 + "012345678901234567890123456789012345678901234567890123456789"
121 + "012345678901234567890123456789012345678901234567890123456789"
122 + "012345678901234567890123456789012345678901234567890123456789"
123 + "012345678901...";
124
125 assertEquals(expected, ArchiveUtils.sanitize(input));
126 }
127
128 @Test
129 void testToAsciiBytes() {
130 final byte[] byteArray = ArchiveUtils.toAsciiBytes("SOCKET");
131 assertArrayEquals(new byte[] { (byte) 83, (byte) 79, (byte) 67, (byte) 75, (byte) 69, (byte) 84 }, byteArray);
132 assertFalse(ArchiveUtils.isEqualWithNull(byteArray, 0, 46, byteArray, 63, 0));
133 }
134
135 @Test
136 void testToAsciiStringThrowsStringIndexOutOfBoundsException() {
137 final byte[] byteArray = new byte[3];
138 assertThrows(StringIndexOutOfBoundsException.class, () -> ArchiveUtils.toAsciiString(byteArray, 940, 2730));
139 }
140
141 @Test
142 void testToStringWithNonNull() {
143 final SevenZArchiveEntry sevenZArchiveEntry = new SevenZArchiveEntry();
144 final String string = ArchiveUtils.toString(sevenZArchiveEntry);
145 assertEquals("- 0 null", string);
146 }
147 }