forked from go-ldap/ldap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
search_test.go
221 lines (198 loc) · 5.11 KB
/
search_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
package ldap
import (
"reflect"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
// TestNewEntry tests that repeated calls to NewEntry return the same value with the same input
func TestNewEntry(t *testing.T) {
dn := "testDN"
attributes := map[string][]string{
"alpha": {"value"},
"beta": {"value"},
"gamma": {"value"},
"delta": {"value"},
"epsilon": {"value"},
}
executedEntry := NewEntry(dn, attributes)
iteration := 0
for {
if iteration == 100 {
break
}
testEntry := NewEntry(dn, attributes)
if !reflect.DeepEqual(executedEntry, testEntry) {
t.Fatalf("subsequent calls to NewEntry did not yield the same result:\n\texpected:\n\t%v\n\tgot:\n\t%v\n", executedEntry, testEntry)
}
iteration = iteration + 1
}
}
func TestGetAttributeValue(t *testing.T) {
dn := "testDN"
attributes := map[string][]string{
"Alpha": {"value"},
"bEta": {"value"},
"gaMma": {"value"},
"delTa": {"value"},
"epsiLon": {"value"},
}
entry := NewEntry(dn, attributes)
if entry.GetAttributeValue("Alpha") != "value" {
t.Errorf("failed to get attribute in original case")
}
if entry.GetEqualFoldAttributeValue("alpha") != "value" {
t.Errorf("failed to get attribute in changed case")
}
}
func TestEntry_Unmarshal(t *testing.T) {
t.Run("passing a struct should fail", func(t *testing.T) {
entry := &Entry{}
type toStruct struct{}
result := toStruct{}
err := entry.Unmarshal(result)
assert.NotNil(t, err)
})
t.Run("passing a ptr to string should fail", func(t *testing.T) {
entry := &Entry{}
str := "foo"
err := entry.Unmarshal(&str)
assert.NotNil(t, err)
})
t.Run("user struct be decoded", func(t *testing.T) {
entry := &Entry{
DN: "cn=mario,ou=Users,dc=go-ldap,dc=github,dc=com",
Attributes: []*EntryAttribute{
{
Name: "cn",
Values: []string{"mario"},
ByteValues: nil,
},
{
Name: "mail",
Values: []string{"[email protected]"},
ByteValues: nil,
},
{
Name: "upn",
Values: []string{"[email protected]"},
ByteValues: nil,
},
// Tests int value.
{
Name: "id",
Values: []string{"2147483647"},
ByteValues: nil,
},
// Tests int64 value.
{
Name: "longId",
Values: []string{"9223372036854775807"},
ByteValues: nil,
},
// Tests []byte value.
{
Name: "data",
Values: []string{"data"},
ByteValues: [][]byte{
[]byte("data"),
},
},
// Tests time.Time value.
{
Name: "createdTimestamp",
Values: []string{"202305041930Z"},
ByteValues: nil,
},
// Tests *DN value
{
Name: "owner",
Values: []string{"uid=foo,dc=example,dc=org"},
ByteValues: nil,
},
// Tests []*DN value
{
Name: "children",
Values: []string{"uid=bar,dc=example,dc=org", "uid=baz,dc=example,dc=org"},
ByteValues: nil,
},
},
}
type User struct {
Dn string `ldap:"dn"`
Cn string `ldap:"cn"`
Mail string `ldap:"mail"`
UPN *string `ldap:"upn"`
ID int `ldap:"id"`
LongID int64 `ldap:"longId"`
Data []byte `ldap:"data"`
Created time.Time `ldap:"createdTimestamp"`
Owner *DN `ldap:"owner"`
Children []*DN `ldap:"children"`
}
created, err := time.Parse("200601021504Z", "202305041930Z")
if err != nil {
t.Errorf("failed to parse ref time: %s", err)
}
owner, err := ParseDN("uid=foo,dc=example,dc=org")
if err != nil {
t.Errorf("failed to parse ref DN: %s", err)
}
var children []*DN
for _, child := range []string{"uid=bar,dc=example,dc=org", "uid=baz,dc=example,dc=org"} {
dn, err := ParseDN(child)
if err != nil {
t.Errorf("failed to parse child ref DN: %s", err)
}
children = append(children, dn)
}
UPN := "[email protected]"
expect := &User{
Dn: "cn=mario,ou=Users,dc=go-ldap,dc=github,dc=com",
Cn: "mario",
Mail: "[email protected]",
UPN: &UPN,
ID: 2147483647,
LongID: 9223372036854775807,
Data: []byte("data"),
Created: created,
Owner: owner,
Children: children,
}
result := &User{}
err = entry.Unmarshal(result)
assert.Nil(t, err)
assert.Equal(t, expect, result)
})
t.Run("group struct be decoded", func(t *testing.T) {
entry := &Entry{
DN: "cn=DREAM_TEAM,ou=Groups,dc=go-ldap,dc=github,dc=com",
Attributes: []*EntryAttribute{
{
Name: "cn",
Values: []string{"DREAM_TEAM"},
ByteValues: nil,
},
{
Name: "member",
Values: []string{"mario", "luigi", "browser"},
ByteValues: nil,
},
},
}
type Group struct {
DN string `ldap:"dn" yaml:"dn" json:"dn"`
CN string `ldap:"cn" yaml:"cn" json:"cn"`
Members []string `ldap:"member"`
}
expect := &Group{
DN: "cn=DREAM_TEAM,ou=Groups,dc=go-ldap,dc=github,dc=com",
CN: "DREAM_TEAM",
Members: []string{"mario", "luigi", "browser"},
}
result := &Group{}
err := entry.Unmarshal(result)
assert.Nil(t, err)
assert.Equal(t, expect, result)
})
}