From 904a2cc7e44c55c65754b0939fbdb84abb57a285 Mon Sep 17 00:00:00 2001
From: coulbeck <coulbeck@localhost>
Date: Wed, 06 Sep 2006 16:57:53 +0000
Subject: [PATCH] Initial few unit tests for protocols.ldap

---
 opends/tests/unit-tests-testng/src/server/org/opends/server/protocols/ldap/TestSearchProtocolOp.java |  147 +++++++++++++++++++++++++++++++++++++++++++++++++
 opends/ext/testng/testng.xml                                                                         |    7 +-
 2 files changed, 151 insertions(+), 3 deletions(-)

diff --git a/opends/ext/testng/testng.xml b/opends/ext/testng/testng.xml
index f9040f3..9fd56cd 100644
--- a/opends/ext/testng/testng.xml
+++ b/opends/ext/testng/testng.xml
@@ -3,6 +3,7 @@
     <test name="default">
         <packages>
             <package name="org.opends.server.protocols.asn1"/>
+            <package name="org.opends.server.protocols.ldap"/>
             <package name="org.opends.server.core"/>
             <package name="org.opends.server.backends.jeb"/>
             <package name="org.opends.server.synchronization"/>
@@ -10,7 +11,7 @@
             <package name="org.opends.server.util"/>
         </packages>
     </test>
-  
+
     <test name="precommit">
         <groups>
             <run>
@@ -19,7 +20,7 @@
             </run>
         </groups>
     </test>
-      
+
     <test name="functional">
         <groups>
             <run>
@@ -28,5 +29,5 @@
             </run>
         </groups>
     </test>
-    
+
 </suite>
