1 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 42 public class RootDSETest extends TestCase 43 { 44 45 protected boolean doDelete = true; 46 47 48 54 protected void setUp() throws Exception 55 { 56 super.setUp(); 57 58 doDelete( new File ( "target" + File.separator + "eve" ) ); 59 } 60 61 62 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 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 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 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 160 assertEquals( 1, attributes.size() ); 161 } 162 163 164 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 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 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 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 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 } 366 | Popular Tags |