github.com/xraypb/Xray-core@v1.8.1/common/net/address_test.go (about)

     1  package net_test
     2  
     3  import (
     4  	"net"
     5  	"testing"
     6  
     7  	"github.com/google/go-cmp/cmp"
     8  	. "github.com/xraypb/Xray-core/common/net"
     9  )
    10  
    11  func TestAddressProperty(t *testing.T) {
    12  	type addrProprty struct {
    13  		IP     []byte
    14  		Domain string
    15  		Family AddressFamily
    16  		String string
    17  	}
    18  
    19  	testCases := []struct {
    20  		Input  Address
    21  		Output addrProprty
    22  	}{
    23  		{
    24  			Input: IPAddress([]byte{byte(1), byte(2), byte(3), byte(4)}),
    25  			Output: addrProprty{
    26  				IP:     []byte{byte(1), byte(2), byte(3), byte(4)},
    27  				Family: AddressFamilyIPv4,
    28  				String: "1.2.3.4",
    29  			},
    30  		},
    31  		{
    32  			Input: IPAddress([]byte{
    33  				byte(1), byte(2), byte(3), byte(4),
    34  				byte(1), byte(2), byte(3), byte(4),
    35  				byte(1), byte(2), byte(3), byte(4),
    36  				byte(1), byte(2), byte(3), byte(4),
    37  			}),
    38  			Output: addrProprty{
    39  				IP: []byte{
    40  					byte(1), byte(2), byte(3), byte(4),
    41  					byte(1), byte(2), byte(3), byte(4),
    42  					byte(1), byte(2), byte(3), byte(4),
    43  					byte(1), byte(2), byte(3), byte(4),
    44  				},
    45  				Family: AddressFamilyIPv6,
    46  				String: "[102:304:102:304:102:304:102:304]",
    47  			},
    48  		},
    49  		{
    50  			Input: IPAddress([]byte{
    51  				byte(0), byte(0), byte(0), byte(0),
    52  				byte(0), byte(0), byte(0), byte(0),
    53  				byte(0), byte(0), byte(255), byte(255),
    54  				byte(1), byte(2), byte(3), byte(4),
    55  			}),
    56  			Output: addrProprty{
    57  				IP:     []byte{byte(1), byte(2), byte(3), byte(4)},
    58  				Family: AddressFamilyIPv4,
    59  				String: "1.2.3.4",
    60  			},
    61  		},
    62  		{
    63  			Input: DomainAddress("example.com"),
    64  			Output: addrProprty{
    65  				Domain: "example.com",
    66  				Family: AddressFamilyDomain,
    67  				String: "example.com",
    68  			},
    69  		},
    70  		{
    71  			Input: IPAddress(net.IPv4(1, 2, 3, 4)),
    72  			Output: addrProprty{
    73  				IP:     []byte{byte(1), byte(2), byte(3), byte(4)},
    74  				Family: AddressFamilyIPv4,
    75  				String: "1.2.3.4",
    76  			},
    77  		},
    78  		{
    79  			Input: ParseAddress("[2001:4860:0:2001::68]"),
    80  			Output: addrProprty{
    81  				IP:     []byte{0x20, 0x01, 0x48, 0x60, 0x00, 0x00, 0x20, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x68},
    82  				Family: AddressFamilyIPv6,
    83  				String: "[2001:4860:0:2001::68]",
    84  			},
    85  		},
    86  		{
    87  			Input: ParseAddress("::0"),
    88  			Output: addrProprty{
    89  				IP:     AnyIPv6.IP(),
    90  				Family: AddressFamilyIPv6,
    91  				String: "[::]",
    92  			},
    93  		},
    94  		{
    95  			Input: ParseAddress("[::ffff:123.151.71.143]"),
    96  			Output: addrProprty{
    97  				IP:     []byte{123, 151, 71, 143},
    98  				Family: AddressFamilyIPv4,
    99  				String: "123.151.71.143",
   100  			},
   101  		},
   102  		{
   103  			Input: NewIPOrDomain(ParseAddress("example.com")).AsAddress(),
   104  			Output: addrProprty{
   105  				Domain: "example.com",
   106  				Family: AddressFamilyDomain,
   107  				String: "example.com",
   108  			},
   109  		},
   110  		{
   111  			Input: NewIPOrDomain(ParseAddress("8.8.8.8")).AsAddress(),
   112  			Output: addrProprty{
   113  				IP:     []byte{8, 8, 8, 8},
   114  				Family: AddressFamilyIPv4,
   115  				String: "8.8.8.8",
   116  			},
   117  		},
   118  		{
   119  			Input: NewIPOrDomain(ParseAddress("[2001:4860:0:2001::68]")).AsAddress(),
   120  			Output: addrProprty{
   121  				IP:     []byte{0x20, 0x01, 0x48, 0x60, 0x00, 0x00, 0x20, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x68},
   122  				Family: AddressFamilyIPv6,
   123  				String: "[2001:4860:0:2001::68]",
   124  			},
   125  		},
   126  	}
   127  
   128  	for _, testCase := range testCases {
   129  		actual := addrProprty{
   130  			Family: testCase.Input.Family(),
   131  			String: testCase.Input.String(),
   132  		}
   133  		if testCase.Input.Family().IsIP() {
   134  			actual.IP = testCase.Input.IP()
   135  		} else {
   136  			actual.Domain = testCase.Input.Domain()
   137  		}
   138  
   139  		if r := cmp.Diff(actual, testCase.Output); r != "" {
   140  			t.Error("for input: ", testCase.Input, ":", r)
   141  		}
   142  	}
   143  }
   144  
   145  func TestInvalidAddressConvertion(t *testing.T) {
   146  	panics := func(f func()) (ret bool) {
   147  		defer func() {
   148  			if r := recover(); r != nil {
   149  				ret = true
   150  			}
   151  		}()
   152  		f()
   153  		return false
   154  	}
   155  
   156  	testCases := []func(){
   157  		func() { ParseAddress("8.8.8.8").Domain() },
   158  		func() { ParseAddress("2001:4860:0:2001::68").Domain() },
   159  		func() { ParseAddress("example.com").IP() },
   160  	}
   161  	for idx, testCase := range testCases {
   162  		if !panics(testCase) {
   163  			t.Error("case ", idx, " failed")
   164  		}
   165  	}
   166  }
   167  
   168  func BenchmarkParseAddressIPv4(b *testing.B) {
   169  	for i := 0; i < b.N; i++ {
   170  		addr := ParseAddress("8.8.8.8")
   171  		if addr.Family() != AddressFamilyIPv4 {
   172  			panic("not ipv4")
   173  		}
   174  	}
   175  }
   176  
   177  func BenchmarkParseAddressIPv6(b *testing.B) {
   178  	for i := 0; i < b.N; i++ {
   179  		addr := ParseAddress("2001:4860:0:2001::68")
   180  		if addr.Family() != AddressFamilyIPv6 {
   181  			panic("not ipv6")
   182  		}
   183  	}
   184  }
   185  
   186  func BenchmarkParseAddressDomain(b *testing.B) {
   187  	for i := 0; i < b.N; i++ {
   188  		addr := ParseAddress("example.com")
   189  		if addr.Family() != AddressFamilyDomain {
   190  			panic("not domain")
   191  		}
   192  	}
   193  }