From 4084ed499d28b70d63525b982c58b24d7f9e2854 Mon Sep 17 00:00:00 2001
From: matthew_swift <matthew_swift@localhost>
Date: Sat, 07 Feb 2009 00:56:52 +0000
Subject: [PATCH] Align ASN1Reader with ASN1Writer by adding readEnumerated().

---
 opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/protocols/asn1/ASN1ByteChannelWriterTestCase.java  |   15 ++-
 opendj-sdk/opends/src/server/org/opends/server/protocols/asn1/ASN1ByteChannelReader.java                                  |    7 +
 opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/protocols/asn1/ASN1OutputStreamWriterTestCase.java |   18 +---
 opendj-sdk/opends/src/server/org/opends/server/protocols/asn1/ASN1ByteSequenceReader.java                                 |   19 ++++
 opendj-sdk/opends/src/server/org/opends/server/protocols/asn1/ASN1Reader.java                                             |   12 +++
 opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/protocols/asn1/ASN1WriterTestCase.java             |    2 
 opendj-sdk/opends/src/server/org/opends/server/protocols/asn1/ASN1InputStreamReader.java                                  |   19 ++++
 opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/protocols/asn1/ASN1ByteChannelReaderTestCase.java  |   58 ++++++++++++++
 opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/protocols/asn1/ASN1ReaderTestCase.java             |   73 +++++++++++++++++
 9 files changed, 201 insertions(+), 22 deletions(-)

diff --git a/opendj-sdk/opends/src/server/org/opends/server/protocols/asn1/ASN1ByteChannelReader.java b/opendj-sdk/opends/src/server/org/opends/server/protocols/asn1/ASN1ByteChannelReader.java
index 2a3fd34..953e68d 100644
--- a/opendj-sdk/opends/src/server/org/opends/server/protocols/asn1/ASN1ByteChannelReader.java
+++ b/opendj-sdk/opends/src/server/org/opends/server/protocols/asn1/ASN1ByteChannelReader.java
@@ -399,6 +399,13 @@
   /**
    * {@inheritDoc}
    */
+  public int readEnumerated() throws ASN1Exception {
+    return reader.readEnumerated();
+  }
+
+  /**
+   * {@inheritDoc}
+   */
   public long readInteger() throws ASN1Exception {
     return reader.readInteger();
   }
diff --git a/opendj-sdk/opends/src/server/org/opends/server/protocols/asn1/ASN1ByteSequenceReader.java b/opendj-sdk/opends/src/server/org/opends/server/protocols/asn1/ASN1ByteSequenceReader.java
index c05f092..7d8d608 100644
--- a/opendj-sdk/opends/src/server/org/opends/server/protocols/asn1/ASN1ByteSequenceReader.java
+++ b/opendj-sdk/opends/src/server/org/opends/server/protocols/asn1/ASN1ByteSequenceReader.java
@@ -271,6 +271,25 @@
   /**
    * {@inheritDoc}
    */
