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

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 months ago
3 years ago
  1. package de.hsfulda.informatik;
  2. import com.unboundid.ldap.sdk.Attribute;
  3. import com.unboundid.ldap.sdk.Entry;
  4. import com.unboundid.ldap.sdk.LDAPException;
  5. import com.unboundid.ldap.sdk.SearchResultEntry;
  6. import org.junit.Test;
  7. import java.io.IOException;
  8. import java.security.GeneralSecurityException;
  9. import java.util.ArrayList;
  10. import java.util.Arrays;
  11. import java.util.List;
  12. import java.util.Map;
  13. import java.util.Set;
  14. import java.util.stream.Collectors;
  15. import static org.junit.Assert.assertEquals;
  16. import static org.junit.Assert.assertTrue;
  17. public class LdapSyncTest {
  18. private List<Entry> addList;
  19. private Set<String> delSet;
  20. @Test
  21. public void testSync() throws LDAPException, GeneralSecurityException, IOException {
  22. final List<SearchResultEntry> remoteEntries = new ArrayList<>();
  23. remoteEntries.add(searchResultEntry("dn=Fd1234,dc=remote,dc=de", "fD1234", "Mustermann", "Mustermann"));
  24. remoteEntries.add(searchResultEntry("dn=fDai1235,dc=remote,dc=de", "FDai1235", "Schuster", "Schuster"));
  25. remoteEntries.add(searchResultEntry("dn=fdaI1236,dc=remote,dc=de", "fdAI1236", "Bunsen", "Bunsen"));
  26. remoteEntries.add(searchResultEntry("dn=fDeT123,dc=remote,dc=de", "FDET123", "Bauer", "Bauer"));
  27. remoteEntries.add(searchResultEntry("dn=fdw9424,dc=remote,dc=de", "fdw9424", "Hein", "Bloed"));
  28. remoteEntries.add(searchResultEntry("dn=fdXx9999,dc=remote,dc=de", "fdxX9999", "Schmidt", "Schmidt"));
  29. final AccountSource remote = new AccountSource("dc=remote,dc=de", remoteEntries);
  30. final List<SearchResultEntry> localEntries = new ArrayList<>();
  31. localEntries.add(searchResultEntry("dn=Fdai1236,dc=local,dc=de", "fDai1236", "Bunsen", "Bunsen"));
  32. localEntries.add(searchResultEntry("dn=fDai1237,dc=local,dc=de", "fdAi1237", "Beaker", "Beaker"));
  33. localEntries.add(searchResultEntry("dn=fdAi1238,dc=local,dc=de", "fdaI1238", "Hopper", "Hopper"));
  34. final AccountSource local = new AccountSource("dc=local,dc=de", localEntries) {
  35. @Override
  36. public void add(List<Entry> usersToBeAdded) {
  37. addList = usersToBeAdded;
  38. }
  39. @Override
  40. public void del(Set<String> usersToBeDeleted) {
  41. delSet = usersToBeDeleted;
  42. }
  43. };
  44. final LdapSync ldapSync = new LdapSync(remote, local);
  45. assertEquals(delSet.size(), 2);
  46. assertTrue(delSet.contains("fdai1237"));
  47. assertTrue(delSet.contains("fdai1238"));
  48. assertEquals(5, addList.size());
  49. for (final Entry entry : addList) {
  50. assertTrue(entry.getDN().endsWith(",dc=local,dc=de"));
  51. final List<String> objectClasses = Arrays.asList(entry.getObjectClassValues());
  52. assertTrue(objectClasses.contains("shadowAccount"));
  53. assertTrue(objectClasses.contains("posixAccount"));
  54. assertTrue(objectClasses.contains("top"));
  55. assertTrue(objectClasses.contains("person"));
  56. assertTrue(objectClasses.contains("inetOrgPerson"));
  57. final List<String> attributes = entry.getAttributes().stream().map(a -> a.getName()).collect(Collectors.toList());
  58. assertTrue(attributes.contains("uid"));
  59. assertTrue(attributes.contains("cn"));
  60. assertTrue(attributes.contains("givenname"));
  61. assertTrue(attributes.contains("sn"));
  62. assertTrue(attributes.contains("uidNumber"));
  63. assertTrue(attributes.contains("gidNumber"));
  64. assertTrue(attributes.contains("homeDirectory"));
  65. assertEquals(entry.getAttributeValue("cn").toLowerCase(), entry.getAttributeValue("cn"));
  66. assertEquals(entry.getAttributeValue("uid").toLowerCase(), entry.getAttributeValue("uid"));
  67. assertEquals(entry.getDN().toLowerCase(), entry.getDN());
  68. System.out.println();
  69. for (final String string : entry.toLDIF()) {
  70. System.out.println(string);
  71. }
  72. }
  73. }
  74. @Test
  75. public void testUidNumber() {
  76. assertEquals(41000, (int) LdapSync.computeUid("fd1000"));
  77. assertEquals(49999, (int) LdapSync.computeUid("fd9999"));
  78. assertEquals(10141000, (int) LdapSync.computeUid("fdaa1000"));
  79. assertEquals(10149999, (int) LdapSync.computeUid("fdaa9999"));
  80. assertEquals(262641000, (int) LdapSync.computeUid("fdzz1000"));
  81. assertEquals(262649999, (int) LdapSync.computeUid("fdzz9999"));
  82. assertEquals(270000001, (int) LdapSync.computeUid("fd0000001"));
  83. assertEquals(279999999, (int) LdapSync.computeUid("fd9999999"));
  84. // test user defined mappings
  85. Map<String, Integer> mappings;
  86. mappings = LdapSync.parseUserDefinedMappings("fdg00244:300000244,fdbla:300009999, fdfoo:300009777 , fdbar:300009888");
  87. assertEquals(300000244, (int) LdapSync.computeUid("fdg00244", mappings));
  88. assertEquals(300009999, (int) LdapSync.computeUid("fdbla", mappings));
  89. assertEquals(300009777, (int) LdapSync.computeUid("fdfoo", mappings));
  90. assertEquals(300009888, (int) LdapSync.computeUid("fdbar", mappings));
  91. assertEquals(41000, (int) LdapSync.computeUid("fd1000", mappings));
  92. assertEquals(49999, (int) LdapSync.computeUid("fd9999", mappings));
  93. assertEquals(10141000, (int) LdapSync.computeUid("fdaa1000", mappings));
  94. assertEquals(10149999, (int) LdapSync.computeUid("fdaa9999", mappings));
  95. assertEquals(262641000, (int) LdapSync.computeUid("fdzz1000", mappings));
  96. assertEquals(262649999, (int) LdapSync.computeUid("fdzz9999", mappings));
  97. mappings = LdapSync.parseUserDefinedMappings("fdg00244:300000244");
  98. assertEquals(300000244, (int) LdapSync.computeUid("fdg00244", mappings));
  99. assertEquals( 41000, (int) LdapSync.computeUid("fd1000", mappings));
  100. assertEquals( 49999, (int) LdapSync.computeUid("fd9999", mappings));
  101. assertEquals( 10141000, (int) LdapSync.computeUid("fdaa1000", mappings));
  102. assertEquals(262641000, (int) LdapSync.computeUid("fdzz1000", mappings));
  103. assertEquals( 10041000, (int) LdapSync.computeUid("fda1000", mappings));
  104. assertEquals(260041000, (int) LdapSync.computeUid("fdz1000", mappings));
  105. assertEquals( 10149999, (int) LdapSync.computeUid("fdaa9999", mappings));
  106. assertEquals(262649999, (int) LdapSync.computeUid("fdzz9999", mappings));
  107. assertEquals(300000000, (int) LdapSync.computeUid("fdg00000", mappings));
  108. assertEquals(300099999, (int) LdapSync.computeUid("fdg99999", mappings));
  109. assertEquals(230049123, (int) LdapSync.computeUid("fdw9123", mappings));
  110. mappings = LdapSync.parseUserDefinedMappings("aaabbb");
  111. assertEquals(0, mappings.size());
  112. mappings = LdapSync.parseUserDefinedMappings("aaa:123");
  113. assertEquals(1, mappings.size());
  114. mappings = LdapSync.parseUserDefinedMappings("aaa::123");
  115. assertEquals(0, mappings.size());
  116. mappings = LdapSync.parseUserDefinedMappings("aaa:123,,bbb:456");
  117. assertEquals(2, mappings.size());
  118. mappings = LdapSync.parseUserDefinedMappings("aaa:123, bbb:ccc");
  119. assertEquals(1, mappings.size());
  120. }
  121. private SearchResultEntry searchResultEntry(final String dn, final String cn, final String sn, final String givenname) {
  122. final Attribute[] attributes = {
  123. new Attribute("cn", cn),
  124. new Attribute("uid", cn),
  125. new Attribute("sn", sn),
  126. new Attribute("givenname", givenname),
  127. new Attribute("objectClass", "inetOrgPerson"),
  128. new Attribute("objectClass", "top"),
  129. };
  130. return new SearchResultEntry(dn, attributes);
  131. }
  132. }