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

neil_a_wilson
09.57.2006 60646df757715c436c47d28b6c530935e9f0ad4f
Add a number of test cases for the LDAP tools (LDAPSearch, LDAPModify,
LDAPCompare, LDAPDelete, and LDAPPasswordModify).
6 files added
1 files modified
6068 ■■■■■ changed files
opends/tests/unit-tests-testng/src/server/org/opends/server/TestCaseUtils.java 25 ●●●●● patch | view | raw | blame | history
opends/tests/unit-tests-testng/src/server/org/opends/server/tools/LDAPCompareTestCase.java 1051 ●●●●● patch | view | raw | blame | history
opends/tests/unit-tests-testng/src/server/org/opends/server/tools/LDAPDeleteTestCase.java 836 ●●●●● patch | view | raw | blame | history
opends/tests/unit-tests-testng/src/server/org/opends/server/tools/LDAPModifyTestCase.java 1626 ●●●●● patch | view | raw | blame | history
opends/tests/unit-tests-testng/src/server/org/opends/server/tools/LDAPPasswordModifyTestCase.java 1150 ●●●●● patch | view | raw | blame | history
opends/tests/unit-tests-testng/src/server/org/opends/server/tools/LDAPSearchTestCase.java 1334 ●●●●● patch | view | raw | blame | history
opends/tests/unit-tests-testng/src/server/org/opends/server/tools/ToolsTestCase.java 46 ●●●●● patch | view | raw | blame | history
opends/tests/unit-tests-testng/src/server/org/opends/server/TestCaseUtils.java
@@ -593,4 +593,29 @@
  public static List<Entry> makeEntries(String... lines) throws Exception {
     return entriesFromLdifString(makeLdif(lines));
  }
  /**
   * Creates a temporary text file with the specified contents.  It will be
   * marked for automatic deletion when the JVM exits.
   *
   * @return  The absolute path to the file that was created.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  public static String createTempFile(String... lines)
          throws Exception
  {
    File f = File.createTempFile("LDAPModifyTestCase", ".txt");
    f.deleteOnExit();
    FileWriter w = new FileWriter(f);
    for (String s : lines)
    {
      w.write(s + System.getProperty("line.separator"));
    }
    w.close();
    return f.getAbsolutePath();
  }
}
opends/tests/unit-tests-testng/src/server/org/opends/server/tools/LDAPCompareTestCase.java
New file
@@ -0,0 +1,1051 @@
/*
 * 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.tools;
import java.io.File;
import java.io.FileWriter;
import java.util.ArrayList;
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.OperatingSystem;
import org.opends.server.types.ResultCode;
import org.opends.server.util.Base64;
import static org.testng.Assert.*;
import static org.opends.server.util.ServerConstants.*;
/**
 * A set of test cases for the LDAPCompare tool.
 */
public class LDAPCompareTestCase
       extends ToolsTestCase
{
  // The path to a file containing an invalid bind password.
  private String invalidPasswordFile;
  // The path to a file containing a valid bind password.
  private String validPasswordFile;
  /**
   * Ensures that the Directory Server is running and performs other necessary
   * setup.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @BeforeClass()
  public void startServerAndCreatePasswordFiles()
         throws Exception
  {
    TestCaseUtils.startServer();
    File pwFile = File.createTempFile("valid-bind-password-", ".txt");
    pwFile.deleteOnExit();
    FileWriter fileWriter = new FileWriter(pwFile);
    fileWriter.write("password" + System.getProperty("line.separator"));
    fileWriter.close();
    validPasswordFile = pwFile.getAbsolutePath();
    pwFile = File.createTempFile("invalid-bind-password-", ".txt");
    pwFile.deleteOnExit();
    fileWriter = new FileWriter(pwFile);
    fileWriter.write("wrongPassword" + System.getProperty("line.separator"));
    fileWriter.close();
    invalidPasswordFile = pwFile.getAbsolutePath();
  }
  /**
   * Retrieves sets of invalid arguments that may not be used to initialize
   * the LDAPCompare tool.
   *
   * @return  Sets of invalid arguments that may not be used to initialize the
   *          LDAPCompare tool.
   */
  @DataProvider(name = "invalidArgs")
  public Object[][] getInvalidArgumentLists()
  {
    ArrayList<String[]> argLists = new ArrayList<String[]>();
    String[] args = {}; // No arguments
    argLists.add(args);
    args = new String[] // No value for "-D" argument.
    {
      "-D",
    };
    argLists.add(args);
    args = new String[] // No value for "-w" argument.
    {
      "-w",
    };
    argLists.add(args);
    args = new String[] // No value for "-j" argument.
    {
      "-j",
    };
    argLists.add(args);
    args = new String[] // No value for "-i" argument.
    {
      "-i",
    };
    argLists.add(args);
    args = new String[] // No value for "-K" argument.
    {
      "-K",
    };
    argLists.add(args);
    args = new String[] // No value for "-P" argument.
    {
      "-P",
    };
    argLists.add(args);
    args = new String[] // No value for "-W" argument.
    {
      "-W",
    };
    argLists.add(args);
    args = new String[] // No value for "-h" argument.
    {
      "-h",
    };
    argLists.add(args);
    args = new String[] // No value for "-p" argument.
    {
      "-p",
    };
    argLists.add(args);
    args = new String[] // No value for "-V" argument.
    {
      "-V",
    };
    argLists.add(args);
    args = new String[] // No value for "-f" argument.
    {
      "-f",
    };
    argLists.add(args);
    args = new String[] // No value for "-J" argument.
    {
      "-J",
    };
    argLists.add(args);
    args = new String[] // No value for "-o" argument.
    {
      "-o",
    };
    argLists.add(args);
    args = new String[] // No value for "--assertionFilter" argument.
    {
      "--assertionFilter",
    };
    argLists.add(args);
    args = new String[] // Invalid short argument
    {
      "-I"
    };
    argLists.add(args);
    args = new String[] // Invalid long argument
    {
      "--invalidLongArgument"
    };
    argLists.add(args);
    args = new String[] // Invalid assertion filter
    {
      "--assertionFilter", "(invalidfilter)",
      "uid:test.user",
      "uid=test.user,o=test"
    };
    argLists.add(args);
    args = new String[] // Invalid bind password file path
    {
      "-D", "cn=Directory Manager",
      "-j", "no.such.file",
      "uid:test.user",
      "uid=test.user,o=test"
    };
    argLists.add(args);
    args = new String[] // Both bind password and password file
    {
      "-D", "cn=Directory Manager",
      "-w", "password",
      "-j", validPasswordFile,
      "uid:test.user",
      "uid=test.user,o=test"
    };
    argLists.add(args);
    args = new String[] // Non-numeric LDAP version.
    {
      "-V", "nonnumeric",
      "uid:test.user",
      "uid=test.user,o=test"
    };
    argLists.add(args);
    args = new String[] // Invalid LDAP version.
    {
      "-V", "1",
      "uid:test.user",
      "uid=test.user,o=test"
    };
    argLists.add(args);
    args = new String[] // Invalid DN file path.
    {
      "-f", "no.such.file",
      "uid:test.user",
      "uid=test.user,o=test"
    };
    argLists.add(args);
    args = new String[] // Invalid control criticality
    {
      "-J", "1.2.3.4:invalidcriticality",
      "uid:test.user",
      "uid=test.user,o=test"
    };
    argLists.add(args);
    args = new String[] // Non-numeric port
    {
      "-p", "nonnumeric",
      "uid:test.user",
      "uid=test.user,o=test"
    };
    argLists.add(args);
    args = new String[] // Port value out of range.
    {
      "-p", "999999",
      "uid:test.user",
      "uid=test.user,o=test"
    };
    argLists.add(args);
    args = new String[] // SASL external without SSL or StartTLS
    {
      "-r",
      "-K", "key.store.file",
      "uid:test.user",
      "uid=test.user,o=test"
    };
    argLists.add(args);
    args = new String[] // SASL external without keystore file
    {
      "-Z",
      "-r",
      "uid:test.user",
      "uid=test.user,o=test"
    };
    argLists.add(args);
    args = new String[] // No trailing arguments
    {
      "-D", "cn=Directory Manager",
      "-w", "password"
    };
    argLists.add(args);
    args = new String[] // Only one trailing argument.
    {
      "-D", "cn=Directory Manager",
      "-w", "password",
      "uid:test.user",
    };
    argLists.add(args);
    args = new String[] // Malformed attribute-value assertion
    {
      "-D", "cn=Directory Manager",
      "-w", "password",
      "malformed",
      "uid=test.user,o=test"
    };
    argLists.add(args);
    Object[][] returnArray = new Object[argLists.size()][1];
    for (int i=0; i < argLists.size(); i++)
    {
      returnArray[i][0] = argLists.get(i);
    }
    return returnArray;
  }
  /**
   * Tests the LDAPCompare tool with sets of invalid arguments.
   *
   * @param  args  The set of arguments to use for the LDAPCompare tool.
   */
  @Test(dataProvider = "invalidArgs")
  public void testInvalidArguments(String[] args)
  {
    assertFalse(LDAPCompare.mainCompare(args, false, null, null) == 0);
  }
  /**
   * Tests a simple LDAPv2 compare.
   *
   * @throws  Exception  If an unexpectd problem occurs.
   */
  @Test()
  public void testSimpleLDAPv2Compare()
         throws Exception
  {
    TestCaseUtils.initializeTestBackend(true);
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-V", "2",
      "-D", "cn=Directory Manager",
      "-w", "password",
      "o:test",
      "o=test"
    };
    assertEquals(LDAPCompare.mainCompare(args, false, null, System.err), 0);
  }
  /**
   * Tests a simple LDAPv3 compare in which the assertion is true.
   *
   * @throws  Exception  If an unexpectd problem occurs.
   */
  @Test()
  public void testSimpleLDAPv3CompareTrue()
         throws Exception
  {
    TestCaseUtils.initializeTestBackend(true);
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-V", "3",
      "-D", "cn=Directory Manager",
      "-w", "password",
      "o:test",
      "o=test"
    };
    assertEquals(LDAPCompare.mainCompare(args, false, null, System.err), 0);
  }
  /**
   * Tests a simple LDAPv3 compare in which the assertion is false.
   *
   * @throws  Exception  If an unexpectd problem occurs.
   */
  @Test()
  public void testSimpleLDAPv3CompareFalse()
         throws Exception
  {
    TestCaseUtils.initializeTestBackend(true);
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-V", "3",
      "-D", "cn=Directory Manager",
      "-w", "password",
      "o:nottest",
      "o=test"
    };
    assertEquals(LDAPCompare.mainCompare(args, false, null, System.err), 0);
  }
  /**
   * Tests a simple compare using SSL with blind trust.
   *
   * @throws  Exception  If an unexpectd problem occurs.
   */
  @Test()
  public void testSSLBlindTrust()
         throws Exception
  {
    TestCaseUtils.initializeTestBackend(true);
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapsPort()),
      "-Z",
      "-X",
      "-D", "cn=Directory Manager",
      "-w", "password",
      "o:test",
      "o=test"
    };
    assertEquals(LDAPCompare.mainCompare(args, false, null, System.err), 0);
  }
  /**
   * Tests a simple compare using SSL with a trust store.
   *
   * @throws  Exception  If an unexpectd problem occurs.
   */
  @Test()
  public void testSSLTrustStore()
         throws Exception
  {
    TestCaseUtils.initializeTestBackend(true);
    String trustStorePath = DirectoryServer.getServerRoot() + File.separator +
                            "config" + File.separator + "client.truststore";
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapsPort()),
      "-Z",
      "-P", trustStorePath,
      "-D", "cn=Directory Manager",
      "-w", "password",
      "o:test",
      "o=test"
    };
    assertEquals(LDAPCompare.mainCompare(args, false, null, System.err), 0);
  }
  /**
   * Tests a simple compare using StartTLS with blind trust.
   *
   * @throws  Exception  If an unexpectd problem occurs.
   */
  @Test()
  public void testStartTLSBlindTrust()
         throws Exception
  {
    TestCaseUtils.initializeTestBackend(true);
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-q",
      "-X",
      "-D", "cn=Directory Manager",
      "-w", "password",
      "o:test",
      "o=test"
    };
    assertEquals(LDAPCompare.mainCompare(args, false, null, System.err), 0);
  }
  /**
   * Tests a simple compare using StartTLS with a trust store.
   *
   * @throws  Exception  If an unexpectd problem occurs.
   */
  @Test()
  public void testStartTLSTrustStore()
         throws Exception
  {
    TestCaseUtils.initializeTestBackend(true);
    String trustStorePath = DirectoryServer.getServerRoot() + File.separator +
                            "config" + File.separator + "client.truststore";
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-q",
      "-P", trustStorePath,
      "-D", "cn=Directory Manager",
      "-w", "password",
      "o:test",
      "o=test"
    };
    assertEquals(LDAPCompare.mainCompare(args, false, null, System.err), 0);
  }
  /**
   * Tests a simple LDAP compare over SSL using a trust store and SASL EXTERNAL
   * authentication.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test()
  public void testSimpleCompareSSLTrustStoreSASLExternal()
         throws Exception
  {
    TestCaseUtils.initializeTestBackend(true);
    Entry e = TestCaseUtils.makeEntry(
         "dn: cn=Test User,o=test",
         "objectClass: top",
         "objectClass: person",
         "objectClass: organizationalPerson",
         "objectClass: inetOrgPerson",
         "cn: Test User",
         "givenName: Test",
         "sn: User");
    InternalClientConnection conn =
         InternalClientConnection.getRootConnection();
    AddOperation addOperation =
         conn.processAdd(e.getDN(), e.getObjectClasses(), e.getUserAttributes(),
                         e.getOperationalAttributes());
    assertEquals(addOperation.getResultCode(), ResultCode.SUCCESS);
    String keyStorePath   = DirectoryServer.getServerRoot() + File.separator +
                            "config" + File.separator + "client.keystore";
    String trustStorePath = DirectoryServer.getServerRoot() + File.separator +
                            "config" + File.separator + "client.truststore";
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapsPort()),
      "-Z",
      "-K", keyStorePath,
      "-W", "password",
      "-P", trustStorePath,
      "-r",
      "cn:Test User",
      "cn=Test User,o=test"
    };
    assertEquals(LDAPCompare.mainCompare(args, false, null, System.err), 0);
  }
  /**
   * Tests a simple LDAP compare using StartTLS with a trust store and SASL
   * EXTERNAL authentication.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test()
  public void testSimpleCompareStartTLSTrustStoreSASLExternal()
         throws Exception
  {
    TestCaseUtils.initializeTestBackend(true);
    Entry e = TestCaseUtils.makeEntry(
         "dn: cn=Test User,o=test",
         "objectClass: top",
         "objectClass: person",
         "objectClass: organizationalPerson",
         "objectClass: inetOrgPerson",
         "cn: Test User",
         "givenName: Test",
         "sn: User");
    InternalClientConnection conn =
         InternalClientConnection.getRootConnection();
    AddOperation addOperation =
         conn.processAdd(e.getDN(), e.getObjectClasses(), e.getUserAttributes(),
                         e.getOperationalAttributes());
    assertEquals(addOperation.getResultCode(), ResultCode.SUCCESS);
    String keyStorePath   = DirectoryServer.getServerRoot() + File.separator +
                            "config" + File.separator + "client.keystore";
    String trustStorePath = DirectoryServer.getServerRoot() + File.separator +
                            "config" + File.separator + "client.truststore";
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-q",
      "-K", keyStorePath,
      "-W", "password",
      "-P", trustStorePath,
      "-r",
      "cn:Test User",
      "cn=Test User,o=test"
    };
    assertEquals(LDAPCompare.mainCompare(args, false, null, System.err), 0);
  }
  /**
   * Tests a simple compare operation using CRAM-MD5 authentication.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test()
  public void testCRAMMD5()
         throws Exception
  {
    TestCaseUtils.initializeTestBackend(true);
    Entry e = TestCaseUtils.makeEntry(
         "dn: uid=test.user,o=test",
         "objectClass: top",
         "objectClass: person",
         "objectClass: organizationalPerson",
         "objectClass: inetOrgPerson",
         "uid: test.user",
         "givenName: Test",
         "sn: User",
         "cn: Test User",
         "userPassword: password",
         "pwdPolicySubentry: cn=Clear UserPassword Policy," +
              "cn=Password Policies,cn=config");
    InternalClientConnection conn =
         InternalClientConnection.getRootConnection();
    AddOperation addOperation =
         conn.processAdd(e.getDN(), e.getObjectClasses(), e.getUserAttributes(),
                         e.getOperationalAttributes());
    assertEquals(addOperation.getResultCode(), ResultCode.SUCCESS);
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-o", "mech=CRAM-MD5",
      "-o", "authid=u:test.user",
      "-w", "password",
      "givenName:Test",
      "uid=test.user,o=test"
    };
    assertEquals(LDAPCompare.mainCompare(args, false, null, System.err), 0);
  }
  /**
   * Tests a simple compare operation using DIGEST-MD5 authentication.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test()
  public void testDigestMD5()
         throws Exception
  {
    TestCaseUtils.initializeTestBackend(true);
    Entry e = TestCaseUtils.makeEntry(
         "dn: uid=test.user,o=test",
         "objectClass: top",
         "objectClass: person",
         "objectClass: organizationalPerson",
         "objectClass: inetOrgPerson",
         "uid: test.user",
         "givenName: Test",
         "sn: User",
         "cn: Test User",
         "userPassword: password",
         "pwdPolicySubentry: cn=Clear UserPassword Policy," +
              "cn=Password Policies,cn=config");
    InternalClientConnection conn =
         InternalClientConnection.getRootConnection();
    AddOperation addOperation =
         conn.processAdd(e.getDN(), e.getObjectClasses(), e.getUserAttributes(),
                         e.getOperationalAttributes());
    assertEquals(addOperation.getResultCode(), ResultCode.SUCCESS);
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-o", "authid=u:test.user",
      "-o", "authzid=u:test.user",
      "-o", "realm=o=test",
      "-w", "password",
      "givenName:Test",
      "uid=test.user,o=test"
    };
    assertEquals(LDAPCompare.mainCompare(args, false, null, System.err), 0);
  }
  /**
   * Tests a simple compare operation using PLAIN authentication.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test()
  public void testPLAIN()
         throws Exception
  {
    TestCaseUtils.initializeTestBackend(true);
    Entry e = TestCaseUtils.makeEntry(
         "dn: uid=test.user,o=test",
         "objectClass: top",
         "objectClass: person",
         "objectClass: organizationalPerson",
         "objectClass: inetOrgPerson",
         "uid: test.user",
         "givenName: Test",
         "sn: User",
         "cn: Test User",
         "userPassword: password");
    InternalClientConnection conn =
         InternalClientConnection.getRootConnection();
    AddOperation addOperation =
         conn.processAdd(e.getDN(), e.getObjectClasses(), e.getUserAttributes(),
                         e.getOperationalAttributes());
    assertEquals(addOperation.getResultCode(), ResultCode.SUCCESS);
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-o", "mech=PLAIN",
      "-o", "authid=dn:cn=Directory Manager",
      "-w", "password",
      "givenName:Test",
      "uid=test.user,o=test"
    };
    assertEquals(LDAPCompare.mainCompare(args, false, null, System.err), 0);
  }
  /**
   * Tests a a comparison in which the assertion value is base64-encoded with a
   * valid encoding.
   *
   * @throws  Exception  If an unexpectd problem occurs.
   */
  @Test()
  public void testCompareValidBase64Assertion()
         throws Exception
  {
    TestCaseUtils.initializeTestBackend(true);
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-V", "3",
      "-D", "cn=Directory Manager",
      "-w", "password",
      "o::" + Base64.encode("test".getBytes("UTF-8")),
      "o=test"
    };
    assertEquals(LDAPCompare.mainCompare(args, false, null, System.err), 0);
  }
  /**
   * Tests a a comparison in which the assertion value should be base64-encoded
   * but uses an incorrect encoding.
   *
   * @throws  Exception  If an unexpectd problem occurs.
   */
  @Test()
  public void testCompareInvalidBase64Assertion()
         throws Exception
  {
    TestCaseUtils.initializeTestBackend(true);
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-V", "3",
      "-D", "cn=Directory Manager",
      "-w", "password",
      "o::***invalidencoding***",
      "o=test"
    };
    assertFalse(LDAPCompare.mainCompare(args, false, null, null) == 0);
  }
  /**
   * Tests a a comparison in which the assertion value is contained in a file.
   *
   * @throws  Exception  If an unexpectd problem occurs.
   */
  @Test()
  public void testCompareAssertionValueFromFile()
         throws Exception
  {
    TestCaseUtils.initializeTestBackend(true);
    File f = File.createTempFile("testCompareAssertionValueFromFile", ".txt");
    f.deleteOnExit();
    FileWriter w = new FileWriter(f);
    w.write("test");
    w.close();
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-V", "3",
      "-D", "cn=Directory Manager",
      "-w", "password",
      "o:<" + f.getAbsolutePath(),
      "o=test"
    };
    assertEquals(LDAPCompare.mainCompare(args, false, null, System.err), 0);
  }
  /**
   * Tests a a comparison in which the assertion value is contained in a file
   * that does not exist.
   *
   * @throws  Exception  If an unexpectd problem occurs.
   */
  @Test()
  public void testCompareAssertionValueFromNonExistentFile()
         throws Exception
  {
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-V", "3",
      "-D", "cn=Directory Manager",
      "-w", "password",
      "o:<does.not.exist",
      "o=test"
    };
    assertFalse(LDAPCompare.mainCompare(args, false, null, null) == 0);
  }
  /**
   * Tests a a comparison using the LDAP assertion control in which the
   * assertion is true.
   *
   * @throws  Exception  If an unexpectd problem occurs.
   */
  @Test()
  public void testCompareLDAPAssertionControlTrue()
         throws Exception
  {
    TestCaseUtils.initializeTestBackend(true);
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-V", "3",
      "-D", "cn=Directory Manager",
      "-w", "password",
      "--assertionFilter", "(o=test)",
      "o:test",
      "o=test"
    };
    assertEquals(LDAPCompare.mainCompare(args, false, null, System.err), 0);
  }
  /**
   * Tests a a comparison using the LDAP assertion control in which the
   * assertion is not true.
   *
   * @throws  Exception  If an unexpectd problem occurs.
   */
  @Test()
  public void testCompareLDAPAssertionControlNotTrue()
         throws Exception
  {
    TestCaseUtils.initializeTestBackend(true);
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-V", "3",
      "-D", "cn=Directory Manager",
      "-w", "password",
      "--assertionFilter", "(o=notAMatch)",
      "o:test",
      "o=test"
    };
    assertFalse(LDAPCompare.mainCompare(args, false, null, null) == 0);
  }
  /**
   * Tests a a compare operation reading the DNs to compare from a file.  Some
   * of the compares will succeed and others will not.
   *
   * @throws  Exception  If an unexpectd problem occurs.
   */
  @Test()
  public void testCompareDNsFromFile()
         throws Exception
  {
    TestCaseUtils.initializeTestBackend(true);
    String path = TestCaseUtils.createTempFile(
         "o=test",
         "dc=example,dc=com",
         "o=nonexistentsuffix",
         "malformed",
         "o=nonexistent,o=test");
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-V", "3",
      "-D", "cn=Directory Manager",
      "-w", "password",
      "-c",
      "-f", path,
      "o:test",
    };
    LDAPCompare.mainCompare(args, false, null, null);
  }
  /**
   * Tests a a compare operation reading the DNs to compare from a file that
   * doesn't exist.
   *
   * @throws  Exception  If an unexpectd problem occurs.
   */
  @Test()
  public void testCompareDNsFromNonExistentFile()
         throws Exception
  {
    TestCaseUtils.initializeTestBackend(true);
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-V", "3",
      "-D", "cn=Directory Manager",
      "-w", "password",
      "-c",
      "-f", "does.not.exist",
      "o:test",
    };
    assertFalse(LDAPCompare.mainCompare(args, false, null, null) == 0);
  }
  /**
   * Tests the LDAPCompare tool with the "--help" option.
   */
  @Test()
  public void testHelp()
  {
    String[] args = { "--help" };
    assertEquals(LDAPCompare.mainCompare(args, false, null, null), 0);
    args = new String[] { "-H" };
    assertEquals(LDAPCompare.mainCompare(args, false, null, null), 0);
  }
}
opends/tests/unit-tests-testng/src/server/org/opends/server/tools/LDAPDeleteTestCase.java
New file
@@ -0,0 +1,836 @@
/*
 * 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.tools;
import java.io.File;
import java.io.FileWriter;
import java.util.ArrayList;
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.OperatingSystem;
import org.opends.server.types.ResultCode;
import org.opends.server.util.Base64;
import static org.testng.Assert.*;
import static org.opends.server.util.ServerConstants.*;
/**
 * A set of test cases for the LDAPDelete tool.
 */
