From 7c94a02ef7ed3965c644e2ed27966a9fb0eadb60 Mon Sep 17 00:00:00 2001
From: lutoff <lutoff@localhost>
Date: Fri, 22 Jun 2007 09:06:28 +0000
Subject: [PATCH]     * Add the ability to programmatically call the CLI     * Fix some NullPointerException in trustStore management     * Fix a bug in the certificate nickname handling     * Add unit tests

---
 opends/src/server/org/opends/server/admin/client/cli/DsServiceCliMain.java                          |   23 ++-
 opends/tests/unit-tests-testng/src/server/org/opends/server/admin/client/cli/DsserviceTestCase.java |  325 ++++++++++++++++++++++++++++++++++++++++++++++
 opends/src/server/org/opends/server/admin/client/cli/DsServiceCliParser.java                        |   44 ++++-
 3 files changed, 371 insertions(+), 21 deletions(-)

diff --git a/opends/src/server/org/opends/server/admin/client/cli/DsServiceCliMain.java b/opends/src/server/org/opends/server/admin/client/cli/DsServiceCliMain.java
index c993a70..0cc6ff8 100644
--- a/opends/src/server/org/opends/server/admin/client/cli/DsServiceCliMain.java
+++ b/opends/src/server/org/opends/server/admin/client/cli/DsServiceCliMain.java
@@ -86,7 +86,7 @@
 
   public static void main(String[] args)
   {
-    int retCode = mainCLI(args, System.out, System.err);
+    int retCode = mainCLI(args, true, System.out, System.err);
 
     if(retCode != 0)
     {
@@ -105,7 +105,7 @@
 
   public static int mainCLI(String[] args)
   {
-    return mainCLI(args, System.out, System.err);
+    return mainCLI(args, true, System.out, System.err);
   }
 
   /**
@@ -114,18 +114,18 @@
    *
    * @param  args              The command-line arguments provided to this
    *                           program.
+   * @param initializeServer   Indicates whether to initialize the server.
    * @param  outStream         The output stream to use for standard output, or
    *                           <CODE>null</CODE> if standard output is not
    *                           needed.
    * @param  errStream         The output stream to use for standard error, or
    *                           <CODE>null</CODE> if standard error is not
    *                           needed.
-   *
    * @return The error code.
    */
 
-  public static int mainCLI(String[] args, OutputStream outStream,
-      OutputStream errStream)
+  public static int mainCLI(String[] args, boolean initializeServer,
+      OutputStream outStream, OutputStream errStream)
   {
     PrintStream out;
     if (outStream == null)
@@ -148,7 +148,7 @@
     }
 
     DsServiceCliMain dsServiceCli = new DsServiceCliMain(out, err);
-    return dsServiceCli.execute(args);
+    return dsServiceCli.execute(args,initializeServer);
   }
 
   /**
@@ -157,10 +157,11 @@
    *
    * @param  args              The command-line arguments provided to this
    *                           program.
+   * @param  initializeServer  Indicates whether to initialize the server.
    *
    * @return The error code.
    */
-  public int execute(String[] args)
+  public int execute(String[] args, boolean initializeServer)
   {
     // Create the command-line argument parser for use with this
     // program.
@@ -281,7 +282,13 @@
     }
     ADSContext adsContext = new ADSContext(ctx);
 
-    DirectoryServer.bootstrapClient();
+    // Should we initialize the server in client mode?
+    if (initializeServer)
+    {
+      // Bootstrap and initialize directory data structures.
+      DirectoryServer.bootstrapClient();
+    }
+
     // perform the subCommand
     ADSContextException adsException = null ;
     try
diff --git a/opends/src/server/org/opends/server/admin/client/cli/DsServiceCliParser.java b/opends/src/server/org/opends/server/admin/client/cli/DsServiceCliParser.java
index 11f3ea9..9b165c8 100644
--- a/opends/src/server/org/opends/server/admin/client/cli/DsServiceCliParser.java
+++ b/opends/src/server/org/opends/server/admin/client/cli/DsServiceCliParser.java
@@ -554,17 +554,32 @@
       try
       {
         FileInputStream fos = new FileInputStream(trustStorePathArg.getValue());
-        String trustStorePasswordValue = null;
+        String trustStorePasswordStringValue = null;
+        char[] trustStorePasswordValue = null;
         if (trustStorePasswordArg.isPresent())
         {
-          trustStorePasswordValue = trustStorePasswordArg.getValue();
+          trustStorePasswordStringValue = trustStorePasswordArg.getValue();
         }
         else if (trustStorePasswordFileArg.isPresent())
         {
-          trustStorePasswordValue = trustStorePasswordFileArg.getValue();
+          trustStorePasswordStringValue = trustStorePasswordFileArg.getValue();
         }
+
+        if (trustStorePasswordStringValue !=  null)
+        {
+          trustStorePasswordStringValue = System
+              .getProperty("javax.net.ssl.trustStorePassword");
+        }
+
+
+        if (trustStorePasswordStringValue !=  null)
+        {
+          trustStorePasswordValue = trustStorePasswordStringValue.toCharArray();
+        }
+
         truststore = KeyStore.getInstance(KeyStore.getDefaultType());
-        truststore.load(fos, trustStorePasswordValue.toCharArray());
+        truststore.load(fos, trustStorePasswordValue);
+        fos.close();
       }
       catch (KeyStoreException e)
       {
@@ -596,7 +611,6 @@
       }
     }
     truststoreManager = new ApplicationTrustManager(truststore);
