151dbd7e631479b11d5162bc607c769afa92f4e7
[utils] /
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 }