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

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2016-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/interfaces/seccomp"
    29  	"github.com/snapcore/snapd/interfaces/udev"
    30  	"github.com/snapcore/snapd/release"
    31  	"github.com/snapcore/snapd/snap"
    32  	"github.com/snapcore/snapd/testutil"
    33  )
    34  
    35  type X11InterfaceSuite struct {
    36  	iface           interfaces.Interface
    37  	coreSlotInfo    *snap.SlotInfo
    38  	coreSlot        *interfaces.ConnectedSlot
    39  	classicSlotInfo *snap.SlotInfo
    40  	classicSlot     *interfaces.ConnectedSlot
    41  	plugInfo        *snap.PlugInfo
    42  	plug            *interfaces.ConnectedPlug
    43  }
    44  
    45  var _ = Suite(&X11InterfaceSuite{
    46  	iface: builtin.MustInterface("x11"),
    47  })
    48  
    49  const x11MockPlugSnapInfoYaml = `name: consumer
    50  version: 0
    51  apps:
    52   app:
    53    plugs: [x11]
    54  `
    55  
    56  // an x11 slot on an x11 snap (as installed on a core/all-snap system)
    57  const x11CoreYaml = `name: x11
    58  version: 0
    59  apps:
    60   app1:
    61    slots: [x11]
    62  `
    63  
    64  // an x11 slot on the core snap (as automatically added on classic)
    65  const x11ClassicYaml = `name: core
    66  version: 0
    67  type: os
    68  slots:
    69   x11:
    70    interface: x11
    71  `
    72  
    73  func (s *X11InterfaceSuite) SetUpTest(c *C) {
    74  	s.plug, s.plugInfo = MockConnectedPlug(c, x11MockPlugSnapInfoYaml, nil, "x11")
    75  	s.coreSlot, s.coreSlotInfo = MockConnectedSlot(c, x11CoreYaml, nil, "x11")
    76  	s.classicSlot, s.classicSlotInfo = MockConnectedSlot(c, x11ClassicYaml, nil, "x11")
    77  }
    78  
    79  func (s *X11InterfaceSuite) TestName(c *C) {
    80  	c.Assert(s.iface.Name(), Equals, "x11")
    81  }
    82  
    83  func (s *X11InterfaceSuite) TestSanitizeSlot(c *C) {
    84  	c.Assert(interfaces.BeforePrepareSlot(s.iface, s.coreSlotInfo), IsNil)
    85  	c.Assert(interfaces.BeforePrepareSlot(s.iface, s.classicSlotInfo), IsNil)
    86  }
    87  
    88  func (s *X11InterfaceSuite) TestSanitizePlug(c *C) {
    89  	c.Assert(interfaces.BeforePreparePlug(s.iface, s.plugInfo), IsNil)
    90  }
    91  
    92  func (s *X11InterfaceSuite) TestAppArmorSpec(c *C) {
    93  	// on a core system with x11 slot coming from a regular app snap.
    94  	restore := release.MockOnClassic(false)
    95  	defer restore()
    96  
    97  	// connected plug to core slot
    98  	spec := &apparmor.Specification{}
    99  	c.Assert(spec.AddConnectedPlug(s.iface, s.plug, s.coreSlot), IsNil)
   100  	c.Assert(spec.SecurityTags(), DeepEquals, []string{"snap.consumer.app"})
   101  	c.Assert(spec.SnippetForTag("snap.consumer.app"), testutil.Contains, "fontconfig")
   102  
   103  	// connected core slot to plug
   104  	spec = &apparmor.Specification{}
   105  	c.Assert(spec.AddConnectedSlot(s.iface, s.plug, s.coreSlot), IsNil)
   106  	c.Assert(spec.SecurityTags(), DeepEquals, []string{"snap.x11.app1"})
   107  	c.Assert(spec.SnippetForTag("snap.x11.app1"), testutil.Contains, `peer=(label="snap.consumer.app"),`)
   108  
   109  	// permanent core slot
   110  	spec = &apparmor.Specification{}
   111  	c.Assert(spec.AddPermanentSlot(s.iface, s.coreSlotInfo), IsNil)
   112  	c.Assert(spec.SecurityTags(), DeepEquals, []string{"snap.x11.app1"})
   113  	c.Assert(spec.SnippetForTag("snap.x11.app1"), testutil.Contains, "capability sys_tty_config,")
   114  }
   115  
   116  func (s *X11InterfaceSuite) TestAppArmorSpecOnClassic(c *C) {
   117  	// on a classic system with x11 slot coming from the core snap.
   118  	restore := release.MockOnClassic(true)
   119  	defer restore()
   120  
   121  	// connected plug to classic slot
   122  	spec := &apparmor.Specification{}
   123  	c.Assert(spec.AddConnectedPlug(s.iface, s.plug, s.classicSlot), IsNil)
   124  	c.Assert(spec.SecurityTags(), DeepEquals, []string{"snap.consumer.app"})
   125  	c.Assert(spec.SnippetForTag("snap.consumer.app"), testutil.Contains, "owner /run/user/[0-9]*/.Xauthority r,")
   126  
   127  	// connected classic slot to plug
   128  	spec = &apparmor.Specification{}
   129  	c.Assert(spec.AddConnectedSlot(s.iface, s.plug, s.classicSlot), IsNil)
   130  	c.Assert(spec.SecurityTags(), HasLen, 0)
   131  
   132  	// permanent classic slot
   133  	spec = &apparmor.Specification{}
   134  	c.Assert(spec.AddPermanentSlot(s.iface, s.classicSlotInfo), IsNil)
   135  	c.Assert(spec.SecurityTags(), HasLen, 0)
   136  }
   137  
   138  func (s *X11InterfaceSuite) TestSecCompOnClassic(c *C) {
   139  	// on a classic system with x11 slot coming from the core snap.
   140  	restore := release.MockOnClassic(true)
   141  	defer restore()
   142  
   143  	seccompSpec := &seccomp.Specification{}
   144  	err := seccompSpec.AddPermanentSlot(s.iface, s.classicSlotInfo)
   145  	c.Assert(err, IsNil)
   146  	err = seccompSpec.AddConnectedPlug(s.iface, s.plug, s.classicSlot)
   147  	c.Assert(err, IsNil)
   148  
   149  	// app snap has additional seccomp rules
   150  	c.Assert(seccompSpec.SecurityTags(), DeepEquals, []string{"snap.consumer.app"})
   151  	c.Assert(seccompSpec.SnippetForTag("snap.consumer.app"), testutil.Contains, "bind\n")
   152  }
   153  
   154  func (s *X11InterfaceSuite) TestSecCompOnCore(c *C) {
   155  	// on a core system with x11 slot coming from a snap.
   156  	restore := release.MockOnClassic(false)
   157  	defer restore()
   158  
   159  	seccompSpec := &seccomp.Specification{}
   160  	err := seccompSpec.AddPermanentSlot(s.iface, s.coreSlotInfo)
   161  	c.Assert(err, IsNil)
   162  	err = seccompSpec.AddConnectedPlug(s.iface, s.plug, s.coreSlot)
   163  	c.Assert(err, IsNil)
   164  
   165  	// both app and x11 have secomp rules set
   166  	c.Assert(seccompSpec.SecurityTags(), DeepEquals, []string{"snap.consumer.app", "snap.x11.app1"})
   167  	c.Assert(seccompSpec.SnippetForTag("snap.x11.app1"), testutil.Contains, "listen\n")
   168  	c.Assert(seccompSpec.SnippetForTag("snap.consumer.app"), testutil.Contains, "bind\n")
   169  }
   170  
   171  func (s *X11InterfaceSuite) TestUDev(c *C) {
   172  	// on a core system with x11 slot coming from a regular app snap.
   173  	restore := release.MockOnClassic(false)
   174  	defer restore()
   175  
   176  	spec := &udev.Specification{}
   177  	c.Assert(spec.AddPermanentSlot(s.iface, s.coreSlotInfo), IsNil)
   178  	c.Assert(spec.Snippets(), HasLen, 6)
   179  	c.Assert(spec.Snippets(), testutil.Contains, `# x11
   180  KERNEL=="event[0-9]*", TAG+="snap_x11_app1"`)
   181  	c.Assert(spec.Snippets(), testutil.Contains, `# x11
   182  KERNEL=="mice", TAG+="snap_x11_app1"`)
   183  	c.Assert(spec.Snippets(), testutil.Contains, `# x11
   184  KERNEL=="mouse[0-9]*", TAG+="snap_x11_app1"`)
   185  	c.Assert(spec.Snippets(), testutil.Contains, `# x11
   186  KERNEL=="ts[0-9]*", TAG+="snap_x11_app1"`)
   187  	c.Assert(spec.Snippets(), testutil.Contains, `# x11
   188  KERNEL=="tty[0-9]*", TAG+="snap_x11_app1"`)
   189  	c.Assert(spec.Snippets(), testutil.Contains, `TAG=="snap_x11_app1", RUN+="/usr/lib/snapd/snap-device-helper $env{ACTION} snap_x11_app1 $devpath $major:$minor"`)
   190  	c.Assert(spec.TriggeredSubsystems(), DeepEquals, []string{"input"})
   191  
   192  	// on a classic system with x11 slot coming from the core snap.
   193  	restore = release.MockOnClassic(true)
   194  	defer restore()
   195  
   196  	spec = &udev.Specification{}
   197  	c.Assert(spec.AddPermanentSlot(s.iface, s.coreSlotInfo), IsNil)
   198  	c.Assert(spec.Snippets(), HasLen, 0)
   199  	c.Assert(spec.TriggeredSubsystems(), IsNil)
   200  }
   201  
   202  func (s *X11InterfaceSuite) TestStaticInfo(c *C) {
   203  	si := interfaces.StaticInfoOf(s.iface)
   204  	c.Assert(si.ImplicitOnCore, Equals, false)
   205  	c.Assert(si.ImplicitOnClassic, Equals, true)
   206  	c.Assert(si.Summary, Equals, `allows interacting with or running as an X11 server`)
   207  	c.Assert(si.BaseDeclarationSlots, testutil.Contains, "x11")
   208  }
   209  
   210  func (s *X11InterfaceSuite) TestAutoConnect(c *C) {
   211  	c.Assert(s.iface.AutoConnect(s.plugInfo, s.coreSlotInfo), Equals, true)
   212  	c.Assert(s.iface.AutoConnect(s.plugInfo, s.classicSlotInfo), Equals, true)
   213  }
   214  
   215  func (s *X11InterfaceSuite) TestInterfaces(c *C) {
   216  	c.Check(builtin.Interfaces(), testutil.DeepContains, s.iface)
   217  }