github.com/kelleygo/clashcore@v1.0.2/rules/logic_test/logic_test.go (about) 1 package logic_test 2 3 import ( 4 // https://github.com/golang/go/wiki/CodeReviewComments#import-dot 5 . "github.com/kelleygo/clashcore/rules/logic" 6 7 C "github.com/kelleygo/clashcore/constant" 8 "github.com/kelleygo/clashcore/rules" 9 "github.com/stretchr/testify/assert" 10 "testing" 11 ) 12 13 var ParseRule = rules.ParseRule 14 15 func TestAND(t *testing.T) { 16 and, err := NewAND("((DOMAIN,baidu.com),(NETWORK,TCP),(DST-PORT,10001-65535))", "DIRECT", ParseRule) 17 assert.Equal(t, nil, err) 18 assert.Equal(t, "DIRECT", and.Adapter()) 19 assert.Equal(t, false, and.ShouldResolveIP()) 20 m, _ := and.Match(&C.Metadata{ 21 Host: "baidu.com", 22 NetWork: C.TCP, 23 DstPort: 20000, 24 }) 25 assert.Equal(t, true, m) 26 27 and, err = NewAND("(DOMAIN,baidu.com),(NETWORK,TCP),(DST-PORT,10001-65535))", "DIRECT", ParseRule) 28 assert.NotEqual(t, nil, err) 29 30 and, err = NewAND("((AND,(DOMAIN,baidu.com),(NETWORK,TCP)),(NETWORK,TCP),(DST-PORT,10001-65535))", "DIRECT", ParseRule) 31 assert.Equal(t, nil, err) 32 } 33 34 func TestNOT(t *testing.T) { 35 not, err := NewNOT("((DST-PORT,6000-6500))", "REJECT", ParseRule) 36 assert.Equal(t, nil, err) 37 m, _ := not.Match(&C.Metadata{ 38 DstPort: 6100, 39 }) 40 assert.Equal(t, false, m) 41 42 _, err = NewNOT("((DST-PORT,5600-6666),(DOMAIN,baidu.com))", "DIRECT", ParseRule) 43 assert.NotEqual(t, nil, err) 44 45 _, err = NewNOT("(())", "DIRECT", ParseRule) 46 assert.NotEqual(t, nil, err) 47 } 48 49 func TestOR(t *testing.T) { 50 or, err := NewOR("((DOMAIN,baidu.com),(NETWORK,TCP),(DST-PORT,10001-65535))", "DIRECT", ParseRule) 51 assert.Equal(t, nil, err) 52 m, _ := or.Match(&C.Metadata{ 53 NetWork: C.TCP, 54 }) 55 assert.Equal(t, true, m) 56 assert.Equal(t, false, or.ShouldResolveIP()) 57 }