1
0

destination_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package net_test
  2. import (
  3. "testing"
  4. "github.com/google/go-cmp/cmp"
  5. . "github.com/xtls/xray-core/common/net"
  6. )
  7. func TestDestinationProperty(t *testing.T) {
  8. testCases := []struct {
  9. Input Destination
  10. Network Network
  11. String string
  12. NetString string
  13. }{
  14. {
  15. Input: TCPDestination(IPAddress([]byte{1, 2, 3, 4}), 80),
  16. Network: Network_TCP,
  17. String: "tcp:1.2.3.4:80",
  18. NetString: "1.2.3.4:80",
  19. },
  20. {
  21. Input: UDPDestination(IPAddress([]byte{0x20, 0x01, 0x48, 0x60, 0x48, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x88, 0x88}), 53),
  22. Network: Network_UDP,
  23. String: "udp:[2001:4860:4860::8888]:53",
  24. NetString: "[2001:4860:4860::8888]:53",
  25. },
  26. {
  27. Input: UnixDestination(DomainAddress("/tmp/test.sock")),
  28. Network: Network_UNIX,
  29. String: "unix:/tmp/test.sock",
  30. NetString: "/tmp/test.sock",
  31. },
  32. }
  33. for _, testCase := range testCases {
  34. dest := testCase.Input
  35. if r := cmp.Diff(dest.Network, testCase.Network); r != "" {
  36. t.Error("unexpected Network in ", dest.String(), ": ", r)
  37. }
  38. if r := cmp.Diff(dest.String(), testCase.String); r != "" {
  39. t.Error(r)
  40. }
  41. if r := cmp.Diff(dest.NetAddr(), testCase.NetString); r != "" {
  42. t.Error(r)
  43. }
  44. }
  45. }
  46. func TestDestinationParse(t *testing.T) {
  47. cases := []struct {
  48. Input string
  49. Output Destination
  50. Error bool
  51. }{
  52. {
  53. Input: "tcp:127.0.0.1:80",
  54. Output: TCPDestination(LocalHostIP, Port(80)),
  55. },
  56. {
  57. Input: "udp:8.8.8.8:53",
  58. Output: UDPDestination(IPAddress([]byte{8, 8, 8, 8}), Port(53)),
  59. },
  60. {
  61. Input: "unix:/tmp/test.sock",
  62. Output: UnixDestination(DomainAddress("/tmp/test.sock")),
  63. },
  64. {
  65. Input: "8.8.8.8:53",
  66. Output: Destination{
  67. Address: IPAddress([]byte{8, 8, 8, 8}),
  68. Port: Port(53),
  69. },
  70. },
  71. {
  72. Input: ":53",
  73. Output: Destination{
  74. Address: AnyIP,
  75. Port: Port(53),
  76. },
  77. },
  78. {
  79. Input: "8.8.8.8",
  80. Error: true,
  81. },
  82. {
  83. Input: "8.8.8.8:http",
  84. Error: true,
  85. },
  86. {
  87. Input: "/tmp/test.sock",
  88. Error: true,
  89. },
  90. }
  91. for _, testcase := range cases {
  92. d, err := ParseDestination(testcase.Input)
  93. if !testcase.Error {
  94. if err != nil {
  95. t.Error("for test case: ", testcase.Input, " expected no error, but got ", err)
  96. }
  97. if d != testcase.Output {
  98. t.Error("for test case: ", testcase.Input, " expected output: ", testcase.Output.String(), " but got ", d.String())
  99. }
  100. } else if err == nil {
  101. t.Error("for test case: ", testcase.Input, " expected error, but got nil")
  102. }
  103. }
  104. }