1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.ldap.server.jndi;
18
19
20 import junit.framework.TestCase;
21 import org.apache.commons.io.FileUtils;
22 import org.apache.ldap.common.exception.LdapNoPermissionException;
23 import org.apache.mina.util.AvailablePortFinder;
24
25 import javax.naming.Context;
26 import javax.naming.InitialContext;
27 import javax.naming.NamingException;
28 import javax.naming.directory.Attributes;
29 import javax.naming.directory.DirContext;
30 import javax.naming.directory.ModificationItem;
31 import java.io.File;
32 import java.io.IOException;
33 import java.util.Hashtable;
34
35
36 /***
37 * Testing RootDSE lookups and context creation using the empty string.
38 *
39 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
40 * @version $Rev: 165254 $
41 */
42 public class RootDSETest extends TestCase
43 {
44 /*** flag whether to delete database files for each test or not */
45 protected boolean doDelete = true;
46
47
48 /***
49 * Get's the initial context factory for the provider's ou=system context
50 * root.
51 *
52 * @see junit.framework.TestCase#setUp()
53 */
54 protected void setUp() throws Exception
55 {
56 super.setUp();
57
58 doDelete( new File( "target" + File.separator + "eve" ) );
59 }
60
61
62 /***
63 * Deletes the Eve working directory.
64 *
65 * @throws java.io.IOException if there are failures while deleting.
66 */
67 protected void doDelete( File wkdir ) throws IOException
68 {
69 if ( doDelete )
70 {
71 if ( wkdir.exists() )
72 {
73 FileUtils.deleteDirectory( wkdir );
74 }
75 }
76 }
77
78
79 /***
80 * Sets the system context root to null.
81 *
82 * @see junit.framework.TestCase#tearDown()
83 */
84 protected void tearDown() throws Exception
85 {
86 super.tearDown();
87
88 Hashtable env = new Hashtable();
89
90 env.put( Context.PROVIDER_URL, "ou=system" );
91
92 env.put( Context.INITIAL_CONTEXT_FACTORY, "org.apache.ldap.server.jndi.CoreContextFactory" );
93
94 env.put( EnvKeys.SHUTDOWN, "" );
95
96 env.put( Context.SECURITY_PRINCIPAL, "uid=admin,ou=system" );
97
98 env.put( Context.SECURITY_CREDENTIALS, "secret" );
99
100 try { new InitialContext( env ); } catch( Exception e ) {}
101 }
102
103
104 /***
105 * Creates an initial context using the empty string for the provider URL.
106 * This should work.
107 *
108 * @throws NamingException if there are any problems
109 */
110 public void testGetInitialContext() throws NamingException
111 {
112 Hashtable env = new Hashtable();
113
114 env.put( EnvKeys.WKDIR, "target/server" );
115
116 env.put( Context.PROVIDER_URL, "" );
117
118 env.put( Context.SECURITY_PRINCIPAL, "uid=admin,ou=system" );
119
120 env.put( Context.SECURITY_CREDENTIALS, "secret" );
121
122 env.put( Context.INITIAL_CONTEXT_FACTORY, CoreContextFactory.class.getName() );
123
124 InitialContext initCtx = new InitialContext( env );
125
126 assertNotNull( initCtx );
127 }
128
129
130 /***
131 * Gets a DirContext from the InitialContext for the empty string or RootDSE
132 * and checks that none of the operational attributes are returned.
133 *
134 * @throws NamingException if there are any problems
135 */
136 public void testGetInitialContextLookupAttributes() throws NamingException
137 {
138 Hashtable env = new Hashtable();
139
140 env.put( EnvKeys.WKDIR, "target/server" );
141
142 env.put( Context.PROVIDER_URL, "" );
143
144 env.put( Context.SECURITY_PRINCIPAL, "uid=admin,ou=system" );
145
146 env.put( Context.SECURITY_CREDENTIALS, "secret" );
147
148 env.put( Context.INITIAL_CONTEXT_FACTORY, CoreContextFactory.class.getName() );
149
150 InitialContext initCtx = new InitialContext( env );
151
152 assertNotNull( initCtx );
153
154 DirContext ctx = ( DirContext ) initCtx.lookup( "" );
155
156 Attributes attributes = ctx.getAttributes( "" );
157
158
159
160 assertEquals( 1, attributes.size() );
161 }
162
163
164 /***
165 * Checks for namingContexts and vendorName attributes.
166 *
167 * @throws NamingException if there are any problems
168 */
169 public void testGetInitialContextLookupAttributesByName() throws NamingException
170 {
171 Hashtable env = new Hashtable();
172
173 env.put( EnvKeys.WKDIR, "target/server" );
174
175 env.put( Context.PROVIDER_URL, "" );
176
177 env.put( Context.SECURITY_PRINCIPAL, "uid=admin,ou=system" );
178
179 env.put( Context.SECURITY_CREDENTIALS, "secret" );
180
181 env.put( Context.INITIAL_CONTEXT_FACTORY, CoreContextFactory.class.getName() );
182
183 InitialContext initCtx = new InitialContext( env );
184
185 assertNotNull( initCtx );
186
187 DirContext ctx = ( DirContext ) initCtx.lookup( "" );
188
189 Attributes attributes = ctx.getAttributes( "", new String[]{ "namingContexts", "vendorName" });
190
191 assertEquals( 2, attributes.size() );
192
193 assertEquals( "Apache Software Foundation", attributes.get( "vendorName" ).get() );
194
195 assertTrue( attributes.get( "namingContexts" ).contains( "ou=system" ) );
196 }
197
198
199 /***
200 * Checks for lack of permissions to delete this entry.
201 *
202 * @throws NamingException if there are any problems
203 */
204 public void testDelete() throws NamingException
205 {
206 Hashtable env = new Hashtable();
207
208 env.put( EnvKeys.WKDIR, "target/server" );
209
210 env.put( Context.PROVIDER_URL, "" );
211
212 env.put( Context.SECURITY_PRINCIPAL, "uid=admin,ou=system" );
213
214 env.put( Context.SECURITY_CREDENTIALS, "secret" );
215
216 env.put( Context.INITIAL_CONTEXT_FACTORY, CoreContextFactory.class.getName() );
217
218 InitialContext initCtx = new InitialContext( env );
219
220 assertNotNull( initCtx );
221
222 DirContext ctx = ( DirContext ) initCtx.lookup( "" );
223
224 LdapNoPermissionException notNull = null;
225
226 try
227 {
228 ctx.destroySubcontext( "" );
229
230 fail( "we should never get here" );
231 }
232 catch ( LdapNoPermissionException e )
233 {
234 notNull = e;
235 }
236
237 assertNotNull( notNull );
238 }
239
240
241 /***
242 * Checks for lack of permissions to rename or move this entry.
243 *
244 * @throws NamingException if there are any problems
245 */
246 public void testRename() throws NamingException
247 {
248 Hashtable env = new Hashtable();
249
250 env.put( EnvKeys.WKDIR, "target/server" );
251
252 env.put( Context.PROVIDER_URL, "" );
253
254 env.put( Context.SECURITY_PRINCIPAL, "uid=admin,ou=system" );
255
256 env.put( Context.SECURITY_CREDENTIALS, "secret" );
257
258 env.put( Context.INITIAL_CONTEXT_FACTORY, CoreContextFactory.class.getName() );
259
260 InitialContext initCtx = new InitialContext( env );
261
262 assertNotNull( initCtx );
263
264 DirContext ctx = ( DirContext ) initCtx.lookup( "" );
265
266 LdapNoPermissionException notNull = null;
267
268 try
269 {
270 ctx.rename( "", "ou=system" );
271
272 fail( "we should never get here" );
273 }
274 catch ( LdapNoPermissionException e )
275 {
276 notNull = e;
277 }
278
279 assertNotNull( notNull );
280 }
281
282
283 /***
284 * Checks for lack of permissions to modify this entry.
285 *
286 * @throws NamingException if there are any problems
287 */
288 public void testModify() throws NamingException
289 {
290 Hashtable env = new Hashtable();
291
292 env.put( EnvKeys.WKDIR, "target/server" );
293
294 env.put( Context.PROVIDER_URL, "" );
295
296 env.put( Context.SECURITY_PRINCIPAL, "uid=admin,ou=system" );
297
298 env.put( Context.SECURITY_CREDENTIALS, "secret" );
299
300 env.put( Context.INITIAL_CONTEXT_FACTORY, CoreContextFactory.class.getName() );
301
302 InitialContext initCtx = new InitialContext( env );
303
304 assertNotNull( initCtx );
305
306 DirContext ctx = ( DirContext ) initCtx.lookup( "" );
307
308 LdapNoPermissionException notNull = null;
309
310 try
311 {
312 ctx.modifyAttributes( "", 0, null );
313
314 fail( "we should never get here" );
315 }
316 catch ( LdapNoPermissionException e )
317 {
318 notNull = e;
319 }
320
321 assertNotNull( notNull );
322 }
323
324
325 /***
326 * Checks for lack of permissions to modify this entry.
327 *
328 * @throws NamingException if there are any problems
329 */
330 public void testModify2() throws NamingException
331 {
332 Hashtable env = new Hashtable();
333
334 env.put( EnvKeys.WKDIR, "target/server" );
335
336 env.put( Context.PROVIDER_URL, "" );
337
338 env.put( Context.SECURITY_PRINCIPAL, "uid=admin,ou=system" );
339
340 env.put( Context.SECURITY_CREDENTIALS, "secret" );
341
342 env.put( Context.INITIAL_CONTEXT_FACTORY, CoreContextFactory.class.getName() );
343
344 InitialContext initCtx = new InitialContext( env );
345
346 assertNotNull( initCtx );
347
348 DirContext ctx = ( DirContext ) initCtx.lookup( "" );
349
350 LdapNoPermissionException notNull = null;
351
352 try
353 {
354 ctx.modifyAttributes( "", new ModificationItem[]{} );
355
356 fail( "we should never get here" );
357 }
358 catch ( LdapNoPermissionException e )
359 {
360 notNull = e;
361 }
362
363 assertNotNull( notNull );
364 }
365 }