001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /**
019: * @author Stepan M. Mishura
020: * @version $Revision$
021: */package org.apache.harmony.security.tests.asn1.der;
022:
023: import java.io.IOException;
024: import java.util.ArrayList;
025: import java.util.Arrays;
026:
027: import org.apache.harmony.security.asn1.ASN1Boolean;
028: import org.apache.harmony.security.asn1.ASN1Constants;
029: import org.apache.harmony.security.asn1.ASN1Explicit;
030: import org.apache.harmony.security.asn1.ASN1SequenceOf;
031: import org.apache.harmony.security.asn1.ASN1Type;
032: import org.apache.harmony.security.asn1.DerInputStream;
033: import org.apache.harmony.security.asn1.DerOutputStream;
034:
035: import junit.framework.TestCase;
036:
037: /**
038: * ASN.1 DER test for Explicitly tagged type
039: *
040: * @see http://asn1.elibel.tm.fr/en/standards/index.htm
041: */
042:
043: public class ExplicitTest extends TestCase {
044:
045: public static void main(String[] args) {
046: junit.textui.TestRunner.run(ExplicitTest.class);
047: }
048:
049: private static ASN1SequenceOf sequence = new ASN1SequenceOf(
050: ASN1Boolean.getInstance());
051:
052: private static Object[][] taggedType;
053:
054: protected void setUp() throws Exception {
055: super .setUp();
056:
057: taggedType = new Object[][] {
058: //format: object to encode / ASN.1 tagged type / byte array
059:
060: //
061: // Boolean = false
062: //
063:
064: // [UNIVERSAL 5] Boolean
065: new Object[] {
066: Boolean.FALSE,
067: new byte[] { 0x25, 0x03, 0x01, 0x01, 0x00 },
068: new ASN1Explicit(ASN1Constants.CLASS_UNIVERSAL,
069: 5, ASN1Boolean.getInstance()) },
070:
071: // [APPLICATION 5] Boolean
072: new Object[] {
073: Boolean.FALSE,
074: new byte[] { 0x65, 0x03, 0x01, 0x01, 0x00 },
075: new ASN1Explicit(
076: ASN1Constants.CLASS_APPLICATION, 5,
077: ASN1Boolean.getInstance()) },
078:
079: // [CONTEXT-SPECIFIC 5] Boolean
080: new Object[] {
081: Boolean.FALSE,
082: new byte[] { (byte) 0xA5, 0x03, 0x01, 0x01,
083: 0x00 },
084: new ASN1Explicit(
085: ASN1Constants.CLASS_CONTEXTSPECIFIC, 5,
086: ASN1Boolean.getInstance()) },
087:
088: // [5] Boolean (default = CONTEXT-SPECIFIC)
089: new Object[] {
090: Boolean.FALSE,
091: new byte[] { (byte) 0xA5, 0x03, 0x01, 0x01,
092: 0x00 },
093: new ASN1Explicit(5, ASN1Boolean.getInstance()) },
094:
095: // [PRIVATE 5] Boolean
096: new Object[] {
097: Boolean.FALSE,
098: new byte[] { (byte) 0xE5, 0x03, 0x01, 0x01,
099: 0x00 },
100: new ASN1Explicit(ASN1Constants.CLASS_PRIVATE,
101: 5, ASN1Boolean.getInstance()) },
102:
103: //
104: // Boolean = true
105: //
106:
107: // [UNIVERSAL 5] Boolean
108: new Object[] {
109: Boolean.TRUE,
110: new byte[] { 0x25, 0x03, 0x01, 0x01,
111: (byte) 0xFF },
112: new ASN1Explicit(ASN1Constants.CLASS_UNIVERSAL,
113: 5, ASN1Boolean.getInstance()) },
114:
115: // [APPLICATION 5] Boolean
116: new Object[] {
117: Boolean.TRUE,
118: new byte[] { 0x65, 0x03, 0x01, 0x01,
119: (byte) 0xFF },
120: new ASN1Explicit(
121: ASN1Constants.CLASS_APPLICATION, 5,
122: ASN1Boolean.getInstance()) },
123:
124: // [CONTEXT-SPECIFIC 5] Boolean
125: new Object[] {
126: Boolean.TRUE,
127: new byte[] { (byte) 0xA5, 0x03, 0x01, 0x01,
128: (byte) 0xFF },
129: new ASN1Explicit(
130: ASN1Constants.CLASS_CONTEXTSPECIFIC, 5,
131: ASN1Boolean.getInstance()) },
132:
133: // [5] Boolean (default = CONTEXT-SPECIFIC)
134: new Object[] {
135: Boolean.TRUE,
136: new byte[] { (byte) 0xA5, 0x03, 0x01, 0x01,
137: (byte) 0xFF },
138: new ASN1Explicit(
139: ASN1Constants.CLASS_CONTEXTSPECIFIC, 5,
140: ASN1Boolean.getInstance()) },
141:
142: // [PRIVATE 5] Boolean
143: new Object[] {
144: Boolean.TRUE,
145: new byte[] { (byte) 0xE5, 0x03, 0x01, 0x01,
146: (byte) 0xFF },
147: new ASN1Explicit(ASN1Constants.CLASS_PRIVATE,
148: 5, ASN1Boolean.getInstance()) },
149: //
150: // SequenceOf - testing constructed ASN.1 type
151: //
152:
153: // [UNIVERSAL 5] SequenceOf
154: new Object[] {
155: new ArrayList(),
156: new byte[] { 0x25, 0x02, 0x30, 0x00 },
157: new ASN1Explicit(ASN1Constants.CLASS_UNIVERSAL,
158: 5, sequence) },
159:
160: // [APPLICATION 5] SequenceOf
161: new Object[] {
162: new ArrayList(),
163: new byte[] { 0x65, 0x02, 0x30, 0x00 },
164: new ASN1Explicit(
165: ASN1Constants.CLASS_APPLICATION, 5,
166: sequence) },
167:
168: // [CONTEXT-SPECIFIC 5] SequenceOf
169: new Object[] {
170: new ArrayList(),
171: new byte[] { (byte) 0xA5, 0x02, 0x30, 0x00 },
172: new ASN1Explicit(
173: ASN1Constants.CLASS_CONTEXTSPECIFIC, 5,
174: sequence) },
175:
176: // [5] SequenceOf (default = CONTEXT-SPECIFIC)
177: new Object[] {
178: new ArrayList(),
179: new byte[] { (byte) 0xA5, 0x02, 0x30, 0x00 },
180: new ASN1Explicit(
181: ASN1Constants.CLASS_CONTEXTSPECIFIC, 5,
182: sequence) },
183:
184: // [PRIVATE 5] SequenceOf
185: new Object[] {
186: new ArrayList(),
187: new byte[] { (byte) 0xE5, 0x02, 0x30, 0x00 },
188: new ASN1Explicit(ASN1Constants.CLASS_PRIVATE,
189: 5, sequence) } };
190: }
191:
192: public void testDecode_Valid() throws IOException {
193:
194: for (int i = 0; i < taggedType.length; i++) {
195: DerInputStream in = new DerInputStream(
196: (byte[]) taggedType[i][1]);
197: assertEquals("Test case: " + i, taggedType[i][0],
198: ((ASN1Type) taggedType[i][2]).decode(in));
199: }
200: }
201:
202: //FIXME need testcase for decoding invalid encodings
203:
204: public void testEncode() throws IOException {
205:
206: for (int i = 0; i < taggedType.length; i++) {
207: DerOutputStream out = new DerOutputStream(
208: (ASN1Type) taggedType[i][2], taggedType[i][0]);
209: assertTrue("Test case: " + i, Arrays.equals(
210: (byte[]) taggedType[i][1], out.encoded));
211: }
212: }
213: }
|