}
public void testLDAPConnection() throws SQLException {
// setup
String dbName = TestConfiguration.getCurrent().getDefaultDatabaseName();
DataSource ds = JDBCDataSource.getDataSource();
Connection conn = ds.getConnection("system", "admin");
// set the ldap properties
setDatabaseProperty("derby.connection.requireAuthentication", "true", conn);
setDatabaseProperty("derby.authentication.provider", "LDAP", conn);
setDatabaseProperty("derby.authentication.server", ldapServer, conn);
setDatabaseProperty("derby.authentication.ldap.searchBase", "o=" + dnString, conn);
setDatabaseProperty("derby.authentication.ldap.searchFilter","(&(objectClass=inetOrgPerson)(uid=%USERNAME%))", conn);
// java.naming.factory.initial is Context.INITIAL_CONTEXT_FACTORY
// but using literal string here to avoid unnecessary import.
// If the initial context factory is not provided it'll default to
// com.sun.jndi.ldap.LdapCtxFactory in LDAPAuthenticationSchemeImpl.
if ((ldapContextFactory != null) && (ldapContextFactory.length() > 0))
setDatabaseProperty("java.naming.factory.initial", ldapContextFactory, conn);
commit();
// shutdown the database as system, so the properties take effect
TestConfiguration.getCurrent().shutdownDatabase();
conn.close();
// actual test.
// first attempt simple connection that should succeed
ds = JDBCDataSource.getDataSource(dbName);
assertLDAPDSConnectionOK(ds, ldapUser, ldapPassword);
assertLDAPDrvMgrConnectionOK(dbName, ldapUser, ldapPassword);
// try to get a connection for a user that has been added, but who
// should *not* be on the ldap server; should fail
assertInvalidLDAPDSConnectionFails(ds, "Jamie", "theHooligan");
assertInvalidLDAPDrvMgrConnectionFails(dbName, "Jamie", "thHooligan");
// try to get a connection using the valid ldapuser, but incorrect pwd
assertInvalidLDAPDSConnectionFails(ds, ldapUser, ldapPassword + "ish");
assertInvalidLDAPDrvMgrConnectionFails(dbName, ldapUser, ldapPassword + "ish");
// set the users DN locally
Connection conn3 = ds.getConnection(ldapUser, ldapPassword);
String tmpString1 = "derby.user." + ldapUser;
String tmpString2 = "uid=" + ldapUser + ",ou=People,o=" + dnString;
setDatabaseProperty(tmpString1, tmpString2, conn3);
conn3.commit();
// restart to let setting take effect
JDBCDataSource.setBeanProperty(ds, "shutdownDatabase", "shutdown");
// shutdown really only happens on next attempt to connect
try {
ds.getConnection(ldapUser, ldapPassword);
fail("expected system shutdown resulting in 08006 error.");
} catch (SQLException e) {
assertSQLState("08006", e);
}
conn3.close();