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