[jboss-cvs] JBossAS SVN: r112718 - in branches/JBPAPP_5_1_2_GA_JBPAPP-7788/testsuite/src/main/org/jboss/test: cluster/multicfg/test and 2 other directories.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Thu Mar 1 07:15:13 EST 2012


Author: pskopek at redhat.com
Date: 2012-03-01 07:15:11 -0500 (Thu, 01 Mar 2012)
New Revision: 112718

Added:
   branches/JBPAPP_5_1_2_GA_JBPAPP-7788/testsuite/src/main/org/jboss/test/NamingUtil.java
Modified:
   branches/JBPAPP_5_1_2_GA_JBPAPP-7788/testsuite/src/main/org/jboss/test/cluster/multicfg/test/HAJndiTestCase.java
   branches/JBPAPP_5_1_2_GA_JBPAPP-7788/testsuite/src/main/org/jboss/test/naming/test/SecurityUnitTestCase.java
   branches/JBPAPP_5_1_2_GA_JBPAPP-7788/testsuite/src/main/org/jboss/test/naming/test/SimpleUnitTestCase.java
   branches/JBPAPP_5_1_2_GA_JBPAPP-7788/testsuite/src/main/org/jboss/test/security/test/LoginModulesUnitTestCase.java
Log:
[JBPAPP-7788] test fixes with regard to issue fixed by this JIRA

