package ldap import ( "testing" "github.com/vjeantet/goldap/message" ) func testServer() *server { return &server{ nc: []string{"dc=netauth", "dc=example", "dc=com"}, } } func TestEntityIDFromDN(t *testing.T) { s := testServer() valid := []struct { dn string id string }{ {"uid=jsmith,ou=entities,dc=netauth,dc=example,dc=com", "jsmith"}, {"uid=jsmith, ou=entities, dc=netauth, dc=example, dc=com", "jsmith"}, } for _, tc := range valid { id, err := s.entityIDFromDN(message.LDAPDN(tc.dn)) if err != nil { t.Errorf("%q: unexpected error %q", tc.dn, err) continue } if id != tc.id { t.Errorf("%q: got id %q, want %q", tc.dn, id, tc.id) } } invalid := []string{ "", "uid=jsmith", "cn=jsmith,ou=entities,dc=netauth,dc=example,dc=com", "uid=jsmith,ou=groups,dc=netauth,dc=example,dc=com", "uid=jsmith,ou=entities,dc=netauth", "uid=jsmith,ou=entities,dc=netauth,dc=example,dc=com,dc=extra", "uid=jsmith,ou=entities,dc=netauth,dc=other,dc=com", } for _, dn := range invalid { if id, err := s.entityIDFromDN(message.LDAPDN(dn)); err == nil { t.Errorf("%q: expected an error, got id %q", dn, id) } } }