public class LDAPDeleteTestCase
       extends ToolsTestCase
{
  // The path to a file containing an invalid bind password.
  private String invalidPasswordFile;
  // The path to a file containing a valid bind password.
  private String validPasswordFile;
  /**
   * Ensures that the Directory Server is running and performs other necessary
   * setup.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @BeforeClass()
  public void startServerAndCreatePasswordFiles()
         throws Exception
  {
    TestCaseUtils.startServer();
    File pwFile = File.createTempFile("valid-bind-password-", ".txt");
    pwFile.deleteOnExit();
    FileWriter fileWriter = new FileWriter(pwFile);
    fileWriter.write("password" + System.getProperty("line.separator"));
    fileWriter.close();
    validPasswordFile = pwFile.getAbsolutePath();
    pwFile = File.createTempFile("invalid-bind-password-", ".txt");
    pwFile.deleteOnExit();
    fileWriter = new FileWriter(pwFile);
    fileWriter.write("wrongPassword" + System.getProperty("line.separator"));
    fileWriter.close();
    invalidPasswordFile = pwFile.getAbsolutePath();
  }
  /**
   * Retrieves sets of invalid arguments that may not be used to initialize
   * the LDAPDelete tool.
   *
   * @return  Sets of invalid arguments that may not be used to initialize the
   *          LDAPDelete tool.
   */
  @DataProvider(name = "invalidArgs")
  public Object[][] getInvalidArgumentLists()
  {
    ArrayList<String[]> argLists = new ArrayList<String[]>();
    String[] args = {}; // No arguments
    args = new String[] // No value for "-D" argument.
    {
      "-D",
    };
    argLists.add(args);
    args = new String[] // No value for "-w" argument.
    {
      "-w",
    };
    argLists.add(args);
    args = new String[] // No value for "-j" argument.
    {
      "-j",
    };
    argLists.add(args);
    args = new String[] // No value for "-i" argument.
    {
      "-i",
    };
    argLists.add(args);
    args = new String[] // No value for "-K" argument.
    {
      "-K",
    };
    argLists.add(args);
    args = new String[] // No value for "-P" argument.
    {
      "-P",
    };
    argLists.add(args);
    args = new String[] // No value for "-W" argument.
    {
      "-W",
    };
    argLists.add(args);
    args = new String[] // No value for "-h" argument.
    {
      "-h",
    };
    argLists.add(args);
    args = new String[] // No value for "-p" argument.
    {
      "-p",
    };
    argLists.add(args);
    args = new String[] // No value for "-V" argument.
    {
      "-V",
    };
    argLists.add(args);
    args = new String[] // No value for "-f" argument.
    {
      "-f",
    };
    argLists.add(args);
    args = new String[] // No value for "-J" argument.
    {
      "-J",
    };
    argLists.add(args);
    args = new String[] // No value for "-o" argument.
    {
      "-o",
    };
    argLists.add(args);
    args = new String[] // Invalid short argument
    {
      "-I"
    };
    argLists.add(args);
    args = new String[] // Invalid long argument
    {
      "--invalidLongArgument"
    };
    argLists.add(args);
    args = new String[] // Invalid bind password file path
    {
      "-D", "cn=Directory Manager",
      "-j", "no.such.file",
      "o=test"
    };
    argLists.add(args);
    args = new String[] // Both bind password and password file
    {
      "-D", "cn=Directory Manager",
      "-w", "password",
      "-j", validPasswordFile,
      "o=test"
    };
    argLists.add(args);
    args = new String[] // Non-numeric LDAP version.
    {
      "-V", "nonnumeric",
      "o=test"
    };
    argLists.add(args);
    args = new String[] // Invalid LDAP version.
    {
      "-V", "1",
      "o=test"
    };
    argLists.add(args);
    args = new String[] // Invalid DN file path.
    {
      "-f", "no.such.file",
      "o=test"
    };
    argLists.add(args);
    args = new String[] // Invalid control criticality
    {
      "-J", "1.2.3.4:invalidcriticality",
      "o=test"
    };
    argLists.add(args);
    args = new String[] // Non-numeric port
    {
      "-p", "nonnumeric",
      "o=test"
    };
    argLists.add(args);
    args = new String[] // Port value out of range.
    {
      "-p", "999999",
      "o=test"
    };
    argLists.add(args);
    args = new String[] // SASL external without SSL or StartTLS
    {
      "-r",
      "-K", "key.store.file",
      "o=test"
    };
    argLists.add(args);
    args = new String[] // SASL external without keystore file
    {
      "-Z",
      "-r",
      "o=test"
    };
    argLists.add(args);
    Object[][] returnArray = new Object[argLists.size()][1];
    for (int i=0; i < argLists.size(); i++)
    {
      returnArray[i][0] = argLists.get(i);
    }
    return returnArray;
  }
  /**
   * Tests the LDAPDelete tool with sets of invalid arguments.
   *
   * @param  args  The set of arguments to use for the LDAPDelete tool.
   */
  @Test(dataProvider = "invalidArgs")
  public void testInvalidArguments(String[] args)
  {
    assertFalse(LDAPDelete.mainDelete(args, false, null, null) == 0);
  }
  /**
   * Tests a simple LDAPv2 delete.
   *
   * @throws  Exception  If an unexpectd problem occurs.
   */
  @Test()
  public void testSimpleLDAPv2Delete()
         throws Exception
  {
    TestCaseUtils.initializeTestBackend(true);
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-V", "2",
      "-D", "cn=Directory Manager",
      "-w", "password",
      "o=test"
    };
    assertEquals(LDAPDelete.mainDelete(args, false, null, System.err), 0);
  }
  /**
   * Tests a simple LDAPv3 delete.
   *
   * @throws  Exception  If an unexpectd problem occurs.
   */
  @Test()
  public void testSimpleLDAPv3Delete()
         throws Exception
  {
    TestCaseUtils.initializeTestBackend(true);
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-V", "3",
      "-D", "cn=Directory Manager",
      "-w", "password",
      "o=test"
    };
    assertEquals(LDAPDelete.mainDelete(args, false, null, System.err), 0);
  }
  /**
   * Tests the LDAPDelete tool using SSL with blind trust.
   *
   * @throws  Exception  If an unexpectd problem occurs.
   */
  @Test()
  public void testDeleteSSLBlindTrust()
         throws Exception
  {
    TestCaseUtils.initializeTestBackend(true);
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapsPort()),
      "-Z",
      "-X",
      "-D", "cn=Directory Manager",
      "-w", "password",
      "o=test"
    };
    assertEquals(LDAPDelete.mainDelete(args, false, null, System.err), 0);
  }
  /**
   * Tests the LDAPDelete tool using SSL with a trust store.
   *
   * @throws  Exception  If an unexpectd problem occurs.
   */
  @Test()
  public void testDeleteSSLTrustStore()
         throws Exception
  {
    TestCaseUtils.initializeTestBackend(true);
    String trustStorePath = DirectoryServer.getServerRoot() + File.separator +
                            "config" + File.separator + "client.truststore";
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapsPort()),
      "-Z",
      "-P", trustStorePath,
      "-D", "cn=Directory Manager",
      "-w", "password",
      "o=test"
    };
    assertEquals(LDAPDelete.mainDelete(args, false, null, System.err), 0);
  }
  /**
   * Tests the LDAPDelete tool using StartTLS with blind trust.
   *
   * @throws  Exception  If an unexpectd problem occurs.
   */
  @Test()
  public void testDeleteStartTLSBlindTrust()
         throws Exception
  {
    TestCaseUtils.initializeTestBackend(true);
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-q",
      "-X",
      "-D", "cn=Directory Manager",
      "-w", "password",
      "o=test"
    };
    assertEquals(LDAPDelete.mainDelete(args, false, null, System.err), 0);
  }
  /**
   * Tests the LDAPDelete tool using StartTLS with a trust store.
   *
   * @throws  Exception  If an unexpectd problem occurs.
   */
  @Test()
  public void testDeleteStartTLSTrustStore()
         throws Exception
  {
    TestCaseUtils.initializeTestBackend(true);
    String trustStorePath = DirectoryServer.getServerRoot() + File.separator +
                            "config" + File.separator + "client.truststore";
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-q",
      "-P", trustStorePath,
      "-D", "cn=Directory Manager",
      "-w", "password",
      "o=test"
    };
    assertEquals(LDAPDelete.mainDelete(args, false, null, System.err), 0);
  }
  /**
   * Tests the LDAPDelete tool using SASL PLAIN authentication.
   *
   * @throws  Exception  If an unexpectd problem occurs.
   */
  @Test()
  public void testDeletePLAIN()
         throws Exception
  {
    TestCaseUtils.initializeTestBackend(true);
    String trustStorePath = DirectoryServer.getServerRoot() + File.separator +
                            "config" + File.separator + "client.truststore";
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-o", "mech=PLAIN",
      "-o", "authid=dn:cn=Directory Manager",
      "-w", "password",
      "o=test"
    };
    assertEquals(LDAPDelete.mainDelete(args, false, null, System.err), 0);
  }
  /**
   * Tests deleting an entry that doesn't exist.
   *
   * @throws  Exception  If an unexpectd problem occurs.
   */
  @Test()
  public void testDeleteNonExistent()
         throws Exception
  {
    TestCaseUtils.initializeTestBackend(true);
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-V", "3",
      "-D", "cn=Directory Manager",
      "-w", "password",
      "cn=Does Not Exist,o=test"
    };
    assertFalse(LDAPDelete.mainDelete(args, false, null, null) == 0);
  }
  /**
   * Tests deleting with a malformed DN.
   *
   * @throws  Exception  If an unexpectd problem occurs.
   */
  @Test()
  public void testDeleteMalformedDN()
         throws Exception
  {
    TestCaseUtils.initializeTestBackend(true);
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-V", "3",
      "-D", "cn=Directory Manager",
      "-w", "password",
      "malformed"
    };
    assertFalse(LDAPDelete.mainDelete(args, false, null, null) == 0);
  }
  /**
   * Tests deleting an entry with one or more children but not including the
   * subtree delete control.
   *
   * @throws  Exception  If an unexpectd problem occurs.
   */
  @Test()
  public void testDeleteParentNoSubtreeDeleteControl()
         throws Exception
  {
    TestCaseUtils.initializeTestBackend(true);
    Entry e = TestCaseUtils.makeEntry(
         "dn: uid=test.user,o=test",
         "objectClass: top",
         "objectClass: person",
         "objectClass: organizationalPerson",
         "objectClass: inetOrgPerson",
         "uid: test.user",
         "givenName: Test",
         "sn: User",
         "cn: Test User",
         "userPassword: password");
    InternalClientConnection conn =
         InternalClientConnection.getRootConnection();
    AddOperation addOperation =
         conn.processAdd(e.getDN(), e.getObjectClasses(), e.getUserAttributes(),
                         e.getOperationalAttributes());
    assertEquals(addOperation.getResultCode(), ResultCode.SUCCESS);
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-V", "3",
      "-D", "cn=Directory Manager",
      "-w", "password",
      "o=test"
    };
    assertFalse(LDAPDelete.mainDelete(args, false, null, null) == 0);
  }
  /**
   * Tests the LDAPDelete tool reading a valid bind password from a file.
   *
   * @throws  Exception  If an unexpectd problem occurs.
   */
  @Test()
  public void testDeleteValidPasswordFile()
         throws Exception
  {
    TestCaseUtils.initializeTestBackend(true);
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-D", "cn=Directory Manager",
      "-j", validPasswordFile,
      "o=test"
    };
    assertEquals(LDAPDelete.mainDelete(args, false, null, System.err), 0);
  }
  /**
   * Tests the LDAPDelete tool reading an invalid bind password from a file.
   *
   * @throws  Exception  If an unexpectd problem occurs.
   */
  @Test()
  public void testDeleteInvalidPasswordFile()
         throws Exception
  {
    TestCaseUtils.initializeTestBackend(true);
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-D", "cn=Directory Manager",
      "-j", invalidPasswordFile,
      "o=test"
    };
    assertFalse(LDAPDelete.mainDelete(args, false, null, null) == 0);
  }
  /**
   * Tests the LDAPDelete tool reading the bind password from a nonexistent
   * file.
   *
   * @throws  Exception  If an unexpectd problem occurs.
   */
  @Test()
  public void testDeleteNonExistentPasswordFile()
         throws Exception
  {
    TestCaseUtils.initializeTestBackend(true);
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-D", "cn=Directory Manager",
      "-j", "does.not.exist",
      "o=test"
    };
    assertFalse(LDAPDelete.mainDelete(args, false, null, null) == 0);
  }
  /**
   * Tests the LDAPDelete tool reading the DNs to delete from a file.  Some of
   * the deletes will succeed and some will fail.
   *
   * @throws  Exception  If an unexpectd problem occurs.
   */
  @Test()
  public void testDeleteDNsFromFile()
         throws Exception
  {
    TestCaseUtils.initializeTestBackend(true);
    Entry e = TestCaseUtils.makeEntry(
         "dn: uid=test.user,o=test",
         "objectClass: top",
         "objectClass: person",
         "objectClass: organizationalPerson",
         "objectClass: inetOrgPerson",
         "uid: test.user",
         "givenName: Test",
         "sn: User",
         "cn: Test User",
         "userPassword: password");
    InternalClientConnection conn =
         InternalClientConnection.getRootConnection();
    AddOperation addOperation =
         conn.processAdd(e.getDN(), e.getObjectClasses(), e.getUserAttributes(),
                         e.getOperationalAttributes());
    assertEquals(addOperation.getResultCode(), ResultCode.SUCCESS);
    String path = TestCaseUtils.createTempFile(
         "o=test",
         "uid=test.user,o=test",
         "malformed",
         "o=suffix does not exist",
         "uid=entry does not exist,o=test",
         "o=test");
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-D", "cn=Directory Manager",
      "-j", validPasswordFile,
      "-c",
      "-f", path
    };
    LDAPDelete.mainDelete(args, false, null, null);
  }
  /**
   * Tests a subtree delete operation.
   *
   * @throws  Exception  If an unexpectd problem occurs.
   */
  @Test()
  public void testSubtreeDelete()
         throws Exception
  {
    TestCaseUtils.initializeTestBackend(true);
    Entry e = TestCaseUtils.makeEntry(
         "dn: uid=test.user,o=test",
         "objectClass: top",
         "objectClass: person",
         "objectClass: organizationalPerson",
         "objectClass: inetOrgPerson",
         "uid: test.user",
         "givenName: Test",
         "sn: User",
         "cn: Test User",
         "userPassword: password");
    InternalClientConnection conn =
         InternalClientConnection.getRootConnection();
    AddOperation addOperation =
         conn.processAdd(e.getDN(), e.getObjectClasses(), e.getUserAttributes(),
                         e.getOperationalAttributes());
    assertEquals(addOperation.getResultCode(), ResultCode.SUCCESS);
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-D", "cn=Directory Manager",
      "-w", "password",
      "-x",
      "o=test"
    };
    assertEquals(LDAPDelete.mainDelete(args, false, null, System.err), 0);
  }
  /**
   * Tests a simple delete using the client-side no-op option.
   *
   * @throws  Exception  If an unexpectd problem occurs.
   */
  @Test()
  public void testDeleteClientSideNoOp()
         throws Exception
  {
    TestCaseUtils.initializeTestBackend(true);
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-D", "cn=Directory Manager",
      "-w", "password",
      "-n",
      "o=test"
    };
    assertEquals(LDAPDelete.mainDelete(args, false, null, System.err), 0);
  }
  /**
   * Tests a simple delete using the server-side no-op control.
   *
   * @throws  Exception  If an unexpectd problem occurs.
   */
  @Test()
  public void testDeleteServerSideNoOp()
         throws Exception
  {
    TestCaseUtils.initializeTestBackend(true);
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-D", "cn=Directory Manager",
      "-w", "password",
      "-J", OID_LDAP_NOOP_OPENLDAP_ASSIGNED + ":true",
      "o=test"
    };
    LDAPDelete.mainDelete(args, false, null, null);
  }
  /**
   * Tests the LDAPDelete tool with the "--help" option.
   */
  @Test()
  public void testHelp()
  {
    String[] args = { "--help" };
    assertEquals(LDAPDelete.mainDelete(args, false, null, null), 0);
    args = new String[] { "-H" };
    assertEquals(LDAPDelete.mainDelete(args, false, null, null), 0);
  }
}
opends/tests/unit-tests-testng/src/server/org/opends/server/tools/LDAPModifyTestCase.java
New file
@@ -0,0 +1,1626 @@
/*
 * 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.tools;
import java.io.File;
import java.io.FileWriter;
import java.util.ArrayList;
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 static org.testng.Assert.*;
import static org.opends.server.util.ServerConstants.*;
/**
 * A set of test cases for the LDAPModify tool.
 */
