You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

156 lines
7.6 KiB

package de.hsfulda.informatik;
import com.unboundid.ldap.sdk.Attribute;
import com.unboundid.ldap.sdk.Entry;
import com.unboundid.ldap.sdk.LDAPException;
import com.unboundid.ldap.sdk.SearchResultEntry;
import org.junit.Test;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
public class LdapSyncTest {
private List<Entry> addList;
private Set<String> delSet;
@Test
public void testSync() throws LDAPException, GeneralSecurityException, IOException {
final List<SearchResultEntry> remoteEntries = new ArrayList<>();
remoteEntries.add(searchResultEntry("dn=Fd1234,dc=remote,dc=de", "fD1234", "Mustermann", "Mustermann"));
remoteEntries.add(searchResultEntry("dn=fDai1235,dc=remote,dc=de", "FDai1235", "Schuster", "Schuster"));
remoteEntries.add(searchResultEntry("dn=fdaI1236,dc=remote,dc=de", "fdAI1236", "Bunsen", "Bunsen"));
remoteEntries.add(searchResultEntry("dn=fDeT123,dc=remote,dc=de", "FDET123", "Bauer", "Bauer"));
remoteEntries.add(searchResultEntry("dn=fdw9424,dc=remote,dc=de", "fdw9424", "Hein", "Bloed"));
remoteEntries.add(searchResultEntry("dn=fdXx9999,dc=remote,dc=de", "fdxX9999", "Schmidt", "Schmidt"));
final AccountSource remote = new AccountSource("dc=remote,dc=de", remoteEntries);
final List<SearchResultEntry> localEntries = new ArrayList<>();
localEntries.add(searchResultEntry("dn=Fdai1236,dc=local,dc=de", "fDai1236", "Bunsen", "Bunsen"));
localEntries.add(searchResultEntry("dn=fDai1237,dc=local,dc=de", "fdAi1237", "Beaker", "Beaker"));
localEntries.add(searchResultEntry("dn=fdAi1238,dc=local,dc=de", "fdaI1238", "Hopper", "Hopper"));
final AccountSource local = new AccountSource("dc=local,dc=de", localEntries) {
@Override
public void add(List<Entry> usersToBeAdded) {
addList = usersToBeAdded;
}
@Override
public void del(Set<String> usersToBeDeleted) {
delSet = usersToBeDeleted;
}
};
final LdapSync ldapSync = new LdapSync(remote, local);
assertEquals(delSet.size(), 2);
assertTrue(delSet.contains("fdai1237"));
assertTrue(delSet.contains("fdai1238"));
assertEquals(5, addList.size());
for (final Entry entry : addList) {
assertTrue(entry.getDN().endsWith(",dc=local,dc=de"));
final List<String> objectClasses = Arrays.asList(entry.getObjectClassValues());
assertTrue(objectClasses.contains("shadowAccount"));
assertTrue(objectClasses.contains("posixAccount"));
assertTrue(objectClasses.contains("top"));
assertTrue(objectClasses.contains("person"));
assertTrue(objectClasses.contains("inetOrgPerson"));
final List<String> attributes = entry.getAttributes().stream().map(a -> a.getName()).collect(Collectors.toList());
assertTrue(attributes.contains("uid"));
assertTrue(attributes.contains("cn"));
assertTrue(attributes.contains("givenname"));
assertTrue(attributes.contains("sn"));
assertTrue(attributes.contains("uidNumber"));
assertTrue(attributes.contains("gidNumber"));
assertTrue(attributes.contains("homeDirectory"));
assertEquals(entry.getAttributeValue("cn").toLowerCase(), entry.getAttributeValue("cn"));
assertEquals(entry.getAttributeValue("uid").toLowerCase(), entry.getAttributeValue("uid"));
assertEquals(entry.getDN().toLowerCase(), entry.getDN());
System.out.println();
for (final String string : entry.toLDIF()) {
System.out.println(string);
}
}
}
@Test
public void testUidNumber() {
assertEquals(41000, (int) LdapSync.computeUid("fd1000"));
assertEquals(49999, (int) LdapSync.computeUid("fd9999"));
assertEquals(10141000, (int) LdapSync.computeUid("fdaa1000"));
assertEquals(10149999, (int) LdapSync.computeUid("fdaa9999"));
assertEquals(262641000, (int) LdapSync.computeUid("fdzz1000"));
assertEquals(262649999, (int) LdapSync.computeUid("fdzz9999"));
assertEquals(270000001, (int) LdapSync.computeUid("fd0000001"));
assertEquals(279999999, (int) LdapSync.computeUid("fd9999999"));
// test user defined mappings
Map<String, Integer> mappings;
mappings = LdapSync.parseUserDefinedMappings("fdg00244:300000244,fdbla:300009999, fdfoo:300009777 , fdbar:300009888");
assertEquals(300000244, (int) LdapSync.computeUid("fdg00244", mappings));
assertEquals(300009999, (int) LdapSync.computeUid("fdbla", mappings));
assertEquals(300009777, (int) LdapSync.computeUid("fdfoo", mappings));
assertEquals(300009888, (int) LdapSync.computeUid("fdbar", mappings));
assertEquals(41000, (int) LdapSync.computeUid("fd1000", mappings));
assertEquals(49999, (int) LdapSync.computeUid("fd9999", mappings));
assertEquals(10141000, (int) LdapSync.computeUid("fdaa1000", mappings));
assertEquals(10149999, (int) LdapSync.computeUid("fdaa9999", mappings));
assertEquals(262641000, (int) LdapSync.computeUid("fdzz1000", mappings));
assertEquals(262649999, (int) LdapSync.computeUid("fdzz9999", mappings));
mappings = LdapSync.parseUserDefinedMappings("fdg00244:300000244");
assertEquals(300000244, (int) LdapSync.computeUid("fdg00244", mappings));
assertEquals( 41000, (int) LdapSync.computeUid("fd1000", mappings));
assertEquals( 49999, (int) LdapSync.computeUid("fd9999", mappings));
assertEquals( 10141000, (int) LdapSync.computeUid("fdaa1000", mappings));
assertEquals(262641000, (int) LdapSync.computeUid("fdzz1000", mappings));
assertEquals( 10041000, (int) LdapSync.computeUid("fda1000", mappings));
assertEquals(260041000, (int) LdapSync.computeUid("fdz1000", mappings));
assertEquals( 10149999, (int) LdapSync.computeUid("fdaa9999", mappings));
assertEquals(262649999, (int) LdapSync.computeUid("fdzz9999", mappings));
assertEquals(300000000, (int) LdapSync.computeUid("fdg00000", mappings));
assertEquals(300099999, (int) LdapSync.computeUid("fdg99999", mappings));
assertEquals(230049123, (int) LdapSync.computeUid("fdw9123", mappings));
mappings = LdapSync.parseUserDefinedMappings("aaabbb");
assertEquals(0, mappings.size());
mappings = LdapSync.parseUserDefinedMappings("aaa:123");
assertEquals(1, mappings.size());
mappings = LdapSync.parseUserDefinedMappings("aaa::123");
assertEquals(0, mappings.size());
mappings = LdapSync.parseUserDefinedMappings("aaa:123,,bbb:456");
assertEquals(2, mappings.size());
mappings = LdapSync.parseUserDefinedMappings("aaa:123, bbb:ccc");
assertEquals(1, mappings.size());
}
private SearchResultEntry searchResultEntry(final String dn, final String cn, final String sn, final String givenname) {
final Attribute[] attributes = {
new Attribute("cn", cn),
new Attribute("uid", cn),
new Attribute("sn", sn),
new Attribute("givenname", givenname),
new Attribute("objectClass", "inetOrgPerson"),
new Attribute("objectClass", "top"),
};
return new SearchResultEntry(dn, attributes);
}
}