gitee.com/mysnapcore/mysnapd@v0.1.0/interfaces/builtin/wayland_test.go (about)

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