forked from wildfly/wildfly
/
AsynchronousSecurityTestCase.java
187 lines (164 loc) · 7.04 KB
/
AsynchronousSecurityTestCase.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
* JBoss, Home of Professional Open Source.
* Copyright 2012, Red Hat, Inc., and individual contributors
* as indicated by the @author tags. See the copyright.txt file 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.as.test.integration.ejb.security.asynchronous;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import javax.ejb.EJBAccessException;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.security.auth.login.LoginContext;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.arquillian.test.api.ArquillianResource;
import org.jboss.as.arquillian.api.ServerSetup;
import org.jboss.as.arquillian.api.ServerSetupTask;
import org.jboss.as.test.integration.ejb.security.AnnotationAuthorizationTestCase;
import org.jboss.as.test.integration.ejb.security.EjbSecurityDomainSetup;
import org.jboss.as.test.integration.security.common.AbstractSecurityDomainSetup;
import org.jboss.as.test.shared.integration.ejb.security.Util;
import org.jboss.logging.Logger;
import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.StringAsset;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
/**
* Migration test from EJB Testsuite (asynchronous) to AS7 [JIRA JBQA-5483].
*
* Test if asynchronous calls and security check works.
*/
@RunWith(Arquillian.class)
@ServerSetup({AsynchronousSecurityTestCase.AsynchronousSecurityTestCaseSetup.class})
public class AsynchronousSecurityTestCase {
private static final Logger log = Logger.getLogger(AsynchronousSecurityTestCase.class);
private static final String ARCHIVE_NAME = "AsyncSecurityTestCase";
static class AsynchronousSecurityTestCaseSetup extends EjbSecurityDomainSetup {
@Override
protected String getSecurityDomainName() {
return "async-security-test";
}
}
@ArquillianResource
private InitialContext iniCtx;
@Deployment
public static Archive<?> deploy() {
JavaArchive jar = ShrinkWrap.create(JavaArchive.class, ARCHIVE_NAME + ".jar");
jar.addPackage(AsynchronousSecurityTestCase.class.getPackage());
jar.addClass(AnnotationAuthorizationTestCase.class);
jar.addClasses(EjbSecurityDomainSetup.class, AbstractSecurityDomainSetup.class, ServerSetupTask.class, Util.class);
jar.addAsResource(AsynchronousSecurityTestCase.class.getPackage(), "roles.properties", "roles.properties");
jar.addAsResource(AsynchronousSecurityTestCase.class.getPackage(), "users.properties", "users.properties");
jar.addAsManifestResource(new StringAsset("Dependencies: org.jboss.as.controller-client,org.jboss.dmr\n"),"MANIFEST.MF");
log.info(jar.toString(true));
return jar;
}
protected <Q, T> Q lookupInterface(Class<T> bean, Class<Q> intf) throws NamingException {
log.info("initctx: " + iniCtx);
return intf.cast(iniCtx.lookup("java:global/" + ARCHIVE_NAME + "/" + bean.getSimpleName() + "!"
+ intf.getName()));
}
@Test
public void testAsynchSecurityMethod() throws Exception {
SecuredStatelessRemote securedBean = lookupInterface(SecuredStatelessBean.class, SecuredStatelessRemote.class);
boolean result = false;
// Test 1
SecuredStatelessBean.reset();
LoginContext lc = Util.getCLMLoginContext("somebody", "password");
lc.login();
try {
result = securedBean.method();
/* SecuredStatelessBean.startLatch.countDown();
result = future.get(); */
} finally {
lc.logout();
}
Assert.assertTrue(result);
// Test 2
SecuredStatelessBean.reset();
result = false;
lc = Util.getCLMLoginContext("rolefail", "password");
lc.login();
try {
result = securedBean.method();
/* SecuredStatelessBean.startLatch.countDown();
result = future.get(); */
} catch (ExecutionException ee) {
if(!(ee.getCause() instanceof EJBAccessException)) {
Assert.fail("Exception cause was not EJBAccessException and was " + ee);
}
} catch (EJBAccessException ejbe) {
// it's ok too
} finally {
lc.logout();
}
Assert.assertFalse(result);
// Test 3
SecuredStatelessBean.reset();
result = false;
lc = Util.getCLMLoginContext("nosuchuser", "password");
lc.login();
try {
result = securedBean.method();
/* SecuredStatelessBean.startLatch.countDown();
result = future.get(); */
} catch (ExecutionException ee) {
if(!(ee.getCause() instanceof EJBAccessException)) {
Assert.fail("Exception cause was not EJBAccessException and was " + ee);
}
} catch (EJBAccessException ejbe) {
// it's ok too
} finally {
lc.logout();
}
Assert.assertFalse(result);
}
@Test
public void testAsyncSecurityPermition() throws Exception {
SecuredStatelessBean.reset();
SecuredStatelessRemote securedBean = lookupInterface(SecuredStatelessBean.class, SecuredStatelessRemote.class);
LoginContext lc = Util.getCLMLoginContext("rolefail", "password");
lc.login();
// Test 1
try {
Boolean result = securedBean.uncheckedMethod();
/*SecuredStatelessBean.startLatch.countDown();
boolean result = future.get(); */
Assert.assertTrue(result);
// Test 2
// future = null;
result = false;
SecuredStatelessBean.reset();
try {
result = securedBean.excludedMethod();
/*SecuredStatelessBean.startLatch.countDown();
result = future.get(); */
} catch (Exception ee) {
// this is ok
}
Assert.assertFalse(result);
} finally {
lc.logout();
}
}
}