github.com/meulengracht/snapd@v0.0.0-20210719210640-8bde69bcc84e/interfaces/builtin/io_ports_control_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/seccomp"
    32  	"github.com/snapcore/snapd/interfaces/udev"
    33  	"github.com/snapcore/snapd/snap"
    34  	"github.com/snapcore/snapd/testutil"
    35  )
    36  
    37  type ioPortsControlInterfaceSuite struct {
    38  	iface    interfaces.Interface
    39  	slotInfo *snap.SlotInfo
    40  	slot     *interfaces.ConnectedSlot
    41  	plugInfo *snap.PlugInfo
    42  	plug     *interfaces.ConnectedPlug
    43  }
    44  
    45  var _ = Suite(&ioPortsControlInterfaceSuite{
    46  	iface: builtin.MustInterface("io-ports-control"),
    47  })
    48  
    49  const ioPortsControlConsumerYaml = `name: consumer
    50  version: 0
    51  apps:
    52   app:
    53    plugs: [io-ports-control]
    54  `
    55  
    56  const ioPortsControlCoreYaml = `name: core
    57  version: 0
    58  type: os
    59  slots:
    60    io-ports-control:
    61  `
    62  
    63  func (s *ioPortsControlInterfaceSuite) SetUpTest(c *C) {
    64  	s.plug, s.plugInfo = MockConnectedPlug(c, ioPortsControlConsumerYaml, nil, "io-ports-control")
    65  	s.slot, s.slotInfo = MockConnectedSlot(c, ioPortsControlCoreYaml, nil, "io-ports-control")
    66  }
    67  
    68  func (s *ioPortsControlInterfaceSuite) TestName(c *C) {
    69  	c.Assert(s.iface.Name(), Equals, "io-ports-control")
    70  }
    71  
    72  func (s *ioPortsControlInterfaceSuite) TestSanitizeSlot(c *C) {
    73  	c.Assert(interfaces.BeforePrepareSlot(s.iface, s.slotInfo), IsNil)
    74  }
    75  
    76  func (s *ioPortsControlInterfaceSuite) TestSanitizePlug(c *C) {
    77  	c.Assert(interfaces.BeforePreparePlug(s.iface, s.plugInfo), IsNil)
    78  }
    79  
    80  func (s *ioPortsControlInterfaceSuite) TestAppArmorSpec(c *C) {
    81  	spec := &apparmor.Specification{}
    82  	c.Assert(spec.AddConnectedPlug(s.iface, s.plug, s.slot), IsNil)
    83  	c.Assert(spec.SecurityTags(), DeepEquals, []string{"snap.consumer.app"})
    84  	c.Assert(spec.SnippetForTag("snap.consumer.app"), testutil.Contains, "/dev/port rw,")
    85  }
    86  
    87  func (s *ioPortsControlInterfaceSuite) TestSecCompSpec(c *C) {
    88  	spec := &seccomp.Specification{}
    89  	c.Assert(spec.AddConnectedPlug(s.iface, s.plug, s.slot), IsNil)
    90  	c.Assert(spec.SecurityTags(), DeepEquals, []string{"snap.consumer.app"})
    91  	c.Assert(spec.SnippetForTag("snap.consumer.app"), testutil.Contains, "ioperm\n")
    92  }
    93  
    94  func (s *ioPortsControlInterfaceSuite) TestUDevSpec(c *C) {
    95  	udevSpec := &udev.Specification{}
    96  	c.Assert(udevSpec.AddConnectedPlug(s.iface, s.plug, s.slot), IsNil)
    97  	c.Assert(udevSpec.Snippets(), HasLen, 2)
    98  	c.Assert(udevSpec.Snippets(), testutil.Contains, `# io-ports-control
    99  KERNEL=="port", TAG+="snap_consumer_app"`)
   100  	c.Assert(udevSpec.Snippets(), testutil.Contains, fmt.Sprintf(`TAG=="snap_consumer_app", RUN+="%v/snap-device-helper $env{ACTION} snap_consumer_app $devpath $major:$minor"`, dirs.DistroLibExecDir))
   101  }
   102  
   103  func (s *ioPortsControlInterfaceSuite) TestStaticInfo(c *C) {
   104  	si := interfaces.StaticInfoOf(s.iface)
   105  	c.Assert(si.ImplicitOnCore, Equals, true)
   106  	c.Assert(si.ImplicitOnClassic, Equals, true)
   107  	c.Assert(si.Summary, Equals, `allows access to all I/O ports`)
   108  	c.Assert(si.BaseDeclarationSlots, testutil.Contains, "io-ports-control")
   109  }
   110  
   111  func (s *ioPortsControlInterfaceSuite) TestAutoConnect(c *C) {
   112  	c.Assert(s.iface.AutoConnect(s.plugInfo, s.slotInfo), Equals, true)
   113  }
   114  
   115  func (s *ioPortsControlInterfaceSuite) TestInterfaces(c *C) {
   116  	c.Check(builtin.Interfaces(), testutil.DeepContains, s.iface)
   117  }