From 34b2037d4e4bda15068c6ad527064343bcf867f0 Mon Sep 17 00:00:00 2001
From: coulbeck <coulbeck@localhost>
Date: Mon, 02 Oct 2006 20:08:16 +0000
Subject: [PATCH] Initial unit tests for core CompareOperation.

---
 opends/tests/unit-tests-testng/src/server/org/opends/server/core/CompareOperationTestCase.java |  592 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 592 insertions(+), 0 deletions(-)

diff --git a/opends/tests/unit-tests-testng/src/server/org/opends/server/core/CompareOperationTestCase.java b/opends/tests/unit-tests-testng/src/server/org/opends/server/core/CompareOperationTestCase.java
new file mode 100644
index 0000000..eff1f6e
--- /dev/null
+++ b/opends/tests/unit-tests-testng/src/server/org/opends/server/core/CompareOperationTestCase.java
@@ -0,0 +1,592 @@
+/*
+ * 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.core;
+
+import org.opends.server.protocols.internal.InternalClientConnection;
+import org.opends.server.protocols.asn1.ASN1OctetString;
+import org.opends.server.protocols.ldap.LDAPFilter;
+import org.opends.server.types.Control;
+import org.opends.server.types.ResultCode;
+import org.opends.server.types.Entry;
+import org.opends.server.TestCaseUtils;
+import org.opends.server.controls.LDAPAssertionRequestControl;
+import org.opends.server.controls.ProxiedAuthV1Control;
+import org.opends.server.controls.ProxiedAuthV2Control;
+import org.opends.server.plugins.InvocationCounterPlugin;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.Test;
+import static org.testng.Assert.*;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class CompareOperationTestCase extends OperationTestCase
+{
+  private Entry entry;
+
+
+  @BeforeClass
+  public void setUp() throws Exception
+  {
+    TestCaseUtils.startServer();
+    TestCaseUtils.initializeTestBackend(true);
+
+    InternalClientConnection connection =
+         InternalClientConnection.getRootConnection();
+
+    // Add a test entry.
+    entry = TestCaseUtils.makeEntry(
+         "dn: uid=rogasawara,o=test",
+         "userpassword: password",
+         "objectclass: top",
+         "objectclass: person",
+         "objectclass: organizationalPerson",
+         "objectclass: inetOrgPerson",
+         "uid: rogasawara",
+         "mail: rogasawara@airius.co.jp",
+         "givenname;lang-ja:: 44Ot44OJ44OL44O8",
+         "sn;lang-ja:: 5bCP56yg5Y6f",
+         "cn;lang-ja:: 5bCP56yg5Y6fIOODreODieODi+ODvA==",
+         "title;lang-ja:: 5Za25qWt6YOoIOmDqOmVtw==",
+         "preferredlanguage: ja",
+         "givenname:: 44Ot44OJ44OL44O8",
+         "sn:: 5bCP56yg5Y6f",
+         "cn:: 5bCP56yg5Y6fIOODreODieODi+ODvA==",
+         "title:: 5Za25qWt6YOoIOmDqOmVtw==",
+         "givenname;lang-ja;phonetic:: 44KN44Gp44Gr44O8",
+         "sn;lang-ja;phonetic:: 44GK44GM44GV44KP44KJ",
+         "cn;lang-ja;phonetic:: 44GK44GM44GV44KP44KJIOOCjeOBqeOBq+ODvA==",
+         "title;lang-ja;phonetic:: " +
+              "44GI44GE44GO44KH44GG44G2IOOBtuOBoeOCh+OBhg==",
+         "givenname;lang-en: Rodney",
+         "sn;lang-en: Ogasawara",
+         "cn;lang-en: Rodney Ogasawara",
+         "title;lang-en: Sales, Director"
+    );
+    AddOperation addOperation =
+         connection.processAdd(entry.getDN(),
+                               entry.getObjectClasses(),
+                               entry.getUserAttributes(),
+                               entry.getOperationalAttributes());
+    assertEquals(addOperation.getResultCode(), ResultCode.SUCCESS);
+    assertNotNull(DirectoryServer.getEntry(entry.getDN()));
+  }
+
+
+  public Operation[] createTestOperations() throws Exception
+  {
+    InternalClientConnection conn =
+         InternalClientConnection.getRootConnection();
+
+    return new Operation[]
+    {
+      new CompareOperation(conn, InternalClientConnection.nextOperationID(),
+                           InternalClientConnection.nextMessageID(),
+                           new ArrayList<Control>(),
+                           new ASN1OctetString(entry.getDN().toString()),
+                           "uid", new ASN1OctetString("rogasawara"))
+    };
+  }
+
+  /**
+   * Invokes a number of operation methods on the provided compare operation
+   * for which all processing has been completed.
+   *
+   * @param  compareOperation  The operation to be tested.
+   */
+  private void examineCompletedOperation(CompareOperation compareOperation)
+  {
+    assertTrue(compareOperation.getProcessingStartTime() > 0);
+    assertTrue(compareOperation.getProcessingStopTime() > 0);
+    assertTrue(compareOperation.getProcessingTime() >= 0);
+    assertNotNull(compareOperation.getResponseLogElements());
+
+    assertEquals(InvocationCounterPlugin.getPreParseCount(), 1);
+    assertEquals(InvocationCounterPlugin.getPreOperationCount(), 1);
+    assertEquals(InvocationCounterPlugin.getPostOperationCount(), 1);
+    assertEquals(InvocationCounterPlugin.getPostResponseCount(), 1);
+  }
+
+  /**
+   * Invokes a number of operation methods on the provided compare operation
+   * for which the pre-operation plugin was not called.
+   *
+   * @param  compareOperation  The operation to be tested.
+   */
+  private void examineIncompleteOperation(CompareOperation compareOperation)
+  {
+    assertTrue(compareOperation.getProcessingStartTime() > 0);
+    assertTrue(compareOperation.getProcessingStopTime() > 0);
+    assertTrue(compareOperation.getProcessingTime() >= 0);
+    assertNotNull(compareOperation.getResponseLogElements());
+    assertTrue(compareOperation.getErrorMessage().length() > 0);
+
+    assertEquals(InvocationCounterPlugin.getPreParseCount(), 1);
+    assertEquals(InvocationCounterPlugin.getPreOperationCount(), 0);
+    assertEquals(InvocationCounterPlugin.getPostOperationCount(), 1);
+    assertEquals(InvocationCounterPlugin.getPostResponseCount(), 1);
+  }
+
+  /**
+   * Invokes a number of operation methods on the provided compare operation
+   * for which an error was found during parsing.
+   *
+   * @param  compareOperation  The operation to be tested.
+   */
+  private void examineUnparsedOperation(CompareOperation compareOperation)
+  {
+    assertTrue(compareOperation.getProcessingStartTime() > 0);
+    assertTrue(compareOperation.getProcessingStopTime() > 0);
+    assertTrue(compareOperation.getProcessingTime() >= 0);
+    assertNotNull(compareOperation.getResponseLogElements());
+    assertTrue(compareOperation.getErrorMessage().length() > 0);
+
+    assertEquals(InvocationCounterPlugin.getPreParseCount(), 1);
+    assertEquals(InvocationCounterPlugin.getPreOperationCount(), 0);
+    assertEquals(InvocationCounterPlugin.getPostOperationCount(), 0);
+    assertEquals(InvocationCounterPlugin.getPostResponseCount(), 1);
+  }
+
+  @Test
+  public void testCompareTrue()
+  {
+    InvocationCounterPlugin.resetAllCounters();
+
+    InternalClientConnection conn =
+         InternalClientConnection.getRootConnection();
+
+    CompareOperation compareOperation =
+         new CompareOperation(conn, InternalClientConnection.nextOperationID(),
+                              InternalClientConnection.nextMessageID(),
+                              new ArrayList<Control>(),
+                              new ASN1OctetString(entry.getDN().toString()),
+                              "uid", new ASN1OctetString("rogasawara"));
+
+    compareOperation.run();
+    assertEquals(compareOperation.getResultCode(),
+                 ResultCode.COMPARE_TRUE);
+    assertEquals(compareOperation.getErrorMessage().length(), 0);
+
+    examineCompletedOperation(compareOperation);
+  }
+
+
+  @Test
+  public void testCompareFalse()
+  {
+    InvocationCounterPlugin.resetAllCounters();
+
+    InternalClientConnection conn =
+         InternalClientConnection.getRootConnection();
+
+    CompareOperation compareOperation =
+         new CompareOperation(conn, InternalClientConnection.nextOperationID(),
+                              InternalClientConnection.nextMessageID(),
+                              new ArrayList<Control>(),
+                              new ASN1OctetString(entry.getDN().toString()),
+                              "uid", new ASN1OctetString("rogasawala"));
+
+    compareOperation.run();
+    assertEquals(compareOperation.getResultCode(),
+                 ResultCode.COMPARE_FALSE);
+
+    assertEquals(compareOperation.getErrorMessage().length(), 0);
+    examineCompletedOperation(compareOperation);
+  }
+
+  @Test
+  public void testCompareEntryNonexistent()
+  {
+    InvocationCounterPlugin.resetAllCounters();
+
+    InternalClientConnection conn =
+         InternalClientConnection.getRootConnection();
+
+    CompareOperation compareOperation =
+         new CompareOperation(conn, InternalClientConnection.nextOperationID(),
+                              InternalClientConnection.nextMessageID(),
+                              new ArrayList<Control>(),
+                              new ASN1OctetString("o=nonexistent,o=test"),
+                              "o", new ASN1OctetString("nonexistent"));
+
+    compareOperation.run();
+    assertEquals(compareOperation.getResultCode(),
+                 ResultCode.NO_SUCH_OBJECT);
+
+    examineIncompleteOperation(compareOperation);
+  }
+
+  @Test
+  public void testCompareInvalidDn()
+  {
+    InvocationCounterPlugin.resetAllCounters();
+
+    InternalClientConnection conn =
+         InternalClientConnection.getRootConnection();
+
+    CompareOperation compareOperation =
+         new CompareOperation(conn, InternalClientConnection.nextOperationID(),
+                              InternalClientConnection.nextMessageID(),
+                              new ArrayList<Control>(),
+                              new ASN1OctetString("rogasawara,o=test"),
+                              "uid", new ASN1OctetString("rogasawara"));
+
+    compareOperation.run();
+    assertEquals(compareOperation.getResultCode(),
+                 ResultCode.INVALID_DN_SYNTAX);
+
+    examineUnparsedOperation(compareOperation);
+  }
+
+  @Test
+  public void testCompareNoSuchAttribute()
+  {
+    InvocationCounterPlugin.resetAllCounters();
+
+    InternalClientConnection conn =
+         InternalClientConnection.getRootConnection();
+
+    CompareOperation compareOperation =
+         new CompareOperation(conn, InternalClientConnection.nextOperationID(),
+                              InternalClientConnection.nextMessageID(),
+                              new ArrayList<Control>(),
+                              new ASN1OctetString(entry.getDN().toString()),
+                              "description", new ASN1OctetString("rogasawara"));
+
+    compareOperation.run();
+    assertEquals(compareOperation.getResultCode(),
+                 ResultCode.NO_SUCH_ATTRIBUTE);
+
+    assertTrue(compareOperation.getErrorMessage().length() > 0);
+    examineCompletedOperation(compareOperation);
+  }
+
+  @Test
+  public void testCompareUndefinedAttribute()
+  {
+    InvocationCounterPlugin.resetAllCounters();
+
+    InternalClientConnection conn =
+         InternalClientConnection.getRootConnection();
+
+    CompareOperation compareOperation =
+         new CompareOperation(conn, InternalClientConnection.nextOperationID(),
+                              InternalClientConnection.nextMessageID(),
+                              new ArrayList<Control>(),
+                              new ASN1OctetString(entry.getDN().toString()),
+                              "NotAnAttribute",
+                              new ASN1OctetString("rogasawara"));
+
+    compareOperation.run();
+    assertEquals(compareOperation.getResultCode(),
+                 ResultCode.NO_SUCH_ATTRIBUTE);
+
+    assertTrue(compareOperation.getErrorMessage().length() > 0);
+    examineCompletedOperation(compareOperation);
+  }
+
+  @Test(enabled = false) // FIXME Issue 739.
+  public void testCompareSubtype()
+  {
+    InternalClientConnection conn =
+         InternalClientConnection.getRootConnection();
+
+    CompareOperation compareOperation =
+         new CompareOperation(conn, InternalClientConnection.nextOperationID(),
+                              InternalClientConnection.nextMessageID(),
+                              new ArrayList<Control>(),
+                              new ASN1OctetString(entry.getDN().toString()),
+                              "name",
+                              new ASN1OctetString("Ogasawara"));
+
+    compareOperation.run();
+    assertEquals(compareOperation.getResultCode(),
+                 ResultCode.COMPARE_TRUE);
+  }
+
+  @Test
+  public void testCompareOptions1()
+  {
+    InternalClientConnection conn =
+         InternalClientConnection.getRootConnection();
+
+    CompareOperation compareOperation =
+         new CompareOperation(conn, InternalClientConnection.nextOperationID(),
+                              InternalClientConnection.nextMessageID(),
+                              new ArrayList<Control>(),
+                              new ASN1OctetString(entry.getDN().toString()),
+                              "sn",
+                              new ASN1OctetString("Ogasawara"));
+
+    compareOperation.run();
+    assertEquals(compareOperation.getResultCode(),
+                 ResultCode.COMPARE_TRUE);
+  }
+
+  @Test
+  public void testCompareOptions2()
+  {
+    InternalClientConnection conn =
+         InternalClientConnection.getRootConnection();
+
+    CompareOperation compareOperation =
+         new CompareOperation(conn, InternalClientConnection.nextOperationID(),
+                              InternalClientConnection.nextMessageID(),
+                              new ArrayList<Control>(),
+                              new ASN1OctetString(entry.getDN().toString()),
+                              "sn;lang-ja",
+                              new ASN1OctetString("Ogasawara"));
+
+    compareOperation.run();
+    assertEquals(compareOperation.getResultCode(),
+                 ResultCode.COMPARE_FALSE);
+  }
+
+  @Test
+  public void testCompareTrueAssertion() throws Exception
+  {
+    InvocationCounterPlugin.resetAllCounters();
+
+    InternalClientConnection conn =
+         InternalClientConnection.getRootConnection();
+
+    LDAPFilter ldapFilter = LDAPFilter.decode("(preferredlanguage=ja)");
+    LDAPAssertionRequestControl assertControl =
+         new LDAPAssertionRequestControl(true, ldapFilter);
+    List<Control> controls = new ArrayList<Control>();
+    controls.add(assertControl);
+
+    CompareOperation compareOperation =
+         new CompareOperation(conn, InternalClientConnection.nextOperationID(),
+                              InternalClientConnection.nextMessageID(),
+                              controls,
+                              new ASN1OctetString(entry.getDN().toString()),
+                              "uid", new ASN1OctetString("rogasawara"));
+
+    compareOperation.run();
+    assertEquals(compareOperation.getResultCode(),
+                 ResultCode.COMPARE_TRUE);
+
+    assertEquals(compareOperation.getErrorMessage().length(), 0);
+    examineCompletedOperation(compareOperation);
+  }
+
+  @Test
+  public void testCompareAssertionFailed() throws Exception
+  {
+    InvocationCounterPlugin.resetAllCounters();
+
+    InternalClientConnection conn =
+         InternalClientConnection.getRootConnection();
+
+    LDAPFilter ldapFilter = LDAPFilter.decode("(preferredlanguage=en)");
+    LDAPAssertionRequestControl assertControl =
+         new LDAPAssertionRequestControl(true, ldapFilter);
+    List<Control> controls = new ArrayList<Control>();
+    controls.add(assertControl);
+
+    CompareOperation compareOperation =
+         new CompareOperation(conn, InternalClientConnection.nextOperationID(),
+                              InternalClientConnection.nextMessageID(),
+                              controls,
+                              new ASN1OctetString(entry.getDN().toString()),
+                              "uid", new ASN1OctetString("rogasawara"));
+
+    compareOperation.run();
+    assertEquals(compareOperation.getResultCode(),
+                 ResultCode.ASSERTION_FAILED);
+
+    examineIncompleteOperation(compareOperation);
+  }
+
+  @Test
+  public void testCompareProxiedAuthV1() throws Exception
+  {
+    InvocationCounterPlugin.resetAllCounters();
+
+    InternalClientConnection conn =
+         InternalClientConnection.getRootConnection();
+
+    ProxiedAuthV1Control authV1Control =
+         new ProxiedAuthV1Control(new ASN1OctetString());
+    List<Control> controls = new ArrayList<Control>();
+    controls.add(authV1Control);
+
+    CompareOperation compareOperation =
+         new CompareOperation(conn, InternalClientConnection.nextOperationID(),
+                              InternalClientConnection.nextMessageID(),
+                              controls,
+                              new ASN1OctetString(entry.getDN().toString()),
+                              "uid", new ASN1OctetString("rogasawara"));
+
+    compareOperation.run();
+    assertEquals(compareOperation.getResultCode(),
+                 ResultCode.COMPARE_TRUE);
+
+    assertEquals(compareOperation.getErrorMessage().length(), 0);
+    examineCompletedOperation(compareOperation);
+  }
+
+  @Test
+  public void testCompareProxiedAuthV1Denied() throws Exception
+  {
+    InvocationCounterPlugin.resetAllCounters();
+
+    InternalClientConnection conn =
+         InternalClientConnection.getRootConnection();
+
+    ProxiedAuthV1Control authV1Control =
+         new ProxiedAuthV1Control(new ASN1OctetString("cn=nonexistent,o=test"));
+    List<Control> controls = new ArrayList<Control>();
+    controls.add(authV1Control);
+
+    CompareOperation compareOperation =
+         new CompareOperation(conn, InternalClientConnection.nextOperationID(),
+                              InternalClientConnection.nextMessageID(),
+                              controls,
+                              new ASN1OctetString(entry.getDN().toString()),
+                              "uid", new ASN1OctetString("rogasawara"));
+
+    compareOperation.run();
+    assertEquals(compareOperation.getResultCode(),
+                 ResultCode.AUTHORIZATION_DENIED);
+
+    examineIncompleteOperation(compareOperation);
+  }
+
+  @Test
+  public void testCompareProxiedAuthV2() throws Exception
+  {
+    InvocationCounterPlugin.resetAllCounters();
+
+    InternalClientConnection conn =
+         InternalClientConnection.getRootConnection();
+
+    ProxiedAuthV2Control authV2Control =
+         new ProxiedAuthV2Control(new ASN1OctetString("dn:"));
+    List<Control> controls = new ArrayList<Control>();
+    controls.add(authV2Control);
+
+    CompareOperation compareOperation =
+         new CompareOperation(conn, InternalClientConnection.nextOperationID(),
+                              InternalClientConnection.nextMessageID(),
+                              controls,
+                              new ASN1OctetString(entry.getDN().toString()),
+                              "uid", new ASN1OctetString("rogasawara"));
+
+    compareOperation.run();
+    assertEquals(compareOperation.getResultCode(),
+                 ResultCode.COMPARE_TRUE);
+
+    assertEquals(compareOperation.getErrorMessage().length(), 0);
+    examineCompletedOperation(compareOperation);
+  }
+
+  @Test
+  public void testCompareProxiedAuthV2Denied() throws Exception
+  {
+    InvocationCounterPlugin.resetAllCounters();
+
+    InternalClientConnection conn =
+         InternalClientConnection.getRootConnection();
+
+    ProxiedAuthV2Control authV2Control = new ProxiedAuthV2Control(
+         new ASN1OctetString("dn:cn=nonexistent,o=test"));
+    List<Control> controls = new ArrayList<Control>();
+    controls.add(authV2Control);
+
+    CompareOperation compareOperation =
+         new CompareOperation(conn, InternalClientConnection.nextOperationID(),
+                              InternalClientConnection.nextMessageID(),
+                              controls,
+                              new ASN1OctetString(entry.getDN().toString()),
+                              "uid", new ASN1OctetString("rogasawara"));
+
+    compareOperation.run();
+    assertEquals(compareOperation.getResultCode(),
+                 ResultCode.AUTHORIZATION_DENIED);
+
+    examineIncompleteOperation(compareOperation);
+  }
+
+  @Test(enabled = false) // FIXME Issue 741.
+  public void testCompareProxiedAuthV2Criticality() throws Exception
+  {
+    InvocationCounterPlugin.resetAllCounters();
+
+    InternalClientConnection conn =
+         InternalClientConnection.getRootConnection();
+
+    ProxiedAuthV2Control authV2Control =
+         new ProxiedAuthV2Control(new ASN1OctetString());
+    authV2Control.setCritical(false);
+    List<Control> controls = new ArrayList<Control>();
+    controls.add(authV2Control);
+
+    CompareOperation compareOperation =
+         new CompareOperation(conn, InternalClientConnection.nextOperationID(),
+                              InternalClientConnection.nextMessageID(),
+                              controls,
+                              new ASN1OctetString(entry.getDN().toString()),
+                              "uid", new ASN1OctetString("rogasawara"));
+
+    compareOperation.run();
+    assertEquals(compareOperation.getResultCode(),
+                 ResultCode.PROTOCOL_ERROR);
+
+    examineIncompleteOperation(compareOperation);
+  }
+
+  @Test
+  public void testCompareUnsupportedControl() throws Exception
+  {
+    InvocationCounterPlugin.resetAllCounters();
+
+    InternalClientConnection conn =
+         InternalClientConnection.getRootConnection();
+
+    LDAPFilter ldapFilter = LDAPFilter.decode("(preferredlanguage=ja)");
+    LDAPAssertionRequestControl assertControl =
+         new LDAPAssertionRequestControl("1.1.1.1.1.1", true, ldapFilter);
+    List<Control> controls = new ArrayList<Control>();
+    controls.add(assertControl);
+    CompareOperation compareOperation =
+         new CompareOperation(conn, InternalClientConnection.nextOperationID(),
+                              InternalClientConnection.nextMessageID(),
+                              controls,
+                              new ASN1OctetString(entry.getDN().toString()),
+                              "uid", new ASN1OctetString("rogasawara"));
+
+    compareOperation.run();
+    assertEquals(compareOperation.getResultCode(),
+                 ResultCode.UNAVAILABLE_CRITICAL_EXTENSION);
+
+    examineIncompleteOperation(compareOperation);
+  }
+
+}

--
Gitblit v1.10.0