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   *   https://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 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.assertNotSame;
26  import static org.junit.jupiter.api.Assertions.assertTrue;
27  
28  import org.junit.jupiter.api.Test;
29  
30  class GeneralPurposeBitTest {
31  
32      @Test
33      void testClone() {
34          final GeneralPurposeBit b = new GeneralPurposeBit();
35          b.useStrongEncryption(true);
36          b.useUTF8ForNames(true);
37          assertEquals(b, b.clone());
38          assertNotSame(b, b.clone());
39      }
40  
41      @Test
42      void testDataDescriptor() {
43          final byte[] flags = { (byte) 8, (byte) 0 };
44          assertTrue(GeneralPurposeBit.parse(flags, 0).usesDataDescriptor());
45          final GeneralPurposeBit b = new GeneralPurposeBit();
46          b.useDataDescriptor(true);
47          assertArrayEquals(flags, b.encode());
48      }
49  
50      @Test
51      void testDefaults() {
52          assertFalse(new GeneralPurposeBit().usesDataDescriptor());
53          assertFalse(new GeneralPurposeBit().usesUTF8ForNames());
54          assertFalse(new GeneralPurposeBit().usesEncryption());
55          assertFalse(new GeneralPurposeBit().usesStrongEncryption());
56          final byte[] b = new byte[2];
57          assertArrayEquals(b, new GeneralPurposeBit().encode());
58      }
59  
60      @Test
61      void testEncryption() {
62          final byte[] flags = { (byte) 1, (byte) 0 };
63          assertTrue(GeneralPurposeBit.parse(flags, 0).usesEncryption());
64          final GeneralPurposeBit b = new GeneralPurposeBit();
65          b.useEncryption(true);
66          assertArrayEquals(flags, b.encode());
67      }
68  
69      @Test
70      void testLanguageEncodingFlag() {
71          final byte[] flags = { (byte) 0, (byte) 8 };
72          assertTrue(GeneralPurposeBit.parse(flags, 0).usesUTF8ForNames());
73          final GeneralPurposeBit b = new GeneralPurposeBit();
74          b.useUTF8ForNames(true);
75          assertArrayEquals(flags, b.encode());
76      }
77  
78      @Test
79      void testParseEdgeCases() {
80          assertFalse(GeneralPurposeBit.parse(new byte[2], 0).usesDataDescriptor());
81          assertFalse(GeneralPurposeBit.parse(new byte[2], 0).usesUTF8ForNames());
82          assertFalse(GeneralPurposeBit.parse(new byte[2], 0).usesEncryption());
83          assertFalse(GeneralPurposeBit.parse(new byte[2], 0).usesStrongEncryption());
84          assertTrue(GeneralPurposeBit.parse(new byte[] { (byte) 255, (byte) 255 }, 0).usesDataDescriptor());
85          assertTrue(GeneralPurposeBit.parse(new byte[] { (byte) 255, (byte) 255 }, 0).usesUTF8ForNames());
86          assertTrue(GeneralPurposeBit.parse(new byte[] { (byte) 255, (byte) 255 }, 0).usesEncryption());
87          assertTrue(GeneralPurposeBit.parse(new byte[] { (byte) 255, (byte) 255 }, 0).usesStrongEncryption());
88      }
89  
90      @Test
91      void testStrongEncryption() {
92          byte[] flags = { (byte) 65, (byte) 0 };
93          assertTrue(GeneralPurposeBit.parse(flags, 0).usesStrongEncryption());
94          final GeneralPurposeBit b = new GeneralPurposeBit();
95          b.useStrongEncryption(true);
96          assertTrue(b.usesEncryption());
97          assertArrayEquals(flags, b.encode());
98  
99          flags = new byte[] { (byte) 64, (byte) 0 };
100         assertFalse(GeneralPurposeBit.parse(flags, 0).usesStrongEncryption());
101     }
102 }