1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.ldap.server.authz.support;
20
21 import java.util.*;
22
23 import javax.naming.Name;
24 import javax.naming.NamingException;
25 import javax.naming.Context;
26 import javax.naming.NamingEnumeration;
27 import javax.naming.directory.Attributes;
28 import javax.naming.directory.BasicAttributes;
29 import javax.naming.directory.SearchControls;
30
31 import junit.framework.Assert;
32 import junit.framework.TestCase;
33
34 import org.apache.ldap.common.aci.ACITuple;
35 import org.apache.ldap.common.aci.AuthenticationLevel;
36 import org.apache.ldap.common.aci.ProtectedItem;
37 import org.apache.ldap.common.name.LdapName;
38 import org.apache.ldap.common.filter.ExprNode;
39 import org.apache.ldap.server.partition.DirectoryPartitionNexusProxy;
40 import org.apache.ldap.server.DirectoryService;
41 import org.apache.ldap.server.DirectoryServiceListener;
42 import org.apache.ldap.server.DirectoryServiceConfiguration;
43 import org.apache.ldap.server.jndi.DeadContext;
44
45
46 /***
47 * Tests {@link MaxImmSubFilter}.
48 *
49 * @author The Apache Directory Project
50 * @version $Rev: 326050 $, $Date: 2005-10-18 04:19:14 -0400 (Tue, 18 Oct 2005) $
51 */
52 public class MaxImmSubFilterTest extends TestCase
53 {
54 private static final Collection EMPTY_COLLECTION =
55 Collections.unmodifiableCollection( new ArrayList() );
56 private static final Set EMPTY_SET =
57 Collections.unmodifiableSet( new HashSet() );
58
59 private static final Name ROOTDSE_NAME = new LdapName();
60 private static final Name ENTRY_NAME;
61 private static final Collection PROTECTED_ITEMS = new ArrayList();
62 private static final Attributes ENTRY = new BasicAttributes();
63
64
65 static
66 {
67 try
68 {
69 ENTRY_NAME = new LdapName( "ou=test, ou=system" );
70 }
71 catch( NamingException e )
72 {
73 throw new Error();
74 }
75
76 PROTECTED_ITEMS.add( new ProtectedItem.MaxImmSub( 2 ) );
77 }
78
79 public void testWrongScope() throws Exception
80 {
81 MaxImmSubFilter filter = new MaxImmSubFilter();
82 Collection tuples = new ArrayList();
83 tuples.add( new ACITuple(
84 EMPTY_COLLECTION, AuthenticationLevel.NONE, EMPTY_COLLECTION,
85 EMPTY_SET, true, 0 ) );
86
87 tuples = Collections.unmodifiableCollection( tuples );
88
89 Assert.assertEquals(
90 tuples, filter.filter(
91 tuples, OperationScope.ATTRIBUTE_TYPE, null, null, null,
92 null, null, ENTRY_NAME, null, null, ENTRY, null ) );
93
94 Assert.assertEquals(
95 tuples, filter.filter(
96 tuples, OperationScope.ATTRIBUTE_TYPE_AND_VALUE, null,
97 null, null, null, null, ENTRY_NAME, null, null, ENTRY, null ) );
98 }
99
100 public void testRootDSE() throws Exception
101 {
102 MaxImmSubFilter filter = new MaxImmSubFilter();
103
104 Collection tuples = new ArrayList();
105 tuples.add( new ACITuple(
106 EMPTY_COLLECTION, AuthenticationLevel.NONE, EMPTY_COLLECTION,
107 EMPTY_SET, true, 0 ) );
108
109 tuples = Collections.unmodifiableCollection( tuples );
110
111 Assert.assertEquals(
112 tuples, filter.filter(
113 tuples, OperationScope.ENTRY,
114 null, null, null, null, null, ROOTDSE_NAME, null, null, ENTRY, null ) );
115 }
116
117 public void testZeroTuple() throws Exception
118 {
119 MaxImmSubFilter filter = new MaxImmSubFilter();
120
121 Assert.assertEquals(
122 0, filter.filter(
123 EMPTY_COLLECTION, OperationScope.ENTRY,
124 null, null, null, null, null, ENTRY_NAME, null, null, ENTRY, null ).size() );
125 }
126
127 public void testDenialTuple() throws Exception
128 {
129 MaxImmSubFilter filter = new MaxImmSubFilter();
130 Collection tuples = new ArrayList();
131 tuples.add( new ACITuple(
132 EMPTY_COLLECTION, AuthenticationLevel.NONE, PROTECTED_ITEMS,
133 EMPTY_SET, false, 0 ) );
134
135 tuples = Collections.unmodifiableCollection( tuples );
136
137 Assert.assertEquals(
138 tuples, filter.filter(
139 tuples, OperationScope.ENTRY, null, null, null,
140 null, null, ENTRY_NAME, null, null, ENTRY, null ) );
141 }
142
143
144 public void testGrantTuple() throws Exception
145 {
146 MaxImmSubFilter filter = new MaxImmSubFilter();
147 Collection tuples = new ArrayList();
148 tuples.add( new ACITuple(
149 EMPTY_COLLECTION, AuthenticationLevel.NONE, PROTECTED_ITEMS,
150 EMPTY_SET, true, 0 ) );
151
152 Assert.assertEquals(
153 1, filter.filter(
154 tuples, OperationScope.ENTRY, new MockProxy(1), null, null,
155 null, null, ENTRY_NAME, null, null, ENTRY, null ).size() );
156
157 Assert.assertEquals(
158 0, filter.filter(
159 tuples, OperationScope.ENTRY, new MockProxy(3), null, null,
160 null, null, ENTRY_NAME, null, null, ENTRY, null ).size() );
161 }
162
163
164 class MockProxy extends DirectoryPartitionNexusProxy
165 {
166 final int count;
167
168 public MockProxy( int count )
169 {
170 super( new DeadContext(), new MockDirectoryService() );
171 this.count = count;
172 }
173
174
175 public NamingEnumeration search( Name base, Map env, ExprNode filter, SearchControls searchCtls ) throws NamingException
176 {
177 return new BogusEnumeration( count );
178 }
179
180
181 public NamingEnumeration search( Name base, Map env, ExprNode filter, SearchControls searchCtls, Collection bypass ) throws NamingException
182 {
183 return new BogusEnumeration( count );
184 }
185 }
186
187 class MockDirectoryService extends DirectoryService
188 {
189 public void startup( DirectoryServiceListener listener, Hashtable environment ) throws NamingException
190 {
191
192 }
193
194
195 public void shutdown() throws NamingException
196 {
197
198 }
199
200
201 public void sync() throws NamingException
202 {
203
204 }
205
206
207 public boolean isStarted()
208 {
209 return true;
210 }
211
212
213 public DirectoryServiceConfiguration getConfiguration()
214 {
215 return null;
216 }
217
218
219 public Context getJndiContext( String baseName ) throws NamingException
220 {
221 return null;
222 }
223
224
225 public Context getJndiContext( String principal, byte[] credential, String authentication, String baseName ) throws NamingException
226 {
227 return null;
228 }
229 }
230
231
232 class BogusEnumeration implements NamingEnumeration
233 {
234 final int count;
235 int ii;
236
237
238 public BogusEnumeration( int count )
239 {
240 this.count = count;
241 }
242
243
244 public Object next() throws NamingException
245 {
246 if ( ii >= count )
247 {
248 throw new NoSuchElementException();
249 }
250
251 ii++;
252 return new Object();
253 }
254
255
256 public boolean hasMore() throws NamingException
257 {
258 return ii < count;
259 }
260
261
262 public void close() throws NamingException
263 {
264 ii = count;
265 }
266
267
268 public boolean hasMoreElements()
269 {
270 return ii < count;
271 }
272
273
274 public Object nextElement()
275 {
276 if ( ii >= count )
277 {
278 throw new NoSuchElementException();
279 }
280
281 ii++;
282 return new Object();
283 }
284 }
285 }