destination_test.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package net_test
  2. import (
  3. "testing"
  4. . "v2ray.com/core/common/net"
  5. . "v2ray.com/core/common/net/testing"
  6. . "v2ray.com/ext/assert"
  7. )
  8. func TestTCPDestination(t *testing.T) {
  9. assert := With(t)
  10. dest := TCPDestination(IPAddress([]byte{1, 2, 3, 4}), 80)
  11. assert(dest, IsTCP)
  12. assert(dest, Not(IsUDP))
  13. assert(dest.String(), Equals, "tcp:1.2.3.4:80")
  14. }
  15. func TestUDPDestination(t *testing.T) {
  16. assert := With(t)
  17. dest := UDPDestination(IPAddress([]byte{0x20, 0x01, 0x48, 0x60, 0x48, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x88, 0x88}), 53)
  18. assert(dest, Not(IsTCP))
  19. assert(dest, IsUDP)
  20. assert(dest.String(), Equals, "udp:[2001:4860:4860::8888]:53")
  21. }
  22. func TestDestinationParse(t *testing.T) {
  23. assert := With(t)
  24. cases := []struct {
  25. Input string
  26. Output Destination
  27. Error bool
  28. }{
  29. {
  30. Input: "tcp:127.0.0.1:80",
  31. Output: TCPDestination(LocalHostIP, Port(80)),
  32. },
  33. {
  34. Input: "udp:8.8.8.8:53",
  35. Output: UDPDestination(IPAddress([]byte{8, 8, 8, 8}), Port(53)),
  36. },
  37. {
  38. Input: "8.8.8.8:53",
  39. Output: Destination{
  40. Address: IPAddress([]byte{8, 8, 8, 8}),
  41. Port: Port(53),
  42. },
  43. },
  44. {
  45. Input: ":53",
  46. Output: Destination{
  47. Address: AnyIP,
  48. Port: Port(53),
  49. },
  50. },
  51. {
  52. Input: "8.8.8.8",
  53. Error: true,
  54. },
  55. {
  56. Input: "8.8.8.8:http",
  57. Error: true,
  58. },
  59. }
  60. for _, testcase := range cases {
  61. d, err := ParseDestination(testcase.Input)
  62. if !testcase.Error {
  63. assert(err, IsNil)
  64. assert(d, Equals, testcase.Output)
  65. } else {
  66. assert(err, IsNotNil)
  67. }
  68. }
  69. }