-    truststoreManager.setHost(getHostName());
     return truststoreManager;
   }
 
@@ -661,17 +675,21 @@
         // in a best effor mode.
         LOG.log(Level.WARNING, "Error with the keystore", e);
       }
-    }
-    ApplicationKeyManager akm = new ApplicationKeyManager(keyStore,
-        keyStorePasswordValue.toCharArray());
-    if (certNicknameArg.isPresent())
-    {
-      return new SelectableCertificateKeyManager(akm, certNicknameArg
-          .getValue());
+      ApplicationKeyManager akm = new ApplicationKeyManager(keyStore,
+          keyStorePasswordValue.toCharArray());
+      if (certNicknameArg.isPresent())
+      {
+        return new SelectableCertificateKeyManager(akm, certNicknameArg
+            .getValue());
+      }
+      else
+      {
+        return akm;
+      }
     }
     else
     {
-      return akm;
+      return null;
     }
   }
 
diff --git a/opends/tests/unit-tests-testng/src/server/org/opends/server/admin/client/cli/DsserviceTestCase.java b/opends/tests/unit-tests-testng/src/server/org/opends/server/admin/client/cli/DsserviceTestCase.java
new file mode 100644
index 0000000..3a7bd0c
--- /dev/null
+++ b/opends/tests/unit-tests-testng/src/server/org/opends/server/admin/client/cli/DsserviceTestCase.java
@@ -0,0 +1,325 @@
+/*
+ * 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-2007 Sun Microsystems, Inc.
+ */
+package org.opends.server.admin.client.cli;
+
+
+
+import java.io.File;
+import java.io.FileWriter;
+
+import org.testng.annotations.AfterClass;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.Test;
+
+import org.opends.server.TestCaseUtils;
+import org.opends.server.core.DirectoryServer;
+
+import static org.testng.Assert.*;
+
+import static org.opends.server.admin.client.cli.DsServiceCliReturnCode.*;
+
+
+
+/**
+ * A set of test cases for the dsservice tool.
+ */
+public class DsserviceTestCase
+{
+  // 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 before()
+         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();
+    
+    String[] args =
+    {
+      "create-ads",
+      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
+      "-w", "password",
+      "admin"
+    };
+
+    assertEquals(DsServiceCliMain.mainCLI(args, false, System.out, System.err),
+        ReturnCode.SUCCESSFUL.getReturnCode());
+  }
+
+  /**
+   * Ensures ADS is removed
+   * @throws  Exception  If an unexpected problem occurs.
+   */
+  @AfterClass()
+  public void afterClass()
+         throws Exception
+  {
+    String[] args =
+    {
+      "delete-ads",
+      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
+      "-w", "password",
+      "admin"
+    };
+
+    assertEquals(DsServiceCliMain.mainCLI(args, false, System.out, System.err),
+        ReturnCode.SUCCESSFUL.getReturnCode());
+  }
+
+  /**
+   * Tests list-groups with a malformed bind DN.
+   */
+  @Test()
+  public void testMalformedBindDN()
+  {
+    String[] args =
+    {
+      "list-groups",
+      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
+      "-D", "malformed",
+      "-w", "password"
+    };
+
+    assertFalse(DsServiceCliMain.mainCLI(args, false, null, null) 
+        == ReturnCode.SUCCESSFUL.getReturnCode());
+  }
+  
+  /**
+   * Tests list-groups with a nonexistent bind DN.
+   */
+  @Test()
+  public void testNonExistentBindDN()
+  {
+    String[] args =
+    {
+      "list-groups",
+      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
+      "-D", "cn=Does Not Exist",
+      "-w", "password"
+    };
+
+    assertFalse(DsServiceCliMain.mainCLI(args, false, System.out, System.err)
+        == ReturnCode.SUCCESSFUL.getReturnCode());
+  }
+
+  /**
+   * Tests list-groups with an invalid password.
+   */
+  @Test()
+  public void testInvalidBindPassword()
+  {
+    String[] args =
+    {
+      "list-groups",
+      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
+      "-D", "cn=Directory Manager",
+      "-w", "wrongPassword"
+    };
+
+    assertFalse(DsServiceCliMain.mainCLI(args, false, System.out, System.err)
+        == ReturnCode.SUCCESSFUL.getReturnCode());
+  }
+
+
+
+
+  /**
+   * Tests list-groups with a valid password read from a file.
+   *
+   * @throws  Exception  If an unexpected problem occurs.
+   */
+  @Test()
+  public void testValidPasswordFromFile()
+         throws Exception
+  {
+    String[] args =
+    {
+      "list-groups",
+      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
+      "-D", "cn=Directory Manager",
+      "-j", validPasswordFile,
+    };
+
+    assertEquals(DsServiceCliMain.mainCLI(args, false, System.out, System.err),
+        ReturnCode.SUCCESSFUL.getReturnCode());
+  }
+  
+  /**
+   * Tests list-groups with an invalid password read from a file.
+   *
+   * @throws  Exception  If an unexpected problem occurs.
+   */
+  @Test()
+  public void testInvalidPasswordFromFile()
+         throws Exception
+  {
+    String[] args =
+    {
+      "list-groups",
+      "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
+      "-D", "cn=Directory Manager",
+      "-j",invalidPasswordFile
+    };
+
+    assertFalse(DsServiceCliMain.mainCLI(args, false, System.out, System.err)
+        == ReturnCode.SUCCESSFUL.getReturnCode());
+  }
+
+  /**
+   * Tests a list-groups over SSL using blind trust.
+   */
+  @Test()
+  public void testListGroupsSSLBlindTrust()
+  {
+    String[] args =
+    {
+      "list-groups",
+      "-p", String.valueOf(TestCaseUtils.getServerLdapsPort()),
+      "-w", "password",
+      "-Z",
+      "-X"
+    };
+
+    assertEquals(DsServiceCliMain.mainCLI(args, false, System.out, System.err),
+        ReturnCode.SUCCESSFUL.getReturnCode());
+  }
+
+
+
+  /**
+   * Tests a list-groups over SSL using a trust store.
+   */
+  @Test()
+  public void testListGroupsSSLTrustStore()
+  {
+    String trustStorePath = DirectoryServer.getServerRoot() + File.separator +
+                            "config" + File.separator + "client.truststore";
+
+    String[] args =
+    {
+      "list-groups",
+      "-p", String.valueOf(TestCaseUtils.getServerLdapsPort()),
+      "-w", "password",
+      "-Z",
+      "-P", trustStorePath
+    };
+
+    assertEquals(DsServiceCliMain.mainCLI(args, false, System.out, System.err),
+        ReturnCode.SUCCESSFUL.getReturnCode());
+  }
+
+
+
+  /**
+   * Tests a list-groups using StartTLS with blind trust.
+   */
+  @Test(enabled=false)
+  public void testListGroupsStartTLSBlindTrust()
+  {
+    String[] args =
+    {
+      "list-groups",
+      "-p", String.valueOf(TestCaseUtils.getServerLdapsPort()),
+      "-w", "password",
+      "-q",
+      "-X"
+    };
+
+    assertEquals(DsServiceCliMain.mainCLI(args, false, null, System.err),
+        ReturnCode.SUCCESSFUL.getReturnCode());
+  }
+
+
+
+  /**
+   * Tests a list-groups using StartTLS with a trust store.
+   */
+  @Test(enabled=false)
+  public void testListGroupsStartTLSTrustStore()
+  {
+    String trustStorePath = DirectoryServer.getServerRoot() + File.separator +
+                            "config" + File.separator + "client.truststore";
+
+    String[] args =
+    {
+      "list-groups",
+      "-p", String.valueOf(TestCaseUtils.getServerLdapsPort()),
+      "-w", "password",
+      "-q",
+      "-P", trustStorePath
+    };
+
+    assertEquals(DsServiceCliMain.mainCLI(args, false, null, System.err),
+        ReturnCode.SUCCESSFUL.getReturnCode());
+  }
+  
+  /**
+   * Tests the dsservice with the "--help" option.
+   */
+  @Test()
+  public void testHelp()
+  {
+    String[] args = { "--help" };
+    assertEquals(DsServiceCliMain.mainCLI(args, false, null, null),
+        ReturnCode.SUCCESSFUL.getReturnCode());
+
+    args = new String[] { "-H" };
+    assertEquals(DsServiceCliMain.mainCLI(args, false, null, null),
+        ReturnCode.SUCCESSFUL.getReturnCode());
+
+    args = new String[] { "-?" };
+    assertEquals(DsServiceCliMain.mainCLI(args, false, null, null),
+        ReturnCode.SUCCESSFUL.getReturnCode());
+  }
+}
+

--
Gitblit v1.10.0