gitee.com/mysnapcore/mysnapd@v0.1.0/interfaces/builtin/raw_input_test.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 2021 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 "gitee.com/mysnapcore/mysnapd/dirs" 28 "gitee.com/mysnapcore/mysnapd/interfaces" 29 "gitee.com/mysnapcore/mysnapd/interfaces/apparmor" 30 "gitee.com/mysnapcore/mysnapd/interfaces/builtin" 31 "gitee.com/mysnapcore/mysnapd/interfaces/seccomp" 32 "gitee.com/mysnapcore/mysnapd/interfaces/udev" 33 "gitee.com/mysnapcore/mysnapd/snap" 34 "gitee.com/mysnapcore/mysnapd/testutil" 35 ) 36 37 type RawInputInterfaceSuite struct { 38 iface interfaces.Interface 39 slotInfo *snap.SlotInfo 40 slot *interfaces.ConnectedSlot 41 plugInfo *snap.PlugInfo 42 plug *interfaces.ConnectedPlug 43 } 44 45 var _ = Suite(&RawInputInterfaceSuite{ 46 iface: builtin.MustInterface("raw-input"), 47 }) 48 49 const rawInputConsumerYaml = `name: consumer 50 version: 0 51 apps: 52 app: 53 plugs: [raw-input] 54 ` 55 56 const rawInputCoreYaml = `name: core 57 version: 0 58 type: os 59 slots: 60 raw-input: 61 ` 62 63 func (s *RawInputInterfaceSuite) SetUpTest(c *C) { 64 s.plug, s.plugInfo = MockConnectedPlug(c, rawInputConsumerYaml, nil, "raw-input") 65 s.slot, s.slotInfo = MockConnectedSlot(c, rawInputCoreYaml, nil, "raw-input") 66 } 67 68 func (s *RawInputInterfaceSuite) TestName(c *C) { 69 c.Assert(s.iface.Name(), Equals, "raw-input") 70 } 71 72 func (s *RawInputInterfaceSuite) TestSanitizeSlot(c *C) { 73 c.Assert(interfaces.BeforePrepareSlot(s.iface, s.slotInfo), IsNil) 74 } 75 76 func (s *RawInputInterfaceSuite) TestSanitizePlug(c *C) { 77 c.Assert(interfaces.BeforePreparePlug(s.iface, s.plugInfo), IsNil) 78 } 79 80 func (s *RawInputInterfaceSuite) TestSecCompSpec(c *C) { 81 spec := &seccomp.Specification{} 82 c.Assert(spec.AddConnectedPlug(s.iface, s.plug, s.slot), IsNil) 83 c.Assert(spec.SecurityTags(), DeepEquals, []string{"snap.consumer.app"}) 84 c.Assert(spec.SnippetForTag("snap.consumer.app"), testutil.Contains, "bind\n") 85 c.Assert(spec.SnippetForTag("snap.consumer.app"), testutil.Contains, "socket AF_NETLINK - NETLINK_KOBJECT_UEVENT\n") 86 } 87 88 func (s *RawInputInterfaceSuite) TestAppArmorSpec(c *C) { 89 spec := &apparmor.Specification{} 90 c.Assert(spec.AddConnectedPlug(s.iface, s.plug, s.slot), IsNil) 91 c.Assert(spec.SecurityTags(), DeepEquals, []string{"snap.consumer.app"}) 92 c.Assert(spec.SnippetForTag("snap.consumer.app"), testutil.Contains, `/dev/input/* rw,`) 93 c.Assert(spec.SnippetForTag("snap.consumer.app"), testutil.Contains, `/sys/devices/**/input[0-9]*/capabilities/* r,`) 94 c.Assert(spec.SnippetForTag("snap.consumer.app"), testutil.Contains, `/run/udev/data/c13:[0-9]* r,`) 95 c.Assert(spec.SnippetForTag("snap.consumer.app"), testutil.Contains, `/run/udev/data/+input:input[0-9]* r,`) 96 } 97 98 func (s *RawInputInterfaceSuite) TestUDevSpec(c *C) { 99 spec := &udev.Specification{} 100 c.Assert(spec.AddConnectedPlug(s.iface, s.plug, s.slot), IsNil) 101 c.Assert(spec.Snippets(), HasLen, 5) 102 c.Assert(spec.Snippets(), testutil.Contains, `# raw-input 103 KERNEL=="event[0-9]*", SUBSYSTEM=="input", TAG+="snap_consumer_app"`) 104 c.Assert(spec.Snippets(), testutil.Contains, `# raw-input 105 KERNEL=="mice", TAG+="snap_consumer_app"`) 106 c.Assert(spec.Snippets(), testutil.Contains, `# raw-input 107 KERNEL=="mouse[0-9]*", TAG+="snap_consumer_app"`) 108 c.Assert(spec.Snippets(), testutil.Contains, `# raw-input 109 KERNEL=="ts[0-9]*", TAG+="snap_consumer_app"`) 110 c.Assert(spec.Snippets(), testutil.Contains, fmt.Sprintf(`TAG=="snap_consumer_app", RUN+="%v/snap-device-helper $env{ACTION} snap_consumer_app $devpath $major:$minor"`, dirs.DistroLibExecDir)) 111 c.Assert(spec.TriggeredSubsystems(), DeepEquals, []string{"input"}) 112 } 113 114 func (s *RawInputInterfaceSuite) TestStaticInfo(c *C) { 115 si := interfaces.StaticInfoOf(s.iface) 116 c.Assert(si.ImplicitOnCore, Equals, true) 117 c.Assert(si.ImplicitOnClassic, Equals, true) 118 c.Assert(si.Summary, Equals, `allows access to raw input devices`) 119 c.Assert(si.BaseDeclarationSlots, testutil.Contains, "raw-input") 120 } 121 122 func (s *RawInputInterfaceSuite) TestAutoConnect(c *C) { 123 c.Assert(s.iface.AutoConnect(s.plugInfo, s.slotInfo), Equals, true) 124 } 125 126 func (s *RawInputInterfaceSuite) TestInterfaces(c *C) { 127 c.Check(builtin.Interfaces(), testutil.DeepContains, s.iface) 128 }