[jboss-cvs] JBossAS SVN: r74114 - in projects/oldstuff/testsuite/opends: src and 7 other directories.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Wed Jun 4 17:58:31 EDT 2008


Author: mmoyses
Date: 2008-06-04 17:58:30 -0400 (Wed, 04 Jun 2008)
New Revision: 74114

Added:
   projects/oldstuff/testsuite/opends/src/
   projects/oldstuff/testsuite/opends/src/main/
   projects/oldstuff/testsuite/opends/src/main/org/
   projects/oldstuff/testsuite/opends/src/main/org/jboss/
   projects/oldstuff/testsuite/opends/src/main/org/jboss/test/
   projects/oldstuff/testsuite/opends/src/main/org/jboss/test/security/
   projects/oldstuff/testsuite/opends/src/main/org/jboss/test/security/test/
   projects/oldstuff/testsuite/opends/src/main/org/jboss/test/security/test/opends/
   projects/oldstuff/testsuite/opends/src/main/org/jboss/test/security/test/opends/LdapBasicUnitTestCase.java
   projects/oldstuff/testsuite/opends/src/main/org/jboss/test/security/test/opends/OpenDSService.java
   projects/oldstuff/testsuite/opends/src/main/org/jboss/test/security/test/opends/OpenDSUtil.java
   projects/oldstuff/testsuite/opends/src/resources/
Log:
JBAS-5584

Added: projects/oldstuff/testsuite/opends/src/main/org/jboss/test/security/test/opends/LdapBasicUnitTestCase.java
===================================================================
--- projects/oldstuff/testsuite/opends/src/main/org/jboss/test/security/test/opends/LdapBasicUnitTestCase.java	                        (rev 0)
+++ projects/oldstuff/testsuite/opends/src/main/org/jboss/test/security/test/opends/LdapBasicUnitTestCase.java	2008-06-04 21:58:30 UTC (rev 74114)
@@ -0,0 +1,144 @@
+/*
+ * JBoss, the OpenSource J2EE webOS
+ *
+ * Distributable under LGPL license.
+ * See terms of license at gnu.org.
+ */ 
+package org.jboss.test.security.test.opends;
+
+import java.io.File;
+import java.net.URL;
+import java.util.Hashtable;
+
+import javax.naming.Context;
+import javax.naming.NamingEnumeration;
+import javax.naming.directory.DirContext;
+import javax.naming.directory.InitialDirContext;
+import javax.naming.directory.SearchControls;
+import javax.naming.directory.SearchResult;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
+import org.jboss.test.JBossTestCase;
+import org.opends.server.tools.LDAPModify;
+
+/**
+ *  Basic tests for the ldap integration
+ *  @author <a href="mailto:Anil.Saldhana at jboss.org">Anil Saldhana</a>
+ *  @version $Revision$
+ *  @since  Sep 13, 2006
+ */
+public class LdapBasicUnitTestCase extends JBossTestCase
+{  
+   public static Test suite()
+   {
+      TestSuite suite = new TestSuite();
+      suite.addTest(new LdapBasicUnitTestCase("testLdap")); 
+      suite.addTest(new LdapBasicUnitTestCase("testJNDI"));
+      return suite; 
+   }
+   
+   /**
+    * Inject the ldap initial ctx factory via system property
+    */
+   private String contextFactory = System.getProperty("jboss.security.ldap.ctxfactory",
+         "com.sun.jndi.ldap.LdapCtxFactory");
+   private OpenDSUtil util = new OpenDSUtil();
+   private String serverHost;
+   private String port = "1389";
+   private String adminDN = "cn=Directory Manager";
+   private String adminPW = "password";
+   private String dn = "dc=jboss,dc=org";
+   
+   public LdapBasicUnitTestCase(String name)
+   {
+      super(name); 
+   }
+
+   protected void setUp() throws Exception
+   {
+       super.setUp();
+       serverHost = getServerHost();
+   }
+    
+   /**
+    * Test if a DN exists. If not, add it. Then do a search. Then delete
+    * @throws Exception
+    */
+   public void testLdap() throws Exception
+   {   
+      if(util.existsDN(serverHost, port, dn))
+         util.deleteDN(serverHost, port, adminDN, adminPW, dn, true);
+      assertTrue("ldap add success?" , performLdifAdd());
+      assertTrue("ldap search success?" , util.existsDN(serverHost, port, dn));
+      assertTrue("ldap delete success?",
+            util.deleteDN(serverHost, port, adminDN, adminPW, dn, true));
+      assertFalse("ldap search should fail" , util.existsDN(serverHost, port, dn));
+   } 
+   
+   /**
+    * Test that JNDI operations work
+    * @throws Exception
+    */
+   public void testJNDI() throws Exception
+   {  
+      DirContext dc = null;
+      NamingEnumeration ne = null;
+      try
+      {
+         dc = this.getDirContext();
+         assertNotNull("DirContext exists?", dc);
+         if(util.existsDN(serverHost, port, dn))
+            util.deleteDN(serverHost, port, adminDN, adminPW, dn, true);
+         assertTrue("ldap add success?" , performLdifAdd());
+         SearchControls sc = new SearchControls();
+         sc.setSearchScope(SearchControls.SUBTREE_SCOPE);
+         ne = dc.search(dn, "(objectclass=*)", sc);
+         while (ne.hasMore()) 
+         { 
+            SearchResult sr = (SearchResult) ne.next(); 
+            assertTrue("Search Result exists?", sr != null); 
+        }
+        assertTrue("ldap delete success?",
+               util.deleteDN(serverHost, port, adminDN, adminPW, dn, true));
+        assertFalse("ldap search should fail" , util.existsDN(serverHost, port, dn));
+      }
+      finally
+      {
+         if(ne != null)
+            ne.close();
+         if(dc != null)
+           dc.close(); 
+      } 
+   }
+ 
+   //***************************************************************
+   //   PRIVATE METHODS
+   //***************************************************************
+   private boolean performLdifAdd() throws Exception
+   {
+      String fileurl = this.getResourceURL("security/opends/ldif/example1.ldif");
+      URL url = this.getDeployURL(fileurl);
+      log.debug("ldap add ldif url="+url);
+      File file = new File(url.getPath()); 
+      String[] cmd = new String[] {"-h", getServerHost(), "-p",
+            "1389", "-D", "cn=Directory Manager",
+            "-w", "password",
+            "-a", "-f",file.getPath()};
+      
+      return LDAPModify.mainModify(cmd) == 0;
+   }
+   
+   private DirContext getDirContext() throws Exception
+   {
+      String url = "ldap://" + getServerHost() + ":1389";
+      Hashtable env = new Hashtable();
+      env.put(Context.INITIAL_CONTEXT_FACTORY, contextFactory);
+      env.put(Context.PROVIDER_URL, url);
+      env.put(Context.SECURITY_AUTHENTICATION, "simple");
+      env.put(Context.SECURITY_PRINCIPAL, this.adminDN);
+      env.put(Context.SECURITY_CREDENTIALS, this.adminPW);
+      return new InitialDirContext(env);   
+   }
+}