Added: branches/JBPAPP_5_1_2_GA_JBPAPP-7788/testsuite/src/main/org/jboss/test/NamingUtil.java
===================================================================
--- branches/JBPAPP_5_1_2_GA_JBPAPP-7788/testsuite/src/main/org/jboss/test/NamingUtil.java	                        (rev 0)
+++ branches/JBPAPP_5_1_2_GA_JBPAPP-7788/testsuite/src/main/org/jboss/test/NamingUtil.java	2012-03-01 12:15:11 UTC (rev 112718)
@@ -0,0 +1,207 @@
+ /*
+  * JBoss, Home of Professional Open Source
+  * Copyright 2005, JBoss Inc., and individual contributors as indicated
+  * by the @authors tag. See the copyright.txt in the distribution for a
+  * full listing of individual contributors.
+  *
+  * This is free software; you can redistribute it and/or modify it
+  * under the terms of the GNU Lesser General Public License as
+  * published by the Free Software Foundation; either version 2.1 of
+  * the License, or (at your option) any later version.
+  *
+  * This software is distributed in the hope that it will be useful,
+  * but WITHOUT ANY WARRANTY; without even the implied warranty of
+  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+  * Lesser General Public License for more details.
+  *
+  * You should have received a copy of the GNU Lesser General Public
+  * License along with this software; if not, write to the Free
+  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+  */
+
+package org.jboss.test;
+
+import java.net.MalformedURLException;
+import java.net.URI;
+import java.net.URL;
+import java.util.Properties;
+
+import javax.naming.Context;
+import javax.naming.InitialContext;
+import javax.security.auth.login.LoginContext;
+
+import org.apache.log4j.Logger;
+import org.jboss.test.util.AppCallbackHandler;
+
+/**
+ * Utility class for tests using naming in a special way.
+ * 
+ * @author  <a href="mailto:pskopek at redhat.com">Peter Skopek</a>
+ *
+ */
+public class NamingUtil
+{
+   
+   public static Logger log = Logger.getLogger(NamingUtil.class);
+   
+   public static final String JNDI_INVOKER = "invoker/JNDIFactory";
+   public static final String HAJNDI_INVOKER = "invoker/HAJNDIFactory";
+   
+   /**
+    * Create test data. It needs to use org.jboss.naming.HttpNamingContextFactory since JNDI through RMI
+    * is already secured and disallow bind/unbind/rebind operations.
+    * 
+    *   
+    *   
+    * @param jndiName JNDI path where to bind data.
+    * @param data Data object to bind. In case data is null last path element is considered subContext. 
+    * @throws Exception
+    */
+   public static void createTestJNDIBinding(String jndiName, Object data, String serverHost) 
+      throws Exception 
+   {
+      
+      Context ctx = NamingUtil.getFullInitialContext(serverHost);
+
+      String[] path = jndiName.split("/");
+      String subPath = "";
+      for (int i = 0; i < path.length; i++) 
+      {
+         
+         if (path[i].equals("")) 
+         {
+            continue;
+         }
+         
+         subPath = subPath + "/" + path[i]; 
+         
+         if (i < path.length - 1)
+         { 
+            ctx.createSubcontext(subPath);
+         }
+         else 
+         {
+            if (data != null)
+            {   
+               ctx.bind(subPath, data);
+            }
+            else
+            {
+               ctx.createSubcontext(subPath);
+            }
+               
+         }
+      }
+      
+      ctx.close();
+      
+   }
+   
+   /**
+    * Returns initial context which is able to perform all JNDI operations.
+    * @param serverHost - use getServerHostForURL() from inside JBoss Testsuite
+    * @param jndiFactoryUrlSuffix - URL suffix to get proper invoker invoker/JNDIFactory or invoker/HAJNDIFactory
+    * @return
+    * @throws Exception
+    */
+   public static InitialContext getFullInitialContext(String serverHost, String jndiFactoryUrlSuffix) 
+         throws Exception 
+   {
+
+      if (jndiFactoryUrlSuffix == null) {
+         jndiFactoryUrlSuffix = JNDI_INVOKER;
+      }
+      
+      Properties env = new Properties();
+      env.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.HttpNamingContextFactory");
+
+      // Specify the login conf file location
+      String authConf = getResourceURL("security/auth.conf");
+      log.debug("Using auth.conf: "+authConf);
+      System.setProperty("java.security.auth.login.config", authConf);
+      AppCallbackHandler handler = new AppCallbackHandler("admin", "admin".toCharArray());
+      LoginContext lc = new LoginContext("createTestJNDIBinding", handler);
+      lc.login();
+
+      // Test the secured JNDI factory
+      // JBPAPP-2997
+      env.setProperty(Context.PROVIDER_URL, "http://" + serverHost + ":8080/" + jndiFactoryUrlSuffix);
+      log.debug("Creating InitialContext with env="+env);
+      InitialContext ctx = new InitialContext(env);
+      
+      return ctx;
+   }
+   
+   
+   
+   /**
+    * Returns initial context which is able to perform all JNDI operations.
+    * @param serverHost - use getServerHostForURL() from inside JBoss Testsuite
+    * @return
+    * @throws Exception
+    */
+   public static InitialContext getFullInitialContext(String serverHost) 
+         throws Exception 
+   {
+      return getFullInitialContext(serverHost, JNDI_INVOKER);
+   }
+   
+   /**
+    * Returns initial context which is able to perform all JNDI operations.
+    * @param serverHost - use getServerHostForURL() from inside JBoss Testsuite
+    * @return
+    * @throws Exception
+    */
+   public static InitialContext getFullHAInitialContext(String serverHost) 
+         throws Exception 
+   {
+      return getFullInitialContext(serverHost, HAJNDI_INVOKER);
+   }
+   
+   /**
+    * Extract hostname from jndiURL parameter and get InitialContext using @see getFullInitialContext
+    * @param jndiUrl
+    * @return
+    * @throws Exception
+    */
+   public static InitialContext getFullInitialContextFromUrl(String jndiUrl) 
+         throws Exception 
+   {
+      log.debug("jndiUrl = " + jndiUrl);
+      URI uri = new URI(jndiUrl);
+      log.debug("host="+uri.getHost());
+      return NamingUtil.getFullInitialContext(uri.getHost());
+   }
+   
+   /**
+    * Extract hostname from jndiURL parameter and get InitialContext using @see getFullInitialContext
+    * @param jndiUrl
+    * @return
+    * @throws Exception
+    */
+   public static InitialContext getFullHAInitialContextFromUrl(String jndiUrl) 
+         throws Exception 
+   {
+      log.debug("jndiUrl = " + jndiUrl);
+      URI uri = new URI(jndiUrl);
+      log.debug("host="+uri.getHost());
+      return NamingUtil.getFullHAInitialContext(uri.getHost());
+   }
+   
+   /**
+    * Returns URL string for given resource. 
+    * 
+    * @param resource
+    * @return
+    * @throws MalformedURLException
+    */
+   private static String getResourceURL(final String resource) 
+         throws MalformedURLException
+   {
+      ClassLoader loader = Thread.currentThread().getContextClassLoader();
+      URL resURL = loader.getResource(resource);
+      return resURL != null ? resURL.toString() : null;
+   }
+   
+}

Modified: branches/JBPAPP_5_1_2_GA_JBPAPP-7788/testsuite/src/main/org/jboss/test/cluster/multicfg/test/HAJndiTestCase.java
===================================================================
--- branches/JBPAPP_5_1_2_GA_JBPAPP-7788/testsuite/src/main/org/jboss/test/cluster/multicfg/test/HAJndiTestCase.java	2012-02-29 16:18:31 UTC (rev 112717)
+++ branches/JBPAPP_5_1_2_GA_JBPAPP-7788/testsuite/src/main/org/jboss/test/cluster/multicfg/test/HAJndiTestCase.java	2012-03-01 12:15:11 UTC (rev 112718)
@@ -29,6 +29,7 @@
 
 import junit.framework.Test;
 
