1 17 package org.apache.ldap.server.authn; 18 19 20 import org.apache.ldap.common.exception.LdapConfigurationException; 21 import org.apache.ldap.common.exception.LdapNoPermissionException; 22 import org.apache.ldap.common.util.ArrayUtils; 23 import org.apache.ldap.server.AbstractCoreTest; 24 import org.apache.ldap.server.jndi.EnvKeys; 25 import org.apache.mina.util.AvailablePortFinder; 26 27 import javax.naming.*; 28 import javax.naming.directory.Attribute ; 29 import javax.naming.directory.Attributes ; 30 import javax.naming.directory.DirContext ; 31 import javax.naming.directory.InitialDirContext ; 32 import javax.naming.ldap.InitialLdapContext ; 33 import java.io.File ; 34 import java.io.IOException ; 35 import java.util.Hashtable ; 36 37 38 45 public class SimpleAuthenticationTest extends AbstractCoreTest 46 { 47 51 public SimpleAuthenticationTest() throws IOException 52 { 53 doDelete( new File ( "target" + File.separator + "eve" ) ); 54 } 55 56 57 71 protected void setUp() throws Exception 72 { 73 if ( getName().equals( "test1AdminAccountCreation" ) || 74 getName().equals( "test2AccountExistsOnRestart" ) ) 75 { 76 super.doDelete = false; 77 } 78 else 79 { 80 super.doDelete = true; 81 } 82 83 if ( getName().equals( "test5BuildDbNoPassWithPrincAuthNone" ) || 84 getName().equals( "test6BuildDbNoPassNotAdminPrinc" ) || 85 getName().equals( "test4BuildDbNoPassNoPrincAuthNone" ) ) 86 { 87 return; 88 } 89 90 super.setUp(); 91 } 92 93 94 100 protected void performAdminAccountChecks( Attributes attrs ) 101 { 102 assertTrue( attrs.get( "objectClass" ).contains( "top" ) ); 103 assertTrue( attrs.get( "objectClass" ).contains( "person" ) ); 104 assertTrue( attrs.get( "objectClass" ).contains( "organizationalPerson" ) ); 105 assertTrue( attrs.get( "objectClass" ).contains( "inetOrgPerson" ) ); 106 assertTrue( attrs.get( "displayName" ).contains( "Directory Superuser" ) ); 107 } 108 109 110 115 public void test1AdminAccountCreation() throws NamingException 116 { 117 DirContext ctx = ( DirContext ) sysRoot.lookup( "uid=admin" ); 118 Attributes attrs = ctx.getAttributes( "" ); 119 performAdminAccountChecks( attrs ); 120 assertTrue( ArrayUtils.isEquals( attrs.get( "userPassword" ).get(), "secret".getBytes() )); 121 } 122 123 124 129 public void test2AccountExistsOnRestart() throws NamingException 130 { 131 DirContext ctx = ( DirContext ) sysRoot.lookup( "uid=admin" ); 132 Attributes attrs = ctx.getAttributes( "" ); 133 134 performAdminAccountChecks( attrs ); 135 assertTrue( ArrayUtils.isEquals( attrs.get( "userPassword" ).get(), "secret".getBytes() )); 136 } 137 138 139 public void test3UseAkarasulu() throws NamingException 140 { 141 Hashtable env = new Hashtable (); 142 env.put( Context.PROVIDER_URL, "ou=system" ); 143 env.put( Context.SECURITY_PRINCIPAL, "uid=akarasulu,ou=users,ou=system" ); 144 env.put( Context.SECURITY_CREDENTIALS, "test" ); 145 env.put( Context.SECURITY_AUTHENTICATION, "simple" ); 146 env.put( Context.INITIAL_CONTEXT_FACTORY, "org.apache.ldap.server.jndi.CoreContextFactory" ); 147 InitialDirContext ic = new InitialDirContext ( env ); 148 Attributes attrs = ic.getAttributes( "uid=akarasulu,ou=users" ); 149 Attribute ou = attrs.get( "ou" ); 150 assertTrue( ou.contains( "Engineering" ) ); 151 assertTrue( ou.contains( "People" ) ); 152 153 Attribute objectClass = attrs.get( "objectClass" ); 154 assertTrue( objectClass.contains( "top" ) ); 155 assertTrue( objectClass.contains( "person" ) ); 156 assertTrue( objectClass.contains( "organizationalPerson" ) ); 157 assertTrue( objectClass.contains( "inetOrgPerson" ) ); 158 159 assertTrue( attrs.get( "telephonenumber" ).contains( "+1 408 555 4798" ) ); 160 assertTrue( attrs.get( "uid" ).contains( "akarasulu" ) ); 161 assertTrue( attrs.get( "givenname" ).contains( "Alex" ) ); 162 assertTrue( attrs.get( "mail" ).contains( "akarasulu@apache.org" ) ); 163 assertTrue( attrs.get( "l" ).contains( "Bogusville" ) ); 164 assertTrue( attrs.get( "sn" ).contains( "Karasulu" ) ); 165 assertTrue( attrs.get( "cn" ).contains( "Alex Karasulu" ) ); 166 assertTrue( attrs.get( "facsimiletelephonenumber" ).contains( "+1 408 555 9751" ) ); 167 assertTrue( attrs.get( "roomnumber" ).contains( "4612" ) ); 168 } 169 170 171 178 public void test4BuildDbNoPassNoPrincAuthNone() throws Exception 179 { 180 tearDown(); 182 doDelete( new File ( "target" + File.separator + "eve" ) ); 183 Hashtable env = new Hashtable (); 184 env.put( Context.SECURITY_AUTHENTICATION, "none" ); 185 env.put( EnvKeys.DISABLE_ANONYMOUS, "true" ); 186 187 int port = AvailablePortFinder.getNextAvailable( 1024 ); 188 189 env.put( EnvKeys.LDAP_PORT, String.valueOf( port ) ); 190 191 try 192 { 193 setSysRoot( env ); 194 fail( "should not get here due to exception" ); 195 } 196 catch( LdapNoPermissionException e ) 197 { 198 } 199 200 Hashtable anonymous = new Hashtable (); 202 203 anonymous.put( EnvKeys.LDAP_PORT, String.valueOf( port ) ); 204 205 InitialLdapContext ctx = ( InitialLdapContext ) setSysRoot( anonymous ); 206 assertNotNull( ctx ); 207 208 env.put( Context.PROVIDER_URL, "ou=system" ); 210 env.put( Context.INITIAL_CONTEXT_FACTORY, "org.apache.ldap.server.jndi.CoreContextFactory" ); 211 212 InitialLdapContext initial = new InitialLdapContext ( env, null ); 213 214 try 215 { 216 ctx = ( InitialLdapContext ) initial.lookup( "uid=admin" ); 217 fail( "should not get here due to exception cuz anonymous user is " 218 + "not allowed read access to the admin account entry" ); 219 } 220 catch( LdapConfigurationException e ) 221 { 222 } 223 catch( LdapNoPermissionException e ) 224 { 225 } 226 } 227 228 229 238 public void test5BuildDbNoPassWithPrincAuthNone() throws Exception 239 { 240 tearDown(); 242 doDelete( new File ( "target" + File.separator + "eve" ) ); 243 Hashtable env = new Hashtable (); 244 env.put( Context.SECURITY_AUTHENTICATION, "none" ); 245 env.put( Context.SECURITY_PRINCIPAL, "uid=admin,ou=system" ); 246 247 try 248 { 249 setSysRoot( env ); 250 fail( "should not get here due to exception" ); 251 } 252 catch( ConfigurationException e ) 253 { 254 } 255 } 256 257 258 267 public void test6BuildDbNoPassNotAdminPrinc() throws Exception 268 { 269 tearDown(); 271 doDelete( new File ( "target" + File.separator + "eve" ) ); 272 Hashtable env = new Hashtable (); 273 env.put( Context.SECURITY_AUTHENTICATION, "simple" ); 274 env.put( Context.SECURITY_PRINCIPAL, "uid=akarasulu,ou=users,ou=system" ); 275 276 try 277 { 278 setSysRoot( env ); 279 fail( "should not get here due to exception" ); 280 } 281 catch( ConfigurationException e ) 282 { 283 } 284 } 285 286 287 293 public void test8PassPrincAuthTypeSimple() throws Exception 294 { 295 Hashtable env = new Hashtable (); 296 env.put( Context.PROVIDER_URL, "ou=system" ); 297 env.put( Context.SECURITY_PRINCIPAL, "uid=admin,ou=system" ); 298 env.put( Context.SECURITY_CREDENTIALS, "secret" ); 299 env.put( Context.SECURITY_AUTHENTICATION, "simple" ); 300 env.put( Context.INITIAL_CONTEXT_FACTORY, "org.apache.ldap.server.jndi.CoreContextFactory" ); 301 assertNotNull( new InitialContext( env ) ); 302 } 303 304 305 311 public void test10TestNonAdminUser() throws Exception 312 { 313 Hashtable env = new Hashtable (); 314 env.put( Context.PROVIDER_URL, "ou=system" ); 315 env.put( Context.SECURITY_PRINCIPAL, "uid=akarasulu,ou=users,ou=system" ); 316 env.put( Context.SECURITY_CREDENTIALS, "test" ); 317 env.put( Context.SECURITY_AUTHENTICATION, "simple" ); 318 env.put( Context.INITIAL_CONTEXT_FACTORY, "org.apache.ldap.server.jndi.CoreContextFactory" ); 319 assertNotNull( new InitialContext( env ) ); 320 } 321 } 322 | Popular Tags |