diff --git a/opends/tests/unit-tests-testng/src/server/org/opends/server/protocols/ldap/TestSearchProtocolOp.java b/opends/tests/unit-tests-testng/src/server/org/opends/server/protocols/ldap/TestSearchProtocolOp.java
new file mode 100644
index 0000000..3c425e0
--- /dev/null
+++ b/opends/tests/unit-tests-testng/src/server/org/opends/server/protocols/ldap/TestSearchProtocolOp.java
@@ -0,0 +1,147 @@
+/*
+ * CDDL HEADER START
+ *
+ * The contents of this file are subject to the terms of the
+ * Common Development and Distribution License, Version 1.0 only
+ * (the "License").  You may not use this file except in compliance
+ * with the License.
+ *
+ * You can obtain a copy of the license at
+ * trunk/opends/resource/legal-notices/OpenDS.LICENSE
+ * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
+ * See the License for the specific language governing permissions
+ * and limitations under the License.
+ *
+ * When distributing Covered Code, include this CDDL HEADER in each
+ * file and include the License file at
+ * trunk/opends/resource/legal-notices/OpenDS.LICENSE.  If applicable,
+ * add the following below this CDDL HEADER, with the fields enclosed
+ * by brackets "[]" replaced with your own identifying * information:
+ *      Portions Copyright [yyyy] [name of copyright owner]
+ *
+ * CDDL HEADER END
+ *
+ *
+ *      Portions Copyright 2006 Sun Microsystems, Inc.
+ */
+
+
+package org.opends.server.protocols.ldap;
+
+import org.testng.annotations.Test;
+import org.opends.server.types.DereferencePolicy;
+import org.opends.server.types.SearchScope;
+import org.opends.server.protocols.asn1.*;
+import static org.opends.server.protocols.ldap.LDAPConstants.*;
+import static org.testng.Assert.*;
+
+import java.util.LinkedHashSet;
+import java.util.Arrays;
+import java.util.ArrayList;
+
+/**
+ * Test class for LDAP Search protocol operation classes.
+ */
+public class TestSearchProtocolOp extends LdapTestCase
+{
+  /**
+   * Create a test search request protocol op.
+   * @return A test search request protocol op.
+   * @throws LDAPException If the test object could not be created.
+   */
+  private SearchRequestProtocolOp buildSearchRequestProtocolOp()
+       throws LDAPException
+  {
+    ASN1OctetString baseDN = new ASN1OctetString("dc=example,dc=COM");
+    SearchScope scope = SearchScope.WHOLE_SUBTREE;
+    DereferencePolicy dereferencePolicy = DereferencePolicy.DEREF_IN_SEARCHING;
+    int sizeLimit = Integer.MAX_VALUE;
+    int timeLimit = Integer.MAX_VALUE;
+    boolean typesOnly = true;
+    LDAPFilter filter = LDAPFilter.decode("(objectClass=*)");
+    String[] attrArray = new String[]
+         {
+              "description", "cn", "cn;optionA"
+         };
+    LinkedHashSet<String> attributes =
+         new LinkedHashSet<String>(Arrays.asList(attrArray));
+
+    return new SearchRequestProtocolOp(baseDN,
+                                       scope,
+                                       dereferencePolicy,
+                                       sizeLimit,
+                                       timeLimit,
+                                       typesOnly,
+                                       filter,
+                                       attributes);
+  }
+
+  @Test ()
+  public void testSearchRequestEncodeDecode() throws Exception
+  {
+    // Construct a protocol op.
+    SearchRequestProtocolOp protocolOp = buildSearchRequestProtocolOp();
+
+    // Encode to ASN1.
+    ASN1Element element = protocolOp.encode();
+
+    // Decode to a new protocol op.
+    ProtocolOp decodedProtocolOp = ProtocolOp.decode(element);
+
+    // Make sure the protocol op is the correct type.
+    assertTrue(decodedProtocolOp instanceof SearchRequestProtocolOp);
+    SearchRequestProtocolOp searchOp =
+         (SearchRequestProtocolOp)decodedProtocolOp;
+
+    // Check that the fields have not been changed during encode and decode.
+    assertTrue(protocolOp.getBaseDN().equals(searchOp.getBaseDN()));
+    assertTrue(protocolOp.getScope().equals(searchOp.getScope()));
+    assertTrue(protocolOp.getDereferencePolicy().
+         equals(searchOp.getDereferencePolicy()));
+    assertTrue(protocolOp.getSizeLimit() == searchOp.getSizeLimit());
+    assertTrue(protocolOp.getTimeLimit() == searchOp.getTimeLimit());
+    assertTrue(protocolOp.getFilter().toString().equals(
+         searchOp.getFilter().toString()));
+    // Check that the attributes are in the correct order (comparing the sets
+    // directly does not guarantee this).
+    assertTrue(Arrays.equals(protocolOp.getAttributes().toArray(),
+                             searchOp.getAttributes().toArray()));
+  }
+
+  @Test (expectedExceptions = LDAPException.class)
+  public void testInvalidSearchRequestTooManyElements() throws Exception
+  {
+    ASN1Element element = buildSearchRequestProtocolOp().encode();
+    ArrayList<ASN1Element> elements = ((ASN1Sequence)element).elements();
+    elements.add(new ASN1Boolean(true));
+    ProtocolOp.decode(new ASN1Sequence(OP_TYPE_SEARCH_REQUEST, elements));
+  }
+
+  @Test (expectedExceptions = LDAPException.class)
+  public void testInvalidSearchRequestTooFewElements() throws Exception
+  {
+    ASN1Element element = buildSearchRequestProtocolOp().encode();
+    ArrayList<ASN1Element> elements = ((ASN1Sequence)element).elements();
+    elements.remove(0);
+    ProtocolOp.decode(new ASN1Sequence(OP_TYPE_SEARCH_REQUEST, elements));
+  }
+
+  @Test (expectedExceptions = LDAPException.class)
+  public void testInvalidSearchRequestScope() throws Exception
+  {
+    ASN1Element element = buildSearchRequestProtocolOp().encode();
+    ArrayList<ASN1Element> elements = ((ASN1Sequence)element).elements();
+    elements.set(1, new ASN1Integer(9));
+    ProtocolOp.decode(new ASN1Sequence(OP_TYPE_SEARCH_REQUEST, elements));
+  }
+
+  @Test (expectedExceptions = LDAPException.class)
+  public void testInvalidSearchRequestDerefPolicy() throws Exception
+  {
+    ASN1Element element = buildSearchRequestProtocolOp().encode();
+    ArrayList<ASN1Element> elements = ((ASN1Sequence)element).elements();
+    elements.set(2, new ASN1Integer(9));
+    ProtocolOp.decode(new ASN1Sequence(OP_TYPE_SEARCH_REQUEST, elements));
+  }
+
+}

--
Gitblit v1.10.0