-
Notifications
You must be signed in to change notification settings - Fork 9
/
add_entry_test.go
116 lines (111 loc) · 2.58 KB
/
add_entry_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
//go:build test
package main
import (
"testing"
)
func TestAddEntryValidate(t *testing.T) {
testcases := []struct {
DN string
Attrs map[string][]string
ExpectedError error
}{
{
"cn=abc,ou=Users,dc=example,dc=com",
map[string][]string{
"cn": {"abc"},
"sn": {"efg"},
},
NewObjectClassViolation(),
},
{
"cn=abc,ou=Users,dc=example,dc=com",
map[string][]string{
"objectClass": {"inetOrgPerson"},
"cn": {"abc"},
},
NewObjectClassViolationRequiresAttribute("inetOrgPerson", "sn"),
},
{
"cn=abc,ou=Users,dc=example,dc=com",
map[string][]string{
"objectClass": {"inetOrgPerson"},
"cn": {"abc"},
"sn": {"efg"},
"displayName": {"hij"},
},
nil,
},
{
"cn=abc,ou=Users,dc=example,dc=com",
map[string][]string{
"objectClass": {"inetOrgPerson"},
"cn": {"abc"},
"sn": {"efg"},
"userPassword": {"hij"},
},
nil,
},
{
"cn=abc,ou=Users,dc=example,dc=com",
map[string][]string{
"objectClass": {"person"},
"cn": {"abc"},
"sn": {"efg"},
"displayName": {"hij"},
},
NewObjectClassViolationNotAllowed("displayName"),
},
{
"cn=abc,ou=Users,dc=example,dc=com",
map[string][]string{
"objectClass": {"uknown"},
"cn": {"abc"},
},
NewInvalidPerSyntax("objectClass", 0),
},
{
"cn=abc,ou=Users,dc=example,dc=com",
map[string][]string{
"objectClass": {"person", "uknown"},
"cn": {"abc"},
"sn": {"efg"},
},
NewInvalidPerSyntax("objectClass", 1),
},
}
server := NewServer(&ServerConfig{
Suffix: "dc=example,dc=com",
})
schemaMap := InitSchemaMap(server)
for i, tc := range testcases {
dn, err := ParseDN(schemaMap, tc.DN)
if err != nil {
t.Errorf("Unexpected error on %d:\nParse DN: %s, got error [%v]\n", i, tc.DN, err)
continue
}
entry := NewAddEntry(schemaMap, dn)
for k, v := range tc.Attrs {
err = entry.Add(k, v)
if err != nil {
break
}
}
if err != nil {
if tc.ExpectedError.Error() != err.Error() {
t.Errorf("Unexpected error on %d:\nError: [%v] expected, got error [%v]\n", i, tc.ExpectedError, err)
}
continue
}
err = entry.Validate()
if tc.ExpectedError == nil {
if err != nil {
t.Errorf("Unexpected error on %d:\nError: [%v] expected, got error [%v]\n", i, tc.ExpectedError, err)
}
continue
} else {
if tc.ExpectedError.Error() != err.Error() {
t.Errorf("Unexpected error on %d:\nError: [%v] expected, got error [%v]\n", i, tc.ExpectedError, err)
}
}
}
}