github.com/bugraaydogar/snapd@v0.0.0-20210315170335-8c70bb858939/interfaces/builtin/audio_record_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/release"
    29  	"github.com/snapcore/snapd/snap"
    30  	"github.com/snapcore/snapd/snap/snaptest"
    31  	"github.com/snapcore/snapd/testutil"
    32  )
    33  
    34  type AudioRecordInterfaceSuite 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(&AudioRecordInterfaceSuite{
    45  	iface: builtin.MustInterface("audio-record"),
    46  })
    47  
    48  const audioRecordMockPlugSnapInfoYaml = `name: consumer
    49  version: 1.0
    50  apps:
    51   app:
    52    command: foo
    53    plugs: [audio-record]
    54  `
    55  
    56  // a audio-record slot on a audio-record snap (as installed on a core/all-snap system)
    57  const audioRecordMockCoreSlotSnapInfoYaml = `name: audio-record
    58  version: 1.0
    59  apps:
    60   app1:
    61    command: foo
    62    slots: [audio-record]
    63  `
    64  
    65  // a audio-record slot on the core snap (as automatically added on classic)
    66  const audioRecordMockClassicSlotSnapInfoYaml = `name: core
    67  version: 0
    68  type: os
    69  slots:
    70   audio-record:
    71    interface: audio-record
    72  `
    73  
    74  func (s *AudioRecordInterfaceSuite) SetUpTest(c *C) {
    75  	// audio-record snap with audio-record slot on an core/all-snap install.
    76  	snapInfo := snaptest.MockInfo(c, audioRecordMockCoreSlotSnapInfoYaml, nil)
    77  	s.coreSlotInfo = snapInfo.Slots["audio-record"]
    78  	s.coreSlot = interfaces.NewConnectedSlot(s.coreSlotInfo, nil, nil)
    79  	// audio-record slot on a core snap in a classic install.
    80  	snapInfo = snaptest.MockInfo(c, audioRecordMockClassicSlotSnapInfoYaml, nil)
    81  	s.classicSlotInfo = snapInfo.Slots["audio-record"]
    82  	s.classicSlot = interfaces.NewConnectedSlot(s.classicSlotInfo, nil, nil)
    83  	// snap with the audio-record plug
    84  	snapInfo = snaptest.MockInfo(c, audioRecordMockPlugSnapInfoYaml, nil)
    85  	s.plugInfo = snapInfo.Plugs["audio-record"]
    86  	s.plug = interfaces.NewConnectedPlug(s.plugInfo, nil, nil)
    87  }
    88  
    89  func (s *AudioRecordInterfaceSuite) TestName(c *C) {
    90  	c.Assert(s.iface.Name(), Equals, "audio-record")
    91  }
    92  
    93  func (s *AudioRecordInterfaceSuite) 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 *AudioRecordInterfaceSuite) TestSanitizePlug(c *C) {
    99  	c.Assert(interfaces.BeforePreparePlug(s.iface, s.plugInfo), IsNil)
   100  }
   101  
   102  func (s *AudioRecordInterfaceSuite) TestAppArmor(c *C) {
   103  	restore := release.MockOnClassic(false)
   104  	defer restore()
   105  
   106  	// connected plug to core slot
   107  	spec := &apparmor.Specification{}
   108  	c.Assert(spec.AddConnectedPlug(s.iface, s.plug, s.coreSlot), IsNil)
   109  	c.Assert(spec.SecurityTags(), DeepEquals, []string{"snap.consumer.app"})
   110  	c.Check(spec.SnippetForTag("snap.consumer.app"), testutil.Contains, "# Access for communication with audio recording service done via\n")
   111  
   112  	// connected core slot to plug
   113  	spec = &apparmor.Specification{}
   114  	c.Assert(spec.AddConnectedSlot(s.iface, s.plug, s.coreSlot), IsNil)
   115  	c.Assert(spec.SecurityTags(), HasLen, 0)
   116  
   117  	// permanent core clot
   118  	spec = &apparmor.Specification{}
   119  	c.Assert(spec.AddPermanentSlot(s.iface, s.coreSlotInfo), IsNil)
   120  	c.Assert(spec.SecurityTags(), HasLen, 0)
   121  }
   122  
   123  func (s *AudioRecordInterfaceSuite) TestAppArmorOnClassic(c *C) {
   124  	restore := release.MockOnClassic(false)
   125  	defer restore()
   126  
   127  	// connected plug to classic slot
   128  	spec := &apparmor.Specification{}
   129  	c.Assert(spec.AddConnectedPlug(s.iface, s.plug, s.classicSlot), IsNil)
   130  	c.Assert(spec.SecurityTags(), DeepEquals, []string{"snap.consumer.app"})
   131  	c.Check(spec.SnippetForTag("snap.consumer.app"), testutil.Contains, "# Access for communication with audio recording service done via\n")
   132  
   133  	// connected classic slot to plug
   134  	spec = &apparmor.Specification{}
   135  	c.Assert(spec.AddConnectedSlot(s.iface, s.plug, s.classicSlot), IsNil)
   136  	c.Assert(spec.SecurityTags(), HasLen, 0)
   137  
   138  	// permanent classic slot
   139  	spec = &apparmor.Specification{}
   140  	c.Assert(spec.AddPermanentSlot(s.iface, s.classicSlotInfo), IsNil)
   141  	c.Assert(spec.SecurityTags(), HasLen, 0)
   142  }
   143  
   144  func (s *AudioRecordInterfaceSuite) TestInterfaces(c *C) {
   145  	c.Check(builtin.Interfaces(), testutil.DeepContains, s.iface)
   146  }