forked from mkouhei/openssh-ldap-pubkey
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathldap_test.go
173 lines (149 loc) · 7.1 KB
/
ldap_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
package main
import (
"testing"
)
func TestIsAddrWithV4(t *testing.T) {
addr := "192.0.2.100"
if !isAddr(addr) {
t.Fatalf("expecting: %s is true, but false", addr)
}
}
func TestIsAddrWithV6(t *testing.T) {
addr := "2001:db8::100"
if !isAddr(addr) {
t.Fatalf("expecting: %s is true, but false", addr)
}
}
func TestIsAddrWithFQDN(t *testing.T) {
host := "ldap.example.org"
if isAddr(host) {
t.Fatalf("expecting: %s is false, but true", host)
}
}
func TestConnect(t *testing.T) {
l := &ldapEnv{"localhost", 389, "dc=example,dc=org", defaultFilter, false, false, false, "", "", ""}
if _, err := l.connect(); err != nil {
t.Fatal("Connect error")
}
}
func TestConnectFail(t *testing.T) {
l := &ldapEnv{"localhost", 9999, "dc=example,dc=org", defaultFilter, false, false, false, "", "", ""}
if _, err := l.connect(); err == nil {
t.Fatal("expecting fail to error.")
}
}
func TestConnectTLS(t *testing.T) {
l := &ldapEnv{"localhost", 636, "dc=example,dc=org", defaultFilter, true, true, false, "", "", ""}
if _, err := l.connectTLS(); err != nil {
t.Fatal("Connect error")
}
}
func TestBind(t *testing.T) {
l := &ldapEnv{"localhost", 389, "dc=example,dc=org", defaultFilter, false, false, false, "", "cn=admin,dc=example,dc=org", "password"}
c, _ := l.connect()
if err := simpleBind(c, l); err != nil {
t.Fatal("Bind error")
}
}
func TestBindFail(t *testing.T) {
l := &ldapEnv{"localhost", 389, "dc=example,dc=org", defaultFilter, false, false, false, "", "cn=admin,dc=example,dc=org", ""}
c, _ := l.connect()
if err := simpleBind(c, l); err == nil {
t.Fatal("Bind error")
}
}
func Example_printPubkey() {
l := &ldapEnv{"localhost", 389, "dc=example,dc=org", defaultFilter, false, false, false, "user0", "", ""}
c, _ := l.connect()
simpleBind(c, l)
entries, _ := l.search(c)
printPubkey(entries)
// Output:
// ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC8OldtAiW9lQ0/2VJcc9UpRW9nfcusGXEu2sS+p5kh05zTYWGd8xHgZD0vfoQfpTfSKuHsL6qlMyKQMfsULWQoMJmMhJZc2hU1LH4u9HXYwJxD7EFleGTfxgYw6F6+LWHPVTTyhq+oMgXp/qfE4lc5A0xd2En9Qc172naHD+cRHZxhfNNYEGhW7E6eYm02Gn4fBN8hSpuZzv3WlpRgFiAWGv9CqObdQUEFFnpYLnC2kmaHqz8lzkZ9c3jdJMn2zPYyDAqQ52GI8EKyX9SrbepGJUaa/DmGyEg8nIBu4U74Sigfcl6dsJmA2qlOqSxia21mnQEFiSARB74pakgiywFV user0@workstation
// ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCrMQOAP3o58yl96HjEsheDAO/qgQ/mLVJK7DW+VFbJ9dGJpJfB4CBXPoT9bfSn4y6dotqjBA1eDbpDyzrhLkIe1MWZrRjkFbzAtB54ydKSU48URsb+XtGnN6kKKpipolQRvr3CRV7Yu2ELJDq+9Oz1gILK4nc1W/iaORVO/tZRPA0vdQwP0qkUf//neUmXXbSxOSm+ekQvZI9KfJ2tWxe+mVSFt+PcC2P4A/bW9dCNplqZdFTMQxLYFpl5ZOz3fwWcy34Shcb5nSZbjpKZdNrpuUCLwq2FMxorupko8kf4RmvMYO3G6p6OqpoIt6raB8DDJ+v/f6jdgPA31HK0sejX user0@vm01
}
func Example_printPubkeyTLS() {
l := &ldapEnv{"localhost", 636, "dc=example,dc=org", defaultFilter, true, true, false, "user0", "", ""}
c, _ := l.connectTLS()
simpleBind(c, l)
entries, _ := l.search(c)
printPubkey(entries)
// Output:
// ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC8OldtAiW9lQ0/2VJcc9UpRW9nfcusGXEu2sS+p5kh05zTYWGd8xHgZD0vfoQfpTfSKuHsL6qlMyKQMfsULWQoMJmMhJZc2hU1LH4u9HXYwJxD7EFleGTfxgYw6F6+LWHPVTTyhq+oMgXp/qfE4lc5A0xd2En9Qc172naHD+cRHZxhfNNYEGhW7E6eYm02Gn4fBN8hSpuZzv3WlpRgFiAWGv9CqObdQUEFFnpYLnC2kmaHqz8lzkZ9c3jdJMn2zPYyDAqQ52GI8EKyX9SrbepGJUaa/DmGyEg8nIBu4U74Sigfcl6dsJmA2qlOqSxia21mnQEFiSARB74pakgiywFV user0@workstation
// ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCrMQOAP3o58yl96HjEsheDAO/qgQ/mLVJK7DW+VFbJ9dGJpJfB4CBXPoT9bfSn4y6dotqjBA1eDbpDyzrhLkIe1MWZrRjkFbzAtB54ydKSU48URsb+XtGnN6kKKpipolQRvr3CRV7Yu2ELJDq+9Oz1gILK4nc1W/iaORVO/tZRPA0vdQwP0qkUf//neUmXXbSxOSm+ekQvZI9KfJ2tWxe+mVSFt+PcC2P4A/bW9dCNplqZdFTMQxLYFpl5ZOz3fwWcy34Shcb5nSZbjpKZdNrpuUCLwq2FMxorupko8kf4RmvMYO3G6p6OqpoIt6raB8DDJ+v/f6jdgPA31HK0sejX user0@vm01
}
func Example_printPubkeyDoesNotUseSSHPublicKey() {
l := &ldapEnv{"localhost", 389, "dc=example,dc=org", defaultFilter, false, false, false, "user2", "", ""}
c, _ := l.connect()
simpleBind(c, l)
entries, _ := l.search(c)
printPubkey(entries)
// Output:
//
}
func Example_printPubkeyDoesNotExistUser() {
l := &ldapEnv{"localhost", 389, "dc=example,dc=org", defaultFilter, false, false, false, "user5", "", ""}
c, _ := l.connect()
simpleBind(c, l)
entries, _ := l.search(c)
printPubkey(entries)
// Output:
//
}
func TestGetHost(t *testing.T) {
requestHost := "example.net"
l := &ldapEnv{requestHost, 389, "dc=example,dc=org", defaultFilter, false, false, false, "user5", "", ""}
if host, err := l.getHost(); host != requestHost && err != nil {
t.Fatalf("expected: %s, but got: %s", requestHost, host)
}
requestHost = "localhost"
l = &ldapEnv{requestHost, 389, "dc=example,dc=org", defaultFilter, false, false, false, "user1", "", ""}
if host, err := l.getHost(); host != requestHost && err != nil {
t.Fatalf("expected: %s, but got: %s", requestHost, host)
}
requestHost = "2001:db8::192:0:2:100"
l = &ldapEnv{requestHost, 389, "dc=example,dc=org", defaultFilter, false, false, false, "user1", "", ""}
if host, err := l.getHost(); host != "[%s]" && err != nil {
t.Fatalf("expected: [%s], but got: %s", requestHost, host)
}
requestHost = "2001:db8::192:0:2:100%enp0s0"
l = &ldapEnv{requestHost, 389, "dc=example,dc=org", defaultFilter, false, false, false, "user1", "", ""}
if host, err := l.getHost(); host != "" && err == nil {
t.Fatalf("expected error: 'invalid host / IP address', but got: %s", host)
}
requestHost = "fe80::192:0:2:100%enp0s0"
l = &ldapEnv{requestHost, 389, "dc=example,dc=org", defaultFilter, false, false, false, "user1", "", ""}
if host, err := l.getHost(); host != "[%s]" && err != nil {
t.Fatalf("expected: [%s], but got: %s", requestHost, host)
}
requestHost = "[2001:db8::192:0:2:100]"
l = &ldapEnv{requestHost, 389, "dc=example,dc=org", defaultFilter, false, false, false, "user1", "", ""}
if host, err := l.getHost(); host != "" && err == nil {
t.Fatalf("expected error: 'invalid host / IP address', but got: %s", host)
}
requestHost = "192.0.2.100"
l = &ldapEnv{requestHost, 389, "dc=example,dc=org", defaultFilter, false, false, false, "user1", "", ""}
if host, err := l.getHost(); host != requestHost || err != nil {
t.Fatalf("expected: %s, but got: %s", requestHost, host)
}
requestHost = "192.168.1"
l = &ldapEnv{requestHost, 389, "dc=example,dc=org", defaultFilter, false, false, false, "user1", "", ""}
if host, err := l.getHost(); host != "" && err == nil {
t.Fatalf("expected error: 'invalid host / IP address', but got: %s", host)
}
requestHost = "invalid:host"
l = &ldapEnv{requestHost, 389, "dc=example,dc=org", defaultFilter, false, false, false, "user1", "", ""}
if host, err := l.getHost(); host != "" && err == nil {
t.Fatalf("expected error: 'invalid host / IP address', but got: %s", host)
}
requestHost = "invalid_host"
l = &ldapEnv{requestHost, 389, "dc=example,dc=org", defaultFilter, false, false, false, "user1", "", ""}
if host, err := l.getHost(); host != "" && err == nil {
t.Fatalf("expected error: 'invalid host / IP address', but got: %s", host)
}
requestHost = "invalid host"
l = &ldapEnv{requestHost, 389, "dc=example,dc=org", defaultFilter, false, false, false, "user1", "", ""}
if host, err := l.getHost(); host != "" && err == nil {
t.Fatalf("expected error: 'invalid host / IP address', but got: %s", host)
}
}