github.com/v2fly/v2ray-core/v5@v5.16.2-0.20240507031116-8191faa6e095/common/strmatcher/matchers_test.go (about) 1 package strmatcher_test 2 3 import ( 4 "reflect" 5 "testing" 6 "unsafe" 7 8 "github.com/v2fly/v2ray-core/v5/common" 9 . "github.com/v2fly/v2ray-core/v5/common/strmatcher" 10 ) 11 12 func TestMatcher(t *testing.T) { 13 cases := []struct { 14 pattern string 15 mType Type 16 input string 17 output bool 18 }{ 19 { 20 pattern: "v2fly.org", 21 mType: Domain, 22 input: "www.v2fly.org", 23 output: true, 24 }, 25 { 26 pattern: "v2fly.org", 27 mType: Domain, 28 input: "v2fly.org", 29 output: true, 30 }, 31 { 32 pattern: "v2fly.org", 33 mType: Domain, 34 input: "www.v3fly.org", 35 output: false, 36 }, 37 { 38 pattern: "v2fly.org", 39 mType: Domain, 40 input: "2fly.org", 41 output: false, 42 }, 43 { 44 pattern: "v2fly.org", 45 mType: Domain, 46 input: "xv2fly.org", 47 output: false, 48 }, 49 { 50 pattern: "v2fly.org", 51 mType: Full, 52 input: "v2fly.org", 53 output: true, 54 }, 55 { 56 pattern: "v2fly.org", 57 mType: Full, 58 input: "xv2fly.org", 59 output: false, 60 }, 61 { 62 pattern: "v2fly.org", 63 mType: Regex, 64 input: "v2flyxorg", 65 output: true, 66 }, 67 } 68 for _, test := range cases { 69 matcher, err := test.mType.New(test.pattern) 70 common.Must(err) 71 if m := matcher.Match(test.input); m != test.output { 72 t.Error("unexpected output: ", m, " for test case ", test) 73 } 74 } 75 } 76 77 func TestToDomain(t *testing.T) { 78 { // Test normal ASCII domain, which should not trigger new string data allocation 79 input := "v2fly.org" 80 domain, err := ToDomain(input) 81 if err != nil { 82 t.Error("unexpected error: ", err) 83 } 84 if domain != input { 85 t.Error("unexpected output: ", domain, " for test case ", input) 86 } 87 if (*reflect.StringHeader)(unsafe.Pointer(&input)).Data != (*reflect.StringHeader)(unsafe.Pointer(&domain)).Data { 88 t.Error("different string data of output: ", domain, " and test case ", input) 89 } 90 } 91 { // Test ASCII domain containing upper case letter, which should be converted to lower case 92 input := "v2FLY.oRg" 93 domain, err := ToDomain(input) 94 if err != nil { 95 t.Error("unexpected error: ", err) 96 } 97 if domain != "v2fly.org" { 98 t.Error("unexpected output: ", domain, " for test case ", input) 99 } 100 } 101 { // Test internationalized domain, which should be translated to ASCII punycode 102 input := "v2fly.公益" 103 domain, err := ToDomain(input) 104 if err != nil { 105 t.Error("unexpected error: ", err) 106 } 107 if domain != "v2fly.xn--55qw42g" { 108 t.Error("unexpected output: ", domain, " for test case ", input) 109 } 110 } 111 { // Test internationalized domain containing upper case letter 112 input := "v2FLY.公益" 113 domain, err := ToDomain(input) 114 if err != nil { 115 t.Error("unexpected error: ", err) 116 } 117 if domain != "v2fly.xn--55qw42g" { 118 t.Error("unexpected output: ", domain, " for test case ", input) 119 } 120 } 121 { // Test domain name of invalid character, which should return with error 122 input := "{" 123 _, err := ToDomain(input) 124 if err == nil { 125 t.Error("unexpected non error for test case ", input) 126 } 127 } 128 { // Test domain name containing a space, which should return with error 129 input := "Mijia Cloud" 130 _, err := ToDomain(input) 131 if err == nil { 132 t.Error("unexpected non error for test case ", input) 133 } 134 } 135 { // Test domain name containing an underscore, which should return with error 136 input := "Mijia_Cloud.com" 137 _, err := ToDomain(input) 138 if err == nil { 139 t.Error("unexpected non error for test case ", input) 140 } 141 } 142 { // Test internationalized domain containing invalid character 143 input := "Mijia Cloud.公司" 144 _, err := ToDomain(input) 145 if err == nil { 146 t.Error("unexpected non error for test case ", input) 147 } 148 } 149 }