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