1   /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
2    *
3    * Licensed under the Apache License, Version 2.0 (the "License");
4    * you may not use this file except in compliance with the License.
5    * You may obtain a copy of the License at
6    *
7    *     http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  
16  package org.acegisecurity.acl;
17  
18  import junit.framework.TestCase;
19  
20  import org.acegisecurity.Authentication;
21  import org.acegisecurity.GrantedAuthority;
22  import org.acegisecurity.GrantedAuthorityImpl;
23  
24  import org.acegisecurity.acl.basic.NamedEntityObjectIdentity;
25  import org.acegisecurity.acl.basic.SimpleAclEntry;
26  
27  import org.acegisecurity.providers.UsernamePasswordAuthenticationToken;
28  
29  import java.util.List;
30  import java.util.Vector;
31  
32  
33  /**
34   * Tests {@link AclProviderManager}.
35   *
36   * @author Ben Alex
37   * @version $Id: AclProviderManagerTests.java 1784 2007-02-24 21:00:24Z luke_t $
38   */
39  public class AclProviderManagerTests extends TestCase {
40      //~ Constructors ===================================================================================================
41  
42      public AclProviderManagerTests() {
43          super();
44      }
45  
46      public AclProviderManagerTests(String arg0) {
47          super(arg0);
48      }
49  
50      //~ Methods ========================================================================================================
51  
52      public static void main(String[] args) {
53          junit.textui.TestRunner.run(AclProviderManagerTests.class);
54      }
55  
56      private AclProviderManager makeProviderManager() {
57          MockProvider provider1 = new MockProvider();
58          List providers = new Vector();
59          providers.add(provider1);
60  
61          AclProviderManager mgr = new AclProviderManager();
62          mgr.setProviders(providers);
63  
64          return mgr;
65      }
66  
67      public final void setUp() throws Exception {
68          super.setUp();
69      }
70  
71      public void testAclLookupFails() {
72          AclProviderManager mgr = makeProviderManager();
73          assertNull(mgr.getAcls(new Integer(5)));
74      }
75  
76      public void testAclLookupForGivenAuthenticationSuccess() {
77          AclProviderManager mgr = makeProviderManager();
78          assertNotNull(mgr.getAcls("STRING", new UsernamePasswordAuthenticationToken("marissa", "not used")));
79      }
80  
81      public void testAclLookupSuccess() {
82          AclProviderManager mgr = makeProviderManager();
83          assertNotNull(mgr.getAcls("STRING"));
84      }
85  
86      public void testRejectsNulls() {
87          AclProviderManager mgr = new AclProviderManager();
88  
89          try {
90              mgr.getAcls(null);
91              fail("Should have thrown IllegalArgumentException");
92          } catch (IllegalArgumentException expected) {
93              assertTrue(true);
94          }
95  
96          try {
97              mgr.getAcls(null, new UsernamePasswordAuthenticationToken("marissa", "not used"));
98              fail("Should have thrown IllegalArgumentException");
99          } catch (IllegalArgumentException expected) {
100             assertTrue(true);
101         }
102 
103         try {
104             mgr.getAcls("SOME_DOMAIN_INSTANCE", null);
105             fail("Should have thrown IllegalArgumentException");
106         } catch (IllegalArgumentException expected) {
107             assertTrue(true);
108         }
109     }
110 
111     public void testReturnsNullIfNoSupportingProvider() {
112         AclProviderManager mgr = makeProviderManager();
113         assertNull(mgr.getAcls(new Integer(4), new UsernamePasswordAuthenticationToken("marissa", "not used")));
114         assertNull(mgr.getAcls(new Integer(4)));
115     }
116 
117     public void testStartupFailsIfProviderListNotContainingProviders()
118         throws Exception {
119         List providers = new Vector();
120         providers.add("THIS_IS_NOT_A_PROVIDER");
121 
122         AclProviderManager mgr = new AclProviderManager();
123 
124         try {
125             mgr.setProviders(providers);
126             fail("Should have thrown IllegalArgumentException");
127         } catch (IllegalArgumentException expected) {
128             assertTrue(true);
129         }
130     }
131 
132     public void testStartupFailsIfProviderListNotSet()
133         throws Exception {
134         AclProviderManager mgr = new AclProviderManager();
135 
136         try {
137             mgr.afterPropertiesSet();
138             fail("Should have thrown IllegalArgumentException");
139         } catch (IllegalArgumentException expected) {
140             assertTrue(true);
141         }
142     }
143 
144     public void testStartupFailsIfProviderListNull() throws Exception {
145         AclProviderManager mgr = new AclProviderManager();
146 
147         try {
148             mgr.setProviders(null);
149             fail("Should have thrown IllegalArgumentException");
150         } catch (IllegalArgumentException expected) {
151             assertTrue(true);
152         }
153     }
154 
155     public void testSuccessfulStartup() throws Exception {
156         AclProviderManager mgr = makeProviderManager();
157         mgr.afterPropertiesSet();
158         assertTrue(true);
159         assertEquals(1, mgr.getProviders().size());
160     }
161 
162     //~ Inner Classes ==================================================================================================
163 
164     private class MockProvider implements AclProvider {
165         private UsernamePasswordAuthenticationToken marissa = new UsernamePasswordAuthenticationToken("marissa",
166                 "not used",
167                 new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_FOO"), new GrantedAuthorityImpl("ROLE_BAR")});
168         private SimpleAclEntry entry100Marissa = new SimpleAclEntry(marissa.getPrincipal(),
169                 new NamedEntityObjectIdentity("OBJECT", "100"), null, 2);
170         private UsernamePasswordAuthenticationToken scott = new UsernamePasswordAuthenticationToken("scott",
171                 "not used",
172                 new GrantedAuthority[] {
173                         new GrantedAuthorityImpl("ROLE_FOO"),
174                         new GrantedAuthorityImpl("ROLE_MANAGER")
175                 });
176         private SimpleAclEntry entry100Scott = new SimpleAclEntry(scott.getPrincipal(),
177                 new NamedEntityObjectIdentity("OBJECT", "100"), null, 4);
178 
179         public AclEntry[] getAcls(Object domainInstance, Authentication authentication) {
180             if (authentication.getPrincipal().equals(scott.getPrincipal())) {
181                 return new AclEntry[] {entry100Scott};
182             }
183 
184             if (authentication.getPrincipal().equals(marissa.getPrincipal())) {
185                 return new AclEntry[] {entry100Marissa};
186             }
187 
188             return null;
189         }
190 
191         public AclEntry[] getAcls(Object domainInstance) {
192             return new AclEntry[] {entry100Marissa, entry100Scott};
193         }
194 
195         /**
196          * Only supports <code>Object</code>s of type <code>String</code>
197          *
198          * @param domainInstance DOCUMENT ME!
199          *
200          * @return DOCUMENT ME!
201          */
202         public boolean supports(Object domainInstance) {
203             return (domainInstance instanceof String);
204         }
205     }
206 }