github.com/hugh712/snapd@v0.0.0-20200910133618-1a99902bd583/interfaces/builtin/pulseaudio_test.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 2016-2017 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/builtin" 27 "github.com/snapcore/snapd/interfaces/seccomp" 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 PulseAudioInterfaceSuite struct { 35 iface interfaces.Interface 36 coreSlotInfo *snap.SlotInfo 37 coreSlot *interfaces.ConnectedSlot 38 classicSlotInfo *snap.SlotInfo 39 classicSlot *interfaces.ConnectedSlot 40 plugInfo *snap.PlugInfo 41 plug *interfaces.ConnectedPlug 42 } 43 44 var _ = Suite(&PulseAudioInterfaceSuite{ 45 iface: builtin.MustInterface("pulseaudio"), 46 }) 47 48 const pulseaudioMockPlugSnapInfoYaml = `name: other 49 version: 1.0 50 apps: 51 app2: 52 command: foo 53 plugs: [pulseaudio] 54 ` 55 56 // a pulseaudio slot on a pulseaudio snap (as installed on a core/all-snap system) 57 const pulseaudioMockCoreSlotSnapInfoYaml = `name: pulseaudio 58 version: 1.0 59 apps: 60 app1: 61 command: foo 62 slots: [pulseaudio] 63 ` 64 65 // a pulseaudio slot on the core snap (as automatically added on classic) 66 const pulseaudioMockClassicSlotSnapInfoYaml = `name: core 67 version: 0 68 type: os 69 slots: 70 pulseaudio: 71 interface: pulseaudio 72 ` 73 74 func (s *PulseAudioInterfaceSuite) SetUpTest(c *C) { 75 // pulseaudio snap with pulseaudio slot on an core/all-snap install. 76 snapInfo := snaptest.MockInfo(c, pulseaudioMockCoreSlotSnapInfoYaml, nil) 77 s.coreSlotInfo = snapInfo.Slots["pulseaudio"] 78 s.coreSlot = interfaces.NewConnectedSlot(s.coreSlotInfo, nil, nil) 79 // pulseaudio slot on a core snap in a classic install. 80 snapInfo = snaptest.MockInfo(c, pulseaudioMockClassicSlotSnapInfoYaml, nil) 81 s.classicSlotInfo = snapInfo.Slots["pulseaudio"] 82 s.classicSlot = interfaces.NewConnectedSlot(s.classicSlotInfo, nil, nil) 83 // snap with the pulseaudio plug 84 snapInfo = snaptest.MockInfo(c, pulseaudioMockPlugSnapInfoYaml, nil) 85 s.plugInfo = snapInfo.Plugs["pulseaudio"] 86 s.plug = interfaces.NewConnectedPlug(s.plugInfo, nil, nil) 87 } 88 89 func (s *PulseAudioInterfaceSuite) TestName(c *C) { 90 c.Assert(s.iface.Name(), Equals, "pulseaudio") 91 } 92 93 func (s *PulseAudioInterfaceSuite) TestSanitizeSlot(c *C) { 94 c.Assert(interfaces.BeforePrepareSlot(s.iface, s.coreSlotInfo), IsNil) 95 c.Assert(interfaces.BeforePrepareSlot(s.iface, s.classicSlotInfo), IsNil) 96 } 97 98 func (s *PulseAudioInterfaceSuite) TestSanitizePlug(c *C) { 99 c.Assert(interfaces.BeforePreparePlug(s.iface, s.plugInfo), IsNil) 100 } 101 102 func (s *PulseAudioInterfaceSuite) TestSecCompOnClassic(c *C) { 103 seccompSpec := &seccomp.Specification{} 104 err := seccompSpec.AddPermanentSlot(s.iface, s.classicSlotInfo) 105 c.Assert(err, IsNil) 106 err = seccompSpec.AddConnectedPlug(s.iface, s.plug, s.classicSlot) 107 c.Assert(err, IsNil) 108 c.Assert(seccompSpec.SecurityTags(), DeepEquals, []string{"snap.other.app2"}) 109 c.Check(seccompSpec.SnippetForTag("snap.other.app2"), testutil.Contains, "shmctl\n") 110 } 111 112 func (s *PulseAudioInterfaceSuite) TestSecCompOnAllSnaps(c *C) { 113 seccompSpec := &seccomp.Specification{} 114 err := seccompSpec.AddPermanentSlot(s.iface, s.coreSlotInfo) 115 c.Assert(err, IsNil) 116 err = seccompSpec.AddConnectedPlug(s.iface, s.plug, s.coreSlot) 117 c.Assert(err, IsNil) 118 c.Assert(seccompSpec.SecurityTags(), DeepEquals, []string{"snap.other.app2", "snap.pulseaudio.app1"}) 119 c.Assert(seccompSpec.SnippetForTag("snap.pulseaudio.app1"), testutil.Contains, "listen\n") 120 c.Assert(seccompSpec.SnippetForTag("snap.other.app2"), testutil.Contains, "shmctl\n") 121 } 122 123 func (s *PulseAudioInterfaceSuite) TestUDev(c *C) { 124 spec := &udev.Specification{} 125 c.Assert(spec.AddPermanentSlot(s.iface, s.coreSlotInfo), IsNil) 126 c.Assert(spec.Snippets(), HasLen, 4) 127 c.Assert(spec.Snippets(), testutil.Contains, `# pulseaudio 128 KERNEL=="controlC[0-9]*", TAG+="snap_pulseaudio_app1"`) 129 c.Assert(spec.Snippets(), testutil.Contains, `# pulseaudio 130 KERNEL=="pcmC[0-9]*D[0-9]*[cp]", TAG+="snap_pulseaudio_app1"`) 131 c.Assert(spec.Snippets(), testutil.Contains, `# pulseaudio 132 KERNEL=="timer", TAG+="snap_pulseaudio_app1"`) 133 c.Assert(spec.Snippets(), testutil.Contains, `TAG=="snap_pulseaudio_app1", RUN+="/usr/lib/snapd/snap-device-helper $env{ACTION} snap_pulseaudio_app1 $devpath $major:$minor"`) 134 } 135 136 func (s *PulseAudioInterfaceSuite) TestInterfaces(c *C) { 137 c.Check(builtin.Interfaces(), testutil.DeepContains, s.iface) 138 }