-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilter_test.go
102 lines (98 loc) · 2.81 KB
/
filter_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
package ldaps
import (
"testing"
"github.com/go-ldap/ldap/v3"
)
func TestGetFilterAttribute(t *testing.T) {
for _, testInfo := range []struct {
Filter string
Attribute string
Expected string
}{
{
Filter: "(objectClass=*)",
Attribute: "objectclass",
Expected: "",
},
{
Filter: "(objectClass=posixAccount)",
Attribute: "objectClass",
Expected: "posixaccount",
},
{
Filter: "(&(cn=awesome)(objectClass=posixGroup))",
Attribute: "objectClass",
Expected: "posixgroup",
},
} {
value, err := GetFilterAttribute(testInfo.Filter, testInfo.Attribute)
if err != nil {
t.Errorf("GetFilterAttribute failed: %v", err)
}
if value != testInfo.Expected {
t.Errorf("GetFilterAttribute: Expected %q got %q", testInfo.Expected, value)
}
}
}
func TestApplyFilter(t *testing.T) {
for _, testInfo := range []struct {
Filter string
Entry *ldap.Entry
Expected bool
}{
{
Filter: "(objectClass=*)",
Entry: ldap.NewEntry("cn=test,ou=users,dc=example,dc=org", map[string][]string{"objectclass": {"User"}}),
Expected: true,
},
{
Filter: "(memberOf=cn=*sers,ou=groups,dc=example,dc=org)",
Entry: ldap.NewEntry("cn=test,ou=users,dc=example,dc=org", map[string][]string{
"objectclass": {"User"},
"memberOf": {"cn=users,ou=groups,dc=example,dc=org","cn=admin-users,ou=groups,dc=example,dc=org"},
}),
Expected: true,
},
{
Filter: "(memberOf=cn=admin-*,ou=groups,dc=example,dc=org)",
Entry: ldap.NewEntry("cn=test,ou=users,dc=example,dc=org", map[string][]string{
"objectclass": {"User"},
"memberOf": {"cn=users,ou=groups,dc=example,dc=org","cn=admin-users,ou=groups,dc=example,dc=org"},
}),
Expected: true,
},
{
Filter: "(memberOf=cn=admin*user,ou=groups,dc=example,dc=org)",
Entry: ldap.NewEntry("cn=test,ou=users,dc=example,dc=org", map[string][]string{
"objectclass": {"User"},
"memberOf": {"cn=users,ou=groups,dc=example,dc=org","cn=admin-users,ou=groups,dc=example,dc=org"},
}),
Expected: false,
},
{
Filter: "(memberOf=cn=*sers,ou=groups,dc=example,dc=org)",
Entry: ldap.NewEntry("cn=test,ou=users,dc=example,dc=org", map[string][]string{
"objectclass": {"User"},
"memberOf": {"cn=admins,ou=groups,dc=example,dc=org"},
}),
Expected: false,
},
} {
berFilter, err := ldap.CompileFilter(testInfo.Filter)
if err != nil {
t.Errorf("Compiling the filter failed: %v", err)
}
matched, ldapResult := ApplyFilter(berFilter, testInfo.Entry)
var resultCode uint16 = 0
if ldapResult != nil {
resultCode = ldapResult.ResultCode
}
if matched != testInfo.Expected {
status := "did not match"
if matched {
status = "matched"
}
t.Errorf("Entry: %v %s: %q return code: %v", testInfo.Entry, status, testInfo.Filter, ldap.LDAPResultCodeMap[resultCode])
}
}
}