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.ASN1Exception;
029: import org.apache.harmony.security.asn1.ASN1SequenceOf;
030: import org.apache.harmony.security.asn1.BerInputStream;
031: import org.apache.harmony.security.asn1.DerInputStream;
032: import org.apache.harmony.security.asn1.DerOutputStream;
033:
034: import junit.framework.TestCase;
035:
036: /**
037: * ASN.1 DER test for SequenceOf type
038: *
039: * @see http://asn1.elibel.tm.fr/en/standards/index.htm
040: */
041:
042: public class SequenceOfTest extends TestCase {
043:
044: public static void main(String[] args) {
045: junit.textui.TestRunner.run(SequenceOfTest.class);
046: }
047:
048: private static ASN1SequenceOf sequenceOf = new ASN1SequenceOf(
049: ASN1Boolean.getInstance());
050:
051: //
052: // Test Cases
053: //
054:
055: private static Object[][] testcases = new Object[][] {
056: // format: object to encode / byte array
057:
058: // sequence : empty sequence
059: new Object[] { new ArrayList(), new byte[] { 0x30, 0x00 } },
060:
061: // sequence : false
062: new Object[] { (new MyArray()).addMy(Boolean.FALSE),
063: new byte[] { 0x30, 0x03, 0x01, 0x01, 0x00 } },
064:
065: // sequence : true
066: new Object[] { (new MyArray()).addMy(Boolean.TRUE),
067: new byte[] { 0x30, 0x03, 0x01, 0x01, (byte) 0xFF } },
068:
069: // sequence : true, false
070: new Object[] {
071: (new MyArray()).addMy(Boolean.TRUE).addMy(
072: Boolean.FALSE), new byte[] { 0x30, 0x06, // sequence of
073: 0x01, 0x01, (byte) 0xFF, // true
074: 0x01, 0x01, 0x00 } //false
075: },
076:
077: //TODO add testcase for another ASN.1 type`
078:
079: };
080:
081: public void testDecode_Valid() throws IOException {
082:
083: for (int i = 0; i < testcases.length; i++) {
084: try {
085: DerInputStream in = new DerInputStream(
086: (byte[]) testcases[i][1]);
087: assertEquals("Test case: " + i, testcases[i][0],
088: sequenceOf.decode(in));
089: } catch (ASN1Exception e) {
090: fail("Test case: " + i + "\n" + e.getMessage());
091: }
092: }
093: }
094:
095: //FIXME need testcase for decoding invalid encodings
096:
097: public void testEncode() throws IOException {
098:
099: for (int i = 0; i < testcases.length; i++) {
100: DerOutputStream out = new DerOutputStream(sequenceOf,
101: testcases[i][0]);
102: assertTrue("Test case: " + i, Arrays.equals(
103: (byte[]) testcases[i][1], out.encoded));
104: }
105: }
106:
107: public void testVerify() throws IOException {
108:
109: ASN1SequenceOf seqVerify = new ASN1SequenceOf(ASN1Boolean
110: .getInstance()) {
111:
112: public Object getDecodedObject(BerInputStream in)
113: throws IOException {
114: throw new IOException(
115: "Method getDecodedObject MUST not be invoked");
116: }
117: };
118:
119: for (int i = 0; i < testcases.length; i++) {
120: DerInputStream in = new DerInputStream(
121: (byte[]) testcases[i][1]);
122: in.setVerify();
123: seqVerify.decode(in);
124: }
125: }
126:
127: //
128: // Support class
129: //
130: public static class MyArray extends ArrayList {
131:
132: public MyArray addMy(Object o) {
133: add(o);
134: return this;
135: }
136: }
137: }
|