+  public int readEnumerated() throws ASN1Exception
+  {
+    // Read the header if haven't done so already
+    peekLength();
+
+    if ((peekLength < 1) || (peekLength > 4))
+    {
+      Message message = ERR_ASN1_INTEGER_INVALID_LENGTH.get(peekLength);
+      throw new ASN1Exception(message);
+    }
+
+    // From an implementation point of view, an enumerated value is
+    // equivalent to an integer.
+    return (int) readInteger();
+  }
+
+  /**
+   * {@inheritDoc}
+   */
   public long readInteger() throws ASN1Exception
   {
     // Read the header if haven't done so already
diff --git a/opendj-sdk/opends/src/server/org/opends/server/protocols/asn1/ASN1InputStreamReader.java b/opendj-sdk/opends/src/server/org/opends/server/protocols/asn1/ASN1InputStreamReader.java
index 94a6f9e..56e838c 100644
--- a/opendj-sdk/opends/src/server/org/opends/server/protocols/asn1/ASN1InputStreamReader.java
+++ b/opendj-sdk/opends/src/server/org/opends/server/protocols/asn1/ASN1InputStreamReader.java
@@ -422,6 +422,25 @@
   /**
    * {@inheritDoc}
    */
+  public int readEnumerated() throws ASN1Exception
+  {
+    // Read the header if haven't done so already
+    peekLength();
+
+    if ((peekLength < 1) || (peekLength > 4))
+    {
+      Message message = ERR_ASN1_INTEGER_INVALID_LENGTH.get(peekLength);
+      throw new ASN1Exception(message);
+    }
+
+    // From an implementation point of view, an enumerated value is
+    // equivalent to an integer.
+    return (int) readInteger();
+  }
+
+  /**
+   * {@inheritDoc}
+   */
   public long readInteger() throws ASN1Exception
   {
     // Read the header if haven't done so already
diff --git a/opendj-sdk/opends/src/server/org/opends/server/protocols/asn1/ASN1Reader.java b/opendj-sdk/opends/src/server/org/opends/server/protocols/asn1/ASN1Reader.java
index 95ddae8..ac9ae3f 100644
--- a/opendj-sdk/opends/src/server/org/opends/server/protocols/asn1/ASN1Reader.java
+++ b/opendj-sdk/opends/src/server/org/opends/server/protocols/asn1/ASN1Reader.java
@@ -139,6 +139,18 @@
 
 
   /**
+   * Reads the next ASN.1 element as an enumerated value and advances
+   * the cursor.
+   *
+   * @return The decoded enumerated value.
+   * @throws ASN1Exception
+   *           If the element cannot be decoded as an enumerated value.
+   */
+  int readEnumerated() throws ASN1Exception;
+
+
+
+  /**
    * Reads the next ASN.1 element as an integer and advances the
    * cursor.
    *
diff --git a/opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/protocols/asn1/ASN1ByteChannelReaderTestCase.java b/opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/protocols/asn1/ASN1ByteChannelReaderTestCase.java
index 13fa758..2a71075 100644
--- a/opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/protocols/asn1/ASN1ByteChannelReaderTestCase.java
+++ b/opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/protocols/asn1/ASN1ByteChannelReaderTestCase.java
@@ -38,6 +38,7 @@
  */
 public class ASN1ByteChannelReaderTestCase extends ASN1ReaderTestCase
 {
+  @Override
   ASN1Reader getReader(byte[] b, int maxElementSize) throws IOException
   {
     ByteArrayInputStream inStream = new ByteArrayInputStream(b);
@@ -54,6 +55,7 @@
    *
    * @throws  Exception  If an unexpected problem occurs.
    */
+  @Override
   @Test(expectedExceptions = { IllegalBlockingModeException.class })
   public void testDecodeShortArrayAsNull()
       throws Exception
@@ -67,6 +69,7 @@
    *
    * @throws  Exception  If an unexpected problem occurs.
    */
+  @Override
   @Test(expectedExceptions = { IllegalBlockingModeException.class })
   public void testDecodeShortArrayAsInteger()
       throws Exception
@@ -75,11 +78,26 @@
   }
 
   /**
+   * Tests the <CODE>readEnumerated</CODE> method that takes a byte array with
+   * a short array.
+   *
+   * @throws  Exception  If an unexpected problem occurs.
+   */
+  @Override
+  @Test(expectedExceptions = { IllegalBlockingModeException.class })
+  public void testDecodeShortArrayAsEnumerated()
+      throws Exception
+  {
+    super.testDecodeShortArrayAsEnumerated();
+  }
+
+  /**
    * Tests the <CODE>decodeAsBoolean</CODE> method that takes a byte array
    * argument with a short array.
    *
    * @throws  Exception  If an unexpected problem occurs.
    */
+  @Override
   @Test(expectedExceptions = { IllegalBlockingModeException.class })
   public void testDecodeShortArrayAsBoolean()
       throws Exception
@@ -93,6 +111,7 @@
    *
    * @throws  Exception  If an unexpected problem occurs.
    */
+  @Override
   @Test(expectedExceptions  = { IllegalBlockingModeException.class })
   public void testDecodeShortArrayAsOctetString()
       throws Exception
@@ -106,6 +125,7 @@
    *
    * @throws  Exception  If an unexpected problem occurs.
    */
+  @Override
   @Test(expectedExceptions = { IllegalBlockingModeException.class })
   public void testDecodeShortArrayAsSequence()
       throws Exception
@@ -119,6 +139,7 @@
    *
    * @throws  Exception  If an unexpected problem occurs.
    */
+  @Override
   @Test(expectedExceptions = { IllegalBlockingModeException.class })
   public void testDecodeLengthMismatchArrayAsBoolean()
       throws Exception
@@ -132,6 +153,7 @@
    *
    * @throws  Exception  If an unexpected problem occurs.
    */
+  @Override
   @Test(expectedExceptions = { IllegalBlockingModeException.class })
   public void testDecodeLengthMismatchArrayAsInteger()
       throws Exception
@@ -140,11 +162,26 @@
   }
 
   /**
+   * Tests the <CODE>readEnumerated</CODE> method that takes a byte array with
+   * a length mismatch.
+   *
+   * @throws  Exception  If an unexpected problem occurs.
+   */
+  @Override
+  @Test(expectedExceptions = { IllegalBlockingModeException.class })
+  public void testDecodeLengthMismatchArrayAsEnumerated()
+      throws Exception
+  {
+    super.testDecodeLengthMismatchArrayAsEnumerated();
+  }
+
+  /**
    * Tests the <CODE>decodeAsOctetString</CODE> method that takes a byte array
    * using an array whose actual length doesn't match with the decoded length.
    *
    * @throws  Exception  If an unexpected problem occurs.
    */
+  @Override
   @Test(expectedExceptions  = { IllegalBlockingModeException.class })
   public void testDecodeLengthMismatchArrayAsOctetString()
       throws Exception
@@ -158,6 +195,7 @@
    *
    * @throws  Exception  If an unexpected problem occurs.
    */
+  @Override
   @Test(expectedExceptions = { IllegalBlockingModeException.class })
   public void testDecodeTruncatedLengthArrayAsBoolean()
       throws Exception
@@ -171,6 +209,7 @@
    *
    * @throws  Exception  If an unexpected problem occurs.
    */
+  @Override
   @Test(expectedExceptions = { IllegalBlockingModeException.class })
   public void testDecodeTruncatedLengthArrayAsInteger()
       throws Exception
@@ -179,11 +218,26 @@
   }
 
   /**
+   * Tests the <CODE>readEnumerated</CODE> method that takes a byte array with
+   * a truncated length array.
+   *
+   * @throws  Exception  If an unexpected problem occurs.
+   */
+  @Override
+  @Test(expectedExceptions = { IllegalBlockingModeException.class })
+  public void testDecodeTruncatedLengthArrayAsEnumerated()
+      throws Exception
+  {
+    super.testDecodeTruncatedLengthArrayAsEnumerated();
+  }
+
+  /**
    * 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.
    */
+  @Override
   @Test(expectedExceptions = { IllegalBlockingModeException.class })
   public void testDecodeTruncatedLengthArrayAsNull()
       throws Exception
@@ -197,6 +251,7 @@
    *
    * @throws  Exception  If an unexpected problem occurs.
    */
+  @Override
   @Test(expectedExceptions  = { IllegalBlockingModeException.class })
   public void testDecodeTruncatedLengthArrayAsOctetString()
       throws Exception
@@ -210,6 +265,7 @@
    *
    * @throws  Exception  If an unexpected problem occurs.
    */
+  @Override
   @Test(expectedExceptions = { IllegalBlockingModeException.class })
   public void testDecodeTruncatedLengthArrayAsSequence()
       throws Exception
@@ -223,6 +279,7 @@
    *
    * @throws  Exception  If an unexpected problem occurs.
    */
+  @Override
   @Test(expectedExceptions = { IllegalBlockingModeException.class })
   public void testDecodeSequencePrematureEof()
       throws Exception
@@ -235,6 +292,7 @@
    *
    * @throws  Exception  If an unexpected problem occurs.
    */
+  @Override
   @Test(expectedExceptions = { IllegalBlockingModeException.class })
   public void testSkipElementIncompleteRead()
       throws Exception
diff --git a/opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/protocols/asn1/ASN1ByteChannelWriterTestCase.java b/opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/protocols/asn1/ASN1ByteChannelWriterTestCase.java
index 7e5e291..4a10cee 100644
--- a/opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/protocols/asn1/ASN1ByteChannelWriterTestCase.java
+++ b/opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/protocols/asn1/ASN1ByteChannelWriterTestCase.java
@@ -26,24 +26,23 @@
  */
 package org.opends.server.protocols.asn1;
 
-import org.opends.server.types.ByteString;
-
-import java.io.ByteArrayOutputStream;
 import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
 import java.io.IOException;
-import java.nio.channels.WritableByteChannel;
 import java.nio.channels.Channels;
 import java.nio.channels.ReadableByteChannel;
+import java.nio.channels.WritableByteChannel;
 
 /**
  * Test class for ASN1ByteChannelWriter
  */
 public class ASN1ByteChannelWriterTestCase extends ASN1WriterTestCase
 {
-  ByteArrayOutputStream outStream = new ByteArrayOutputStream();
-  WritableByteChannel outChannel = Channels.newChannel(outStream);
-  ASN1Writer writer = new ASN1ByteChannelWriter(outChannel, 500);
+  private ByteArrayOutputStream outStream = new ByteArrayOutputStream();
+  private WritableByteChannel outChannel = Channels.newChannel(outStream);
+  private ASN1Writer writer = new ASN1ByteChannelWriter(outChannel, 500);
 
+  @Override
   ASN1Writer getWriter() throws IOException
   {
     writer.flush();
@@ -51,6 +50,7 @@
     return writer;
   }
 
+  @Override
   ASN1Reader getReader(byte[] encodedBytes) throws ASN1Exception, IOException
   {
     ByteArrayInputStream inStream =
@@ -65,6 +65,7 @@
     return reader;
   }
 
+  @Override
   byte[] getEncodedBytes() throws IOException, ASN1Exception
   {
     writer.flush();
diff --git a/opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/protocols/asn1/ASN1OutputStreamWriterTestCase.java b/opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/protocols/asn1/ASN1OutputStreamWriterTestCase.java
index 5638870..fc24ae9 100644
--- a/opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/protocols/asn1/ASN1OutputStreamWriterTestCase.java
+++ b/opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/protocols/asn1/ASN1OutputStreamWriterTestCase.java
@@ -26,32 +26,25 @@
  */
 package org.opends.server.protocols.asn1;
 
-import org.testng.annotations.DataProvider;
-import org.testng.annotations.Test;
-import static org.testng.Assert.*;
-import org.opends.server.types.ByteStringBuilder;
-import org.opends.server.types.ByteString;
-import static org.opends.server.protocols.asn1.ASN1Constants.*;
-import org.opends.server.util.StaticUtils;
-
-import java.io.ByteArrayOutputStream;
 import java.io.ByteArrayInputStream;
-import java.util.Arrays;
+import java.io.ByteArrayOutputStream;
 
 /**
  * Test class for ASN1OutputStreamWriter
  */
 public class ASN1OutputStreamWriterTestCase extends ASN1WriterTestCase
 {
-  ByteArrayOutputStream outStream = new ByteArrayOutputStream();
-  ASN1Writer writer = new ASN1OutputStreamWriter(outStream);
+  private ByteArrayOutputStream outStream = new ByteArrayOutputStream();
+  private ASN1Writer writer = new ASN1OutputStreamWriter(outStream);
 
+  @Override
   ASN1Writer getWriter()
   {
     outStream.reset();
     return writer;
   }
 
+  @Override
   ASN1Reader getReader(byte[] encodedBytes)
   {
     ByteArrayInputStream inStream =
@@ -59,6 +52,7 @@
     return new ASN1InputStreamReader(inStream, 0);
   }
 
+  @Override
   byte[] getEncodedBytes()
   {
     return outStream.toByteArray();
diff --git a/opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/protocols/asn1/ASN1ReaderTestCase.java b/opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/protocols/asn1/ASN1ReaderTestCase.java
index ee4299b..668b566 100644
--- a/opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/protocols/asn1/ASN1ReaderTestCase.java
+++ b/opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/protocols/asn1/ASN1ReaderTestCase.java
@@ -67,8 +67,6 @@
    *
    * @return  A list of byte arrays with encoded ASN.1 elements that can be
    *          decoded as octet strings.
-   *
-   * @throws  Exception  If an unexpected problem occurs.
    */
   @DataProvider(name = "elementArrays")
   public Object[][] getElementArrays()
@@ -102,6 +100,17 @@
         };
   }
 
+  /**
+   * Gets the reader to be use for the unit tests.
+   *
+   * @param b
+   *          The array of bytes to be read.
+   * @param maxElementSize
+   *          The max element size.
+   * @return The reader to be use for the unit tests.
+   * @throws IOException
+   *           In an unexpected IO exception occurred.
+   */
   abstract ASN1Reader getReader(byte[] b, int maxElementSize)
       throws IOException;
 
@@ -213,6 +222,20 @@
     getReader(b, 0).readInteger();
   }
 
+  /**
+   * Tests the <CODE>readEnumerated</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];
+    getReader(b, 0).readEnumerated();
+  }
+
 
 
   /**
@@ -232,6 +255,22 @@
 
 
   /**
+   * Tests the <CODE>readEnumerated</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 };
+    getReader(b, 0).readEnumerated();
+  }
+
+
+
+  /**
    * Tests the <CODE>decodeAsInteger</CODE> method that takes a byte array with
    * a truncated length array.
    *
@@ -248,6 +287,22 @@
 
 
   /**
+   * Tests the <CODE>readEnumerated</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 };
+    getReader(b, 0).readEnumerated();
+  }
+
+
+
+  /**
    * Tests the <CODE>decodeAsInteger</CODE> method that takes a byte array with
    * a length mismatch.
    *
@@ -262,6 +317,20 @@
   }
 
   /**
+   * Tests the <CODE>readEnumerated</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 };
+    getReader(b, 0).readEnumerated();
+  }
+
+  /**
    * Tests the <CODE>decodeAsBoolean</CODE> method that takes a byte array
    * argument with valid arrays.
    *
diff --git a/opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/protocols/asn1/ASN1WriterTestCase.java b/opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/protocols/asn1/ASN1WriterTestCase.java
index 0befcdd..0e60798 100644
--- a/opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/protocols/asn1/ASN1WriterTestCase.java
+++ b/opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/protocols/asn1/ASN1WriterTestCase.java
@@ -47,7 +47,7 @@
 
   // Create an array with all of the valid single-byte types.  We don't
   // support multi-byte types, so this should be a comprehensive data set.
-  byte[] testTypes = new byte[0xFF];
+  private byte[] testTypes = new byte[0xFF];
   {
     for (int i=0x00; i < 0xFF; i++)
     {

--
Gitblit v1.10.0