1
2
3 package reader
4
5 import (
6 "testing"
7
8 "github.com/stretchr/testify/require"
9
10 uevent "edge-infra.dev/pkg/lib/kernel/udev"
11 )
12
13 type ueventTest = []struct {
14 description string
15 inputEvent []byte
16 expectedEvent uevent.UEvent
17 }
18
19 var (
20 libudevHeader = "libudev\x00\xfe\xed\xca\xfe(\x00\x00\x00(\x00\x00\x00\xd5\x03\x00\x00\x8a\xfa\x90\xc8\x00\x00\x00\x00\x02\x00\x04\x00\x10\x80\x00\x00"
21 ttyUEvent = libudevHeader + "ACTION=remove\x00DEVPATH=/devices/pci\x00SUBSYSTEM=tty\x00DEVNAME=/dev/ttyUSB1\x00MAJOR=188\x00MINOR=122\x00SEQNUM=1\x00"
22 usbUEvent = libudevHeader + "ACTION=add\x00DEVPATH=/devices/bus/usb/001/002\x00SUBSYSTEM=usb\x00DEVNAME=/dev/usb1\x00MAJOR=188\x00MINOR=122\x00SEQNUM=2\x00"
23 )
24
25 var (
26 richEvent = libudevHeader +
27 "ACTION=add\x00DEVPATH=/devices/pci0000:00/0000:00:14.0/usb3/3-3\x00SUBSYSTEM=usb\x00DEVNAME=/dev/bus/usb/003/002\x00DEVTYPE=usb_device\x00" +
28 "PRODUCT=58f/6254/100\x00TYPE=0/0/0\x00BUSNUM=003\x00DEVNUM=002\x00SEQNUM=4410\x00MAJOR=189\x00MINOR=32\x00USEC_INITIALIZED=77155422759\x00" +
29 "ID_VENDOR=Alcor Micro Corp.\x00ID_VENDOR_ENC=Alcor Micro Corp.\x00ID_VENDOR_ID=10c4\x00ID_MODEL=6254\x00" +
30 "ID_MODEL_ENC=6254\x00ID_MODEL_ID=ea60\x00ID_REVISION=0100\x00" +
31 "ID_SERIAL=058f_6254\x00ID_SERIAL_SHORT=0001\x00ID_BUS=usb\x00ID_USB_INTERFACES=:ff0000:\x00" +
32 "ID_VENDOR_FROM_DATABASE=Cygnal Integrated Products, Inc.\x00ID_MODEL_FROM_DATABASE=USB Hub\x00" +
33 "DRIVER=usb\x00ID_MM_DEVICE_MANUAL_SCAN_ONLY=1\x00"
34 )
35
36 var ueventTests = ueventTest{
37 {
38 description: "decode tty event example",
39 inputEvent: []byte(ttyUEvent),
40 expectedEvent: uevent.UEvent{
41 Action: "remove",
42 SequenceNumber: "1",
43 EventType: uevent.UdevEvent,
44 SubSystem: "tty",
45 EnvVars: map[string]string{
46 "ACTION": "remove",
47 "DEVPATH": "/devices/pci",
48 "SUBSYSTEM": "tty",
49 "DEVNAME": "/dev/ttyUSB1",
50 "MAJOR": "188",
51 "MINOR": "122",
52 "SEQNUM": "1",
53 },
54 },
55 },
56 {
57 description: "decode usb event example",
58 inputEvent: []byte(usbUEvent),
59 expectedEvent: uevent.UEvent{
60 Action: "add",
61 SequenceNumber: "2",
62 EventType: uevent.UdevEvent,
63 SubSystem: "usb",
64 EnvVars: map[string]string{
65 "ACTION": "add",
66 "DEVPATH": "/devices/bus/usb/001/002",
67 "SUBSYSTEM": "usb",
68 "DEVNAME": "/dev/usb1",
69 "MAJOR": "188",
70 "MINOR": "122",
71 "SEQNUM": "2",
72 },
73 },
74 },
75 {
76 description: "decode rich uevent",
77 inputEvent: []byte(richEvent),
78 expectedEvent: uevent.UEvent{
79 Action: "add",
80 SequenceNumber: "4410",
81 EventType: uevent.UdevEvent,
82 SubSystem: "usb",
83 EnvVars: map[string]string{
84 "ACTION": "add",
85 "BUSNUM": "003",
86 "DEVNAME": "/dev/bus/usb/003/002",
87 "DEVNUM": "002",
88 "DEVPATH": "/devices/pci0000:00/0000:00:14.0/usb3/3-3",
89 "DEVTYPE": "usb_device",
90 "DRIVER": "usb",
91 "ID_BUS": "usb",
92 "ID_MM_DEVICE_MANUAL_SCAN_ONLY": "1",
93 "ID_MODEL": "6254",
94 "ID_MODEL_ENC": "6254",
95 "ID_MODEL_FROM_DATABASE": "USB Hub",
96 "ID_MODEL_ID": "ea60",
97 "ID_REVISION": "0100",
98 "ID_SERIAL": "058f_6254",
99 "ID_SERIAL_SHORT": "0001",
100 "ID_USB_INTERFACES": ":ff0000:",
101 "ID_VENDOR": "Alcor Micro Corp.",
102 "ID_VENDOR_ENC": "Alcor Micro Corp.",
103 "ID_VENDOR_FROM_DATABASE": "Cygnal Integrated Products, Inc.",
104 "ID_VENDOR_ID": "10c4",
105 "MAJOR": "189",
106 "MINOR": "32",
107 "PRODUCT": "58f/6254/100",
108 "SEQNUM": "4410",
109 "SUBSYSTEM": "usb",
110 "TYPE": "0/0/0",
111 "USEC_INITIALIZED": "77155422759",
112 },
113 },
114 },
115 }
116
117 func TestUdevEventDecoding(t *testing.T) {
118 for _, test := range ueventTests {
119 t.Log("Test:", test.description)
120 uevent, err := uevent.Decode(test.inputEvent)
121 require.NoError(t, err)
122 require.NotNil(t, uevent)
123 require.NotNil(t, uevent.ToBytes())
124 require.Equal(t, test.inputEvent, *uevent.ToBytes())
125 require.Equal(t, test.expectedEvent.Action, uevent.Action)
126 require.Equal(t, test.expectedEvent.EventType, uevent.EventType)
127 require.Equal(t, test.expectedEvent.SequenceNumber, uevent.SequenceNumber)
128 require.Equal(t, test.expectedEvent.SubSystem, uevent.SubSystem)
129 require.EqualValues(t, test.expectedEvent.EnvVars, uevent.EnvVars)
130 }
131 }
132
View as plain text