]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.security.core/ext/test/org/argeo/osgi/useradmin/LdifParserTest.java
Fix: LDIF parser manages last entry properly.
[lgpl/argeo-commons.git] / org.argeo.security.core / ext / test / org / argeo / osgi / useradmin / LdifParserTest.java
1 package org.argeo.osgi.useradmin;
2
3 import java.util.ArrayList;
4 import java.util.List;
5 import java.util.SortedMap;
6
7 import javax.naming.NamingEnumeration;
8 import javax.naming.directory.Attribute;
9 import javax.naming.directory.Attributes;
10 import javax.naming.ldap.LdapName;
11
12 import junit.framework.TestCase;
13
14 import org.apache.commons.codec.binary.Base64;
15 import org.apache.commons.codec.digest.DigestUtils;
16
17 public class LdifParserTest extends TestCase {
18 public void testSimpleLdif() throws Exception {
19 LdifParser ldifParser = new LdifParser();
20 SortedMap<LdapName, Attributes> res = ldifParser.read(getClass()
21 .getResourceAsStream("test.ldif"));
22 LdapName rootDn = new LdapName(
23 "uid=root,ou=People,dc=demo,dc=example,dc=org");
24 Attributes rootAttributes = res.get(rootDn);
25 assertNotNull(rootAttributes);
26 assertEquals("Superuser", rootAttributes.get("description").get());
27 byte[] rawPwEntry = (byte[]) rootAttributes.get("userpassword").get();
28 assertEquals("{SHA}ieSV55Qc+eQOaYDRSha/AjzNTJE=",
29 new String(rawPwEntry));
30 byte[] hashedPassword = DigestUtils.sha1("demo".getBytes());
31 assertEquals("{SHA}" + Base64.encodeBase64String(hashedPassword),
32 new String(rawPwEntry));
33
34 LdapName adminDn = new LdapName(
35 "cn=admin,ou=Roles,dc=demo,dc=example,dc=org");
36 Attributes adminAttributes = res.get(adminDn);
37 assertNotNull(adminAttributes);
38 Attribute memberAttribute = adminAttributes.get("member");
39 assertNotNull(memberAttribute);
40 NamingEnumeration<?> members = memberAttribute.getAll();
41 List<String> users = new ArrayList<String>();
42 while (members.hasMore()) {
43 Object value = members.next();
44 users.add(value.toString());
45 }
46 assertEquals(1, users.size());
47 assertEquals(rootDn, new LdapName(users.get(0)));
48 }
49 }