forked from go-ldap/ldap
-
Notifications
You must be signed in to change notification settings - Fork 1
/
search_test.go
171 lines (149 loc) · 3.73 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
package ldap
import (
"reflect"
"testing"
"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,
},
// 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"),
},
},
},
}
type User struct {
Dn string `ldap:"dn"`
Cn string `ldap:"cn"`
Mail string `ldap:"mail"`
ID int `ldap:"id"`
LongID int64 `ldap:"longId"`
Data []byte `ldap:"data"`
}
expect := &User{
Dn: "cn=mario,ou=Users,dc=go-ldap,dc=github,dc=com",
Cn: "mario",
Mail: "[email protected]",
ID: 2147483647,
LongID: 9223372036854775807,
Data: []byte("data"),
}
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)
})
}