github.com/bugraaydogar/snapd@v0.0.0-20210315170335-8c70bb858939/interfaces/builtin/bluez_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  	"github.com/snapcore/snapd/dirs"
    28  	"github.com/snapcore/snapd/interfaces"
    29  	"github.com/snapcore/snapd/interfaces/apparmor"
    30  	"github.com/snapcore/snapd/interfaces/builtin"
    31  	"github.com/snapcore/snapd/interfaces/dbus"
    32  	"github.com/snapcore/snapd/interfaces/seccomp"
    33  	"github.com/snapcore/snapd/interfaces/udev"
    34  	"github.com/snapcore/snapd/release"
    35  	"github.com/snapcore/snapd/snap"
    36  	"github.com/snapcore/snapd/testutil"
    37  )
    38  
    39  type BluezInterfaceSuite struct {
    40  	iface        interfaces.Interface
    41  	appSlot      *interfaces.ConnectedSlot
    42  	appSlotInfo  *snap.SlotInfo
    43  	coreSlot     *interfaces.ConnectedSlot
    44  	coreSlotInfo *snap.SlotInfo
    45  	plug         *interfaces.ConnectedPlug
    46  	plugInfo     *snap.PlugInfo
    47  }
    48  
    49  var _ = Suite(&BluezInterfaceSuite{
    50  	iface: builtin.MustInterface("bluez"),
    51  })
    52  
    53  const bluezConsumerYaml = `name: consumer
    54  version: 0
    55  apps:
    56   app:
    57    plugs: [bluez]
    58  `
    59  
    60  const bluezConsumerTwoAppsYaml = `name: consumer
    61  version: 0
    62  apps:
    63   app1:
    64    plugs: [bluez]
    65   app2:
    66    plugs: [bluez]
    67  `
    68  
    69  const bluezConsumerThreeAppsYaml = `name: consumer
    70  version: 0
    71  apps:
    72   app1:
    73    plugs: [bluez]
    74   app2:
    75    plugs: [bluez]
    76   app3:
    77  `
    78  
    79  const bluezProducerYaml = `name: producer
    80  version: 0
    81  apps:
    82   app:
    83    slots: [bluez]
    84  `
    85  
    86  const bluezProducerTwoAppsYaml = `name: producer
    87  version: 0
    88  apps:
    89   app1:
    90    slots: [bluez]
    91   app2:
    92    slots: [bluez]
    93  `
    94  
    95  const bluezProducerThreeAppsYaml = `name: producer
    96  version: 0
    97  apps:
    98   app1:
    99    slots: [bluez]
   100   app2:
   101   app3:
   102    slots: [bluez]
   103  `
   104  
   105  const bluezCoreYaml = `name: core
   106  type: os
   107  version: 0
   108  slots:
   109    bluez:
   110  `
   111  
   112  func (s *BluezInterfaceSuite) SetUpTest(c *C) {
   113  	s.plug, s.plugInfo = MockConnectedPlug(c, bluezConsumerYaml, nil, "bluez")
   114  	s.appSlot, s.appSlotInfo = MockConnectedSlot(c, bluezProducerYaml, nil, "bluez")
   115  	s.coreSlot, s.coreSlotInfo = MockConnectedSlot(c, bluezCoreYaml, nil, "bluez")
   116  }
   117  
   118  func (s *BluezInterfaceSuite) TestName(c *C) {
   119  	c.Assert(s.iface.Name(), Equals, "bluez")
   120  }
   121  
   122  func (s *BluezInterfaceSuite) TestAppArmorSpec(c *C) {
   123  	// on a core system with bluez slot coming from a regular app snap.
   124  	restore := release.MockOnClassic(false)
   125  	defer restore()
   126  
   127  	// The label uses short form when exactly one app is bound to the bluez slot
   128  	spec := &apparmor.Specification{}
   129  	c.Assert(spec.AddConnectedPlug(s.iface, s.plug, s.appSlot), IsNil)
   130  	c.Assert(spec.SecurityTags(), DeepEquals, []string{"snap.consumer.app"})
   131  	c.Assert(spec.SnippetForTag("snap.consumer.app"), testutil.Contains, `peer=(label="snap.producer.app"),`)
   132  
   133  	// The label glob when all apps are bound to the bluez slot
   134  	slot, _ := MockConnectedSlot(c, bluezProducerTwoAppsYaml, nil, "bluez")
   135  	spec = &apparmor.Specification{}
   136  	c.Assert(spec.AddConnectedPlug(s.iface, s.plug, slot), IsNil)
   137  	c.Assert(spec.SecurityTags(), DeepEquals, []string{"snap.consumer.app"})
   138  	c.Assert(spec.SnippetForTag("snap.consumer.app"), testutil.Contains, `peer=(label="snap.producer.*"),`)
   139  
   140  	// The label uses alternation when some, but not all, apps is bound to the bluez slot
   141  	slot, _ = MockConnectedSlot(c, bluezProducerThreeAppsYaml, nil, "bluez")
   142  	spec = &apparmor.Specification{}
   143  	c.Assert(spec.AddConnectedPlug(s.iface, s.plug, slot), IsNil)
   144  	c.Assert(spec.SecurityTags(), DeepEquals, []string{"snap.consumer.app"})
   145  	c.Assert(spec.SnippetForTag("snap.consumer.app"), testutil.Contains, `peer=(label="snap.producer.{app1,app3}"),`)
   146  
   147  	// The label uses short form when exactly one app is bound to the bluez plug
   148  	spec = &apparmor.Specification{}
   149  	c.Assert(spec.AddConnectedSlot(s.iface, s.plug, s.appSlot), IsNil)
   150  	c.Assert(spec.SecurityTags(), DeepEquals, []string{"snap.producer.app"})
   151  	c.Assert(spec.SnippetForTag("snap.producer.app"), testutil.Contains, `peer=(label="snap.consumer.app"),`)
   152  
   153  	// The label glob when all apps are bound to the bluez plug
   154  	plug, _ := MockConnectedPlug(c, bluezConsumerTwoAppsYaml, nil, "bluez")
   155  	spec = &apparmor.Specification{}
   156  	c.Assert(spec.AddConnectedSlot(s.iface, plug, s.appSlot), IsNil)
   157  	c.Assert(spec.SecurityTags(), DeepEquals, []string{"snap.producer.app"})
   158  	c.Assert(spec.SnippetForTag("snap.producer.app"), testutil.Contains, `peer=(label="snap.consumer.*"),`)
   159  
   160  	// The label uses alternation when some, but not all, apps is bound to the bluez plug
   161  	plug, _ = MockConnectedPlug(c, bluezConsumerThreeAppsYaml, nil, "bluez")
   162  	spec = &apparmor.Specification{}
   163  	c.Assert(spec.AddConnectedSlot(s.iface, plug, s.appSlot), IsNil)
   164  	c.Assert(spec.SecurityTags(), DeepEquals, []string{"snap.producer.app"})
   165  	c.Assert(spec.SnippetForTag("snap.producer.app"), testutil.Contains, `peer=(label="snap.consumer.{app1,app2}"),`)
   166  
   167  	// permanent slot have a non-nil security snippet for apparmor
   168  	spec = &apparmor.Specification{}
   169  	c.Assert(spec.AddConnectedPlug(s.iface, s.plug, s.appSlot), IsNil)
   170  	c.Assert(spec.AddPermanentSlot(s.iface, s.appSlotInfo), IsNil)
   171  	c.Assert(spec.SecurityTags(), DeepEquals, []string{"snap.consumer.app", "snap.producer.app"})
   172  	c.Assert(spec.SnippetForTag("snap.consumer.app"), testutil.Contains, `peer=(label="snap.producer.app"),`)
   173  	c.Assert(spec.SnippetForTag("snap.producer.app"), testutil.Contains, `peer=(label=unconfined),`)
   174  
   175  	// on a classic system with bluez slot coming from the core snap.
   176  	restore = release.MockOnClassic(true)
   177  	defer restore()
   178  
   179  	// connected plug to core slot
   180  	spec = &apparmor.Specification{}
   181  	c.Assert(spec.AddConnectedPlug(s.iface, s.plug, s.coreSlot), IsNil)
   182  	c.Assert(spec.SecurityTags(), DeepEquals, []string{"snap.consumer.app"})
   183  	c.Assert(spec.SnippetForTag("snap.consumer.app"), testutil.Contains, "peer=(name=org.bluez, label=unconfined)")
   184  	c.Assert(spec.SnippetForTag("snap.consumer.app"), testutil.Contains, "peer=(name=org.bluez.obex, label=unconfined)")
   185  	c.Assert(spec.SnippetForTag("snap.consumer.app"), testutil.Contains, "peer=(name=org.bluez.mesh, label=unconfined)")
   186  	c.Assert(spec.SnippetForTag("snap.consumer.app"), testutil.Contains, "peer=(label=unconfined),")
   187  	c.Assert(spec.SnippetForTag("snap.consumer.app"), testutil.Contains, `interface=org.freedesktop.DBus.ObjectManager`)
   188  	c.Assert(spec.SnippetForTag("snap.consumer.app"), testutil.Contains, `interface=org.freedesktop.DBus.*`)
   189  
   190  	// connected core slot to plug
   191  	spec = &apparmor.Specification{}
   192  	c.Assert(spec.AddConnectedSlot(s.iface, s.plug, s.coreSlot), IsNil)
   193  	c.Assert(spec.SecurityTags(), HasLen, 0)
   194  
   195  	// permanent core slot
   196  	spec = &apparmor.Specification{}
   197  	c.Assert(spec.AddPermanentSlot(s.iface, s.coreSlotInfo), IsNil)
   198  	c.Assert(spec.SecurityTags(), HasLen, 0)
   199  }
   200  
   201  func (s *BluezInterfaceSuite) TestDBusSpec(c *C) {
   202  	// on a core system with bluez slot coming from a regular app snap.
   203  	restore := release.MockOnClassic(false)
   204  	defer restore()
   205  
   206  	spec := &dbus.Specification{}
   207  	c.Assert(spec.AddPermanentSlot(s.iface, s.appSlotInfo), IsNil)
   208  	c.Assert(spec.SecurityTags(), DeepEquals, []string{"snap.producer.app"})
   209  	c.Assert(spec.SnippetForTag("snap.producer.app"), testutil.Contains, `<allow own="org.bluez"/>`)
   210  
   211  	// on a classic system with bluez slot coming from the core snap.
   212  	restore = release.MockOnClassic(true)
   213  	defer restore()
   214  
   215  	spec = &dbus.Specification{}
   216  	c.Assert(spec.AddPermanentSlot(s.iface, s.coreSlotInfo), IsNil)
   217  	c.Assert(spec.SecurityTags(), HasLen, 0)
   218  }
   219  
   220  func (s *BluezInterfaceSuite) TestSecCompSpec(c *C) {
   221  	// on a core system with bluez slot coming from a regular app snap.
   222  	restore := release.MockOnClassic(false)
   223  	defer restore()
   224  
   225  	spec := &seccomp.Specification{}
   226  	c.Assert(spec.AddPermanentSlot(s.iface, s.appSlotInfo), IsNil)
   227  	c.Assert(spec.SecurityTags(), DeepEquals, []string{"snap.producer.app"})
   228  	c.Assert(spec.SnippetForTag("snap.producer.app"), testutil.Contains, "listen\n")
   229  
   230  	// on a classic system with bluez slot coming from the core snap.
   231  	restore = release.MockOnClassic(true)
   232  	defer restore()
   233  
   234  	spec = &seccomp.Specification{}
   235  	c.Assert(spec.AddPermanentSlot(s.iface, s.coreSlotInfo), IsNil)
   236  	c.Assert(spec.SecurityTags(), HasLen, 0)
   237  
   238  }
   239  
   240  func (s *BluezInterfaceSuite) TestUDevSpec(c *C) {
   241  	// on a core system with bluez slot coming from a regular app snap.
   242  	restore := release.MockOnClassic(false)
   243  	defer restore()
   244  
   245  	spec := &udev.Specification{}
   246  	c.Assert(spec.AddConnectedPlug(s.iface, s.plug, s.appSlot), IsNil)
   247  	c.Assert(spec.Snippets(), HasLen, 2)
   248  	c.Assert(spec.Snippets(), testutil.Contains, `# bluez
   249  KERNEL=="rfkill", TAG+="snap_consumer_app"`)
   250  	c.Assert(spec.Snippets(), testutil.Contains, fmt.Sprintf(`TAG=="snap_consumer_app", RUN+="%v/snap-device-helper $env{ACTION} snap_consumer_app $devpath $major:$minor"`, dirs.DistroLibExecDir))
   251  
   252  	// on a classic system with bluez slot coming from the core snap.
   253  	restore = release.MockOnClassic(true)
   254  	defer restore()
   255  
   256  	spec = &udev.Specification{}
   257  	c.Assert(spec.AddConnectedPlug(s.iface, s.plug, s.coreSlot), IsNil)
   258  	c.Assert(spec.Snippets(), HasLen, 2)
   259  	c.Assert(spec.Snippets()[0], testutil.Contains, `KERNEL=="rfkill", TAG+="snap_consumer_app"`)
   260  	c.Assert(spec.Snippets(), testutil.Contains, fmt.Sprintf(`TAG=="snap_consumer_app", RUN+="%v/snap-device-helper $env{ACTION} snap_consumer_app $devpath $major:$minor"`, dirs.DistroLibExecDir))
   261  
   262  }
   263  
   264  func (s *BluezInterfaceSuite) TestStaticInfo(c *C) {
   265  	si := interfaces.StaticInfoOf(s.iface)
   266  	c.Assert(si.ImplicitOnCore, Equals, false)
   267  	c.Assert(si.ImplicitOnClassic, Equals, true)
   268  	c.Assert(si.Summary, Equals, `allows operating as the bluez service`)
   269  	c.Assert(si.BaseDeclarationSlots, testutil.Contains, "bluez")
   270  }
   271  
   272  func (s *BluezInterfaceSuite) TestAutoConnect(c *C) {
   273  	c.Assert(s.iface.AutoConnect(s.plugInfo, s.coreSlotInfo), Equals, true)
   274  	c.Assert(s.iface.AutoConnect(s.plugInfo, s.appSlotInfo), Equals, true)
   275  }
   276  
   277  func (s *BluezInterfaceSuite) TestInterfaces(c *C) {
   278  	c.Check(builtin.Interfaces(), testutil.DeepContains, s.iface)
   279  }