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

Jean-Noel Rouvignac
01.41.2013 067d31464e946bbfdb35106b9162155d621fc597
opendj-sdk/opends/src/server/org/opends/server/tools/LDAPCompare.java
@@ -23,16 +23,13 @@
 *
 *
 *      Copyright 2006-2010 Sun Microsystems, Inc.
 *      Portions Copyright 2012 ForgeRock AS.
 *      Portions Copyright 2012-2013 ForgeRock AS
 */
package org.opends.server.tools;
import org.opends.admin.ads.util.ConnectionUtils;
import org.opends.messages.Message;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.Reader;
@@ -41,11 +38,16 @@
import java.util.LinkedList;
import java.util.concurrent.atomic.AtomicInteger;
import org.opends.admin.ads.util.ConnectionUtils;
import org.opends.messages.Message;
import org.opends.server.controls.LDAPAssertionRequestControl;
import org.opends.server.loggers.debug.DebugTracer;
import org.opends.server.protocols.asn1.ASN1Exception;
import org.opends.server.protocols.ldap.CompareRequestProtocolOp;
import org.opends.server.protocols.ldap.CompareResponseProtocolOp;
import org.opends.server.protocols.ldap.LDAPFilter;
import org.opends.server.protocols.ldap.LDAPMessage;
import org.opends.server.protocols.ldap.LDAPResultCode;
import org.opends.server.protocols.ldap.ProtocolOp;
import org.opends.server.types.*;
import org.opends.server.util.Base64;
@@ -58,15 +60,12 @@
import org.opends.server.util.args.IntegerArgument;
import org.opends.server.util.args.StringArgument;
import static org.opends.server.loggers.debug.DebugLogger.*;
import org.opends.server.loggers.debug.DebugTracer;
import static org.opends.messages.ToolMessages.*;
import static org.opends.server.loggers.debug.DebugLogger.*;
import static org.opends.server.protocols.ldap.LDAPResultCode.*;
import static org.opends.server.tools.ToolConstants.*;
import static org.opends.server.util.ServerConstants.*;
import static org.opends.server.util.StaticUtils.*;
import static org.opends.server.tools.ToolConstants.*;
import org.opends.server.controls.LDAPAssertionRequestControl;
/**
@@ -124,22 +123,27 @@
   * @param attributeVal    The attribute value to compare.
   * @param lines           The list of DNs to compare the attribute in.
   * @param compareOptions  The constraints for the compare request.
   * @return the LDAP result code for the operation
   *
   * @throws  IOException  If a problem occurs while communicating with the
   *                       Directory Server.
   *
   * @throws  LDAPException  If the server returns an error response.
   */
  public void readAndExecute(LDAPConnection connection, String attributeType,
  public int readAndExecute(LDAPConnection connection, String attributeType,
                             byte[] attributeVal, ArrayList<String> lines,
                             LDAPCompareOptions compareOptions)
         throws IOException, LDAPException
  {
    int aggResultCode = SUCCESS;
    for(String line : lines)
    {
      executeCompare(connection, attributeType, attributeVal, line,
                     compareOptions);
      int resultCode =
          executeCompare(connection, attributeType, attributeVal, line,
              compareOptions);
      aggResultCode = aggregateResultCode(aggResultCode, resultCode);
    }
    return aggResultCode;
  }
@@ -152,26 +156,57 @@
   * @param attributeVal    The attribute value to compare.
   * @param reader          The reader to read the list of DNs from.
   * @param compareOptions  The constraints for the compare request.
   * @return the LDAP result code for the operation
   *
   * @throws  IOException  If a problem occurs while communicating with the
   *                       Directory Server.
   *
   * @throws  LDAPException  If the server returns an error response.
   */
  public void readAndExecute(LDAPConnection connection, String attributeType,
  public int readAndExecute(LDAPConnection connection, String attributeType,
                             byte[] attributeVal, Reader reader,
                             LDAPCompareOptions compareOptions)
         throws IOException, LDAPException
  {
    int aggResultCode = 0;
    BufferedReader in = new BufferedReader(reader);
    String line = null;
    while ((line = in.readLine()) != null)
    {
      executeCompare(connection, attributeType, attributeVal, line,
                     compareOptions);
      int resultCode =
          executeCompare(connection, attributeType, attributeVal, line,
              compareOptions);
      aggResultCode = aggregateResultCode(aggResultCode, resultCode);
    }
    in.close();
    return aggResultCode;
  }
  /**
   * Aggregates a new result code to the existing aggregated result codes. This
   * method always overwrites the {@link LDAPResultCode#SUCCESS} and
   * {@link LDAPResultCode#COMPARE_TRUE} result codes with the new result code.
   * Then
   *
   * @param aggResultCodes
   *          the aggregated result codes (a.k.a "accumulator")
   * @param newResultCode
   *          the new result code to aggregate
   * @return the new aggregated result code
   */
  int aggregateResultCode(int aggResultCodes, int newResultCode)
  {
    if (aggResultCodes == SUCCESS || aggResultCodes == COMPARE_TRUE)
    {
      aggResultCodes = newResultCode;
    }
    else if (aggResultCodes == COMPARE_FALSE && newResultCode != COMPARE_TRUE)
    {
      aggResultCodes = newResultCode;
    }
    return aggResultCodes;
  }
@@ -183,13 +218,14 @@
   * @param attributeVal    The attribute value to compare.
   * @param line            The DN to compare attribute in.
   * @param compareOptions  The constraints for the compare request.
   * @return the LDAP result code for the operation
   *
   * @throws  IOException  If a problem occurs while communicating with the
   *                       Directory Server.
   *
   * @throws  LDAPException  If the server returns an error response.
   */
  private void executeCompare(LDAPConnection connection, String attributeType,
  private int executeCompare(LDAPConnection connection, String attributeType,
                              byte[] attributeVal, String line,
                              LDAPCompareOptions compareOptions)
          throws IOException, LDAPException
@@ -231,11 +267,10 @@
        }
        else
        {
          Message msg = INFO_OPERATION_FAILED.get("COMPARE");
          err.println(wrapText(msg, MAX_LINE_WIDTH));
          err.println(wrapText(ae.getMessage(), MAX_LINE_WIDTH));
          return;
          return OPERATIONS_ERROR;
        }
      }
@@ -274,13 +309,14 @@
          }
        } else
        {
          Message msg = INFO_OPERATION_FAILED.get("COMPARE");
          LDAPToolUtils.printErrorMessage(err, msg, resultCode, errorMessage,
                                          op.getMatchedDN());
        }
      }
      return resultCode;
    }
    return SUCCESS;
  }
  /**
@@ -362,6 +398,7 @@
    BooleanArgument   noop                   = null;
    BooleanArgument   saslExternal           = null;
    BooleanArgument   showUsage              = null;
    BooleanArgument   useCompareResultCode   = null;
    BooleanArgument   startTLS               = null;
    BooleanArgument   trustAll               = null;
    BooleanArgument   useSSL                 = null;
@@ -638,6 +675,13 @@
                                    OPTION_LONG_HELP,
                                    INFO_DESCRIPTION_SHOWUSAGE.get());
      argParser.addArgument(showUsage);
      useCompareResultCode =
          new BooleanArgument("usecompareresultcode", 'm',
              "useCompareResultCode", INFO_ENCPW_DESCRIPTION_USE_COMPARE_RESULT
                  .get());
      argParser.addArgument(useCompareResultCode);
      argParser.setUsageArgument(showUsage, out);
    } catch (ArgumentException ae)
    {
@@ -665,7 +709,7 @@
    // then print it and exit.
    if (argParser.usageOrVersionDisplayed())
    {
      return 0;
      return SUCCESS;
    }
    if(bindPassword.isPresent() && bindPasswordFile.isPresent())
@@ -1022,15 +1066,24 @@
          return CLIENT_SIDE_PARAM_ERROR;
        }
      }
      int resultCode;
      if(rdr != null)
      {
        ldapCompare.readAndExecute(connection, attributeType, attributeVal,
                                   rdr, compareOptions);
        resultCode =
            ldapCompare.readAndExecute(connection, attributeType, attributeVal,
                rdr, compareOptions);
      } else
      {
        ldapCompare.readAndExecute(connection, attributeType, attributeVal,
                                   dnStrings, compareOptions);
        resultCode =
            ldapCompare.readAndExecute(connection, attributeType, attributeVal,
                dnStrings, compareOptions);
      }
      if (useCompareResultCode.isPresent())
      {
        return resultCode;
      }
      return SUCCESS;
    } catch(LDAPException le)
    {
      if (debugEnabled())
@@ -1064,7 +1117,7 @@
        TRACER.debugCaught(DebugLogLevel.ERROR, e);
      }
      err.println(wrapText(e.getMessage(), MAX_LINE_WIDTH));
      return 1;
      return OPERATIONS_ERROR;
    } finally
    {
      if(connection != null)
@@ -1079,7 +1132,6 @@
        }
      }
    }
    return 0;
  }
  private boolean isScriptFriendly()
opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/tools/LDAPCompareTestCase.java
@@ -32,19 +32,24 @@
import java.io.File;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.concurrent.atomic.AtomicInteger;
import org.opends.server.TestCaseUtils;
import org.opends.server.api.Backend;
import org.opends.server.core.AddOperation;
import org.opends.server.core.DirectoryServer;
import org.opends.server.protocols.internal.InternalClientConnection;
import org.opends.server.types.DN;
import org.opends.server.types.Entry;
import org.opends.server.types.ResultCode;
import org.opends.server.util.Base64;
import org.opends.server.util.StaticUtils;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import org.opends.server.TestCaseUtils;
import org.opends.server.core.AddOperation;
import org.opends.server.core.DirectoryServer;
import org.opends.server.protocols.internal.InternalClientConnection;
import org.opends.server.types.Entry;
import org.opends.server.types.ResultCode;
import org.opends.server.util.Base64;
import static org.opends.server.protocols.ldap.LDAPResultCode.*;
import static org.testng.Assert.*;
@@ -66,18 +71,18 @@
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @BeforeClass()
  @BeforeClass
  public void startServerAndCreatePasswordFiles()
         throws Exception
  {
    TestCaseUtils.startServer();
    TestCaseUtils.dsconfig(
            "set-sasl-mechanism-handler-prop",
            "--handler-name", "DIGEST-MD5",
            "--set", "server-fqdn:" + "127.0.0.1");
    File pwFile = File.createTempFile("valid-bind-password-", ".txt");
    pwFile.deleteOnExit();
    FileWriter fileWriter = new FileWriter(pwFile);
@@ -94,7 +99,6 @@
  @AfterClass
  public void tearDown() throws Exception {
    TestCaseUtils.dsconfig(
            "set-sasl-mechanism-handler-prop",
            "--handler-name", "DIGEST-MD5",
@@ -384,7 +388,7 @@
  @Test(dataProvider = "invalidArgs")
  public void testInvalidArguments(String[] args, String invalidReason)
  {
    assertFalse(LDAPCompare.mainCompare(args, false, null, null) == 0,
    assertFalse(LDAPCompare.mainCompare(args, false, null, null) == SUCCESS,
                "Should have been invalid because:  " + invalidReason);
  }
@@ -413,7 +417,23 @@
      "o=test"
    };
    assertEquals(LDAPCompare.mainCompare(args, false, null, System.err), 0);
    String[] argsUseCompare =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-V", "2",
      "-D", "cn=Directory Manager",
      "-w", "password",
      "--noPropertiesFile",
      "--useCompareResultCode",
      "o:test",
      "o=test"
    };
    assertEquals(LDAPCompare.mainCompare(args, false, null, System.err),
        SUCCESS);
    assertEquals(LDAPCompare.mainCompare(argsUseCompare, false, null,
        System.err), COMPARE_TRUE);
  }
@@ -441,7 +461,23 @@
      "o=test"
    };
    assertEquals(LDAPCompare.mainCompare(args, false, null, System.err), 0);
    String[] argsUseCompare =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-V", "3",
      "-D", "cn=Directory Manager",
      "-w", "password",
      "--noPropertiesFile",
      "--useCompareResultCode",
      "o:test",
      "o=test"
    };
    assertEquals(LDAPCompare.mainCompare(args, false, null, System.err),
        SUCCESS);
    assertEquals(LDAPCompare.mainCompare(argsUseCompare, false, null,
        System.err), COMPARE_TRUE);
  }
@@ -469,10 +505,192 @@
      "o=test"
    };
    assertEquals(LDAPCompare.mainCompare(args, false, null, System.err), 0);
    String[] argsUseCompare =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-V", "3",
      "-D", "cn=Directory Manager",
      "-w", "password",
      "--noPropertiesFile",
      "--useCompareResultCode",
      "o:nottest",
      "o=test"
    };
    assertEquals(LDAPCompare.mainCompare(args, false, null, System.err),
        SUCCESS);
    assertEquals(LDAPCompare.mainCompare(argsUseCompare, false, null,
        System.err), COMPARE_FALSE);
  }
  /**
   * Tests two LDAPv3 compares in which the assertion is true for all.
   *
   * @throws Exception
   *           If an unexpected problem occurs.
   */
  @Test
  public void testMultipleCompareAllTrue() throws Exception
  {
    TestCaseUtils.initializeTestBackend(true);
    Backend memoryBackend =
        DirectoryServer.getBackend(TestCaseUtils.TEST_BACKEND_ID);
    String dn1 = "arg=success,o=test1,o=test";
    String dn2 = "arg=success,o=test2,o=test";
    addEntriesUpToParentDN(memoryBackend, DN.decode(dn1));
    addEntriesUpToParentDN(memoryBackend, DN.decode(dn2));
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-V", "3",
      "-D", "cn=Directory Manager",
      "-w", "password",
      "--noPropertiesFile",
      "--continueOnError",
      "arg:success",
      dn1,
      dn2
    };
    String[] argsUseCompare =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-V", "3",
      "-D", "cn=Directory Manager",
      "-w", "password",
      "--noPropertiesFile",
      "--useCompareResultCode",
      "--continueOnError",
      "arg:success",
      dn1,
      dn2
    };
    assertEquals(LDAPCompare.mainCompare(args, false, null, System.err),
        SUCCESS);
    assertEquals(LDAPCompare.mainCompare(argsUseCompare, false, null,
        System.err), COMPARE_TRUE);
  }
  /**
   * Tests two LDAPv3 compares in which one assertion is true and one is false.
   *
   * @throws Exception
   *           If an unexpected problem occurs.
   */
  @Test
  public void testMultipleCompareOneCompareIsFalse() throws Exception
  {
    TestCaseUtils.initializeTestBackend(true);
    Backend memoryBackend =
        DirectoryServer.getBackend(TestCaseUtils.TEST_BACKEND_ID);
    String dn1 = "arg=success,o=test1,o=test";
    String dn2 = "arg=fail,o=test2,o=test";
    addEntriesUpToParentDN(memoryBackend, DN.decode(dn1));
    addEntriesUpToParentDN(memoryBackend, DN.decode(dn2));
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-V", "3",
      "-D", "cn=Directory Manager",
      "-w", "password",
      "--noPropertiesFile",
      "--continueOnError",
      "arg:success",
      dn1,
      dn2
    };
    String[] argsUseCompare =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-V", "3",
      "-D", "cn=Directory Manager",
      "-w", "password",
      "--noPropertiesFile",
      "--useCompareResultCode",
      "--continueOnError",
      "arg:success",
      dn1,
      dn2
    };
    assertEquals(LDAPCompare.mainCompare(args, false, null, System.err),
        SUCCESS);
    assertEquals(LDAPCompare.mainCompare(argsUseCompare, false, null,
        System.err), COMPARE_FALSE);
  }
  /**
   * Tests two LDAPv3 compares in which one assertion is true and one returns no
   * such object.
   *
   * @throws Exception
   *           If an unexpected problem occurs.
   */
  @Test
  public void testMultipleCompareOneNoSuchObject() throws Exception
  {
    TestCaseUtils.initializeTestBackend(true);
    Backend memoryBackend =
        DirectoryServer.getBackend(TestCaseUtils.TEST_BACKEND_ID);
    String dn1 = "arg=success,o=test1,o=test";
    addEntriesUpToParentDN(memoryBackend, DN.decode(dn1));
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-V", "3",
      "-D", "cn=Directory Manager",
      "-w", "password",
      "--noPropertiesFile",
      "--continueOnError",
      "arg:success",
      dn1,
      "arg=fail,o=test2,o=test"
    };
    String[] argsUseCompare =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-V", "3",
      "-D", "cn=Directory Manager",
      "-w", "password",
      "--noPropertiesFile",
      "--useCompareResultCode",
      "--continueOnError",
      "arg:success",
      dn1,
      "arg=fail,o=test2,o=test"
    };
    assertEquals(LDAPCompare.mainCompare(args, false, null, System.err),
        SUCCESS);
    assertEquals(LDAPCompare.mainCompare(argsUseCompare, false, null,
        System.err), NO_SUCH_OBJECT);
  }
  private void addEntriesUpToParentDN(Backend backend, DN entryDN)
      throws Exception
  {
    if (!backend.entryExists(entryDN.getParent()))
    {
      addEntriesUpToParentDN(backend, entryDN.getParent());
    }
    backend.addEntry(StaticUtils.createEntry(entryDN), null);
  }
  /**
   * Tests a simple compare using SSL with blind trust.
@@ -498,7 +716,24 @@
      "o=test"
    };
    assertEquals(LDAPCompare.mainCompare(args, false, null, System.err), 0);
    String[] argsUseCompare =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapsPort()),
      "-Z",
      "-X",
      "-D", "cn=Directory Manager",
      "-w", "password",
      "--noPropertiesFile",
      "--useCompareResultCode",
      "o:test",
      "o=test"
    };
    assertEquals(LDAPCompare.mainCompare(args, false, null, System.err),
        SUCCESS);
    assertEquals(LDAPCompare.mainCompare(argsUseCompare, false, null,
        System.err), COMPARE_TRUE);
  }
@@ -530,7 +765,24 @@
      "o=test"
    };
    assertEquals(LDAPCompare.mainCompare(args, false, null, System.err), 0);
    String[] argsUseCompare =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapsPort()),
      "-Z",
      "-P", trustStorePath,
      "-D", "cn=Directory Manager",
      "-w", "password",
      "--noPropertiesFile",
      "--useCompareResultCode",
      "o:test",
      "o=test"
    };
    assertEquals(LDAPCompare.mainCompare(args, false, null, System.err),
        SUCCESS);
    assertEquals(LDAPCompare.mainCompare(argsUseCompare, false, null,
        System.err), COMPARE_TRUE);
  }
@@ -559,7 +811,24 @@
      "o=test"
    };
    assertEquals(LDAPCompare.mainCompare(args, false, null, System.err), 0);
    String[] argsUseCompare =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-q",
      "-X",
      "-D", "cn=Directory Manager",
      "-w", "password",
      "--noPropertiesFile",
      "--useCompareResultCode",
      "o:test",
      "o=test"
    };
    assertEquals(LDAPCompare.mainCompare(args, false, null, System.err),
        SUCCESS);
    assertEquals(LDAPCompare.mainCompare(argsUseCompare, false, null,
        System.err), COMPARE_TRUE);
  }
@@ -591,7 +860,24 @@
      "o=test"
    };
    assertEquals(LDAPCompare.mainCompare(args, false, null, System.err), 0);
    String[] argsUseCompare =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-q",
      "-P", trustStorePath,
      "-D", "cn=Directory Manager",
      "-w", "password",
      "--noPropertiesFile",
      "--useCompareResultCode",
      "o:test",
      "o=test"
    };
    assertEquals(LDAPCompare.mainCompare(args, false, null, System.err),
        SUCCESS);
    assertEquals(LDAPCompare.mainCompare(argsUseCompare, false, null,
        System.err), COMPARE_TRUE);
  }
@@ -646,7 +932,25 @@
      "cn=Test User,o=test"
    };
    assertEquals(LDAPCompare.mainCompare(args, false, null, System.err), 0);
    String[] argsUseCompare =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapsPort()),
      "-Z",
      "-K", keyStorePath,
      "-W", "password",
      "-P", trustStorePath,
      "--noPropertiesFile",
      "--useCompareResultCode",
      "-r",
      "cn:Test User",
      "cn=Test User,o=test"
    };
    assertEquals(LDAPCompare.mainCompare(args, false, null, System.err),
        SUCCESS);
    assertEquals(LDAPCompare.mainCompare(argsUseCompare, false, null,
        System.err), COMPARE_TRUE);
  }
@@ -702,7 +1006,26 @@
      "cn=Test User,o=test"
    };
    assertEquals(LDAPCompare.mainCompare(args, false, null, System.err), 0);
    String[] argsUseCompare =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapsPort()),
      "-Z",
      "-K", keyStorePath,
      "-W", "password",
      "-N", "client-cert",
      "-P", trustStorePath,
      "--noPropertiesFile",
      "--useCompareResultCode",
      "-r",
      "cn:Test User",
      "cn=Test User,o=test"
    };
    assertEquals(LDAPCompare.mainCompare(args, false, null, System.err),
        SUCCESS);
    assertEquals(LDAPCompare.mainCompare(argsUseCompare, false, null,
        System.err), COMPARE_TRUE);
  }
@@ -758,7 +1081,7 @@
      "cn=Test User,o=test"
    };
    assertFalse(LDAPCompare.mainCompare(args, false, null, null) == 0);
    assertFalse(LDAPCompare.mainCompare(args, false, null, null) == SUCCESS);
  }
@@ -813,7 +1136,25 @@
      "cn=Test User,o=test"
    };
    assertEquals(LDAPCompare.mainCompare(args, false, null, System.err), 0);
    String[] argsUseCompare =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-q",
      "-K", keyStorePath,
      "-W", "password",
      "-P", trustStorePath,
      "--noPropertiesFile",
      "--useCompareResultCode",
      "-r",
      "cn:Test User",
      "cn=Test User,o=test"
    };
    assertEquals(LDAPCompare.mainCompare(args, false, null, System.err),
        SUCCESS);
    assertEquals(LDAPCompare.mainCompare(argsUseCompare, false, null,
        System.err), COMPARE_TRUE);
  }
@@ -864,7 +1205,23 @@
      "uid=test.user,o=test"
    };
    assertEquals(LDAPCompare.mainCompare(args, false, null, System.err), 0);
    String[] argsUseCompare =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-o", "mech=CRAM-MD5",
      "-o", "authid=u:test.user",
      "-w", "password",
      "--noPropertiesFile",
      "--useCompareResultCode",
      "givenName:Test",
      "uid=test.user,o=test"
    };
    assertEquals(LDAPCompare.mainCompare(args, false, null, System.err),
        SUCCESS);
    assertEquals(LDAPCompare.mainCompare(argsUseCompare, false, null,
        System.err), COMPARE_TRUE);
  }
@@ -903,7 +1260,7 @@
                         e.getOperationalAttributes());
    assertEquals(addOperation.getResultCode(), ResultCode.SUCCESS);
    String[] args =
    {
      "-h", "127.0.0.1",
@@ -917,7 +1274,24 @@
      "uid=test.user,o=test"
    };
    assertEquals(LDAPCompare.mainCompare(args, false, null, System.err), 0);
    String[] argsUseCompare =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-o", "mech=DIGEST-MD5",
      "-o", "authid=u:test.user",
      "-o", "authzid=u:test.user",
      "-w", "password",
      "--useCompareResultCode",
      "--noPropertiesFile",
      "givenName:Test",
      "uid=test.user,o=test"
    };
    assertEquals(LDAPCompare.mainCompare(args, false, null, System.err),
        SUCCESS);
    assertEquals(LDAPCompare.mainCompare(argsUseCompare, false, null,
        System.err), COMPARE_TRUE);
  }
@@ -965,7 +1339,23 @@
      "uid=test.user,o=test"
    };
    assertEquals(LDAPCompare.mainCompare(args, false, null, System.err), 0);
    String[] argsUseCompare =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-o", "mech=PLAIN",
      "-o", "authid=dn:cn=Directory Manager",
      "-w", "password",
      "--noPropertiesFile",
      "--useCompareResultCode",
      "givenName:Test",
      "uid=test.user,o=test"
    };
    assertEquals(LDAPCompare.mainCompare(args, false, null, System.err),
        SUCCESS);
    assertEquals(LDAPCompare.mainCompare(argsUseCompare, false, null,
        System.err), COMPARE_TRUE);
  }
@@ -994,7 +1384,23 @@
      "o=test"
    };
    assertEquals(LDAPCompare.mainCompare(args, false, null, System.err), 0);
    String[] argsUseCompare =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-V", "3",
      "-D", "cn=Directory Manager",
      "-w", "password",
      "--useCompareResultCode",
      "--noPropertiesFile",
      "o::" + Base64.encode("test".getBytes("UTF-8")),
      "o=test"
    };
    assertEquals(LDAPCompare.mainCompare(args, false, null, System.err),
        SUCCESS);
    assertEquals(LDAPCompare.mainCompare(argsUseCompare, false, null,
        System.err), COMPARE_TRUE);
  }
@@ -1023,7 +1429,7 @@
      "o=test"
    };
    assertFalse(LDAPCompare.mainCompare(args, false, null, null) == 0);
    assertFalse(LDAPCompare.mainCompare(args, false, null, null) == SUCCESS);
  }
@@ -1057,7 +1463,23 @@
      "o=test"
    };
    assertEquals(LDAPCompare.mainCompare(args, false, null, System.err), 0);
    String[] argsUseCompare =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-V", "3",
      "-D", "cn=Directory Manager",
      "-w", "password",
      "--useCompareResultCode",
      "--noPropertiesFile",
      "o:<" + f.getAbsolutePath(),
      "o=test"
    };
    assertEquals(LDAPCompare.mainCompare(args, false, null, System.err),
        SUCCESS);
    assertEquals(LDAPCompare.mainCompare(argsUseCompare, false, null,
        System.err), COMPARE_TRUE);
  }
@@ -1084,7 +1506,7 @@
      "o=test"
    };
    assertFalse(LDAPCompare.mainCompare(args, false, null, null) == 0);
    assertFalse(LDAPCompare.mainCompare(args, false, null, null) == SUCCESS);
  }
@@ -1114,7 +1536,24 @@
      "o=test"
    };
    assertEquals(LDAPCompare.mainCompare(args, false, null, System.err), 0);
    String[] argsUseCompare =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-V", "3",
      "-D", "cn=Directory Manager",
      "-w", "password",
      "--assertionFilter", "(o=test)",
      "--useCompareResultCode",
      "--noPropertiesFile",
      "o:test",
      "o=test"
    };
    assertEquals(LDAPCompare.mainCompare(args, false, null, System.err),
        SUCCESS);
    assertEquals(LDAPCompare.mainCompare(argsUseCompare, false, null,
        System.err), COMPARE_TRUE);
  }
@@ -1143,8 +1582,7 @@
      "o:test",
      "o=test"
    };
    assertFalse(LDAPCompare.mainCompare(args, false, null, null) == 0);
    assertFalse(LDAPCompare.mainCompare(args, false, null, null) == SUCCESS);
  }
@@ -1211,7 +1649,7 @@
      "o:test",
    };
    assertFalse(LDAPCompare.mainCompare(args, false, null, null) == 0);
    assertFalse(LDAPCompare.mainCompare(args, false, null, null) == SUCCESS);
  }
@@ -1223,13 +1661,47 @@
  public void testHelp()
  {
    String[] args = { "--help" };
    assertEquals(LDAPCompare.mainCompare(args, false, null, null), 0);
    assertEquals(LDAPCompare.mainCompare(args, false, null, null), SUCCESS);
    args = new String[] { "-H" };
    assertEquals(LDAPCompare.mainCompare(args, false, null, null), 0);
    assertEquals(LDAPCompare.mainCompare(args, false, null, null), SUCCESS);
    args = new String[] { "-?" };
    assertEquals(LDAPCompare.mainCompare(args, false, null, null), 0);
    assertEquals(LDAPCompare.mainCompare(args, false, null, null), SUCCESS);
  }
  @DataProvider(name = "aggregateResults")
  public Object[][] getAggregateResultCodeParamsAndResults()
  {
    return new Object[][] { { SUCCESS, SUCCESS, SUCCESS },
      { SUCCESS, COMPARE_TRUE, COMPARE_TRUE },
      { SUCCESS, COMPARE_FALSE, COMPARE_FALSE },
      { SUCCESS, OPERATIONS_ERROR, OPERATIONS_ERROR },
      { COMPARE_TRUE, COMPARE_TRUE, COMPARE_TRUE },
      { COMPARE_TRUE, COMPARE_FALSE, COMPARE_FALSE },
      { COMPARE_TRUE, OPERATIONS_ERROR, OPERATIONS_ERROR },
      { COMPARE_FALSE, COMPARE_TRUE, COMPARE_FALSE },
      { COMPARE_FALSE, COMPARE_FALSE, COMPARE_FALSE },
      { COMPARE_FALSE, OPERATIONS_ERROR, OPERATIONS_ERROR },
      { OPERATIONS_ERROR, COMPARE_TRUE, OPERATIONS_ERROR },
      { OPERATIONS_ERROR, COMPARE_FALSE, OPERATIONS_ERROR },
      { OPERATIONS_ERROR, OPERATIONS_ERROR, OPERATIONS_ERROR } };
  }
  /**
   * Test the results of calling function
   * {@link LDAPCompare#aggregateResultCode(int, int)}.
   */
  @Test(dataProvider = "aggregateResults")
  public void testAggregateResultCode(int currentAggregatedResult,
      int newResultCode, int finalAggregatedResult)
  {
    LDAPCompare obj = new LDAPCompare(new AtomicInteger(), null, null);
    assertEquals(obj
        .aggregateResultCode(currentAggregatedResult, newResultCode),
        finalAggregatedResult);
  }
}