+import org.jboss.test.NamingUtil;
 import org.jboss.test.cluster.ejb2.crossserver.CalledHome;
 import org.jboss.test.cluster.ejb2.crossserver.CalledRemote;
 import org.jnp.interfaces.NamingContext;
@@ -86,8 +87,8 @@
       getLog().debug("HAJndiTestCase.testLocalBinding()");
       validateUrls();
       
-      // bind to node0 locally
-      Context naming = getContext(NODE0_JNDI);
+      // bind to node0 locally using different JNDI connector because of disabling bind operation from non-local JVMs
+      Context naming = NamingUtil.getFullInitialContextFromUrl(NODE0_JNDI);
       naming.bind(LOCAL0_KEY, LOCAL0_VALUE);
       closeContext(naming);
       
@@ -128,7 +129,7 @@
       validateUrls();
       
       // bind to node0 using HA-JNDI
-      Context naming = getContext(NODE0_HAJNDI);
+      Context naming = NamingUtil.getFullHAInitialContextFromUrl(NODE0_HAJNDI);
       naming.bind(GLOBAL0_KEY, GLOBAL0_VALUE);
       closeContext(naming);
       
@@ -170,7 +171,7 @@
       validateUrls();
       
       // bind to node0 using HA-JNDI
-      Context naming = getContext(NODE0_HAJNDI);
+      Context naming = NamingUtil.getFullHAInitialContextFromUrl(NODE0_HAJNDI);
       naming.bind(JNDI_KEY, JNDI_VALUE1);     
      
       // lookup binding using HA-JNDI on Node0 - should succeed
@@ -201,7 +202,7 @@
       // lookup binding - should fail with NamingException
       value = (String)lookup(naming, JNDI_KEY, false);
       assertNull("lookup after HA-JNDI unbind operation", value);
-      
+      org.jboss.test.cluster.multicfg.test.HAJndiTestCase
       closeContext(naming);  
    }
    
@@ -217,7 +218,7 @@
       validateUrls();
       
       // create subcontexts
-      Context naming = getContext(NODE0_HAJNDI);
+      Context naming = NamingUtil.getFullHAInitialContext(NODE0_HAJNDI);
       Context sub1 = naming.createSubcontext(SUBCON1);
       Context sub2 = naming.createSubcontext(SUBCON2);
       Context sub1a = sub1.createSubcontext(SUBCON1A);

Modified: branches/JBPAPP_5_1_2_GA_JBPAPP-7788/testsuite/src/main/org/jboss/test/naming/test/SecurityUnitTestCase.java
===================================================================
--- branches/JBPAPP_5_1_2_GA_JBPAPP-7788/testsuite/src/main/org/jboss/test/naming/test/SecurityUnitTestCase.java	2012-02-29 16:18:31 UTC (rev 112717)
+++ branches/JBPAPP_5_1_2_GA_JBPAPP-7788/testsuite/src/main/org/jboss/test/naming/test/SecurityUnitTestCase.java	2012-03-01 12:15:11 UTC (rev 112718)
@@ -29,14 +29,17 @@
 import javax.naming.InitialContext;
 import javax.naming.NamingEnumeration;
 import javax.naming.NamingException;
+import javax.naming.NoPermissionException;
 import javax.rmi.PortableRemoteObject;
 import javax.security.auth.login.LoginContext;
 
+import junit.framework.Assert;
 import junit.framework.Test;
 import junit.framework.TestSuite;
 
 import org.jboss.security.SecurityAssociation;
 import org.jboss.test.JBossTestCase;
+import org.jboss.test.NamingUtil;
 import org.jboss.test.naming.interfaces.TestENC;
 import org.jboss.test.naming.interfaces.TestENCHome;
 import org.jboss.test.util.AppCallbackHandler;
@@ -65,6 +68,9 @@
       suite.addTest(new SecurityUnitTestCase("testLoginInitialContext"));
       suite.addTest(new SecurityUnitTestCase("testSecureEJBViaLoginInitialContextFactory"));
       suite.addTest(new SecurityUnitTestCase("testSecureEJBViaJndiLoginInitialContextFactory"));
+      suite.addTest(new SecurityUnitTestCase("testNamingOperationBindSecurity"));
+      suite.addTest(new SecurityUnitTestCase("testNamingOperationUnBindSecurity"));
+      suite.addTest(new SecurityUnitTestCase("testNamingOperationCreateSubcontextSecurity"));
 
       return suite;
    }
