-
Notifications
You must be signed in to change notification settings - Fork 29
/
group_test.go
226 lines (180 loc) · 5.66 KB
/
group_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
package auth
import (
"sort"
"strings"
"testing"
)
func dnToCN(dn string) string {
if splits := strings.Split(dn, ","); len(splits) >= 1 {
if splits2 := strings.Split(splits[0], "="); len(splits2) >= 2 {
return splits2[1]
}
}
return ""
}
func TestConnGroupDN(t *testing.T) {
if testConfig.Server == "" {
t.Skip("ADTEST_SERVER not set")
return
}
if testConfig.BindUPN == "" || testConfig.BindPass == "" {
t.Skip("ADTEST_BIND_UPN or ADTEST_BIND_PASS not set")
return
}
if testConfig.BaseDN == "" {
t.Skip("ADTEST_BASEDN not set")
return
}
config := &Config{Server: testConfig.Server, Port: testConfig.Port, Security: testConfig.BindSecurity, BaseDN: testConfig.BaseDN}
conn, err := config.Connect()
if err != nil {
t.Fatal("Error connecting to server:", err)
}
defer conn.Conn.Close()
status, err := conn.Bind(testConfig.BindUPN, testConfig.BindPass)
if err != nil {
t.Fatal("Error binding to server:", err)
}
if !status {
t.Fatal("Error binding to server: invalid credentials")
}
entry, err := conn.GetAttributes("userPrincipalName", testConfig.BindUPN, []string{"memberOf"})
if err != nil {
t.Fatal("Error getting user groups:", err)
}
dnGroups := entry.GetAttributeValues("memberOf")
if len(dnGroups) == 0 {
t.Skip("BIND_UPN user not member of any groups")
return
}
groupDN, err := conn.GroupDN(dnGroups[0])
if err != nil {
t.Error("Expected err to be nil but got:", err)
}
if dnGroups[0] != groupDN {
t.Errorf("Expected returned group (%s) to be equal to the searched group (%s)", groupDN, dnGroups[0])
}
cn := dnToCN(dnGroups[0])
if cn == "" {
t.Fatal("Error getting group cn: cn not found")
}
groupDN, err = conn.GroupDN(cn)
if err != nil {
t.Error("Expected err to be nil but got:", err)
}
if dnGroups[0] != groupDN {
t.Errorf(`Expected DN to be "%s" but got "%s"`, dnGroups[0], groupDN)
}
}
func TestConnObjectGroups(t *testing.T) {
if testConfig.Server == "" {
t.Skip("ADTEST_SERVER not set")
return
}
if testConfig.BindUPN == "" || testConfig.BindPass == "" {
t.Skip("ADTEST_BIND_UPN or ADTEST_BIND_PASS not set")
return
}
if testConfig.BaseDN == "" {
t.Skip("ADTEST_BASEDN not set")
return
}
config := &Config{Server: testConfig.Server, Port: testConfig.Port, Security: testConfig.BindSecurity, BaseDN: testConfig.BaseDN}
conn, err := config.Connect()
if err != nil {
t.Fatal("Error connecting to server:", err)
}
defer conn.Conn.Close()
status, err := conn.Bind(testConfig.BindUPN, testConfig.BindPass)
if err != nil {
t.Fatal("Error binding to server:", err)
}
if !status {
t.Fatal("Error binding to server: invalid credentials")
}
entry, err := conn.GetAttributes("userPrincipalName", testConfig.BindUPN, []string{"memberOf"})
if err != nil {
t.Fatal("Error getting user groups:", err)
}
dnGroups := entry.GetAttributeValues("memberOf")
if len(dnGroups) == 0 {
t.Skip("BIND_UPN user not member of any groups")
return
}
if _, err = conn.ObjectGroups("objectClass", "false", dnGroups); !strings.HasSuffix(err.Error(), "no entries returned") {
t.Error("No entries: Expected no entries search error but got:", err)
}
userGroups, err := conn.ObjectGroups("userPrincipalName", testConfig.BindUPN, dnGroups)
if err != nil {
t.Fatal("Expected err to be nil but got:", err)
}
sort.Strings(dnGroups)
sort.Strings(userGroups)
if len(dnGroups) != len(userGroups) {
t.Errorf("Expected returned group count (%d) to be equal to searched group count (%d)", len(userGroups), len(dnGroups))
}
for i := range dnGroups {
if dnGroups[i] != userGroups[i] {
t.Fatalf("Expected returned group (%s) to be equal to searched group (%s):", userGroups[i], dnGroups[i])
}
}
userGroups, err = conn.ObjectGroups("dn", entry.DN, dnGroups)
if err != nil {
t.Fatal("Using DN: Expected err to be nil but got:", err)
}
sort.Strings(userGroups)
if len(dnGroups) != len(userGroups) {
t.Errorf("Using DN: Expected returned group count (%d) to be equal to searched group count (%d)", len(userGroups), len(dnGroups))
}
for i := range dnGroups {
if dnGroups[i] != userGroups[i] {
t.Fatalf("Using DN: Expected returned group (%s) to be equal to searched group (%s):", userGroups[i], dnGroups[i])
}
}
}
func TestConnObjectPrimaryGroup(t *testing.T) {
if testConfig.Server == "" {
t.Skip("ADTEST_SERVER not set")
return
}
if testConfig.BindUPN == "" || testConfig.BindPass == "" {
t.Skip("ADTEST_BIND_UPN or ADTEST_BIND_PASS not set")
return
}
if testConfig.BaseDN == "" {
t.Skip("ADTEST_BASEDN not set")
return
}
config := &Config{Server: testConfig.Server, Port: testConfig.Port, Security: testConfig.BindSecurity, BaseDN: testConfig.BaseDN}
conn, err := config.Connect()
if err != nil {
t.Fatal("Error connecting to server:", err)
}
defer conn.Conn.Close()
status, err := conn.Bind(testConfig.BindUPN, testConfig.BindPass)
if err != nil {
t.Fatal("Error binding to server:", err)
}
if !status {
t.Fatal("Error binding to server: invalid credentials")
}
entry, err := conn.GetAttributes("userPrincipalName", testConfig.BindUPN, []string{"memberOf"})
if err != nil {
t.Fatal("Error getting user groups:", err)
}
dnGroups := entry.GetAttributeValues("memberOf")
if len(dnGroups) == 0 {
t.Skip("BIND_UPN user not member of any groups")
return
}
if _, err = conn.ObjectPrimaryGroup("objectClass", "false"); !strings.HasSuffix(err.Error(), "no entries returned") {
t.Error("No entries: Expected no entries search error but got:", err)
}
dn, err := conn.ObjectPrimaryGroup("userPrincipalName", testConfig.BindUPN)
if err != nil {
t.Fatal("Expected err to be nil but got:", err)
}
if dn == "" {
t.Error("Expected to primary group dn to not be empty")
}
}