mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

neil_a_wilson
11.32.2006 a1842fba52865f987e83fe9f1d56eaed585420dd
Update ASN.1 test cases for the integer, long, enumerated, Boolean, and null
element types.
6 files modified
3075 ■■■■ changed files
opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/protocols/asn1/TestASN1Boolean.java 514 ●●●● patch | view | raw | blame | history
opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/protocols/asn1/TestASN1Enumerated.java 659 ●●●● patch | view | raw | blame | history
opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/protocols/asn1/TestASN1Exception.java 4 ●●●● patch | view | raw | blame | history
opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/protocols/asn1/TestASN1Integer.java 675 ●●●● patch | view | raw | blame | history
opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/protocols/asn1/TestASN1Long.java 925 ●●●● patch | view | raw | blame | history
opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/protocols/asn1/TestASN1Null.java 298 ●●●● patch | view | raw | blame | history
opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/protocols/asn1/TestASN1Boolean.java
@@ -26,128 +26,448 @@
 */
package org.opends.server.protocols.asn1;
import static org.testng.AssertJUnit.assertEquals;
import static org.testng.AssertJUnit.assertFalse;
import static org.testng.AssertJUnit.assertTrue;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import static org.testng.Assert.*;
/**
 * This class defines a set of tests for the
 * org.opends.server.protocols.asn1.ASN1Boolean class.
 */
