1
2 package devices
3
4 import (
5 "io/fs"
6 "path/filepath"
7
8 . "github.com/onsi/ginkgo/v2"
9 "github.com/onsi/gomega"
10 . "github.com/onsi/gomega"
11 )
12
13 var _ = Describe("Crawl device tree", func() {
14 It("Match all devices", func() {
15 opts := []Option{WithMatchAll(true)}
16 devices, err := Find(opts...)
17 Expect(err).To(BeNil())
18 Expect(devices).To(HaveLen(8))
19 })
20
21 It("Match all input devices", func() {
22 m := NewMatcher().WithRules(NewPropRule("SUBSYSTEM", "input"))
23 Expect(m.SearchPaths()).To(gomega.ContainElement(filepath.Join(SysPath, "class", "input")))
24 subSystemSymlinkFn = func(path string) (string, error) {
25 return filepath.Dir(filepath.Dir(path)), nil
26 }
27 opts := []Option{WithMatchers(m)}
28 devices, err := Find(opts...)
29 Expect(err).To(BeNil())
30 Expect(devices).To(HaveLen(3))
31 })
32
33 It("Match against idVendor and idProduct attributes", func() {
34 m := NewMatcher().WithRules(NewAttrRule("idProduct", "0002"), NewAttrRule("idVendor", "1d6b"), NewPropRule("SUBSYSTEM", "usb"))
35 Expect(m.SearchPaths()).To(gomega.ContainElement(filepath.Join(SysPath, "class", "usb")))
36 opts := []Option{WithMatchers(m)}
37 devices, err := Find(opts...)
38 Expect(err).To(BeNil())
39 Expect(devices).To(HaveLen(8))
40 })
41
42 It("Match against idVendor and idProduct attributes through glob", func() {
43 m := NewMatcher().WithRules(NewAttrRule("idProduct", "*"), NewAttrRule("idVendor", "*"), NewPropRule("SUBSYSTEM", "usb"))
44 Expect(m.SearchPaths()).To(gomega.ContainElement(filepath.Join(SysPath, "class", "usb")))
45 opts := []Option{WithMatchers(m)}
46 devices, err := Find(opts...)
47 Expect(err).To(BeNil())
48 Expect(devices).To(HaveLen(8))
49 })
50 })
51
52 var _ = Describe("Device has ascendants", func() {
53 It("Root device has no ascendants", func() {
54 opts := []Option{WithMatchAll(true)}
55 devices, err := Find(opts...)
56 Expect(err).To(BeNil())
57 Expect(hasAscendant(testDevicePath, devices)).To(BeFalse())
58 })
59
60 It("Immediate child of root device as ascendant", func() {
61 opts := []Option{WithMatchAll(true)}
62 devices, err := Find(opts...)
63 Expect(err).To(BeNil())
64 Expect(hasAscendant(filepath.Join(testDevicePath, "1-5"), devices)).To(BeTrue())
65 })
66
67 It("Deep rooted child device has ascendant", func() {
68 opts := []Option{WithMatchAll(true)}
69 devices, err := Find(opts...)
70 Expect(err).To(BeNil())
71 Expect(hasAscendant(filepath.Join(testDevicePath, "1-5", "1-5-1-0"), devices)).To(BeTrue())
72 })
73 })
74
75 var _ = Describe("Device symlink resolved to absolute path", func() {
76 It("Real absolute path", func() {
77 realPath, err := fetchRealPath(sysDevicePath)
78 Expect(err).To(BeNil())
79 Expect(realPath).To(BeEquivalentTo(sysDevicePath))
80 })
81
82 It("Symlink path", func() {
83 isSymlinkFn = func(_ fs.FileInfo) bool { return true }
84 realPath, err := fetchRealPath(filepath.Join(sysClassPath, "input"))
85 Expect(err).To(BeNil())
86
87
88 Expect(realPath).To(ContainSubstring(filepath.Join("pkg", "lib", "kernel", "devices", sysClassPath, "input")))
89 })
90 })
91
View as plain text