github.com/Uhtred009/v2ray-core-1@v4.31.2+incompatible/common/net/destination_test.go (about)

     1  package net_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/google/go-cmp/cmp"
     7  
     8  	. "v2ray.com/core/common/net"
     9  )
    10  
    11  func TestDestinationProperty(t *testing.T) {
    12  	testCases := []struct {
    13  		Input     Destination
    14  		Network   Network
    15  		String    string
    16  		NetString string
    17  	}{
    18  		{
    19  			Input:     TCPDestination(IPAddress([]byte{1, 2, 3, 4}), 80),
    20  			Network:   Network_TCP,
    21  			String:    "tcp:1.2.3.4:80",
    22  			NetString: "1.2.3.4:80",
    23  		},
    24  		{
    25  			Input:     UDPDestination(IPAddress([]byte{0x20, 0x01, 0x48, 0x60, 0x48, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x88, 0x88}), 53),
    26  			Network:   Network_UDP,
    27  			String:    "udp:[2001:4860:4860::8888]:53",
    28  			NetString: "[2001:4860:4860::8888]:53",
    29  		},
    30  	}
    31  
    32  	for _, testCase := range testCases {
    33  		dest := testCase.Input
    34  		if r := cmp.Diff(dest.Network, testCase.Network); r != "" {
    35  			t.Error("unexpected Network in ", dest.String(), ": ", r)
    36  		}
    37  		if r := cmp.Diff(dest.String(), testCase.String); r != "" {
    38  			t.Error(r)
    39  		}
    40  		if r := cmp.Diff(dest.NetAddr(), testCase.NetString); r != "" {
    41  			t.Error(r)
    42  		}
    43  	}
    44  }
    45  
    46  func TestDestinationParse(t *testing.T) {
    47  	cases := []struct {
    48  		Input  string
    49  		Output Destination
    50  		Error  bool
    51  	}{
    52  		{
    53  			Input:  "tcp:127.0.0.1:80",
    54  			Output: TCPDestination(LocalHostIP, Port(80)),
    55  		},
    56  		{
    57  			Input:  "udp:8.8.8.8:53",
    58  			Output: UDPDestination(IPAddress([]byte{8, 8, 8, 8}), Port(53)),
    59  		},
    60  		{
    61  			Input: "8.8.8.8:53",
    62  			Output: Destination{
    63  				Address: IPAddress([]byte{8, 8, 8, 8}),
    64  				Port:    Port(53),
    65  			},
    66  		},
    67  		{
    68  			Input: ":53",
    69  			Output: Destination{
    70  				Address: AnyIP,
    71  				Port:    Port(53),
    72  			},
    73  		},
    74  		{
    75  			Input: "8.8.8.8",
    76  			Error: true,
    77  		},
    78  		{
    79  			Input: "8.8.8.8:http",
    80  			Error: true,
    81  		},
    82  	}
    83  
    84  	for _, testcase := range cases {
    85  		d, err := ParseDestination(testcase.Input)
    86  		if !testcase.Error {
    87  			if err != nil {
    88  				t.Error("for test case: ", testcase.Input, " expected no error, but got ", err)
    89  			}
    90  			if d != testcase.Output {
    91  				t.Error("for test case: ", testcase.Input, " expected output: ", testcase.Output.String(), " but got ", d.String())
    92  			}
    93  		} else {
    94  			if err == nil {
    95  				t.Error("for test case: ", testcase.Input, " expected error, but got nil")
    96  			}
    97  		}
    98  	}
    99  }