Added: projects/oldstuff/testsuite/opends/src/main/org/jboss/test/security/test/opends/OpenDSService.java
===================================================================
--- projects/oldstuff/testsuite/opends/src/main/org/jboss/test/security/test/opends/OpenDSService.java	                        (rev 0)
+++ projects/oldstuff/testsuite/opends/src/main/org/jboss/test/security/test/opends/OpenDSService.java	2008-06-04 21:58:30 UTC (rev 74114)
@@ -0,0 +1,105 @@
+/*
+ * JBoss, the OpenSource J2EE webOS
+ *
+ * Distributable under LGPL license.
+ * See terms of license at gnu.org.
+ */ 
+package org.jboss.test.security.test.opends;
+
+import java.net.URL;
+import java.security.AccessController;
+import java.security.PrivilegedAction;
+
+import org.jboss.system.ServiceMBeanSupport;
+import org.opends.server.core.DirectoryServer;
+ 
+
+/**
+ *  XMBean Service for OpenDS integration
+ *  @author <a href="mailto:Anil.Saldhana at jboss.org">Anil Saldhana</a>
+ *  @version $Revision$
+ *  @since  Sep 13, 2006
+ */
+public class OpenDSService extends ServiceMBeanSupport
+{
+   public static String objectName = "jboss.test:service=opends";
+   
+   private String newline = (String)
+            AccessController.doPrivileged(new GetSystemPropertyAction("line.separator"));  
+   
+   
+   /**
+    * Print some information about the DS (eg: connections etc)
+    * @return
+    */
+   public String printDiagnostics()
+   {
+     StringBuilder sb = new StringBuilder();
+     sb.append("Maximum concurrent client connections allowed:");
+     sb.append(DirectoryServer.getMaxAllowedConnections());
+     sb.append(newline).append("# of client connections currently established:");
+     sb.append(DirectoryServer.getCurrentConnections());
+     return sb.toString();
+   }
+   
+   /**
+    * Restart the Directory Server
+    */
+   public void restart()
+   {
+     DirectoryServer.restart(getClass().getName(), "DS restart");   
+   }
+   
+   protected void startService() throws Exception
+   {
+      super.startService();
+      
+      //Get the location of the conf directory
+      String confLoc = (String)AccessController.doPrivileged(
+            new GetSystemPropertyAction("jboss.server.config.url"));
+      
+      /**
+       * There seems to be a need to maintain the opends directory
+       * structure. We will create it under the conf dir
+       */
+      ClassLoader tcl = Thread.currentThread().getContextClassLoader();
+      URL ldif = tcl.getResource("opends/config/config.ldif");
+      log.debug("config ldif="+ldif);
+      
+      String[] strArr = new String[] {"--configClass",
+                                    "org.opends.server.config.ConfigFileHandler",
+                                    "--configFile", 
+                                    ldif.getPath()}; 
+      //Start the OpenDS
+      DirectoryServer.main(strArr);
+   }
+
+   protected void stopService() throws Exception
+   { 
+      log.debug("Asking DS to shutdown"); 
+      DirectoryServer.shutDown(getClass().getName(), "Shut down DS");
+   }
+   
+   
+   /**
+    * 
+    * A GetSystemPropetyAction.
+    * 
+    * @author <a href="anil.saldhana at jboss.com">Anil Saldhana</a>
+    * @version $Revision: 1.1 $
+    */
+   public class GetSystemPropertyAction implements PrivilegedAction
+   { 
+      private String property;
+
+      public GetSystemPropertyAction(String prop)
+      {
+         this.property = prop;
+      }
+
+      public Object run()
+      {
+         return System.getProperty(property);
+      }
+   } 
+}
\ No newline at end of file

