github.com/kubiko/snapd@v0.0.0-20201013125620-d4f3094d9ddf/interfaces/builtin/wayland_test.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 2017-2018 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/seccomp" 29 "github.com/snapcore/snapd/interfaces/udev" 30 "github.com/snapcore/snapd/release" 31 "github.com/snapcore/snapd/snap" 32 "github.com/snapcore/snapd/testutil" 33 ) 34 35 type WaylandInterfaceSuite struct { 36 iface interfaces.Interface 37 coreSlotInfo *snap.SlotInfo 38 coreSlot *interfaces.ConnectedSlot 39 classicSlotInfo *snap.SlotInfo 40 classicSlot *interfaces.ConnectedSlot 41 plugInfo *snap.PlugInfo 42 plug *interfaces.ConnectedPlug 43 } 44 45 var _ = Suite(&WaylandInterfaceSuite{ 46 iface: builtin.MustInterface("wayland"), 47 }) 48 49 const waylandConsumerYaml = `name: consumer 50 version: 0 51 apps: 52 app: 53 plugs: [wayland] 54 ` 55 56 // a wayland slot on a wayland snap (as installed on a core/all-snap system) 57 const waylandCoreYaml = `name: wayland 58 version: 0 59 apps: 60 app1: 61 slots: [wayland] 62 ` 63 64 // a wayland slot on the core snap (as automatically added on classic) 65 const waylandClassicYaml = `name: core 66 version: 0 67 type: os 68 slots: 69 wayland: 70 interface: wayland 71 ` 72 73 func (s *WaylandInterfaceSuite) SetUpTest(c *C) { 74 s.plug, s.plugInfo = MockConnectedPlug(c, waylandConsumerYaml, nil, "wayland") 75 s.coreSlot, s.coreSlotInfo = MockConnectedSlot(c, waylandCoreYaml, nil, "wayland") 76 s.classicSlot, s.classicSlotInfo = MockConnectedSlot(c, waylandClassicYaml, nil, "wayland") 77 } 78 79 func (s *WaylandInterfaceSuite) TestName(c *C) { 80 c.Assert(s.iface.Name(), Equals, "wayland") 81 } 82 83 func (s *WaylandInterfaceSuite) TestSanitizeSlot(c *C) { 84 c.Assert(interfaces.BeforePrepareSlot(s.iface, s.coreSlotInfo), IsNil) 85 c.Assert(interfaces.BeforePrepareSlot(s.iface, s.classicSlotInfo), IsNil) 86 } 87 88 func (s *WaylandInterfaceSuite) TestSanitizePlug(c *C) { 89 c.Assert(interfaces.BeforePreparePlug(s.iface, s.plugInfo), IsNil) 90 } 91 92 func (s *WaylandInterfaceSuite) TestAppArmorSpec(c *C) { 93 // on a core system with wayland slot coming from a regular app snap. 94 restore := release.MockOnClassic(false) 95 defer restore() 96 97 // connected plug to core slot 98 spec := &apparmor.Specification{} 99 c.Assert(spec.AddConnectedPlug(s.iface, s.plug, s.coreSlot), IsNil) 100 c.Assert(spec.SecurityTags(), DeepEquals, []string{"snap.consumer.app"}) 101 c.Assert(spec.SnippetForTag("snap.consumer.app"), testutil.Contains, "/etc/drirc r,") 102 103 // connected core slot to plug 104 spec = &apparmor.Specification{} 105 c.Assert(spec.AddConnectedSlot(s.iface, s.plug, s.coreSlot), IsNil) 106 c.Assert(spec.SecurityTags(), DeepEquals, []string{"snap.wayland.app1"}) 107 c.Assert(spec.SnippetForTag("snap.wayland.app1"), testutil.Contains, "owner /run/user/[0-9]*/snap.consumer/{mesa,mutter,sdl,wayland-cursor,weston,xwayland}-shared-* rw,") 108 109 // permanent core slot 110 spec = &apparmor.Specification{} 111 c.Assert(spec.AddPermanentSlot(s.iface, s.coreSlotInfo), IsNil) 112 c.Assert(spec.SecurityTags(), DeepEquals, []string{"snap.wayland.app1"}) 113 c.Assert(spec.SnippetForTag("snap.wayland.app1"), testutil.Contains, "capability sys_tty_config,") 114 } 115 116 func (s *WaylandInterfaceSuite) TestAppArmorSpecOnClassic(c *C) { 117 // on a classic system with wayland slot coming from the core snap. 118 restore := release.MockOnClassic(true) 119 defer restore() 120 121 // connected plug to classic slot 122 spec := &apparmor.Specification{} 123 c.Assert(spec.AddConnectedPlug(s.iface, s.plug, s.classicSlot), IsNil) 124 c.Assert(spec.SecurityTags(), DeepEquals, []string{"snap.consumer.app"}) 125 c.Assert(spec.SnippetForTag("snap.consumer.app"), testutil.Contains, "owner /run/user/[0-9]*/wayland-[0-9]* rw,") 126 127 // connected classic slot to plug 128 spec = &apparmor.Specification{} 129 c.Assert(spec.AddConnectedSlot(s.iface, s.plug, s.classicSlot), IsNil) 130 c.Assert(spec.SecurityTags(), HasLen, 0) 131 132 // permanent classic slot 133 spec = &apparmor.Specification{} 134 c.Assert(spec.AddPermanentSlot(s.iface, s.classicSlotInfo), IsNil) 135 c.Assert(spec.SecurityTags(), HasLen, 0) 136 } 137 138 func (s *WaylandInterfaceSuite) TestSecCompOnClassic(c *C) { 139 // on a classic system with wayland slot coming from the core snap. 140 restore := release.MockOnClassic(true) 141 defer restore() 142 143 seccompSpec := &seccomp.Specification{} 144 err := seccompSpec.AddPermanentSlot(s.iface, s.classicSlotInfo) 145 c.Assert(err, IsNil) 146 err = seccompSpec.AddConnectedPlug(s.iface, s.plug, s.classicSlot) 147 c.Assert(err, IsNil) 148 // No SecComp on Classic 149 c.Assert(seccompSpec.SecurityTags(), IsNil) 150 } 151 152 func (s *WaylandInterfaceSuite) TestSecCompOnCore(c *C) { 153 // on a core system with wayland slot coming from a regular app snap. 154 restore := release.MockOnClassic(false) 155 defer restore() 156 157 seccompSpec := &seccomp.Specification{} 158 err := seccompSpec.AddPermanentSlot(s.iface, s.coreSlotInfo) 159 c.Assert(err, IsNil) 160 err = seccompSpec.AddConnectedPlug(s.iface, s.plug, s.coreSlot) 161 c.Assert(err, IsNil) 162 c.Assert(seccompSpec.SecurityTags(), DeepEquals, []string{"snap.wayland.app1"}) 163 c.Assert(seccompSpec.SnippetForTag("snap.wayland.app1"), testutil.Contains, "listen\n") 164 } 165 166 func (s *WaylandInterfaceSuite) TestUDev(c *C) { 167 spec := &udev.Specification{} 168 c.Assert(spec.AddPermanentSlot(s.iface, s.coreSlotInfo), IsNil) 169 c.Assert(spec.Snippets(), HasLen, 6) 170 c.Assert(spec.Snippets(), testutil.Contains, `# wayland 171 KERNEL=="event[0-9]*", TAG+="snap_wayland_app1"`) 172 c.Assert(spec.Snippets(), testutil.Contains, `# wayland 173 KERNEL=="mice", TAG+="snap_wayland_app1"`) 174 c.Assert(spec.Snippets(), testutil.Contains, `# wayland 175 KERNEL=="mouse[0-9]*", TAG+="snap_wayland_app1"`) 176 c.Assert(spec.Snippets(), testutil.Contains, `# wayland 177 KERNEL=="ts[0-9]*", TAG+="snap_wayland_app1"`) 178 c.Assert(spec.Snippets(), testutil.Contains, `# wayland 179 KERNEL=="tty[0-9]*", TAG+="snap_wayland_app1"`) 180 c.Assert(spec.Snippets(), testutil.Contains, `TAG=="snap_wayland_app1", RUN+="/usr/lib/snapd/snap-device-helper $env{ACTION} snap_wayland_app1 $devpath $major:$minor"`) 181 c.Assert(spec.TriggeredSubsystems(), DeepEquals, []string{"input"}) 182 } 183 184 func (s *WaylandInterfaceSuite) TestStaticInfo(c *C) { 185 si := interfaces.StaticInfoOf(s.iface) 186 c.Assert(si.ImplicitOnCore, Equals, false) 187 c.Assert(si.ImplicitOnClassic, Equals, true) 188 c.Assert(si.Summary, Equals, `allows access to compositors supporting wayland protocol`) 189 c.Assert(si.BaseDeclarationSlots, testutil.Contains, "wayland") 190 } 191 192 func (s *WaylandInterfaceSuite) TestAutoConnect(c *C) { 193 c.Assert(s.iface.AutoConnect(s.plugInfo, s.coreSlotInfo), Equals, true) 194 c.Assert(s.iface.AutoConnect(s.plugInfo, s.classicSlotInfo), Equals, true) 195 } 196 197 func (s *WaylandInterfaceSuite) TestInterfaces(c *C) { 198 c.Check(builtin.Interfaces(), testutil.DeepContains, s.iface) 199 }