forked from BeryJu/ldap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver_modify_test.go
More file actions
134 lines (120 loc) · 4.68 KB
/
Copy pathserver_modify_test.go
File metadata and controls
134 lines (120 loc) · 4.68 KB
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
package ldap
import (
"context"
"os/exec"
"strings"
"testing"
)
func TestAdd(t *testing.T) {
s := NewServer()
s.BindFunc("", modifyTestHandler{})
s.AddFunc("", modifyTestHandler{})
LaunchServerForTest(t, s, func() {
cmd := exec.Command("ldapadd", "-v", "-H", ldapURL, "-x", "-f", "tests/add.ldif")
out, _ := cmd.CombinedOutput()
if !strings.Contains(string(out), "modify complete") {
t.Errorf("ldapadd failed: %v", string(out))
}
cmd = exec.Command("ldapadd", "-v", "-H", ldapURL, "-x", "-f", "tests/add2.ldif")
out, _ = cmd.CombinedOutput()
if !strings.Contains(string(out), "ldap_add: Insufficient access") {
t.Errorf("ldapadd should have failed: %v", string(out))
}
if strings.Contains(string(out), "modify complete") {
t.Errorf("ldapadd should have failed: %v", string(out))
}
})
}
func TestDelete(t *testing.T) {
s := NewServer()
s.BindFunc("", modifyTestHandler{})
s.DeleteFunc("", modifyTestHandler{})
LaunchServerForTest(t, s, func() {
cmd := exec.Command("ldapdelete", "-v", "-H", ldapURL, "-x", "cn=Delete Me,dc=example,dc=com")
out, _ := cmd.CombinedOutput()
if !strings.Contains(string(out), "Delete Result: Success (0)") || !strings.Contains(string(out), "Additional info: Success") {
t.Errorf("ldapdelete failed: %v", string(out))
}
cmd = exec.Command("ldapdelete", "-v", "-H", ldapURL, "-x", "cn=Bob,dc=example,dc=com")
out, _ = cmd.CombinedOutput()
if strings.Contains(string(out), "Success") || !strings.Contains(string(out), "ldap_delete: Insufficient access") {
t.Errorf("ldapdelete should have failed: %v", string(out))
}
})
}
func TestModify(t *testing.T) {
s := NewServer()
s.BindFunc("", modifyTestHandler{})
s.ModifyFunc("", modifyTestHandler{})
LaunchServerForTest(t, s, func() {
cmd := exec.Command("ldapmodify", "-v", "-H", ldapURL, "-x", "-f", "tests/modify.ldif")
out, _ := cmd.CombinedOutput()
if !strings.Contains(string(out), "modify complete") {
t.Errorf("ldapmodify failed: %v", string(out))
}
cmd = exec.Command("ldapmodify", "-v", "-H", ldapURL, "-x", "-f", "tests/modify2.ldif")
out, _ = cmd.CombinedOutput()
if !strings.Contains(string(out), "ldap_modify: Insufficient access") || strings.Contains(string(out), "modify complete") {
t.Errorf("ldapmodify should have failed: %v", string(out))
}
})
}
/*
func TestModifyDN(t *testing.T) {
s := NewServer()
s.BindFunc("", modifyTestHandler{})
s.AddFunc("", modifyTestHandler{})
LaunchServerForTest(t, s, func() {
cmd := exec.Command("ldapadd", "-v", "-H", ldapURL, "-x", "-f", "tests/add.ldif")
//ldapmodrdn -H ldap://localhost:3389 -x "uid=babs,dc=example,dc=com" "uid=babsy,dc=example,dc=com"
out, _ := cmd.CombinedOutput()
if !strings.Contains(string(out), "modify complete") {
t.Errorf("ldapadd failed: %v", string(out))
}
cmd = exec.Command("ldapadd", "-v", "-H", ldapURL, "-x", "-f", "tests/add2.ldif")
out, _ = cmd.CombinedOutput()
if !strings.Contains(string(out), "ldap_add: Insufficient access") {
t.Errorf("ldapadd should have failed: %v", string(out))
}
if strings.Contains(string(out), "modify complete") {
t.Errorf("ldapadd should have failed: %v", string(out))
}
})
}
*/
type modifyTestHandler struct{}
func (h modifyTestHandler) Bind(ctx context.Context, bindDN, bindSimplePw string) (LDAPResultCode, context.Context, error) {
if bindDN == "" && bindSimplePw == "" {
return LDAPResultSuccess, ctx, nil
}
return LDAPResultInvalidCredentials, ctx, nil
}
func (h modifyTestHandler) Add(ctx context.Context, req AddRequest) (LDAPResultCode, error) {
// only succeed on expected contents of add.ldif:
if len(req.Attributes) == 5 && req.DN == "cn=Barbara Jensen,dc=example,dc=com" &&
req.Attributes[2].Type == "sn" && len(req.Attributes[2].Vals) == 1 &&
req.Attributes[2].Vals[0] == "Jensen" {
return LDAPResultSuccess, nil
}
return LDAPResultInsufficientAccessRights, nil
}
func (h modifyTestHandler) Delete(ctx context.Context, deleteDN string) (LDAPResultCode, error) {
// only succeed on expected deleteDN
if deleteDN == "cn=Delete Me,dc=example,dc=com" {
return LDAPResultSuccess, nil
}
return LDAPResultInsufficientAccessRights, nil
}
func (h modifyTestHandler) Modify(ctx context.Context, req ModifyRequest) (LDAPResultCode, error) {
add, replace, del := ModifiedAttributes(req)
// only succeed on expected contents of modify.ldif:
if req.DN == "cn=testy,dc=example,dc=com" && len(add) == 1 &&
len(del) == 3 && len(replace) == 2 &&
del[2].Type == "details" && len(del[2].Vals) == 0 {
return LDAPResultSuccess, nil
}
return LDAPResultInsufficientAccessRights, nil
}
func (h modifyTestHandler) ModifyDN(ctx context.Context, req ModifyDNRequest) (LDAPResultCode, error) {
return LDAPResultInsufficientAccessRights, nil
}