github.com/igoogolx/clash@v1.19.8/rule/parser_test.go (about) 1 package rules 2 3 import ( 4 "errors" 5 "fmt" 6 "testing" 7 8 C "github.com/igoogolx/clash/constant" 9 10 "github.com/samber/lo" 11 "github.com/stretchr/testify/assert" 12 "github.com/stretchr/testify/require" 13 ) 14 15 func TestParseRule(t *testing.T) { 16 type testCase struct { 17 tp C.RuleConfig 18 payload string 19 target string 20 params []string 21 expectedRule C.Rule 22 expectedError error 23 } 24 25 policy := "DIRECT" 26 27 testCases := []testCase{ 28 { 29 tp: C.RuleConfigDomain, 30 payload: "example.com", 31 target: policy, 32 expectedRule: NewDomain("example.com", policy), 33 }, 34 { 35 tp: C.RuleConfigDomainSuffix, 36 payload: "example.com", 37 target: policy, 38 expectedRule: NewDomainSuffix("example.com", policy), 39 }, 40 { 41 tp: C.RuleConfigDomainKeyword, 42 payload: "example.com", 43 target: policy, 44 expectedRule: NewDomainKeyword("example.com", policy), 45 }, 46 { 47 tp: C.RuleConfigGeoIP, 48 payload: "CN", 49 target: policy, params: []string{noResolve}, 50 expectedRule: NewGEOIP("CN", policy, true), 51 }, 52 { 53 tp: C.RuleConfigIPCIDR, 54 payload: "127.0.0.0/8", 55 target: policy, 56 expectedRule: lo.Must(NewIPCIDR("127.0.0.0/8", policy, WithIPCIDRNoResolve(false))), 57 }, 58 { 59 tp: C.RuleConfigIPCIDR, 60 payload: "127.0.0.0/8", 61 target: policy, params: []string{noResolve}, 62 expectedRule: lo.Must(NewIPCIDR("127.0.0.0/8", policy, WithIPCIDRNoResolve(true))), 63 }, 64 { 65 tp: C.RuleConfigIPCIDR6, 66 payload: "2001:db8::/32", 67 target: policy, 68 expectedRule: lo.Must(NewIPCIDR("2001:db8::/32", policy, WithIPCIDRNoResolve(false))), 69 }, 70 { 71 tp: C.RuleConfigIPCIDR6, 72 payload: "2001:db8::/32", 73 target: policy, params: []string{noResolve}, 74 expectedRule: lo.Must(NewIPCIDR("2001:db8::/32", policy, WithIPCIDRNoResolve(true))), 75 }, 76 { 77 tp: C.RuleConfigSrcIPCIDR, 78 payload: "192.168.1.201/32", 79 target: policy, 80 expectedRule: lo.Must(NewIPCIDR("192.168.1.201/32", policy, WithIPCIDRSourceIP(true), WithIPCIDRNoResolve(true))), 81 }, 82 { 83 tp: C.RuleConfigSrcPort, 84 payload: "80", 85 target: policy, 86 expectedRule: lo.Must(NewPort("80", policy, PortTypeSrc)), 87 }, 88 { 89 tp: C.RuleConfigDstPort, 90 payload: "80", 91 target: policy, 92 expectedRule: lo.Must(NewPort("80", policy, PortTypeDest)), 93 }, 94 { 95 tp: C.RuleConfigInboundPort, 96 payload: "80", 97 target: policy, 98 expectedRule: lo.Must(NewPort("80", policy, PortTypeInbound)), 99 }, 100 { 101 tp: C.RuleConfigProcessName, 102 payload: "example.exe", 103 target: policy, 104 expectedRule: lo.Must(NewProcess("example.exe", policy, true)), 105 }, 106 { 107 tp: C.RuleConfigProcessPath, 108 payload: "C:\\Program Files\\example.exe", 109 target: policy, 110 expectedRule: lo.Must(NewProcess("C:\\Program Files\\example.exe", policy, false)), 111 }, 112 { 113 tp: C.RuleConfigProcessPath, 114 payload: "/opt/example/example", 115 target: policy, 116 expectedRule: lo.Must(NewProcess("/opt/example/example", policy, false)), 117 }, 118 { 119 tp: C.RuleConfigIPSet, 120 payload: "example", 121 target: policy, 122 // unit test runs on Linux machine and NewIPSet(...) won't be available 123 expectedError: errors.New("operation not permitted"), 124 }, 125 { 126 tp: C.RuleConfigIPSet, 127 payload: "example", 128 target: policy, params: []string{noResolve}, 129 // unit test runs on Linux machine and NewIPSet(...) won't be available 130 expectedError: errors.New("operation not permitted"), 131 }, 132 { 133 tp: C.RuleConfigMatch, 134 payload: "example", 135 target: policy, 136 expectedRule: NewMatch(policy), 137 }, 138 { 139 tp: C.RuleConfigRuleSet, 140 payload: "example", 141 target: policy, 142 expectedError: fmt.Errorf("unsupported rule type %s", C.RuleConfigRuleSet), 143 }, 144 { 145 tp: C.RuleConfigScript, 146 payload: "example", 147 target: policy, 148 expectedError: fmt.Errorf("unsupported rule type %s", C.RuleConfigScript), 149 }, 150 { 151 tp: "UNKNOWN", 152 payload: "example", 153 target: policy, 154 expectedError: errors.New("unsupported rule type UNKNOWN"), 155 }, 156 { 157 tp: "ABCD", 158 payload: "example", 159 target: policy, 160 expectedError: errors.New("unsupported rule type ABCD"), 161 }, 162 } 163 164 for _, tc := range testCases { 165 _, err := ParseRule(string(tc.tp), tc.payload, tc.target, tc.params) 166 if tc.expectedError != nil { 167 require.Error(t, err) 168 assert.EqualError(t, err, tc.expectedError.Error()) 169 } else { 170 require.NoError(t, err) 171 } 172 } 173 }