github.com/stolowski/snapd@v0.0.0-20210407085831-115137ce5a22/interfaces/builtin/i2c_test.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 2016 Canonical Ltd 5 * 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License version 3 as 8 * published by the Free Software Foundation. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License 16 * along with this program. If not, see <http://www.gnu.org/licenses/>. 17 * 18 */ 19 20 package builtin_test 21 22 import ( 23 "fmt" 24 25 . "gopkg.in/check.v1" 26 27 "github.com/snapcore/snapd/dirs" 28 "github.com/snapcore/snapd/interfaces" 29 "github.com/snapcore/snapd/interfaces/apparmor" 30 "github.com/snapcore/snapd/interfaces/builtin" 31 "github.com/snapcore/snapd/interfaces/udev" 32 "github.com/snapcore/snapd/snap" 33 "github.com/snapcore/snapd/snap/snaptest" 34 "github.com/snapcore/snapd/testutil" 35 ) 36 37 type I2cInterfaceSuite struct { 38 testutil.BaseTest 39 iface interfaces.Interface 40 41 // OS Snap 42 testSlot1 *interfaces.ConnectedSlot 43 testSlot1Info *snap.SlotInfo 44 testSlotCleaned *interfaces.ConnectedSlot 45 testSlotCleanedInfo *snap.SlotInfo 46 47 // Gadget Snap 48 testUDev1 *interfaces.ConnectedSlot 49 testUDev1Info *snap.SlotInfo 50 testUDev2 *interfaces.ConnectedSlot 51 testUDev2Info *snap.SlotInfo 52 testUDev3 *interfaces.ConnectedSlot 53 testUDev3Info *snap.SlotInfo 54 testSysfsName1 *interfaces.ConnectedSlot 55 testSysfsName1Info *snap.SlotInfo 56 testUDevBadValue1 *interfaces.ConnectedSlot 57 testUDevBadValue1Info *snap.SlotInfo 58 testUDevBadValue2 *interfaces.ConnectedSlot 59 testUDevBadValue2Info *snap.SlotInfo 60 testUDevBadValue3 *interfaces.ConnectedSlot 61 testUDevBadValue3Info *snap.SlotInfo 62 testUDevBadValue4 *interfaces.ConnectedSlot 63 testUDevBadValue4Info *snap.SlotInfo 64 testUDevBadValue5 *interfaces.ConnectedSlot 65 testUDevBadValue5Info *snap.SlotInfo 66 testUDevBadValue6 *interfaces.ConnectedSlot 67 testUDevBadValue6Info *snap.SlotInfo 68 testUDevBadValue7 *interfaces.ConnectedSlot 69 testUDevBadValue7Info *snap.SlotInfo 70 testUDevBadInterface1 *interfaces.ConnectedSlot 71 testUDevBadInterface1Info *snap.SlotInfo 72 testSysfsNameBadValue1 *interfaces.ConnectedSlot 73 testSysfsNameBadValue1Info *snap.SlotInfo 74 testSysfsNameAndPath *interfaces.ConnectedSlot 75 testSysfsNameAndPathInfo *snap.SlotInfo 76 testSysfsNameEmpty *interfaces.ConnectedSlot 77 testSysfsNameEmptyInfo *snap.SlotInfo 78 79 // Consuming Snap 80 testPlugPort1 *interfaces.ConnectedPlug 81 testPlugPort1Info *snap.PlugInfo 82 } 83 84 var _ = Suite(&I2cInterfaceSuite{ 85 iface: builtin.MustInterface("i2c"), 86 }) 87 88 func (s *I2cInterfaceSuite) SetUpTest(c *C) { 89 // Mock for OS Snap 90 osSnapInfo := snaptest.MockInfo(c, ` 91 name: ubuntu-core 92 version: 0 93 type: os 94 slots: 95 test-port-1: 96 interface: i2c 97 path: /dev/i2c-0 98 test-port-unclean: 99 interface: i2c 100 path: /dev/i2c-1/./ 101 `, nil) 102 s.testSlot1Info = osSnapInfo.Slots["test-port-1"] 103 s.testSlotCleanedInfo = osSnapInfo.Slots["test-port-unclean"] 104 105 // Mock for Gadget Snap 106 gadgetSnapInfo := snaptest.MockInfo(c, ` 107 name: some-device 108 version: 0 109 type: gadget 110 slots: 111 test-udev-1: 112 interface: i2c 113 path: /dev/i2c-1 114 test-udev-2: 115 interface: i2c 116 path: /dev/i2c-2 117 test-udev-3: 118 interface: i2c 119 path: /dev/i2c-0 120 test-sysfs-name-1: 121 interface: i2c 122 sysfs-name: 1-0050 123 test-udev-bad-value-1: 124 interface: i2c 125 path: /dev/i2c 126 test-udev-bad-value-2: 127 interface: i2c 128 path: /dev/i2c-a 129 test-udev-bad-value-3: 130 interface: i2c 131 path: /dev/i2c-2a 132 test-udev-bad-value-4: 133 interface: i2c 134 path: /dev/foo-0 135 test-udev-bad-value-5: 136 interface: i2c 137 path: /dev/i2c-foo 138 test-udev-bad-value-6: 139 interface: i2c 140 path: "" 141 test-udev-bad-value-7: 142 interface: i2c 143 test-udev-bad-interface-1: 144 interface: other-interface 145 test-sysfs-name-bad-value-1: 146 interface: i2c 147 sysfs-name: /slash/not/allowed 148 test-sysfs-name-and-path: 149 interface: i2c 150 path: /dev/i2c-0 151 sysfs-name: 1-0050 152 test-sysfs-name-empty: 153 interface: i2c 154 sysfs-name: "" 155 `, nil) 156 s.testUDev1Info = gadgetSnapInfo.Slots["test-udev-1"] 157 s.testUDev1 = interfaces.NewConnectedSlot(s.testUDev1Info, nil, nil) 158 s.testUDev2Info = gadgetSnapInfo.Slots["test-udev-2"] 159 s.testUDev2 = interfaces.NewConnectedSlot(s.testUDev2Info, nil, nil) 160 s.testUDev3Info = gadgetSnapInfo.Slots["test-udev-3"] 161 s.testUDev3 = interfaces.NewConnectedSlot(s.testUDev3Info, nil, nil) 162 s.testSysfsName1Info = gadgetSnapInfo.Slots["test-sysfs-name-1"] 163 s.testSysfsName1 = interfaces.NewConnectedSlot(s.testSysfsName1Info, nil, nil) 164 s.testUDevBadValue1Info = gadgetSnapInfo.Slots["test-udev-bad-value-1"] 165 s.testUDevBadValue1 = interfaces.NewConnectedSlot(s.testUDevBadValue1Info, nil, nil) 166 s.testUDevBadValue2Info = gadgetSnapInfo.Slots["test-udev-bad-value-2"] 167 s.testUDevBadValue2 = interfaces.NewConnectedSlot(s.testUDevBadValue2Info, nil, nil) 168 s.testUDevBadValue3Info = gadgetSnapInfo.Slots["test-udev-bad-value-3"] 169 s.testUDevBadValue3 = interfaces.NewConnectedSlot(s.testUDevBadValue3Info, nil, nil) 170 s.testUDevBadValue4Info = gadgetSnapInfo.Slots["test-udev-bad-value-4"] 171 s.testUDevBadValue4 = interfaces.NewConnectedSlot(s.testUDevBadValue4Info, nil, nil) 172 s.testUDevBadValue5Info = gadgetSnapInfo.Slots["test-udev-bad-value-5"] 173 s.testUDevBadValue5 = interfaces.NewConnectedSlot(s.testUDevBadValue5Info, nil, nil) 174 s.testUDevBadValue6Info = gadgetSnapInfo.Slots["test-udev-bad-value-6"] 175 s.testUDevBadValue6 = interfaces.NewConnectedSlot(s.testUDevBadValue6Info, nil, nil) 176 s.testUDevBadValue7Info = gadgetSnapInfo.Slots["test-udev-bad-value-7"] 177 s.testUDevBadValue7 = interfaces.NewConnectedSlot(s.testUDevBadValue7Info, nil, nil) 178 s.testUDevBadInterface1Info = gadgetSnapInfo.Slots["test-udev-bad-interface-1"] 179 s.testSysfsNameBadValue1Info = gadgetSnapInfo.Slots["test-sysfs-name-bad-value-1"] 180 s.testSysfsNameBadValue1 = interfaces.NewConnectedSlot(s.testSysfsNameBadValue1Info, nil, nil) 181 s.testSysfsNameAndPathInfo = gadgetSnapInfo.Slots["test-sysfs-name-and-path"] 182 s.testSysfsNameAndPath = interfaces.NewConnectedSlot(s.testSysfsNameAndPathInfo, nil, nil) 183 s.testSysfsNameEmptyInfo = gadgetSnapInfo.Slots["test-sysfs-name-empty"] 184 s.testSysfsNameEmpty = interfaces.NewConnectedSlot(s.testSysfsNameEmptyInfo, nil, nil) 185 186 // Snap Consumers 187 consumingSnapInfo := snaptest.MockInfo(c, ` 188 name: client-snap 189 version: 0 190 plugs: 191 plug-for-port-1: 192 interface: i2c 193 apps: 194 app-accessing-1-port: 195 command: foo 196 plugs: [i2c] 197 `, nil) 198 s.testPlugPort1Info = consumingSnapInfo.Plugs["plug-for-port-1"] 199 s.testPlugPort1 = interfaces.NewConnectedPlug(s.testPlugPort1Info, nil, nil) 200 } 201 202 func (s *I2cInterfaceSuite) TestName(c *C) { 203 c.Assert(s.iface.Name(), Equals, "i2c") 204 } 205 206 func (s *I2cInterfaceSuite) TestSanitizeCoreSnapSlot(c *C) { 207 c.Assert(interfaces.BeforePrepareSlot(s.iface, s.testSlot1Info), IsNil) 208 // Verify historically filepath.Clean()d paths are still valid 209 c.Assert(interfaces.BeforePrepareSlot(s.iface, s.testSlotCleanedInfo), IsNil) 210 } 211 212 func (s *I2cInterfaceSuite) TestSanitizeGadgetSnapSlot(c *C) { 213 c.Assert(interfaces.BeforePrepareSlot(s.iface, s.testUDev1Info), IsNil) 214 c.Assert(interfaces.BeforePrepareSlot(s.iface, s.testUDev2Info), IsNil) 215 c.Assert(interfaces.BeforePrepareSlot(s.iface, s.testUDev3Info), IsNil) 216 c.Assert(interfaces.BeforePrepareSlot(s.iface, s.testSysfsName1Info), IsNil) 217 } 218 219 func (s *I2cInterfaceSuite) TestSanitizeBadGadgetSnapSlot(c *C) { 220 c.Assert(interfaces.BeforePrepareSlot(s.iface, s.testUDevBadValue1Info), ErrorMatches, "i2c path attribute must be a valid device node") 221 c.Assert(interfaces.BeforePrepareSlot(s.iface, s.testUDevBadValue2Info), ErrorMatches, "i2c path attribute must be a valid device node") 222 c.Assert(interfaces.BeforePrepareSlot(s.iface, s.testUDevBadValue3Info), ErrorMatches, "i2c path attribute must be a valid device node") 223 c.Assert(interfaces.BeforePrepareSlot(s.iface, s.testUDevBadValue4Info), ErrorMatches, "i2c path attribute must be a valid device node") 224 c.Assert(interfaces.BeforePrepareSlot(s.iface, s.testUDevBadValue5Info), ErrorMatches, "i2c path attribute must be a valid device node") 225 c.Assert(interfaces.BeforePrepareSlot(s.iface, s.testUDevBadValue6Info), ErrorMatches, "i2c slot must have a path or sysfs-name attribute") 226 c.Assert(interfaces.BeforePrepareSlot(s.iface, s.testUDevBadValue7Info), ErrorMatches, "i2c slot must have a path or sysfs-name attribute") 227 c.Assert(interfaces.BeforePrepareSlot(s.iface, s.testSysfsNameBadValue1Info), ErrorMatches, "i2c sysfs-name attribute must be a valid sysfs-name") 228 c.Assert(interfaces.BeforePrepareSlot(s.iface, s.testSysfsNameAndPathInfo), ErrorMatches, "i2c slot can only use path or sysfs-name") 229 c.Assert(interfaces.BeforePrepareSlot(s.iface, s.testSysfsNameEmptyInfo), ErrorMatches, "i2c sysfs-name attribute must be a valid sysfs-name") 230 } 231 232 func (s *I2cInterfaceSuite) TestUDevSpec(c *C) { 233 spec := &udev.Specification{} 234 c.Assert(spec.AddConnectedPlug(s.iface, s.testPlugPort1, s.testUDev1), IsNil) 235 c.Assert(spec.Snippets(), HasLen, 2) 236 c.Assert(spec.Snippets(), testutil.Contains, `# i2c 237 KERNEL=="i2c-1", TAG+="snap_client-snap_app-accessing-1-port"`) 238 c.Assert(spec.Snippets(), testutil.Contains, fmt.Sprintf(`TAG=="snap_client-snap_app-accessing-1-port", RUN+="%v/snap-device-helper $env{ACTION} snap_client-snap_app-accessing-1-port $devpath $major:$minor"`, dirs.DistroLibExecDir)) 239 } 240 241 func (s *I2cInterfaceSuite) TestUDevSpecSysfsName(c *C) { 242 spec := &udev.Specification{} 243 c.Assert(spec.AddConnectedPlug(s.iface, s.testPlugPort1, s.testSysfsName1), IsNil) 244 c.Assert(spec.Snippets(), HasLen, 0) 245 } 246 247 func (s *I2cInterfaceSuite) TestAppArmorSpecPath(c *C) { 248 spec := &apparmor.Specification{} 249 c.Assert(spec.AddConnectedPlug(s.iface, s.testPlugPort1, s.testUDev1), IsNil) 250 c.Assert(spec.SecurityTags(), DeepEquals, []string{"snap.client-snap.app-accessing-1-port"}) 251 c.Assert(spec.SnippetForTag("snap.client-snap.app-accessing-1-port"), testutil.Contains, `/dev/i2c-1 rw,`) 252 c.Assert(spec.SnippetForTag("snap.client-snap.app-accessing-1-port"), testutil.Contains, `/sys/devices/platform/{*,**.i2c}/i2c-1/** rw, # Add any condensed parametric rules`) 253 } 254 255 func (s *I2cInterfaceSuite) TestAppArmorSpecPathMany(c *C) { 256 spec := &apparmor.Specification{} 257 c.Assert(spec.AddConnectedPlug(s.iface, s.testPlugPort1, s.testUDev1), IsNil) 258 c.Assert(spec.AddConnectedPlug(s.iface, s.testPlugPort1, s.testUDev2), IsNil) 259 // NOTE: the snap name is misleading. 260 c.Assert(spec.SecurityTags(), DeepEquals, []string{"snap.client-snap.app-accessing-1-port"}) 261 c.Assert(spec.SnippetForTag("snap.client-snap.app-accessing-1-port"), testutil.Contains, `/dev/i2c-1 rw,`) 262 c.Assert(spec.SnippetForTag("snap.client-snap.app-accessing-1-port"), testutil.Contains, `/dev/i2c-2 rw,`) 263 c.Assert(spec.SnippetForTag("snap.client-snap.app-accessing-1-port"), testutil.Contains, `/sys/devices/platform/{*,**.i2c}/i2c-{1,2}/** rw, # Add any condensed parametric rules`) 264 } 265 266 func (s *I2cInterfaceSuite) TestAppArmorSpecSysfsName(c *C) { 267 spec := &apparmor.Specification{} 268 c.Assert(spec.AddConnectedPlug(s.iface, s.testPlugPort1, s.testSysfsName1), IsNil) 269 c.Assert(spec.SecurityTags(), DeepEquals, []string{"snap.client-snap.app-accessing-1-port"}) 270 c.Assert(spec.SnippetForTag("snap.client-snap.app-accessing-1-port"), Equals, ` 271 # Description: Can access I2C sysfs name 272 273 /sys/bus/i2c/devices/1-0050/** rw, 274 `) 275 } 276 277 func (s *I2cInterfaceSuite) TestAutoConnect(c *C) { 278 c.Check(s.iface.AutoConnect(nil, nil), Equals, true) 279 } 280 281 func (s *I2cInterfaceSuite) TestInterfaces(c *C) { 282 c.Check(builtin.Interfaces(), testutil.DeepContains, s.iface) 283 }