Skip to content

Commit

Permalink
tests for WithRDN
Browse files Browse the repository at this point in the history
  • Loading branch information
merlinz01 committed Apr 15, 2024
1 parent 0acf473 commit 96aeebe
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
1 change: 1 addition & 0 deletions dn.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ func (d DN) CommonSuperior(other DN) DN {
// Returns the DN with the specified RDNAttribute added to the end.
func (d DN) WithRDN(rdn RDN) DN {
new := make(DN, len(d), len(d)+1)
copy(new, d)
return append(new, rdn)
}

Expand Down
34 changes: 34 additions & 0 deletions dn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,3 +249,37 @@ func TestDNCommonAncestor(t *testing.T) {
}
}
}

func TestDNWithRD(t *testing.T) {
type rdTest struct {
baseDN string
rdnType string
rdnValue string
expectedDN string
}
rdTests := []rdTest{
{"", "dc", "com", "dc=com"},
{"dc=com", "dc", "example", "dc=example,dc=com"},
{"dc=example,dc=com", "ou", "users", "ou=users,dc=example,dc=com"},
{"ou=users,dc=example,dc=com", "uid", "jdoe", "uid=jdoe,ou=users,dc=example,dc=com"},
{"uid=jdoe,ou=users,dc=example,dc=com", "cn", "John Doe", "cn=John Doe,uid=jdoe,ou=users,dc=example,dc=com"},
}
for _, test := range rdTests {
baseDN, err := ldapserver.ParseDN(test.baseDN)
if err != nil {
t.Fatalf("Error parsing base DN: %s", err)
}
expectedDN, err := ldapserver.ParseDN(test.expectedDN)
if err != nil {
t.Fatalf("Error parsing expected DN: %s", err)
}
rdn := ldapserver.RDN{{test.rdnType, test.rdnValue}}
dn := baseDN.WithRDN(rdn)
if !dn.Equal(expectedDN) {
t.Errorf("Expected \"%s\", got \"%s\" for DN with RDN \"%s\" in base DN \"%s\"", expectedDN, dn, rdn, baseDN)
}
if !dn.Equal(baseDN.WithRDNAttribute(test.rdnType, test.rdnValue)) {
t.Errorf("Expected \"%s\", got \"%s\" for DN with RDN \"%s\" in base DN \"%s\"", expectedDN, dn, rdn, baseDN)
}
}
}

0 comments on commit 96aeebe

Please sign in to comment.