gitee.com/mysnapcore/mysnapd@v0.1.0/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  	"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/snap"
    34  	"gitee.com/mysnapcore/mysnapd/testutil"
    35  )
    36  
    37  type RawUsbInterfaceSuite 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(&RawUsbInterfaceSuite{
    46  	iface: builtin.MustInterface("raw-usb"),
    47  })
    48  
    49  const rawusbConsumerYaml = `name: consumer
    50  version: 0
    51  apps:
    52   app:
    53    plugs: [raw-usb]
    54  `
    55  
    56  const rawusbCoreYaml = `name: core
    57  version: 0
    58  type: os
    59  slots:
    60    raw-usb:
    61  `
    62  
    63  func (s *RawUsbInterfaceSuite) SetUpTest(c *C) {
    64  	s.plug, s.plugInfo = MockConnectedPlug(c, rawusbConsumerYaml, nil, "raw-usb")
    65  	s.slot, s.slotInfo = MockConnectedSlot(c, rawusbCoreYaml, nil, "raw-usb")
    66  }
    67  
    68  func (s *RawUsbInterfaceSuite) TestName(c *C) {
    69  	c.Assert(s.iface.Name(), Equals, "raw-usb")
    70  }
    71  
    72  func (s *RawUsbInterfaceSuite) TestSanitizeSlot(c *C) {
    73  	c.Assert(interfaces.BeforePrepareSlot(s.iface, s.slotInfo), IsNil)
    74  }
    75  
    76  func (s *RawUsbInterfaceSuite) TestSanitizePlug(c *C) {
    77  	c.Assert(interfaces.BeforePreparePlug(s.iface, s.plugInfo), IsNil)
    78  }
    79  
    80  func (s *RawUsbInterfaceSuite) 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, `/sys/bus/usb/devices/`)
    85  }
    86  
    87  func (s *RawUsbInterfaceSuite) 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, `socket AF_NETLINK - NETLINK_KOBJECT_UEVENT`)
    92  }
    93  
    94  func (s *RawUsbInterfaceSuite) TestUDevSpec(c *C) {
    95  	spec := &udev.Specification{}
    96  	c.Assert(spec.AddConnectedPlug(s.iface, s.plug, s.slot), IsNil)
    97  	c.Assert(spec.Snippets(), HasLen, 4)
    98  	c.Assert(spec.Snippets(), testutil.Contains, `# raw-usb
    99  SUBSYSTEM=="usb", TAG+="snap_consumer_app"`)
   100  	c.Assert(spec.Snippets(), testutil.Contains, `# raw-usb
   101  SUBSYSTEM=="usbmisc", TAG+="snap_consumer_app"`)
   102  	c.Assert(spec.Snippets(), testutil.Contains, `# raw-usb
   103  SUBSYSTEM=="tty", ENV{ID_BUS}=="usb", TAG+="snap_consumer_app"`)
   104  	c.Assert(spec.Snippets(), testutil.Contains, fmt.Sprintf(`TAG=="snap_consumer_app", RUN+="%v/snap-device-helper $env{ACTION} snap_consumer_app $devpath $major:$minor"`, dirs.DistroLibExecDir))
   105  }
   106  
   107  func (s *RawUsbInterfaceSuite) TestStaticInfo(c *C) {
   108  	si := interfaces.StaticInfoOf(s.iface)
   109  	c.Assert(si.ImplicitOnCore, Equals, true)
   110  	c.Assert(si.ImplicitOnClassic, Equals, true)
   111  	c.Assert(si.Summary, Equals, `allows raw access to all USB devices`)
   112  	c.Assert(si.BaseDeclarationSlots, testutil.Contains, "raw-usb")
   113  }
   114  
   115  func (s *RawUsbInterfaceSuite) TestAutoConnect(c *C) {
   116  	c.Assert(s.iface.AutoConnect(s.plugInfo, s.slotInfo), Equals, true)
   117  }
   118  
   119  func (s *RawUsbInterfaceSuite) TestInterfaces(c *C) {
   120  	c.Check(builtin.Interfaces(), testutil.DeepContains, s.iface)
   121  }