address_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package protocol_test
  2. import (
  3. "bytes"
  4. "testing"
  5. "v2ray.com/core/common"
  6. "v2ray.com/core/common/buf"
  7. "v2ray.com/core/common/compare"
  8. "v2ray.com/core/common/net"
  9. . "v2ray.com/core/common/protocol"
  10. )
  11. func TestAddressReading(t *testing.T) {
  12. data := []struct {
  13. Options []AddressOption
  14. Input []byte
  15. Address net.Address
  16. Port net.Port
  17. Error bool
  18. }{
  19. {
  20. Options: []AddressOption{},
  21. Input: []byte{},
  22. Error: true,
  23. },
  24. {
  25. Options: []AddressOption{},
  26. Input: []byte{0, 0, 0, 0, 0},
  27. Error: true,
  28. },
  29. {
  30. Options: []AddressOption{AddressFamilyByte(0x01, net.AddressFamilyIPv4)},
  31. Input: []byte{1, 0, 0, 0, 0, 0, 53},
  32. Address: net.IPAddress([]byte{0, 0, 0, 0}),
  33. Port: net.Port(53),
  34. },
  35. {
  36. Options: []AddressOption{AddressFamilyByte(0x01, net.AddressFamilyIPv4)},
  37. Input: []byte{1, 0, 0, 0, 0},
  38. Error: true,
  39. },
  40. {
  41. Options: []AddressOption{AddressFamilyByte(0x04, net.AddressFamilyIPv6)},
  42. Input: []byte{4, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 0, 80},
  43. Address: net.IPAddress([]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6}),
  44. Port: net.Port(80),
  45. },
  46. {
  47. Options: []AddressOption{AddressFamilyByte(0x03, net.AddressFamilyDomain)},
  48. Input: []byte{3, 9, 118, 50, 114, 97, 121, 46, 99, 111, 109, 0, 80},
  49. Address: net.DomainAddress("v2ray.com"),
  50. Port: net.Port(80),
  51. },
  52. {
  53. Options: []AddressOption{AddressFamilyByte(0x03, net.AddressFamilyDomain)},
  54. Input: []byte{3, 9, 118, 50, 114, 97, 121, 46, 99, 111, 109, 0},
  55. Error: true,
  56. },
  57. {
  58. Options: []AddressOption{AddressFamilyByte(0x03, net.AddressFamilyDomain)},
  59. Input: []byte{3, 7, 56, 46, 56, 46, 56, 46, 56, 0, 80},
  60. Address: net.ParseAddress("8.8.8.8"),
  61. Port: net.Port(80),
  62. },
  63. {
  64. Options: []AddressOption{AddressFamilyByte(0x03, net.AddressFamilyDomain)},
  65. Input: []byte{3, 7, 10, 46, 56, 46, 56, 46, 56, 0, 80},
  66. Error: true,
  67. },
  68. {
  69. Options: []AddressOption{AddressFamilyByte(0x03, net.AddressFamilyDomain)},
  70. Input: append(append([]byte{3, 24}, []byte("2a00:1450:4007:816::200e")...), 0, 80),
  71. Address: net.ParseAddress("2a00:1450:4007:816::200e"),
  72. Port: net.Port(80),
  73. },
  74. }
  75. for _, tc := range data {
  76. b := buf.New()
  77. parser := NewAddressParser(tc.Options...)
  78. addr, port, err := parser.ReadAddressPort(b, bytes.NewReader(tc.Input))
  79. b.Release()
  80. if tc.Error {
  81. if err == nil {
  82. t.Errorf("Expect error but not: %v", tc)
  83. }
  84. } else {
  85. if err != nil {
  86. t.Errorf("Expect no error but: %s %v", err.Error(), tc)
  87. }
  88. if addr != tc.Address {
  89. t.Error("Got address ", addr.String(), " want ", tc.Address.String())
  90. }
  91. if tc.Port != port {
  92. t.Error("Got port ", port, " want ", tc.Port)
  93. }
  94. }
  95. }
  96. }
  97. func TestAddressWriting(t *testing.T) {
  98. data := []struct {
  99. Options []AddressOption
  100. Address net.Address
  101. Port net.Port
  102. Bytes []byte
  103. Error bool
  104. }{
  105. {
  106. Options: []AddressOption{AddressFamilyByte(0x01, net.AddressFamilyIPv4)},
  107. Address: net.LocalHostIP,
  108. Port: net.Port(80),
  109. Bytes: []byte{1, 127, 0, 0, 1, 0, 80},
  110. },
  111. }
  112. for _, tc := range data {
  113. parser := NewAddressParser(tc.Options...)
  114. b := buf.New()
  115. err := parser.WriteAddressPort(b, tc.Address, tc.Port)
  116. if tc.Error {
  117. if err == nil {
  118. t.Error("Expect error but nil")
  119. }
  120. } else {
  121. common.Must(err)
  122. if err := compare.BytesEqualWithDetail(tc.Bytes, b.Bytes()); err != nil {
  123. t.Error(err)
  124. }
  125. }
  126. }
  127. }