Debian Codegraph @

internal/ldap/dn_test.go

raw
1 // Copyright (c) 2011-2015 Michael Mitton ([email protected])
2 // Portions copyright (c) 2015-2016 go-ldap Authors
3 // Static-Check Fixes Copyright 2024 The NATS Authors
4
5 package ldap
6
7 import (
8 "reflect"
9 "testing"
10 )
11
12 func TestSuccessfulDNParsing(t *testing.T) {
13 testcases := map[string]DN{
14 "": {[]*RelativeDN{}},
15 "cn=Jim\\2C \\22Hasse Hö\\22 Hansson!,dc=dummy,dc=com": {[]*RelativeDN{
16 {[]*AttributeTypeAndValue{{"cn", "Jim, \"Hasse Hö\" Hansson!"}}},
17 {[]*AttributeTypeAndValue{{"dc", "dummy"}}},
18 {[]*AttributeTypeAndValue{{"dc", "com"}}}}},
19 "UID=jsmith,DC=example,DC=net": {[]*RelativeDN{
20 {[]*AttributeTypeAndValue{{"UID", "jsmith"}}},
21 {[]*AttributeTypeAndValue{{"DC", "example"}}},
22 {[]*AttributeTypeAndValue{{"DC", "net"}}}}},
23 "OU=Sales+CN=J. Smith,DC=example,DC=net": {[]*RelativeDN{
24 {[]*AttributeTypeAndValue{
25 {"OU", "Sales"},
26 {"CN", "J. Smith"}}},
27 {[]*AttributeTypeAndValue{{"DC", "example"}}},
28 {[]*AttributeTypeAndValue{{"DC", "net"}}}}},
29 //
30 // "1.3.6.1.4.1.1466.0=#04024869": {[]*RelativeDN{
31 // {[]*AttributeTypeAndValue{{"1.3.6.1.4.1.1466.0", "Hi"}}}}},
32 // "1.3.6.1.4.1.1466.0=#04024869,DC=net": {[]*RelativeDN{
33 // {[]*AttributeTypeAndValue{{"1.3.6.1.4.1.1466.0", "Hi"}}},
34 // {[]*AttributeTypeAndValue{{"DC", "net"}}}}},
35 "CN=Lu\\C4\\8Di\\C4\\87": {[]*RelativeDN{
36 {[]*AttributeTypeAndValue{{"CN", "Lučić"}}}}},
37 " CN = Lu\\C4\\8Di\\C4\\87 ": {[]*RelativeDN{
38 {[]*AttributeTypeAndValue{{"CN", "Lučić"}}}}},
39 ` A = 1 , B = 2 `: {[]*RelativeDN{
40 {[]*AttributeTypeAndValue{{"A", "1"}}},
41 {[]*AttributeTypeAndValue{{"B", "2"}}}}},
42 ` A = 1 + B = 2 `: {[]*RelativeDN{
43 {[]*AttributeTypeAndValue{
44 {"A", "1"},
45 {"B", "2"}}}}},
46 ` \ \ A\ \ = \ \ 1\ \ , \ \ B\ \ = \ \ 2\ \ `: {[]*RelativeDN{
47 {[]*AttributeTypeAndValue{{" A ", " 1 "}}},
48 {[]*AttributeTypeAndValue{{" B ", " 2 "}}}}},
49 ` \ \ A\ \ = \ \ 1\ \ + \ \ B\ \ = \ \ 2\ \ `: {[]*RelativeDN{
50 {[]*AttributeTypeAndValue{
51 {" A ", " 1 "},
52 {" B ", " 2 "}}}}},
53 }
54
55 for test, answer := range testcases {
56 dn, err := ParseDN(test)
57 if err != nil {
58 t.Error(err.Error())
59 continue
60 }
61 if !reflect.DeepEqual(dn, &answer) {
62 t.Errorf("Parsed DN %s is not equal to the expected structure", test)
63 t.Logf("Expected:")
64 for _, rdn := range answer.RDNs {
65 for _, attribs := range rdn.Attributes {
66 t.Logf("#%v\n", attribs)
67 }
68 }
69 t.Logf("Actual:")
70 for _, rdn := range dn.RDNs {
71 for _, attribs := range rdn.Attributes {
72 t.Logf("#%v\n", attribs)
73 }
74 }
75 }
76 }
77 }
78
79 func TestErrorDNParsing(t *testing.T) {
80 testcases := map[string]string{
81 "*": "DN ended with incomplete type, value pair",
82 "cn=Jim\\0Test": "failed to decode escaped character: encoding/hex: invalid byte: U+0054 'T'",
83 "cn=Jim\\0": "got corrupted escaped character",
84 "DC=example,=net": "DN ended with incomplete type, value pair",
85 // "1=#0402486": "failed to decode BER encoding: encoding/hex: odd length hex string",
86 "test,DC=example,DC=com": "incomplete type, value pair",
87 "=test,DC=example,DC=com": "incomplete type, value pair",
88 }
89
90 for test, answer := range testcases {
91 _, err := ParseDN(test)
92 if err == nil {
93 t.Errorf("Expected %s to fail parsing but succeeded\n", test)
94 } else if err.Error() != answer {
95 t.Errorf("Unexpected error on %s:\n%s\nvs.\n%s\n", test, answer, err.Error())
96 }
97 }
98 }
99
100 func TestDNEqual(t *testing.T) {
101 testcases := []struct {
102 A string
103 B string
104 Equal bool
105 }{
106 // Exact match
107 {"", "", true},
108 {"o=A", "o=A", true},
109 {"o=A", "o=B", false},
110
111 {"o=A,o=B", "o=A,o=B", true},
112 {"o=A,o=B", "o=A,o=C", false},
113
114 {"o=A+o=B", "o=A+o=B", true},
115 {"o=A+o=B", "o=A+o=C", false},
116
117 // Case mismatch in type is ignored
118 {"o=A", "O=A", true},
119 {"o=A,o=B", "o=A,O=B", true},
120 {"o=A+o=B", "o=A+O=B", true},
121
122 // Case mismatch in value is significant
123 {"o=a", "O=A", false},
124 {"o=a,o=B", "o=A,O=B", false},
125 {"o=a+o=B", "o=A+O=B", false},
126
127 // Multi-valued RDN order mismatch is ignored
128 {"o=A+o=B", "O=B+o=A", true},
129 // Number of RDN attributes is significant
130 {"o=A+o=B", "O=B+o=A+O=B", false},
131
132 // Missing values are significant
133 {"o=A+o=B", "O=B+o=A+O=C", false}, // missing values matter
134 {"o=A+o=B+o=C", "O=B+o=A", false}, // missing values matter
135
136 // Whitespace tests
137 // Matching
138 {
139 "cn=John Doe, ou=People, dc=sun.com",
140 "cn=John Doe, ou=People, dc=sun.com",
141 true,
142 },
143 // Difference in leading/trailing chars is ignored
144 {
145 "cn=John Doe, ou=People, dc=sun.com",
146 "cn=John Doe,ou=People,dc=sun.com",
147 true,
148 },
149 // Difference in values is significant
150 {
151 "cn=John Doe, ou=People, dc=sun.com",
152 "cn=John Doe, ou=People, dc=sun.com",
153 false,
154 },
155 }
156
157 for i, tc := range testcases {
158 a, err := ParseDN(tc.A)
159 if err != nil {
160 t.Errorf("%d: %v", i, err)
161 continue
162 }
163 b, err := ParseDN(tc.B)
164 if err != nil {
165 t.Errorf("%d: %v", i, err)
166 continue
167 }
168 if expected, actual := tc.Equal, a.Equal(b); expected != actual {
169 t.Errorf("%d: when comparing '%s' and '%s' expected %v, got %v", i, tc.A, tc.B, expected, actual)
170 continue
171 }
172 if expected, actual := tc.Equal, b.Equal(a); expected != actual {
173 t.Errorf("%d: when comparing '%s' and '%s' expected %v, got %v", i, tc.A, tc.B, expected, actual)
174 continue
175 }
176 }
177 }
178
179 func TestDNAncestor(t *testing.T) {
180 testcases := []struct {
181 A string
182 B string
183 Ancestor bool
184 }{
185 // Exact match returns false
186 {"", "", false},
187 {"o=A", "o=A", false},
188 {"o=A,o=B", "o=A,o=B", false},
189 {"o=A+o=B", "o=A+o=B", false},
190
191 // Mismatch
192 {"ou=C,ou=B,o=A", "ou=E,ou=D,ou=B,o=A", false},
193
194 // Descendant
195 {"ou=C,ou=B,o=A", "ou=E,ou=C,ou=B,o=A", true},
196 }
197
198 for i, tc := range testcases {
199 a, err := ParseDN(tc.A)
200 if err != nil {
201 t.Errorf("%d: %v", i, err)
202 continue
203 }
204 b, err := ParseDN(tc.B)
205 if err != nil {
206 t.Errorf("%d: %v", i, err)
207 continue
208 }
209 if expected, actual := tc.Ancestor, a.AncestorOf(b); expected != actual {
210 t.Errorf("%d: when comparing '%s' and '%s' expected %v, got %v", i, tc.A, tc.B, expected, actual)
211 continue
212 }
213 }
214 }
215
216 func TestDNReverseMatchDuplicates(t *testing.T) {
217 // A has two CN=Alice RDNs and one O=NATS.
218 // B has one CN=Alice, one O=NATS, and one OU=Users.
219 // They have the same length (3).
220 dnA, _ := ParseDN("CN=Alice,CN=Alice,O=NATS")
221 dnB, _ := ParseDN("CN=Alice,O=NATS,OU=Users")
222 if dnA.RDNsMatch(dnB) {
223 t.Fatal("Expected RDNsMatch to return false for different RDN sets, but it returned true")
224 }
225 }