forked from go-zookeeper/zk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsasl_test.go
68 lines (50 loc) · 1.39 KB
/
sasl_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
package zk
import (
"strings"
"testing"
)
func TestDecode(t *testing.T) {
// Response is expected.
buf := `0000nonce="1x1",realm="2x2",rspauth="3x3"`
resp := &setSaslResponse{}
length, err := resp.Decode([]byte(buf))
if err != nil || length != len(buf) {
t.Errorf("failed to check Decode, %v", resp)
}
if resp.Nonce != "1x1" || resp.Realm != "2x2" {
t.Errorf("failed to check Decode, %v", resp)
}
if resp.RspAuth != `"3x3"` {
t.Errorf("failed to check Decode, %v", resp)
}
// Response is not expected.
buf = `0000nonce"1x1",realm="2x2",rspauth="3x3"`
resp = &setSaslResponse{}
_, err = resp.Decode([]byte(buf))
if err == nil {
t.Errorf("failed to check abnormal Decode, %v", resp)
}
}
func TestGenA1(t *testing.T) {
resp := setSaslResponse{}
resp.Realm = "test"
resp.Nonce = "1111"
hash := resp.genA1("super", "password", "1111")
if hash == "" {
t.Errorf("failed to genA1, %v", resp)
}
}
func TestGenSaslChallenge(t *testing.T) {
resp := setSaslResponse{}
resp.Realm = "zk-sasl-md5"
resp.Nonce = "qWkHmx+rW9vYQNysvUOCA3gWLks3u9cL5rc9JJFi"
auth := "super:admin"
hash, err := resp.GenSaslChallenge([]byte(auth), "140741146289")
if hash == "" || err != nil {
t.Errorf("failed to genA1, %v, error: %v", resp, err)
}
expect := "08125d12f8b89ca7dd8b5028b5cd7c3b"
if !strings.Contains(hash, expect) {
t.Errorf("failed to gen hash %s, expect %s.", hash, expect)
}
}