1 | /** |
---|
2 | * Licensed to the Apache Software Foundation (ASF) under one |
---|
3 | * or more contributor license agreements. See the NOTICE file |
---|
4 | * distributed with this work for additional information |
---|
5 | * regarding copyright ownership. The ASF licenses this file |
---|
6 | * to you under the Apache License, Version 2.0 (the |
---|
7 | * "License"); you may not use this file except in compliance |
---|
8 | * with the License. You may obtain a copy of the License at |
---|
9 | * |
---|
10 | * http://www.apache.org/licenses/LICENSE-2.0 |
---|
11 | * |
---|
12 | * Unless required by applicable law or agreed to in writing, software |
---|
13 | * distributed under the License is distributed on an "AS IS" BASIS, |
---|
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
15 | * See the License for the specific language governing permissions and |
---|
16 | * limitations under the License. |
---|
17 | */ |
---|
18 | package org.apache.hadoop.security; |
---|
19 | |
---|
20 | import java.util.Iterator; |
---|
21 | import java.util.Set; |
---|
22 | |
---|
23 | import org.apache.hadoop.security.SecurityUtil.AccessControlList; |
---|
24 | |
---|
25 | import junit.framework.TestCase; |
---|
26 | |
---|
27 | public class TestAccessControlList extends TestCase { |
---|
28 | |
---|
29 | public void testWildCardAccessControlList() throws Exception { |
---|
30 | AccessControlList acl; |
---|
31 | |
---|
32 | acl = new AccessControlList("*"); |
---|
33 | assertTrue(acl.allAllowed()); |
---|
34 | |
---|
35 | acl = new AccessControlList(" * "); |
---|
36 | assertTrue(acl.allAllowed()); |
---|
37 | |
---|
38 | acl = new AccessControlList(" *"); |
---|
39 | assertTrue(acl.allAllowed()); |
---|
40 | |
---|
41 | acl = new AccessControlList("* "); |
---|
42 | assertTrue(acl.allAllowed()); |
---|
43 | } |
---|
44 | |
---|
45 | public void testAccessControlList() throws Exception { |
---|
46 | AccessControlList acl; |
---|
47 | Set<String> users; |
---|
48 | Set<String> groups; |
---|
49 | |
---|
50 | acl = new AccessControlList("drwho tardis"); |
---|
51 | users = acl.getUsers(); |
---|
52 | assertEquals(users.size(), 1); |
---|
53 | assertEquals(users.iterator().next(), "drwho"); |
---|
54 | groups = acl.getGroups(); |
---|
55 | assertEquals(groups.size(), 1); |
---|
56 | assertEquals(groups.iterator().next(), "tardis"); |
---|
57 | |
---|
58 | acl = new AccessControlList("drwho"); |
---|
59 | users = acl.getUsers(); |
---|
60 | assertEquals(users.size(), 1); |
---|
61 | assertEquals(users.iterator().next(), "drwho"); |
---|
62 | groups = acl.getGroups(); |
---|
63 | assertEquals(groups.size(), 0); |
---|
64 | |
---|
65 | acl = new AccessControlList("drwho "); |
---|
66 | users = acl.getUsers(); |
---|
67 | assertEquals(users.size(), 1); |
---|
68 | assertEquals(users.iterator().next(), "drwho"); |
---|
69 | groups = acl.getGroups(); |
---|
70 | assertEquals(groups.size(), 0); |
---|
71 | |
---|
72 | acl = new AccessControlList(" tardis"); |
---|
73 | users = acl.getUsers(); |
---|
74 | assertEquals(users.size(), 0); |
---|
75 | groups = acl.getGroups(); |
---|
76 | assertEquals(groups.size(), 1); |
---|
77 | assertEquals(groups.iterator().next(), "tardis"); |
---|
78 | |
---|
79 | Iterator<String> iter; |
---|
80 | acl = new AccessControlList("drwho,joe tardis,users"); |
---|
81 | users = acl.getUsers(); |
---|
82 | assertEquals(users.size(), 2); |
---|
83 | iter = users.iterator(); |
---|
84 | assertEquals(iter.next(), "drwho"); |
---|
85 | assertEquals(iter.next(), "joe"); |
---|
86 | groups = acl.getGroups(); |
---|
87 | assertEquals(groups.size(), 2); |
---|
88 | iter = groups.iterator(); |
---|
89 | assertEquals(iter.next(), "tardis"); |
---|
90 | assertEquals(iter.next(), "users"); |
---|
91 | |
---|
92 | acl = new AccessControlList("drwho,joe tardis, users"); |
---|
93 | users = acl.getUsers(); |
---|
94 | assertEquals(users.size(), 2); |
---|
95 | iter = users.iterator(); |
---|
96 | assertEquals(iter.next(), "drwho"); |
---|
97 | assertEquals(iter.next(), "joe"); |
---|
98 | groups = acl.getGroups(); |
---|
99 | assertEquals(groups.size(), 2); |
---|
100 | iter = groups.iterator(); |
---|
101 | assertEquals(iter.next(), "tardis"); |
---|
102 | assertEquals(iter.next(), "users"); |
---|
103 | } |
---|
104 | } |
---|