64 lines
1.6 KiB
Go
64 lines
1.6 KiB
Go
package socks5
|
|
|
|
import (
|
|
"bytes"
|
|
"errors"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"git.tcp.direct/kayos/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())
|
|
}
|