auth_test.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package socks5
  2. import (
  3. "bytes"
  4. "testing"
  5. )
  6. func TestNoAuth(t *testing.T) {
  7. req := bytes.NewBuffer(nil)
  8. req.Write([]byte{1, NoAuth})
  9. var resp bytes.Buffer
  10. s, _ := New(&Config{})
  11. ctx, err := s.authenticate(&resp, req)
  12. if err != nil {
  13. t.Fatalf("err: %v", err)
  14. }
  15. if ctx.Method != NoAuth {
  16. t.Fatal("Invalid Context Method")
  17. }
  18. out := resp.Bytes()
  19. if !bytes.Equal(out, []byte{socks5Version, NoAuth}) {
  20. t.Fatalf("bad: %v", out)
  21. }
  22. }
  23. func TestPasswordAuth_Valid(t *testing.T) {
  24. req := bytes.NewBuffer(nil)
  25. req.Write([]byte{2, NoAuth, UserPassAuth})
  26. req.Write([]byte{1, 3, 'f', 'o', 'o', 3, 'b', 'a', 'r'})
  27. var resp bytes.Buffer
  28. cred := StaticCredentials{
  29. "foo": "bar",
  30. }
  31. cator := UserPassAuthenticator{Credentials: cred}
  32. s, _ := New(&Config{AuthMethods: []Authenticator{cator}})
  33. ctx, err := s.authenticate(&resp, req)
  34. if err != nil {
  35. t.Fatalf("err: %v", err)
  36. }
  37. if ctx.Method != UserPassAuth {
  38. t.Fatal("Invalid Context Method")
  39. }
  40. val, ok := ctx.Payload["Username"]
  41. if !ok {
  42. t.Fatal("Missing key Username in auth context's payload")
  43. }
  44. if val != "foo" {
  45. t.Fatal("Invalid Username in auth context's payload")
  46. }
  47. out := resp.Bytes()
  48. if !bytes.Equal(out, []byte{socks5Version, UserPassAuth, 1, authSuccess}) {
  49. t.Fatalf("bad: %v", out)
  50. }
  51. }
  52. func TestPasswordAuth_Invalid(t *testing.T) {
  53. req := bytes.NewBuffer(nil)
  54. req.Write([]byte{2, NoAuth, UserPassAuth})
  55. req.Write([]byte{1, 3, 'f', 'o', 'o', 3, 'b', 'a', 'z'})
  56. var resp bytes.Buffer
  57. cred := StaticCredentials{
  58. "foo": "bar",
  59. }
  60. cator := UserPassAuthenticator{Credentials: cred}
  61. s, _ := New(&Config{AuthMethods: []Authenticator{cator}})
  62. ctx, err := s.authenticate(&resp, req)
  63. if err != UserAuthFailed {
  64. t.Fatalf("err: %v", err)
  65. }
  66. if ctx != nil {
  67. t.Fatal("Invalid Context Method")
  68. }
  69. out := resp.Bytes()
  70. if !bytes.Equal(out, []byte{socks5Version, UserPassAuth, 1, authFailure}) {
  71. t.Fatalf("bad: %v", out)
  72. }
  73. }
  74. func TestNoSupportedAuth(t *testing.T) {
  75. req := bytes.NewBuffer(nil)
  76. req.Write([]byte{1, NoAuth})
  77. var resp bytes.Buffer
  78. cred := StaticCredentials{
  79. "foo": "bar",
  80. }
  81. cator := UserPassAuthenticator{Credentials: cred}
  82. s, _ := New(&Config{AuthMethods: []Authenticator{cator}})
  83. ctx, err := s.authenticate(&resp, req)
  84. if err != NoSupportedAuth {
  85. t.Fatalf("err: %v", err)
  86. }
  87. if ctx != nil {
  88. t.Fatal("Invalid Context Method")
  89. }
  90. out := resp.Bytes()
  91. if !bytes.Equal(out, []byte{socks5Version, noAcceptable}) {
  92. t.Fatalf("bad: %v", out)
  93. }
  94. }