1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.shiro.realm.text;
20
21 import org.apache.shiro.authz.AuthorizationException;
22 import org.apache.shiro.subject.PrincipalCollection;
23 import org.apache.shiro.subject.SimplePrincipalCollection;
24 import org.junit.Test;
25
26 import java.text.ParseException;
27 import java.util.Arrays;
28
29 import static org.junit.Assert.*;
30
31 public class TextConfigurationRealmTest {
32
33 private TestRealm realm;
34
35 private void setRoles() {
36 StringBuilder roleDefinitions = new StringBuilder()
37 .append("role1 = role1_permission1\n")
38 .append("role2 = role2_persission1, role2_permission2\n");
39 realm.setRoleDefinitions(roleDefinitions.toString());
40 }
41
42 private void setUsers() {
43 StringBuilder userDefinitions = new StringBuilder();
44 for (int i = 1; i < 3; i++) {
45 userDefinitions.append(String.format("user%1$d = user%1$d_password, role1, role2%n", i));
46 }
47 realm.setUserDefinitions(userDefinitions.toString());
48 }
49
50 private void setUpForReadConfigurationTest() {
51 realm = new TestRealm() {
52
53
54
55
56 public void test(Thread runnable) throws InterruptedException {
57
58 USERS_LOCK.writeLock().lock();
59 try {
60 ROLES_LOCK.writeLock().lock();
61 try {
62
63 runnable.start();
64 Thread.sleep(500);
65
66 realm.onInit();
67
68 } finally {
69 ROLES_LOCK.writeLock().unlock();
70 }
71 } finally {
72 USERS_LOCK.writeLock().unlock();
73 }
74 }
75 };
76 setRoles();
77 setUsers();
78 }
79
80
81
82
83 private void executeTest(Runnable runnable) throws InterruptedException {
84 TestThread testThread = new TestThread(runnable);
85 Thread testTask = new Thread(testThread);
86 realm.test(testTask);
87 testTask.join(500);
88
89 testThread.test();
90 }
91
92
93
94
95 @Test
96 public void testRoleAndUserAccount() throws InterruptedException {
97 setUpForReadConfigurationTest();
98 executeTest(new Runnable() {
99 public void run() {
100 assertTrue("role not found when it was expected", realm.roleExists("role1"));
101 assertTrue("user not found when it was expected", realm.accountExists("user1"));
102 }
103 });
104 }
105
106
107
108
109 @Test
110 public void testHasRole() throws InterruptedException {
111 setUpForReadConfigurationTest();
112 executeTest(new Runnable() {
113 public void run() {
114 PrincipalCollection principalCollection = new SimplePrincipalCollection("user1", "realm1");
115 assertTrue("principal doesn't have role when it should",
116 realm.hasRole(principalCollection, "role2"));
117 assertTrue("principal doesn't have all roles when it should",
118 realm.hasAllRoles(principalCollection, Arrays.asList(new String[]{"role1", "role2"})));
119 }
120 });
121 }
122
123
124
125
126 @Test
127 public void testCheckRole() throws InterruptedException {
128 setUpForReadConfigurationTest();
129 executeTest(new Runnable() {
130 public void run() {
131 PrincipalCollection principalCollection = new SimplePrincipalCollection("user1", "realm1");
132 try {
133 realm.checkRoles(principalCollection, new String[]{"role1", "role2"});
134 } catch (AuthorizationException ae) {
135 fail("principal doesn't have all roles when it should");
136 }
137 }
138 });
139 }
140
141
142
143
144 @Test
145 public void testCheckPermission() throws InterruptedException {
146 setUpForReadConfigurationTest();
147 executeTest(new Runnable() {
148 public void run() {
149 PrincipalCollection principalCollection = new SimplePrincipalCollection("user1", "realm1");
150 try {
151 realm.checkPermission(principalCollection, "role1_permission1");
152 realm.checkPermissions(principalCollection, new String[]{"role1_permission1", "role2_permission2"});
153 } catch (AuthorizationException ae) {
154 fail("principal doesn't have permission when it should");
155 }
156 }
157 });
158 }
159
160
161
162
163 @Test
164 public void testIsPermitted() throws InterruptedException {
165 setUpForReadConfigurationTest();
166 executeTest(new Runnable() {
167 public void run() {
168 PrincipalCollection principalCollection = new SimplePrincipalCollection("user1", "realm1");
169 assertTrue("permission not permitted when it should be", realm.isPermitted(principalCollection, "role1_permission1"));
170 assertTrue("permission not permitted when it should be",
171 realm.isPermittedAll(principalCollection, new String[]{"role1_permission1", "role2_permission2"}));
172 }
173 });
174 }
175
176
177
178
179 @Test
180 public void testProcessRoleDefinitions() throws InterruptedException {
181 realm = new TestRealm() {
182 public void test(Thread runnable) throws InterruptedException {
183
184
185 ROLES_LOCK.writeLock().lock();
186 try {
187 runnable.start();
188 Thread.sleep(500);
189
190 assertFalse("role exists when it shouldn't", realm.roleExists("role1"));
191 } finally {
192 ROLES_LOCK.writeLock().unlock();
193 }
194 }
195 };
196
197 TestThread testThread = new TestThread(new Runnable() {
198 public void run() {
199 try {
200 realm.processRoleDefinitions();
201 } catch (ParseException e) {
202 fail("Unable to parse role definitions");
203 }
204 }
205 });
206 setRoles();
207 Thread testTask = new Thread(testThread);
208 realm.test(testTask);
209 testTask.join(500);
210 assertTrue("role doesn't exist when it should", realm.roleExists("role1"));
211 testThread.test();
212 }
213
214
215
216
217 @Test
218 public void testProcessUserDefinitions() throws InterruptedException {
219 realm = new TestRealm() {
220 public void test(Thread runnable) throws InterruptedException {
221
222
223 USERS_LOCK.writeLock().lock();
224 try {
225 runnable.start();
226 Thread.sleep(500);
227
228 assertFalse("account exists when it shouldn't", realm.accountExists("user1"));
229 } finally {
230 USERS_LOCK.writeLock().unlock();
231 }
232 }
233 };
234 TestThread testThread = new TestThread(new Runnable() {
235 public void run() {
236 try {
237 realm.processUserDefinitions();
238 } catch (ParseException e) {
239 fail("Unable to parse user definitions");
240 }
241 }
242 });
243 setUsers();
244 Thread testTask = new Thread(testThread);
245 realm.test(testTask);
246 testTask.join(500);
247 assertTrue("account doesn't exist when it should", realm.accountExists("user1"));
248 testThread.test();
249 }
250
251
252
253
254 private class TestThread implements Runnable {
255 private Runnable test;
256 private volatile AssertionError ae;
257
258 public TestThread(Runnable test) {
259 this.test = test;
260 }
261
262 public void run() {
263 try {
264 test.run();
265 } catch (AssertionError ae) {
266 this.ae = ae;
267 }
268 }
269
270 public void test() {
271 if (ae != null)
272 throw ae;
273 }
274 }
275
276
277
278
279 private abstract class TestRealm extends TextConfigurationRealm {
280 abstract public void test(Thread runnable) throws InterruptedException;
281 }
282 }