5ec36f7ec43b273e0d980a682125d10d4c67b57e
[utils] / security / src / test / java / org / wamblee / security / authorization / AuthorizationServiceTest.java
1 /*
2  * Copyright 2005 the original author or authors.
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */ 
16
17 package org.wamblee.security.authorization;
18
19 import static org.wamblee.security.authorization.AuthorizationResult.DENIED;
20 import static org.wamblee.security.authorization.AuthorizationResult.GRANTED;
21 import junit.framework.TestCase;
22
23 import org.wamblee.usermgt.UserAccessor;
24
25 /**
26  * Tests the authorization service. 
27  *
28  * @author Erik Brakkee
29  */
30 public class AuthorizationServiceTest extends TestCase {
31     
32     private AuthorizationRule rule1; 
33     private AuthorizationRule rule2;  
34     private AuthorizationRule rule3; 
35     private AuthorizationService service; 
36     
37     protected AuthorizationService getService() { 
38         return service; 
39     }
40     
41     /* (non-Javadoc)
42      * @see junit.framework.TestCase#setUp()
43      */
44     @Override
45     protected void setUp() throws Exception {
46         super.setUp();
47         
48         rule1 = createRule(GRANTED, "users", "/oni/", AllOperation.class); 
49         rule2 = createRule(DENIED, "users", "/abc/", ReadOperation.class); 
50         rule3 = createRule(GRANTED, "users", "/abc/", AllOperation.class);
51         
52         service = createService(); 
53         service.appendRule(rule1); 
54         service.appendRule(rule2);
55         service.appendRule(rule3);
56     }
57     
58     protected void resetTestRules() { 
59         ((TestAuthorizationRule)rule1).reset(); 
60         ((TestAuthorizationRule)rule2).reset(); 
61         ((TestAuthorizationRule)rule3).reset(); 
62     }
63     
64     protected UserAccessor createUserAccessor() { 
65         return new TestUserAccessor(); 
66     }
67
68     /**
69      * Creates an authorization service with some rules for testing. . 
70      * @return Authorization service. 
71      */
72     protected AuthorizationService createService() {
73         DefaultAuthorizationService service = new DefaultAuthorizationService() ;
74         service.setUserAccessor(createUserAccessor());
75         return service;
76     }
77     
78     protected AuthorizationRule createRule(AuthorizationResult aResult, String aGroup, String aPath, Class<? extends Operation> aOperation) { 
79         return new TestAuthorizationRule(aResult, aGroup, aPath, aOperation);
80     }
81     
82     protected void checkMatchCount(int aCount, AuthorizationRule aRule) { 
83         assertEquals( aCount,  ((TestAuthorizationRule)aRule).getMatchCount()); 
84     }
85     
86     protected Object createResource(String aPath) { 
87         return new TestResource(aPath);
88     }
89     
90     protected void checkRuleCount(int aCount) { 
91         // Empty
92     }
93     
94     /**
95      * Several checks to verify the outcome of matching against the first rule. 
96      *
97      */
98     public void testFirstRuleGrants() { 
99         assertTrue( service.isAllowed(createResource("/oni/xyz.jpg"), new ReadOperation())); 
100         checkMatchCount(1, rule1);
101         assertTrue(service.isAllowed(createResource("/oni/xyz.jpg"), new WriteOperation())); 
102         checkMatchCount(2, rule1);
103         assertTrue(service.isAllowed(createResource("/oni/xyz.jpg"), new DeleteOperation())); 
104         checkMatchCount(3, rule1);
105         assertTrue(service.isAllowed(createResource("/oni/xyz.jpg"), new CreateOperation())); 
106         checkMatchCount(4, rule1);
107         checkMatchCount(0, rule2);
108         checkMatchCount(0, rule3);
109     }
110     
111     /**
112      * Verify that a match with the second rule leads to a denial of authorization. 
113      *
114      */
115     public void testSecondRuleDenies() {
116         assertFalse(service.isAllowed(createResource("/abc/xyz.jpg"), new ReadOperation())); 
117         checkMatchCount(0, rule1);
118         checkMatchCount(1, rule2);
119         checkMatchCount(0, rule3);
120     }
121     
122     /**
123      * Verifies that the third rule is used when appropriate and that it grants access. 
124      *
125      */
126     public void testThirdRuleGrants() { 
127         assertTrue(service.isAllowed(createResource("/abc/xyz.jpg"), new WriteOperation())); 
128         checkMatchCount(0, rule1); 
129         checkMatchCount(0, rule2);
130         checkMatchCount(1, rule3); 
131     }
132     
133     /**
134      * Removes a rule and checks it is removed. 
135      *
136      */
137     public void testRemoveRule() { 
138         checkRuleCount(3);
139         assertTrue(service.isAllowed(createResource("/abc/xyz.jpg"), new WriteOperation())); 
140         service.removeRule(2); 
141         assertFalse(service.isAllowed(createResource("/abc/xyz.jpg"), new WriteOperation()));
142         checkRuleCount(2);
143     }
144     
145     /**
146      * Inserts a rule and checks it is inserted. 
147      *
148      */
149     public void testInsertRule() {
150         checkRuleCount(3);
151         assertFalse(service.isAllowed(createResource("/janse/xyz.jpg"), new WriteOperation()));     
152         service.appendRule(createRule(GRANTED, "users", "/janse/", WriteOperation.class));
153         assertTrue(service.isAllowed(createResource("/janse/xyz.jpg"), new WriteOperation()));
154         checkRuleCount(4);
155             
156     }
157    
158     /**
159      * Gets the rules. Verifies that all rules are obtained. 
160      *
161      */
162     public void testGetRules() { 
163         AuthorizationRule[] rules = service.getRules();
164         assertEquals(3, rules.length); 
165     }
166     
167     /**
168      * Verifies that when no rules match, access is denied. 
169      *
170      */
171     public void testNoRulesSupportResource() { 
172         assertFalse(service.isAllowed(createResource("/xyxyxyxy"), new ReadOperation()));
173         checkMatchCount(0, rule1); 
174         checkMatchCount(0, rule2);
175         checkMatchCount(0, rule3); 
176     }
177 }