github.com/bugraaydogar/snapd@v0.0.0-20210315170335-8c70bb858939/interfaces/builtin/desktop_legacy_test.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 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/snap"
    29  	"github.com/snapcore/snapd/testutil"
    30  )
    31  
    32  type DesktopLegacyInterfaceSuite struct {
    33  	iface        interfaces.Interface
    34  	coreSlotInfo *snap.SlotInfo
    35  	coreSlot     *interfaces.ConnectedSlot
    36  	plugInfo     *snap.PlugInfo
    37  	plug         *interfaces.ConnectedPlug
    38  }
    39  
    40  var _ = Suite(&DesktopLegacyInterfaceSuite{
    41  	iface: builtin.MustInterface("desktop-legacy"),
    42  })
    43  
    44  const desktopLegacyConsumerYaml = `name: consumer
    45  version: 0
    46  apps:
    47   app:
    48    plugs: [desktop-legacy]
    49  `
    50  
    51  const desktopLegacyCoreYaml = `name: core
    52  version: 0
    53  type: os
    54  slots:
    55    desktop-legacy:
    56  `
    57  
    58  func (s *DesktopLegacyInterfaceSuite) SetUpTest(c *C) {
    59  	s.plug, s.plugInfo = MockConnectedPlug(c, desktopLegacyConsumerYaml, nil, "desktop-legacy")
    60  	s.coreSlot, s.coreSlotInfo = MockConnectedSlot(c, desktopLegacyCoreYaml, nil, "desktop-legacy")
    61  }
    62  
    63  func (s *DesktopLegacyInterfaceSuite) TestName(c *C) {
    64  	c.Assert(s.iface.Name(), Equals, "desktop-legacy")
    65  }
    66  
    67  func (s *DesktopLegacyInterfaceSuite) TestSanitizeSlot(c *C) {
    68  	c.Assert(interfaces.BeforePrepareSlot(s.iface, s.coreSlotInfo), IsNil)
    69  }
    70  
    71  func (s *DesktopLegacyInterfaceSuite) TestSanitizePlug(c *C) {
    72  	c.Assert(interfaces.BeforePreparePlug(s.iface, s.plugInfo), IsNil)
    73  }
    74  
    75  func (s *DesktopLegacyInterfaceSuite) TestAppArmorSpec(c *C) {
    76  	// connected plug to core slot
    77  	spec := &apparmor.Specification{}
    78  	c.Assert(spec.AddConnectedPlug(s.iface, s.plug, s.coreSlot), IsNil)
    79  	c.Assert(spec.SecurityTags(), DeepEquals, []string{"snap.consumer.app"})
    80  	c.Assert(spec.SnippetForTag("snap.consumer.app"), testutil.Contains, "# Description: Can access common desktop legacy methods")
    81  	c.Assert(spec.SnippetForTag("snap.consumer.app"), testutil.Contains, "#include <abstractions/dbus-accessibility-strict>")
    82  	c.Assert(spec.SnippetForTag("snap.consumer.app"), testutil.Contains, `peer=(addr="@/tmp/ibus/dbus-*"),`)
    83  	c.Assert(spec.SnippetForTag("snap.consumer.app"), testutil.Contains, `deny /var/lib/snapd/desktop/applications/mimeinfo.cache r,`)
    84  
    85  	// getDesktopFileRules() rules
    86  	c.Assert(spec.SnippetForTag("snap.consumer.app"), testutil.Contains, `# This leaks the names of snaps with desktop files`)
    87  	c.Assert(spec.SnippetForTag("snap.consumer.app"), testutil.Contains, `/var/lib/snapd/desktop/applications/ r,`)
    88  	c.Assert(spec.SnippetForTag("snap.consumer.app"), testutil.Contains, `/var/lib/snapd/desktop/applications/@{SNAP_INSTANCE_DESKTOP}_*.desktop r,`)
    89  	c.Assert(spec.SnippetForTag("snap.consumer.app"), testutil.Contains, `deny /var/lib/snapd/desktop/applications/@{SNAP_INSTANCE_DESKTOP}[^_.]*.desktop r,`)
    90  	c.Assert(spec.SnippetForTag("snap.consumer.app"), testutil.Contains, `deny /var/lib/snapd/desktop/applications/[^c]* r,`)
    91  	c.Assert(spec.SnippetForTag("snap.consumer.app"), testutil.Contains, `deny /var/lib/snapd/desktop/applications/consume[^r]* r,`)
    92  
    93  	// connected plug to core slot
    94  	spec = &apparmor.Specification{}
    95  	c.Assert(spec.AddConnectedSlot(s.iface, s.plug, s.coreSlot), IsNil)
    96  	c.Assert(spec.SecurityTags(), HasLen, 0)
    97  }
    98  
    99  func (s *DesktopLegacyInterfaceSuite) TestStaticInfo(c *C) {
   100  	si := interfaces.StaticInfoOf(s.iface)
   101  	c.Assert(si.ImplicitOnCore, Equals, false)
   102  	c.Assert(si.ImplicitOnClassic, Equals, true)
   103  	c.Assert(si.Summary, Equals, `allows privileged access to desktop legacy methods`)
   104  	c.Assert(si.BaseDeclarationSlots, testutil.Contains, "desktop-legacy")
   105  }
   106  
   107  func (s *DesktopLegacyInterfaceSuite) TestInterfaces(c *C) {
   108  	c.Check(builtin.Interfaces(), testutil.DeepContains, s.iface)
   109  }