2014-01-23 19:27:48 +00:00
|
|
|
package socks5
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2020-08-05 06:40:07 +00:00
|
|
|
"errors"
|
2014-01-23 19:27:48 +00:00
|
|
|
"testing"
|
2020-08-05 05:17:05 +00:00
|
|
|
|
2020-08-05 06:40:07 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2022-10-17 01:36:23 +00:00
|
|
|
"git.tcp.direct/kayos/go-socks5/statute"
|
2014-01-23 19:27:48 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestNoAuth(t *testing.T) {
|
|
|
|
req := bytes.NewBuffer(nil)
|
2020-08-05 06:40:07 +00:00
|
|
|
rsp := new(bytes.Buffer)
|
|
|
|
cator := NoAuthAuthenticator{}
|
2016-01-11 07:52:06 +00:00
|
|
|
|
2020-08-05 06:40:07 +00:00
|
|
|
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())
|
2014-01-23 19:27:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPasswordAuth_Valid(t *testing.T) {
|
2020-08-05 06:40:07 +00:00
|
|
|
req := bytes.NewBuffer([]byte{1, 3, 'f', 'o', 'o', 3, 'b', 'a', 'r'})
|
|
|
|
rsp := new(bytes.Buffer)
|
|
|
|
cator := UserPassAuthenticator{
|
|
|
|
StaticCredentials{
|
|
|
|
"foo": "bar",
|
|
|
|
},
|
2014-01-23 19:27:48 +00:00
|
|
|
}
|
2016-01-11 07:52:06 +00:00
|
|
|
|
2020-08-05 06:40:07 +00:00
|
|
|
ctx, err := cator.Authenticate(req, rsp, "")
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, statute.MethodUserPassAuth, ctx.Method)
|
2016-01-11 07:52:06 +00:00
|
|
|
|
2020-04-23 02:15:02 +00:00
|
|
|
val, ok := ctx.Payload["username"]
|
2020-08-05 06:40:07 +00:00
|
|
|
require.True(t, ok)
|
|
|
|
require.Equal(t, "foo", val)
|
2020-04-23 02:15:02 +00:00
|
|
|
|
|
|
|
val, ok = ctx.Payload["password"]
|
2020-08-05 06:40:07 +00:00
|
|
|
require.True(t, ok)
|
|
|
|
require.Equal(t, "bar", val)
|
2016-01-11 07:52:06 +00:00
|
|
|
|
2020-08-05 06:40:07 +00:00
|
|
|
assert.Equal(t, []byte{statute.VersionSocks5, statute.MethodUserPassAuth, 1, statute.AuthSuccess}, rsp.Bytes())
|
2014-01-23 19:27:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPasswordAuth_Invalid(t *testing.T) {
|
2020-08-05 06:40:07 +00:00
|
|
|
req := bytes.NewBuffer([]byte{1, 3, 'f', 'o', 'o', 3, 'b', 'a', 'z'})
|
|
|
|
rsp := new(bytes.Buffer)
|
|
|
|
cator := UserPassAuthenticator{
|
|
|
|
StaticCredentials{
|
|
|
|
"foo": "bar",
|
|
|
|
},
|
2014-01-23 19:27:48 +00:00
|
|
|
}
|
|
|
|
|
2020-08-05 06:40:07 +00:00
|
|
|
ctx, err := cator.Authenticate(req, rsp, "")
|
|
|
|
require.True(t, errors.Is(err, statute.ErrUserAuthFailed))
|
|
|
|
require.Nil(t, ctx)
|
2016-01-11 07:52:06 +00:00
|
|
|
|
2020-08-05 06:40:07 +00:00
|
|
|
assert.Equal(t, []byte{statute.VersionSocks5, statute.MethodUserPassAuth, 1, statute.AuthFailure}, rsp.Bytes())
|
2014-01-23 19:27:48 +00:00
|
|
|
}
|