github.com/rigado/snapd@v2.42.5-go-mod+incompatible/interfaces/builtin/audio_playback_test.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 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/snap/snaptest"
    33  	"github.com/snapcore/snapd/testutil"
    34  )
    35  
    36  type AudioPlaybackInterfaceSuite struct {
    37  	iface           interfaces.Interface
    38  	coreSlotInfo    *snap.SlotInfo
    39  	coreSlot        *interfaces.ConnectedSlot
    40  	classicSlotInfo *snap.SlotInfo
    41  	classicSlot     *interfaces.ConnectedSlot
    42  	plugInfo        *snap.PlugInfo
    43  	plug            *interfaces.ConnectedPlug
    44  }
    45  
    46  var _ = Suite(&AudioPlaybackInterfaceSuite{
    47  	iface: builtin.MustInterface("audio-playback"),
    48  })
    49  
    50  const audioPlaybackMockPlugSnapInfoYaml = `name: consumer
    51  version: 1.0
    52  apps:
    53   app:
    54    command: foo
    55    plugs: [audio-playback]
    56  `
    57  
    58  // a audio-playback slot on a audio-playback snap (as installed on a core/all-snap system)
    59  const audioPlaybackMockCoreSlotSnapInfoYaml = `name: audio-playback
    60  version: 1.0
    61  apps:
    62   app1:
    63    command: foo
    64    slots: [audio-playback]
    65  `
    66  
    67  // a audio-playback slot on the core snap (as automatically added on classic)
    68  const audioPlaybackMockClassicSlotSnapInfoYaml = `name: core
    69  version: 0
    70  type: os
    71  slots:
    72   audio-playback:
    73    interface: audio-playback
    74  `
    75  
    76  func (s *AudioPlaybackInterfaceSuite) SetUpTest(c *C) {
    77  	// audio-playback snap with audio-playback slot on an core/all-snap install.
    78  	snapInfo := snaptest.MockInfo(c, audioPlaybackMockCoreSlotSnapInfoYaml, nil)
    79  	s.coreSlotInfo = snapInfo.Slots["audio-playback"]
    80  	s.coreSlot = interfaces.NewConnectedSlot(s.coreSlotInfo, nil, nil)
    81  	// audio-playback slot on a core snap in a classic install.
    82  	snapInfo = snaptest.MockInfo(c, audioPlaybackMockClassicSlotSnapInfoYaml, nil)
    83  	s.classicSlotInfo = snapInfo.Slots["audio-playback"]
    84  	s.classicSlot = interfaces.NewConnectedSlot(s.classicSlotInfo, nil, nil)
    85  	// snap with the audio-playback plug
    86  	snapInfo = snaptest.MockInfo(c, audioPlaybackMockPlugSnapInfoYaml, nil)
    87  	s.plugInfo = snapInfo.Plugs["audio-playback"]
    88  	s.plug = interfaces.NewConnectedPlug(s.plugInfo, nil, nil)
    89  }
    90  
    91  func (s *AudioPlaybackInterfaceSuite) TestName(c *C) {
    92  	c.Assert(s.iface.Name(), Equals, "audio-playback")
    93  }
    94  
    95  func (s *AudioPlaybackInterfaceSuite) TestSanitizeSlot(c *C) {
    96  	c.Assert(interfaces.BeforePrepareSlot(s.iface, s.coreSlotInfo), IsNil)
    97  	c.Assert(interfaces.BeforePrepareSlot(s.iface, s.classicSlotInfo), IsNil)
    98  }
    99  
   100  func (s *AudioPlaybackInterfaceSuite) TestSanitizePlug(c *C) {
   101  	c.Assert(interfaces.BeforePreparePlug(s.iface, s.plugInfo), IsNil)
   102  }
   103  
   104  func (s *AudioPlaybackInterfaceSuite) TestSecComp(c *C) {
   105  	restore := release.MockOnClassic(false)
   106  	defer restore()
   107  
   108  	// connected plug to core slot
   109  	spec := &seccomp.Specification{}
   110  	c.Assert(spec.AddConnectedPlug(s.iface, s.plug, s.coreSlot), IsNil)
   111  	c.Assert(spec.SecurityTags(), DeepEquals, []string{"snap.consumer.app"})
   112  	c.Assert(spec.SnippetForTag("snap.consumer.app"), testutil.Contains, "shmctl\n")
   113  
   114  	// connected core slot to plug
   115  	spec = &seccomp.Specification{}
   116  	c.Assert(spec.AddConnectedSlot(s.iface, s.plug, s.coreSlot), IsNil)
   117  	c.Assert(spec.SecurityTags(), HasLen, 0)
   118  
   119  	// permanent core slot
   120  	spec = &seccomp.Specification{}
   121  	c.Assert(spec.AddPermanentSlot(s.iface, s.coreSlotInfo), IsNil)
   122  	c.Assert(spec.SecurityTags(), DeepEquals, []string{"snap.audio-playback.app1"})
   123  	c.Assert(spec.SnippetForTag("snap.audio-playback.app1"), testutil.Contains, "listen\n")
   124  }
   125  
   126  func (s *AudioPlaybackInterfaceSuite) TestSecCompOnClassic(c *C) {
   127  	restore := release.MockOnClassic(true)
   128  	defer restore()
   129  
   130  	// connected plug to classic slot
   131  	spec := &seccomp.Specification{}
   132  	c.Assert(spec.AddConnectedPlug(s.iface, s.plug, s.classicSlot), IsNil)
   133  	c.Assert(spec.SecurityTags(), DeepEquals, []string{"snap.consumer.app"})
   134  	c.Check(spec.SnippetForTag("snap.consumer.app"), testutil.Contains, "shmctl\n")
   135  
   136  	// connected classic slot to plug
   137  	spec = &seccomp.Specification{}
   138  	c.Assert(spec.AddConnectedSlot(s.iface, s.plug, s.classicSlot), IsNil)
   139  	c.Assert(spec.SecurityTags(), HasLen, 0)
   140  
   141  	// permanent classic slot
   142  	spec = &seccomp.Specification{}
   143  	c.Assert(spec.AddPermanentSlot(s.iface, s.classicSlotInfo), IsNil)
   144  	c.Assert(spec.SecurityTags(), HasLen, 0)
   145  }
   146  
   147  func (s *AudioPlaybackInterfaceSuite) TestAppArmor(c *C) {
   148  	restore := release.MockOnClassic(false)
   149  	defer restore()
   150  
   151  	// connected plug to core slot
   152  	spec := &apparmor.Specification{}
   153  	c.Assert(spec.AddConnectedPlug(s.iface, s.plug, s.coreSlot), IsNil)
   154  	c.Assert(spec.SecurityTags(), DeepEquals, []string{"snap.consumer.app"})
   155  	c.Check(spec.SnippetForTag("snap.consumer.app"), testutil.Contains, "/{run,dev}/shm/pulse-shm-* mrwk,\n")
   156  
   157  	// connected core slot to plug
   158  	spec = &apparmor.Specification{}
   159  	c.Assert(spec.AddConnectedSlot(s.iface, s.plug, s.coreSlot), IsNil)
   160  	c.Assert(spec.SecurityTags(), HasLen, 0)
   161  
   162  	// permanent core slot
   163  	spec = &apparmor.Specification{}
   164  	c.Assert(spec.AddPermanentSlot(s.iface, s.coreSlotInfo), IsNil)
   165  	c.Assert(spec.SecurityTags(), DeepEquals, []string{"snap.audio-playback.app1"})
   166  	c.Check(spec.SnippetForTag("snap.audio-playback.app1"), testutil.Contains, "capability setuid,\n")
   167  }
   168  
   169  func (s *AudioPlaybackInterfaceSuite) TestAppArmorOnClassic(c *C) {
   170  	restore := release.MockOnClassic(true)
   171  	defer restore()
   172  
   173  	// connected plug to classic slot
   174  	spec := &apparmor.Specification{}
   175  	c.Assert(spec.AddConnectedPlug(s.iface, s.plug, s.classicSlot), IsNil)
   176  	c.Assert(spec.SecurityTags(), DeepEquals, []string{"snap.consumer.app"})
   177  	c.Check(spec.SnippetForTag("snap.consumer.app"), testutil.Contains, "/{run,dev}/shm/pulse-shm-* mrwk,\n")
   178  	c.Check(spec.SnippetForTag("snap.consumer.app"), testutil.Contains, "/etc/pulse/ r,\n")
   179  
   180  	// connected classic slot to plug
   181  	spec = &apparmor.Specification{}
   182  	c.Assert(spec.AddConnectedSlot(s.iface, s.plug, s.classicSlot), IsNil)
   183  	c.Assert(spec.SecurityTags(), HasLen, 0)
   184  
   185  	// permanent classic slot
   186  	spec = &apparmor.Specification{}
   187  	c.Assert(spec.AddPermanentSlot(s.iface, s.classicSlotInfo), IsNil)
   188  	c.Assert(spec.SecurityTags(), HasLen, 0)
   189  }
   190  
   191  func (s *AudioPlaybackInterfaceSuite) TestUDev(c *C) {
   192  	spec := &udev.Specification{}
   193  	c.Assert(spec.AddPermanentSlot(s.iface, s.coreSlotInfo), IsNil)
   194  	c.Assert(spec.Snippets(), HasLen, 4)
   195  	c.Assert(spec.Snippets(), testutil.Contains, `# audio-playback
   196  KERNEL=="controlC[0-9]*", TAG+="snap_audio-playback_app1"`)
   197  	c.Assert(spec.Snippets(), testutil.Contains, `# audio-playback
   198  KERNEL=="pcmC[0-9]*D[0-9]*[cp]", TAG+="snap_audio-playback_app1"`)
   199  	c.Assert(spec.Snippets(), testutil.Contains, `# audio-playback
   200  KERNEL=="timer", TAG+="snap_audio-playback_app1"`)
   201  	c.Assert(spec.Snippets(), testutil.Contains, `TAG=="snap_audio-playback_app1", RUN+="/usr/lib/snapd/snap-device-helper $env{ACTION} snap_audio-playback_app1 $devpath $major:$minor"`)
   202  }
   203  
   204  func (s *AudioPlaybackInterfaceSuite) TestInterfaces(c *C) {
   205  	c.Check(builtin.Interfaces(), testutil.DeepContains, s.iface)
   206  }