forked from armon/go-socks5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth_test.go
88 lines (71 loc) · 1.98 KB
/
auth_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
package socks5
import (
"bytes"
"testing"
)
func TestNoAuth(t *testing.T) {
req := bytes.NewBuffer(nil)
req.Write([]byte{1, noAuth})
var resp bytes.Buffer
s, _ := New(&Config{})
if err := s.authenticate(&resp, req); err != nil {
t.Fatalf("err: %v", err)
}
out := resp.Bytes()
if !bytes.Equal(out, []byte{socks5Version, noAuth}) {
t.Fatalf("bad: %v", out)
}
}
func TestPasswordAuth_Valid(t *testing.T) {
req := bytes.NewBuffer(nil)
req.Write([]byte{2, noAuth, userPassAuth})
req.Write([]byte{1, 3, 'f', 'o', 'o', 3, 'b', 'a', 'r'})
var resp bytes.Buffer
cred := StaticCredentials{
"foo": "bar",
}
cator := UserPassAuthenticator{Credentials: cred}
s, _ := New(&Config{AuthMethods:[]Authenticator{cator}})
if err := s.authenticate(&resp, req); err != nil {
t.Fatalf("err: %v", err)
}
out := resp.Bytes()
if !bytes.Equal(out, []byte{socks5Version, userPassAuth, 1, authSuccess}) {
t.Fatalf("bad: %v", out)
}
}
func TestPasswordAuth_Invalid(t *testing.T) {
req := bytes.NewBuffer(nil)
req.Write([]byte{2, noAuth, userPassAuth})
req.Write([]byte{1, 3, 'f', 'o', 'o', 3, 'b', 'a', 'z'})
var resp bytes.Buffer
cred := StaticCredentials{
"foo": "bar",
}
cator := UserPassAuthenticator{Credentials: cred}
s, _ := New(&Config{AuthMethods:[]Authenticator{cator}})
if err := s.authenticate(&resp, req); err != UserAuthFailed {
t.Fatalf("err: %v", err)
}
out := resp.Bytes()
if !bytes.Equal(out, []byte{socks5Version, userPassAuth, 1, authFailure}) {
t.Fatalf("bad: %v", out)
}
}
func TestNoSupportedAuth(t *testing.T) {
req := bytes.NewBuffer(nil)
req.Write([]byte{1, noAuth})
var resp bytes.Buffer
cred := StaticCredentials{
"foo": "bar",
}
cator := UserPassAuthenticator{Credentials: cred}
s, _ := New(&Config{AuthMethods:[]Authenticator{cator}})
if err := s.authenticate(&resp, req); err != NoSupportedAuth {
t.Fatalf("err: %v", err)
}
out := resp.Bytes()
if !bytes.Equal(out, []byte{socks5Version, noAcceptable}) {
t.Fatalf("bad: %v", out)
}
}