SequenceOfTest.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 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.ASN1Exception; 30import org.apache.harmony.security.asn1.ASN1SequenceOf; 31import org.apache.harmony.security.asn1.BerInputStream; 32import org.apache.harmony.security.asn1.DerInputStream; 33import org.apache.harmony.security.asn1.DerOutputStream; 34 35import junit.framework.TestCase; 36 37 38/** 39 * ASN.1 DER test for SequenceOf type 40 * 41 * @see http://asn1.elibel.tm.fr/en/standards/index.htm 42 */ 43 44public class SequenceOfTest extends TestCase { 45 46 public static void main(String[] args) { 47 junit.textui.TestRunner.run(SequenceOfTest.class); 48 } 49 50 private static ASN1SequenceOf sequenceOf = new ASN1SequenceOf(ASN1Boolean 51 .getInstance()); 52 53 // 54 // Test Cases 55 // 56 57 private static Object[][] testcases = new Object[][] { 58 // format: object to encode / byte array 59 60 // sequence : empty sequence 61 new Object[] { new ArrayList(), new byte[] { 0x30, 0x00 } }, 62 63 // sequence : false 64 new Object[] { (new MyArray()).addMy(Boolean.FALSE), 65 new byte[] { 0x30, 0x03, 0x01, 0x01, 0x00 } }, 66 67 // sequence : true 68 new Object[] { (new MyArray()).addMy(Boolean.TRUE), 69 new byte[] { 0x30, 0x03, 0x01, 0x01, (byte) 0xFF } }, 70 71 // sequence : true, false 72 new Object[] { 73 (new MyArray()).addMy(Boolean.TRUE).addMy(Boolean.FALSE), 74 new byte[] { 0x30, 0x06, // sequence of 75 0x01, 0x01, (byte) 0xFF, // true 76 0x01, 0x01, 0x00 } //false 77 }, 78 79 //TODO add testcase for another ASN.1 type` 80 81 }; 82 83 public void testDecode_Valid() throws IOException { 84 85 for (int i = 0; i < testcases.length; i++) { 86 try { 87 DerInputStream in = new DerInputStream((byte[]) testcases[i][1]); 88 assertEquals("Test case: " + i, testcases[i][0], sequenceOf 89 .decode(in)); 90 } catch (ASN1Exception e) { 91 fail("Test case: " + i + "\n" + e.getMessage()); 92 } 93 } 94 } 95 96 //FIXME need testcase for decoding invalid encodings 97 98 public void testEncode() throws IOException { 99 100 for (int i = 0; i < testcases.length; i++) { 101 DerOutputStream out = new DerOutputStream(sequenceOf, 102 testcases[i][0]); 103 assertTrue("Test case: " + i, Arrays.equals( 104 (byte[]) testcases[i][1], out.encoded)); 105 } 106 } 107 108 public void testVerify() throws IOException { 109 110 ASN1SequenceOf seqVerify = new ASN1SequenceOf(ASN1Boolean.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((byte[]) testcases[i][1]); 121 in.setVerify(); 122 seqVerify.decode(in); 123 } 124 } 125 126 // 127 // Support class 128 // 129 public static class MyArray extends ArrayList { 130 131 public MyArray addMy(Object o) { 132 add(o); 133 return this; 134 } 135 } 136}