TestASN1Element.java revision b5a02d06a79b80a1f26a998673a3b7aae35b31da
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at
* trunk/opends/resource/legal-notices/OpenDS.LICENSE. If applicable,
* add the following below this CDDL HEADER, with the fields enclosed
* by brackets "[]" replaced with your own identifying * information:
* Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*
*
* Portions Copyright 2006 Sun Microsystems, Inc.
*/
/**
* This class defines a set of JUnit tests for the
* org.opends.server.protocols.asn1.ASN1Element class.
*
*
* @author Neil A. Wilson
*/
public class TestASN1Element
extends DirectoryServerTestCase
{
// The sets of pre-encoded ASN.1 elements that will be included in the test
// case.
// The set of pre-encoded element sets that will be used in the test cases.
private ArrayList<byte[]> testEncodedElementSets;
// The set of pre-encoded integer values that will be included in the test
// cases.
private ArrayList<byte[]> testEncodedIntegers;
// The set of pre-encoded lengths that will be included in the test cases.
private ArrayList<byte[]> testEncodedLengths;
// The set of BER types that will be included in the test cases.
// The set of element values that will be included in the test cases.
private ArrayList<byte[]> testValues;
// The set of integer values that will be included in the test cases.
// The set of lengths that will be included in the test cases.
/**
* Creates a new instance of this JUnit test case with the provided name.
*
* @param name The name to use for this JUnit test case.
*/
{
super(name);
}
/**
* Performs any necessary initialization for this test case.
*/
public void setUp()
{
// Initialize the set of types. It will encapsulate the entire range of
// possible byte values.
for (int i=0; i < 0xFF; i++)
{
}
// Initialize the set of values. Don't make these too big since they
// consume memory.
testValues = new ArrayList<byte[]>();
// Initialize the set of element lengths and their pre-encoded
// representations. Don't make these too big since we will create arrays
// with these lengths during testing.
testEncodedLengths = new ArrayList<byte[]>();
(byte) 0x00 });
(byte) 0xFF });
(byte) 0x00, (byte) 0x00 });
// Initialize the set of integer values and their pre-encoded
// representations. These can get big since they will not be used to create
// arrays. Also, there is no need to test negative values since LDAP
// doesn't make use of them.
testEncodedIntegers = new ArrayList<byte[]>();
(byte) 0x00 });
(byte) 0xFF });
(byte) 0x00 });
(byte) 0xFF });
(byte) 0x00, (byte) 0x00 });
(byte) 0xFF, (byte) 0xFF });
(byte) 0x00, (byte) 0x00 });
(byte) 0xFF, (byte) 0xFF });
// Initialize the sets of ASN.1 elements that will be used in testing the
testEncodedElementSets = new ArrayList<byte[]>();
// Sets containing from 1 to 10 elements.
for (int i=1; i <= 10; i++)
{
for (int j=0; j < i; j++)
{
}
}
}
/**
* Performs any necessary cleanup for this test case.
*/
public void tearDown()
{
// No implementation required.
}
/**
* Tests the <CODE>getType</CODE> method.
*/
public void testGetType()
{
{
try
{
}
catch (AssertionFailedError afe)
{
throw afe;
}
for (byte[] value : testValues)
{
try
{
}
catch (AssertionFailedError afe)
{
bytesToHex(value));
throw afe;
}
}
}
}
/**
* Tests the <CODE>setType</CODE> method.
*/
public void testSetType()
{
{
try
{
}
catch (AssertionFailedError afe)
{
throw afe;
}
}
}
/**
* Tests the <CODE>isUniversal</CODE> method.
*/
public void testIsUniversal()
{
{
try
{
}
catch (AssertionFailedError afe)
{
throw afe;
}
}
}
/**
* Tests the <CODE>isApplicationSpecific</CODE> method.
*/
public void testIsApplicationSpecific()
{
{
try
{
}
catch (AssertionFailedError afe)
{
throw afe;
}
}
}
/**
* Tests the <CODE>isContextSpecific</CODE> method.
*/
public void testIsContextSpecific()
{
{
try
{
}
catch (AssertionFailedError afe)
{
throw afe;
}
}
}
/**
* Tests the <CODE>isPrivate</CODE> method.
*/
public void testIsPrivate()
{
{
try
{
}
catch (AssertionFailedError afe)
{
throw afe;
}
}
}
/**
* Tests the <CODE>isPrimitive</CODE> method.
*/
public void testIsPrimitive()
{
{
try
{
}
catch (AssertionFailedError afe)
{
throw afe;
}
}
}
/**
* Tests the <CODE>isConstructed</CODE> method.
*/
public void testIsConstructed()
{
{
try
{
}
catch (AssertionFailedError afe)
{
throw afe;
}
}
}
/**
* Tests the <CODE>getValue</CODE> method.
*/
public void testGetValue()
{
{
try
{
}
catch (AssertionFailedError afe)
{
throw afe;
}
for (byte[] value : testValues)
{
try
{
{
}
else
{
}
}
catch (AssertionFailedError afe)
{
throw afe;
}
}
}
}
/**
* Tests the <CODE>setValue</CODE> method.
*/
public void testSetValue()
{
for (byte[] value : testValues)
{
try
{
}
catch (Exception e)
{
printException(e);
}
try
{
{
}
else
{
}
}
catch (AssertionFailedError afe)
{
throw afe;
}
}
}
/**
* Tests the <CODE>encodeLength</CODE> method.
*/
public void testEncodeLength()
{
for (int i=0; i < numLengths; i++)
{
try
{
}
catch (AssertionFailedError afe)
{
throw afe;
}
}
}
/**
* Tests the <CODE>encode</CODE> and <CODE>decode</CODE> methods.
*/
public void testEncodeAndDecode()
{
{
for (byte[] value : testValues)
{
int length;
byte[] encodedLength;
{
length = 0;
encodedLength = new byte[] { (byte) 0x00 };
}
else
{
}
{
length);
}
try
{
}
catch (AssertionFailedError afe)
{
", valueLength=" + length);
throw afe;
}
try
{
}
catch (ASN1Exception ae)
{
}
catch (AssertionFailedError afe)
{
", valueLength=" + length);
throw afe;
}
}
for (int i=0; i < numLengths; i++)
{
try
{
}
catch (AssertionFailedError afe)
{
length);
throw afe;
}
try
{
}
catch (ASN1Exception ae)
{
}
catch (AssertionFailedError afe)
{
length);
throw afe;
}
}
}
}
/**
* Tests the <CODE>encodeValue</CODE> method with a single boolean argument.
*/
public void testEncodeBooleanValue()
{
byte[] encodedFalse = new byte[] { (byte) 0x00 };
byte[] encodedTrue = new byte[] { (byte) 0xFF };
try
{
ASN1Element.encodeValue(false)));
}
catch (AssertionFailedError afe)
{
printError("encodeValue failed for a boolean value of false");
throw afe;
}
try
{
ASN1Element.encodeValue(true)));
}
catch (AssertionFailedError afe)
{
printError("encodeValue failed for a boolean value of true");
throw afe;
}
}
/**
* Tests the <CODE>encodeValue</CODE> method with a single int argument.
*/
public void testEncodeIntValue()
{
for (int i=0; i < numIntValues; i++)
{
try
{
}
catch (AssertionFailedError afe)
{
throw afe;
}
}
}
/**
* Tests the <CODE>encodeValue</CODE> method with a set of ASN.1 elements.
*/
public void testEncodeAndDecodeElements()
{
for (int i=0; i < numElementSets; i++)
{
try
{
}
catch (AssertionFailedError afe)
{
if (elementSet == null)
{
printError("encodeValue failed for a null set of elements");
}
else
{
" elements");
}
throw afe;
}
try
{
}
catch (ASN1Exception ae)
{
}
if (elementSet == null)
{
}
else
{
}
try
{
}
catch (AssertionFailedError afe)
{
printError("decodeElements failed for value=" +
throw afe;
}
}
}
/**
* Tests the <CODE>decodeAsBoolean</CODE> method.
*/
public void testDecodeAsBoolean()
{
for (int i=0; i < 256; i++)
{
byte[] valueByte = new byte[] { (byte) i };
boolean booleanValue = (i != 0);
try
{
}
catch (ASN1Exception ae)
{
}
catch (AssertionFailedError afe)
{
printError("decodeAsBoolean failed for type=00, value=" +
throw afe;
}
try
{
}
catch (ASN1Exception ae)
{
}
catch (AssertionFailedError afe)
{
printError("decodeAsBoolean failed for type=01, value=" +
throw afe;
}
}
}
/**
* Tests the <CODE>decodeAsEnumerated</CODE> method.
*/
public void testDecodeAsEnumerated()
{
for (int i=0; i < numIntValues; i++)
{
try
{
}
catch (ASN1Exception ae)
{
"intValue=" + intValue;
}
catch (AssertionFailedError afe)
{
printError("decodeAsEnumerated failed for type=00, intValue=" +
intValue);
throw afe;
}
try
{
}
catch (ASN1Exception ae)
{
"intValue=" + intValue;
}
catch (AssertionFailedError afe)
{
printError("decodeAsEnumerated failed for type=0A, intValue=" +
intValue);
throw afe;
}
}
}
/**
* Tests the <CODE>decodeAsInteger</CODE> method.
*/
public void testDecodeAsInteger()
{
for (int i=0; i < numIntValues; i++)
{
try
{
}
catch (ASN1Exception ae)
{
"intValue=" + intValue;
}
catch (AssertionFailedError afe)
{
throw afe;
}
try
{
}
catch (ASN1Exception ae)
{
"intValue=" + intValue;
}
catch (AssertionFailedError afe)
{
throw afe;
}
}
}
/**
* Tests the <CODE>decodeAsNull</CODE> method.
*/
public void testDecodeAsNull()
{
{
try
{
}
catch (ASN1Exception ae)
{
}
catch (AssertionFailedError afe)
{
throw afe;
}
}
}
/**
* Tests the <CODE>decodeAsOctetString</CODE> method.
*/
public void testDecodeAsOctetString()
{
for (byte[] value : testValues)
{
byte[] compareValue;
{
compareValue = new byte[0];
}
else
{
}
try
{
}
catch (ASN1Exception ae)
{
}
catch (AssertionFailedError afe)
{
printError("decodeAsOctetString failed for type=00, value=" +
bytesToHex(value));
throw afe;
}
try
{
}
catch (ASN1Exception ae)
{
}
catch (AssertionFailedError afe)
{
printError("decodeAsOctetString failed for type=04, value=" +
bytesToHex(value));
throw afe;
}
}
}
/**
* Tests the <CODE>decodeAsSequence</CODE> method.
*/
public void testDecodeAsSequence()
{
for (int i=0; i < numElementSets; i++)
{
if (elementSet == null)
{
}
else
{
}
try
{
}
catch (ASN1Exception ae)
{
}
catch (AssertionFailedError afe)
{
printError("decodeAsSequence failed for type=00, value=" +
throw afe;
}
try
{
}
catch (ASN1Exception ae)
{
}
catch (AssertionFailedError afe)
{
printError("decodeAsSequence failed for type=30, value=" +
throw afe;
}
}
}
/**
* Tests the <CODE>decodeAsSet</CODE> method.
*/
public void testDecodeAsSet()
{
for (int i=0; i < numElementSets; i++)
{
if (elementSet == null)
{
}
else
{
}
try
{
}
catch (ASN1Exception ae)
{
}
catch (AssertionFailedError afe)
{
printError("decodeAsSet failed for type=00, value=" +
throw afe;
}
try
{
}
catch (ASN1Exception ae)
{
}
catch (AssertionFailedError afe)
{
printError("decodeAsSet failed for type=31, value=" +
throw afe;
}
}
}
/**
* Tests the <CODE>equals</CODE> and <CODE>hashCode</CODE> methods.
*/
public void testEqualsAndHashCode()
{
// Perform simple tests for two basic elements that should be the same, one
// that should differ in type, one that should differ in value, and one that
// should differ in both.
try
{
}
catch (AssertionFailedError afe)
{
printError("e1.equals(e2) failed");
throw afe;
}
try
{
}
{
printError("e2.equals(e1) failed");
throw afe;
}
try
{
}
catch (AssertionFailedError afe)
{
printError("e1.equals(e3) failed");
throw afe;
}
try
{
}
catch (AssertionFailedError afe)
{
printError("e1.equals(e4) failed");
throw afe;
}
try
{
}
catch (AssertionFailedError afe)
{
printError("e1.equals(e5) failed");
throw afe;
}
try
{
}
catch (AssertionFailedError afe)
{
printError("e4.equals(e6) failed");
throw afe;
}
// Make sure that equal elements have equal hash codes.
try
{
}
catch (AssertionFailedError afe)
{
printError("hashCode failed");
throw afe;
}
// Test equals against a null element.
try
{
}
catch (AssertionFailedError afe)
{
printError("e1.equals(null) failed for type=00");
throw afe;
}
// Test boolean elements against equivalent generic elements.
new byte[] { (byte) 0xFF });
new byte[] { (byte) 0x00 });
try
{
}
catch (AssertionFailedError afe)
{
printError("genericElement.equals(booleanElement) failed for " +
"booleanValue=true");
throw afe;
}
try
{
}
catch (AssertionFailedError afe)
{
printError("booleanElement.equals(genericElement) failed for " +
"booleanValue=true");
throw afe;
}
try
{
}
catch (AssertionFailedError afe)
{
printError("genericElement.hashCode != booleanElement.hashCode " +
"for booleanValue=true");
throw afe;
}
try
{
}
catch (AssertionFailedError afe)
{
printError("genericElement.equals(booleanElement) failed for " +
"booleanValue=false");
throw afe;
}
try
{
}
catch (AssertionFailedError afe)
{
printError("booleanElement.equals(genericElement) failed for " +
"booleanValue=false");
throw afe;
}
try
{
}
catch (AssertionFailedError afe)
{
printError("genericElement.hashCode != booleanElement.hashCode " +
"for booleanValue=false");
throw afe;
}
// Test integer elements against equivalent generic elements.
for (int i=0; i < numIntegers; i++)
{
try
{
}
catch (AssertionFailedError afe)
{
printError("genericElement.equals(integerElement) failed for " +
"intValue=" + intValue);
throw afe;
}
try
{
}
catch (AssertionFailedError afe)
{
printError("integerElement.equals(genericElement) failed for " +
"intValue=" + intValue);
throw afe;
}
try
{
// Test for matching hash codes.
}
catch (AssertionFailedError afe)
{
printError("genericElement.hashCode != integerElement.hashCode for " +
"intValue=" + intValue);
throw afe;
}
}
}
}