1   package org.apache.turbine.services.security;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import junit.framework.Test;
23  import junit.framework.TestSuite;
24  
25  import org.apache.turbine.om.security.Group;
26  import org.apache.turbine.om.security.User;
27  import org.apache.turbine.test.BaseTurbineHsqlTest;
28  import org.apache.turbine.util.security.AccessControlList;
29  
30  public class TestSecurityACL
31          extends BaseTurbineHsqlTest
32  {
33      public TestSecurityACL(String name)
34              throws Exception
35      {
36          super(name, "conf/test/TurbineResources.properties");
37      }
38  
39      public static Test suite()
40      {
41          return new TestSuite(TestSecurityACL.class);
42      }
43  
44      public void testInit()
45      {
46          SecurityService ss = TurbineSecurity.getService();
47          assertTrue("Service failed to initialize", ss.getInit());
48      }
49  
50      public void testAcl1()
51      	throws Exception
52      {
53          SecurityService ss = TurbineSecurity.getService();
54  
55          User admin = ss.getUser("admin");
56          assertNotNull(admin);
57  
58          AccessControlList acl = ss.getACL(admin);
59          assertNotNull(acl);
60  
61          assertFalse(acl.hasRole("Admin", "global"));
62          assertTrue(acl.hasRole("Admin", "Turbine"));
63          assertFalse(acl.hasRole("User", "global"));
64          assertFalse(acl.hasRole("User", "Turbine"));
65  
66          assertFalse(acl.hasPermission("Admin", "global"));
67          assertTrue(acl.hasPermission("Admin", "Turbine"));
68          assertFalse(acl.hasPermission("Login", "global"));
69          assertFalse(acl.hasPermission("Login", "Turbine"));
70          assertFalse(acl.hasPermission("Application", "global"));
71          assertFalse(acl.hasPermission("Application", "Turbine"));
72      }
73  
74      public void testAcl2()
75      	throws Exception
76      {
77          SecurityService ss = TurbineSecurity.getService();
78  
79          User admin = ss.getUser("user");
80          assertNotNull(admin);
81  
82          AccessControlList acl = ss.getACL(admin);
83          assertNotNull(acl);
84  
85          assertFalse(acl.hasRole("Admin", "global"));
86          assertFalse(acl.hasRole("Admin", "Turbine"));
87          assertFalse(acl.hasRole("User", "global"));
88          assertTrue(acl.hasRole("User", "Turbine"));
89  
90          assertFalse(acl.hasPermission("Admin", "global"));
91          assertFalse(acl.hasPermission("Admin", "Turbine"));
92          assertFalse(acl.hasPermission("Login", "global"));
93          assertTrue(acl.hasPermission("Login", "Turbine"));
94          assertFalse(acl.hasPermission("Application", "global"));
95          assertTrue(acl.hasPermission("Application", "Turbine"));
96      }
97  
98      public void testAcl3()
99      	throws Exception
100     {
101         SecurityService ss = TurbineSecurity.getService();
102 
103         User user = ss.getUser("user");
104         assertNotNull(user);
105 
106         AccessControlList acl = ss.getACL(user);
107         assertNotNull(acl);
108 
109         Group turbine = ss.getGroupByName("Turbine");
110         assertNotNull(turbine);
111 
112         assertEquals(0, acl.getRoles().size());
113         assertEquals(1, acl.getRoles(turbine).size());
114         assertEquals(0, acl.getPermissions().size());
115         assertEquals(2, acl.getPermissions(turbine).size());
116     }
117 
118 }
119