destination_test.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. package net_test
  2. import (
  3. "testing"
  4. "github.com/google/go-cmp/cmp"
  5. . "v2ray.com/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. for _, testCase := range testCases {
  28. dest := testCase.Input
  29. if r := cmp.Diff(dest.Network, testCase.Network); r != "" {
  30. t.Error("unexpected Network in ", dest.String(), ": ", r)
  31. }
  32. if r := cmp.Diff(dest.String(), testCase.String); r != "" {
  33. t.Error(r)
  34. }
  35. if r := cmp.Diff(dest.NetAddr(), testCase.NetString); r != "" {
  36. t.Error(r)
  37. }
  38. }
  39. }
  40. func TestDestinationParse(t *testing.T) {
  41. cases := []struct {
  42. Input string
  43. Output Destination
  44. Error bool
  45. }{
  46. {
  47. Input: "tcp:127.0.0.1:80",
  48. Output: TCPDestination(LocalHostIP, Port(80)),
  49. },
  50. {
  51. Input: "udp:8.8.8.8:53",
  52. Output: UDPDestination(IPAddress([]byte{8, 8, 8, 8}), Port(53)),
  53. },
  54. {
  55. Input: "8.8.8.8:53",
  56. Output: Destination{
  57. Address: IPAddress([]byte{8, 8, 8, 8}),
  58. Port: Port(53),
  59. },
  60. },
  61. {
  62. Input: ":53",
  63. Output: Destination{
  64. Address: AnyIP,
  65. Port: Port(53),
  66. },
  67. },
  68. {
  69. Input: "8.8.8.8",
  70. Error: true,
  71. },
  72. {
  73. Input: "8.8.8.8:http",
  74. Error: true,
  75. },
  76. }
  77. for _, testcase := range cases {
  78. d, err := ParseDestination(testcase.Input)
  79. if !testcase.Error {
  80. if err != nil {
  81. t.Error("for test case: ", testcase.Input, " expected no error, but got ", err)
  82. }
  83. if d != testcase.Output {
  84. t.Error("for test case: ", testcase.Input, " expected output: ", testcase.Output.String(), " but got ", d.String())
  85. }
  86. } else if err == nil {
  87. t.Error("for test case: ", testcase.Input, " expected error, but got nil")
  88. }
  89. }
  90. }