ExplicitTest.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 Stepan M. Mishura
20*/
21
22package org.apache.harmony.security.tests.asn1.der;
23
24import java.io.IOException;
25import java.util.ArrayList;
26import java.util.Arrays;
27
28import org.apache.harmony.security.asn1.ASN1Boolean;
29import org.apache.harmony.security.asn1.ASN1Constants;
30import org.apache.harmony.security.asn1.ASN1Explicit;
31import org.apache.harmony.security.asn1.ASN1SequenceOf;
32import org.apache.harmony.security.asn1.ASN1Type;
33import org.apache.harmony.security.asn1.DerInputStream;
34import org.apache.harmony.security.asn1.DerOutputStream;
35
36import junit.framework.TestCase;
37
38
39/**
40 * ASN.1 DER test for Explicitly tagged type
41 *
42 * @see http://asn1.elibel.tm.fr/en/standards/index.htm
43 */
44
45public class ExplicitTest extends TestCase {
46
47    private static ASN1SequenceOf sequence = new ASN1SequenceOf(ASN1Boolean
48            .getInstance());
49
50    private static Object[][] taggedType;
51
52    protected void setUp() throws Exception {
53        super.setUp();
54
55        taggedType = new Object[][] {
56                //format: object to encode / ASN.1 tagged type / byte array
57
58                //
59                // Boolean = false
60                //
61
62                // [UNIVERSAL 5] Boolean
63                new Object[] {
64                        Boolean.FALSE,
65                        new byte[] { 0x25, 0x03, 0x01, 0x01, 0x00 },
66                        new ASN1Explicit(ASN1Constants.CLASS_UNIVERSAL, 5,
67                                ASN1Boolean.getInstance()) },
68
69                // [APPLICATION 5] Boolean
70                new Object[] {
71                        Boolean.FALSE,
72                        new byte[] { 0x65, 0x03, 0x01, 0x01, 0x00 },
73                        new ASN1Explicit(ASN1Constants.CLASS_APPLICATION, 5,
74                                ASN1Boolean.getInstance()) },
75
76                // [CONTEXT-SPECIFIC 5] Boolean
77                new Object[] {
78                        Boolean.FALSE,
79                        new byte[] { (byte) 0xA5, 0x03, 0x01, 0x01, 0x00 },
80                        new ASN1Explicit(ASN1Constants.CLASS_CONTEXTSPECIFIC,
81                                5, ASN1Boolean.getInstance()) },
82
83                // [5] Boolean (default = CONTEXT-SPECIFIC)
84                new Object[] { Boolean.FALSE,
85                        new byte[] { (byte) 0xA5, 0x03, 0x01, 0x01, 0x00 },
86                        new ASN1Explicit(5, ASN1Boolean.getInstance()) },
87
88                // [PRIVATE 5] Boolean
89                new Object[] {
90                        Boolean.FALSE,
91                        new byte[] { (byte) 0xE5, 0x03, 0x01, 0x01, 0x00 },
92                        new ASN1Explicit(ASN1Constants.CLASS_PRIVATE, 5,
93                                ASN1Boolean.getInstance()) },
94
95                //
96                // Boolean = true
97                //
98
99                // [UNIVERSAL 5] Boolean
100                new Object[] {
101                        Boolean.TRUE,
102                        new byte[] { 0x25, 0x03, 0x01, 0x01, (byte) 0xFF },
103                        new ASN1Explicit(ASN1Constants.CLASS_UNIVERSAL, 5,
104                                ASN1Boolean.getInstance()) },
105
106                // [APPLICATION 5] Boolean
107                new Object[] {
108                        Boolean.TRUE,
109                        new byte[] { 0x65, 0x03, 0x01, 0x01, (byte) 0xFF },
110                        new ASN1Explicit(ASN1Constants.CLASS_APPLICATION, 5,
111                                ASN1Boolean.getInstance()) },
112
113                // [CONTEXT-SPECIFIC 5] Boolean
114                new Object[] {
115                        Boolean.TRUE,
116                        new byte[] { (byte) 0xA5, 0x03, 0x01, 0x01, (byte) 0xFF },
117                        new ASN1Explicit(ASN1Constants.CLASS_CONTEXTSPECIFIC,
118                                5, ASN1Boolean.getInstance()) },
119
120                // [5] Boolean (default = CONTEXT-SPECIFIC)
121                new Object[] {
122                        Boolean.TRUE,
123                        new byte[] { (byte) 0xA5, 0x03, 0x01, 0x01, (byte) 0xFF },
124                        new ASN1Explicit(ASN1Constants.CLASS_CONTEXTSPECIFIC,
125                                5, ASN1Boolean.getInstance()) },
126
127                // [PRIVATE 5] Boolean
128                new Object[] {
129                        Boolean.TRUE,
130                        new byte[] { (byte) 0xE5, 0x03, 0x01, 0x01, (byte) 0xFF },
131                        new ASN1Explicit(ASN1Constants.CLASS_PRIVATE, 5,
132                                ASN1Boolean.getInstance()) },
133                //
134                // SequenceOf - testing constructed ASN.1 type
135                //
136
137                // [UNIVERSAL 5] SequenceOf
138                new Object[] {
139                        new ArrayList(),
140                        new byte[] { 0x25, 0x02, 0x30, 0x00 },
141                        new ASN1Explicit(ASN1Constants.CLASS_UNIVERSAL, 5,
142                                sequence) },
143
144                // [APPLICATION 5] SequenceOf
145                new Object[] {
146                        new ArrayList(),
147                        new byte[] { 0x65, 0x02, 0x30, 0x00 },
148                        new ASN1Explicit(ASN1Constants.CLASS_APPLICATION, 5,
149                                sequence) },
150
151                // [CONTEXT-SPECIFIC 5] SequenceOf
152                new Object[] {
153                        new ArrayList(),
154                        new byte[] { (byte) 0xA5, 0x02, 0x30, 0x00 },
155                        new ASN1Explicit(ASN1Constants.CLASS_CONTEXTSPECIFIC,
156                                5, sequence) },
157
158                // [5] SequenceOf (default = CONTEXT-SPECIFIC)
159                new Object[] {
160                        new ArrayList(),
161                        new byte[] { (byte) 0xA5, 0x02, 0x30, 0x00 },
162                        new ASN1Explicit(ASN1Constants.CLASS_CONTEXTSPECIFIC,
163                                5, sequence) },
164
165                // [PRIVATE 5] SequenceOf
166                new Object[] {
167                        new ArrayList(),
168                        new byte[] { (byte) 0xE5, 0x02, 0x30, 0x00 },
169                        new ASN1Explicit(ASN1Constants.CLASS_PRIVATE, 5,
170                                sequence) } };
171    }
172
173    public void testDecode_Valid() throws IOException {
174
175        for (int i = 0; i < taggedType.length; i++) {
176            DerInputStream in = new DerInputStream((byte[]) taggedType[i][1]);
177            assertEquals("Test case: " + i, taggedType[i][0],
178                    ((ASN1Type) taggedType[i][2]).decode(in));
179        }
180    }
181
182    //FIXME need testcase for decoding invalid encodings
183
184    public void testEncode() throws IOException {
185
186        for (int i = 0; i < taggedType.length; i++) {
187            DerOutputStream out = new DerOutputStream(
188                    (ASN1Type) taggedType[i][2], taggedType[i][0]);
189            assertTrue("Test case: " + i, Arrays.equals(
190                    (byte[]) taggedType[i][1], out.encoded));
191        }
192    }
193}
194