BooleanTest.java revision e98fbf8686c5289bf03fe5c3de7ff82d3a77104d
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* @author Vladimir N. Molotkov 20*/ 21 22package org.apache.harmony.security.tests.asn1.der; 23 24import java.io.IOException; 25import java.util.Arrays; 26 27import junit.framework.TestCase; 28 29import org.apache.harmony.security.asn1.ASN1Boolean; 30import org.apache.harmony.security.asn1.ASN1Exception; 31import org.apache.harmony.security.asn1.DerInputStream; 32import org.apache.harmony.security.asn1.DerOutputStream; 33 34 35/** 36 * ASN.1 DER test for Boolean type 37 * 38 * @see http://asn1.elibel.tm.fr/en/standards/index.htm 39 */ 40public class BooleanTest extends TestCase { 41 42 private static byte[] eFalse = new byte[] { 0x01, 0x01, 0x00 }; 43 44 private static byte[] eTrue = new byte[] { 0x01, 0x01, (byte) 0xFF }; 45 46 public void test_Decode_Encode() throws IOException { 47 48 // oid decoder/encoder for testing 49 ASN1Boolean asn1 = ASN1Boolean.getInstance(); 50 51 // decoding false 52 DerInputStream in = new DerInputStream(eFalse); 53 assertEquals("Decoding false value", Boolean.FALSE, asn1.decode(in)); 54 55 // decoding true 56 in = new DerInputStream(eTrue); 57 assertEquals("Decoding true value", Boolean.TRUE, asn1.decode(in)); 58 59 // encoding false 60 DerOutputStream out = new DerOutputStream(asn1, Boolean.FALSE); 61 assertTrue("Encoding false value", Arrays.equals(eFalse, out.encoded)); 62 63 // encoding true 64 out = new DerOutputStream(asn1, Boolean.TRUE); 65 assertTrue("Encoding true value", Arrays.equals(eTrue, out.encoded)); 66 } 67 68 public void testDecode_Invalid() throws IOException { 69 70 byte[][] invalid = new byte[][] { 71 // wrong tag: tag is not 0x01 72 new byte[] { 0x02, 0x01, 0x00 }, 73 // wrong length: length is not 1 74 new byte[] { 0x01, 0x02, 0x00 }, 75 // wrong content: content is not 0x01 or 0xFF 76 new byte[] { 0x01, 0x01, 0x33 } }; 77 78 for (int i = 0; i < invalid.length; i++) { 79 try { 80 DerInputStream in = new DerInputStream(invalid[i]); 81 ASN1Boolean.getInstance().decode(in); 82 fail("No expected ASN1Exception for: " + i); 83 } catch (ASN1Exception e) { 84 } 85 } 86 } 87} 88