package org.bouncycastle.asn1.test;
import java.io.IOException;
import junit.framework.TestCase;
import org.bouncycastle.asn1.ASN1Boolean;
import org.bouncycastle.asn1.ASN1Enumerated;
import org.bouncycastle.asn1.ASN1ObjectIdentifier;
import org.bouncycastle.asn1.ASN1Primitive;
import org.bouncycastle.asn1.ASN1Sequence;
import org.bouncycastle.util.encoders.Hex;
public class EnumeratedTest
extends TestCase
{
private static final byte[] MultipleSingleByteItems = Hex.decode("30060a01010101ff");
private static final byte[] MultipleDoubleByteItems = Hex.decode("30080a0201010a020202");
private static final byte[] MultipleTripleByteItems = Hex.decode("300a0a0301010106032b0601");
public void testReadingMultipleSingleByteItems()
throws IOException
{
ASN1Primitive obj = ASN1Primitive.fromByteArray(MultipleSingleByteItems);
assertTrue("Null ASN.1 SEQUENCE", obj instanceof ASN1Sequence);
ASN1Sequence sequence = (ASN1Sequence)obj;
assertEquals("2 items expected", 2, sequence.size());
ASN1Enumerated enumerated = ASN1Enumerated.getInstance(sequence.getObjectAt(0));
assertNotNull("ENUMERATED expected", enumerated);
assertEquals("Unexpected ENUMERATED value", 1, enumerated.getValue().intValue());
ASN1Boolean b = ASN1Boolean.getInstance(sequence.getObjectAt(1));
assertNotNull("BOOLEAN expected", b);
assertTrue("Unexpected BOOLEAN value", b.isTrue());
}
public void testReadingMultipleDoubleByteItems()
throws IOException
{
ASN1Primitive obj = ASN1Primitive.fromByteArray(MultipleDoubleByteItems);
assertTrue("Null ASN.1 SEQUENCE", obj instanceof ASN1Sequence);
ASN1Sequence sequence = (ASN1Sequence)obj;
assertEquals("2 items expected", 2, sequence.size());
ASN1Enumerated enumerated1 = ASN1Enumerated.getInstance(sequence.getObjectAt(0));
assertNotNull("ENUMERATED expected", enumerated1);
assertEquals("Unexpected ENUMERATED value", 257, enumerated1.getValue().intValue());
ASN1Enumerated enumerated2 = ASN1Enumerated.getInstance(sequence.getObjectAt(1));
assertNotNull("ENUMERATED expected", enumerated2);
assertEquals("Unexpected ENUMERATED value", 514, enumerated2.getValue().intValue());
}
public void testReadingMultipleTripleByteItems()
throws IOException
{
ASN1Primitive obj = ASN1Primitive.fromByteArray(MultipleTripleByteItems);
assertTrue("Null ASN.1 SEQUENCE", obj instanceof ASN1Sequence);
ASN1Sequence sequence = (ASN1Sequence)obj;
assertEquals("2 items expected", 2, sequence.size());
ASN1Enumerated enumerated = ASN1Enumerated.getInstance(sequence.getObjectAt(0));
assertNotNull("ENUMERATED expected", enumerated);
assertEquals("Unexpected ENUMERATED value", 65793, enumerated.getValue().intValue());
ASN1ObjectIdentifier objectId = ASN1ObjectIdentifier.getInstance(sequence.getObjectAt(1));
assertNotNull("OBJECT IDENTIFIER expected", objectId);
assertEquals("Unexpected OBJECT IDENTIFIER value", "1.3.6.1", objectId.getId());
}
}