forked from things-go/go-socks5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth_test.go
63 lines (50 loc) · 1.58 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
package socks5
import (
"bytes"
"errors"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/things-go/go-socks5/statute"
)
func TestNoAuth(t *testing.T) {
req := bytes.NewBuffer(nil)
rsp := new(bytes.Buffer)
cator := NoAuthAuthenticator{}
ctx, err := cator.Authenticate(req, rsp, "")
require.NoError(t, err)
assert.Equal(t, statute.MethodNoAuth, ctx.Method)
assert.Equal(t, []byte{statute.VersionSocks5, statute.MethodNoAuth}, rsp.Bytes())
}
func TestPasswordAuth_Valid(t *testing.T) {
req := bytes.NewBuffer([]byte{1, 3, 'f', 'o', 'o', 3, 'b', 'a', 'r'})
rsp := new(bytes.Buffer)
cator := UserPassAuthenticator{
StaticCredentials{
"foo": "bar",
},
}
ctx, err := cator.Authenticate(req, rsp, "")
require.NoError(t, err)
assert.Equal(t, statute.MethodUserPassAuth, ctx.Method)
val, ok := ctx.Payload["username"]
require.True(t, ok)
require.Equal(t, "foo", val)
val, ok = ctx.Payload["password"]
require.True(t, ok)
require.Equal(t, "bar", val)
assert.Equal(t, []byte{statute.VersionSocks5, statute.MethodUserPassAuth, 1, statute.AuthSuccess}, rsp.Bytes())
}
func TestPasswordAuth_Invalid(t *testing.T) {
req := bytes.NewBuffer([]byte{1, 3, 'f', 'o', 'o', 3, 'b', 'a', 'z'})
rsp := new(bytes.Buffer)
cator := UserPassAuthenticator{
StaticCredentials{
"foo": "bar",
},
}
ctx, err := cator.Authenticate(req, rsp, "")
require.True(t, errors.Is(err, statute.ErrUserAuthFailed))
require.Nil(t, ctx)
assert.Equal(t, []byte{statute.VersionSocks5, statute.MethodUserPassAuth, 1, statute.AuthFailure}, rsp.Bytes())
}