@@ -171,17 +177,9 @@
       /* Try without a login to ensure that a lookup against "readonly" works.
        *First create the readonly context using the standard JNDI factory
       */
-      InitialContext bootCtx = new InitialContext();
-      try
-      {
-         bootCtx.unbind("readonly");
-      }
-      catch(NamingException ignore)
-      {
-      }
-      Context readonly = bootCtx.createSubcontext("readonly");
-      readonly.bind("data", "somedata");
 
+      NamingUtil.createTestJNDIBinding("readonly/data", "somedata", getServerHost());
+      
       Properties env = new Properties();
       env.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.HttpNamingContextFactory");
       env.setProperty(Context.PROVIDER_URL, INVOKER_BASE + "ReadOnlyJNDIFactory");
@@ -250,9 +248,16 @@
       }
       catch(NamingException ignore)
       {
+          getLog().error("Caught exception, but will be ignored", ignore);
       }
       getLog().debug("Creating readonly context");
-      bootCtx.createSubcontext("readonly");
+      try {
+         bootCtx.createSubcontext("readonly");
+      }
+      catch (Exception e) {
+         getLog().debug(e);
+         throw new Exception(e);
+      }
       bootCtx.bind("readonly/data", "somedata");
 
       // Test access through the readonly proxy
@@ -425,4 +430,83 @@
          super.undeploy("naming.jar");
       }
    }
+   
+   public void testNamingOperationBindSecurity() throws Exception
+   {
+      getLog().debug("+++ testNamingOperationBindSecurity");
+
+      Properties env = new Properties();
+      env.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
+      // JBPAPP-2997
+      env.setProperty(Context.PROVIDER_URL, "jnp://" + getServerHostForURL() + ":1099/");
+
+      getLog().debug("Creating InitialContext with env="+env);
+      InitialContext ctx = new InitialContext(env);
+      getLog().debug("Created InitialContext, ctx="+ctx);
+      getLog().info("ctx="+ctx.getClass().getName());
+      
+      try 
+      {
+         ctx.bind("fruit", "lemon");
+         Assert.fail("binding is expected to fail with javax.naming.NoPermissionException");
+      }
+      catch (NoPermissionException npe) 
+      {
+         // expected
+         getLog().debug("Caught exception as expected");
+      }
+   }
+   
+   public void testNamingOperationUnBindSecurity() throws Exception
+   {
+      getLog().debug("+++ testNamingOperationUnBindSecurity");
+
+      Properties env = new Properties();
+      env.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
+      // JBPAPP-2997
+      env.setProperty(Context.PROVIDER_URL, "jnp://" + getServerHost() + ":1099/");
+
+      getLog().debug("Creating InitialContext with env="+env);
+      InitialContext ctx = new InitialContext(env);
+      getLog().debug("Created InitialContext, ctx="+ctx);
+      getLog().info("ctx="+ctx.getClass().getName());
+      
+      try 
+      {
+         ctx.unbind("/queue/D");
+         Assert.fail("unbinding is expected to fail with javax.naming.NoPermissionException");
+      }
+      catch (NoPermissionException npe) 
+      {
+         // expected
+         getLog().debug("Caught exception as expected");
+      }
+   }
+      
+   public void testNamingOperationCreateSubcontextSecurity() throws Exception
+   {
+      getLog().debug("+++ testNamingOperationCreateSubcontextSecurity");
+
+      Properties env = new Properties();
+      env.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
+      // JBPAPP-2997
+      env.setProperty(Context.PROVIDER_URL, "jnp://" + getServerHost() + ":1099/");
+
+      getLog().debug("Creating InitialContext with env="+env);
+      InitialContext ctx = new InitialContext(env);
+      getLog().debug("Created InitialContext, ctx="+ctx);
+      getLog().info("ctx="+ctx.getClass().getName());
+      
+      try 
+      {
+         ctx.createSubcontext("/never_should_be_created_subcontext");
+         Assert.fail("createSubcontext is expected to fail with javax.naming.NoPermissionException");
+      }
+      catch (NoPermissionException npe) 
+      {
+         // expected
+         getLog().debug("Caught exception as expected");
+      }
+   }
+   
 }

