github.com/rigado/snapd@v2.42.5-go-mod+incompatible/interfaces/builtin/raw_usb_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  	. "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/snap"
    31  	"github.com/snapcore/snapd/testutil"
    32  )
    33  
    34  type RawUsbInterfaceSuite 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(&RawUsbInterfaceSuite{
    43  	iface: builtin.MustInterface("raw-usb"),
    44  })
    45  
    46  const rawusbConsumerYaml = `name: consumer
    47  version: 0
    48  apps:
    49   app:
    50    plugs: [raw-usb]
    51  `
    52  
    53  const rawusbCoreYaml = `name: core
    54  version: 0
    55  type: os
    56  slots:
    57    raw-usb:
    58  `
    59  
    60  func (s *RawUsbInterfaceSuite) SetUpTest(c *C) {
    61  	s.plug, s.plugInfo = MockConnectedPlug(c, rawusbConsumerYaml, nil, "raw-usb")
    62  	s.slot, s.slotInfo = MockConnectedSlot(c, rawusbCoreYaml, nil, "raw-usb")
    63  }
    64  
    65  func (s *RawUsbInterfaceSuite) TestName(c *C) {
    66  	c.Assert(s.iface.Name(), Equals, "raw-usb")
    67  }
    68  
    69  func (s *RawUsbInterfaceSuite) TestSanitizeSlot(c *C) {
    70  	c.Assert(interfaces.BeforePrepareSlot(s.iface, s.slotInfo), IsNil)
    71  }
    72  
    73  func (s *RawUsbInterfaceSuite) TestSanitizePlug(c *C) {
    74  	c.Assert(interfaces.BeforePreparePlug(s.iface, s.plugInfo), IsNil)
    75  }
    76  
    77  func (s *RawUsbInterfaceSuite) TestAppArmorSpec(c *C) {
    78  	spec := &apparmor.Specification{}
    79  	c.Assert(spec.AddConnectedPlug(s.iface, s.plug, s.slot), IsNil)
    80  	c.Assert(spec.SecurityTags(), DeepEquals, []string{"snap.consumer.app"})
    81  	c.Assert(spec.SnippetForTag("snap.consumer.app"), testutil.Contains, `/sys/bus/usb/devices/`)
    82  }
    83  
    84  func (s *RawUsbInterfaceSuite) TestSecCompSpec(c *C) {
    85  	spec := &seccomp.Specification{}
    86  	c.Assert(spec.AddConnectedPlug(s.iface, s.plug, s.slot), IsNil)
    87  	c.Assert(spec.SecurityTags(), DeepEquals, []string{"snap.consumer.app"})
    88  	c.Assert(spec.SnippetForTag("snap.consumer.app"), testutil.Contains, `socket AF_NETLINK - NETLINK_KOBJECT_UEVENT`)
    89  }
    90  
    91  func (s *RawUsbInterfaceSuite) TestUDevSpec(c *C) {
    92  	spec := &udev.Specification{}
    93  	c.Assert(spec.AddConnectedPlug(s.iface, s.plug, s.slot), IsNil)
    94  	c.Assert(spec.Snippets(), HasLen, 3)
    95  	c.Assert(spec.Snippets(), testutil.Contains, `# raw-usb
    96  SUBSYSTEM=="usb", TAG+="snap_consumer_app"`)
    97  	c.Assert(spec.Snippets(), testutil.Contains, `# raw-usb
    98  SUBSYSTEM=="tty", ENV{ID_BUS}=="usb", TAG+="snap_consumer_app"`)
    99  	c.Assert(spec.Snippets(), testutil.Contains, `TAG=="snap_consumer_app", RUN+="/usr/lib/snapd/snap-device-helper $env{ACTION} snap_consumer_app $devpath $major:$minor"`)
   100  }
   101  
   102  func (s *RawUsbInterfaceSuite) TestStaticInfo(c *C) {
   103  	si := interfaces.StaticInfoOf(s.iface)
   104  	c.Assert(si.ImplicitOnCore, Equals, true)
   105  	c.Assert(si.ImplicitOnClassic, Equals, true)
   106  	c.Assert(si.Summary, Equals, `allows raw access to all USB devices`)
   107  	c.Assert(si.BaseDeclarationSlots, testutil.Contains, "raw-usb")
   108  }
   109  
   110  func (s *RawUsbInterfaceSuite) TestAutoConnect(c *C) {
   111  	c.Assert(s.iface.AutoConnect(s.plugInfo, s.slotInfo), Equals, true)
   112  }
   113  
   114  func (s *RawUsbInterfaceSuite) TestInterfaces(c *C) {
   115  	c.Check(builtin.Interfaces(), testutil.DeepContains, s.iface)
   116  }