github.com/kubiko/snapd@v0.0.0-20201013125620-d4f3094d9ddf/interfaces/builtin/bluetooth_control_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/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/snap"
    31  	"github.com/snapcore/snapd/snap/snaptest"
    32  	"github.com/snapcore/snapd/testutil"
    33  )
    34  
    35  type BluetoothControlInterfaceSuite struct {
    36  	iface    interfaces.Interface
    37  	slot     *interfaces.ConnectedSlot
    38  	slotInfo *snap.SlotInfo
    39  	plug     *interfaces.ConnectedPlug
    40  	plugInfo *snap.PlugInfo
    41  }
    42  
    43  const btcontrolMockPlugSnapInfoYaml = `name: other
    44  version: 1.0
    45  apps:
    46   app2:
    47    command: foo
    48    plugs: [bluetooth-control]
    49  `
    50  
    51  var _ = Suite(&BluetoothControlInterfaceSuite{
    52  	iface: builtin.MustInterface("bluetooth-control"),
    53  })
    54  
    55  func (s *BluetoothControlInterfaceSuite) SetUpTest(c *C) {
    56  	s.slotInfo = &snap.SlotInfo{
    57  		Snap:      &snap.Info{SuggestedName: "core", SnapType: snap.TypeOS},
    58  		Name:      "bluetooth-control",
    59  		Interface: "bluetooth-control",
    60  		Apps: map[string]*snap.AppInfo{
    61  			"app1": {
    62  				Snap: &snap.Info{
    63  					SuggestedName: "core",
    64  				},
    65  				Name: "app1"}},
    66  	}
    67  	s.slot = interfaces.NewConnectedSlot(s.slotInfo, nil, nil)
    68  	plugSnap := snaptest.MockInfo(c, btcontrolMockPlugSnapInfoYaml, nil)
    69  	s.plugInfo = plugSnap.Plugs["bluetooth-control"]
    70  	s.plug = interfaces.NewConnectedPlug(s.plugInfo, nil, nil)
    71  }
    72  
    73  func (s *BluetoothControlInterfaceSuite) TestName(c *C) {
    74  	c.Assert(s.iface.Name(), Equals, "bluetooth-control")
    75  }
    76  
    77  func (s *BluetoothControlInterfaceSuite) TestSanitizeSlot(c *C) {
    78  	c.Assert(interfaces.BeforePrepareSlot(s.iface, s.slotInfo), IsNil)
    79  }
    80  
    81  func (s *BluetoothControlInterfaceSuite) TestSanitizePlug(c *C) {
    82  	c.Assert(interfaces.BeforePreparePlug(s.iface, s.plugInfo), IsNil)
    83  }
    84  
    85  func (s *BluetoothControlInterfaceSuite) TestAppArmorSpec(c *C) {
    86  	spec := &apparmor.Specification{}
    87  	c.Assert(spec.AddConnectedPlug(s.iface, s.plug, s.slot), IsNil)
    88  	c.Assert(spec.SecurityTags(), DeepEquals, []string{"snap.other.app2"})
    89  	c.Assert(spec.SnippetForTag("snap.other.app2"), testutil.Contains, "capability net_admin")
    90  }
    91  
    92  func (s *BluetoothControlInterfaceSuite) TestSecCompSpec(c *C) {
    93  	spec := &seccomp.Specification{}
    94  	c.Assert(spec.AddConnectedPlug(s.iface, s.plug, s.slot), IsNil)
    95  	c.Assert(spec.SecurityTags(), DeepEquals, []string{"snap.other.app2"})
    96  	c.Assert(spec.SnippetForTag("snap.other.app2"), testutil.Contains, "\nbind\n")
    97  }
    98  
    99  func (s *BluetoothControlInterfaceSuite) TestUDevSpec(c *C) {
   100  	spec := &udev.Specification{}
   101  	c.Assert(spec.AddConnectedPlug(s.iface, s.plug, s.slot), IsNil)
   102  	c.Assert(spec.Snippets(), HasLen, 3)
   103  	c.Assert(spec.Snippets(), testutil.Contains, `# bluetooth-control
   104  SUBSYSTEM=="bluetooth", TAG+="snap_other_app2"`)
   105  	c.Assert(spec.Snippets(), testutil.Contains, `# bluetooth-control
   106  SUBSYSTEM=="BT_chrdev", TAG+="snap_other_app2"`)
   107  	c.Assert(spec.Snippets(), testutil.Contains, `TAG=="snap_other_app2", RUN+="/usr/lib/snapd/snap-device-helper $env{ACTION} snap_other_app2 $devpath $major:$minor"`)
   108  }
   109  
   110  func (s *BluetoothControlInterfaceSuite) TestInterfaces(c *C) {
   111  	c.Check(builtin.Interfaces(), testutil.DeepContains, s.iface)
   112  }