Modified: branches/JBPAPP_5_1_2_GA_JBPAPP-7788/testsuite/src/main/org/jboss/test/naming/test/SimpleUnitTestCase.java
===================================================================
--- branches/JBPAPP_5_1_2_GA_JBPAPP-7788/testsuite/src/main/org/jboss/test/naming/test/SimpleUnitTestCase.java	2012-02-29 16:18:31 UTC (rev 112717)
+++ branches/JBPAPP_5_1_2_GA_JBPAPP-7788/testsuite/src/main/org/jboss/test/naming/test/SimpleUnitTestCase.java	2012-03-01 12:15:11 UTC (rev 112718)
@@ -21,7 +21,8 @@
  */
 package org.jboss.test.naming.test;
 
-import java.util.Properties;
+import org.jboss.test.JBossTestCase;
+import org.jboss.test.NamingUtil;
 
 import javax.naming.Context;
 import javax.naming.InitialContext;
@@ -58,7 +59,7 @@
    public void testCreateSubcontext() throws Exception
    {
       getLog().debug("+++ testCreateSubcontext");
-      InitialContext ctx = getInitialContext();
+      InitialContext ctx = NamingUtil.getFullInitialContext(getServerHost());
       ctx.createSubcontext("foo");
       try
       {
@@ -103,6 +104,7 @@
          count ++;
       }
       assertTrue("list count > 0 ", count > 0);
+      ctx.close();
    }
 
    public void testNameChanges() throws Exception
@@ -181,13 +183,8 @@
    public void testCreateHaJndiSubcontext() throws Exception
    {
       getLog().debug("+++ testCreateHaJndiSubcontext");
-      // Lookup a name that does not exist
-      java.util.Properties env = new java.util.Properties();
-      env.setProperty(javax.naming.Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
-      env.setProperty(javax.naming.Context.PROVIDER_URL, "jnp://" + getServerHost() + ":1100/");
-      getLog().debug("Creating InitialContext with env="+env);
-
-      InitialContext ctx = new javax.naming.InitialContext(env);
+      // Look a name that does not exist
+      InitialContext ctx = NamingUtil.getFullHAInitialContext(getServerHost());
       Object obj = ctx.lookup("");
       getLog().debug("lookup('') against HA-JNDI succeeded as expected, obj="+obj);
 

Modified: branches/JBPAPP_5_1_2_GA_JBPAPP-7788/testsuite/src/main/org/jboss/test/security/test/LoginModulesUnitTestCase.java
===================================================================
--- branches/JBPAPP_5_1_2_GA_JBPAPP-7788/testsuite/src/main/org/jboss/test/security/test/LoginModulesUnitTestCase.java	2012-02-29 16:18:31 UTC (rev 112717)
+++ branches/JBPAPP_5_1_2_GA_JBPAPP-7788/testsuite/src/main/org/jboss/test/security/test/LoginModulesUnitTestCase.java	2012-03-01 12:15:11 UTC (rev 112718)
@@ -68,6 +68,7 @@
 import org.jboss.security.auth.spi.UsernamePasswordLoginModule;
 import org.jboss.security.plugins.JaasSecurityDomain;
 import org.jboss.test.JBossTestCase;
+import org.jboss.test.NamingUtil;
 
 /** Tests of the LoginModule classes.
 
@@ -984,8 +985,9 @@
    public void testCertLogin() throws Exception
    {
       log.info("testCertLogin");
-      InitialContext ctx = new InitialContext();
+      InitialContext ctx = NamingUtil.getFullInitialContext(getServerHost());
       ctx.rebind("testCertLogin", new TestSecurityDomain());
+      ctx.close();
 
       KeyStore store = KeyStore.getInstance("JKS");
       ClassLoader loader = Thread.currentThread().getContextClassLoader();
@@ -1005,9 +1007,10 @@
    public void testCertRoles() throws Exception
    {
       log.info("testCertRoles");
-      InitialContext ctx = new InitialContext();
+      InitialContext ctx = NamingUtil.getFullInitialContext(getServerHost());
       ctx.rebind("testCertRoles", new TestSecurityDomain());
-
+      ctx.close();
+      
       KeyStore store = KeyStore.getInstance("JKS");
       ClassLoader loader = Thread.currentThread().getContextClassLoader();
       URL resURL = loader.getResource("security/tst.keystore");
@@ -1064,8 +1067,9 @@
       Class.forName("org.hsqldb.jdbcDriver");
       // Create a DataSource binding
       TestDS ds = new TestDS();
-      InitialContext ctx = new InitialContext();
+      InitialContext ctx = NamingUtil.getFullInitialContext(getServerHost());
       ctx.rebind("testJdbc", ds);
+      ctx.close();
 
       // Start database and setup tables
       Connection conn = ds.getConnection("sa", "");



More information about the jboss-cvs-commits mailing list