1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
|
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2016 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package builtin_test
import (
"fmt"
. "gopkg.in/check.v1"
"github.com/snapcore/snapd/dirs"
"github.com/snapcore/snapd/interfaces"
"github.com/snapcore/snapd/interfaces/apparmor"
"github.com/snapcore/snapd/interfaces/builtin"
"github.com/snapcore/snapd/interfaces/udev"
"github.com/snapcore/snapd/snap"
"github.com/snapcore/snapd/snap/snaptest"
"github.com/snapcore/snapd/testutil"
)
type HidrawInterfaceSuite struct {
testutil.BaseTest
iface interfaces.Interface
// OS Snap
osSnapInfo *snap.Info
testSlot1 *interfaces.ConnectedSlot
testSlot1Info *snap.SlotInfo
testSlot2 *interfaces.ConnectedSlot
testSlot2Info *snap.SlotInfo
testSlotCleaned *interfaces.ConnectedSlot
testSlotCleanedInfo *snap.SlotInfo
missingPathSlot *interfaces.ConnectedSlot
missingPathSlotInfo *snap.SlotInfo
badPathSlot1 *interfaces.ConnectedSlot
badPathSlot1Info *snap.SlotInfo
badPathSlot2 *interfaces.ConnectedSlot
badPathSlot2Info *snap.SlotInfo
badPathSlot3 *interfaces.ConnectedSlot
badPathSlot3Info *snap.SlotInfo
badInterfaceSlot *interfaces.ConnectedSlot
badInterfaceSlotInfo *snap.SlotInfo
// Gadget Snap
gadgetSnapInfo *snap.Info
testUDev1 *interfaces.ConnectedSlot
testUDev1Info *snap.SlotInfo
testUDev2 *interfaces.ConnectedSlot
testUDev2Info *snap.SlotInfo
testUDevBadValue1 *interfaces.ConnectedSlot
testUDevBadValue1Info *snap.SlotInfo
testUDevBadValue2 *interfaces.ConnectedSlot
testUDevBadValue2Info *snap.SlotInfo
testUDevBadValue3 *interfaces.ConnectedSlot
testUDevBadValue3Info *snap.SlotInfo
// Consuming Snap
testPlugPort1 *interfaces.ConnectedPlug
testPlugPort1Info *snap.PlugInfo
testPlugPort2 *interfaces.ConnectedPlug
testPlugPort2Info *snap.PlugInfo
testPlugPort3 *interfaces.ConnectedPlug
testPlugPort3Info *snap.PlugInfo
}
var _ = Suite(&HidrawInterfaceSuite{
iface: builtin.MustInterface("hidraw"),
})
func (s *HidrawInterfaceSuite) SetUpTest(c *C) {
s.osSnapInfo = snaptest.MockInfo(c, `
name: ubuntu-core
version: 0
type: os
slots:
test-port-1:
interface: hidraw
path: /dev/hidraw0
test-port-2:
interface: hidraw
path: /dev/hidraw987
test-port-unclean:
interface: hidraw
path: /dev/./././hidraw876
missing-path: hidraw
bad-path-1:
interface: hidraw
path: path
bad-path-2:
interface: hidraw
path: /dev/hid0
bad-path-3:
interface: hidraw
path: /dev/hidraw9271
bad-interface: other-interface
`, nil)
appSet, err := interfaces.NewSnapAppSet(s.osSnapInfo, nil)
c.Assert(err, IsNil)
s.testSlot1Info = s.osSnapInfo.Slots["test-port-1"]
s.testSlot1 = interfaces.NewConnectedSlot(s.testSlot1Info, appSet, nil, nil)
s.testSlot2Info = s.osSnapInfo.Slots["test-port-2"]
s.testSlot2 = interfaces.NewConnectedSlot(s.testSlot2Info, appSet, nil, nil)
s.testSlotCleanedInfo = s.osSnapInfo.Slots["test-port-unclean"]
s.testSlotCleaned = interfaces.NewConnectedSlot(s.testSlotCleanedInfo, appSet, nil, nil)
s.missingPathSlotInfo = s.osSnapInfo.Slots["missing-path"]
s.missingPathSlot = interfaces.NewConnectedSlot(s.missingPathSlotInfo, appSet, nil, nil)
s.badPathSlot1Info = s.osSnapInfo.Slots["bad-path-1"]
s.badPathSlot1 = interfaces.NewConnectedSlot(s.badPathSlot1Info, appSet, nil, nil)
s.badPathSlot2Info = s.osSnapInfo.Slots["bad-path-2"]
s.badPathSlot2 = interfaces.NewConnectedSlot(s.badPathSlot2Info, appSet, nil, nil)
s.badPathSlot3Info = s.osSnapInfo.Slots["bad-path-3"]
s.badPathSlot3 = interfaces.NewConnectedSlot(s.badPathSlot3Info, appSet, nil, nil)
s.badInterfaceSlotInfo = s.osSnapInfo.Slots["bad-interface"]
s.badInterfaceSlot = interfaces.NewConnectedSlot(s.badInterfaceSlotInfo, appSet, nil, nil)
s.gadgetSnapInfo = snaptest.MockInfo(c, `
name: some-device
version: 0
type: gadget
slots:
test-udev-1:
interface: hidraw
usb-vendor: 0x0001
usb-product: 0x0001
path: /dev/hidraw-canbus
test-udev-2:
interface: hidraw
usb-vendor: 0xffff
usb-product: 0xffff
path: /dev/hidraw-mydevice
test-udev-bad-value-1:
interface: hidraw
usb-vendor: -1
usb-product: 0xffff
path: /dev/hidraw-mydevice
test-udev-bad-value-2:
interface: hidraw
usb-vendor: 0x1234
usb-product: 0x10000
path: /dev/hidraw-mydevice
test-udev-bad-value-3:
interface: hidraw
usb-vendor: 0x789a
usb-product: 0x4321
path: /dev/my-device
`, nil)
appSet, err = interfaces.NewSnapAppSet(s.gadgetSnapInfo, nil)
c.Assert(err, IsNil)
s.testUDev1Info = s.gadgetSnapInfo.Slots["test-udev-1"]
s.testUDev1 = interfaces.NewConnectedSlot(s.testUDev1Info, appSet, nil, nil)
s.testUDev2Info = s.gadgetSnapInfo.Slots["test-udev-2"]
s.testUDev2 = interfaces.NewConnectedSlot(s.testUDev2Info, appSet, nil, nil)
s.testUDevBadValue1Info = s.gadgetSnapInfo.Slots["test-udev-bad-value-1"]
s.testUDevBadValue1 = interfaces.NewConnectedSlot(s.testUDevBadValue1Info, appSet, nil, nil)
s.testUDevBadValue2Info = s.gadgetSnapInfo.Slots["test-udev-bad-value-2"]
s.testUDevBadValue2 = interfaces.NewConnectedSlot(s.testUDevBadValue2Info, appSet, nil, nil)
s.testUDevBadValue3Info = s.gadgetSnapInfo.Slots["test-udev-bad-value-3"]
s.testUDevBadValue3 = interfaces.NewConnectedSlot(s.testUDevBadValue3Info, appSet, nil, nil)
consumingSnapInfo := snaptest.MockInfo(c, `
name: client-snap
version: 0
plugs:
plug-for-device-1:
interface: hidraw
plug-for-device-2:
interface: hidraw
plug-for-device-3:
interface: hidraw
apps:
app-accessing-1-device:
command: foo
plugs: [hidraw]
app-accessing-2-devices:
command: bar
plugs: [plug-for-device-1, plug-for-device-2]
app-accessing-3rd-device:
command: baz
plugs: [plug-for-device-3]
`, nil)
appSet, err = interfaces.NewSnapAppSet(consumingSnapInfo, nil)
c.Assert(err, IsNil)
s.testPlugPort1Info = consumingSnapInfo.Plugs["plug-for-device-1"]
s.testPlugPort1 = interfaces.NewConnectedPlug(s.testPlugPort1Info, appSet, nil, nil)
s.testPlugPort2Info = consumingSnapInfo.Plugs["plug-for-device-2"]
s.testPlugPort2 = interfaces.NewConnectedPlug(s.testPlugPort2Info, appSet, nil, nil)
s.testPlugPort3Info = consumingSnapInfo.Plugs["plug-for-device-3"]
s.testPlugPort3 = interfaces.NewConnectedPlug(s.testPlugPort3Info, appSet, nil, nil)
}
func (s *HidrawInterfaceSuite) TestName(c *C) {
c.Assert(s.iface.Name(), Equals, "hidraw")
}
func (s *HidrawInterfaceSuite) TestSanitizeCoreSnapSlots(c *C) {
for _, slot := range []*snap.SlotInfo{s.testSlot1Info, s.testSlot2Info} {
c.Assert(interfaces.BeforePrepareSlot(s.iface, slot), IsNil)
}
// Verify historically filepath.Clean()d paths are still valid
c.Assert(interfaces.BeforePrepareSlot(s.iface, s.testSlotCleanedInfo), IsNil)
}
func (s *HidrawInterfaceSuite) TestSanitizeBadCoreSnapSlots(c *C) {
// Slots without the "path" attribute are rejected.
c.Assert(interfaces.BeforePrepareSlot(s.iface, s.missingPathSlotInfo), ErrorMatches,
`hidraw slots must have a path attribute`)
// Slots with incorrect value of the "path" attribute are rejected.
for _, slot := range []*snap.SlotInfo{s.badPathSlot1Info, s.badPathSlot2Info, s.badPathSlot3Info} {
c.Assert(interfaces.BeforePrepareSlot(s.iface, slot), ErrorMatches, "hidraw path attribute must be a valid device node")
}
}
func (s *HidrawInterfaceSuite) TestSanitizeGadgetSnapSlots(c *C) {
c.Assert(interfaces.BeforePrepareSlot(s.iface, s.testUDev1Info), IsNil)
c.Assert(interfaces.BeforePrepareSlot(s.iface, s.testUDev2Info), IsNil)
}
func (s *HidrawInterfaceSuite) TestSanitizeBadGadgetSnapSlots(c *C) {
c.Assert(interfaces.BeforePrepareSlot(s.iface, s.testUDevBadValue1Info), ErrorMatches, "hidraw usb-vendor attribute not valid: -1")
c.Assert(interfaces.BeforePrepareSlot(s.iface, s.testUDevBadValue2Info), ErrorMatches, "hidraw usb-product attribute not valid: 65536")
c.Assert(interfaces.BeforePrepareSlot(s.iface, s.testUDevBadValue3Info), ErrorMatches, "hidraw path attribute specifies invalid symlink location")
}
func (s *HidrawInterfaceSuite) TestPermanentSlotUDevSnippets(c *C) {
spec := udev.NewSpecification(s.testSlot1.AppSet())
for _, slot := range []*snap.SlotInfo{s.testSlot1Info, s.testSlot2Info} {
c.Assert(spec.AddPermanentSlot(s.iface, slot), IsNil)
c.Assert(spec.Snippets(), HasLen, 0)
}
expectedSnippet1 := `# hidraw
IMPORT{builtin}="usb_id"
SUBSYSTEM=="hidraw", SUBSYSTEMS=="usb", ATTRS{idVendor}=="0001", ATTRS{idProduct}=="0001", SYMLINK+="hidraw-canbus"`
spec = udev.NewSpecification(s.testUDev1.AppSet())
c.Assert(spec.AddPermanentSlot(s.iface, s.testUDev1Info), IsNil)
c.Assert(spec.Snippets(), HasLen, 1)
snippet := spec.Snippets()[0]
c.Assert(snippet, Equals, expectedSnippet1)
expectedSnippet2 := `# hidraw
IMPORT{builtin}="usb_id"
SUBSYSTEM=="hidraw", SUBSYSTEMS=="usb", ATTRS{idVendor}=="ffff", ATTRS{idProduct}=="ffff", SYMLINK+="hidraw-mydevice"`
spec = udev.NewSpecification(s.testUDev2.AppSet())
c.Assert(spec.AddPermanentSlot(s.iface, s.testUDev2Info), IsNil)
c.Assert(spec.Snippets(), HasLen, 1)
snippet = spec.Snippets()[0]
c.Assert(snippet, Equals, expectedSnippet2)
}
func (s *HidrawInterfaceSuite) TestConnectedPlugUDevSnippets(c *C) {
// add the plug for the slot with just path
spec := udev.NewSpecification(s.testPlugPort1.AppSet())
c.Assert(spec.AddConnectedPlug(s.iface, s.testPlugPort1, s.testSlot1), IsNil)
c.Assert(spec.Snippets(), HasLen, 2)
snippet := spec.Snippets()[0]
expectedSnippet1 := `# hidraw
SUBSYSTEM=="hidraw", KERNEL=="hidraw0", TAG+="snap_client-snap_app-accessing-2-devices"`
c.Assert(snippet, Equals, expectedSnippet1)
extraSnippet := spec.Snippets()[1]
expectedExtraSnippet1 := fmt.Sprintf(`TAG=="snap_client-snap_app-accessing-2-devices", SUBSYSTEM!="module", SUBSYSTEM!="subsystem", RUN+="%v/snap-device-helper $env{ACTION} snap_client-snap_app-accessing-2-devices $devpath $major:$minor"`, dirs.DistroLibExecDir)
c.Assert(extraSnippet, Equals, expectedExtraSnippet1)
// add the plug for the first slot with vendor and product ids
spec = udev.NewSpecification(s.testPlugPort1.AppSet())
c.Assert(spec.AddConnectedPlug(s.iface, s.testPlugPort1, s.testUDev1), IsNil)
c.Assert(spec.Snippets(), HasLen, 2)
snippet = spec.Snippets()[0]
expectedSnippet2 := `# hidraw
IMPORT{builtin}="usb_id"
SUBSYSTEM=="hidraw", SUBSYSTEMS=="usb", ATTRS{idVendor}=="0001", ATTRS{idProduct}=="0001", TAG+="snap_client-snap_app-accessing-2-devices"`
c.Assert(snippet, Equals, expectedSnippet2)
extraSnippet = spec.Snippets()[1]
expectedExtraSnippet2 := fmt.Sprintf(`TAG=="snap_client-snap_app-accessing-2-devices", SUBSYSTEM!="module", SUBSYSTEM!="subsystem", RUN+="%v/snap-device-helper $env{ACTION} snap_client-snap_app-accessing-2-devices $devpath $major:$minor"`, dirs.DistroLibExecDir)
c.Assert(extraSnippet, Equals, expectedExtraSnippet2)
// add the plug for the second slot with vendor and product ids
spec = udev.NewSpecification(s.testPlugPort2.AppSet())
c.Assert(spec.AddConnectedPlug(s.iface, s.testPlugPort2, s.testUDev2), IsNil)
c.Assert(spec.Snippets(), HasLen, 2)
snippet = spec.Snippets()[0]
expectedSnippet3 := `# hidraw
IMPORT{builtin}="usb_id"
SUBSYSTEM=="hidraw", SUBSYSTEMS=="usb", ATTRS{idVendor}=="ffff", ATTRS{idProduct}=="ffff", TAG+="snap_client-snap_app-accessing-2-devices"`
c.Assert(snippet, Equals, expectedSnippet3)
extraSnippet = spec.Snippets()[1]
expectedExtraSnippet3 := fmt.Sprintf(`TAG=="snap_client-snap_app-accessing-2-devices", SUBSYSTEM!="module", SUBSYSTEM!="subsystem", RUN+="%v/snap-device-helper $env{ACTION} snap_client-snap_app-accessing-2-devices $devpath $major:$minor"`, dirs.DistroLibExecDir)
c.Assert(extraSnippet, Equals, expectedExtraSnippet3)
}
func (s *HidrawInterfaceSuite) TestConnectedPlugAppArmorSnippets(c *C) {
expectedSnippet1 := `/dev/hidraw0 rw,`
apparmorSpec := apparmor.NewSpecification(s.testPlugPort1.AppSet())
err := apparmorSpec.AddConnectedPlug(s.iface, s.testPlugPort1, s.testSlot1)
c.Assert(err, IsNil)
c.Assert(apparmorSpec.SecurityTags(), DeepEquals, []string{"snap.client-snap.app-accessing-2-devices"})
snippet := apparmorSpec.SnippetForTag("snap.client-snap.app-accessing-2-devices")
c.Assert(snippet, DeepEquals, expectedSnippet1)
expectedSnippet2 := `/dev/hidraw[0-9]{,[0-9],[0-9][0-9]} rw,`
apparmorSpec = apparmor.NewSpecification(s.testPlugPort1.AppSet())
err = apparmorSpec.AddConnectedPlug(s.iface, s.testPlugPort1, s.testUDev1)
c.Assert(err, IsNil)
c.Assert(apparmorSpec.SecurityTags(), DeepEquals, []string{"snap.client-snap.app-accessing-2-devices"})
snippet = apparmorSpec.SnippetForTag("snap.client-snap.app-accessing-2-devices")
c.Assert(snippet, DeepEquals, expectedSnippet2)
expectedSnippet3 := `/dev/hidraw[0-9]{,[0-9],[0-9][0-9]} rw,`
apparmorSpec = apparmor.NewSpecification(s.testPlugPort2.AppSet())
err = apparmorSpec.AddConnectedPlug(s.iface, s.testPlugPort2, s.testUDev2)
c.Assert(err, IsNil)
c.Assert(apparmorSpec.SecurityTags(), DeepEquals, []string{"snap.client-snap.app-accessing-2-devices"})
snippet = apparmorSpec.SnippetForTag("snap.client-snap.app-accessing-2-devices")
c.Assert(snippet, DeepEquals, expectedSnippet3)
}
func (s *HidrawInterfaceSuite) TestConnectedPlugUDevSnippetsForPath(c *C) {
expectedSnippet1 := `# hidraw
SUBSYSTEM=="hidraw", KERNEL=="hidraw0", TAG+="snap_client-snap_app-accessing-2-devices"`
expectedExtraSnippet1 := fmt.Sprintf(`TAG=="snap_client-snap_app-accessing-2-devices", SUBSYSTEM!="module", SUBSYSTEM!="subsystem", RUN+="%v/snap-device-helper $env{ACTION} snap_client-snap_app-accessing-2-devices $devpath $major:$minor"`, dirs.DistroLibExecDir)
udevSpec := udev.NewSpecification(s.testPlugPort1.AppSet())
err := udevSpec.AddConnectedPlug(s.iface, s.testPlugPort1, s.testSlot1)
c.Assert(err, IsNil)
c.Assert(udevSpec.Snippets(), HasLen, 2)
snippet := udevSpec.Snippets()[0]
c.Assert(snippet, Equals, expectedSnippet1)
extraSnippet := udevSpec.Snippets()[1]
c.Assert(extraSnippet, Equals, expectedExtraSnippet1)
expectedSnippet2 := `# hidraw
IMPORT{builtin}="usb_id"
SUBSYSTEM=="hidraw", SUBSYSTEMS=="usb", ATTRS{idVendor}=="0001", ATTRS{idProduct}=="0001", TAG+="snap_client-snap_app-accessing-2-devices"`
expectedExtraSnippet2 := fmt.Sprintf(`TAG=="snap_client-snap_app-accessing-2-devices", SUBSYSTEM!="module", SUBSYSTEM!="subsystem", RUN+="%v/snap-device-helper $env{ACTION} snap_client-snap_app-accessing-2-devices $devpath $major:$minor"`, dirs.DistroLibExecDir)
udevSpec = udev.NewSpecification(s.testPlugPort1.AppSet())
err = udevSpec.AddConnectedPlug(s.iface, s.testPlugPort1, s.testUDev1)
c.Assert(err, IsNil)
c.Assert(udevSpec.Snippets(), HasLen, 2)
snippet = udevSpec.Snippets()[0]
c.Assert(snippet, Equals, expectedSnippet2)
extraSnippet = udevSpec.Snippets()[1]
c.Assert(extraSnippet, Equals, expectedExtraSnippet2)
expectedSnippet3 := `# hidraw
IMPORT{builtin}="usb_id"
SUBSYSTEM=="hidraw", SUBSYSTEMS=="usb", ATTRS{idVendor}=="ffff", ATTRS{idProduct}=="ffff", TAG+="snap_client-snap_app-accessing-2-devices"`
expectedExtraSnippet3 := fmt.Sprintf(`TAG=="snap_client-snap_app-accessing-2-devices", SUBSYSTEM!="module", SUBSYSTEM!="subsystem", RUN+="%v/snap-device-helper $env{ACTION} snap_client-snap_app-accessing-2-devices $devpath $major:$minor"`, dirs.DistroLibExecDir)
udevSpec = udev.NewSpecification(s.testPlugPort2.AppSet())
err = udevSpec.AddConnectedPlug(s.iface, s.testPlugPort2, s.testUDev2)
c.Assert(err, IsNil)
c.Assert(udevSpec.Snippets(), HasLen, 2)
snippet = udevSpec.Snippets()[0]
c.Assert(snippet, Equals, expectedSnippet3)
extraSnippet = udevSpec.Snippets()[1]
c.Assert(extraSnippet, Equals, expectedExtraSnippet3)
}
func (s *HidrawInterfaceSuite) TestInterfaces(c *C) {
c.Check(builtin.Interfaces(), testutil.DeepContains, s.iface)
}
|