public class TestASN1Boolean extends ASN1TestCase {
public class TestASN1Boolean
       extends ASN1TestCase
{
  /**
   * Retrieves the set of boolean values that may be used for testing.
   *
   * @return  The set of boolean values that may be used for testing.
   */
  @DataProvider(name = "booleanValues")
  public Object[][] getBooleanValues()
  {
    return new Object[][]
    {
      new Object[] { false },
      new Object[] { true }
    };
  }
  /**
   * Tests the first constructor, which takes a single boolean argument.
   *
   * @param  b  The boolean value to use in the test.
   */
  @Test(dataProvider = "booleanValues")
  public void testConstructor1(boolean b)
  {
    new ASN1Boolean(b);
  }
  /**
   * Tests the second constructor, which takes byte and boolean arguments.
   *
   * @param  b  The boolean value to use in the test.
   */
  @Test(dataProvider = "booleanValues")
  public void testConstructor2(boolean b)
  {
    new ASN1Boolean((byte) 0x50, b);
  }
  /**
   * Tests the <CODE>booleanValue</CODE> method.
   *
   * @param  b  The boolean value to use in the test.
   */
  @Test
  public void testBooleanValue() {
    assertTrue(new ASN1Boolean(true).booleanValue());
    assertFalse(new ASN1Boolean(false).booleanValue());
  @Test(dataProvider = "booleanValues")
  public void testBooleanValue(boolean b)
  {
    assertEquals(b, new ASN1Boolean(b).booleanValue());
  }
  /**
   * Tests the <CODE>setValue</CODE> method that takes a boolean
   * argument.
   * Tests the <CODE>setValue</CODE> method that takes a boolean argument.
   *
   * @param  b  The boolean value to use in the test.
   */
  @Test(dependsOnMethods = { "testBooleanValue" })
  public void testSetBooleanValue() {
    ASN1Boolean element = new ASN1Boolean(true); // Create a new true
    // element.
    element.setValue(true); // Test setting the same value.
    assertTrue(element.booleanValue());
    element.setValue(false); // Test setting the opposite value.
    assertFalse(element.booleanValue());
    element = new ASN1Boolean(false); // Create a new false element.
    element.setValue(false); // Test setting the same value.
    assertFalse(element.booleanValue());
    element.setValue(true); // Test setting the opposite value.
    assertTrue(element.booleanValue());
  @Test(dataProvider = "booleanValues")
  public void testSetBooleanValue(boolean b)
  {
    ASN1Boolean booleanElement = new ASN1Boolean(!b);
    booleanElement.setValue(b);
    assertEquals(b, booleanElement.booleanValue());
  }
  /**
   * Tests the <CODE>setValue</CODE> method that takes a byte array
   * Retrieves the set of byte array values that may be used for testing.
   *
   * @return  The set of byte array values that may be used for testing.
   */
  @DataProvider(name = "byteValues")
  public Object[][] getByteValues()
  {
    Object[][] array = new Object[256][1];
    for (int i=0; i < 256; i++)
    {
      array[i] = new Object[] { new byte[] { (byte) (i & 0xFF) } };
    }
    return array;
  }
  /**
   * Tests the <CODE>setValue</CODE> method that takes a byte array argument
   * with valid values.
   *
   * @param  b  The byte array to use in the test.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test(dataProvider = "byteValues")
  public void testSetValidByteValue(byte[] b)
         throws Exception
  {
    ASN1Boolean booleanElement = new ASN1Boolean(false);
    booleanElement.setValue(b);
    assertEquals((b[0] != 0x00), booleanElement.booleanValue());
  }
  /**
   * Tests the <CODE>setValue</CODE> method that takes a byte array argument
   * with a null value.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test(expectedExceptions = { ASN1Exception.class })
  public void testSetNullByteValue()
         throws Exception
  {
    ASN1Boolean booleanElement = new ASN1Boolean(false);
    byte[] b = null;
    booleanElement.setValue(b);
  }
  /**
   * Tests the <CODE>setValue</CODE> method that takes a byte array argument
   * with an empty array value.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test(expectedExceptions = { ASN1Exception.class })
  public void testSetZeroByteValue()
         throws Exception
  {
    ASN1Boolean booleanElement = new ASN1Boolean(false);
    byte[] b = new byte[0];
    booleanElement.setValue(b);
  }
  /**
   * Tests the <CODE>setValue</CODE> method that takes a byte array argument
   * with a multi-byte array value.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test(expectedExceptions = { ASN1Exception.class })
  public void testSetMultiByteValue()
         throws Exception
  {
    ASN1Boolean booleanElement = new ASN1Boolean(false);
    byte[] b = new byte[2];
    booleanElement.setValue(b);
  }
  /**
   * Tests the <CODE>decodeAsBoolean</CODE> method that takes an ASN1Element
   * argument with valid elements.
   *
   * @param  b  The byte array to use for the element values.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test(dataProvider = "byteValues")
  public void testDecodeValidElementAsBoolean(byte[] b)
         throws Exception
  {
    // First, try with an actual boolean element.
    ASN1Element e = new ASN1Boolean(false);
    e.setValue(b);
    ASN1Boolean booleanElement = ASN1Boolean.decodeAsBoolean(e);
    assertEquals((b[0] != 0x00), booleanElement.booleanValue());
    e = new ASN1Boolean((byte) 0x50, false);
    e.setValue(b);
    booleanElement = ASN1Boolean.decodeAsBoolean(e);
    assertEquals((b[0] != 0x00), booleanElement.booleanValue());
    // Next, test with a generic ASN.1 element.
    e = new ASN1Element(ASN1Constants.UNIVERSAL_BOOLEAN_TYPE, b);
    booleanElement = ASN1Boolean.decodeAsBoolean(e);
    assertEquals((b[0] != 0x00), booleanElement.booleanValue());
    e = new ASN1Element((byte) 0x50, b);
    booleanElement = ASN1Boolean.decodeAsBoolean(e);
    assertEquals((b[0] != 0x00), booleanElement.booleanValue());
  }
  /**
   * Tests the <CODE>decodeAsBoolean</CODE> method that takes an ASN1Element
   * argument with a null element.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test(expectedExceptions = { ASN1Exception.class })
  public void testDecodeNullElementAsBoolean()
         throws Exception
  {
    ASN1Element e = null;
    ASN1Boolean.decodeAsBoolean(e);
  }
  /**
   * Tests the <CODE>decodeAsBoolean</CODE> method that takes an ASN1Element
   * argument with a zero-byte element.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test(expectedExceptions = { ASN1Exception.class })
  public void testDecodeZeroByteElementAsBoolean()
         throws Exception
  {
    ASN1Element e = new ASN1Element((byte) 0x50, new byte[0]);
    ASN1Boolean.decodeAsBoolean(e);
  }
  /**
   * Tests the <CODE>decodeAsBoolean</CODE> method that takes an ASN1Element
   * argument with a multi-byte element.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test(expectedExceptions = { ASN1Exception.class })
  public void testDecodeMultiByteElementAsBoolean()
         throws Exception
  {
    ASN1Element e = new ASN1Element((byte) 0x50, new byte[2]);
    ASN1Boolean.decodeAsBoolean(e);
  }
  /**
   * Tests the <CODE>decodeAsBoolean</CODE> method that takes a byte array
   * argument with valid arrays.
   *
   * @param  b  The byte array to use for the element values.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test(dataProvider = "byteValues")
  public void testDecodeValidArrayAsBoolean(byte[] b)
         throws Exception
  {
    // First, test with the standard Boolean type.
    byte[] elementArray = new byte[] { 0x01, 0x01, b[0] };
    ASN1Boolean booleanElement = ASN1Boolean.decodeAsBoolean(elementArray);
    assertEquals((b[0] != 0x00), booleanElement.booleanValue());
    // Next, test with a nonstandard Boolean type.
    elementArray[0] = (byte) 0x50;
    booleanElement = ASN1Boolean.decodeAsBoolean(elementArray);
    assertEquals((b[0] != 0x00), booleanElement.booleanValue());
  }
  /**
   * Tests the <CODE>decodeAsBoolean</CODE> method that takes a byte array
   * argument with valid arrays using extended lengths.
   *
   * @param  b  The byte array to use for the element values.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test(dataProvider = "byteValues")
  public void testDecodeValidExtendedArrayAsBoolean(byte[] b)
         throws Exception
  {
    // First, test with the standard Boolean type.
    byte[] elementArray = new byte[] { 0x01, (byte) 0x81, 0x01, b[0] };
    ASN1Boolean booleanElement = ASN1Boolean.decodeAsBoolean(elementArray);
    assertEquals((b[0] != 0x00), booleanElement.booleanValue());
    // Next, test with a nonstandard Boolean type.
    elementArray[0] = (byte) 0x50;
    booleanElement = ASN1Boolean.decodeAsBoolean(elementArray);
    assertEquals((b[0] != 0x00), booleanElement.booleanValue());
  }
  /**
   * Tests the <CODE>decodeAsBoolean</CODE> method that takes a byte array
   * argument with a null array.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test(expectedExceptions = { ASN1Exception.class })
  public void testDecodeNullArrayAsBoolean()
         throws Exception
  {
    byte[] b = null;
    ASN1Boolean.decodeAsBoolean(b);
  }
  /**
   * Tests the <CODE>decodeAsBoolean</CODE> method that takes a byte array
   * argument with a short array.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test(expectedExceptions = { ASN1Exception.class })
  public void testDecodeShortArrayAsBoolean()
         throws Exception
  {
    byte[] b = new byte[1];
    ASN1Boolean.decodeAsBoolean(b);
  }
  /**
   * Tests the <CODE>decodeAsBoolean</CODE> method that takes a byte array
   * argument with an array that takes too many bytes to expressthe length.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test(expectedExceptions = { ASN1Exception.class })
  public void testDecodeLongLengthArrayAsBoolean()
         throws Exception
  {
    byte[] b = { 0x01, (byte) 0x85, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00 };
    ASN1Boolean.decodeAsBoolean(b);
  }
  /**
   * Tests the <CODE>decodeAsBoolean</CODE> method that takes a byte array
   * argument with an array that doesn't contain a full length.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test(expectedExceptions = { ASN1Exception.class })
  public void testDecodeTruncatedLengthArrayAsBoolean()
         throws Exception
  {
    byte[] b = { 0x01, (byte) 0x82, 0x00 };
    ASN1Boolean.decodeAsBoolean(b);
  }
  /**
   * Tests the <CODE>decodeAsBoolean</CODE> method that takes a byte array
   * argument with an array that has more bytes than indicated by the length.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test(expectedExceptions = { ASN1Exception.class })
  public void testDecodeLengthMismatchArrayAsBoolean()
         throws Exception
  {
    byte[] b = { 0x01, 0x01, 0x00, 0x00 };
    ASN1Boolean.decodeAsBoolean(b);
  }
  /**
   * Tests the <CODE>decodeAsBoolean</CODE> method that takes a byte array
   * argument with an array that has an invalid number of bytes in the value.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test(expectedExceptions = { ASN1Exception.class })
  public void testDecodeLongValueArrayAsBoolean()
         throws Exception
  {
    byte[] b = { 0x01, 0x02, 0x00, 0x00 };
    ASN1Boolean.decodeAsBoolean(b);
  }
  /**
   * Tests the first <CODE>toString</CODE> method which takes a string builder
   * argument.
   *
   * @throws Exception
   *           If the test failed unexpectedly.
   * @param  b  The byte array to use as the element value.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test(dependsOnMethods = { "testBooleanValue" })
  public void testSetByteValue() throws Exception {
    ASN1Boolean element = new ASN1Boolean(true);
    element.setValue(new byte[] { 0x00 });
    assertFalse(element.booleanValue());
    // Test setting the value to all other possible byte
    // representations, which
    // should evaluate to "true".
    for (int i = 1; i < 256; i++) {
      byte byteValue = (byte) (i & 0xFF);
      element.setValue(new byte[] { byteValue });
      assertTrue(element.booleanValue());
  @Test(dataProvider = "byteValues")
  public void testToString1(byte[] b)
         throws Exception
  {
    ASN1Boolean booleanElement = new ASN1Boolean(false);
    booleanElement.setValue(b);
    booleanElement.toString(new StringBuilder());
    }
  }
  /**
   * Tests the <CODE>decodeAsBoolean</CODE> method that takes an ASN.1
   * element argument.
   * Tests the second <CODE>toString</CODE> method which takes string builder
   * and integer arguments.
   *
   * @throws Exception
   *           If the test failed unexpectedly.
   */
  @Test(dependsOnMethods = { "testBooleanValue" })
  public void testDecodeElementAsBoolean() throws Exception {
    // Test a boolean element of "true".
    ASN1Boolean trueBoolean = new ASN1Boolean(true);
    assertTrue(ASN1Boolean.decodeAsBoolean(trueBoolean).booleanValue());
    // Test a boolean element of "false".
    ASN1Boolean falseBoolean = new ASN1Boolean(false);
    assertFalse(ASN1Boolean.decodeAsBoolean(falseBoolean).booleanValue());
    // Test the valid generic elements that may be used.
    for (int i = 0; i < 256; i++) {
      byte byteValue = (byte) (i & 0xFF);
      boolean compareValue = (i != 0);
      ASN1Element element = new ASN1Element((byte) 0x00,
          new byte[] { byteValue });
      assertEquals(compareValue, ASN1Boolean.decodeAsBoolean(element)
          .booleanValue());
    }
  }
  /**
   * Tests the <CODE>decodeAsBoolean</CODE> method that takes a byte
   * array argument.
   * @param  b  The byte array to use as the element value.
   *
   * @throws Exception
   *           If the test failed unexpectedly.
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test(dependsOnMethods = { "testBooleanValue" })
  public void testDecodeBytesAsBoolean() throws Exception {
    byte[] encodedElement = new byte[] { 0x00, 0x01, 0x00 };
    // Test all possible byte values.
    for (int i = 0; i < 256; i++) {
      boolean compareValue = (i != 0);
      // Set the value.
      encodedElement[2] = (byte) (i & 0xFF);
      // Test with the standard Boolean type.
      encodedElement[0] = (byte) 0x01;
      assertEquals(compareValue, ASN1Boolean
          .decodeAsBoolean(encodedElement).booleanValue());
  @Test(dataProvider = "byteValues")
  public void testToString2(byte[] b)
         throws Exception
  {
    ASN1Boolean booleanElement = new ASN1Boolean(false);
    booleanElement.setValue(b);
    booleanElement.toString(new StringBuilder(), 1);
    }
  }
}
opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/protocols/asn1/TestASN1Enumerated.java
@@ -26,195 +26,548 @@
 */
package org.opends.server.protocols.asn1;
import static org.testng.AssertJUnit.assertEquals;
import static org.testng.AssertJUnit.assertTrue;
import java.util.ArrayList;
import java.util.Arrays;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeClass;
import static org.testng.Assert.*;
/**
 * This class defines a set of tests for the
 * org.opends.server.protocols.asn1.ASN1Enumerated class.
 */
public class TestASN1Enumerated extends ASN1TestCase {
  // The set of encoded values for the test integers.
  private ArrayList<byte[]> testEncodedIntegers;
public class TestASN1Enumerated
       extends ASN1TestCase
{
  /**
   * Retrieves the set of int values that should be used for testing.
   *
   * @return  The set of int values that should be used for testing.
   */
  @DataProvider(name = "intValues")
  public Object[][] getIntValues()
  {
    return new Object[][]
    {
      new Object[] { 0x00000000 },
      new Object[] { 0x00000001 },
      new Object[] { 0x0000000F },
      new Object[] { 0x00000010 },
      new Object[] { 0x0000007F },
      new Object[] { 0x00000080 },
      new Object[] { 0x000000FF },
      new Object[] { 0x00000100 },
      new Object[] { 0x00000FFF },
      new Object[] { 0x00001000 },
      new Object[] { 0x0000FFFF },
      new Object[] { 0x00010000 },
      new Object[] { 0x000FFFFF },
      new Object[] { 0x00100000 },
      new Object[] { 0x00FFFFFF },
      new Object[] { 0x01000000 },
      new Object[] { 0x0FFFFFFF },
      new Object[] { 0x10000000 },
      new Object[] { 0x7FFFFFFF }
    };
  }
  // The set of integer values to use in test cases.
  private ArrayList<Integer> testIntegers;
  /**
   * Performs any necessary initialization for this test case.
   * Tests the first constructor, which takes a single integer argument.
   *
   * @param  i  The integer value to use to create the element.
   */
  @BeforeClass
  public void setUp() {
    testIntegers = new ArrayList<Integer>();
    testEncodedIntegers = new ArrayList<byte[]>();
    // Add all values that can be encoded using a single byte.
    for (int i = 0; i < 128; i++) {
      testIntegers.add(i);
      testEncodedIntegers.add(new byte[] { (byte) (i & 0xFF) });
  @Test(dataProvider = "intValues")
  public void testConstructor1(int i)
  {
    new ASN1Enumerated(i);
    }
    testIntegers.add(0x80); // The smallest 2-byte encoding.
    testEncodedIntegers.add(new byte[] { (byte) 0x00, (byte) 0x80 });
    testIntegers.add(0xFF); // A boundary case for 2-byte encoding.
    testEncodedIntegers.add(new byte[] { (byte) 0x00, (byte) 0xFF });
    testIntegers.add(0x0100); // A boundary case for 2-byte encoding.
    testEncodedIntegers.add(new byte[] { (byte) 0x01, (byte) 0x00 });
    testIntegers.add(0x7FFF); // The largest 2-byte encoding.
    testEncodedIntegers.add(new byte[] { (byte) 0x7F, (byte) 0xFF });
    testIntegers.add(0x8000); // The smallest 3-byte encoding.
    testEncodedIntegers.add(new byte[] { (byte) 0x00, (byte) 0x80,
        (byte) 0x00 });
    testIntegers.add(0xFFFF); // A boundary case for 3-byte encoding.
    testEncodedIntegers.add(new byte[] { (byte) 0x00, (byte) 0xFF,
        (byte) 0xFF });
    testIntegers.add(0x010000); // A boundary case for 3-byte encoding.
    testEncodedIntegers.add(new byte[] { (byte) 0x01, (byte) 0x00,
        (byte) 0x00 });
    testIntegers.add(0x7FFFFF); // The largest 3-byte encoding.
    testEncodedIntegers.add(new byte[] { (byte) 0x7F, (byte) 0xFF,
        (byte) 0xFF });
    testIntegers.add(0x800000); // The smallest 4-byte encoding.
    testEncodedIntegers.add(new byte[] { (byte) 0x00, (byte) 0x80,
        (byte) 0x00, (byte) 0x00 });
    testIntegers.add(0xFFFFFF); // A boundary case for 4-byte encoding.
    testEncodedIntegers.add(new byte[] { (byte) 0x00, (byte) 0xFF,
        (byte) 0xFF, (byte) 0xFF });
    testIntegers.add(0x01000000); // A boundary case for 4-byte
    // encoding.
    testEncodedIntegers.add(new byte[] { (byte) 0x01, (byte) 0x00,
        (byte) 0x00, (byte) 0x00 });
    testIntegers.add(0x7FFFFFFF); // The largest value we will allow.
    testEncodedIntegers.add(new byte[] { (byte) 0x7F, (byte) 0xFF,
        (byte) 0xFF, (byte) 0xFF });
  /**
   * Tests the second constructor, which takes byte and integer arguments.
   *
   * @param  i  The integer value to use to create the element.
   */
  @Test(dataProvider = "intValues")
  public void testConstructor2(int i)
  {
    new ASN1Enumerated((byte) 0x50, i);
  }
  /**
   * Tests the <CODE>intValue</CODE> method.
   *
   * @param  i  The integer value to use for the test.
   */
  @Test()
  public void testIntValue() {
    for (int i : testIntegers) {
      ASN1Integer element = new ASN1Integer(i);
  @Test(dataProvider = "intValues")
  public void testIntValue(int i)
  {
    assertEquals(i, new ASN1Enumerated(i).intValue());
  }
      assertEquals(i, element.intValue());
    }
  }
  /**
   * Tests the <CODE>setValue</CODE> method that takes an int
   * argument.
   * Tests the <CODE>setValue</CODE> method that takes an int argument.
   *
   * @param  i  The integer value to use for the test.
   */
  @Test()
  public void testSetIntValue() {
    ASN1Integer element = new ASN1Integer(0);
    int numIntegers = testIntegers.size();
    for (int i = 0; i < numIntegers; i++) {
      int intValue = testIntegers.get(i);
      byte[] encodedIntValue = testEncodedIntegers.get(i);
      element.setValue(intValue);
      assertEquals(intValue, element.intValue());
      assertTrue(Arrays.equals(encodedIntValue, element.value()));
  @Test(dataProvider = "intValues")
  public void testSetIntValue(int i)
  {
    ASN1Enumerated enumeratedElement = new ASN1Enumerated(0);
    enumeratedElement.setValue(i);
    assertEquals(i, enumeratedElement.intValue());
    }
  }
  /**
   * Tests the <CODE>setValue</CODE> method that takes a byte array
   * Tests the <CODE>setValue</CODE> method that takes a byte array argument
   * with a valid array.
   *
   * @param  i  The integer value to use for the test.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test(dataProvider = "intValues")
  public void testSetByteValue(int i)
         throws Exception
  {
    ASN1Enumerated enumeratedElement = new ASN1Enumerated(0);
    byte[] encoding;
    if ((i & 0xFF) == i)
    {
      encoding = new byte[1];
      encoding[0] = (byte) (i & 0xFF);
    }
    else if ((i & 0xFFFF) == i)
    {
      encoding = new byte[2];
      encoding[0] = (byte) ((i >> 8) & 0xFF);
      encoding[1] = (byte) (i & 0xFF);
    }
    else if ((i & 0xFFFFFF) == i)
    {
      encoding = new byte[3];
      encoding[0] = (byte) ((i >> 16) & 0xFF);
      encoding[1] = (byte) ((i >> 8) & 0xFF);
      encoding[2] = (byte) (i & 0xFF);
    }
    else
    {
      encoding = new byte[4];
      encoding[0] = (byte) ((i >> 24) & 0xFF);
      encoding[1] = (byte) ((i >> 16) & 0xFF);
      encoding[2] = (byte) ((i >> 8) & 0xFF);
      encoding[3] = (byte) (i & 0xFF);
    }
    enumeratedElement.setValue(encoding);
    assertEquals(i, enumeratedElement.intValue());
  }
  /**
   * Tests the <CODE>setValue</CODE> method that takes a byte array argument
   * with a null array.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test(expectedExceptions = { ASN1Exception.class })
  public void testSetByteValueNull()
         throws Exception
  {
    ASN1Enumerated enumeratedElement = new ASN1Enumerated(0);
    byte[] b = null;
    enumeratedElement.setValue(b);
  }
  /**
   * Tests the <CODE>setValue</CODE> method that takes a byte array argument
   * with an empty array.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test(expectedExceptions = { ASN1Exception.class })
  public void testSetByteValueEmptyArray()
         throws Exception
  {
    ASN1Enumerated enumeratedElement = new ASN1Enumerated(0);
    byte[] b = new byte[0];
    enumeratedElement.setValue(b);
  }
  /**
   * Tests the <CODE>setValue</CODE> method that takes a byte array argument
   * with a long array.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test(expectedExceptions = { ASN1Exception.class })
  public void testSetByteValueLongArray()
         throws Exception
  {
    ASN1Enumerated enumeratedElement = new ASN1Enumerated(0);
    byte[] b = new byte[5];
    enumeratedElement.setValue(b);
  }
  /**
   * Tests the <CODE>decodeAsEnumerated</CODE> method that takes an ASN1Element
   * arguent using a valid value.
   *
   * @param i  The integer value to use in the test.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test(dataProvider = "intValues")
  public void testDecodeValidElementAsEnumerated(int i)
         throws Exception
  {
    // First, make sure that we can decode an integer element as an integer.
    ASN1Element e = new ASN1Enumerated(i);
    ASN1Enumerated enumeratedElement = ASN1Enumerated.decodeAsEnumerated(e);
    assertEquals(i, enumeratedElement.intValue());
    e = new ASN1Enumerated((byte) 0x50, i);
    enumeratedElement = ASN1Enumerated.decodeAsEnumerated(e);
    assertEquals(i, enumeratedElement.intValue());
    // Next, make sure that we can decode a generic element as an integer.
    byte[] encoding;
    if ((i & 0xFF) == i)
    {
      encoding = new byte[1];
      encoding[0] = (byte) (i & 0xFF);
    }
    else if ((i & 0xFFFF) == i)
    {
      encoding = new byte[2];
      encoding[0] = (byte) ((i >> 8) & 0xFF);
      encoding[1] = (byte) (i & 0xFF);
    }
    else if ((i & 0xFFFFFF) == i)
    {
      encoding = new byte[3];
      encoding[0] = (byte) ((i >> 16) & 0xFF);
      encoding[1] = (byte) ((i >> 8) & 0xFF);
      encoding[2] = (byte) (i & 0xFF);
    }
    else
    {
      encoding = new byte[4];
      encoding[0] = (byte) ((i >> 24) & 0xFF);
      encoding[1] = (byte) ((i >> 16) & 0xFF);
      encoding[2] = (byte) ((i >> 8) & 0xFF);
      encoding[3] = (byte) (i & 0xFF);
    }
    e = new ASN1Element(ASN1Constants.UNIVERSAL_ENUMERATED_TYPE, encoding);
    enumeratedElement = ASN1Enumerated.decodeAsEnumerated(e);
    assertEquals(i, enumeratedElement.intValue());
    e = new ASN1Element((byte) 0x50, encoding);
    enumeratedElement = ASN1Enumerated.decodeAsEnumerated(e);
    assertEquals(i, enumeratedElement.intValue());
  }
  /**
   * Tests the <CODE>decodeAsEnumerated</CODE> method that takes an ASN1Element
   * arguent using a valid value.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test(expectedExceptions = { ASN1Exception.class })
  public void testDecodeNullElementAsEnumerated()
         throws Exception
  {
    ASN1Element e = null;
    ASN1Enumerated.decodeAsEnumerated(e);
  }
  /**
   * Tests the <CODE>decodeAsEnumerated</CODE> method that takes an ASN1Element
   * arguent a zero-length element.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test(expectedExceptions = { ASN1Exception.class })
  public void testDecodeZeroLengthElementAsEnumerated()
         throws Exception
  {
    ASN1Element e = new ASN1Element(ASN1Constants.UNIVERSAL_ENUMERATED_TYPE);
    ASN1Enumerated.decodeAsEnumerated(e);
  }
  /**
   * Tests the <CODE>decodeAsEnumerated</CODE> method that takes an ASN1Element
   * arguent a long value element.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test(expectedExceptions = { ASN1Exception.class })
  public void testDecodeLongValueElementAsEnumerated()
         throws Exception
  {
    ASN1Element e = new ASN1Element(ASN1Constants.UNIVERSAL_ENUMERATED_TYPE,
                                    new byte[5]);
    ASN1Enumerated.decodeAsEnumerated(e);
  }
  /**
   * Tests the <CODE>decodeAsEnumerated</CODE> method that takes a byte array
   * with a valid array.
   *
   * @param i  The integer value to use in the test.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test(dataProvider = "intValues")
  public void testDecodeValidArrayAsEnumerated(int i)
         throws Exception
  {
    byte[] encoding;
    if ((i & 0xFF) == i)
    {
      encoding = new byte[1];
      encoding[0] = (byte) (i & 0xFF);
    }
    else if ((i & 0xFFFF) == i)
    {
      encoding = new byte[2];
      encoding[0] = (byte) ((i >> 8) & 0xFF);
      encoding[1] = (byte) (i & 0xFF);
    }
    else if ((i & 0xFFFFFF) == i)
    {
      encoding = new byte[3];
      encoding[0] = (byte) ((i >> 16) & 0xFF);
      encoding[1] = (byte) ((i >> 8) & 0xFF);
      encoding[2] = (byte) (i & 0xFF);
    }
    else
    {
      encoding = new byte[4];
      encoding[0] = (byte) ((i >> 24) & 0xFF);
      encoding[1] = (byte) ((i >> 16) & 0xFF);
      encoding[2] = (byte) ((i >> 8) & 0xFF);
      encoding[3] = (byte) (i & 0xFF);
    }
    byte[] encodedElement = new byte[2 + encoding.length];
    encodedElement[0] = ASN1Constants.UNIVERSAL_ENUMERATED_TYPE;
    encodedElement[1] = (byte) encoding.length;
    System.arraycopy(encoding, 0, encodedElement, 2, encoding.length);
    ASN1Enumerated enumeratedElement =
         ASN1Enumerated.decodeAsEnumerated(encodedElement);
    assertEquals(i, enumeratedElement.intValue());
  }
  /**
   * Tests the <CODE>decodeAsEnumerated</CODE> method that takes a byte array
   * with a valid extended length array.
   *
   * @param i  The integer value to use in the test.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test(dataProvider = "intValues")
  public void testDecodeValidExtendedLengthArrayAsEnumerated(int i)
         throws Exception
  {
    byte[] encoding;
    if ((i & 0xFF) == i)
    {
      encoding = new byte[1];
      encoding[0] = (byte) (i & 0xFF);
    }
    else if ((i & 0xFFFF) == i)
    {
      encoding = new byte[2];
      encoding[0] = (byte) ((i >> 8) & 0xFF);
      encoding[1] = (byte) (i & 0xFF);
    }
    else if ((i & 0xFFFFFF) == i)
    {
      encoding = new byte[3];
      encoding[0] = (byte) ((i >> 16) & 0xFF);
      encoding[1] = (byte) ((i >> 8) & 0xFF);
      encoding[2] = (byte) (i & 0xFF);
    }
    else
    {
      encoding = new byte[4];
      encoding[0] = (byte) ((i >> 24) & 0xFF);
      encoding[1] = (byte) ((i >> 16) & 0xFF);
      encoding[2] = (byte) ((i >> 8) & 0xFF);
      encoding[3] = (byte) (i & 0xFF);
    }
    byte[] encodedElement = new byte[3 + encoding.length];
    encodedElement[0] = ASN1Constants.UNIVERSAL_ENUMERATED_TYPE;
    encodedElement[1] = (byte) 0x81;
    encodedElement[2] = (byte) encoding.length;
    System.arraycopy(encoding, 0, encodedElement, 3, encoding.length);
    ASN1Enumerated enumeratedElement =
         ASN1Enumerated.decodeAsEnumerated(encodedElement);
    assertEquals(i, enumeratedElement.intValue());
  }
  /**
   * Tests the <CODE>decodeAsEnumerated</CODE> method that takes a byte array
   * with a null array.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test(expectedExceptions = { ASN1Exception.class })
  public void testDecodeNullArrayAsEnumerated()
         throws Exception
  {
    byte[] b = null;
    ASN1Enumerated.decodeAsEnumerated(b);
  }
  /**
   * Tests the <CODE>decodeAsEnumerated</CODE> method that takes a byte array
   * with a short array.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test(expectedExceptions = { ASN1Exception.class })
  public void testDecodeShortArrayAsEnumerated()
         throws Exception
  {
    byte[] b = new byte[0];
    ASN1Enumerated.decodeAsEnumerated(b);
  }
  /**
   * Tests the <CODE>decodeAsEnumerated</CODE> method that takes a byte array
   * with a long length array.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test(expectedExceptions = { ASN1Exception.class })
  public void testDecodeLongLengthArrayAsEnumerated()
         throws Exception
  {
    byte[] b = { 0x02, (byte) 0x85, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00 };
    ASN1Enumerated.decodeAsEnumerated(b);
  }
  /**
   * Tests the <CODE>decodeAsEnumerated</CODE> method that takes a byte array
   * with a truncated length array.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test(expectedExceptions = { ASN1Exception.class })
  public void testDecodeTruncatedLengthArrayAsEnumerated()
         throws Exception
  {
    byte[] b = { 0x02, (byte) 0x82, 0x00 };
    ASN1Enumerated.decodeAsEnumerated(b);
  }
  /**
   * Tests the <CODE>decodeAsEnumerated</CODE> method that takes a byte array
   * with a length mismatch.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test(expectedExceptions = { ASN1Exception.class })
  public void testDecodeLengthMismatchArrayAsEnumerated()
         throws Exception
  {
    byte[] b = { 0x02, (byte) 0x81, 0x01 };
    ASN1Enumerated.decodeAsEnumerated(b);
  }
  /**
   * Tests the <CODE>decodeAsEnumerated</CODE> method that takes a byte array
   * with a value too long for an integer.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test(expectedExceptions = { ASN1Exception.class })
  public void testDecodeLongIntLengthArrayAsEnumerated()
         throws Exception
  {
    byte[] b = { 0x02, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00 };
    ASN1Enumerated.decodeAsEnumerated(b);
  }
  /**
   * Tests the first <CODE>toString</CODE> method that takes a string builder
   * argument.
   *
   * @throws Exception
   *           If the test failed unexpectedly.
   * @param  i  The integer value to use in the test.
   */
  @Test()
  public void testSetByteValue() throws Exception {
    ASN1Integer element = new ASN1Integer(0);
    int numIntegers = testIntegers.size();
    for (int i = 0; i < numIntegers; i++) {
      int intValue = testIntegers.get(i);
      byte[] encodedIntValue = testEncodedIntegers.get(i);
      element.setValue(encodedIntValue);
      assertEquals(intValue, element.intValue());
  @Test(dataProvider = "intValues")
  public void testToString1(int i)
  {
    new ASN1Enumerated(i).toString(new StringBuilder());
    }
  }
  /**
   * Tests the <CODE>decodeAsEnumerated</CODE> method that takes an
   * ASN.1 element argument.
   * Tests the second <CODE>toString</CODE> method that takes string builder and
   * integer arguments.
   *
   * @throws Exception
   *           If the test failed unexpectedly.
   * @param  i  The integer value to use in the test.
   */
  @Test()
  public void testDecodeElementAsEnumerated() throws Exception {
    int numIntegers = testIntegers.size();
    for (int i = 0; i < numIntegers; i++) {
      int intValue = testIntegers.get(i);
      byte[] encodedIntValue = testEncodedIntegers.get(i);
      ASN1Element element = new ASN1Element((byte) 0x00, encodedIntValue);
      assertEquals(intValue, ASN1Enumerated.decodeAsEnumerated(element)
          .intValue());
      element = new ASN1Element((byte) 0x0A, encodedIntValue);
      assertEquals(intValue, ASN1Enumerated.decodeAsEnumerated(element)
          .intValue());
  @Test(dataProvider = "intValues")
  public void testToString2(int i)
  {
    new ASN1Enumerated(i).toString(new StringBuilder(), 1);
    }
  }
  /**
   * Tests the <CODE>decodeAsEnumerated</CODE> method that takes a
   * byte array argument.
   *
   * @throws Exception
   *           If the test failed unexpectedly.
   */
  @Test()
  public void testDecodeBytesAsEnumerated() throws Exception {
    int numIntegers = testIntegers.size();
    for (int i = 0; i < numIntegers; i++) {
      int intValue = testIntegers.get(i);
      byte[] encodedIntValue = testEncodedIntegers.get(i);
      byte[] encodedLength = ASN1Element
          .encodeLength(encodedIntValue.length);
      byte[] encodedElement = new byte[1 + encodedLength.length
          + encodedIntValue.length];
      encodedElement[0] = (byte) 0x00;
      System.arraycopy(encodedLength, 0, encodedElement, 1,
          encodedLength.length);
      System.arraycopy(encodedIntValue, 0, encodedElement,
          1 + encodedLength.length, encodedIntValue.length);
      assertEquals(intValue, ASN1Enumerated.decodeAsEnumerated(
          encodedElement).intValue());
      encodedElement[0] = (byte) 0x0A;
      assertEquals(intValue, ASN1Enumerated.decodeAsEnumerated(
          encodedElement).intValue());
    }
  }
}
opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/protocols/asn1/TestASN1Exception.java
@@ -42,7 +42,7 @@
       extends ASN1TestCase
{
  /**
   * Tests the first construtor, which takes integer and string arguments.
   * Tests the first constructor, which takes integer and string arguments.
   */
  @Test()
  public void testConstructor1()
@@ -53,7 +53,7 @@
  /**
   * Tests the first construtor, which takes integer, string, and throwable
   * Tests the second constructor, which takes integer, string, and throwable
   * arguments.
   */
  @Test()
opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/protocols/asn1/TestASN1Integer.java
@@ -26,194 +26,565 @@
 */
package org.opends.server.protocols.asn1;
import static org.testng.AssertJUnit.assertEquals;
import static org.testng.AssertJUnit.assertTrue;
import java.util.ArrayList;
import java.util.Arrays;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeClass;
import static org.testng.Assert.*;
/**
 * This class defines a set of tests for the
 * org.opends.server.protocols.asn1.ASN1Integer class.
 */
public class TestASN1Integer extends ASN1TestCase {
  // The set of encoded values for the test integers.
  private ArrayList<byte[]> testEncodedIntegers;
public class TestASN1Integer
       extends ASN1TestCase
{
  /**
   * Retrieves the set of int values that should be used for testing.
   *
   * @return  The set of int values that should be used for testing.
   */
  @DataProvider(name = "intValues")
  public Object[][] getIntValues()
  {
    return new Object[][]
    {
      new Object[] { 0x00000000 },
      new Object[] { 0x00000001 },
      new Object[] { 0x0000000F },
      new Object[] { 0x00000010 },
      new Object[] { 0x0000007F },
      new Object[] { 0x00000080 },
      new Object[] { 0x000000FF },
      new Object[] { 0x00000100 },
      new Object[] { 0x00000FFF },
      new Object[] { 0x00001000 },
      new Object[] { 0x0000FFFF },
      new Object[] { 0x00010000 },
      new Object[] { 0x000FFFFF },
      new Object[] { 0x00100000 },
      new Object[] { 0x00FFFFFF },
      new Object[] { 0x01000000 },
      new Object[] { 0x0FFFFFFF },
      new Object[] { 0x10000000 },
      new Object[] { 0x7FFFFFFF },
      new Object[] { -0x00000001 },
      new Object[] { -0x0000000F },
      new Object[] { -0x00000010 },
      new Object[] { -0x0000007F },
      new Object[] { -0x00000080 },
      new Object[] { -0x000000FF },
      new Object[] { -0x00000100 },
      new Object[] { -0x00000FFF },
      new Object[] { -0x00001000 },
      new Object[] { -0x0000FFFF },
      new Object[] { -0x00010000 },
      new Object[] { -0x000FFFFF },
      new Object[] { -0x00100000 },
      new Object[] { -0x00FFFFFF },
      new Object[] { -0x01000000 },
      new Object[] { -0x0FFFFFFF },
      new Object[] { -0x10000000 },
      new Object[] { -0x7FFFFFFF },
      new Object[] { 0x80000000 }
    };
  }
  // The set of integer values to use in test cases.
  private ArrayList<Integer> testIntegers;
  /**
   * Performs any necessary initialization for this test case.
   * Tests the first constructor, which takes a single integer argument.
   *
   * @param  i  The integer value to use to create the element.
   */
  @BeforeClass
  public void setUp() {
    testIntegers = new ArrayList<Integer>();
    testEncodedIntegers = new ArrayList<byte[]>();
    // Add all values that can be encoded using a single byte.
    for (int i = 0; i < 128; i++) {
      testIntegers.add(i);
      testEncodedIntegers.add(new byte[] { (byte) (i & 0xFF) });
  @Test(dataProvider = "intValues")
  public void testConstructor1(int i)
  {
    new ASN1Integer(i);
    }
    testIntegers.add(0x80); // The smallest 2-byte encoding.
    testEncodedIntegers.add(new byte[] { (byte) 0x00, (byte) 0x80 });
    testIntegers.add(0xFF); // A boundary case for 2-byte encoding.
    testEncodedIntegers.add(new byte[] { (byte) 0x00, (byte) 0xFF });
    testIntegers.add(0x0100); // A boundary case for 2-byte encoding.
    testEncodedIntegers.add(new byte[] { (byte) 0x01, (byte) 0x00 });
    testIntegers.add(0x7FFF); // The largest 2-byte encoding.
    testEncodedIntegers.add(new byte[] { (byte) 0x7F, (byte) 0xFF });
    testIntegers.add(0x8000); // The smallest 3-byte encoding.
    testEncodedIntegers.add(new byte[] { (byte) 0x00, (byte) 0x80,
        (byte) 0x00 });
    testIntegers.add(0xFFFF); // A boundary case for 3-byte encoding.
    testEncodedIntegers.add(new byte[] { (byte) 0x00, (byte) 0xFF,
        (byte) 0xFF });
    testIntegers.add(0x010000); // A boundary case for 3-byte encoding.
    testEncodedIntegers.add(new byte[] { (byte) 0x01, (byte) 0x00,
        (byte) 0x00 });
    testIntegers.add(0x7FFFFF); // The largest 3-byte encoding.
    testEncodedIntegers.add(new byte[] { (byte) 0x7F, (byte) 0xFF,
        (byte) 0xFF });
    testIntegers.add(0x800000); // The smallest 4-byte encoding.
    testEncodedIntegers.add(new byte[] { (byte) 0x00, (byte) 0x80,
        (byte) 0x00, (byte) 0x00 });
    testIntegers.add(0xFFFFFF); // A boundary case for 4-byte encoding.
    testEncodedIntegers.add(new byte[] { (byte) 0x00, (byte) 0xFF,
        (byte) 0xFF, (byte) 0xFF });
    testIntegers.add(0x01000000); // A boundary case for 4-byte
    // encoding.
    testEncodedIntegers.add(new byte[] { (byte) 0x01, (byte) 0x00,
        (byte) 0x00, (byte) 0x00 });
    testIntegers.add(0x7FFFFFFF); // The largest value we will allow.
    testEncodedIntegers.add(new byte[] { (byte) 0x7F, (byte) 0xFF,
        (byte) 0xFF, (byte) 0xFF });
  /**
   * Tests the second constructor, which takes byte and integer arguments.
   *
   * @param  i  The integer value to use to create the element.
   */
  @Test(dataProvider = "intValues")
  public void testConstructor2(int i)
  {
    new ASN1Integer((byte) 0x50, i);
  }
  /**
   * Tests the <CODE>intValue</CODE> method.
   *
   * @param  i  The integer value to use for the test.
   */
  @Test()
  public void testIntValue() {
    for (int i : testIntegers) {
      ASN1Integer element = new ASN1Integer(i);
      assertEquals(i, element.intValue());
  @Test(dataProvider = "intValues")
  public void testIntValue(int i)
  {
    assertEquals(i, new ASN1Integer(i).intValue());
    }
  }
  /**
   * Tests the <CODE>setValue</CODE> method that takes an int
   * argument.
   * Tests the <CODE>setValue</CODE> method that takes an int argument.
   *
   * @param  i  The integer value to use for the test.
   */
  @Test()
  public void testSetIntValue() {
    ASN1Integer element = new ASN1Integer(0);
    int numIntegers = testIntegers.size();
    for (int i = 0; i < numIntegers; i++) {
      int intValue = testIntegers.get(i);
      byte[] encodedIntValue = testEncodedIntegers.get(i);
      element.setValue(intValue);
      assertEquals(intValue, element.intValue());
      assertTrue(Arrays.equals(encodedIntValue, element.value()));
  @Test(dataProvider = "intValues")
  public void testSetIntValue(int i)
  {
    ASN1Integer integerElement = new ASN1Integer(0);
    integerElement.setValue(i);
    assertEquals(i, integerElement.intValue());
    }
  }
  /**
   * Tests the <CODE>setValue</CODE> method that takes a byte array
   * Tests the <CODE>setValue</CODE> method that takes a byte array argument
   * with a valid array.
   *
   * @param  i  The integer value to use for the test.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test(dataProvider = "intValues")
  public void testSetByteValue(int i)
         throws Exception
  {
    ASN1Integer integerElement = new ASN1Integer(0);
    byte[] encoding;
    if ((i & 0x7F) == i)
    {
      encoding = new byte[1];
      encoding[0] = (byte) (i & 0xFF);
    }
    else if ((i & 0x7FFF) == i)
    {
      encoding = new byte[2];
      encoding[0] = (byte) ((i >> 8) & 0xFF);
      encoding[1] = (byte) (i & 0xFF);
    }
    else if ((i & 0x7FFFFF) == i)
    {
      encoding = new byte[3];
      encoding[0] = (byte) ((i >> 16) & 0xFF);
      encoding[1] = (byte) ((i >> 8) & 0xFF);
      encoding[2] = (byte) (i & 0xFF);
    }
    else
    {
      encoding = new byte[4];
      encoding[0] = (byte) ((i >> 24) & 0xFF);
      encoding[1] = (byte) ((i >> 16) & 0xFF);
      encoding[2] = (byte) ((i >> 8) & 0xFF);
      encoding[3] = (byte) (i & 0xFF);
    }
    integerElement.setValue(encoding);
    assertEquals(i, integerElement.intValue());
  }
  /**
   * Tests the <CODE>setValue</CODE> method that takes a byte array argument
   * with a null array.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test(expectedExceptions = { ASN1Exception.class })
  public void testSetByteValueNull()
         throws Exception
  {
    ASN1Integer integerElement = new ASN1Integer(0);
    byte[] b = null;
    integerElement.setValue(b);
  }
  /**
   * Tests the <CODE>setValue</CODE> method that takes a byte array argument
   * with an empty array.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test(expectedExceptions = { ASN1Exception.class })
  public void testSetByteValueEmptyArray()
         throws Exception
  {
    ASN1Integer integerElement = new ASN1Integer(0);
    byte[] b = new byte[0];
    integerElement.setValue(b);
  }
  /**
   * Tests the <CODE>setValue</CODE> method that takes a byte array argument
   * with a long array.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test(expectedExceptions = { ASN1Exception.class })
  public void testSetByteValueLongArray()
         throws Exception
  {
    ASN1Integer integerElement = new ASN1Integer(0);
    byte[] b = new byte[5];
    integerElement.setValue(b);
  }
  /**
   * Tests the <CODE>decodeAsInteger</CODE> method that takes an ASN1Element
   * arguent using a valid value.
   *
   * @param i  The integer value to use in the test.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test(dataProvider = "intValues")
  public void testDecodeValidElementAsInteger(int i)
         throws Exception
  {
    // First, make sure that we can decode an integer element as an integer.
    ASN1Element e = new ASN1Integer(i);
    ASN1Integer integerElement = ASN1Integer.decodeAsInteger(e);
    assertEquals(i, integerElement.intValue());
    e = new ASN1Integer((byte) 0x50, i);
    integerElement = ASN1Integer.decodeAsInteger(e);
    assertEquals(i, integerElement.intValue());
    // Next, make sure that we can decode a generic element as an integer.
    byte[] encoding;
    if ((i & 0x7F) == i)
    {
      encoding = new byte[1];
      encoding[0] = (byte) (i & 0xFF);
    }
    else if ((i & 0x7FFF) == i)
    {
      encoding = new byte[2];
      encoding[0] = (byte) ((i >> 8) & 0xFF);
      encoding[1] = (byte) (i & 0xFF);
    }
    else if ((i & 0x7FFFFF) == i)
    {
      encoding = new byte[3];
      encoding[0] = (byte) ((i >> 16) & 0xFF);
      encoding[1] = (byte) ((i >> 8) & 0xFF);
      encoding[2] = (byte) (i & 0xFF);
    }
    else
    {
      encoding = new byte[4];
      encoding[0] = (byte) ((i >> 24) & 0xFF);
      encoding[1] = (byte) ((i >> 16) & 0xFF);
      encoding[2] = (byte) ((i >> 8) & 0xFF);
      encoding[3] = (byte) (i & 0xFF);
    }
    e = new ASN1Element(ASN1Constants.UNIVERSAL_INTEGER_TYPE, encoding);
    integerElement = ASN1Integer.decodeAsInteger(e);
    assertEquals(i, integerElement.intValue());
    e = new ASN1Element((byte) 0x50, encoding);
    integerElement = ASN1Integer.decodeAsInteger(e);
    assertEquals(i, integerElement.intValue());
  }
  /**
   * Tests the <CODE>decodeAsInteger</CODE> method that takes an ASN1Element
   * arguent using a valid value.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test(expectedExceptions = { ASN1Exception.class })
  public void testDecodeNullElementAsInteger()
         throws Exception
  {
    ASN1Element e = null;
    ASN1Integer.decodeAsInteger(e);
  }
  /**
   * Tests the <CODE>decodeAsInteger</CODE> method that takes an ASN1Element
   * arguent a zero-length element.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test(expectedExceptions = { ASN1Exception.class })
  public void testDecodeZeroLengthElementAsInteger()
         throws Exception
  {
    ASN1Element e = new ASN1Element(ASN1Constants.UNIVERSAL_INTEGER_TYPE);
    ASN1Integer.decodeAsInteger(e);
  }
  /**
   * Tests the <CODE>decodeAsInteger</CODE> method that takes an ASN1Element
   * arguent a long value element.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test(expectedExceptions = { ASN1Exception.class })
  public void testDecodeLongValueElementAsInteger()
         throws Exception
  {
    ASN1Element e = new ASN1Element(ASN1Constants.UNIVERSAL_INTEGER_TYPE,
                                    new byte[5]);
    ASN1Integer.decodeAsInteger(e);
  }
  /**
   * Tests the <CODE>decodeAsInteger</CODE> method that takes a byte array with
   * a valid array.
   *
   * @param i  The integer value to use in the test.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test(dataProvider = "intValues")
  public void testDecodeValidArrayAsInteger(int i)
         throws Exception
  {
    byte[] encoding;
    if ((i & 0x7F) == i)
    {
      encoding = new byte[1];
      encoding[0] = (byte) (i & 0xFF);
    }
    else if ((i & 0x7FFF) == i)
    {
      encoding = new byte[2];
      encoding[0] = (byte) ((i >> 8) & 0xFF);
      encoding[1] = (byte) (i & 0xFF);
    }
    else if ((i & 0x7FFFFF) == i)
    {
      encoding = new byte[3];
      encoding[0] = (byte) ((i >> 16) & 0xFF);
      encoding[1] = (byte) ((i >> 8) & 0xFF);
      encoding[2] = (byte) (i & 0xFF);
    }
    else
    {
      encoding = new byte[4];
      encoding[0] = (byte) ((i >> 24) & 0xFF);
      encoding[1] = (byte) ((i >> 16) & 0xFF);
      encoding[2] = (byte) ((i >> 8) & 0xFF);
      encoding[3] = (byte) (i & 0xFF);
    }
    byte[] encodedElement = new byte[2 + encoding.length];
    encodedElement[0] = ASN1Constants.UNIVERSAL_INTEGER_TYPE;
    encodedElement[1] = (byte) encoding.length;
    System.arraycopy(encoding, 0, encodedElement, 2, encoding.length);
    ASN1Integer integerElement = ASN1Integer.decodeAsInteger(encodedElement);
    assertEquals(i, integerElement.intValue());
  }
  /**
   * Tests the <CODE>decodeAsInteger</CODE> method that takes a byte array with
   * a valid extended length array.
   *
   * @param i  The integer value to use in the test.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test(dataProvider = "intValues")
  public void testDecodeValidExtendedLengthArrayAsInteger(int i)
         throws Exception
  {
    byte[] encoding;
    if ((i & 0x7F) == i)
    {
      encoding = new byte[1];
      encoding[0] = (byte) (i & 0xFF);
    }
    else if ((i & 0x7FFF) == i)
    {
      encoding = new byte[2];
      encoding[0] = (byte) ((i >> 8) & 0xFF);
      encoding[1] = (byte) (i & 0xFF);
    }
    else if ((i & 0x7FFFFF) == i)
    {
      encoding = new byte[3];
      encoding[0] = (byte) ((i >> 16) & 0xFF);
      encoding[1] = (byte) ((i >> 8) & 0xFF);
      encoding[2] = (byte) (i & 0xFF);
    }
    else
    {
      encoding = new byte[4];
      encoding[0] = (byte) ((i >> 24) & 0xFF);
      encoding[1] = (byte) ((i >> 16) & 0xFF);
      encoding[2] = (byte) ((i >> 8) & 0xFF);
      encoding[3] = (byte) (i & 0xFF);
    }
    byte[] encodedElement = new byte[3 + encoding.length];
    encodedElement[0] = ASN1Constants.UNIVERSAL_INTEGER_TYPE;
    encodedElement[1] = (byte) 0x81;
    encodedElement[2] = (byte) encoding.length;
    System.arraycopy(encoding, 0, encodedElement, 3, encoding.length);
    ASN1Integer integerElement = ASN1Integer.decodeAsInteger(encodedElement);
    assertEquals(i, integerElement.intValue());
  }
  /**
   * Tests the <CODE>decodeAsInteger</CODE> method that takes a byte array with
   * a null array.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test(expectedExceptions = { ASN1Exception.class })
  public void testDecodeNullArrayAsInteger()
         throws Exception
  {
    byte[] b = null;
    ASN1Integer.decodeAsInteger(b);
  }
  /**
   * Tests the <CODE>decodeAsInteger</CODE> method that takes a byte array with
   * a short array.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test(expectedExceptions = { ASN1Exception.class })
  public void testDecodeShortArrayAsInteger()
         throws Exception
  {
    byte[] b = new byte[0];
    ASN1Integer.decodeAsInteger(b);
  }
  /**
   * Tests the <CODE>decodeAsInteger</CODE> method that takes a byte array with
   * a long length array.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test(expectedExceptions = { ASN1Exception.class })
  public void testDecodeLongLengthArrayAsInteger()
         throws Exception
  {
    byte[] b = { 0x02, (byte) 0x85, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00 };
    ASN1Integer.decodeAsInteger(b);
  }
  /**
   * Tests the <CODE>decodeAsInteger</CODE> method that takes a byte array with
   * a truncated length array.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test(expectedExceptions = { ASN1Exception.class })
  public void testDecodeTruncatedLengthArrayAsInteger()
         throws Exception
  {
    byte[] b = { 0x02, (byte) 0x82, 0x00 };
    ASN1Integer.decodeAsInteger(b);
  }
  /**
   * Tests the <CODE>decodeAsInteger</CODE> method that takes a byte array with
   * a length mismatch.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test(expectedExceptions = { ASN1Exception.class })
  public void testDecodeLengthMismatchArrayAsInteger()
         throws Exception
  {
    byte[] b = { 0x02, (byte) 0x81, 0x01 };
    ASN1Integer.decodeAsInteger(b);
  }
  /**
   * Tests the <CODE>decodeAsInteger</CODE> method that takes a byte array with
   * a value too long for an integer.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test(expectedExceptions = { ASN1Exception.class })
  public void testDecodeLongIntLengthArrayAsInteger()
         throws Exception
  {
    byte[] b = { 0x02, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00 };
    ASN1Integer.decodeAsInteger(b);
  }
  /**
   * Tests the first <CODE>toString</CODE> method that takes a string builder
   * argument.
   *
   * @throws Exception
   *           If the test failed unexpectedly.
   * @param  i  The integer value to use in the test.
   */
  @Test()
  public void testSetByteValue() throws Exception {
    ASN1Integer element = new ASN1Integer(0);
    int numIntegers = testIntegers.size();
    for (int i = 0; i < numIntegers; i++) {
      int intValue = testIntegers.get(i);
      byte[] encodedIntValue = testEncodedIntegers.get(i);
      element.setValue(encodedIntValue);
      assertEquals(intValue, element.intValue());
  @Test(dataProvider = "intValues")
  public void testToString1(int i)
  {
    new ASN1Integer(i).toString(new StringBuilder());
    }
  }
  /**
   * Tests the <CODE>decodeAsInteger</CODE> method that takes an ASN.1
   * element argument.
   * Tests the second <CODE>toString</CODE> method that takes string builder and
   * integer arguments.
   *
   * @throws Exception
   *           If the test failed unexpectedly.
   * @param  i  The integer value to use in the test.
   */
  @Test()
  public void testDecodeElementAsInteger() throws Exception {
    int numIntegers = testIntegers.size();
    for (int i = 0; i < numIntegers; i++) {
      int intValue = testIntegers.get(i);
      byte[] encodedIntValue = testEncodedIntegers.get(i);
      ASN1Element element = new ASN1Element((byte) 0x00, encodedIntValue);
      assertEquals(intValue, ASN1Integer.decodeAsInteger(element)
          .intValue());
      element = new ASN1Element((byte) 0x02, encodedIntValue);
      assertEquals(intValue, ASN1Integer.decodeAsInteger(element)
          .intValue());
  @Test(dataProvider = "intValues")
  public void testToString2(int i)
  {
    new ASN1Integer(i).toString(new StringBuilder(), 1);
    }
  }
  /**
   * Tests the <CODE>decodeAsInteger</CODE> method that takes a byte
   * array argument.
   *
   * @throws Exception
   *           If the test failed unexpectedly.
   */
  @Test()
  public void testDecodeBytesAsInteger() throws Exception {
    int numIntegers = testIntegers.size();
    for (int i = 0; i < numIntegers; i++) {
      int intValue = testIntegers.get(i);
      byte[] encodedIntValue = testEncodedIntegers.get(i);
      byte[] encodedLength = ASN1Element
          .encodeLength(encodedIntValue.length);
      byte[] encodedElement = new byte[1 + encodedLength.length
          + encodedIntValue.length];
      encodedElement[0] = (byte) 0x00;
      System.arraycopy(encodedLength, 0, encodedElement, 1,
          encodedLength.length);
      System.arraycopy(encodedIntValue, 0, encodedElement,
          1 + encodedLength.length, encodedIntValue.length);
      assertEquals(intValue, ASN1Integer.decodeAsInteger(encodedElement)
          .intValue());
      encodedElement[0] = (byte) 0x02;
      assertEquals(intValue, ASN1Integer.decodeAsInteger(encodedElement)
          .intValue());
    }
  }
}
opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/protocols/asn1/TestASN1Long.java
@@ -26,275 +26,734 @@
 */
package org.opends.server.protocols.asn1;
import static org.testng.AssertJUnit.assertEquals;
import static org.testng.AssertJUnit.assertTrue;
import java.util.ArrayList;
import java.util.Arrays;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeClass;
import static org.testng.Assert.*;
/**
 * This class defines a set of tests for the
 * org.opends.server.protocols.asn1.ASN1Long class.
 */
public class TestASN1Long extends ASN1TestCase {
  // The set of encoded values for the test longs.
  private ArrayList<byte[]> testEncodedLongs;
public class TestASN1Long
       extends ASN1TestCase
{
  /**
   * Retrieves the set of long values that should be used for testing.
   *
   * @return  The set of long values that should be used for testing.
   */
  @DataProvider(name = "longValues")
  public Object[][] getLongValues()
  {
    return new Object[][]
    {
      new Object[] { 0x0000000000000000L },
      new Object[] { 0x0000000000000001L },
      new Object[] { 0x000000000000007FL },
      new Object[] { 0x0000000000000080L },
      new Object[] { 0x00000000000000FFL },
      new Object[] { 0x0000000000000100L },
      new Object[] { 0x000000000000FFFFL },
      new Object[] { 0x0000000000010000L },
      new Object[] { 0x0000000000FFFFFFL },
      new Object[] { 0x0000000001000000L },
      new Object[] { 0x00000000FFFFFFFFL },
      new Object[] { 0x0000000100000000L },
      new Object[] { 0x000000FFFFFFFFFFL },
      new Object[] { 0x0000010000000000L },
      new Object[] { 0x0000FFFFFFFFFFFFL },
      new Object[] { 0x0001000000000000L },
      new Object[] { 0x00FFFFFFFFFFFFFFL },
      new Object[] { 0x0100000000000000L },
      new Object[] { 0x7FFFFFFFFFFFFFFFL },
      new Object[] { -0x0000000000000001L },
      new Object[] { -0x000000000000007FL },
      new Object[] { -0x0000000000000080L },
      new Object[] { -0x00000000000000FFL },
      new Object[] { -0x0000000000000100L },
      new Object[] { -0x000000000000FFFFL },
      new Object[] { -0x0000000000010000L },
      new Object[] { -0x0000000000FFFFFFL },
      new Object[] { -0x0000000001000000L },
      new Object[] { -0x00000000FFFFFFFFL },
      new Object[] { -0x0000000100000000L },
      new Object[] { -0x000000FFFFFFFFFFL },
      new Object[] { -0x0000010000000000L },
      new Object[] { -0x0000FFFFFFFFFFFFL },
      new Object[] { -0x0001000000000000L },
      new Object[] { -0x00FFFFFFFFFFFFFFL },
      new Object[] { -0x0100000000000000L },
      new Object[] { -0x7FFFFFFFFFFFFFFFL },
      new Object[] { 0x8000000000000000L }
    };
  }
  // The set of long values to use in test cases.
  private ArrayList<Long> testLongs;
  /**
   * Performs any necessary initialization for this test case.
   * Tests the first constructor, which takes a single long argument.
   *
   * @param  l  The long value to use to create the element.
   */
  @BeforeClass
  public void setUp() {
    testLongs = new ArrayList<Long>();
    testEncodedLongs = new ArrayList<byte[]>();
    // Add all values that can be encoded using a single byte.
    for (int i = 0; i < 128; i++) {
      testLongs.add(new Long(i));
      testEncodedLongs.add(new byte[] { (byte) (i & 0xFF) });
  @Test(dataProvider = "longValues")
  public void testConstructor1(long l)
  {
    new ASN1Long(l);
    }
    testLongs.add(new Long(0x80)); // The smallest 2-byte encoding.
    testEncodedLongs.add(new byte[] { (byte) 0x00, (byte) 0x80 });
    testLongs.add(new Long(0xFF)); // A boundary case for 2-byte
    // encoding.
    testEncodedLongs.add(new byte[] { (byte) 0x00, (byte) 0xFF });
    testLongs.add(new Long(0x0100)); // A boundary case for 2-byte
    // encoding.
    testEncodedLongs.add(new byte[] { (byte) 0x01, (byte) 0x00 });
    testLongs.add(new Long(0x7FFF)); // The largest 2-byte encoding.
    testEncodedLongs.add(new byte[] { (byte) 0x7F, (byte) 0xFF });
    testLongs.add(new Long(0x8000)); // The smallest 3-byte encoding.
    testEncodedLongs
        .add(new byte[] { (byte) 0x00, (byte) 0x80, (byte) 0x00 });
    testLongs.add(new Long(0xFFFF)); // A boundary case for 3-byte
    // encoding.
    testEncodedLongs
        .add(new byte[] { (byte) 0x00, (byte) 0xFF, (byte) 0xFF });
    testLongs.add(new Long(0x010000)); // A boundary case for 3-byte
    // encoding.
    testEncodedLongs
        .add(new byte[] { (byte) 0x01, (byte) 0x00, (byte) 0x00 });
    testLongs.add(new Long(0x7FFFFF)); // The largest 3-byte encoding.
    testEncodedLongs
        .add(new byte[] { (byte) 0x7F, (byte) 0xFF, (byte) 0xFF });
    testLongs.add(new Long(0x800000)); // The smallest 4-byte encoding.
    testEncodedLongs.add(new byte[] { (byte) 0x00, (byte) 0x80,
        (byte) 0x00, (byte) 0x00 });
    testLongs.add(new Long(0xFFFFFF)); // A boundary case for 4-byte
    // encoding.
    testEncodedLongs.add(new byte[] { (byte) 0x00, (byte) 0xFF,
        (byte) 0xFF, (byte) 0xFF });
    testLongs.add(new Long(0x01000000)); // A boundary case for 4-byte
    // encoding.
    testEncodedLongs.add(new byte[] { (byte) 0x01, (byte) 0x00,
        (byte) 0x00, (byte) 0x00 });
    testLongs.add(new Long(0x7FFFFFFF)); // The largest 4-byte
    // encoding.
    testEncodedLongs.add(new byte[] { (byte) 0x7F, (byte) 0xFF,
        (byte) 0xFF, (byte) 0xFF });
    testLongs.add(0x80000000L); // The smallest 5-byte encoding.
    testEncodedLongs.add(new byte[] { (byte) 0x00, (byte) 0x80,
        (byte) 0x00, (byte) 0x00, (byte) 0x00 });
    testLongs.add(0xFFFFFFFFL); // A boundary case for 5-byte encoding.
    testEncodedLongs.add(new byte[] { (byte) 0x00, (byte) 0xFF,
        (byte) 0xFF, (byte) 0xFF, (byte) 0xFF });
    testLongs.add(0x0100000000L); // A boundary case for 5-byte
    // encoding.
    testEncodedLongs.add(new byte[] { (byte) 0x01, (byte) 0x00,
        (byte) 0x00, (byte) 0x00, (byte) 0x00 });
    testLongs.add(0x07FFFFFFFFL); // The largest 5-byte encoding.
    testEncodedLongs.add(new byte[] { (byte) 0x07, (byte) 0xFF,
        (byte) 0xFF, (byte) 0xFF, (byte) 0xFF });
    testLongs.add(0x8000000000L); // The smallest 6-byte encoding.
    testEncodedLongs.add(new byte[] { (byte) 0x00, (byte) 0x80,
        (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00 });
    testLongs.add(0xFFFFFFFFFFL); // A boundary case for 6-byte
    // encoding.
    testEncodedLongs.add(new byte[] { (byte) 0x00, (byte) 0xFF,
        (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF });
    testLongs.add(0x010000000000L); // A boundary case for 6-byte
    // encoding.
    testEncodedLongs.add(new byte[] { (byte) 0x01, (byte) 0x00,
        (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00 });
    testLongs.add(0x07FFFFFFFFFFL); // The largest 6-byte encoding.
    testEncodedLongs.add(new byte[] { (byte) 0x07, (byte) 0xFF,
        (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF });
    testLongs.add(0x800000000000L); // The smallest 7-byte encoding.
    testEncodedLongs.add(new byte[] { (byte) 0x00, (byte) 0x80,
        (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00 });
    testLongs.add(0xFFFFFFFFFFFFL); // A boundary case for 7-byte
    // encoding.
    testEncodedLongs.add(new byte[] { (byte) 0x00, (byte) 0xFF,
        (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF });
    testLongs.add(0x01000000000000L); // A boundary case for 7-byte
    // encoding.
    testEncodedLongs.add(new byte[] { (byte) 0x01, (byte) 0x00,
        (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00 });
    testLongs.add(0x07FFFFFFFFFFFFL); // The largest 7-byte encoding.
    testEncodedLongs.add(new byte[] { (byte) 0x07, (byte) 0xFF,
        (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF });
    testLongs.add(0x80000000000000L); // The smallest 8-byte encoding.
    testEncodedLongs.add(new byte[] { (byte) 0x00, (byte) 0x80,
        (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
        (byte) 0x00 });
    testLongs.add(0xFFFFFFFFFFFFFFL); // A boundary case for 8-byte
    // encoding.
    testEncodedLongs.add(new byte[] { (byte) 0x00, (byte) 0xFF,
        (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
        (byte) 0xFF });
    testLongs.add(0x0100000000000000L); // A boundary case for 8-byte
    // encoding.
    testEncodedLongs.add(new byte[] { (byte) 0x01, (byte) 0x00,
        (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
        (byte) 0x00 });
    testLongs.add(0x07FFFFFFFFFFFFFFL); // The largest 8-byte encoding.
    testEncodedLongs.add(new byte[] { (byte) 0x07, (byte) 0xFF,
        (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
        (byte) 0xFF });
  /**
   * Tests the second constructor, which takes byte and long arguments.
   *
   * @param  l  The long value to use to create the element.
   */
  @Test(dataProvider = "longValues")
  public void testConstructor2(long l)
  {
    new ASN1Long((byte) 0x50, l);
  }
  /**
   * Tests the <CODE>longValue</CODE> method.
   *
   * @param  l  The long value to use for the test.
   */
  @Test()
  public void testLongValue() {
    for (long l : testLongs) {
      ASN1Long element = new ASN1Long(l);
  @Test(dataProvider = "longValues")
  public void testLongValue(long l)
  {
    assertEquals(l, new ASN1Long(l).longValue());
  }
      assertEquals(l, element.longValue());
    }
  }
  /**
   * Tests the <CODE>setValue</CODE> method that takes a long
   * argument.
   * Tests the <CODE>setValue</CODE> method that takes a long argument.
   *
   * @param  l  The long value to use for the test.
   */
  @Test()
  public void testSetLongValue() {
    ASN1Long element = new ASN1Long(0);
    int numLongs = testLongs.size();
    for (int i = 0; i < numLongs; i++) {
      long longValue = testLongs.get(i);
      byte[] encodedLongValue = testEncodedLongs.get(i);
      element.setValue(longValue);
      assertEquals(longValue, element.longValue());
      assertTrue(Arrays.equals(encodedLongValue, element.value()));
  @Test(dataProvider = "longValues")
  public void testSetLongValue(long l)
  {
    ASN1Long longElement = new ASN1Long(0);
    longElement.setValue(l);
    assertEquals(l, longElement.longValue());
    }
  }
  /**
   * Tests the <CODE>setValue</CODE> method that takes a byte array
   * Tests the <CODE>setValue</CODE> method that takes a byte array argument
   * with a valid array.
   *
   * @param  l  The long value to use for the test.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test(dataProvider = "longValues")
  public void testSetByteValue(long l)
         throws Exception
  {
    ASN1Long longElement = new ASN1Long(0);
    byte[] encoding;
    if ((l & 0x7FL) == l)
    {
      encoding = new byte[1];
      encoding[0] = (byte) (l & 0xFF);
    }
    else if ((l & 0x7FFFL) == l)
    {
      encoding = new byte[2];
      encoding[0] = (byte) ((l >> 8) & 0xFF);
      encoding[1] = (byte) (l & 0xFF);
    }
    else if ((l & 0x7FFFFFL) == l)
    {
      encoding = new byte[3];
      encoding[0] = (byte) ((l >> 16) & 0xFF);
      encoding[1] = (byte) ((l >> 8) & 0xFF);
      encoding[2] = (byte) (l & 0xFF);
    }
    else if ((l & 0x7FFFFFFFL) == l)
    {
      encoding = new byte[4];
      encoding[0] = (byte) ((l >> 24) & 0xFF);
      encoding[1] = (byte) ((l >> 16) & 0xFF);
      encoding[2] = (byte) ((l >> 8) & 0xFF);
      encoding[3] = (byte) (l & 0xFF);
    }
    else if ((l & 0x7FFFFFFFFFL) == l)
    {
      encoding = new byte[5];
      encoding[0] = (byte) ((l >> 32) & 0xFF);
      encoding[1] = (byte) ((l >> 24) & 0xFF);
      encoding[2] = (byte) ((l >> 16) & 0xFF);
      encoding[3] = (byte) ((l >> 8) & 0xFF);
      encoding[4] = (byte) (l & 0xFF);
    }
    else if ((l & 0x7FFFFFFFFFFFL) == l)
    {
      encoding = new byte[6];
      encoding[0] = (byte) ((l >> 40) & 0xFF);
      encoding[1] = (byte) ((l >> 32) & 0xFF);
      encoding[2] = (byte) ((l >> 24) & 0xFF);
      encoding[3] = (byte) ((l >> 16) & 0xFF);
      encoding[4] = (byte) ((l >> 8) & 0xFF);
      encoding[5] = (byte) (l & 0xFF);
    }
    else if ((l & 0x7FFFFFFFFFFFFFL) == l)
    {
      encoding = new byte[7];
      encoding[0] = (byte) ((l >> 48) & 0xFF);
      encoding[1] = (byte) ((l >> 40) & 0xFF);
      encoding[2] = (byte) ((l >> 32) & 0xFF);
      encoding[3] = (byte) ((l >> 24) & 0xFF);
      encoding[4] = (byte) ((l >> 16) & 0xFF);
      encoding[5] = (byte) ((l >> 8) & 0xFF);
      encoding[6] = (byte) (l & 0xFF);
    }
    else
    {
      encoding = new byte[8];
      encoding[0] = (byte) ((l >> 56) & 0xFF);
      encoding[1] = (byte) ((l >> 48) & 0xFF);
      encoding[2] = (byte) ((l >> 40) & 0xFF);
      encoding[3] = (byte) ((l >> 32) & 0xFF);
      encoding[4] = (byte) ((l >> 24) & 0xFF);
      encoding[5] = (byte) ((l >> 16) & 0xFF);
      encoding[6] = (byte) ((l >> 8) & 0xFF);
      encoding[7] = (byte) (l & 0xFF);
    }
    longElement.setValue(encoding);
    assertEquals(l, longElement.longValue());
  }
  /**
   * Tests the <CODE>setValue</CODE> method that takes a byte array argument
   * with a null array.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test(expectedExceptions = { ASN1Exception.class })
  public void testSetByteValueNull()
         throws Exception
  {
    ASN1Long longElement = new ASN1Long(0);
    byte[] b = null;
    longElement.setValue(b);
  }
  /**
   * Tests the <CODE>setValue</CODE> method that takes a byte array argument
   * with an empty array.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test(expectedExceptions = { ASN1Exception.class })
  public void testSetByteValueEmptyArray()
         throws Exception
  {
    ASN1Long longElement = new ASN1Long(0);
    byte[] b = new byte[0];
    longElement.setValue(b);
  }
  /**
   * Tests the <CODE>setValue</CODE> method that takes a byte array argument
   * with a long array.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test(expectedExceptions = { ASN1Exception.class })
  public void testSetByteValueLongArray()
         throws Exception
  {
    ASN1Long longElement = new ASN1Long(0);
    byte[] b = new byte[9];
    longElement.setValue(b);
  }
  /**
   * Tests the <CODE>decodeAsLong</CODE> method that takes an ASN1Element
   * arguent using a valid value.
   *
   * @param  l  The long value to use in the test.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test(dataProvider = "longValues")
  public void testDecodeValidElementAsLong(long l)
         throws Exception
  {
    // First, make sure that we can decode a long element as a long.
    ASN1Element e = new ASN1Long(l);
    ASN1Long longElement = ASN1Long.decodeAsLong(e);
    assertEquals(l, longElement.longValue());
    e = new ASN1Long((byte) 0x50, l);
    longElement = ASN1Long.decodeAsLong(e);
    assertEquals(l, longElement.longValue());
    // Next, make sure that we can decode a generic element as a long.
    byte[] encoding;
    if ((l & 0x7FL) == l)
    {
      encoding = new byte[1];
      encoding[0] = (byte) (l & 0xFF);
    }
    else if ((l & 0x7FFFL) == l)
    {
      encoding = new byte[2];
      encoding[0] = (byte) ((l >> 8) & 0xFF);
      encoding[1] = (byte) (l & 0xFF);
    }
    else if ((l & 0x7FFFFFL) == l)
    {
      encoding = new byte[3];
      encoding[0] = (byte) ((l >> 16) & 0xFF);
      encoding[1] = (byte) ((l >> 8) & 0xFF);
      encoding[2] = (byte) (l & 0xFF);
    }
    else if ((l & 0x7FFFFFFFL) == l)
    {
      encoding = new byte[4];
      encoding[0] = (byte) ((l >> 24) & 0xFF);
      encoding[1] = (byte) ((l >> 16) & 0xFF);
      encoding[2] = (byte) ((l >> 8) & 0xFF);
      encoding[3] = (byte) (l & 0xFF);
    }
    else if ((l & 0x7FFFFFFFFFL) == l)
    {
      encoding = new byte[5];
      encoding[0] = (byte) ((l >> 32) & 0xFF);
      encoding[1] = (byte) ((l >> 24) & 0xFF);
      encoding[2] = (byte) ((l >> 16) & 0xFF);
      encoding[3] = (byte) ((l >> 8) & 0xFF);
      encoding[4] = (byte) (l & 0xFF);
    }
    else if ((l & 0x7FFFFFFFFFFFL) == l)
    {
      encoding = new byte[6];
      encoding[0] = (byte) ((l >> 40) & 0xFF);
      encoding[1] = (byte) ((l >> 32) & 0xFF);
      encoding[2] = (byte) ((l >> 24) & 0xFF);
      encoding[3] = (byte) ((l >> 16) & 0xFF);
      encoding[4] = (byte) ((l >> 8) & 0xFF);
      encoding[5] = (byte) (l & 0xFF);
    }
    else if ((l & 0x7FFFFFFFFFFFFFL) == l)
    {
      encoding = new byte[7];
      encoding[0] = (byte) ((l >> 48) & 0xFF);
      encoding[1] = (byte) ((l >> 40) & 0xFF);
      encoding[2] = (byte) ((l >> 32) & 0xFF);
      encoding[3] = (byte) ((l >> 24) & 0xFF);
      encoding[4] = (byte) ((l >> 16) & 0xFF);
      encoding[5] = (byte) ((l >> 8) & 0xFF);
      encoding[6] = (byte) (l & 0xFF);
    }
    else
    {
      encoding = new byte[8];
      encoding[0] = (byte) ((l >> 56) & 0xFF);
      encoding[1] = (byte) ((l >> 48) & 0xFF);
      encoding[2] = (byte) ((l >> 40) & 0xFF);
      encoding[3] = (byte) ((l >> 32) & 0xFF);
      encoding[4] = (byte) ((l >> 24) & 0xFF);
      encoding[5] = (byte) ((l >> 16) & 0xFF);
      encoding[6] = (byte) ((l >> 8) & 0xFF);
      encoding[7] = (byte) (l & 0xFF);
    }
    e = new ASN1Element(ASN1Constants.UNIVERSAL_INTEGER_TYPE, encoding);
    longElement = ASN1Long.decodeAsLong(e);
    assertEquals(l, longElement.longValue());
    e = new ASN1Element((byte) 0x50, encoding);
    longElement = ASN1Long.decodeAsLong(e);
    assertEquals(l, longElement.longValue());
  }
  /**
   * Tests the <CODE>decodeAsLong</CODE> method that takes an ASN1Element
   * arguent using a valid value.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test(expectedExceptions = { ASN1Exception.class })
  public void testDecodeNullElementAsLong()
         throws Exception
  {
    ASN1Element e = null;
    ASN1Long.decodeAsLong(e);
  }
  /**
   * Tests the <CODE>decodeAsLong</CODE> method that takes an ASN1Element
   * arguent a zero-length element.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test(expectedExceptions = { ASN1Exception.class })
  public void testDecodeZeroLengthElementAsLong()
         throws Exception
  {
    ASN1Element e = new ASN1Element(ASN1Constants.UNIVERSAL_INTEGER_TYPE);
    ASN1Long.decodeAsLong(e);
  }
  /**
   * Tests the <CODE>decodeAsLong</CODE> method that takes an ASN1Element
   * arguent a long value element.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test(expectedExceptions = { ASN1Exception.class })
  public void testDecodeLongValueElementAsLong()
         throws Exception
  {
    ASN1Element e = new ASN1Element(ASN1Constants.UNIVERSAL_INTEGER_TYPE,
                                    new byte[9]);
    ASN1Long.decodeAsLong(e);
  }
  /**
   * Tests the <CODE>decodeAsLong</CODE> method that takes a byte array with
   * a valid array.
   *
   * @param  l  The long value to use in the test.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test(dataProvider = "longValues")
  public void testDecodeValidArrayAsLong(long l)
         throws Exception
  {
    byte[] encoding;
    if ((l & 0x7FL) == l)
    {
      encoding = new byte[1];
      encoding[0] = (byte) (l & 0xFF);
    }
    else if ((l & 0x7FFFL) == l)
    {
      encoding = new byte[2];
      encoding[0] = (byte) ((l >> 8) & 0xFF);
      encoding[1] = (byte) (l & 0xFF);
    }
    else if ((l & 0x7FFFFFL) == l)
    {
      encoding = new byte[3];
      encoding[0] = (byte) ((l >> 16) & 0xFF);
      encoding[1] = (byte) ((l >> 8) & 0xFF);
      encoding[2] = (byte) (l & 0xFF);
    }
    else if ((l & 0x7FFFFFFFL) == l)
    {
      encoding = new byte[4];
      encoding[0] = (byte) ((l >> 24) & 0xFF);
      encoding[1] = (byte) ((l >> 16) & 0xFF);
      encoding[2] = (byte) ((l >> 8) & 0xFF);
      encoding[3] = (byte) (l & 0xFF);
    }
    else if ((l & 0x7FFFFFFFFFL) == l)
    {
      encoding = new byte[5];
      encoding[0] = (byte) ((l >> 32) & 0xFF);
      encoding[1] = (byte) ((l >> 24) & 0xFF);
      encoding[2] = (byte) ((l >> 16) & 0xFF);
      encoding[3] = (byte) ((l >> 8) & 0xFF);
      encoding[4] = (byte) (l & 0xFF);
    }
    else if ((l & 0x7FFFFFFFFFFFL) == l)
    {
      encoding = new byte[6];
      encoding[0] = (byte) ((l >> 40) & 0xFF);
      encoding[1] = (byte) ((l >> 32) & 0xFF);
      encoding[2] = (byte) ((l >> 24) & 0xFF);
      encoding[3] = (byte) ((l >> 16) & 0xFF);
      encoding[4] = (byte) ((l >> 8) & 0xFF);
      encoding[5] = (byte) (l & 0xFF);
    }
    else if ((l & 0x7FFFFFFFFFFFFFL) == l)
    {
      encoding = new byte[7];
      encoding[0] = (byte) ((l >> 48) & 0xFF);
      encoding[1] = (byte) ((l >> 40) & 0xFF);
      encoding[2] = (byte) ((l >> 32) & 0xFF);
      encoding[3] = (byte) ((l >> 24) & 0xFF);
      encoding[4] = (byte) ((l >> 16) & 0xFF);
      encoding[5] = (byte) ((l >> 8) & 0xFF);
      encoding[6] = (byte) (l & 0xFF);
    }
    else
    {
      encoding = new byte[8];
      encoding[0] = (byte) ((l >> 56) & 0xFF);
      encoding[1] = (byte) ((l >> 48) & 0xFF);
      encoding[2] = (byte) ((l >> 40) & 0xFF);
      encoding[3] = (byte) ((l >> 32) & 0xFF);
      encoding[4] = (byte) ((l >> 24) & 0xFF);
      encoding[5] = (byte) ((l >> 16) & 0xFF);
      encoding[6] = (byte) ((l >> 8) & 0xFF);
      encoding[7] = (byte) (l & 0xFF);
    }
    byte[] encodedElement = new byte[2 + encoding.length];
    encodedElement[0] = ASN1Constants.UNIVERSAL_INTEGER_TYPE;
    encodedElement[1] = (byte) encoding.length;
    System.arraycopy(encoding, 0, encodedElement, 2, encoding.length);
    ASN1Long longElement = ASN1Long.decodeAsLong(encodedElement);
    assertEquals(l, longElement.longValue());
  }
  /**
   * Tests the <CODE>decodeAsLong</CODE> method that takes a byte array with
   * a valid extended length array.
   *
   * @param  l  The long value to use in the test.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test(dataProvider = "longValues")
  public void testDecodeValidExtendedLengthArrayAsLong(long l)
         throws Exception
  {
    byte[] encoding;
    if ((l & 0x7FL) == l)
    {
      encoding = new byte[1];
      encoding[0] = (byte) (l & 0xFF);
    }
    else if ((l & 0x7FFFL) == l)
    {
      encoding = new byte[2];
      encoding[0] = (byte) ((l >> 8) & 0xFF);
      encoding[1] = (byte) (l & 0xFF);
    }
    else if ((l & 0x7FFFFFL) == l)
    {
      encoding = new byte[3];
      encoding[0] = (byte) ((l >> 16) & 0xFF);
      encoding[1] = (byte) ((l >> 8) & 0xFF);
      encoding[2] = (byte) (l & 0xFF);
    }
    else if ((l & 0x7FFFFFFFL) == l)
    {
      encoding = new byte[4];
      encoding[0] = (byte) ((l >> 24) & 0xFF);
      encoding[1] = (byte) ((l >> 16) & 0xFF);
      encoding[2] = (byte) ((l >> 8) & 0xFF);
      encoding[3] = (byte) (l & 0xFF);
    }
    else if ((l & 0x7FFFFFFFFFL) == l)
    {
      encoding = new byte[5];
      encoding[0] = (byte) ((l >> 32) & 0xFF);
      encoding[1] = (byte) ((l >> 24) & 0xFF);
      encoding[2] = (byte) ((l >> 16) & 0xFF);
      encoding[3] = (byte) ((l >> 8) & 0xFF);
      encoding[4] = (byte) (l & 0xFF);
    }
    else if ((l & 0x7FFFFFFFFFFFL) == l)
    {
      encoding = new byte[6];
      encoding[0] = (byte) ((l >> 40) & 0xFF);
      encoding[1] = (byte) ((l >> 32) & 0xFF);
      encoding[2] = (byte) ((l >> 24) & 0xFF);
      encoding[3] = (byte) ((l >> 16) & 0xFF);
      encoding[4] = (byte) ((l >> 8) & 0xFF);
      encoding[5] = (byte) (l & 0xFF);
    }
    else if ((l & 0x7FFFFFFFFFFFFFL) == l)
    {
      encoding = new byte[7];
      encoding[0] = (byte) ((l >> 48) & 0xFF);
      encoding[1] = (byte) ((l >> 40) & 0xFF);
      encoding[2] = (byte) ((l >> 32) & 0xFF);
      encoding[3] = (byte) ((l >> 24) & 0xFF);
      encoding[4] = (byte) ((l >> 16) & 0xFF);
      encoding[5] = (byte) ((l >> 8) & 0xFF);
      encoding[6] = (byte) (l & 0xFF);
    }
    else
    {
      encoding = new byte[8];
      encoding[0] = (byte) ((l >> 56) & 0xFF);
      encoding[1] = (byte) ((l >> 48) & 0xFF);
      encoding[2] = (byte) ((l >> 40) & 0xFF);
      encoding[3] = (byte) ((l >> 32) & 0xFF);
      encoding[4] = (byte) ((l >> 24) & 0xFF);
      encoding[5] = (byte) ((l >> 16) & 0xFF);
      encoding[6] = (byte) ((l >> 8) & 0xFF);
      encoding[7] = (byte) (l & 0xFF);
    }
    byte[] encodedElement = new byte[3 + encoding.length];
    encodedElement[0] = ASN1Constants.UNIVERSAL_INTEGER_TYPE;
    encodedElement[1] = (byte) 0x81;
    encodedElement[2] = (byte) encoding.length;
    System.arraycopy(encoding, 0, encodedElement, 3, encoding.length);
    ASN1Long longElement = ASN1Long.decodeAsLong(encodedElement);
    assertEquals(l, longElement.longValue());
  }
  /**
   * Tests the <CODE>decodeAsLong</CODE> method that takes a byte array with
   * a null array.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test(expectedExceptions = { ASN1Exception.class })
  public void testDecodeNullArrayAsLong()
         throws Exception
  {
    byte[] b = null;
    ASN1Long.decodeAsLong(b);
  }
  /**
   * Tests the <CODE>decodeAsLong</CODE> method that takes a byte array with
   * a short array.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test(expectedExceptions = { ASN1Exception.class })
  public void testDecodeShortArrayAsLong()
         throws Exception
  {
    byte[] b = new byte[0];
    ASN1Long.decodeAsLong(b);
  }
  /**
   * Tests the <CODE>decodeAsLong</CODE> method that takes a byte array with
   * a long length array.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test(expectedExceptions = { ASN1Exception.class })
  public void testDecodeLongLengthArrayAsLong()
         throws Exception
  {
    byte[] b = { 0x02, (byte) 0x85, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00 };
    ASN1Long.decodeAsLong(b);
  }
  /**
   * Tests the <CODE>decodeAsLong</CODE> method that takes a byte array with
   * a truncated length array.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test(expectedExceptions = { ASN1Exception.class })
  public void testDecodeTruncatedLengthArrayAsLong()
         throws Exception
  {
    byte[] b = { 0x02, (byte) 0x82, 0x00 };
    ASN1Long.decodeAsLong(b);
  }
  /**
   * Tests the <CODE>decodeAsLong</CODE> method that takes a byte array with
   * a length mismatch.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test(expectedExceptions = { ASN1Exception.class })
  public void testDecodeLengthMismatchArrayAsLong()
         throws Exception
  {
    byte[] b = { 0x02, (byte) 0x81, 0x01 };
    ASN1Long.decodeAsLong(b);
  }
  /**
   * Tests the <CODE>decodeAsLong</CODE> method that takes a byte array with
   * a value too long for a long.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test(expectedExceptions = { ASN1Exception.class })
  public void testDecodeLongIntLengthArrayAsLong()
         throws Exception
  {
    byte[] b = { 0x02, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                 0x00 };
    ASN1Long.decodeAsLong(b);
  }
  /**
   * Tests the first <CODE>toString</CODE> method that takes a string builder
   * argument.
   *
   * @throws Exception
   *           If the test failed unexpectedly.
   * @param  l  The long value to use in the test.
   */
  @Test()
  public void testSetByteValue() throws Exception {
    ASN1Long element = new ASN1Long(0);
    int numLongs = testLongs.size();
    for (int i = 0; i < numLongs; i++) {
      long longValue = testLongs.get(i);
      byte[] encodedLongValue = testEncodedLongs.get(i);
      element.setValue(encodedLongValue);
      assertEquals(longValue, element.longValue());
  @Test(dataProvider = "longValues")
  public void testToString1(long l)
  {
    new ASN1Long(l).toString(new StringBuilder());
    }
  }
  /**
   * Tests the <CODE>decodeAsLong</CODE> method that takes an ASN.1
   * element argument.
   * Tests the second <CODE>toString</CODE> method that takes string builder and
   * integer arguments.
   *
   * @throws Exception
   *           If the test failed unexpectedly.
   * @param  l  The long value to use in the test.
   */
  @Test()
  public void testDecodeElementAsLong() throws Exception {
    int numLongs = testLongs.size();
    for (int i = 0; i < numLongs; i++) {
      long longValue = testLongs.get(i);
      byte[] encodedLongValue = testEncodedLongs.get(i);
      ASN1Element element = new ASN1Element((byte) 0x00, encodedLongValue);
      assertEquals(longValue, ASN1Long.decodeAsLong(element).longValue());
      element = new ASN1Element((byte) 0x02, encodedLongValue);
      assertEquals(longValue, ASN1Long.decodeAsLong(element).longValue());
  @Test(dataProvider = "longValues")
  public void testToString2(long l)
  {
    new ASN1Long(l).toString(new StringBuilder(), 1);
    }
  }
  /**
   * Tests the <CODE>decodeAsLong</CODE> method that takes a byte
   * array argument.
   *
   * @throws Exception
   *           If the test failed unexpectedly.
   */
  @Test()
  public void testDecodeBytesAsLong() throws Exception {
    int numLongs = testLongs.size();
    for (int i = 0; i < numLongs; i++) {
      long longValue = testLongs.get(i);
      byte[] encodedLongValue = testEncodedLongs.get(i);
      byte[] encodedLength = ASN1Element
          .encodeLength(encodedLongValue.length);
      byte[] encodedElement = new byte[1 + encodedLength.length
          + encodedLongValue.length];
      encodedElement[0] = (byte) 0x00;
      System.arraycopy(encodedLength, 0, encodedElement, 1,
          encodedLength.length);
      System.arraycopy(encodedLongValue, 0, encodedElement,
          1 + encodedLength.length, encodedLongValue.length);
      assertEquals(longValue, ASN1Long.decodeAsLong(encodedElement)
          .longValue());
      encodedElement[0] = (byte) 0x02;
      assertEquals(longValue, ASN1Long.decodeAsLong(encodedElement)
          .longValue());
    }
  }
}
opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/protocols/asn1/TestASN1Null.java
@@ -26,66 +26,286 @@
 */
package org.opends.server.protocols.asn1;
import org.testng.annotations.Test;
/**
 * This class defines a set of tests for the
 * org.opends.server.protocols.asn1.ASN1Null class.
 */
public class TestASN1Null extends ASN1TestCase {
public class TestASN1Null
       extends ASN1TestCase
{
  /**
   * Tests the <CODE>setValue</CODE> method.
   *
   * @throws Exception
   *           If the test failed unexpectedly.
   * Tests the first constructor, which doesn't take any arguments.
   */
  @Test()
  public void testSetValue() throws Exception {
    ASN1Null element = new ASN1Null();
    // Test with a null array.
    element.setValue(null);
    // Test with an empty array.
    element.setValue(new byte[0]);
  public void testConstructor1()
  {
    new ASN1Null();
  }
  /**
   * Tests the <CODE>decodeAsNull</CODE> method that takes an ASN.1
   * element argument.
   *
   * @throws Exception
   *           If the test failed unexpectedly.
   * Tests the second constructor, which takes a single byte argument.
   */
  @Test()
  public void testDecodeElementAsNull() throws Exception {
    // Test with a type of 0x00.
    ASN1Element element = new ASN1Element((byte) 0x00);
    ASN1Null.decodeAsNull(element);
    // Test with a type of 0x05.
    element = new ASN1Element((byte) 0x05);
    ASN1Null.decodeAsNull(element);
  public void testConstructor2()
  {
    for (int i=0; i < 254; i++)
    {
      new ASN1Null((byte) (i & 0xFF));
  }
  }
  /**
   * Tests the <CODE>decodeAsNull</CODE> method that takes a byte
   * array argument.
   * Tests the <CODE>setValue</CODE> method with a null argument.
   *
   * @throws Exception
   *           If the test failed unexpectedly.
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test()
  public void testDecodeBytesAsNull() throws Exception {
    byte[] encodedElement = new byte[] { (byte) 0x00, (byte) 0x00 };
  public void testSetNullValue()
         throws Exception
  {
    ASN1Null n = new ASN1Null();
    n.setValue(null);
  }
    // Test with all possible type representations.
    for (int i = 0; i < 256; i++) {
      byte type = (byte) (i & 0xFF);
      encodedElement[0] = type;
      ASN1Null.decodeAsNull(encodedElement);
  /**
   * Tests the <CODE>setValue</CODE> method with an empty byte array argument.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test()
  public void testSetEmptyValue()
         throws Exception
  {
    ASN1Null n = new ASN1Null();
    n.setValue(new byte[0]);
  }
  /**
   * Tests the <CODE>setValue</CODE> method with a non-empty byte array
   * argument.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test(expectedExceptions = { ASN1Exception.class })
  public void testSetNonEmptyValue()
         throws Exception
  {
    ASN1Null n = new ASN1Null();
    n.setValue(new byte[1]);
  }
  /**
   * Tests the <CODE>decodeAsNull</CODE> method that takes an ASN1Element
   * argument with a null argument.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test(expectedExceptions = { ASN1Exception.class })
  public void testDecodeNullElementAsNull()
         throws Exception
  {
    ASN1Element e = null;
    ASN1Null.decodeAsNull(e);
  }
  /**
   * Tests the <CODE>decodeAsNull</CODE> method that takes an ASN1Element
   * argument with an element with a zero-length value.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test()
  public void testDecodeZeroLengthElementAsNull()
         throws Exception
  {
    ASN1Element e = new ASN1OctetString(new byte[0]);
    ASN1Null.decodeAsNull(e);
  }
  /**
   * Tests the <CODE>decodeAsNull</CODE> method that takes an ASN1Element
   * argument with an element with a nonzero-length value.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test(expectedExceptions = { ASN1Exception.class })
  public void testDecodeNonZeroLengthElementAsNull()
         throws Exception
  {
    ASN1Element e = new ASN1OctetString(new byte[1]);
    ASN1Null.decodeAsNull(e);
  }
  /**
   * Tests the <CODE>decodeAsNull</CODE> method that takes a byte array argument
   * with a null array.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test(expectedExceptions = { ASN1Exception.class })
  public void testDecodeNullArrayAsNull()
         throws Exception
  {
    byte[] b = null;
    ASN1Null.decodeAsNull(b);
  }
  /**
   * Tests the <CODE>decodeAsNull</CODE> method that takes a byte array argument
   * with a short array.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test(expectedExceptions = { ASN1Exception.class })
  public void testDecodeShortArrayAsNull()
         throws Exception
  {
    byte[] b = new byte[1];
    ASN1Null.decodeAsNull(b);
  }
  /**
   * Tests the <CODE>decodeAsNull</CODE> method that takes a byte array argument
   * with an array with a long length.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test(expectedExceptions = { ASN1Exception.class })
  public void testDecodeLongLengthArrayAsNull()
         throws Exception
  {
    byte[] b = new byte[] { 0x05, (byte) 0x85, 0x00, 0x00, 0x00, 0x00, 0x00 };
    ASN1Null.decodeAsNull(b);
  }
  /**
   * Tests the <CODE>decodeAsNull</CODE> method that takes a byte array argument
   * with an array with a truncated length.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test(expectedExceptions = { ASN1Exception.class })
  public void testDecodeTruncatedLengthArrayAsNull()
         throws Exception
  {
    byte[] b = new byte[] { 0x05, (byte) 0x82, 0x00 };
    ASN1Null.decodeAsNull(b);
  }
  /**
   * Tests the <CODE>decodeAsNull</CODE> method that takes a byte array argument
   * with an array with a length mismatch.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test(expectedExceptions = { ASN1Exception.class })
  public void testDecodeLengthMismatchArrayAsNull()
         throws Exception
  {
    byte[] b = new byte[] { 0x05, 0x00, 0x00 };
    ASN1Null.decodeAsNull(b);
  }
  /**
   * Tests the <CODE>decodeAsNull</CODE> method that takes a byte array argument
   * with an arry with a nonzero length.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test(expectedExceptions = { ASN1Exception.class })
  public void testDecodeNonZeroLengthArrayAsNull()
         throws Exception
  {
    byte[] b = new byte[] { 0x05, 0x01, 0x00 };
    ASN1Null.decodeAsNull(b);
  }
  /**
   * Tests the <CODE>decodeAsNull</CODE> method that takes a byte array argument
   * with an arry with a zero length.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test()
  public void testDecodeZeroLengthArrayAsNull()
         throws Exception
  {
    byte[] b = new byte[] { 0x05, 0x00 };
    ASN1Null.decodeAsNull(b);
  }
  /**
   * Tests the <CODE>decodeAsNull</CODE> method that takes a byte array argument
   * with an arry with a zero length that takes multiple bytes to encode.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test()
  public void testDecodeExtendedZeroLengthArrayAsNull()
         throws Exception
  {
    byte[] b = new byte[] { 0x05, (byte) 0x81, 0x00 };
    ASN1Null.decodeAsNull(b);
  }
  /**
   * Tests the <CODE>toString</CODE> method that takes a string builder
   * argument.
   */
  @Test()
  public void testToString1()
  {
    new ASN1Null().toString(new StringBuilder());
  }
  /**
   * Tests the <CODE>toString</CODE> method that takes string builder and
   * integer arguments.
   */
  @Test()
  public void testToString2()
  {
    new ASN1Null().toString(new StringBuilder(), 1);
    }
  }
}