| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- package protocol_test
- import (
- "bytes"
- "testing"
- "v2ray.com/core/common"
- "v2ray.com/core/common/buf"
- "v2ray.com/core/common/compare"
- "v2ray.com/core/common/net"
- . "v2ray.com/core/common/protocol"
- )
- func TestAddressReading(t *testing.T) {
- data := []struct {
- Options []AddressOption
- Input []byte
- Address net.Address
- Port net.Port
- Error bool
- }{
- {
- Options: []AddressOption{},
- Input: []byte{},
- Error: true,
- },
- {
- Options: []AddressOption{},
- Input: []byte{0, 0, 0, 0, 0},
- Error: true,
- },
- {
- Options: []AddressOption{AddressFamilyByte(0x01, net.AddressFamilyIPv4)},
- Input: []byte{1, 0, 0, 0, 0, 0, 53},
- Address: net.IPAddress([]byte{0, 0, 0, 0}),
- Port: net.Port(53),
- },
- {
- Options: []AddressOption{AddressFamilyByte(0x01, net.AddressFamilyIPv4)},
- Input: []byte{1, 0, 0, 0, 0},
- Error: true,
- },
- {
- Options: []AddressOption{AddressFamilyByte(0x04, net.AddressFamilyIPv6)},
- Input: []byte{4, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 0, 80},
- Address: net.IPAddress([]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6}),
- Port: net.Port(80),
- },
- {
- Options: []AddressOption{AddressFamilyByte(0x03, net.AddressFamilyDomain)},
- Input: []byte{3, 9, 118, 50, 114, 97, 121, 46, 99, 111, 109, 0, 80},
- Address: net.DomainAddress("v2ray.com"),
- Port: net.Port(80),
- },
- {
- Options: []AddressOption{AddressFamilyByte(0x03, net.AddressFamilyDomain)},
- Input: []byte{3, 9, 118, 50, 114, 97, 121, 46, 99, 111, 109, 0},
- Error: true,
- },
- {
- Options: []AddressOption{AddressFamilyByte(0x03, net.AddressFamilyDomain)},
- Input: []byte{3, 7, 56, 46, 56, 46, 56, 46, 56, 0, 80},
- Address: net.ParseAddress("8.8.8.8"),
- Port: net.Port(80),
- },
- {
- Options: []AddressOption{AddressFamilyByte(0x03, net.AddressFamilyDomain)},
- Input: []byte{3, 7, 10, 46, 56, 46, 56, 46, 56, 0, 80},
- Error: true,
- },
- {
- Options: []AddressOption{AddressFamilyByte(0x03, net.AddressFamilyDomain)},
- Input: append(append([]byte{3, 24}, []byte("2a00:1450:4007:816::200e")...), 0, 80),
- Address: net.ParseAddress("2a00:1450:4007:816::200e"),
- Port: net.Port(80),
- },
- }
- for _, tc := range data {
- b := buf.New()
- parser := NewAddressParser(tc.Options...)
- addr, port, err := parser.ReadAddressPort(b, bytes.NewReader(tc.Input))
- b.Release()
- if tc.Error {
- if err == nil {
- t.Errorf("Expect error but not: %v", tc)
- }
- } else {
- if err != nil {
- t.Errorf("Expect no error but: %s %v", err.Error(), tc)
- }
- if addr != tc.Address {
- t.Error("Got address ", addr.String(), " want ", tc.Address.String())
- }
- if tc.Port != port {
- t.Error("Got port ", port, " want ", tc.Port)
- }
- }
- }
- }
- func TestAddressWriting(t *testing.T) {
- data := []struct {
- Options []AddressOption
- Address net.Address
- Port net.Port
- Bytes []byte
- Error bool
- }{
- {
- Options: []AddressOption{AddressFamilyByte(0x01, net.AddressFamilyIPv4)},
- Address: net.LocalHostIP,
- Port: net.Port(80),
- Bytes: []byte{1, 127, 0, 0, 1, 0, 80},
- },
- }
- for _, tc := range data {
- parser := NewAddressParser(tc.Options...)
- b := buf.New()
- err := parser.WriteAddressPort(b, tc.Address, tc.Port)
- if tc.Error {
- if err == nil {
- t.Error("Expect error but nil")
- }
- } else {
- common.Must(err)
- if err := compare.BytesEqualWithDetail(tc.Bytes, b.Bytes()); err != nil {
- t.Error(err)
- }
- }
- }
- }
|