public class LDAPModifyTestCase
       extends ToolsTestCase
{
  // The path to a file containing an invalid bind password.
  private String invalidPasswordFile;
  // The path to a file containing a valid bind password.
  private String validPasswordFile;
  // The path to a file containing a simple, valid modification.
  private String modifyFilePath;
  /**
   * Ensures that the Directory Server is running and performs other necessary
   * setup.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @BeforeClass()
  public void startServerAndCreatePasswordFiles()
         throws Exception
  {
    TestCaseUtils.startServer();
    File pwFile = File.createTempFile("valid-bind-password-", ".txt");
    pwFile.deleteOnExit();
    FileWriter fileWriter = new FileWriter(pwFile);
    fileWriter.write("password" + System.getProperty("line.separator"));
    fileWriter.close();
    validPasswordFile = pwFile.getAbsolutePath();
    pwFile = File.createTempFile("invalid-bind-password-", ".txt");
    pwFile.deleteOnExit();
    fileWriter = new FileWriter(pwFile);
    fileWriter.write("wrongPassword" + System.getProperty("line.separator"));
    fileWriter.close();
    invalidPasswordFile = pwFile.getAbsolutePath();
    modifyFilePath = TestCaseUtils.createTempFile("dn: o=test",
                                                  "changetype: modify",
                                                  "replace: description",
                                                  "description: foo");
  }
  /**
   * Retrieves sets of invalid arguments that may not be used to initialize
   * the LDAPModify tool.
   *
   * @return  Sets of invalid arguments that may not be used to initialize the
   *          LDAPModify tool.
   */
  @DataProvider(name = "invalidArgs")
  public Object[][] getInvalidArgumentLists()
  {
    ArrayList<String[]> argLists = new ArrayList<String[]>();
    String[] args;
    args = new String[] // Missing value for -D argument.
    {
      "-D"
    };
    argLists.add(args);
    args = new String[] // Missing value for -w argument.
    {
      "-w"
    };
    argLists.add(args);
    args = new String[] // Missing value for -j argument.
    {
      "-j"
    };
    argLists.add(args);
    args = new String[] // Missing value for -Y argument.
    {
      "-Y"
    };
    argLists.add(args);
    args = new String[] // Missing value for -i argument.
    {
      "-i"
    };
    argLists.add(args);
    args = new String[] // Missing value for -K argument.
    {
      "-K"
    };
    argLists.add(args);
    args = new String[] // Missing value for -P argument.
    {
      "-P"
    };
    argLists.add(args);
    args = new String[] // Missing value for -W argument.
    {
      "-W"
    };
    argLists.add(args);
    args = new String[] // Missing value for -h argument.
    {
      "-h"
    };
    argLists.add(args);
    args = new String[] // Missing value for -p argument.
    {
      "-p"
    };
    argLists.add(args);
    args = new String[] // Missing value for -V argument.
    {
      "-V"
    };
    argLists.add(args);
    args = new String[] // Missing value for -f argument.
    {
      "-f"
    };
    argLists.add(args);
    args = new String[] // Missing value for -J argument.
    {
      "-J"
    };
    argLists.add(args);
    args = new String[] // Missing value for -o argument.
    {
      "-o"
    };
    argLists.add(args);
    args = new String[] // Missing value for --assertionFilter argument.
    {
      "-assertionFilter"
    };
    argLists.add(args);
    args = new String[] // Missing value for --preReadAttributes argument.
    {
      "--preReadAttributes"
    };
    argLists.add(args);
    args = new String[] // Missing value for --postReadAttributes argument.
    {
      "--postReadAttributes"
    };
    argLists.add(args);
    args = new String[] // Invalid bind password file path
    {
      "-D", "cn=Directory Manager",
      "-j", "no.such.file",
    };
    argLists.add(args);
    args = new String[] // Both bind password and password file
    {
      "-D", "cn=Directory Manager",
      "-w", "password",
      "-j", validPasswordFile,
    };
    argLists.add(args);
    args = new String[] // Non-numeric LDAP version.
    {
      "-V", "nonnumeric",
    };
    argLists.add(args);
    args = new String[] // Invalid LDAP version.
    {
      "-V", "1",
    };
    argLists.add(args);
    args = new String[] // Invalid control criticality
    {
      "-J", "1.2.3.4:invalidcriticality",
    };
    argLists.add(args);
    args = new String[] // Non-numeric port
    {
      "-p", "nonnumeric",
    };
    argLists.add(args);
    args = new String[] // Port value out of range.
    {
      "-p", "999999",
    };
    argLists.add(args);
    args = new String[] // SASL external without SSL or StartTLS
    {
      "-r",
      "-K", "key.store.file",
    };
    argLists.add(args);
    args = new String[] // SASL external without keystore file
    {
      "-Z",
      "-r",
    };
    argLists.add(args);
    args = new String[] // Invalid LDAP assertion filter
    {
      "--assertionFilter", "(invalid)"
    };
    argLists.add(args);
    args = new String[] // No such LDIF file
    {
      "-f", "no.such.file"
    };
    argLists.add(args);
    Object[][] returnArray = new Object[argLists.size()][1];
    for (int i=0; i < argLists.size(); i++)
    {
      returnArray[i][0] = argLists.get(i);
    }
    return returnArray;
  }
  /**
   * Tests the LDAPModify tool with sets of invalid arguments.
   *
   * @param  args  The set of arguments to use for the LDAPModify tool.
   */
  @Test(dataProvider = "invalidArgs")
  public void testInvalidArguments(String[] args)
  {
    assertFalse(LDAPModify.mainModify(args, false, null, null) == 0);
  }
  /**
   * Tests a simple modify operation using LDAPv2.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test()
  public void testLDAPv2Modify()
         throws Exception
  {
    TestCaseUtils.initializeTestBackend(true);
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-D", "cn=Directory Manager",
      "-w", "password",
      "-V", "2",
      "-f", modifyFilePath
    };
    assertEquals(LDAPModify.mainModify(args, false, null, System.err), 0);
  }
  /**
   * Tests a simple modify operation using LDAPv3.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test()
  public void testLDAPv3Modify()
         throws Exception
  {
    TestCaseUtils.initializeTestBackend(true);
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-D", "cn=Directory Manager",
      "-w", "password",
      "-V", "3",
      "-f", modifyFilePath
    };
    assertEquals(LDAPModify.mainModify(args, false, null, System.err), 0);
  }
  /**
   * Tests a simple modify operation over SSL using blind trust.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test()
  public void testSSLBlindTrust()
         throws Exception
  {
    TestCaseUtils.initializeTestBackend(true);
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapsPort()),
      "-Z",
      "-X",
      "-D", "cn=Directory Manager",
      "-w", "password",
      "-f", modifyFilePath
    };
    assertEquals(LDAPModify.mainModify(args, false, null, System.err), 0);
  }
  /**
   * Tests a simple modify operation over SSL using a trust store.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test()
  public void testSSLTrustStore()
         throws Exception
  {
    TestCaseUtils.initializeTestBackend(true);
    String trustStorePath = DirectoryServer.getServerRoot() + File.separator +
                            "config" + File.separator + "client.truststore";
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapsPort()),
      "-Z",
      "-P", trustStorePath,
      "-D", "cn=Directory Manager",
      "-w", "password",
      "-f", modifyFilePath
    };
    assertEquals(LDAPModify.mainModify(args, false, null, System.err), 0);
  }
  /**
   * Tests a simple modify operation with StartTSL using blind trust.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test()
  public void testStartTLSBlindTrust()
         throws Exception
  {
    TestCaseUtils.initializeTestBackend(true);
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-q",
      "-X",
      "-D", "cn=Directory Manager",
      "-w", "password",
      "-f", modifyFilePath
    };
    assertEquals(LDAPModify.mainModify(args, false, null, System.err), 0);
  }
  /**
   * Tests a simple modify operation with StartTLS using a trust store.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test()
  public void testStartTLSTrustStore()
         throws Exception
  {
    TestCaseUtils.initializeTestBackend(true);
    String trustStorePath = DirectoryServer.getServerRoot() + File.separator +
                            "config" + File.separator + "client.truststore";
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-q",
      "-P", trustStorePath,
      "-D", "cn=Directory Manager",
      "-w", "password",
      "-f", modifyFilePath
    };
    assertEquals(LDAPModify.mainModify(args, false, null, System.err), 0);
  }
  /**
   * Tests a simple modify operation over SSL using a trust store and SASL
   * EXTERNAL.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test()
  public void testSSLTrustStoreSASLExternal()
         throws Exception
  {
    TestCaseUtils.initializeTestBackend(true);
    Entry e = TestCaseUtils.makeEntry(
         "dn: cn=Test User,o=test",
         "objectClass: top",
         "objectClass: person",
         "objectClass: organizationalPerson",
         "objectClass: inetOrgPerson",
         "cn: Test User",
         "givenName: Test",
         "sn: User");
    InternalClientConnection conn =
         InternalClientConnection.getRootConnection();
    AddOperation addOperation =
         conn.processAdd(e.getDN(), e.getObjectClasses(), e.getUserAttributes(),
                         e.getOperationalAttributes());
    assertEquals(addOperation.getResultCode(), ResultCode.SUCCESS);
    String keyStorePath   = DirectoryServer.getServerRoot() + File.separator +
                            "config" + File.separator + "client.keystore";
    String trustStorePath = DirectoryServer.getServerRoot() + File.separator +
                            "config" + File.separator + "client.truststore";
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapsPort()),
      "-Z",
      "-K", keyStorePath,
      "-W", "password",
      "-P", trustStorePath,
      "-r",
      "-f", modifyFilePath
    };
    assertEquals(LDAPModify.mainModify(args, false, null, System.err), 0);
  }
  /**
   * Tests a simple modify operation with StartTLS using a trust store and SASL
   * EXTERNAL.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test()
  public void testStartTLSTrustStoreSASLExternal()
         throws Exception
  {
    TestCaseUtils.initializeTestBackend(true);
    Entry e = TestCaseUtils.makeEntry(
         "dn: cn=Test User,o=test",
         "objectClass: top",
         "objectClass: person",
         "objectClass: organizationalPerson",
         "objectClass: inetOrgPerson",
         "cn: Test User",
         "givenName: Test",
         "sn: User");
    InternalClientConnection conn =
         InternalClientConnection.getRootConnection();
    AddOperation addOperation =
         conn.processAdd(e.getDN(), e.getObjectClasses(), e.getUserAttributes(),
                         e.getOperationalAttributes());
    assertEquals(addOperation.getResultCode(), ResultCode.SUCCESS);
    String keyStorePath   = DirectoryServer.getServerRoot() + File.separator +
                            "config" + File.separator + "client.keystore";
    String trustStorePath = DirectoryServer.getServerRoot() + File.separator +
                            "config" + File.separator + "client.truststore";
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-q",
      "-K", keyStorePath,
      "-W", "password",
      "-P", trustStorePath,
      "-r",
      "-f", modifyFilePath
    };
    assertEquals(LDAPModify.mainModify(args, false, null, System.err), 0);
  }
  /**
   * Tests a simple modify operation using CRAM-MD5 authentication.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test()
  public void testCRAMMD5()
         throws Exception
  {
    TestCaseUtils.initializeTestBackend(true);
    Entry e = TestCaseUtils.makeEntry(
         "dn: uid=test.user,o=test",
         "objectClass: top",
         "objectClass: person",
         "objectClass: organizationalPerson",
         "objectClass: inetOrgPerson",
         "uid: test.user",
         "givenName: Test",
         "sn: User",
         "cn: Test User",
         "userPassword: password",
         "pwdPolicySubentry: cn=Clear UserPassword Policy," +
              "cn=Password Policies,cn=config");
    InternalClientConnection conn =
         InternalClientConnection.getRootConnection();
    AddOperation addOperation =
         conn.processAdd(e.getDN(), e.getObjectClasses(), e.getUserAttributes(),
                         e.getOperationalAttributes());
    assertEquals(addOperation.getResultCode(), ResultCode.SUCCESS);
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-o", "mech=CRAM-MD5",
      "-o", "authid=u:test.user",
      "-w", "password",
      "-f", modifyFilePath
    };
    assertEquals(LDAPModify.mainModify(args, false, null, System.err), 0);
  }
  /**
   * Tests a simple modify operation using DIGEST-MD5 authentication.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test()
  public void testDigestMD5()
         throws Exception
  {
    TestCaseUtils.initializeTestBackend(true);
    Entry e = TestCaseUtils.makeEntry(
         "dn: uid=test.user,o=test",
         "objectClass: top",
         "objectClass: person",
         "objectClass: organizationalPerson",
         "objectClass: inetOrgPerson",
         "uid: test.user",
         "givenName: Test",
         "sn: User",
         "cn: Test User",
         "userPassword: password",
         "pwdPolicySubentry: cn=Clear UserPassword Policy," +
              "cn=Password Policies,cn=config");
    InternalClientConnection conn =
         InternalClientConnection.getRootConnection();
    AddOperation addOperation =
         conn.processAdd(e.getDN(), e.getObjectClasses(), e.getUserAttributes(),
                         e.getOperationalAttributes());
    assertEquals(addOperation.getResultCode(), ResultCode.SUCCESS);
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-o", "mech=DIGEST-MD5",
      "-o", "authid=u:test.user",
      "-o", "authzid=u:test.user",
      "-o", "realm=o=test",
      "-w", "password",
      "-f", modifyFilePath
    };
    assertEquals(LDAPModify.mainModify(args, false, null, System.err), 0);
  }
  /**
   * Tests a simple modify operation using PLAIN authentication.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test()
  public void testPLAIN()
         throws Exception
  {
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-o", "mech=PLAIN",
      "-o", "authid=dn:cn=Directory Manager",
      "-w", "password",
      "-f", modifyFilePath
    };
    assertEquals(LDAPModify.mainModify(args, false, null, System.err), 0);
  }
  /**
   * Tests a simple modify operation using the --noop client-side option.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test()
  public void testModifyClientSideNoOp()
         throws Exception
  {
    TestCaseUtils.initializeTestBackend(true);
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-D", "cn=Directory Manager",
      "-w", "password",
      "--noop",
      "-f", modifyFilePath
    };
    assertEquals(LDAPModify.mainModify(args, false, null, System.err), 0);
  }
  /**
   * Tests a simple add operation using the --noop client-side option.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test()
  public void testAddClientSideNoOp()
         throws Exception
  {
    TestCaseUtils.initializeTestBackend(true);
    String path = TestCaseUtils.createTempFile(
         "dn: ou=People,o=test",
         "changetype: add",
         "objectClass: top",
         "objectClass: organizationalUnit",
         "o: test");
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-D", "cn=Directory Manager",
      "-w", "password",
      "--noop",
      "-f", path
    };
    assertEquals(LDAPModify.mainModify(args, false, null, System.err), 0);
  }
  /**
   * Tests a simple delete operation using the --noop client-side option.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test()
  public void testDeleteClientSideNoOp()
         throws Exception
  {
    TestCaseUtils.initializeTestBackend(true);
    String path = TestCaseUtils.createTempFile(
         "dn: o=test",
         "changetype: delete");
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-D", "cn=Directory Manager",
      "-w", "password",
      "--noop",
      "-f", path
    };
    assertEquals(LDAPModify.mainModify(args, false, null, System.err), 0);
  }
  /**
   * Tests a simple modify DN operation using the --noop client-side option.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test()
  public void testModifyDNClientSideNoOp()
         throws Exception
  {
    TestCaseUtils.initializeTestBackend(true);
    String path = TestCaseUtils.createTempFile(
         "dn: ou=People,o=test",
         "changetype: moddn",
         "newRDN: ou=Users",
         "deleteOldRDN: 1");
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-D", "cn=Directory Manager",
      "-w", "password",
      "--noop",
      "-f", path
    };
    assertEquals(LDAPModify.mainModify(args, false, null, System.err), 0);
  }
  /**
   * Tests a simple modify operation using LDAP No-Op control.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test()
  public void testModifyLDAPNoOp()
         throws Exception
  {
    TestCaseUtils.initializeTestBackend(true);
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-D", "cn=Directory Manager",
      "-w", "password",
      "-J", OID_LDAP_NOOP_OPENLDAP_ASSIGNED + ":true",
      "-f", modifyFilePath
    };
    assertEquals(LDAPModify.mainModify(args, false, null, System.err), 0);
  }
  /**
   * Tests a simple add operation using LDAP No-Op control.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test()
  public void testAddLDAPNoOp()
         throws Exception
  {
    TestCaseUtils.initializeTestBackend(true);
    String path = TestCaseUtils.createTempFile(
         "dn: ou=People,o=test",
         "changetype: add",
         "objectClass: top",
         "objectClass: organizationalUnit",
         "ou: People");
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-D", "cn=Directory Manager",
      "-w", "password",
      "-J", OID_LDAP_NOOP_OPENLDAP_ASSIGNED + ":true",
      "-f", path
    };
    assertEquals(LDAPModify.mainModify(args, false, null, System.err), 0);
  }
  /**
   * Tests a simple delete operation using LDAP No-Op control.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test()
  public void testDeleteLDAPNoOp()
         throws Exception
  {
    TestCaseUtils.initializeTestBackend(true);
    String path = TestCaseUtils.createTempFile(
         "dn: o=test",
         "changetype: delete");
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-D", "cn=Directory Manager",
      "-w", "password",
      "-J", OID_LDAP_NOOP_OPENLDAP_ASSIGNED + ":true",
      "-f", path
    };
    assertEquals(LDAPModify.mainModify(args, false, null, System.err), 0);
  }
  /**
   * Tests a simple modify DN operation using LDAP No-Op control.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test()
  public void testModifyDNLDAPNoOp()
         throws Exception
  {
    TestCaseUtils.initializeTestBackend(true);
    Entry e = TestCaseUtils.makeEntry(
         "dn: ou=People,o=test",
         "objectClass: top",
         "objectClass: organizationalUnit",
         "ou: People");
    InternalClientConnection conn =
         InternalClientConnection.getRootConnection();
    AddOperation addOperation =
         conn.processAdd(e.getDN(), e.getObjectClasses(),
                         e.getUserAttributes(), e.getOperationalAttributes());
    assertEquals(addOperation.getResultCode(), ResultCode.SUCCESS);
    String path = TestCaseUtils.createTempFile(
         "dn: ou=People,o=test",
         "changetype: moddn",
         "newRDN: ou=Users",
         "deleteOldRDN: 1");
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-D", "cn=Directory Manager",
      "-w", "password",
      "-J", OID_LDAP_NOOP_OPENLDAP_ASSIGNED + ":true",
      "-f", path
    };
    assertEquals(LDAPModify.mainModify(args, false, null, System.err), 0);
  }
  /**
   * Tests a simple modify operation using the LDAP assertion control in which
   * the assertion is true.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test()
  public void testModifyLDAPAssertionTrue()
         throws Exception
  {
    TestCaseUtils.initializeTestBackend(true);
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-D", "cn=Directory Manager",
      "-w", "password",
      "--assertionFilter", "(o=test)",
      "-f", modifyFilePath
    };
    assertEquals(LDAPModify.mainModify(args, false, null, System.err), 0);
  }
  /**
   * Tests a simple modify operation using the LDAP assertion control in which
   * the assertion is not true.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test()
  public void testModifyLDAPAssertionFalse()
         throws Exception
  {
    TestCaseUtils.initializeTestBackend(true);
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-D", "cn=Directory Manager",
      "-w", "password",
      "--assertionFilter", "(o=foo)",
      "-f", modifyFilePath
    };
    assertFalse(LDAPModify.mainModify(args, false, null, null) == 0);
  }
  /**
   * Tests a simple delete operation using the LDAP assertion control in which
   * the assertion is true.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test()
  public void testDeleteLDAPAssertionTrue()
         throws Exception
  {
    TestCaseUtils.initializeTestBackend(true);
    String path = TestCaseUtils.createTempFile("dn: o=test",
                                               "changetype: delete");
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-D", "cn=Directory Manager",
      "-w", "password",
      "--assertionFilter", "(o=test)",
      "-f", path
    };
    assertEquals(LDAPModify.mainModify(args, false, null, System.err), 0);
  }
  /**
   * Tests a simple modify DN operation using the LDAP assertion control in
   * which the assertion is true.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test()
  public void testModifyDNLDAPAssertionTrue()
         throws Exception
  {
    TestCaseUtils.initializeTestBackend(true);
    Entry e = TestCaseUtils.makeEntry(
         "dn: ou=People,o=test",
         "objectClass: top",
         "objectClass: organizationalUnit",
         "ou: People");
    InternalClientConnection conn =
         InternalClientConnection.getRootConnection();
    AddOperation addOperation =
         conn.processAdd(e.getDN(), e.getObjectClasses(),
                         e.getUserAttributes(), e.getOperationalAttributes());
    assertEquals(addOperation.getResultCode(), ResultCode.SUCCESS);
    String path = TestCaseUtils.createTempFile("dn: ou=People,o=test",
                                               "changetype: moddn",
                                               "newRDN: ou=Users",
                                               "deleteOldRDN: 1");
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-D", "cn=Directory Manager",
      "-w", "password",
      "--assertionFilter", "(ou=People)",
      "-f", path
    };
    assertEquals(LDAPModify.mainModify(args, false, null, System.err), 0);
  }
  /**
   * Tests a simple modify operation using the LDAP pre-read control with a
   * single attribute.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test()
  public void testModifyLDAPPreReadSingleAttribute()
         throws Exception
  {
    TestCaseUtils.initializeTestBackend(true);
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-D", "cn=Directory Manager",
      "-w", "password",
      "--preReadAttributes", "o",
      "-f", modifyFilePath
    };
    assertEquals(LDAPModify.mainModify(args, false, null, System.err), 0);
  }
  /**
   * Tests a simple modify operation using the LDAP pre-read control with a
   * single attribute.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test()
  public void testModifyLDAPPreReadMultipleAttributes()
         throws Exception
  {
    TestCaseUtils.initializeTestBackend(true);
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-D", "cn=Directory Manager",
      "-w", "password",
      "--preReadAttributes", "o,objectClass",
      "-f", modifyFilePath
    };
    assertEquals(LDAPModify.mainModify(args, false, null, System.err), 0);
  }
  /**
   * Tests a simple delete operation using the LDAP pre-read control with a
   * single attribute.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test()
  public void testDeleteLDAPPreReadSingleAttribute()
         throws Exception
  {
    TestCaseUtils.initializeTestBackend(true);
    String path = TestCaseUtils.createTempFile("dn: o=test",
                                               "changetype: delete");
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-D", "cn=Directory Manager",
      "-w", "password",
      "--preReadAttributes", "o",
      "-f", path
    };
    assertEquals(LDAPModify.mainModify(args, false, null, System.err), 0);
  }
  /**
   * Tests a simple modify DN operation using the LDAP pre-read control with a
   * single attribute.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test()
  public void testModifyDNLDAPPreReadSingleAttribute()
         throws Exception
  {
    TestCaseUtils.initializeTestBackend(true);
    Entry e = TestCaseUtils.makeEntry(
         "dn: ou=People,o=test",
         "objectClass: top",
         "objectClass: organizationalUnit",
         "ou: People");
    InternalClientConnection conn =
         InternalClientConnection.getRootConnection();
    AddOperation addOperation =
         conn.processAdd(e.getDN(), e.getObjectClasses(),
                         e.getUserAttributes(), e.getOperationalAttributes());
    assertEquals(addOperation.getResultCode(), ResultCode.SUCCESS);
    String path = TestCaseUtils.createTempFile("dn: ou=People,o=test",
                                               "changetype: moddn",
                                               "newRDN: ou=Users",
                                               "deleteOldRDN: 1");
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-D", "cn=Directory Manager",
      "-w", "password",
      "--preReadAttributes", "o",
      "-f", path
    };
    assertEquals(LDAPModify.mainModify(args, false, null, System.err), 0);
  }
  /**
   * Tests a simple modify operation using the LDAP post-read control with a
   * single attribute.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test()
  public void testModifyLDAPostReadSingleAttribute()
         throws Exception
  {
    TestCaseUtils.initializeTestBackend(true);
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-D", "cn=Directory Manager",
      "-w", "password",
      "--postReadAttributes", "o",
      "-f", modifyFilePath
    };
    assertEquals(LDAPModify.mainModify(args, false, null, System.err), 0);
  }
  /**
   * Tests a simple modify operation using the LDAP post-read control with a
   * single attribute.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test()
  public void testModifyLDAPPostReadMultipleAttributes()
         throws Exception
  {
    TestCaseUtils.initializeTestBackend(true);
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-D", "cn=Directory Manager",
      "-w", "password",
      "--postReadAttributes", "o,objectClass",
      "-f", modifyFilePath
    };
    assertEquals(LDAPModify.mainModify(args, false, null, System.err), 0);
  }
  /**
   * Tests a simple add operation using the LDAP post-read control with a
   * single attribute.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test()
  public void testAddLDAPostReadSingleAttribute()
         throws Exception
  {
    TestCaseUtils.initializeTestBackend(true);
    String path = TestCaseUtils.createTempFile(
         "dn: ou=People,o=test",
         "changetype: add",
         "objectClass: top",
         "objectClass: organizationalUnit",
         "ou: People");
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-D", "cn=Directory Manager",
      "-w", "password",
      "--postReadAttributes", "o",
      "-f", path
    };
    assertEquals(LDAPModify.mainModify(args, false, null, System.err), 0);
  }
  /**
   * Tests a simple modify DN operation using the LDAP post-read control with a
   * single attribute.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test()
  public void testModifyDNLDAPostReadSingleAttribute()
         throws Exception
  {
    TestCaseUtils.initializeTestBackend(true);
    Entry e = TestCaseUtils.makeEntry(
         "dn: ou=People,o=test",
         "objectClass: top",
         "objectClass: organizationalUnit",
         "ou: People");
    InternalClientConnection conn =
         InternalClientConnection.getRootConnection();
    AddOperation addOperation =
         conn.processAdd(e.getDN(), e.getObjectClasses(),
                         e.getUserAttributes(), e.getOperationalAttributes());
    assertEquals(addOperation.getResultCode(), ResultCode.SUCCESS);
    String path = TestCaseUtils.createTempFile("dn: ou=People,o=test",
                                               "changetype: moddn",
                                               "newRDN: ou=Users",
                                               "deleteOldRDN: 1");
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-D", "cn=Directory Manager",
      "-w", "password",
      "--postReadAttributes", "o",
      "-f", path
    };
    assertEquals(LDAPModify.mainModify(args, false, null, System.err), 0);
  }
  /**
   * Tests a modify operation that will fail on the server side.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test()
  public void testServerSideModifyFailure()
         throws Exception
  {
    TestCaseUtils.initializeTestBackend(true);
    String path = TestCaseUtils.createTempFile(
         "dn: ou=People,o=test",
         "changetype: modify",
         "replace: description",
         "description: foo");
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-D", "cn=Directory Manager",
      "-w", "password",
      "--postReadAttributes", "o,objectClass",
      "-f", path
    };
    assertFalse(LDAPModify.mainModify(args, false, null, null) == 0);
  }
  /**
   * Tests performing an add operation with an explicit changetype.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test()
  public void testAddExplicitChangeType()
         throws Exception
  {
    TestCaseUtils.initializeTestBackend(true);
    String path = TestCaseUtils.createTempFile(
         "dn: ou=People,o=test",
         "changetype: add",
         "objectClass: top",
         "objectClass: organizationalUnit",
         "ou: People");
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-D", "cn=Directory Manager",
      "-w", "password",
      "-f", path
    };
    assertEquals(LDAPModify.mainModify(args, false, null, System.err), 0);
  }
  /**
   * Tests performing an add operation with an implied changetype.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test()
  public void testAddImplicitChangeType()
         throws Exception
  {
    TestCaseUtils.initializeTestBackend(true);
    String path = TestCaseUtils.createTempFile(
         "dn: ou=People,o=test",
         "objectClass: top",
         "objectClass: organizationalUnit",
         "ou: People");
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-D", "cn=Directory Manager",
      "-w", "password",
      "-a",
      "-f", path
    };
    assertEquals(LDAPModify.mainModify(args, false, null, System.err), 0);
  }
  /**
   * Tests performing a modify DN operation.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test()
  public void testModifyDN()
         throws Exception
  {
    TestCaseUtils.initializeTestBackend(true);
    String path = TestCaseUtils.createTempFile(
         "dn: ou=People,o=test",
         "changetype: add",
         "objectClass: top",
         "objectClass: organizationalUnit",
         "ou: People",
         "",
         "dn: ou=People,o=test",
         "changetype: moddn",
         "newRDN: ou=Users",
         "deleteOldRDN: 1");
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-D", "cn=Directory Manager",
      "-w", "password",
      "-f", path
    };
    assertEquals(LDAPModify.mainModify(args, false, null, System.err), 0);
  }
  /**
   * Tests performing a delete operation.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test()
  public void testDelete()
         throws Exception
  {
    TestCaseUtils.initializeTestBackend(true);
    String path = TestCaseUtils.createTempFile(
         "dn: ou=People,o=test",
         "changetype: add",
         "objectClass: top",
         "objectClass: organizationalUnit",
         "ou: People",
         "",
         "dn: ou=People,o=test",
         "changetype: delete");
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-D", "cn=Directory Manager",
      "-w", "password",
      "-f", path
    };
    assertEquals(LDAPModify.mainModify(args, false, null, System.err), 0);
  }
  /**
   * Tests with various forms of malformed LDIF changes.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test()
  public void testMalformedLDIF()
         throws Exception
  {
    TestCaseUtils.initializeTestBackend(true);
    String path = TestCaseUtils.createTempFile(
         "dn: o=test",
         "description: No Changetype",
         "",
         "dn: o=test",
         "changetype: invalid",
         "replace: description",
         "description: Invalid Changetype",
         "",
         "dn: o=test",
         "changetype: modify",
         "invalid: description",
         "description: Invalid Attribute Modification",
         "",
         "dn: ou=People,o=test",
         "",
         "dn: ou=People,o=test",
         "changetype: add",
         "",
         "dn: ou=People,o=test",
         "changetype: moddn",
         "",
         "dn: ou=People,o=test",
         "changetype: moddn",
         "newrdn: invalid",
         "deleteOldRDN: 1",
         "",
         "dn: ou=People,o=test",
         "changetype: moddn",
         "newrdn: ou=Users",
         "deleteOldRDN: invalid");
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-D", "cn=Directory Manager",
      "-w", "password",
      "-c",
      "-f", path
    };
    LDAPModify.mainModify(args, false, null,null);
  }
  /**
   * Tests a modify attempt failure without continueOnError.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test()
  public void testMalformedLDIFNoContinueOnError()
         throws Exception
  {
    TestCaseUtils.initializeTestBackend(true);
    String path = TestCaseUtils.createTempFile(
         "dn: o=test",
         "description: No Changetype",
         "",
         "dn: o=test",
         "changetype: invalid",
         "replace: description",
         "description: Invalid Changetype");
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-D", "cn=Directory Manager",
      "-w", "password",
      "-f", path
    };
    LDAPModify.mainModify(args, false, null,null);
  }
  /**
   * Tests the LDAPModify tool with the "--help" option.
   */
  @Test()
  public void testHelp()
  {
    String[] args = { "--help" };
    assertEquals(LDAPModify.mainModify(args, false, null, null), 0);
    args = new String[] { "-H" };
    assertEquals(LDAPModify.mainModify(args, false, null, null), 0);
  }
}
opends/tests/unit-tests-testng/src/server/org/opends/server/tools/LDAPPasswordModifyTestCase.java
New file
@@ -0,0 +1,1150 @@
/*
 * 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.tools;
import java.io.File;
import java.io.FileWriter;
import java.util.ArrayList;
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 static org.testng.Assert.*;
import static org.opends.server.util.ServerConstants.*;
/**
 * A set of test cases for the LDAPPasswordModify tool.
 */
