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 private static ASN1SequenceOf sequenceOf = new ASN1SequenceOf(ASN1Boolean 47 .getInstance()); 48 49 // 50 // Test Cases 51 // 52 53 private static Object[][] testcases = new Object[][] { 54 // format: object to encode / byte array 55 56 // sequence : empty sequence 57 new Object[] { new ArrayList(), new byte[] { 0x30, 0x00 } }, 58 59 // sequence : false 60 new Object[] { (new MyArray()).addMy(Boolean.FALSE), 61 new byte[] { 0x30, 0x03, 0x01, 0x01, 0x00 } }, 62 63 // sequence : true 64 new Object[] { (new MyArray()).addMy(Boolean.TRUE), 65 new byte[] { 0x30, 0x03, 0x01, 0x01, (byte) 0xFF } }, 66 67 // sequence : true, false 68 new Object[] { 69 (new MyArray()).addMy(Boolean.TRUE).addMy(Boolean.FALSE), 70 new byte[] { 0x30, 0x06, // sequence of 71 0x01, 0x01, (byte) 0xFF, // true 72 0x01, 0x01, 0x00 } //false 73 }, 74 75 //TODO add testcase for another ASN.1 type` 76 77 }; 78 79 public void testDecode_Valid() throws IOException { 80 81 for (int i = 0; i < testcases.length; i++) { 82 try { 83 DerInputStream in = new DerInputStream((byte[]) testcases[i][1]); 84 assertEquals("Test case: " + i, testcases[i][0], sequenceOf 85 .decode(in)); 86 } catch (ASN1Exception e) { 87 fail("Test case: " + i + "\n" + e.getMessage()); 88 } 89 } 90 } 91 92 //FIXME need testcase for decoding invalid encodings 93 94 public void testEncode() throws IOException { 95 96 for (int i = 0; i < testcases.length; i++) { 97 DerOutputStream out = new DerOutputStream(sequenceOf, 98 testcases[i][0]); 99 assertTrue("Test case: " + i, Arrays.equals( 100 (byte[]) testcases[i][1], out.encoded)); 101 } 102 } 103 104 public void testVerify() throws IOException { 105 106 ASN1SequenceOf seqVerify = new ASN1SequenceOf(ASN1Boolean.getInstance()) { 107 108 public Object getDecodedObject(BerInputStream in) 109 throws IOException { 110 throw new IOException( 111 "Method getDecodedObject MUST not be invoked"); 112 } 113 }; 114 115 for (int i = 0; i < testcases.length; i++) { 116 DerInputStream in = new DerInputStream((byte[]) testcases[i][1]); 117 in.setVerify(); 118 seqVerify.decode(in); 119 } 120 } 121 122 // 123 // Support class 124 // 125 public static class MyArray extends ArrayList { 126 127 public MyArray addMy(Object o) { 128 add(o); 129 return this; 130 } 131 } 132} 133