View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.commons.compress.archivers.zip;
21  
22  import java.util.Arrays;
23  
24  import junit.framework.TestCase;
25  
26  public class GeneralPurposeBitTest extends TestCase {
27  
28      public void testDefaults() {
29          assertFalse(new GeneralPurposeBit().usesDataDescriptor());
30          assertFalse(new GeneralPurposeBit().usesUTF8ForNames());
31          assertFalse(new GeneralPurposeBit().usesEncryption());
32          assertFalse(new GeneralPurposeBit().usesStrongEncryption());
33          byte[] b = new byte[2];
34          assertTrue(Arrays.equals(b, new GeneralPurposeBit().encode()));
35      }
36  
37      public void testParseEdgeCases() {
38          assertFalse(GeneralPurposeBit.parse(new byte[2], 0)
39                      .usesDataDescriptor());
40          assertFalse(GeneralPurposeBit.parse(new byte[2], 0)
41                      .usesUTF8ForNames());
42          assertFalse(GeneralPurposeBit.parse(new byte[2], 0)
43                      .usesEncryption());
44          assertFalse(GeneralPurposeBit.parse(new byte[2], 0)
45                      .usesStrongEncryption());
46          assertTrue(GeneralPurposeBit.parse(new byte[] {(byte) 255, (byte) 255},
47                                             0)
48                     .usesDataDescriptor());
49          assertTrue(GeneralPurposeBit.parse(new byte[] {(byte) 255, (byte) 255},
50                                             0)
51                     .usesUTF8ForNames());
52          assertTrue(GeneralPurposeBit.parse(new byte[] {(byte) 255, (byte) 255},
53                                             0)
54                     .usesEncryption());
55          assertTrue(GeneralPurposeBit.parse(new byte[] {(byte) 255, (byte) 255},
56                                             0)
57                     .usesStrongEncryption());
58      }
59  
60      public void testDataDescriptor() {
61          byte[] flags = new byte[] {(byte) 8, (byte) 0};
62          assertTrue(GeneralPurposeBit.parse(flags, 0).usesDataDescriptor());
63          GeneralPurposeBit b = new GeneralPurposeBit();
64          b.useDataDescriptor(true);
65          assertTrue(Arrays.equals(flags, b.encode()));
66      }
67  
68      public void testLanguageEncodingFlag() {
69          byte[] flags = new byte[] {(byte) 0, (byte) 8};
70          assertTrue(GeneralPurposeBit.parse(flags, 0).usesUTF8ForNames());
71          GeneralPurposeBit b = new GeneralPurposeBit();
72          b.useUTF8ForNames(true);
73          assertTrue(Arrays.equals(flags, b.encode()));
74      }
75  
76      public void testEncryption() {
77          byte[] flags = new byte[] {(byte) 1, (byte) 0};
78          assertTrue(GeneralPurposeBit.parse(flags, 0).usesEncryption());
79          GeneralPurposeBit b = new GeneralPurposeBit();
80          b.useEncryption(true);
81          assertTrue(Arrays.equals(flags, b.encode()));
82      }
83  
84      public void testStringEncryption() {
85          byte[] flags = new byte[] {(byte) 65, (byte) 0};
86          assertTrue(GeneralPurposeBit.parse(flags, 0).usesStrongEncryption());
87          GeneralPurposeBit b = new GeneralPurposeBit();
88          b.useStrongEncryption(true);
89          assertTrue(b.usesEncryption());
90          assertTrue(Arrays.equals(flags, b.encode()));
91  
92          flags = new byte[] {(byte) 64, (byte) 0};
93          assertFalse(GeneralPurposeBit.parse(flags, 0).usesStrongEncryption());
94      }
95  
96  }