BooleanTest.java revision 561ee011997c6c2f1befbfaa9d5f0a99771c1d63
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 public static void main(String[] args) { 43 junit.textui.TestRunner.run(BooleanTest.class); 44 } 45 46 private static byte[] eFalse = new byte[] { 0x01, 0x01, 0x00 }; 47 48 private static byte[] eTrue = new byte[] { 0x01, 0x01, (byte) 0xFF }; 49 50 public void test_Decode_Encode() throws IOException { 51 52 // oid decoder/encoder for testing 53 ASN1Boolean asn1 = ASN1Boolean.getInstance(); 54 55 // decoding false 56 DerInputStream in = new DerInputStream(eFalse); 57 assertEquals("Decoding false value", Boolean.FALSE, asn1.decode(in)); 58 59 // decoding true 60 in = new DerInputStream(eTrue); 61 assertEquals("Decoding true value", Boolean.TRUE, asn1.decode(in)); 62 63 // encoding false 64 DerOutputStream out = new DerOutputStream(asn1, Boolean.FALSE); 65 assertTrue("Encoding false value", Arrays.equals(eFalse, out.encoded)); 66 67 // encoding true 68 out = new DerOutputStream(asn1, Boolean.TRUE); 69 assertTrue("Encoding true value", Arrays.equals(eTrue, out.encoded)); 70 } 71 72 public void testDecode_Invalid() throws IOException { 73 74 byte[][] invalid = new byte[][] { 75 // wrong tag: tag is not 0x01 76 new byte[] { 0x02, 0x01, 0x00 }, 77 // wrong length: length is not 1 78 new byte[] { 0x01, 0x02, 0x00 }, 79 // wrong content: content is not 0x01 or 0xFF 80 new byte[] { 0x01, 0x01, 0x33 } }; 81 82 for (int i = 0; i < invalid.length; i++) { 83 try { 84 DerInputStream in = new DerInputStream(invalid[i]); 85 ASN1Boolean.getInstance().decode(in); 86 fail("No expected ASN1Exception for: " + i); 87 } catch (ASN1Exception e) { 88 } 89 } 90 } 91} 92