public class LDAPPasswordModifyTestCase
       extends ToolsTestCase
{
  // The path to a file containing the current bind password.
  private String currentPasswordFile;
  // The path to a file containing the new password.
  private String newPasswordFile;
  /**
   * Ensures that the Directory Server is running and performs other necessary
   * setup.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @BeforeClass()
  public void startServerAndCreatePasswordFiles()
         throws Exception
  {
    TestCaseUtils.startServer();
    File pwFile = File.createTempFile("valid-bind-password-", ".txt");
    pwFile.deleteOnExit();
    FileWriter fileWriter = new FileWriter(pwFile);
    fileWriter.write("newPassword" + System.getProperty("line.separator"));
    fileWriter.close();
    newPasswordFile = pwFile.getAbsolutePath();
    pwFile = File.createTempFile("invalid-bind-password-", ".txt");
    pwFile.deleteOnExit();
    fileWriter = new FileWriter(pwFile);
    fileWriter.write("password" + System.getProperty("line.separator"));
    fileWriter.close();
    currentPasswordFile = pwFile.getAbsolutePath();
  }
  /**
   * Retrieves sets of invalid arguments that may not be used to initialize
   * the LDAPModify tool.
   *
   * @return  Sets of invalid arguments that may not be used to initialize the
   *          LDAPModify tool.
   */
  @DataProvider(name = "invalidArgs")
  public Object[][] getInvalidArgumentLists()
  {
    ArrayList<String[]> argLists = new ArrayList<String[]>();
    String[] args = {}; // No arguments.
    argLists.add(args);
    args = new String[] // Missing value for -h argument.
    {
      "-h"
    };
    argLists.add(args);
    args = new String[] // Missing value for -p argument.
    {
      "-p"
    };
    argLists.add(args);
    args = new String[] // Missing value for -D argument.
    {
      "-D"
    };
    argLists.add(args);
    args = new String[] // Missing value for -w argument.
    {
      "-w"
    };
    argLists.add(args);
    args = new String[] // Missing value for -W argument.
    {
      "-W"
    };
    argLists.add(args);
    args = new String[] // Missing value for -a argument.
    {
      "-a"
    };
    argLists.add(args);
    args = new String[] // Missing value for -n argument.
    {
      "-n"
    };
    argLists.add(args);
    args = new String[] // Missing value for -N argument.
    {
      "-N"
    };
    argLists.add(args);
    args = new String[] // Missing value for -c argument.
    {
      "-c"
    };
    argLists.add(args);
    args = new String[] // Missing value for -C argument.
    {
      "-C"
    };
    argLists.add(args);
    args = new String[] // Missing value for -k argument.
    {
      "-k"
    };
    argLists.add(args);
    args = new String[] // Missing value for -K argument.
    {
      "-K"
    };
    argLists.add(args);
    args = new String[] // Missing value for -t argument.
    {
      "-t"
    };
    argLists.add(args);
    args = new String[] // Missing value for -T argument.
    {
      "-T"
    };
    argLists.add(args);
    args = new String[] // Invalid bind password file path
    {
      "-D", "cn=Directory Manager",
      "-W", "no.such.file"
    };
    argLists.add(args);
    args = new String[] // Both bind password and password file
    {
      "-D", "cn=Directory Manager",
      "-w", "password",
      "-W", currentPasswordFile
    };
    argLists.add(args);
    args = new String[] // Both current password and current password file
    {
      "-D", "cn=Directory Manager",
      "-c", "password",
      "-C", currentPasswordFile
    };
    argLists.add(args);
    args = new String[] // Both new password and new password file
    {
      "-D", "cn=Directory Manager",
      "-n", "password",
      "-N", newPasswordFile
    };
    argLists.add(args);
    args = new String[] // Both SSL and StartTLS
    {
      "-Z",
      "-q"
    };
    argLists.add(args);
    args = new String[] // Non-numeric port
    {
      "-p", "nonnumeric"
    };
    argLists.add(args);
    args = new String[] // Port value out of range.
    {
      "-p", "999999"
    };
    argLists.add(args);
    args = new String[] // Bind DN without a password or password file
    {
      "-D", "cn=Directory Manager"
    };
    argLists.add(args);
    args = new String[] // Bind password without a DN
    {
      "-w", "password"
    };
    argLists.add(args);
    args = new String[] // Bind password file without a DN
    {
      "-W", currentPasswordFile
    };
    argLists.add(args);
    args = new String[] // No bind credentials, with authzID, no current PW
    {
      "-a", "u:test.user"
    };
    argLists.add(args);
    args = new String[] // Provide DN for authzID without DN
    {
      "-A"
    };
    argLists.add(args);
    Object[][] returnArray = new Object[argLists.size()][1];
    for (int i=0; i < argLists.size(); i++)
    {
      returnArray[i][0] = argLists.get(i);
    }
    return returnArray;
  }
  /**
   * Tests the LDAPModify tool with sets of invalid arguments.
   *
   * @param  args  The set of arguments to use for the LDAPModify tool.
   */
  @Test(dataProvider = "invalidArgs")
  public void testInvalidArguments(String[] args)
  {
    assertFalse(LDAPPasswordModify.mainPasswordModify(args, false, null,
                                                      null) == 0);
  }
  /**
   * Tests the ability to perform a self change including both the current and
   * new passwords.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  public void testSelfChangeCurrentPasswordNewPassword()
         throws Exception
  {
    TestCaseUtils.initializeTestBackend(true);
    Entry e = TestCaseUtils.makeEntry(
         "dn: uid=test.user,o=test",
         "objectClass: top",
         "objectClass: person",
         "objectClass: organizationalPerson",
         "objectClass: inetOrgPerson",
         "uid: test.user",
         "givenName: Test",
         "sn: User",
         "cn: Test User",
         "userPassword: password");
    InternalClientConnection conn =
         InternalClientConnection.getRootConnection();
    AddOperation addOperation =
         conn.processAdd(e.getDN(), e.getObjectClasses(),
                         e.getUserAttributes(), e.getOperationalAttributes());
    assertEquals(addOperation.getResultCode(), ResultCode.SUCCESS);
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-D", "uid=test.user,o=test",
      "-w", "password",
      "-c", "password",
      "-n", "newPassword"
    };
    assertEquals(LDAPPasswordModify.mainPasswordModify(args, false, null, null),
                 0);
  }
  /**
   * Tests the ability to perform a self change including a new password but no
   * current password.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  public void testSelfChangeNoCurrentPasswordNewPassword()
         throws Exception
  {
    TestCaseUtils.initializeTestBackend(true);
    Entry e = TestCaseUtils.makeEntry(
         "dn: uid=test.user,o=test",
         "objectClass: top",
         "objectClass: person",
         "objectClass: organizationalPerson",
         "objectClass: inetOrgPerson",
         "uid: test.user",
         "givenName: Test",
         "sn: User",
         "cn: Test User",
         "userPassword: password");
    InternalClientConnection conn =
         InternalClientConnection.getRootConnection();
    AddOperation addOperation =
         conn.processAdd(e.getDN(), e.getObjectClasses(),
                         e.getUserAttributes(), e.getOperationalAttributes());
    assertEquals(addOperation.getResultCode(), ResultCode.SUCCESS);
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-D", "uid=test.user,o=test",
      "-w", "password",
      "-n", "newPassword"
    };
    assertEquals(LDAPPasswordModify.mainPasswordModify(args, false, null, null),
                 0);
  }
  /**
   * Tests the ability to perform a self change including the current password
   * but no new password.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  public void testSelfChangeCurrentPasswordNoNewPassword()
         throws Exception
  {
    TestCaseUtils.initializeTestBackend(true);
    Entry e = TestCaseUtils.makeEntry(
         "dn: uid=test.user,o=test",
         "objectClass: top",
         "objectClass: person",
         "objectClass: organizationalPerson",
         "objectClass: inetOrgPerson",
         "uid: test.user",
         "givenName: Test",
         "sn: User",
         "cn: Test User",
         "userPassword: password");
    InternalClientConnection conn =
         InternalClientConnection.getRootConnection();
    AddOperation addOperation =
         conn.processAdd(e.getDN(), e.getObjectClasses(),
                         e.getUserAttributes(), e.getOperationalAttributes());
    assertEquals(addOperation.getResultCode(), ResultCode.SUCCESS);
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-D", "uid=test.user,o=test",
      "-w", "password",
      "-c", "password"
    };
    assertEquals(LDAPPasswordModify.mainPasswordModify(args, false, null, null),
                 0);
  }
  /**
   * Tests the ability to perform a self change including neither the current
   * nor new passwords.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  public void testSelfChangeNoCurrentPasswordNoNewPassword()
         throws Exception
  {
    TestCaseUtils.initializeTestBackend(true);
    Entry e = TestCaseUtils.makeEntry(
         "dn: uid=test.user,o=test",
         "objectClass: top",
         "objectClass: person",
         "objectClass: organizationalPerson",
         "objectClass: inetOrgPerson",
         "uid: test.user",
         "givenName: Test",
         "sn: User",
         "cn: Test User",
         "userPassword: password");
    InternalClientConnection conn =
         InternalClientConnection.getRootConnection();
    AddOperation addOperation =
         conn.processAdd(e.getDN(), e.getObjectClasses(),
                         e.getUserAttributes(), e.getOperationalAttributes());
    assertEquals(addOperation.getResultCode(), ResultCode.SUCCESS);
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-D", "uid=test.user,o=test",
      "-w", "password"
    };
    assertEquals(LDAPPasswordModify.mainPasswordModify(args, false, null, null),
                 0);
  }
  /**
   * Tests the ability to perform an authenticated self change including an
   * explicit authorization ID, a current password, and a new password.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  public void testAuthenticatedSelfExplicitAuthzIDCurrentNew()
         throws Exception
  {
    TestCaseUtils.initializeTestBackend(true);
    Entry e = TestCaseUtils.makeEntry(
         "dn: uid=test.user,o=test",
         "objectClass: top",
         "objectClass: person",
         "objectClass: organizationalPerson",
         "objectClass: inetOrgPerson",
         "uid: test.user",
         "givenName: Test",
         "sn: User",
         "cn: Test User",
         "userPassword: password");
    InternalClientConnection conn =
         InternalClientConnection.getRootConnection();
    AddOperation addOperation =
         conn.processAdd(e.getDN(), e.getObjectClasses(),
                         e.getUserAttributes(), e.getOperationalAttributes());
    assertEquals(addOperation.getResultCode(), ResultCode.SUCCESS);
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-D", "uid=test.user,o=test",
      "-w", "password",
      "-a", "u:test.user",
      "-c", "password",
      "-n", "newPassword"
    };
    assertEquals(LDAPPasswordModify.mainPasswordModify(args, false, null, null),
                 0);
  }
  /**
   * Tests the ability to perform an authenticated self change including an
   * implicit authorization ID, a current password, and a new password.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  public void testAuthenticatedSelfImplicitAuthzIDCurrentNew()
         throws Exception
  {
    TestCaseUtils.initializeTestBackend(true);
    Entry e = TestCaseUtils.makeEntry(
         "dn: uid=test.user,o=test",
         "objectClass: top",
         "objectClass: person",
         "objectClass: organizationalPerson",
         "objectClass: inetOrgPerson",
         "uid: test.user",
         "givenName: Test",
         "sn: User",
         "cn: Test User",
         "userPassword: password");
    InternalClientConnection conn =
         InternalClientConnection.getRootConnection();
    AddOperation addOperation =
         conn.processAdd(e.getDN(), e.getObjectClasses(),
                         e.getUserAttributes(), e.getOperationalAttributes());
    assertEquals(addOperation.getResultCode(), ResultCode.SUCCESS);
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-D", "uid=test.user,o=test",
      "-w", "password",
      "-A",
      "-c", "password",
      "-n", "newPassword"
    };
    assertEquals(LDAPPasswordModify.mainPasswordModify(args, false, null, null),
                 0);
  }
  /**
   * Tests the ability to perform an authenticated self change including an
   * implicit authorization ID, an implicit current password, and an explicit
   * new password.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  public void testAuthenticatedSelfImplicitAuthzIDNoCurrentNew()
         throws Exception
  {
    TestCaseUtils.initializeTestBackend(true);
    Entry e = TestCaseUtils.makeEntry(
         "dn: uid=test.user,o=test",
         "objectClass: top",
         "objectClass: person",
         "objectClass: organizationalPerson",
         "objectClass: inetOrgPerson",
         "uid: test.user",
         "givenName: Test",
         "sn: User",
         "cn: Test User",
         "userPassword: password");
    InternalClientConnection conn =
         InternalClientConnection.getRootConnection();
    AddOperation addOperation =
         conn.processAdd(e.getDN(), e.getObjectClasses(),
                         e.getUserAttributes(), e.getOperationalAttributes());
    assertEquals(addOperation.getResultCode(), ResultCode.SUCCESS);
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-D", "uid=test.user,o=test",
      "-w", "password",
      "-A",
      "-n", "newPassword"
    };
    assertEquals(LDAPPasswordModify.mainPasswordModify(args, false, null, null),
                 0);
  }
  /**
   * Tests the ability to perform an unauthenticated self change with a new
   * password.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  public void testUnauthenticatedSelfChangeNewPassword()
         throws Exception
  {
    TestCaseUtils.initializeTestBackend(true);
    Entry e = TestCaseUtils.makeEntry(
         "dn: uid=test.user,o=test",
         "objectClass: top",
         "objectClass: person",
         "objectClass: organizationalPerson",
         "objectClass: inetOrgPerson",
         "uid: test.user",
         "givenName: Test",
         "sn: User",
         "cn: Test User",
         "userPassword: password");
    InternalClientConnection conn =
         InternalClientConnection.getRootConnection();
    AddOperation addOperation =
         conn.processAdd(e.getDN(), e.getObjectClasses(),
                         e.getUserAttributes(), e.getOperationalAttributes());
    assertEquals(addOperation.getResultCode(), ResultCode.SUCCESS);
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-a", "dn:uid=test.user,o=test",
      "-c", "password",
      "-n", "newPassword"
    };
    assertEquals(LDAPPasswordModify.mainPasswordModify(args, false, null, null),
                 0);
  }
  /**
   * Tests the ability to perform an unauthenticated self change with no new
   * password.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  public void testUnauthenticatedSelfChangeNoNewPassword()
         throws Exception
  {
    TestCaseUtils.initializeTestBackend(true);
    Entry e = TestCaseUtils.makeEntry(
         "dn: uid=test.user,o=test",
         "objectClass: top",
         "objectClass: person",
         "objectClass: organizationalPerson",
         "objectClass: inetOrgPerson",
         "uid: test.user",
         "givenName: Test",
         "sn: User",
         "cn: Test User",
         "userPassword: password");
    InternalClientConnection conn =
         InternalClientConnection.getRootConnection();
    AddOperation addOperation =
         conn.processAdd(e.getDN(), e.getObjectClasses(),
                         e.getUserAttributes(), e.getOperationalAttributes());
    assertEquals(addOperation.getResultCode(), ResultCode.SUCCESS);
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-a", "dn:uid=test.user,o=test",
      "-c", "password"
    };
    assertEquals(LDAPPasswordModify.mainPasswordModify(args, false, null, null),
                 0);
  }
  /**
   * Tests the ability to perform an administrative reset with a new password.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  public void testAdminResetNewPassword()
         throws Exception
  {
    TestCaseUtils.initializeTestBackend(true);
    Entry e = TestCaseUtils.makeEntry(
         "dn: uid=test.user,o=test",
         "objectClass: top",
         "objectClass: person",
         "objectClass: organizationalPerson",
         "objectClass: inetOrgPerson",
         "uid: test.user",
         "givenName: Test",
         "sn: User",
         "cn: Test User",
         "userPassword: password");
    InternalClientConnection conn =
         InternalClientConnection.getRootConnection();
    AddOperation addOperation =
         conn.processAdd(e.getDN(), e.getObjectClasses(),
                         e.getUserAttributes(), e.getOperationalAttributes());
    assertEquals(addOperation.getResultCode(), ResultCode.SUCCESS);
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-D", "cn=Directory Manager",
      "-w", "password",
      "-a", "dn:uid=test.user,o=test",
      "-n", "newPassword"
    };
    assertEquals(LDAPPasswordModify.mainPasswordModify(args, false, null, null),
                 0);
  }
  /**
   * Tests the ability to perform an administrative reset with no new password.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  public void testAdminResetNoNewPassword()
         throws Exception
  {
    TestCaseUtils.initializeTestBackend(true);
    Entry e = TestCaseUtils.makeEntry(
         "dn: uid=test.user,o=test",
         "objectClass: top",
         "objectClass: person",
         "objectClass: organizationalPerson",
         "objectClass: inetOrgPerson",
         "uid: test.user",
         "givenName: Test",
         "sn: User",
         "cn: Test User",
         "userPassword: password");
    InternalClientConnection conn =
         InternalClientConnection.getRootConnection();
    AddOperation addOperation =
         conn.processAdd(e.getDN(), e.getObjectClasses(),
                         e.getUserAttributes(), e.getOperationalAttributes());
    assertEquals(addOperation.getResultCode(), ResultCode.SUCCESS);
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-D", "cn=Directory Manager",
      "-w", "password",
      "-a", "dn:uid=test.user,o=test"
    };
    assertEquals(LDAPPasswordModify.mainPasswordModify(args, false, null, null),
                 0);
  }
  /**
   * Tests the ability to perform a password change over SSL with blind trust.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  public void testSSLBlindTrust()
         throws Exception
  {
    TestCaseUtils.initializeTestBackend(true);
    Entry e = TestCaseUtils.makeEntry(
         "dn: uid=test.user,o=test",
         "objectClass: top",
         "objectClass: person",
         "objectClass: organizationalPerson",
         "objectClass: inetOrgPerson",
         "uid: test.user",
         "givenName: Test",
         "sn: User",
         "cn: Test User",
         "userPassword: password");
    InternalClientConnection conn =
         InternalClientConnection.getRootConnection();
    AddOperation addOperation =
         conn.processAdd(e.getDN(), e.getObjectClasses(),
                         e.getUserAttributes(), e.getOperationalAttributes());
    assertEquals(addOperation.getResultCode(), ResultCode.SUCCESS);
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapsPort()),
      "-Z",
      "-X",
      "-a", "dn:uid=test.user,o=test",
      "-c", "password",
      "-n", "newPassword"
    };
    assertEquals(LDAPPasswordModify.mainPasswordModify(args, false, null, null),
                 0);
  }
  /**
   * Tests the ability to perform a password change over SSL with a trust store.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  public void testSSLTrustStore()
         throws Exception
  {
    TestCaseUtils.initializeTestBackend(true);
    String trustStorePath = DirectoryServer.getServerRoot() + File.separator +
                            "config" + File.separator + "client.truststore";
    Entry e = TestCaseUtils.makeEntry(
         "dn: uid=test.user,o=test",
         "objectClass: top",
         "objectClass: person",
         "objectClass: organizationalPerson",
         "objectClass: inetOrgPerson",
         "uid: test.user",
         "givenName: Test",
         "sn: User",
         "cn: Test User",
         "userPassword: password");
    InternalClientConnection conn =
         InternalClientConnection.getRootConnection();
    AddOperation addOperation =
         conn.processAdd(e.getDN(), e.getObjectClasses(),
                         e.getUserAttributes(), e.getOperationalAttributes());
    assertEquals(addOperation.getResultCode(), ResultCode.SUCCESS);
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapsPort()),
      "-Z",
      "-t", trustStorePath,
      "-a", "dn:uid=test.user,o=test",
      "-c", "password",
      "-n", "newPassword"
    };
    assertEquals(LDAPPasswordModify.mainPasswordModify(args, false, null, null),
                 0);
  }
  /**
   * Tests the ability to perform a password change using StartTLS with blind
   * trust.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  public void testStartTLSBlindTrust()
         throws Exception
  {
    TestCaseUtils.initializeTestBackend(true);
    Entry e = TestCaseUtils.makeEntry(
         "dn: uid=test.user,o=test",
         "objectClass: top",
         "objectClass: person",
         "objectClass: organizationalPerson",
         "objectClass: inetOrgPerson",
         "uid: test.user",
         "givenName: Test",
         "sn: User",
         "cn: Test User",
         "userPassword: password");
    InternalClientConnection conn =
         InternalClientConnection.getRootConnection();
    AddOperation addOperation =
         conn.processAdd(e.getDN(), e.getObjectClasses(),
                         e.getUserAttributes(), e.getOperationalAttributes());
    assertEquals(addOperation.getResultCode(), ResultCode.SUCCESS);
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-q",
      "-X",
      "-a", "dn:uid=test.user,o=test",
      "-c", "password",
      "-n", "newPassword"
    };
    assertEquals(LDAPPasswordModify.mainPasswordModify(args, false, null, null),
                 0);
  }
  /**
   * Tests the ability to perform a password change using StartTLS with a trust
   * store.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  public void testStartTLSTrustStore()
         throws Exception
  {
    TestCaseUtils.initializeTestBackend(true);
    String trustStorePath = DirectoryServer.getServerRoot() + File.separator +
                            "config" + File.separator + "client.truststore";
    Entry e = TestCaseUtils.makeEntry(
         "dn: uid=test.user,o=test",
         "objectClass: top",
         "objectClass: person",
         "objectClass: organizationalPerson",
         "objectClass: inetOrgPerson",
         "uid: test.user",
         "givenName: Test",
         "sn: User",
         "cn: Test User",
         "userPassword: password");
    InternalClientConnection conn =
         InternalClientConnection.getRootConnection();
    AddOperation addOperation =
         conn.processAdd(e.getDN(), e.getObjectClasses(),
                         e.getUserAttributes(), e.getOperationalAttributes());
    assertEquals(addOperation.getResultCode(), ResultCode.SUCCESS);
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-q",
      "-t", trustStorePath,
      "-a", "dn:uid=test.user,o=test",
      "-c", "password",
      "-n", "newPassword"
    };
    assertEquals(LDAPPasswordModify.mainPasswordModify(args, false, null, null),
                 0);
  }
  /**
   * Tests the ability to perform a password reset when reading the bind and new
   * passwords from a file.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  public void testBindAndNewPasswordsFromFile()
         throws Exception
  {
    TestCaseUtils.initializeTestBackend(true);
    Entry e = TestCaseUtils.makeEntry(
         "dn: uid=test.user,o=test",
         "objectClass: top",
         "objectClass: person",
         "objectClass: organizationalPerson",
         "objectClass: inetOrgPerson",
         "uid: test.user",
         "givenName: Test",
         "sn: User",
         "cn: Test User",
         "userPassword: password");
    InternalClientConnection conn =
         InternalClientConnection.getRootConnection();
    AddOperation addOperation =
         conn.processAdd(e.getDN(), e.getObjectClasses(),
                         e.getUserAttributes(), e.getOperationalAttributes());
    assertEquals(addOperation.getResultCode(), ResultCode.SUCCESS);
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-D", "cn=Directory Manager",
      "-W", currentPasswordFile,
      "-a", "dn:uid=test.user,o=test",
      "-N", newPasswordFile
    };
    assertEquals(LDAPPasswordModify.mainPasswordModify(args, false, null, null),
                 0);
  }
  /**
   * Tests the ability to perform a password change when reading the current and
   * new passwords from a file.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  public void testCurrentAndNewPasswordsFromFile()
         throws Exception
  {
    TestCaseUtils.initializeTestBackend(true);
    Entry e = TestCaseUtils.makeEntry(
         "dn: uid=test.user,o=test",
         "objectClass: top",
         "objectClass: person",
         "objectClass: organizationalPerson",
         "objectClass: inetOrgPerson",
         "uid: test.user",
         "givenName: Test",
         "sn: User",
         "cn: Test User",
         "userPassword: password");
    InternalClientConnection conn =
         InternalClientConnection.getRootConnection();
    AddOperation addOperation =
         conn.processAdd(e.getDN(), e.getObjectClasses(),
                         e.getUserAttributes(), e.getOperationalAttributes());
    assertEquals(addOperation.getResultCode(), ResultCode.SUCCESS);
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-a", "u:test.user",
      "-C", currentPasswordFile,
      "-N", newPasswordFile
    };
    assertEquals(LDAPPasswordModify.mainPasswordModify(args, false, null, null),
                 0);
  }
  /**
   * Tests a failure when attempting an administrative reset with an invalid DN
   * in the authorization ID.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  public void testResetWithInvalidAuthzDN()
         throws Exception
  {
    TestCaseUtils.initializeTestBackend(true);
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-D", "cn=Directory Manager",
      "-w", "password",
      "-a", "dn:invalid",
      "-n", "newPassword"
    };
    assertFalse(LDAPPasswordModify.mainPasswordModify(args, false, null,
                                                      null) == 0);
  }
  /**
   * Tests a failure when attempting an administrative reset on a user entry
   * that doesn't exist.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  public void testResetOnNonExistentUser()
         throws Exception
  {
    TestCaseUtils.initializeTestBackend(true);
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-D", "cn=Directory Manager",
      "-w", "password",
      "-a", "dn:cn=Does Not Exist,o=test",
      "-n", "newPassword"
    };
    assertFalse(LDAPPasswordModify.mainPasswordModify(args, false, null,
                                                      null) == 0);
  }
  /**
   * Tests the LDAPModify tool with the "--help" option.
   */
  @Test()
  public void testHelp()
  {
    String[] args = { "--help" };
    assertEquals(LDAPPasswordModify.mainPasswordModify(args, false, null, null),
                 0);
    args = new String[] { "-H" };
    assertEquals(LDAPPasswordModify.mainPasswordModify(args, false, null, null),
                 0);
  }
}
opends/tests/unit-tests-testng/src/server/org/opends/server/tools/LDAPSearchTestCase.java
New file
@@ -0,0 +1,1334 @@
/*
 * 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.tools;
import java.io.File;
import java.io.FileWriter;
import java.util.ArrayList;
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 static org.testng.Assert.*;
import static org.opends.server.util.ServerConstants.*;
/**
 * A set of test cases for the LDAPSearch tool.
 */
