server_test.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // Copyright 2018 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package sockstest
  5. import (
  6. "net"
  7. "reflect"
  8. "testing"
  9. "golang.org/x/net/internal/socks"
  10. )
  11. func TestParseAuthRequest(t *testing.T) {
  12. for i, tt := range []struct {
  13. wire []byte
  14. req *AuthRequest
  15. }{
  16. {
  17. []byte{0x05, 0x00},
  18. &AuthRequest{
  19. socks.Version5,
  20. nil,
  21. },
  22. },
  23. {
  24. []byte{0x05, 0x01, 0xff},
  25. &AuthRequest{
  26. socks.Version5,
  27. []socks.AuthMethod{
  28. socks.AuthMethodNoAcceptableMethods,
  29. },
  30. },
  31. },
  32. {
  33. []byte{0x05, 0x02, 0x00, 0xff},
  34. &AuthRequest{
  35. socks.Version5,
  36. []socks.AuthMethod{
  37. socks.AuthMethodNotRequired,
  38. socks.AuthMethodNoAcceptableMethods,
  39. },
  40. },
  41. },
  42. // corrupted requests
  43. {nil, nil},
  44. {[]byte{0x00, 0x01}, nil},
  45. {[]byte{0x06, 0x00}, nil},
  46. {[]byte{0x05, 0x02, 0x00}, nil},
  47. } {
  48. req, err := ParseAuthRequest(tt.wire)
  49. if !reflect.DeepEqual(req, tt.req) {
  50. t.Errorf("#%d: got %v, %v; want %v", i, req, err, tt.req)
  51. continue
  52. }
  53. }
  54. }
  55. func TestParseCmdRequest(t *testing.T) {
  56. for i, tt := range []struct {
  57. wire []byte
  58. req *CmdRequest
  59. }{
  60. {
  61. []byte{0x05, 0x01, 0x00, 0x01, 192, 0, 2, 1, 0x17, 0x4b},
  62. &CmdRequest{
  63. socks.Version5,
  64. socks.CmdConnect,
  65. socks.Addr{
  66. IP: net.IP{192, 0, 2, 1},
  67. Port: 5963,
  68. },
  69. },
  70. },
  71. {
  72. []byte{0x05, 0x01, 0x00, 0x03, 0x04, 'F', 'Q', 'D', 'N', 0x17, 0x4b},
  73. &CmdRequest{
  74. socks.Version5,
  75. socks.CmdConnect,
  76. socks.Addr{
  77. Name: "FQDN",
  78. Port: 5963,
  79. },
  80. },
  81. },
  82. // corrupted requests
  83. {nil, nil},
  84. {[]byte{0x05}, nil},
  85. {[]byte{0x06, 0x01, 0x00, 0x01, 192, 0, 2, 2, 0x17, 0x4b}, nil},
  86. {[]byte{0x05, 0x01, 0xff, 0x01, 192, 0, 2, 3}, nil},
  87. {[]byte{0x05, 0x01, 0x00, 0x01, 192, 0, 2, 4}, nil},
  88. {[]byte{0x05, 0x01, 0x00, 0x03, 0x04, 'F', 'Q', 'D', 'N'}, nil},
  89. } {
  90. req, err := ParseCmdRequest(tt.wire)
  91. if !reflect.DeepEqual(req, tt.req) {
  92. t.Errorf("#%d: got %v, %v; want %v", i, req, err, tt.req)
  93. continue
  94. }
  95. }
  96. }