github.com/hugh712/snapd@v0.0.0-20200910133618-1a99902bd583/osutil/udev/netlink/matcher_test.go (about) 1 package netlink 2 3 import "testing" 4 5 type Testcases []Testcase 6 7 type Testcase struct { 8 Object interface{} 9 Valid bool 10 } 11 12 func TestRules(testing *testing.T) { 13 t := testingWrapper{testing} 14 15 uevent := UEvent{ 16 Action: ADD, 17 KObj: "/devices/pci0000:00/0000:00:14.0/usb2/2-1/2-1:1.2/0003:04F2:0976.0008/hidraw/hidraw4", 18 Env: map[string]string{ 19 "ACTION": "add", 20 "DEVPATH": "/devices/pci0000:00/0000:00:14.0/usb2/2-1/2-1:1.2/0003:04F2:0976.0008/hidraw/hidraw4", 21 "SUBSYSTEM": "hidraw", 22 "MAJOR": "247", 23 "MINOR": "4", 24 "DEVNAME": "hidraw4", 25 "SEQNUM": "2569", 26 }, 27 } 28 29 add := ADD.String() 30 wrongAction := "can't match" 31 32 rules := []RuleDefinition{ 33 { 34 Action: nil, 35 Env: map[string]string{ 36 "DEVNAME": "hidraw\\d+", 37 }, 38 }, 39 40 { 41 Action: &add, 42 Env: make(map[string]string, 0), 43 }, 44 45 { 46 Action: nil, 47 Env: map[string]string{ 48 "SUBSYSTEM": "can't match", 49 "MAJOR": "247", 50 }, 51 }, 52 53 { 54 Action: &add, 55 Env: map[string]string{ 56 "SUBSYSTEM": "hidraw", 57 "MAJOR": "\\d+", 58 }, 59 }, 60 61 { 62 Action: &wrongAction, 63 Env: map[string]string{ 64 "SUBSYSTEM": "hidraw", 65 "MAJOR": "\\d+", 66 }, 67 }, 68 } 69 70 testcases := []Testcase{ 71 { 72 Object: &rules[0], 73 Valid: true, 74 }, 75 { 76 Object: &rules[1], 77 Valid: true, 78 }, 79 { 80 Object: &rules[2], 81 Valid: false, 82 }, 83 { 84 Object: &rules[3], 85 Valid: true, 86 }, 87 { 88 Object: &rules[4], 89 Valid: false, 90 }, 91 { 92 Object: &RuleDefinitions{[]RuleDefinition{rules[0], rules[4]}}, 93 Valid: true, 94 }, 95 { 96 Object: &RuleDefinitions{[]RuleDefinition{rules[4], rules[0]}}, 97 Valid: true, 98 }, 99 { 100 Object: &RuleDefinitions{[]RuleDefinition{rules[2], rules[4]}}, 101 Valid: false, 102 }, 103 { 104 Object: &RuleDefinitions{[]RuleDefinition{rules[3], rules[1]}}, 105 Valid: true, 106 }, 107 } 108 109 for k, tcase := range testcases { 110 matcher := tcase.Object.(Matcher) 111 112 err := matcher.Compile() 113 t.FatalfIf(err != nil, "Testcase n°%d should compile without error, err: %v", k+1, err) 114 115 ok := matcher.Evaluate(uevent) 116 t.FatalfIf((ok != tcase.Valid) && tcase.Valid, "Testcase n°%d should evaluate event", k+1) 117 t.FatalfIf((ok != tcase.Valid) && !tcase.Valid, "Testcase n°%d shouldn't evaluate event", k+1) 118 } 119 }