public class LDAPSearchTestCase
       extends ToolsTestCase
{
  // The path to a file containing an invalid bind password.
  private String invalidPasswordFile;
  // The path to a file containing a valid bind password.
  private String validPasswordFile;
  /**
   * Ensures that the Directory Server is running and performs other necessary
   * setup.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @BeforeClass()
  public void startServerAndCreatePasswordFiles()
         throws Exception
  {
    TestCaseUtils.startServer();
    File pwFile = File.createTempFile("valid-bind-password-", ".txt");
    pwFile.deleteOnExit();
    FileWriter fileWriter = new FileWriter(pwFile);
    fileWriter.write("password" + System.getProperty("line.separator"));
    fileWriter.close();
    validPasswordFile = pwFile.getAbsolutePath();
    pwFile = File.createTempFile("invalid-bind-password-", ".txt");
    pwFile.deleteOnExit();
    fileWriter = new FileWriter(pwFile);
    fileWriter.write("wrongPassword" + System.getProperty("line.separator"));
    fileWriter.close();
    invalidPasswordFile = pwFile.getAbsolutePath();
  }
  /**
   * Retrieves sets of invalid arguments that may not be used to initialize
   * the LDAPSearch tool.
   *
   * @return  Sets of invalid arguments that may not be used to initialize the
   *          LDAPSearch tool.
   */
  @DataProvider(name = "invalidArgs")
  public Object[][] getInvalidArgumentLists()
  {
    ArrayList<String[]> argLists = new ArrayList<String[]>();
    String[] args = {}; // No arguments
    argLists.add(args);
    args = new String[] // No value for '-b' argument
    {
      "-b"
    };
    argLists.add(args);
    args = new String[] // No value for '-D' argument
    {
      "-D"
    };
    argLists.add(args);
    args = new String[] // No value for '-w' argument
    {
      "-w"
    };
    argLists.add(args);
    args = new String[] // No value for '-j' argument
    {
      "-j"
    };
    argLists.add(args);
    args = new String[] // No value for '-Y' argument
    {
      "-Y"
    };
    argLists.add(args);
    args = new String[] // No value for '-i' argument
    {
      "-i"
    };
    argLists.add(args);
    args = new String[] // No value for '-K' argument
    {
      "-K"
    };
    argLists.add(args);
    args = new String[] // No value for '-P' argument
    {
      "-P"
    };
    argLists.add(args);
    args = new String[] // No value for '-W' argument
    {
      "-W"
    };
    argLists.add(args);
    args = new String[] // No value for '-h' argument
    {
      "-h"
    };
    argLists.add(args);
    args = new String[] // No value for '-p' argument
    {
      "-p"
    };
    argLists.add(args);
    args = new String[] // No value for '-V' argument
    {
      "-V"
    };
    argLists.add(args);
    args = new String[] // No value for '-f' argument
    {
      "-f"
    };
    argLists.add(args);
    args = new String[] // No value for '-J' argument
    {
      "-J"
    };
    argLists.add(args);
    args = new String[] // No value for '-z' argument
    {
      "-z"
    };
    argLists.add(args);
    args = new String[] // No value for '-l' argument
    {
      "-l"
    };
    argLists.add(args);
    args = new String[] // No value for '-s' argument
    {
      "-s"
    };
    argLists.add(args);
    args = new String[] // No value for '-a' argument
    {
      "-a"
    };
    argLists.add(args);
    args = new String[] // No value for '-o' argument
    {
      "-o"
    };
    argLists.add(args);
    args = new String[] // No value for '-c' argument
    {
      "-c"
    };
    argLists.add(args);
    args = new String[] // No value for '--assertionFilter' argument
    {
      "--assertionFilter"
    };
    argLists.add(args);
    args = new String[] // No value for '--matchedValuesFilter' argument
    {
      "--matchedValuesFilter"
    };
    argLists.add(args);
    args = new String[] // Invalid short argument
    {
      "-I"
    };
    argLists.add(args);
    args = new String[] // Invalid long argument
    {
      "--invalidLongArgument"
    };
    argLists.add(args);
    args = new String[] // No base DN
    {
      "(objectClass=*)"
    };
    argLists.add(args);
    args = new String[] // Base DN with no value
    {
      "-b"
    };
    argLists.add(args);
    args = new String[] // No filter
    {
      "-b", ""
    };
    argLists.add(args);
    args = new String[] // Invalid search filter
    {
      "-b", "",
      "(invalidfilter)"
    };
    argLists.add(args);
    args = new String[] // Invalid assertion filter
    {
      "-b", "",
      "--assertionFilter", "(invalidfilter)",
      "(objectClass=*)"
    };
    argLists.add(args);
    args = new String[] // Invalid matched values filter
    {
      "-b", "",
      "--matchedValuesFilter", "(invalidfilter)",
      "(objectClass=*)"
    };
    argLists.add(args);
    args = new String[] // Invalid bind password file path
    {
      "-D", "cn=Directory Manager",
      "-j", "no.such.file",
      "-b", "",
      "(objectClass=*)"
    };
    argLists.add(args);
    args = new String[] // Both bind password and password file
    {
      "-D", "cn=Directory Manager",
      "-w", "password",
      "-j", validPasswordFile,
      "-b", "",
      "(objectClass=*)"
    };
    argLists.add(args);
    args = new String[] // Non-numeric LDAP version.
    {
      "-b", "",
      "-V", "nonnumeric",
      "(objectClass=*)"
    };
    argLists.add(args);
    args = new String[] // Invalid LDAP version.
    {
      "-b", "",
      "-V", "1",
      "(objectClass=*)"
    };
    argLists.add(args);
    args = new String[] // Invalid filter file path.
    {
      "-b", "",
      "-f", "no.such.file"
    };
    argLists.add(args);
    args = new String[] // Invalid control criticality
    {
      "-b", "",
      "-J", "1.2.3.4:invalidcriticality",
      "(objectClass=*)"
    };
    argLists.add(args);
    args = new String[] // Invalid scope
    {
      "-b", "",
      "-s", "invalid",
      "(objectClass=*)"
    };
    argLists.add(args);
    args = new String[] // Invalid dereference policy
    {
      "-b", "",
      "-a", "invalid",
      "(objectClass=*)"
    };
    argLists.add(args);
    args = new String[] // Invalid psearch descriptor
    {
      "-b", "",
      "-C", "invalid",
      "(objectClass=*)"
    };
    argLists.add(args);
    args = new String[] // Invalid psearch changetype
    {
      "-b", "",
      "-C", "ps:invalid",
      "(objectClass=*)"
    };
    argLists.add(args);
    args = new String[] // Invalid psearch changetype in list
    {
      "-b", "",
      "-C", "ps:add,delete,modify,modifydn,invalid",
      "(objectClass=*)"
    };
    argLists.add(args);
    args = new String[] // Invalid psearch changesOnly
    {
      "-b", "",
      "-C", "ps:all:invalid",
      "(objectClass=*)"
    };
    argLists.add(args);
    args = new String[] // Invalid psearch entryChangeControls
    {
      "-b", "",
      "-C", "ps:all:1:invalid",
      "(objectClass=*)"
    };
    argLists.add(args);
    args = new String[] // Non-numeric port
    {
      "-p", "nonnumeric",
      "-b", "",
      "(objectClass=*)"
    };
    argLists.add(args);
    args = new String[] // Port value out of range.
    {
      "-p", "999999",
      "-b", "",
      "(objectClass=*)"
    };
    argLists.add(args);
    args = new String[] // Non-numeric size limit
    {
      "-z", "nonnumeric",
      "-b", "",
      "(objectClass=*)"
    };
    argLists.add(args);
    args = new String[] // Size limit out of range.
    {
      "-z", "-1",
      "-b", "",
      "(objectClass=*)"
    };
    argLists.add(args);
    args = new String[] // Non-numeric time limit
    {
      "-l", "nonnumeric",
      "-b", "",
      "(objectClass=*)"
    };
    argLists.add(args);
    args = new String[] // Time limit out of range.
    {
      "-l", "-1",
      "-b", "",
      "(objectClass=*)"
    };
    argLists.add(args);
    args = new String[] // SASL external without SSL or StartTLS
    {
      "-r",
      "-b", "",
      "-K", "key.store.file",
      "(objectClass=*)"
    };
    argLists.add(args);
    args = new String[] // SASL external without keystore file
    {
      "-Z",
      "-r",
      "-b", "",
      "(objectClass=*)"
    };
    argLists.add(args);
    Object[][] returnArray = new Object[argLists.size()][1];
    for (int i=0; i < argLists.size(); i++)
    {
      returnArray[i][0] = argLists.get(i);
    }
    return returnArray;
  }
  /**
   * Tests the LDAPSearch tool with sets of invalid arguments.
   *
   * @param  args  The set of arguments to use for the LDAPSearch tool.
   */
  @Test(dataProvider = "invalidArgs")
  public void testInvalidArguments(String[] args)
  {
    assertFalse(LDAPSearch.mainSearch(args, false, null, null) == 0);
  }
  /**
   * Tests a simple LDAPv2 search.
   */
  @Test()
  public void testSimpleLDAPv2Search()
  {
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-V", "2",
      "-D", "cn=Directory Manager",
      "-w", "password",
      "-b", "",
      "-s", "base",
      "(objectClass=*)"
    };
    assertEquals(LDAPSearch.mainSearch(args, false, null, System.err), 0);
  }
  /**
   * Tests a simple LDAPv3 search.
   */
  @Test()
  public void testSimpleLDAPv3Search()
  {
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-V", "3",
      "-D", "cn=Directory Manager",
      "-w", "password",
      "-b", "",
      "-s", "base",
      "(objectClass=*)"
    };
    assertEquals(LDAPSearch.mainSearch(args, false, null, System.err), 0);
  }
  /**
   * Tests a simple LDAP search over SSL using blind trust.
   */
  @Test()
  public void testSimpleSearchSSLBlindTrust()
  {
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapsPort()),
      "-Z",
      "-X",
      "-b", "",
      "-s", "base",
      "(objectClass=*)"
    };
    assertEquals(LDAPSearch.mainSearch(args, false, null, System.err), 0);
  }
  /**
   * Tests a simple LDAP search over SSL using a trust store.
   */
  @Test()
  public void testSimpleSearchSSLTrustStore()
  {
    String trustStorePath = DirectoryServer.getServerRoot() + File.separator +
                            "config" + File.separator + "client.truststore";
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapsPort()),
      "-Z",
      "-P", trustStorePath,
      "-b", "",
      "-s", "base",
      "(objectClass=*)"
    };
    assertEquals(LDAPSearch.mainSearch(args, false, null, System.err), 0);
  }
  /**
   * Tests a simple LDAP search using StartTLS with blind trust.
   */
  @Test()
  public void testSimpleSearchStartTLSBlindTrust()
  {
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-q",
      "-X",
      "-b", "",
      "-s", "base",
      "(objectClass=*)"
    };
    assertEquals(LDAPSearch.mainSearch(args, false, null, System.err), 0);
  }
  /**
   * Tests a simple LDAP search using StartTLS with a trust store.
   */
  @Test()
  public void testSimpleSearchStartTLSTrustStore()
  {
    String trustStorePath = DirectoryServer.getServerRoot() + File.separator +
                            "config" + File.separator + "client.truststore";
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-q",
      "-P", trustStorePath,
      "-b", "",
      "-s", "base",
      "(objectClass=*)"
    };
    assertEquals(LDAPSearch.mainSearch(args, false, null, System.err), 0);
  }
  /**
   * Tests a simple LDAP search over SSL using a trust store and SASL EXTERNAL
   * authentication.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test()
  public void testSimpleSearchSSLTrustStoreSASLExternal()
         throws Exception
  {
    TestCaseUtils.initializeTestBackend(true);
    Entry e = TestCaseUtils.makeEntry(
         "dn: cn=Test User,o=test",
         "objectClass: top",
         "objectClass: person",
         "objectClass: organizationalPerson",
         "objectClass: inetOrgPerson",
         "cn: Test User",
         "givenName: Test",
         "sn: User");
    InternalClientConnection conn =
         InternalClientConnection.getRootConnection();
    AddOperation addOperation =
         conn.processAdd(e.getDN(), e.getObjectClasses(), e.getUserAttributes(),
                         e.getOperationalAttributes());
    assertEquals(addOperation.getResultCode(), ResultCode.SUCCESS);
    String keyStorePath   = DirectoryServer.getServerRoot() + File.separator +
                            "config" + File.separator + "client.keystore";
    String trustStorePath = DirectoryServer.getServerRoot() + File.separator +
                            "config" + File.separator + "client.truststore";
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapsPort()),
      "-Z",
      "-K", keyStorePath,
      "-W", "password",
      "-P", trustStorePath,
      "-r",
      "-b", "",
      "-s", "base",
      "(objectClass=*)"
    };
    assertEquals(LDAPSearch.mainSearch(args, false, null, System.err), 0);
  }
  /**
   * Tests a simple LDAP search using StartTLS with a trust store and SASL
   * EXTERNAL authentication.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test()
  public void testSimpleSearchStartTLSTrustStoreSASLExternal()
         throws Exception
  {
    TestCaseUtils.initializeTestBackend(true);
    Entry e = TestCaseUtils.makeEntry(
         "dn: cn=Test User,o=test",
         "objectClass: top",
         "objectClass: person",
         "objectClass: organizationalPerson",
         "objectClass: inetOrgPerson",
         "cn: Test User",
         "givenName: Test",
         "sn: User");
    InternalClientConnection conn =
         InternalClientConnection.getRootConnection();
    AddOperation addOperation =
         conn.processAdd(e.getDN(), e.getObjectClasses(), e.getUserAttributes(),
                         e.getOperationalAttributes());
    assertEquals(addOperation.getResultCode(), ResultCode.SUCCESS);
    String keyStorePath   = DirectoryServer.getServerRoot() + File.separator +
                            "config" + File.separator + "client.keystore";
    String trustStorePath = DirectoryServer.getServerRoot() + File.separator +
                            "config" + File.separator + "client.truststore";
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-q",
      "-K", keyStorePath,
      "-W", "password",
      "-P", trustStorePath,
      "-r",
      "-b", "",
      "-s", "base",
      "(objectClass=*)"
    };
    assertEquals(LDAPSearch.mainSearch(args, false, null, System.err), 0);
  }
  /**
   * Tests a simple search operation using CRAM-MD5 authentication.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test()
  public void testCRAMMD5()
         throws Exception
  {
    TestCaseUtils.initializeTestBackend(true);
    Entry e = TestCaseUtils.makeEntry(
         "dn: uid=test.user,o=test",
         "objectClass: top",
         "objectClass: person",
         "objectClass: organizationalPerson",
         "objectClass: inetOrgPerson",
         "uid: test.user",
         "givenName: Test",
         "sn: User",
         "cn: Test User",
         "userPassword: password",
         "pwdPolicySubentry: cn=Clear UserPassword Policy," +
              "cn=Password Policies,cn=config");
    InternalClientConnection conn =
         InternalClientConnection.getRootConnection();
    AddOperation addOperation =
         conn.processAdd(e.getDN(), e.getObjectClasses(), e.getUserAttributes(),
                         e.getOperationalAttributes());
    assertEquals(addOperation.getResultCode(), ResultCode.SUCCESS);
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-o", "mech=CRAM-MD5",
      "-o", "authid=u:test.user",
      "-w", "password",
      "-b", "",
      "-s", "base",
      "(objectClass=*)"
    };
    assertEquals(LDAPSearch.mainSearch(args, false, null, System.err), 0);
  }
  /**
   * Tests a simple search operation using CRAM-MD5 authentication.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test()
  public void testDigestMD5()
         throws Exception
  {
    TestCaseUtils.initializeTestBackend(true);
    Entry e = TestCaseUtils.makeEntry(
         "dn: uid=test.user,o=test",
         "objectClass: top",
         "objectClass: person",
         "objectClass: organizationalPerson",
         "objectClass: inetOrgPerson",
         "uid: test.user",
         "givenName: Test",
         "sn: User",
         "cn: Test User",
         "userPassword: password",
         "pwdPolicySubentry: cn=Clear UserPassword Policy," +
              "cn=Password Policies,cn=config");
    InternalClientConnection conn =
         InternalClientConnection.getRootConnection();
    AddOperation addOperation =
         conn.processAdd(e.getDN(), e.getObjectClasses(), e.getUserAttributes(),
                         e.getOperationalAttributes());
    assertEquals(addOperation.getResultCode(), ResultCode.SUCCESS);
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-o", "mech=DIGEST-MD5",
      "-o", "authid=u:test.user",
      "-o", "authzid=u:test.user",
      "-o", "realm=o=test",
      "-w", "password",
      "-b", "",
      "-s", "base",
      "(objectClass=*)"
    };
    assertEquals(LDAPSearch.mainSearch(args, false, null, System.err), 0);
  }
  /**
   * Tests a simple search operation using PLAIN authentication.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test()
  public void testPLAIN()
         throws Exception
  {
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-o", "mech=PLAIN",
      "-o", "authid=dn:cn=Directory Manager",
      "-w", "password",
      "-b", "",
      "-s", "base",
      "(objectClass=*)"
    };
    assertEquals(LDAPSearch.mainSearch(args, false, null, System.err), 0);
  }
  /**
   * Tests a search with a malformed bind DN.
   */
  @Test()
  public void testMalformedBindDN()
  {
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-D", "malformed",
      "-w", "password",
      "-b", "",
      "-s", "base",
      "(objectClass=*)"
    };
    assertFalse(LDAPSearch.mainSearch(args, false, null, null) == 0);
  }
  /**
   * Tests a search with a nonexistent bind DN.
   */
  @Test()
  public void testNonExistentBindDN()
  {
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-D", "cn=Does Not Exist",
      "-w", "password",
      "-b", "",
      "-s", "base",
      "(objectClass=*)"
    };
    assertFalse(LDAPSearch.mainSearch(args, false, null, null) == 0);
  }
  /**
   * Tests a search with an invalid password.
   */
  @Test()
  public void testInvalidBindPassword()
  {
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-D", "cn=Directory Manager",
      "-w", "wrongPassword",
      "-b", "",
      "-s", "base",
      "(objectClass=*)"
    };
    assertFalse(LDAPSearch.mainSearch(args, false, null, null) == 0);
  }
  /**
   * Tests a search with a valid password read from a file.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test()
  public void testValidPasswordFromFile()
         throws Exception
  {
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-D", "cn=Directory Manager",
      "-j", validPasswordFile,
      "-b", "",
      "-s", "base",
      "(objectClass=*)"
    };
    assertEquals(LDAPSearch.mainSearch(args, false, null, System.err), 0);
  }
  /**
   * Tests a search with an invalid password read from a file.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test()
  public void testInvalidPasswordFromFile()
         throws Exception
  {
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-D", "cn=Directory Manager",
      "-j", invalidPasswordFile,
      "-b", "",
      "-s", "base",
      "(objectClass=*)"
    };
    assertFalse(LDAPSearch.mainSearch(args, false, null, null) == 0);
  }
  /**
   * Tests a search with a malformed base DN.
   */
  @Test()
  public void testMalformedBaseDN()
  {
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-b", "malformed",
      "-s", "base",
      "(objectClass=*)"
    };
    assertFalse(LDAPSearch.mainSearch(args, false, null, null) == 0);
  }
  /**
   * Tests a search with a nonexistent base DN.
   */
  @Test()
  public void testNonExistentBaseDN()
  {
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-b", "o=does not exist",
      "-s", "base",
      "(objectClass=*)"
    };
    assertFalse(LDAPSearch.mainSearch(args, false, null, null) == 0);
  }
  /**
   * Retrieves the set of valid search scopes.
   *
   * @return  The set of valid search scopes.
   */
  @DataProvider(name = "scopes")
  public Object[][] getSearchScopes()
  {
    return new Object[][]
    {
      new Object[] { "base" },
      new Object[] { "one" },
      new Object[] { "sub" },
      new Object[] { "subordinate" },
    };
  }
  /**
   * Tests searches with the various allowed search scopes.
   *
   * @param  scope  The scope to use for the search.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test(dataProvider = "scopes")
  public void testSearchScopes(String scope)
         throws Exception
  {
    TestCaseUtils.initializeTestBackend(true);
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-b", "o=test",
      "-s", scope,
      "(objectClass=*)"
    };
    assertEquals(LDAPSearch.mainSearch(args, false, null, System.err), 0);
  }
  /**
   * Retrieves the set of valid alias dereferencing policies.
   *
   * @return  The set of valid alias dereferencing policies.
   */
  @DataProvider(name = "derefPolicies")
  public Object[][] getDerefPolicies()
  {
    return new Object[][]
    {
      new Object[] { "never" },
      new Object[] { "always" },
      new Object[] { "search" },
      new Object[] { "find" },
    };
  }
  /**
   * Tests searches with the various allowed dereference policy values.
   *
   * @param  derefPolicy  The alias dereferencing policy for the search.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test(dataProvider = "derefPolicies")
  public void testDerefPolicies(String derefPolicy)
         throws Exception
  {
    TestCaseUtils.initializeTestBackend(true);
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-b", "o=test",
      "-s", "base",
      "-a", derefPolicy,
      "(objectClass=*)"
    };
    assertEquals(LDAPSearch.mainSearch(args, false, null, System.err), 0);
  }
  /**
   * Tests with the typesOnly option.
   */
  @Test()
  public void testTypesOnly()
         throws Exception
  {
    TestCaseUtils.initializeTestBackend(true);
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-b", "o=test",
      "-s", "base",
      "-A",
      "(objectClass=*)"
    };
    assertEquals(LDAPSearch.mainSearch(args, false, null, System.err), 0);
  }
  /**
   * Tests with the reportAuthzID option for an unauthenticated search.
   */
  @Test()
  public void testReportAuthzIDUnauthenticated()
         throws Exception
  {
    TestCaseUtils.initializeTestBackend(true);
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-b", "o=test",
      "-s", "base",
      "--reportAuthzID",
      "(objectClass=*)"
    };
    assertEquals(LDAPSearch.mainSearch(args, false, null, System.err), 0);
  }
  /**
   * Tests with the reportAuthzID option for an authenticated search.
   */
  @Test()
  public void testReportAuthzIDAuthenticated()
         throws Exception
  {
    TestCaseUtils.initializeTestBackend(true);
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-D", "cn=Directory Manager",
      "-w", "password",
      "-b", "o=test",
      "-s", "base",
      "--reportAuthzID",
      "(objectClass=*)"
    };
    assertEquals(LDAPSearch.mainSearch(args, false, null, System.err), 0);
  }
  /**
   * Tests with the usePasswordPolicyControl option for an authenticated search.
   */
  @Test()
  public void testUsePasswordPolicyControl()
         throws Exception
  {
    TestCaseUtils.initializeTestBackend(true);
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-D", "cn=Directory Manager",
      "-w", "password",
      "-b", "o=test",
      "-s", "base",
      "--usePasswordPolicyControl",
      "(objectClass=*)"
    };
    assertEquals(LDAPSearch.mainSearch(args, false, null, System.err), 0);
  }
  /**
   * Tests with the account usability control for an authenticated search.
   */
  @Test()
  public void testAccountUsabilityControl()
         throws Exception
  {
    TestCaseUtils.initializeTestBackend(true);
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-D", "cn=Directory Manager",
      "-w", "password",
      "-b", "o=test",
      "-s", "base",
      "-J", OID_ACCOUNT_USABLE_CONTROL + ":true",
      "(objectClass=*)"
    };
    assertEquals(LDAPSearch.mainSearch(args, false, null, System.err), 0);
  }
  /**
   * Tests with the LDAP assertion control in which the assertion is true.
   */
  @Test()
  public void testLDAPAssertionControlTrue()
         throws Exception
  {
    TestCaseUtils.initializeTestBackend(true);
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-D", "cn=Directory Manager",
      "-w", "password",
      "-b", "o=test",
      "-s", "base",
      "--assertionFilter", "(objectClass=top)",
      "(objectClass=*)"
    };
    assertEquals(LDAPSearch.mainSearch(args, false, null, System.err), 0);
  }
  /**
   * Tests with the LDAP assertion control in which the assertion is false.
   */
  @Test()
  public void testLDAPAssertionControlFalse()
         throws Exception
  {
    TestCaseUtils.initializeTestBackend(true);
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-D", "cn=Directory Manager",
      "-w", "password",
      "-b", "o=test",
      "-s", "base",
      "--assertionFilter", "(objectClass=doesNotMatch)",
      "(objectClass=*)"
    };
    assertFalse(LDAPSearch.mainSearch(args, false, null, null) == 0);
  }
  /**
   * Tests with the LDAP matched values control.
   */
  @Test()
  public void testMatchedValuesControl()
         throws Exception
  {
    TestCaseUtils.initializeTestBackend(true);
    String[] args =
    {
      "-h", "127.0.0.1",
      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
      "-D", "cn=Directory Manager",
      "-w", "password",
      "-b", "o=test",
      "-s", "base",
      "--matchedValuesFilter", "(objectClass=*person)",
      "(objectClass=*)",
      "objectClass"
    };
    assertEquals(LDAPSearch.mainSearch(args, false, null, System.err), 0);
  }
  /**
   * Tests the LDAPSearch tool with the "--help" option.
   */
  @Test()
  public void testHelp()
  {
    String[] args = { "--help" };
    assertEquals(LDAPSearch.mainSearch(args, false, null, null), 0);
    args = new String[] { "-H" };
    assertEquals(LDAPSearch.mainSearch(args, false, null, null), 0);
  }
}
opends/tests/unit-tests-testng/src/server/org/opends/server/tools/ToolsTestCase.java
New file
@@ -0,0 +1,46 @@
/*
 * 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.tools;
import org.testng.annotations.Test;
import org.opends.server.DirectoryServerTestCase;
/**
 * An abstract base class for all tools test cases.
 */
@Test(groups = { "precommit", "tools" })
public abstract class ToolsTestCase
       extends DirectoryServerTestCase
{
  // No implementation required.
}