01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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: * @version $Revision$
21: */package org.apache.harmony.security.tests.asn1.der;
22:
23: import java.io.IOException;
24: import java.util.Arrays;
25:
26: import junit.framework.TestCase;
27:
28: import org.apache.harmony.security.asn1.ASN1Boolean;
29: import org.apache.harmony.security.asn1.ASN1Exception;
30: import org.apache.harmony.security.asn1.DerInputStream;
31: import org.apache.harmony.security.asn1.DerOutputStream;
32:
33: /**
34: * ASN.1 DER test for Boolean type
35: *
36: * @see http://asn1.elibel.tm.fr/en/standards/index.htm
37: */
38: public class BooleanTest extends TestCase {
39:
40: public static void main(String[] args) {
41: junit.textui.TestRunner.run(BooleanTest.class);
42: }
43:
44: private static byte[] eFalse = new byte[] { 0x01, 0x01, 0x00 };
45:
46: private static byte[] eTrue = new byte[] { 0x01, 0x01, (byte) 0xFF };
47:
48: public void test_Decode_Encode() throws IOException {
49:
50: // oid decoder/encoder for testing
51: ASN1Boolean asn1 = ASN1Boolean.getInstance();
52:
53: // decoding false
54: DerInputStream in = new DerInputStream(eFalse);
55: assertEquals("Decoding false value", Boolean.FALSE, asn1
56: .decode(in));
57:
58: // decoding true
59: in = new DerInputStream(eTrue);
60: assertEquals("Decoding true value", Boolean.TRUE, asn1
61: .decode(in));
62:
63: // encoding false
64: DerOutputStream out = new DerOutputStream(asn1, Boolean.FALSE);
65: assertTrue("Encoding false value", Arrays.equals(eFalse,
66: out.encoded));
67:
68: // encoding true
69: out = new DerOutputStream(asn1, Boolean.TRUE);
70: assertTrue("Encoding true value", Arrays.equals(eTrue,
71: out.encoded));
72: }
73:
74: public void testDecode_Invalid() throws IOException {
75:
76: byte[][] invalid = new byte[][] {
77: // wrong tag: tag is not 0x01
78: new byte[] { 0x02, 0x01, 0x00 },
79: // wrong length: length is not 1
80: new byte[] { 0x01, 0x02, 0x00 },
81: // wrong content: content is not 0x01 or 0xFF
82: new byte[] { 0x01, 0x01, 0x33 } };
83:
84: for (int i = 0; i < invalid.length; i++) {
85: try {
86: DerInputStream in = new DerInputStream(invalid[i]);
87: ASN1Boolean.getInstance().decode(in);
88: fail("No expected ASN1Exception for: " + i);
89: } catch (ASN1Exception e) {
90: }
91: }
92: }
93: }
|