github.com/rigado/snapd@v2.42.5-go-mod+incompatible/interfaces/builtin/mpris_test.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2016 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 MprisInterfaceSuite struct {
    35  	iface    interfaces.Interface
    36  	slotInfo *snap.SlotInfo
    37  	slot     *interfaces.ConnectedSlot
    38  	plugInfo *snap.PlugInfo
    39  	plug     *interfaces.ConnectedPlug
    40  }
    41  
    42  var _ = Suite(&MprisInterfaceSuite{
    43  	iface: builtin.MustInterface("mpris"),
    44  })
    45  
    46  func (s *MprisInterfaceSuite) SetUpTest(c *C) {
    47  	var mockPlugSnapInfoYaml = `name: other
    48  version: 1.0
    49  apps:
    50   app:
    51    command: foo
    52    plugs: [mpris]
    53  `
    54  	var mockSlotSnapInfoYaml = `name: mpris
    55  version: 1.0
    56  apps:
    57   app:
    58    command: foo
    59    slots: [mpris]
    60  `
    61  
    62  	snapInfo := snaptest.MockInfo(c, mockPlugSnapInfoYaml, nil)
    63  	s.plugInfo = snapInfo.Plugs["mpris"]
    64  	s.plug = interfaces.NewConnectedPlug(s.plugInfo, nil, nil)
    65  	snapInfo = snaptest.MockInfo(c, mockSlotSnapInfoYaml, nil)
    66  	s.slotInfo = snapInfo.Slots["mpris"]
    67  	s.slot = interfaces.NewConnectedSlot(s.slotInfo, nil, nil)
    68  }
    69  
    70  func (s *MprisInterfaceSuite) TestName(c *C) {
    71  	c.Assert(s.iface.Name(), Equals, "mpris")
    72  }
    73  
    74  func (s *MprisInterfaceSuite) TestGetName(c *C) {
    75  	const mockSnapYaml = `name: mpris-client
    76  version: 1.0
    77  slots:
    78   mpris-slot:
    79    interface: mpris
    80    name: foo
    81  `
    82  	info := snaptest.MockInfo(c, mockSnapYaml, nil)
    83  	slot := info.Slots["mpris-slot"]
    84  	name, err := builtin.MprisGetName(s.iface, slot.Attrs)
    85  	c.Assert(err, IsNil)
    86  	c.Assert(name, Equals, "foo")
    87  }
    88  
    89  func (s *MprisInterfaceSuite) TestGetNameMissing(c *C) {
    90  	const mockSnapYaml = `name: mpris-client
    91  version: 1.0
    92  slots:
    93   mpris-slot:
    94    interface: mpris
    95  `
    96  	info := snaptest.MockInfo(c, mockSnapYaml, nil)
    97  	slot := info.Slots["mpris-slot"]
    98  	name, err := builtin.MprisGetName(s.iface, slot.Attrs)
    99  	c.Assert(err, IsNil)
   100  	c.Assert(name, Equals, "@{SNAP_INSTANCE_NAME}")
   101  }
   102  func (s *MprisInterfaceSuite) TestGetNameBadDot(c *C) {
   103  	const mockSnapYaml = `name: mpris-client
   104  version: 1.0
   105  slots:
   106   mpris-slot:
   107    interface: mpris
   108    name: foo.bar
   109  `
   110  	info := snaptest.MockInfo(c, mockSnapYaml, nil)
   111  	slot := info.Slots["mpris-slot"]
   112  	name, err := builtin.MprisGetName(s.iface, slot.Attrs)
   113  	c.Assert(err, Not(IsNil))
   114  	c.Assert(err, ErrorMatches, "invalid name element: \"foo.bar\"")
   115  	c.Assert(name, Equals, "")
   116  }
   117  
   118  func (s *MprisInterfaceSuite) TestGetNameBadList(c *C) {
   119  	const mockSnapYaml = `name: mpris-client
   120  version: 1.0
   121  slots:
   122   mpris-slot:
   123    interface: mpris
   124    name:
   125    - foo
   126  `
   127  	info := snaptest.MockInfo(c, mockSnapYaml, nil)
   128  	slot := info.Slots["mpris-slot"]
   129  	name, err := builtin.MprisGetName(s.iface, slot.Attrs)
   130  	c.Assert(err, Not(IsNil))
   131  	c.Assert(err, ErrorMatches, `name element \[foo\] is not a string`)
   132  	c.Assert(name, Equals, "")
   133  }
   134  
   135  func (s *MprisInterfaceSuite) TestGetNameUnknownAttribute(c *C) {
   136  	const mockSnapYaml = `name: mpris-client
   137  version: 1.0
   138  slots:
   139   mpris-slot:
   140    interface: mpris
   141    unknown: foo
   142  `
   143  	info := snaptest.MockInfo(c, mockSnapYaml, nil)
   144  	slot := info.Slots["mpris-slot"]
   145  	name, err := builtin.MprisGetName(s.iface, slot.Attrs)
   146  	c.Assert(err, Not(IsNil))
   147  	c.Assert(err, ErrorMatches, "unknown attribute 'unknown'")
   148  	c.Assert(name, Equals, "")
   149  }
   150  
   151  // The label glob when all apps are bound to the mpris slot
   152  func (s *MprisInterfaceSuite) TestConnectedPlugSnippetUsesSlotLabelAll(c *C) {
   153  	app1 := &snap.AppInfo{Name: "app1"}
   154  	app2 := &snap.AppInfo{Name: "app2"}
   155  	slot := interfaces.NewConnectedSlot(&snap.SlotInfo{
   156  		Snap: &snap.Info{
   157  			SuggestedName: "mpris",
   158  			Apps:          map[string]*snap.AppInfo{"app1": app1, "app2": app2},
   159  		},
   160  		Name:      "mpris",
   161  		Interface: "mpris",
   162  		Apps:      map[string]*snap.AppInfo{"app1": app1, "app2": app2},
   163  	}, nil, nil)
   164  
   165  	apparmorSpec := &apparmor.Specification{}
   166  	err := apparmorSpec.AddConnectedPlug(s.iface, s.plug, slot)
   167  	c.Assert(err, IsNil)
   168  	c.Assert(apparmorSpec.SecurityTags(), DeepEquals, []string{"snap.other.app"})
   169  	c.Assert(apparmorSpec.SnippetForTag("snap.other.app"), testutil.Contains, `peer=(label="snap.mpris.*"),`)
   170  }
   171  
   172  // The label uses alternation when some, but not all, apps are bound to the mpris slot
   173  func (s *MprisInterfaceSuite) TestConnectedPlugSnippetUsesSlotLabelSome(c *C) {
   174  	app1 := &snap.AppInfo{Name: "app1"}
   175  	app2 := &snap.AppInfo{Name: "app2"}
   176  	app3 := &snap.AppInfo{Name: "app3"}
   177  	slot := interfaces.NewConnectedSlot(&snap.SlotInfo{
   178  		Snap: &snap.Info{
   179  			SuggestedName: "mpris",
   180  			Apps:          map[string]*snap.AppInfo{"app1": app1, "app2": app2, "app3": app3},
   181  		},
   182  		Name:      "mpris",
   183  		Interface: "mpris",
   184  		Apps:      map[string]*snap.AppInfo{"app1": app1, "app2": app2},
   185  	}, nil, nil)
   186  
   187  	apparmorSpec := &apparmor.Specification{}
   188  	err := apparmorSpec.AddConnectedPlug(s.iface, s.plug, slot)
   189  	c.Assert(err, IsNil)
   190  	c.Assert(apparmorSpec.SecurityTags(), DeepEquals, []string{"snap.other.app"})
   191  	c.Assert(apparmorSpec.SnippetForTag("snap.other.app"), testutil.Contains, `peer=(label="snap.mpris.{app1,app2}"),`)
   192  }
   193  
   194  // The label uses short form when exactly one app is bound to the mpris slot
   195  func (s *MprisInterfaceSuite) TestConnectedPlugSnippetUsesSlotLabelOne(c *C) {
   196  	app := &snap.AppInfo{Name: "app"}
   197  	slot := interfaces.NewConnectedSlot(&snap.SlotInfo{
   198  		Snap: &snap.Info{
   199  			SuggestedName: "mpris",
   200  			Apps:          map[string]*snap.AppInfo{"app": app},
   201  		},
   202  		Name:      "mpris",
   203  		Interface: "mpris",
   204  		Apps:      map[string]*snap.AppInfo{"app": app},
   205  	}, nil, nil)
   206  
   207  	apparmorSpec := &apparmor.Specification{}
   208  	err := apparmorSpec.AddConnectedPlug(s.iface, s.plug, slot)
   209  	c.Assert(err, IsNil)
   210  	c.Assert(apparmorSpec.SecurityTags(), DeepEquals, []string{"snap.other.app"})
   211  	c.Assert(apparmorSpec.SnippetForTag("snap.other.app"), testutil.Contains, `peer=(label="snap.mpris.app"),`)
   212  }
   213  
   214  // The label glob when all apps are bound to the mpris plug
   215  func (s *MprisInterfaceSuite) TestConnectedSlotSnippetUsesPlugLabelAll(c *C) {
   216  	app1 := &snap.AppInfo{Name: "app1"}
   217  	app2 := &snap.AppInfo{Name: "app2"}
   218  	plug := interfaces.NewConnectedPlug(&snap.PlugInfo{
   219  		Snap: &snap.Info{
   220  			SuggestedName: "mpris",
   221  			Apps:          map[string]*snap.AppInfo{"app1": app1, "app2": app2},
   222  		},
   223  		Name:      "mpris",
   224  		Interface: "mpris",
   225  		Apps:      map[string]*snap.AppInfo{"app1": app1, "app2": app2},
   226  	}, nil, nil)
   227  
   228  	apparmorSpec := &apparmor.Specification{}
   229  	err := apparmorSpec.AddConnectedSlot(s.iface, plug, s.slot)
   230  	c.Assert(err, IsNil)
   231  	c.Assert(apparmorSpec.SecurityTags(), DeepEquals, []string{"snap.mpris.app"})
   232  	c.Assert(apparmorSpec.SnippetForTag("snap.mpris.app"), testutil.Contains, `peer=(label="snap.mpris.*"),`)
   233  }
   234  
   235  // The label uses alternation when some, but not all, apps is bound to the mpris plug
   236  func (s *MprisInterfaceSuite) TestConnectedSlotSnippetUsesPlugLabelSome(c *C) {
   237  	app1 := &snap.AppInfo{Name: "app1"}
   238  	app2 := &snap.AppInfo{Name: "app2"}
   239  	app3 := &snap.AppInfo{Name: "app3"}
   240  	plug := interfaces.NewConnectedPlug(&snap.PlugInfo{
   241  		Snap: &snap.Info{
   242  			SuggestedName: "mpris",
   243  			Apps:          map[string]*snap.AppInfo{"app1": app1, "app2": app2, "app3": app3},
   244  		},
   245  		Name:      "mpris",
   246  		Interface: "mpris",
   247  		Apps:      map[string]*snap.AppInfo{"app1": app1, "app2": app2},
   248  	}, nil, nil)
   249  
   250  	apparmorSpec := &apparmor.Specification{}
   251  	err := apparmorSpec.AddConnectedSlot(s.iface, plug, s.slot)
   252  	c.Assert(err, IsNil)
   253  	c.Assert(apparmorSpec.SecurityTags(), DeepEquals, []string{"snap.mpris.app"})
   254  	c.Assert(apparmorSpec.SnippetForTag("snap.mpris.app"), testutil.Contains, `peer=(label="snap.mpris.{app1,app2}"),`)
   255  }
   256  
   257  // The label uses short form when exactly one app is bound to the mpris plug
   258  func (s *MprisInterfaceSuite) TestConnectedSlotSnippetUsesPlugLabelOne(c *C) {
   259  	app := &snap.AppInfo{Name: "app"}
   260  	plug := interfaces.NewConnectedPlug(&snap.PlugInfo{
   261  		Snap: &snap.Info{
   262  			SuggestedName: "mpris",
   263  			Apps:          map[string]*snap.AppInfo{"app": app},
   264  		},
   265  		Name:      "mpris",
   266  		Interface: "mpris",
   267  		Apps:      map[string]*snap.AppInfo{"app": app},
   268  	}, nil, nil)
   269  
   270  	apparmorSpec := &apparmor.Specification{}
   271  	err := apparmorSpec.AddConnectedSlot(s.iface, plug, s.slot)
   272  	c.Assert(err, IsNil)
   273  	c.Assert(apparmorSpec.SecurityTags(), DeepEquals, []string{"snap.mpris.app"})
   274  	c.Assert(apparmorSpec.SnippetForTag("snap.mpris.app"), testutil.Contains, `peer=(label="snap.mpris.app"),`)
   275  }
   276  
   277  func (s *MprisInterfaceSuite) TestPermanentSlotAppArmor(c *C) {
   278  	apparmorSpec := &apparmor.Specification{}
   279  	err := apparmorSpec.AddPermanentSlot(s.iface, s.slotInfo)
   280  	c.Assert(err, IsNil)
   281  	c.Assert(apparmorSpec.SecurityTags(), DeepEquals, []string{"snap.mpris.app"})
   282  
   283  	// verify bind rule
   284  	c.Assert(apparmorSpec.SnippetForTag("snap.mpris.app"), testutil.Contains, "dbus (bind)\n    bus=session\n    name=\"org.mpris.MediaPlayer2.@{SNAP_INSTANCE_NAME}{,.*}\",\n")
   285  }
   286  
   287  func (s *MprisInterfaceSuite) TestPermanentSlotAppArmorWithName(c *C) {
   288  	const mockSnapYaml = `name: mpris-client
   289  version: 1.0
   290  slots:
   291   mpris-slot:
   292    interface: mpris
   293    name: foo
   294  apps:
   295   app:
   296    command: foo
   297  `
   298  	info := snaptest.MockInfo(c, mockSnapYaml, nil)
   299  	slot := info.Slots["mpris-slot"]
   300  
   301  	apparmorSpec := &apparmor.Specification{}
   302  	err := apparmorSpec.AddPermanentSlot(s.iface, slot)
   303  	c.Assert(err, IsNil)
   304  	c.Assert(apparmorSpec.SecurityTags(), DeepEquals, []string{"snap.mpris-client.app"})
   305  
   306  	// verify bind rule
   307  	c.Assert(apparmorSpec.SnippetForTag("snap.mpris-client.app"), testutil.Contains, "dbus (bind)\n    bus=session\n    name=\"org.mpris.MediaPlayer2.foo{,.*}\",\n")
   308  }
   309  
   310  func (s *MprisInterfaceSuite) TestPermanentSlotAppArmorNative(c *C) {
   311  	restore := release.MockOnClassic(false)
   312  	defer restore()
   313  
   314  	apparmorSpec := &apparmor.Specification{}
   315  	err := apparmorSpec.AddPermanentSlot(s.iface, s.slotInfo)
   316  	c.Assert(err, IsNil)
   317  	c.Assert(apparmorSpec.SecurityTags(), DeepEquals, []string{"snap.mpris.app"})
   318  
   319  	// verify classic rule not present
   320  	c.Assert(apparmorSpec.SnippetForTag("snap.mpris.app"), Not(testutil.Contains), "# Allow unconfined clients to interact with the player on classic\n")
   321  }
   322  
   323  func (s *MprisInterfaceSuite) TestPermanentSlotAppArmorClassic(c *C) {
   324  	restore := release.MockOnClassic(true)
   325  	defer restore()
   326  
   327  	apparmorSpec := &apparmor.Specification{}
   328  	err := apparmorSpec.AddPermanentSlot(s.iface, s.slotInfo)
   329  	c.Assert(err, IsNil)
   330  	c.Assert(apparmorSpec.SecurityTags(), DeepEquals, []string{"snap.mpris.app"})
   331  
   332  	// verify classic rule present
   333  	c.Assert(apparmorSpec.SnippetForTag("snap.mpris.app"), testutil.Contains, "# Allow unconfined clients to interact with the player on classic\n")
   334  }
   335  
   336  func (s *MprisInterfaceSuite) TestInterfaces(c *C) {
   337  	c.Check(builtin.Interfaces(), testutil.DeepContains, s.iface)
   338  }