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