View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *   http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   * 
17   */
18  
19  package org.apache.commons.compress;
20  
21  import org.apache.commons.compress.archivers.sevenz.SevenZArchiveEntry;
22  import org.apache.commons.compress.utils.ArchiveUtils;
23  import org.junit.Test;
24  
25  import static org.junit.Assert.*;
26  
27  public class ArchiveUtilsTest extends AbstractTestCase {
28  
29      private static final int bytesToTest = 50;
30      private static final byte[] byteTest = new byte[bytesToTest];
31      static {
32          for(int i=0; i < byteTest.length ;) {
33              byteTest[i]=(byte) i;
34              byteTest[i+1]=(byte) -i;
35              i += 2;
36          }
37      }
38  
39      @Test
40      public void testCompareBA(){
41          final byte[] buffer1 = {1,2,3};
42          final byte[] buffer2 = {1,2,3,0};
43          final byte[] buffer3 = {1,2,3};
44          assertTrue(ArchiveUtils.isEqual(buffer1, buffer2, true));
45          assertFalse(ArchiveUtils.isEqual(buffer1, buffer2, false));
46          assertFalse(ArchiveUtils.isEqual(buffer1, buffer2));
47          assertTrue(ArchiveUtils.isEqual(buffer2, buffer1, true));
48          assertFalse(ArchiveUtils.isEqual(buffer2, buffer1, false));
49          assertFalse(ArchiveUtils.isEqual(buffer2, buffer1));
50          assertTrue(ArchiveUtils.isEqual(buffer1, buffer3));
51          assertTrue(ArchiveUtils.isEqual(buffer3, buffer1));
52      }
53  
54      @Test
55      public void testCompareAscii(){
56          final byte[] buffer1 = {'a','b','c'};
57          final byte[] buffer2 = {'d','e','f',0};
58          assertTrue(ArchiveUtils.matchAsciiBuffer("abc", buffer1));
59          assertFalse(ArchiveUtils.matchAsciiBuffer("abc\0", buffer1));
60          assertTrue(ArchiveUtils.matchAsciiBuffer("def\0", buffer2));
61          assertFalse(ArchiveUtils.matchAsciiBuffer("def", buffer2));
62      }
63  
64      @Test
65      public void testAsciiConversions() {
66          asciiToByteAndBackOK("");
67          asciiToByteAndBackOK("abcd");
68          asciiToByteAndBackFail("\u8025");
69      }
70  
71      @Test
72      public void sanitizeShortensString() {
73          final String input = "012345678901234567890123456789012345678901234567890123456789"
74              + "012345678901234567890123456789012345678901234567890123456789"
75              + "012345678901234567890123456789012345678901234567890123456789"
76              + "012345678901234567890123456789012345678901234567890123456789"
77              + "012345678901234567890123456789012345678901234567890123456789";
78          final String expected = "012345678901234567890123456789012345678901234567890123456789"
79              + "012345678901234567890123456789012345678901234567890123456789"
80              + "012345678901234567890123456789012345678901234567890123456789"
81              + "012345678901234567890123456789012345678901234567890123456789"
82              + "012345678901...";
83          assertEquals(expected, ArchiveUtils.sanitize(input));
84      }
85  
86      @Test
87      public void sanitizeLeavesShortStringsAlone() {
88          final String input = "012345678901234567890123456789012345678901234567890123456789";
89          assertEquals(input, ArchiveUtils.sanitize(input));
90      }
91  
92      @Test
93      public void sanitizeRemovesUnprintableCharacters() {
94          final String input = "\b12345678901234567890123456789012345678901234567890123456789";
95          final String expected = "?12345678901234567890123456789012345678901234567890123456789";
96          assertEquals(expected, ArchiveUtils.sanitize(input));
97      }
98  
99      @Test
100     public void testIsEqualWithNullWithPositive() {
101 
102         byte[] byteArray = new byte[8];
103         byteArray[1] = (byte) (-77);
104 
105         assertFalse(ArchiveUtils.isEqualWithNull(byteArray, 0, (byte)0, byteArray, (byte)0, (byte)80));
106 
107     }
108 
109     @Test
110     public void testToAsciiBytes() {
111 
112         byte[] byteArray = ArchiveUtils.toAsciiBytes("SOCKET");
113 
114         assertArrayEquals(new byte[] {(byte)83, (byte)79, (byte)67, (byte)75, (byte)69, (byte)84}, byteArray);
115 
116         assertFalse(ArchiveUtils.isEqualWithNull(byteArray, 0, 46, byteArray, 63, 0));
117 
118     }
119 
120     @Test
121     public void testToStringWithNonNull() {
122 
123         SevenZArchiveEntry sevenZArchiveEntry = new SevenZArchiveEntry();
124         String string = ArchiveUtils.toString(sevenZArchiveEntry);
125 
126         assertEquals("-       0 null", string);
127 
128     }
129 
130     @Test
131     public void testIsEqual() {
132 
133         assertTrue(ArchiveUtils.isEqual((byte[]) null, 0, 0, (byte[]) null, 0, 0));
134 
135     }
136 
137     @Test(expected = StringIndexOutOfBoundsException.class)
138     public void testToAsciiStringThrowsStringIndexOutOfBoundsException() {
139 
140         byte[] byteArray = new byte[3];
141 
142         ArchiveUtils.toAsciiString(byteArray, 940, 2730);
143 
144     }
145 
146     private void asciiToByteAndBackOK(final String inputString) {
147         assertEquals(inputString, ArchiveUtils.toAsciiString(ArchiveUtils.toAsciiBytes(inputString)));
148     }
149 
150     private void asciiToByteAndBackFail(final String inputString) {
151         assertFalse(inputString.equals(ArchiveUtils.toAsciiString(ArchiveUtils.toAsciiBytes(inputString))));
152     }
153 }