Added: projects/oldstuff/testsuite/opends/src/main/org/jboss/test/security/test/opends/OpenDSUtil.java
===================================================================
--- projects/oldstuff/testsuite/opends/src/main/org/jboss/test/security/test/opends/OpenDSUtil.java	                        (rev 0)
+++ projects/oldstuff/testsuite/opends/src/main/org/jboss/test/security/test/opends/OpenDSUtil.java	2008-06-04 21:58:30 UTC (rev 74114)
@@ -0,0 +1,155 @@
+/*
+ * JBoss, the OpenSource J2EE webOS
+ *
+ * Distributable under LGPL license.
+ * See terms of license at gnu.org.
+ */ 
+package org.jboss.test.security.test.opends;
+
+import java.io.File;
+import java.net.URL;
+import java.util.StringTokenizer;
+
+import org.jboss.logging.Logger;
+import org.opends.server.tools.LDAPCompare;
+import org.opends.server.tools.LDAPDelete;
+import org.opends.server.tools.LDAPModify;
+import org.opends.server.tools.LDAPSearch;
+
+/**
+ *  Utility class that deals with the integrated ldap (OpenDS)
+ *  @author <a href="mailto:Anil.Saldhana at jboss.org">Anil Saldhana</a>
+ *  @version $Revision$
+ *  @since  Sep 15, 2006
+ */
+public class OpenDSUtil
+{
+   private static final Logger log = Logger.getLogger(OpenDSUtil.class);
+   
+   public OpenDSUtil()
+   {  
+   }
+   
+   /**
+    * Add a LDIF file into the Directory Server
+    * @param serverHost Server Host (Use getServerHost() of JBossTestxxx)
+    * @param port Port for the DS
+    * @param admin admin dn ("cn=Directory Manager")
+    * @param adminpwd (password)
+    * @param ldifURL (use getDeployURL of JBossTestxxx)
+    * @return whether the add was success
+    */
+   public boolean addLDIF(String serverHost, String port, String admin,
+         String adminpwd, URL ldifURL)
+   {
+      File ldifFile = new File(ldifURL.getPath());
+      if(!ldifFile.exists())
+         throw new IllegalArgumentException("LDIF file:"+ ldifURL + " does not exist");
+      String[] cmd = new String[] {"-h", serverHost, "-p",
+            port, "-D", admin,
+            "-w", adminpwd, "-a", "-f",ldifFile.getPath()};
+      log.debug("addLDIF:" + print(cmd));
+      return LDAPModify.mainModify(cmd) == 0;
+   }
+   
+   /**
+    * Delete a DN in the Directory Server
+   * @param serverHost Server Host (Use getServerHost() of JBossTestxxx)
+    * @param port Port for the DS
+    * @param admin admin dn ("cn=Directory Manager")
+    * @param adminpwd (password)
+    * @param dnToDelete DN to delete (Eg: dc=jboss,dc=org)
+    * @param recursive should children also go?
+    * @return whether the delete op was success
+    */
+   public boolean deleteDN(String serverHost, String port, String admin,
+         String adminpwd, String dnToDelete, boolean recursive)
+   { 
+      String rec = recursive ? "-x" : " ";
+      
+      String[] cmd = new String[] {"-h", serverHost, "-p",
+            port, "-D", admin,
+            "-w", adminpwd, rec,dnToDelete};
+      log.debug("deleteDN:" + print(cmd)); 
+      return LDAPDelete.mainDelete(cmd) == 0;
+   }
+   
+   /**
+    * Check whether a DN exists. Typically before you do a ldap delete
+    * @param serverHost
+    * @param port
+    * @param dn
+    * @return whether the DN exists?
+    */
+   public boolean existsDN(String serverHost, String port, String dn)
+   {   
+      String[] cmd = new String[] {"-h", serverHost, "-p",
+            port, "-b", dn ,"-s", "sub", "objectclass=*"};
+      log.debug("existsDN:" + print(cmd)); 
+      return LDAPSearch.mainSearch(cmd) == 0;
+   }
+   
+   /**
+    * Issue a ldapCompare in the standard ldapCompare cmd line syntax
+    * (Eg: "-h localhost -p 1389 -D "cn=..." -w password -a -f ldif.txt)
+    * @param cmdline
+    * @return whether ldapCompare was success
+    */
+   public boolean ldapCompare(String cmdline)
+   {
+      String[] strArr = getStringArr(cmdline);
+      log.debug("ldapCompare:"+print(strArr));
+      return LDAPCompare.mainCompare(strArr) == 0;
+   }
+   
+   /**
+    * Issue a ldapdelete in the standard ldapdelete cmd line syntax
+    * (Eg: "-h localhost -p 1389 -D "cn=..." -w password -a -f ldif.txt)
+    * @param cmdline
+    * @return whether ldapmodify was success
+    */
+   public boolean ldapDelete(String cmdline)
+   {
+      String[] strArr = getStringArr(cmdline);
+      log.debug("ldapDelete:"+print(strArr));
+      return LDAPDelete.mainDelete(strArr) == 0;
+   }
+   
+   /**
+    * Issue a ldapmodify in the standard ldapmodify cmd line syntax
+    * (Eg: "-h localhost -p 1389 -D "cn=..." -w password -a -f ldif.txt)
+    * @param cmdline
+    * @return whether ldapmodify was success
+    */
+   public boolean ldapModify(String cmdline)
+   {
+      String[] strArr = getStringArr(cmdline);
+      log.debug("ldapModify:"+print(strArr));
+      return LDAPModify.mainModify(strArr) == 0;
+   }
+  
+   //***************************************************************
+   //   PRIVATE METHODS
+   //***************************************************************
+   private String[] getStringArr(String str)
+   {
+      StringTokenizer st = new StringTokenizer(str);
+      int num = st.countTokens();
+      String[] strarr = new String[num];
+      int i = 0;
+      while(st.hasMoreTokens())
+      {
+         strarr[i++] = st.nextToken();
+      }
+      return strarr;
+   } 
+   
+   private String print(String[] arr)
+   {
+      StringBuilder sb = new StringBuilder();
+      int len = arr != null ? arr.length : 0;
+      for(int i=0; i < len; i++)
+         sb.append(arr[i]);
+      return sb.toString();
+   }
+}




More information about the jboss-cvs-commits mailing list