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