github.com/xraypb/Xray-core@v1.8.1/transport/internet/grpc/config_test.go (about)

     1  package grpc
     2  
     3  import (
     4  	"github.com/stretchr/testify/assert"
     5  	"testing"
     6  )
     7  
     8  func TestConfig_GetServiceName(t *testing.T) {
     9  	tests := []struct {
    10  		TestName    string
    11  		ServiceName string
    12  		Expected    string
    13  	}{
    14  		{
    15  			TestName:    "simple no absolute path",
    16  			ServiceName: "hello",
    17  			Expected:    "hello",
    18  		},
    19  		{
    20  			TestName:    "escape no absolute path",
    21  			ServiceName: "hello/world!",
    22  			Expected:    "hello%2Fworld%21",
    23  		},
    24  		{
    25  			TestName:    "absolute path",
    26  			ServiceName: "/my/sample/path/a|b",
    27  			Expected:    "my/sample/path",
    28  		},
    29  		{
    30  			TestName:    "escape absolute path",
    31  			ServiceName: "/hello /world!/a|b",
    32  			Expected:    "hello%20/world%21",
    33  		},
    34  	}
    35  	for _, test := range tests {
    36  		t.Run(test.TestName, func(t *testing.T) {
    37  			config := Config{ServiceName: test.ServiceName}
    38  			assert.Equal(t, test.Expected, config.getServiceName())
    39  		})
    40  	}
    41  }
    42  
    43  func TestConfig_GetTunStreamName(t *testing.T) {
    44  	tests := []struct {
    45  		TestName    string
    46  		ServiceName string
    47  		Expected    string
    48  	}{
    49  		{
    50  			TestName:    "no absolute path",
    51  			ServiceName: "hello",
    52  			Expected:    "Tun",
    53  		},
    54  		{
    55  			TestName:    "absolute path server",
    56  			ServiceName: "/my/sample/path/tun_service|multi_service",
    57  			Expected:    "tun_service",
    58  		},
    59  		{
    60  			TestName:    "absolute path client",
    61  			ServiceName: "/my/sample/path/tun_service",
    62  			Expected:    "tun_service",
    63  		},
    64  		{
    65  			TestName:    "escape absolute path client",
    66  			ServiceName: "/m y/sa !mple/pa\\th/tun\\_serv!ice",
    67  			Expected:    "tun%5C_serv%21ice",
    68  		},
    69  	}
    70  	for _, test := range tests {
    71  		t.Run(test.TestName, func(t *testing.T) {
    72  			config := Config{ServiceName: test.ServiceName}
    73  			assert.Equal(t, test.Expected, config.getTunStreamName())
    74  		})
    75  	}
    76  }
    77  
    78  func TestConfig_GetTunMultiStreamName(t *testing.T) {
    79  	tests := []struct {
    80  		TestName    string
    81  		ServiceName string
    82  		Expected    string
    83  	}{
    84  		{
    85  			TestName:    "no absolute path",
    86  			ServiceName: "hello",
    87  			Expected:    "TunMulti",
    88  		},
    89  		{
    90  			TestName:    "absolute path server",
    91  			ServiceName: "/my/sample/path/tun_service|multi_service",
    92  			Expected:    "multi_service",
    93  		},
    94  		{
    95  			TestName:    "absolute path client",
    96  			ServiceName: "/my/sample/path/multi_service",
    97  			Expected:    "multi_service",
    98  		},
    99  		{
   100  			TestName:    "escape absolute path client",
   101  			ServiceName: "/m y/sa !mple/pa\\th/mu%lti\\_serv!ice",
   102  			Expected:    "mu%25lti%5C_serv%21ice",
   103  		},
   104  	}
   105  	for _, test := range tests {
   106  		t.Run(test.TestName, func(t *testing.T) {
   107  			config := Config{ServiceName: test.ServiceName}
   108  			assert.Equal(t, test.Expected, config.getTunMultiStreamName())
   109  		})
   110  	}
   111  }