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 static org.junit.Assert.*;
22  
23  import org.apache.commons.compress.utils.ArchiveUtils;
24  import org.junit.Test;
25  
26  public class ArchiveUtilsTest extends AbstractTestCase {
27  
28      private static final int bytesToTest = 50;
29      private static final byte[] byteTest = new byte[bytesToTest];
30      static {
31          for(int i=0; i < byteTest.length ;) {
32              byteTest[i]=(byte) i;
33              byteTest[i+1]=(byte) -i;
34              i += 2;
35          }
36      }
37  
38      @Test
39      public void testCompareBA(){
40          final byte[] buffer1 = {1,2,3};
41          final byte[] buffer2 = {1,2,3,0};
42          final byte[] buffer3 = {1,2,3};
43          assertTrue(ArchiveUtils.isEqual(buffer1, buffer2, true));
44          assertFalse(ArchiveUtils.isEqual(buffer1, buffer2, false));
45          assertFalse(ArchiveUtils.isEqual(buffer1, buffer2));
46          assertTrue(ArchiveUtils.isEqual(buffer2, buffer1, true));
47          assertFalse(ArchiveUtils.isEqual(buffer2, buffer1, false));
48          assertFalse(ArchiveUtils.isEqual(buffer2, buffer1));
49          assertTrue(ArchiveUtils.isEqual(buffer1, buffer3));
50          assertTrue(ArchiveUtils.isEqual(buffer3, buffer1));
51      }
52  
53      @Test
54      public void testCompareAscii(){
55          final byte[] buffer1 = {'a','b','c'};
56          final byte[] buffer2 = {'d','e','f',0};
57          assertTrue(ArchiveUtils.matchAsciiBuffer("abc", buffer1));
58          assertFalse(ArchiveUtils.matchAsciiBuffer("abc\0", buffer1));
59          assertTrue(ArchiveUtils.matchAsciiBuffer("def\0", buffer2));
60          assertFalse(ArchiveUtils.matchAsciiBuffer("def", buffer2));
61      }
62  
63      @Test
64      public void testAsciiConversions() {
65          asciiToByteAndBackOK("");
66          asciiToByteAndBackOK("abcd");
67          asciiToByteAndBackFail("\u8025");
68      }
69  
70      @Test
71      public void sanitizeShortensString() {
72          String input = "012345678901234567890123456789012345678901234567890123456789"
73              + "012345678901234567890123456789012345678901234567890123456789"
74              + "012345678901234567890123456789012345678901234567890123456789"
75              + "012345678901234567890123456789012345678901234567890123456789"
76              + "012345678901234567890123456789012345678901234567890123456789";
77          String expected = "012345678901234567890123456789012345678901234567890123456789"
78              + "012345678901234567890123456789012345678901234567890123456789"
79              + "012345678901234567890123456789012345678901234567890123456789"
80              + "012345678901234567890123456789012345678901234567890123456789"
81              + "012345678901...";
82          assertEquals(expected, ArchiveUtils.sanitize(input));
83      }
84  
85      @Test
86      public void sanitizeLeavesShortStringsAlone() {
87          String input = "012345678901234567890123456789012345678901234567890123456789";
88          assertEquals(input, ArchiveUtils.sanitize(input));
89      }
90  
91      @Test
92      public void sanitizeRemovesUnprintableCharacters() {
93          String input = "\b12345678901234567890123456789012345678901234567890123456789";
94          String expected = "?12345678901234567890123456789012345678901234567890123456789";
95          assertEquals(expected, ArchiveUtils.sanitize(input));
96      }
97  
98      private void asciiToByteAndBackOK(final String inputString) {
99          assertEquals(inputString, ArchiveUtils.toAsciiString(ArchiveUtils.toAsciiBytes(inputString)));
100     }
101 
102     private void asciiToByteAndBackFail(final String inputString) {
103         assertFalse(inputString.equals(ArchiveUtils.toAsciiString(ArchiveUtils.toAsciiBytes(inputString))));
104     }
105 }