-
Notifications
You must be signed in to change notification settings - Fork 1
/
dn_test.go
317 lines (306 loc) · 11.9 KB
/
dn_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
package ldapserver_test
import (
"testing"
"github.com/merlinz01/ldapserver"
)
func TestEncodeDN(t *testing.T) {
type dnTest struct {
dnStr string
dn ldapserver.DN
}
tests := []dnTest{
{"uid=jdoe,ou=users,dc=example,dc=com",
ldapserver.DN{{{"dc", "com"}}, {{"dc", "example"}}, {{"ou", "users"}}, {{"uid", "jdoe"}}}},
{"UID=jsmith,DC=example,DC=net",
ldapserver.DN{{{"DC", "net"}}, {{"DC", "example"}}, {{"UID", "jsmith"}}}},
{"CN=J. Smith+OU=Sales,DC=example,DC=net",
ldapserver.DN{{{"DC", "net"}}, {{"DC", "example"}}, {{"CN", "J. Smith"}, {"OU", "Sales"}}}},
{"CN=James \\\"Jim\\\" Smith,DC=example,DC=net",
ldapserver.DN{{{"DC", "net"}}, {{"DC", "example"}}, {{"CN", "James \"Jim\" Smith"}}}},
{"CN=Before\\0DAfter,DC=example,DC=net",
ldapserver.DN{{{"DC", "net"}}, {{"DC", "example"}}, {{"CN", "Before\rAfter"}}}},
{"1.3.6.1.4.1.1466.0=#04024869",
ldapserver.DN{{{"1.3.6.1.4.1.1466.0", "\x48\x69"}}}},
{"CN=Lu\xC4\\8Di\xC4\\87",
ldapserver.DN{{{"CN", "Lu\xC4\x8Di\xC4\x87"}}}},
{"uid=jdoe,ou=C\\+\\+ Developers,dc=example,dc=com",
ldapserver.DN{{{"dc", "com"}}, {{"dc", "example"}}, {{"ou", "C++ Developers"}}, {{"uid", "jdoe"}}}},
{"cn=John Doe\\, Jr.,ou=Developers,dc=example,dc=com",
ldapserver.DN{{{"dc", "com"}}, {{"dc", "example"}}, {{"ou", "Developers"}}, {{"cn", "John Doe, Jr."}}}},
{"cn=\\\"John A. Doe\\, Sr.\\, C\\\\C\\+\\+ Developer\\\"+givenName=John+sn=Doe,ou=Developers,dc=example,dc=com",
ldapserver.DN{{{"dc", "com"}}, {{"dc", "example"}}, {{"ou", "Developers"}}, {{"cn", `"John A. Doe, Sr., C\C++ Developer"`}, {"givenName", "John"}, {"sn", "Doe"}}}},
}
for _, dn := range tests {
pdn, err := ldapserver.ParseDN(dn.dnStr)
if err != nil {
t.Fatalf("Error parsing DN: %s", err)
} else if !pdn.Equal(dn.dn) {
t.Errorf("Expected %s", dn.dn)
t.Fatalf("Got %s", pdn)
} else if pdn.String() != dn.dnStr {
t.Errorf("Expected %s", dn.dnStr)
t.Fatalf("Got %s", pdn.String())
}
}
}
func TestParseDN(t *testing.T) {
type dnTest struct {
dnStr string
dn ldapserver.DN
err error
}
tests := []dnTest{
{"uid=jdoe,ou=users,dc=example,dc=com",
ldapserver.DN{{{"dc", "com"}}, {{"dc", "example"}}, {{"ou", "users"}}, {{"uid", "jdoe"}}}, nil},
{"UID=jsmith,DC=example,DC=net",
ldapserver.DN{{{"DC", "net"}}, {{"DC", "example"}}, {{"UID", "jsmith"}}}, nil},
{"CN=J. Smith+OU=Sales,DC=example,DC=net",
ldapserver.DN{{{"DC", "net"}}, {{"DC", "example"}}, {{"CN", "J. Smith"}, {"OU", "Sales"}}}, nil},
{"CN=James \\\"Jim\\\" Smith,DC=example,DC=net",
ldapserver.DN{{{"DC", "net"}}, {{"DC", "example"}}, {{"CN", "James \"Jim\" Smith"}}}, nil},
{"CN=Before\\0DAfter,DC=example,DC=net",
ldapserver.DN{{{"DC", "net"}}, {{"DC", "example"}}, {{"CN", "Before\rAfter"}}}, nil},
{"CN=,DC=,DC=", ldapserver.DN{{{"DC", ""}}, {{"DC", ""}}, {{"CN", ""}}}, nil},
{"CN", nil, ldapserver.ErrInvalidDN},
{"CN=J. Smith,OU=Sales,DC=example,DC", nil, ldapserver.ErrInvalidDN},
}
for _, dn := range tests {
pdn, err := ldapserver.ParseDN(dn.dnStr)
if err != dn.err {
t.Fatalf("Error parsing DN: %s", err)
} else if !pdn.Equal(dn.dn) {
t.Errorf("Expected %s", dn.dn)
t.Fatalf("Got %s", pdn)
}
}
}
func TestDNIsChild(t *testing.T) {
type childTest struct {
child string
parent string
isChild bool
}
childTests := []childTest{
{"uid=jdoe,ou=users,dc=example,dc=com", "ou=users,dc=example,dc=com", true},
{"ou=users,dc=example,dc=com", "dc=example,dc=com", true},
{"dc=example,dc=com", "dc=com", true},
{"dc=com", "", true},
{"", "dc=com", false},
{"", "", false},
{"uid=jdoe,ou=users,dc=example,dc=com", "", false},
{"uid=jdoe,ou=users,dc=example,dc=com", "uid=jdoe,ou=users,dc=example,dc=com", false},
{"ou=users,dc=example,dc=com", "uid=jdoe,ou=users,dc=example,dc=org", false},
{"uid=jdoe,ou=users,dc=example,dc=com", "uid=jdoe,ou=users,dc=example,dc=com,dc=org", false},
}
for _, test := range childTests {
parent, err := ldapserver.ParseDN(test.parent)
if err != nil {
t.Fatalf("Error parsing parent DN: %s", err)
}
child, err := ldapserver.ParseDN(test.child)
if err != nil {
t.Fatalf("Error parsing child DN: %s", err)
}
if child.IsChild(parent) != test.isChild {
t.Errorf("Expected %t, got %t for \"%s\" is child of \"%s\"", test.isChild, !test.isChild, test.child, test.parent)
}
}
}
func TestDNIsParent(t *testing.T) {
type parentTest struct {
parent string
child string
isChild bool
}
parentTests := []parentTest{
{"ou=users,dc=example,dc=com", "uid=jdoe,ou=users,dc=example,dc=com", true},
{"dc=example,dc=com", "ou=users,dc=example,dc=com", true},
{"dc=com", "dc=example,dc=com", true},
{"", "dc=com", true},
{"dc=com", "", false},
{"", "", false},
{"dc=com", "uid=jdoe,ou=users,dc=example,dc=com", false},
{"uid=jdoe,ou=users,dc=example,dc=com", "uid=jdoe,ou=users,dc=example,dc=com", false},
{"uid=jdoe,ou=users,dc=example,dc=com,dc=org", "uid=jdoe,ou=users,dc=example,dc=com", false},
}
for _, test := range parentTests {
parent, err := ldapserver.ParseDN(test.parent)
if err != nil {
t.Fatalf("Error parsing parent DN: %s", err)
}
child, err := ldapserver.ParseDN(test.child)
if err != nil {
t.Fatalf("Error parsing child DN: %s", err)
}
if parent.IsParent(child) != test.isChild {
t.Errorf("Expected %t, got %t for \"%s\" is parent of \"%s\"", test.isChild, !test.isChild, test.parent, test.child)
}
}
}
func TestDNIsSuperior(t *testing.T) {
type superiorTest struct {
superior string
inferior string
isSuper bool
}
superiorTests := []superiorTest{
{"ou=users,dc=example,dc=com", "uid=jdoe,ou=users,dc=example,dc=com", true},
{"dc=example,dc=com", "ou=users,dc=example,dc=com", true},
{"dc=com", "dc=example,dc=com", true},
{"dc=com", "uid=jdoe,ou=users,dc=example,dc=com", true},
{"", "dc=com", true},
{"dc=com", "", false},
{"", "", false},
{"uid=jdoe,ou=users,dc=example,dc=com", "uid=jdoe,ou=users,dc=example,dc=com", false},
{"ou=users,dc=example,dc=com", "uid=jdoe,ou=users,dc=example,dc=com,dc=org", false},
{"ou=users,dc=example,dc=com,dc=org", "uid=jdoe,ou=users,dc=example,dc=com", false},
}
for _, test := range superiorTests {
superior, err := ldapserver.ParseDN(test.superior)
if err != nil {
t.Fatalf("Error parsing superior DN: %s", err)
}
inferior, err := ldapserver.ParseDN(test.inferior)
if err != nil {
t.Fatalf("Error parsing inferior DN: %s", err)
}
if superior.IsSuperior(inferior) != test.isSuper {
t.Errorf("Expected %t, got %t for \"%s\" is superior of \"%s\"", test.isSuper, !test.isSuper, test.superior, test.inferior)
}
}
}
func TestDNIsSubordinate(t *testing.T) {
type subordinateTest struct {
subordinate string
superior string
isSub bool
}
subordinateTests := []subordinateTest{
{"uid=jdoe,ou=users,dc=example,dc=com", "ou=users,dc=example,dc=com", true},
{"ou=users,dc=example,dc=com", "dc=example,dc=com", true},
{"dc=example,dc=com", "dc=com", true},
{"dc=com", "", true},
{"", "dc=com", false},
{"", "", false},
{"uid=jdoe,ou=users,dc=example,dc=com", "", true},
{"uid=jdoe,ou=users,dc=example,dc=com", "uid=jdoe,ou=users,dc=example,dc=com", false},
{"ou=users,dc=example,dc=com", "uid=jdoe,ou=users,dc=example,dc=org", false},
{"uid=jdoe,ou=users,dc=example,dc=com", "uid=jdoe,ou=users,dc=example,dc=com,dc=org", false},
}
for _, test := range subordinateTests {
subordinate, err := ldapserver.ParseDN(test.subordinate)
if err != nil {
t.Fatalf("Error parsing subordinate DN: %s", err)
}
superior, err := ldapserver.ParseDN(test.superior)
if err != nil {
t.Fatalf("Error parsing superior DN: %s", err)
}
if subordinate.IsSubordinate(superior) != test.isSub {
t.Errorf("Expected %t, got %t for \"%s\" is subordinate of \"%s\"", test.isSub, !test.isSub, test.subordinate, test.superior)
}
}
}
func TestDNIsSibling(t *testing.T) {
type siblingTest struct {
dn1 string
dn2 string
isSib bool
}
siblingTests := []siblingTest{
{"uid=jdoe,ou=users,dc=example,dc=com", "uid=jdoe,ou=users,dc=example,dc=com", true},
{"ou=users,dc=example,dc=com", "uid=jdoe,ou=users,dc=example,dc=com", false},
{"uid=jdoe,ou=users,dc=example,dc=com", "ou=users,dc=example,dc=com", false},
{"ou=printers,dc=example,dc=com", "ou=users,dc=example,dc=com", true},
{"ou=users,dc=example,dc=com", "ou=printers,dc=example,dc=com", true},
{"ou=users,dc=example,dc=com", "ou=users,dc=example,dc=org", false},
{"ou=users,dc=example,dc=com", "ou=users,dc=example,dc=com,dc=org", false},
{"", "", true},
{"", "dc=com", false},
{"dc=com", "dc=org", true},
}
for _, test := range siblingTests {
dn1, err := ldapserver.ParseDN(test.dn1)
if err != nil {
t.Fatalf("Error parsing DN1: %s", err)
}
dn2, err := ldapserver.ParseDN(test.dn2)
if err != nil {
t.Fatalf("Error parsing DN2: %s", err)
}
if dn1.IsSibling(dn2) != test.isSib {
t.Errorf("Expected %t, got %t for \"%s\" is sibling of \"%s\"", test.isSib, !test.isSib, test.dn1, test.dn2)
}
}
}
func TestDNCommonAncestor(t *testing.T) {
type ancestorTest struct {
dn1 string
dn2 string
ca string
}
ancestorTests := []ancestorTest{
{"uid=jdoe,ou=users,dc=example,dc=com", "uid=jdoe,ou=users,dc=example,dc=com", "uid=jdoe,ou=users,dc=example,dc=com"},
{"uid=jdoe,ou=users,dc=example,dc=com", "ou=users,dc=example,dc=com", "ou=users,dc=example,dc=com"},
{"ou=users,dc=example,dc=com", "uid=jdoe,ou=users,dc=example,dc=com", "ou=users,dc=example,dc=com"},
{"ou=users,dc=example,dc=com", "ou=users,dc=example,dc=com", "ou=users,dc=example,dc=com"},
{"ou=users,dc=example,dc=com", "ou=printers,dc=example,dc=com", "dc=example,dc=com"},
{"ou=printers,dc=example,dc=com", "ou=users,dc=example,dc=com", "dc=example,dc=com"},
{"ou=users,dc=example,dc=com", "ou=users,dc=example,dc=org", ""},
{"ou=users,dc=example,dc=com", "ou=users,dc=example,dc=com,dc=org", ""},
{"", "", ""},
{"", "dc=com", ""},
{"dc=com", "dc=org", ""},
}
for _, test := range ancestorTests {
dn1, err := ldapserver.ParseDN(test.dn1)
if err != nil {
t.Fatalf("Error parsing DN1: %s", err)
}
dn2, err := ldapserver.ParseDN(test.dn2)
if err != nil {
t.Fatalf("Error parsing DN2: %s", err)
}
ca, err := ldapserver.ParseDN(test.ca)
if err != nil {
t.Fatalf("Error parsing common ancestor: %s", err)
}
dnca := dn1.CommonSuperior(dn2)
if !dnca.Equal(ca) {
t.Errorf("Expected \"%s\", got \"%s\" for common ancestor of \"%s\" and \"%s\"", ca, dnca, test.dn1, test.dn2)
}
}
}
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)
}
}
}