PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/jboss-as-7.1.1.Final/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/security/loginmodules/IdentityLoginModuleTestCase.java

#
Java | 199 lines | 124 code | 33 blank | 42 comment | 0 complexity | 10c64fdfd73455c1df52cecff7390605 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0
  1. /*
  2. * JBoss, Home of Professional Open Source.
  3. * Copyright (c) 2011, Red Hat, Inc., and individual contributors
  4. * as indicated by the @author tags. See the copyright.txt file in the
  5. * distribution for a full listing of individual contributors.
  6. *
  7. * This is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU Lesser General Public License as
  9. * published by the Free Software Foundation; either version 2.1 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This software is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this software; if not, write to the Free
  19. * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  21. */
  22. package org.jboss.as.test.integration.security.loginmodules;
  23. import java.io.IOException;
  24. import java.net.URL;
  25. import java.util.HashMap;
  26. import java.util.Map;
  27. import org.apache.http.HttpResponse;
  28. import org.apache.http.client.methods.HttpGet;
  29. import org.apache.http.impl.client.DefaultHttpClient;
  30. import org.jboss.arquillian.container.test.api.Deployment;
  31. import org.jboss.arquillian.container.test.api.OperateOnDeployment;
  32. import org.jboss.arquillian.container.test.api.RunAsClient;
  33. import org.jboss.arquillian.junit.Arquillian;
  34. import org.jboss.arquillian.test.api.ArquillianResource;
  35. import org.jboss.as.arquillian.api.ServerSetup;
  36. import org.jboss.as.arquillian.container.ManagementClient;
  37. import org.jboss.as.test.integration.security.common.AbstractSecurityDomainSetup;
  38. import org.jboss.as.test.integration.security.common.Utils;
  39. import org.jboss.as.test.integration.security.loginmodules.common.servlets.PrincipalPrintingServlet;
  40. import org.jboss.logging.Logger;
  41. import org.jboss.security.auth.spi.IdentityLoginModule;
  42. import org.jboss.shrinkwrap.api.ShrinkWrap;
  43. import org.jboss.shrinkwrap.api.spec.WebArchive;
  44. import org.junit.Test;
  45. import org.junit.runner.RunWith;
  46. import static org.junit.Assert.assertTrue;
  47. /**
  48. * Tests of login via IdentityLoginModule
  49. *
  50. * @author <a href="mailto:jlanik@redhat.com">Jan Lanik</a>.
  51. */
  52. @RunWith(Arquillian.class)
  53. @RunAsClient
  54. @ServerSetup({IdentityLoginModuleTestCase.SecurityDomain1Setup.class, IdentityLoginModuleTestCase.SecurityDomain2Setup.class})
  55. public class IdentityLoginModuleTestCase {
  56. private static Logger log = Logger.getLogger(IdentityLoginModuleTestCase.class);
  57. private static final String DEP1 = "IdentityLoginModule-defaultPrincipal";
  58. static class SecurityDomain1Setup extends AbstractSecurityDomainSetup {
  59. @Override
  60. protected String getSecurityDomainName() {
  61. return "TestIdentityLoginDomain";
  62. }
  63. @Override
  64. public void setup(final ManagementClient managementClient, final String containerId) throws Exception {
  65. log.debug("adding module options");
  66. Map<String,String> moduleOptionsMap = new HashMap<String,String>();
  67. moduleOptionsMap.put("roles", "role1,role2");
  68. log.info("creating security domain: TestIdentityLoginDomain");
  69. createSecurityDomain(IdentityLoginModule.class, moduleOptionsMap, managementClient.getControllerClient());
  70. log.info("security domain created");
  71. }
  72. }
  73. static class SecurityDomain2Setup extends AbstractSecurityDomainSetup {
  74. @Override
  75. protected String getSecurityDomainName() {
  76. return "TestIdentityLoginDomain2";
  77. }
  78. @Override
  79. public void setup(final ManagementClient managementClient, final String containerId) throws Exception {
  80. log.debug("adding module options");
  81. Map<String,String> moduleOptionsMap = new HashMap<String,String>();
  82. moduleOptionsMap.put("roles", "role1,role2");
  83. moduleOptionsMap.put("principal", "SomeName");
  84. log.info("creating security domain: TestIdentityLoginDomain");
  85. createSecurityDomain(IdentityLoginModule.class, moduleOptionsMap, managementClient.getControllerClient());
  86. log.info("security domain created");
  87. }
  88. }
  89. /**
  90. * Test deployment with
  91. * <module-option name="roles" value="role1,role2"/>
  92. */
  93. @Deployment(name = DEP1, order = 1)
  94. public static WebArchive appDeployment1() {
  95. log.info("start" + DEP1 + "deployment");
  96. WebArchive war = ShrinkWrap.create(WebArchive.class, DEP1 + ".war");
  97. war.addClass(PrincipalPrintingServlet.class);
  98. war.setWebXML(Utils.getResource("loginmodules/deployments/IdentityLoginModule/web.xml"));
  99. war.addAsWebInfResource(Utils.getResource("loginmodules/deployments/IdentityLoginModule/dep1/jboss-web.xml"),"jboss-web.xml");
  100. log.debug(war.toString(true));
  101. return war;
  102. }
  103. private static final String DEP2 = "IdentityLoginModule-customPrincipal";
  104. /**
  105. * Test deployment with
  106. * <module-option name="prinipal" value="SomeName"/>
  107. * <module-option name="roles" value="role1,role2"/>
  108. */
  109. @Deployment(name = DEP2, order = 2)
  110. public static WebArchive appDeployment2() {
  111. log.info("start" + DEP2 + "deployment");
  112. WebArchive war = ShrinkWrap.create(WebArchive.class, DEP2 + ".war");
  113. war.addClass(PrincipalPrintingServlet.class);
  114. war.setWebXML(Utils.getResource("loginmodules/deployments/IdentityLoginModule/web.xml"));
  115. war.addAsWebInfResource(Utils.getResource("loginmodules/deployments/IdentityLoginModule/dep2/jboss-web.xml"), "jboss-web.xml");
  116. log.debug(war.toString(true));
  117. return war;
  118. }
  119. @OperateOnDeployment(DEP1)
  120. @ArquillianResource
  121. URL URL1;
  122. /**
  123. * Tests assignment of default principal name to the caller
  124. */
  125. @OperateOnDeployment(DEP1)
  126. @Test
  127. public void testDefaultPrincipal(){
  128. DefaultHttpClient httpclient = new DefaultHttpClient();
  129. HttpResponse response;
  130. HttpGet httpget = new HttpGet(URL1.toString());
  131. httpget.addHeader("Authorization", "Basic Yzpj"); //I'm not sure why this have to be here, however it does not work without it
  132. String text;
  133. try {
  134. response = httpclient.execute(httpget);
  135. text = Utils.getContent(response);
  136. } catch (IOException e) {
  137. throw new RuntimeException("Servlet response IO exception", e);
  138. }
  139. assertTrue("default principal ('guest') not assigned to the request by IdentityLoinModule: returned text = " +
  140. text, text.contains("guest"));
  141. }
  142. @OperateOnDeployment(DEP2)
  143. @ArquillianResource
  144. URL URL2;
  145. /**
  146. * Tests assignment of custom principal name to the caller
  147. */
  148. @OperateOnDeployment(DEP2)
  149. @Test
  150. public void testCustomPrincipal(){
  151. DefaultHttpClient httpclient = new DefaultHttpClient();
  152. HttpResponse response;
  153. //HttpGet httpget = new HttpGet("http://localhost:8080/" + DEP2 + "/");
  154. HttpGet httpget = new HttpGet(URL2.toString());
  155. httpget.addHeader("Authorization", "Basic Yzpj");//I'm not sure why this have to be here, however it does not work without it
  156. String text;
  157. try {
  158. response = httpclient.execute(httpget);
  159. text = Utils.getContent(response);
  160. } catch (IOException e) {
  161. throw new RuntimeException("Servlet response IO exception", e);
  162. }
  163. assertTrue("default principal ('guest') not assigned to the request by IdentityLoinModule: returned text = " +
  164. text, text.contains("SomeName"));
  165. }
  166. }