123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- 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{})
- ctx, err := s.authenticate(&resp, req)
- if err != nil {
- t.Fatalf("err: %v", err)
- }
- if ctx.Method != NoAuth {
- t.Fatal("Invalid Context Method")
- }
- 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}})
- ctx, err := s.authenticate(&resp, req)
- if err != nil {
- t.Fatalf("err: %v", err)
- }
- if ctx.Method != UserPassAuth {
- t.Fatal("Invalid Context Method")
- }
- val, ok := ctx.Payload["Username"]
- if !ok {
- t.Fatal("Missing key Username in auth context's payload")
- }
- if val != "foo" {
- t.Fatal("Invalid Username in auth context's payload")
- }
- 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}})
- ctx, err := s.authenticate(&resp, req)
- if err != UserAuthFailed {
- t.Fatalf("err: %v", err)
- }
- if ctx != nil {
- t.Fatal("Invalid Context Method")
- }
- 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}})
- ctx, err := s.authenticate(&resp, req)
- if err != NoSupportedAuth {
- t.Fatalf("err: %v", err)
- }
- if ctx != nil {
- t.Fatal("Invalid Context Method")
- }
- out := resp.Bytes()
- if !bytes.Equal(out, []byte{socks5Version, noAcceptable}) {
- t.Fatalf("bad: %v", out)
- }
- }
|