github.com/rigado/snapd@v2.42.5-go-mod+incompatible/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 . "gopkg.in/check.v1" 24 25 "github.com/snapcore/snapd/interfaces" 26 "github.com/snapcore/snapd/interfaces/apparmor" 27 "github.com/snapcore/snapd/interfaces/builtin" 28 "github.com/snapcore/snapd/interfaces/udev" 29 "github.com/snapcore/snapd/snap" 30 "github.com/snapcore/snapd/snap/snaptest" 31 "github.com/snapcore/snapd/testutil" 32 ) 33 34 type I2cInterfaceSuite struct { 35 testutil.BaseTest 36 iface interfaces.Interface 37 38 // OS Snap 39 testSlot1 *interfaces.ConnectedSlot 40 testSlot1Info *snap.SlotInfo 41 42 // Gadget Snap 43 testUDev1 *interfaces.ConnectedSlot 44 testUDev1Info *snap.SlotInfo 45 testUDev2 *interfaces.ConnectedSlot 46 testUDev2Info *snap.SlotInfo 47 testUDev3 *interfaces.ConnectedSlot 48 testUDev3Info *snap.SlotInfo 49 testSysfsName1 *interfaces.ConnectedSlot 50 testSysfsName1Info *snap.SlotInfo 51 testUDevBadValue1 *interfaces.ConnectedSlot 52 testUDevBadValue1Info *snap.SlotInfo 53 testUDevBadValue2 *interfaces.ConnectedSlot 54 testUDevBadValue2Info *snap.SlotInfo 55 testUDevBadValue3 *interfaces.ConnectedSlot 56 testUDevBadValue3Info *snap.SlotInfo 57 testUDevBadValue4 *interfaces.ConnectedSlot 58 testUDevBadValue4Info *snap.SlotInfo 59 testUDevBadValue5 *interfaces.ConnectedSlot 60 testUDevBadValue5Info *snap.SlotInfo 61 testUDevBadValue6 *interfaces.ConnectedSlot 62 testUDevBadValue6Info *snap.SlotInfo 63 testUDevBadValue7 *interfaces.ConnectedSlot 64 testUDevBadValue7Info *snap.SlotInfo 65 testUDevBadInterface1 *interfaces.ConnectedSlot 66 testUDevBadInterface1Info *snap.SlotInfo 67 testSysfsNameBadValue1 *interfaces.ConnectedSlot 68 testSysfsNameBadValue1Info *snap.SlotInfo 69 testSysfsNameAndPath *interfaces.ConnectedSlot 70 testSysfsNameAndPathInfo *snap.SlotInfo 71 testSysfsNameEmpty *interfaces.ConnectedSlot 72 testSysfsNameEmptyInfo *snap.SlotInfo 73 74 // Consuming Snap 75 testPlugPort1 *interfaces.ConnectedPlug 76 testPlugPort1Info *snap.PlugInfo 77 } 78 79 var _ = Suite(&I2cInterfaceSuite{ 80 iface: builtin.MustInterface("i2c"), 81 }) 82 83 func (s *I2cInterfaceSuite) SetUpTest(c *C) { 84 // Mock for OS Snap 85 osSnapInfo := snaptest.MockInfo(c, ` 86 name: ubuntu-core 87 version: 0 88 type: os 89 slots: 90 test-port-1: 91 interface: i2c 92 path: /dev/i2c-0 93 `, nil) 94 s.testSlot1Info = osSnapInfo.Slots["test-port-1"] 95 96 // Mock for Gadget Snap 97 gadgetSnapInfo := snaptest.MockInfo(c, ` 98 name: some-device 99 version: 0 100 type: gadget 101 slots: 102 test-udev-1: 103 interface: i2c 104 path: /dev/i2c-1 105 test-udev-2: 106 interface: i2c 107 path: /dev/i2c-11 108 test-udev-3: 109 interface: i2c 110 path: /dev/i2c-0 111 test-sysfs-name-1: 112 interface: i2c 113 sysfs-name: 1-0050 114 test-udev-bad-value-1: 115 interface: i2c 116 path: /dev/i2c 117 test-udev-bad-value-2: 118 interface: i2c 119 path: /dev/i2c-a 120 test-udev-bad-value-3: 121 interface: i2c 122 path: /dev/i2c-2a 123 test-udev-bad-value-4: 124 interface: i2c 125 path: /dev/foo-0 126 test-udev-bad-value-5: 127 interface: i2c 128 path: /dev/i2c-foo 129 test-udev-bad-value-6: 130 interface: i2c 131 path: "" 132 test-udev-bad-value-7: 133 interface: i2c 134 test-udev-bad-interface-1: 135 interface: other-interface 136 test-sysfs-name-bad-value-1: 137 interface: i2c 138 sysfs-name: /slash/not/allowed 139 test-sysfs-name-and-path: 140 interface: i2c 141 path: /dev/i2c-0 142 sysfs-name: 1-0050 143 test-sysfs-name-empty: 144 interface: i2c 145 sysfs-name: "" 146 `, nil) 147 s.testUDev1Info = gadgetSnapInfo.Slots["test-udev-1"] 148 s.testUDev1 = interfaces.NewConnectedSlot(s.testUDev1Info, nil, nil) 149 s.testUDev2Info = gadgetSnapInfo.Slots["test-udev-2"] 150 s.testUDev2 = interfaces.NewConnectedSlot(s.testUDev2Info, nil, nil) 151 s.testUDev3Info = gadgetSnapInfo.Slots["test-udev-3"] 152 s.testUDev3 = interfaces.NewConnectedSlot(s.testUDev3Info, nil, nil) 153 s.testSysfsName1Info = gadgetSnapInfo.Slots["test-sysfs-name-1"] 154 s.testSysfsName1 = interfaces.NewConnectedSlot(s.testSysfsName1Info, nil, nil) 155 s.testUDevBadValue1Info = gadgetSnapInfo.Slots["test-udev-bad-value-1"] 156 s.testUDevBadValue1 = interfaces.NewConnectedSlot(s.testUDevBadValue1Info, nil, nil) 157 s.testUDevBadValue2Info = gadgetSnapInfo.Slots["test-udev-bad-value-2"] 158 s.testUDevBadValue2 = interfaces.NewConnectedSlot(s.testUDevBadValue2Info, nil, nil) 159 s.testUDevBadValue3Info = gadgetSnapInfo.Slots["test-udev-bad-value-3"] 160 s.testUDevBadValue3 = interfaces.NewConnectedSlot(s.testUDevBadValue3Info, nil, nil) 161 s.testUDevBadValue4Info = gadgetSnapInfo.Slots["test-udev-bad-value-4"] 162 s.testUDevBadValue4 = interfaces.NewConnectedSlot(s.testUDevBadValue4Info, nil, nil) 163 s.testUDevBadValue5Info = gadgetSnapInfo.Slots["test-udev-bad-value-5"] 164 s.testUDevBadValue5 = interfaces.NewConnectedSlot(s.testUDevBadValue5Info, nil, nil) 165 s.testUDevBadValue6Info = gadgetSnapInfo.Slots["test-udev-bad-value-6"] 166 s.testUDevBadValue6 = interfaces.NewConnectedSlot(s.testUDevBadValue6Info, nil, nil) 167 s.testUDevBadValue7Info = gadgetSnapInfo.Slots["test-udev-bad-value-7"] 168 s.testUDevBadValue7 = interfaces.NewConnectedSlot(s.testUDevBadValue7Info, nil, nil) 169 s.testUDevBadInterface1Info = gadgetSnapInfo.Slots["test-udev-bad-interface-1"] 170 s.testSysfsNameBadValue1Info = gadgetSnapInfo.Slots["test-sysfs-name-bad-value-1"] 171 s.testSysfsNameBadValue1 = interfaces.NewConnectedSlot(s.testSysfsNameBadValue1Info, nil, nil) 172 s.testSysfsNameAndPathInfo = gadgetSnapInfo.Slots["test-sysfs-name-and-path"] 173 s.testSysfsNameAndPath = interfaces.NewConnectedSlot(s.testSysfsNameAndPathInfo, nil, nil) 174 s.testSysfsNameEmptyInfo = gadgetSnapInfo.Slots["test-sysfs-name-empty"] 175 s.testSysfsNameEmpty = interfaces.NewConnectedSlot(s.testSysfsNameEmptyInfo, nil, nil) 176 177 // Snap Consumers 178 consumingSnapInfo := snaptest.MockInfo(c, ` 179 name: client-snap 180 version: 0 181 plugs: 182 plug-for-port-1: 183 interface: i2c 184 apps: 185 app-accessing-1-port: 186 command: foo 187 plugs: [i2c] 188 `, nil) 189 s.testPlugPort1Info = consumingSnapInfo.Plugs["plug-for-port-1"] 190 s.testPlugPort1 = interfaces.NewConnectedPlug(s.testPlugPort1Info, nil, nil) 191 } 192 193 func (s *I2cInterfaceSuite) TestName(c *C) { 194 c.Assert(s.iface.Name(), Equals, "i2c") 195 } 196 197 func (s *I2cInterfaceSuite) TestSanitizeCoreSnapSlot(c *C) { 198 c.Assert(interfaces.BeforePrepareSlot(s.iface, s.testSlot1Info), IsNil) 199 } 200 201 func (s *I2cInterfaceSuite) TestSanitizeGadgetSnapSlot(c *C) { 202 c.Assert(interfaces.BeforePrepareSlot(s.iface, s.testUDev1Info), IsNil) 203 c.Assert(interfaces.BeforePrepareSlot(s.iface, s.testUDev2Info), IsNil) 204 c.Assert(interfaces.BeforePrepareSlot(s.iface, s.testUDev3Info), IsNil) 205 c.Assert(interfaces.BeforePrepareSlot(s.iface, s.testSysfsName1Info), IsNil) 206 } 207 208 func (s *I2cInterfaceSuite) TestSanitizeBadGadgetSnapSlot(c *C) { 209 c.Assert(interfaces.BeforePrepareSlot(s.iface, s.testUDevBadValue1Info), ErrorMatches, "i2c path attribute must be a valid device node") 210 c.Assert(interfaces.BeforePrepareSlot(s.iface, s.testUDevBadValue2Info), ErrorMatches, "i2c path attribute must be a valid device node") 211 c.Assert(interfaces.BeforePrepareSlot(s.iface, s.testUDevBadValue3Info), ErrorMatches, "i2c path attribute must be a valid device node") 212 c.Assert(interfaces.BeforePrepareSlot(s.iface, s.testUDevBadValue4Info), ErrorMatches, "i2c path attribute must be a valid device node") 213 c.Assert(interfaces.BeforePrepareSlot(s.iface, s.testUDevBadValue5Info), ErrorMatches, "i2c path attribute must be a valid device node") 214 c.Assert(interfaces.BeforePrepareSlot(s.iface, s.testUDevBadValue6Info), ErrorMatches, "i2c slot must have a path or sysfs-name attribute") 215 c.Assert(interfaces.BeforePrepareSlot(s.iface, s.testUDevBadValue7Info), ErrorMatches, "i2c slot must have a path or sysfs-name attribute") 216 c.Assert(interfaces.BeforePrepareSlot(s.iface, s.testSysfsNameBadValue1Info), ErrorMatches, "i2c sysfs-name attribute must be a valid sysfs-name") 217 c.Assert(interfaces.BeforePrepareSlot(s.iface, s.testSysfsNameAndPathInfo), ErrorMatches, "i2c slot can only use path or sysfs-name") 218 c.Assert(interfaces.BeforePrepareSlot(s.iface, s.testSysfsNameEmptyInfo), ErrorMatches, "i2c sysfs-name attribute must be a valid sysfs-name") 219 } 220 221 func (s *I2cInterfaceSuite) TestUDevSpec(c *C) { 222 spec := &udev.Specification{} 223 c.Assert(spec.AddConnectedPlug(s.iface, s.testPlugPort1, s.testUDev1), IsNil) 224 c.Assert(spec.Snippets(), HasLen, 2) 225 c.Assert(spec.Snippets(), testutil.Contains, `# i2c 226 KERNEL=="i2c-1", TAG+="snap_client-snap_app-accessing-1-port"`) 227 c.Assert(spec.Snippets(), testutil.Contains, `TAG=="snap_client-snap_app-accessing-1-port", RUN+="/usr/lib/snapd/snap-device-helper $env{ACTION} snap_client-snap_app-accessing-1-port $devpath $major:$minor"`) 228 } 229 230 func (s *I2cInterfaceSuite) TestUDevSpecSysfsName(c *C) { 231 spec := &udev.Specification{} 232 c.Assert(spec.AddConnectedPlug(s.iface, s.testPlugPort1, s.testSysfsName1), IsNil) 233 c.Assert(spec.Snippets(), HasLen, 0) 234 } 235 236 func (s *I2cInterfaceSuite) TestAppArmorSpecPath(c *C) { 237 spec := &apparmor.Specification{} 238 c.Assert(spec.AddConnectedPlug(s.iface, s.testPlugPort1, s.testUDev1), IsNil) 239 c.Assert(spec.SecurityTags(), DeepEquals, []string{"snap.client-snap.app-accessing-1-port"}) 240 c.Assert(spec.SnippetForTag("snap.client-snap.app-accessing-1-port"), testutil.Contains, `/dev/i2c-1 rw,`) 241 c.Assert(spec.SnippetForTag("snap.client-snap.app-accessing-1-port"), testutil.Contains, `/sys/devices/platform/{*,**.i2c}/i2c-1/** rw,`) 242 } 243 244 func (s *I2cInterfaceSuite) TestAppArmorSpecSysfsName(c *C) { 245 spec := &apparmor.Specification{} 246 c.Assert(spec.AddConnectedPlug(s.iface, s.testPlugPort1, s.testSysfsName1), IsNil) 247 c.Assert(spec.SecurityTags(), DeepEquals, []string{"snap.client-snap.app-accessing-1-port"}) 248 c.Assert(spec.SnippetForTag("snap.client-snap.app-accessing-1-port"), Equals, ` 249 # Description: Can access I2C sysfs name 250 251 /sys/bus/i2c/devices/1-0050/** rw, 252 `) 253 } 254 255 func (s *I2cInterfaceSuite) TestAutoConnect(c *C) { 256 c.Check(s.iface.AutoConnect(nil, nil), Equals, true) 257 } 258 259 func (s *I2cInterfaceSuite) TestInterfaces(c *C) { 260 c.Check(builtin.Interfaces(), testutil.DeepContains, s.iface) 261 }