github.com/kubiko/snapd@v0.0.0-20201013125620-d4f3094d9ddf/interfaces/builtin/i2c_test.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2016 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/udev"
    29  	"github.com/snapcore/snapd/snap"
    30  	"github.com/snapcore/snapd/snap/snaptest"
    31  	"github.com/snapcore/snapd/testutil"
    32  )
    33  
    34  type I2cInterfaceSuite struct {
    35  	testutil.BaseTest
    36  	iface interfaces.Interface
    37  
    38  	// OS Snap
    39  	testSlot1           *interfaces.ConnectedSlot
    40  	testSlot1Info       *snap.SlotInfo
    41  	testSlotCleaned     *interfaces.ConnectedSlot
    42  	testSlotCleanedInfo *snap.SlotInfo
    43  
    44  	// Gadget Snap
    45  	testUDev1                  *interfaces.ConnectedSlot
    46  	testUDev1Info              *snap.SlotInfo
    47  	testUDev2                  *interfaces.ConnectedSlot
    48  	testUDev2Info              *snap.SlotInfo
    49  	testUDev3                  *interfaces.ConnectedSlot
    50  	testUDev3Info              *snap.SlotInfo
    51  	testSysfsName1             *interfaces.ConnectedSlot
    52  	testSysfsName1Info         *snap.SlotInfo
    53  	testUDevBadValue1          *interfaces.ConnectedSlot
    54  	testUDevBadValue1Info      *snap.SlotInfo
    55  	testUDevBadValue2          *interfaces.ConnectedSlot
    56  	testUDevBadValue2Info      *snap.SlotInfo
    57  	testUDevBadValue3          *interfaces.ConnectedSlot
    58  	testUDevBadValue3Info      *snap.SlotInfo
    59  	testUDevBadValue4          *interfaces.ConnectedSlot
    60  	testUDevBadValue4Info      *snap.SlotInfo
    61  	testUDevBadValue5          *interfaces.ConnectedSlot
    62  	testUDevBadValue5Info      *snap.SlotInfo
    63  	testUDevBadValue6          *interfaces.ConnectedSlot
    64  	testUDevBadValue6Info      *snap.SlotInfo
    65  	testUDevBadValue7          *interfaces.ConnectedSlot
    66  	testUDevBadValue7Info      *snap.SlotInfo
    67  	testUDevBadInterface1      *interfaces.ConnectedSlot
    68  	testUDevBadInterface1Info  *snap.SlotInfo
    69  	testSysfsNameBadValue1     *interfaces.ConnectedSlot
    70  	testSysfsNameBadValue1Info *snap.SlotInfo
    71  	testSysfsNameAndPath       *interfaces.ConnectedSlot
    72  	testSysfsNameAndPathInfo   *snap.SlotInfo
    73  	testSysfsNameEmpty         *interfaces.ConnectedSlot
    74  	testSysfsNameEmptyInfo     *snap.SlotInfo
    75  
    76  	// Consuming Snap
    77  	testPlugPort1     *interfaces.ConnectedPlug
    78  	testPlugPort1Info *snap.PlugInfo
    79  }
    80  
    81  var _ = Suite(&I2cInterfaceSuite{
    82  	iface: builtin.MustInterface("i2c"),
    83  })
    84  
    85  func (s *I2cInterfaceSuite) SetUpTest(c *C) {
    86  	// Mock for OS Snap
    87  	osSnapInfo := snaptest.MockInfo(c, `
    88  name: ubuntu-core
    89  version: 0
    90  type: os
    91  slots:
    92    test-port-1:
    93      interface: i2c
    94      path: /dev/i2c-0
    95    test-port-unclean:
    96      interface: i2c
    97      path: /dev/i2c-1/./
    98  `, nil)
    99  	s.testSlot1Info = osSnapInfo.Slots["test-port-1"]
   100  	s.testSlotCleanedInfo = osSnapInfo.Slots["test-port-unclean"]
   101  
   102  	// Mock for Gadget Snap
   103  	gadgetSnapInfo := snaptest.MockInfo(c, `
   104  name: some-device
   105  version: 0
   106  type: gadget
   107  slots:
   108    test-udev-1:
   109      interface: i2c
   110      path: /dev/i2c-1
   111    test-udev-2:
   112      interface: i2c
   113      path: /dev/i2c-2
   114    test-udev-3:
   115      interface: i2c
   116      path: /dev/i2c-0
   117    test-sysfs-name-1:
   118      interface: i2c
   119      sysfs-name: 1-0050
   120    test-udev-bad-value-1:
   121      interface: i2c
   122      path: /dev/i2c
   123    test-udev-bad-value-2:
   124      interface: i2c
   125      path: /dev/i2c-a
   126    test-udev-bad-value-3:
   127      interface: i2c
   128      path: /dev/i2c-2a
   129    test-udev-bad-value-4:
   130      interface: i2c
   131      path: /dev/foo-0
   132    test-udev-bad-value-5:
   133      interface: i2c
   134      path: /dev/i2c-foo
   135    test-udev-bad-value-6:
   136      interface: i2c
   137      path: ""
   138    test-udev-bad-value-7:
   139      interface: i2c
   140    test-udev-bad-interface-1:
   141      interface: other-interface
   142    test-sysfs-name-bad-value-1:
   143      interface: i2c
   144      sysfs-name: /slash/not/allowed
   145    test-sysfs-name-and-path:
   146      interface: i2c
   147      path: /dev/i2c-0
   148      sysfs-name: 1-0050
   149    test-sysfs-name-empty:
   150      interface: i2c
   151      sysfs-name: ""
   152  `, nil)
   153  	s.testUDev1Info = gadgetSnapInfo.Slots["test-udev-1"]
   154  	s.testUDev1 = interfaces.NewConnectedSlot(s.testUDev1Info, nil, nil)
   155  	s.testUDev2Info = gadgetSnapInfo.Slots["test-udev-2"]
   156  	s.testUDev2 = interfaces.NewConnectedSlot(s.testUDev2Info, nil, nil)
   157  	s.testUDev3Info = gadgetSnapInfo.Slots["test-udev-3"]
   158  	s.testUDev3 = interfaces.NewConnectedSlot(s.testUDev3Info, nil, nil)
   159  	s.testSysfsName1Info = gadgetSnapInfo.Slots["test-sysfs-name-1"]
   160  	s.testSysfsName1 = interfaces.NewConnectedSlot(s.testSysfsName1Info, nil, nil)
   161  	s.testUDevBadValue1Info = gadgetSnapInfo.Slots["test-udev-bad-value-1"]
   162  	s.testUDevBadValue1 = interfaces.NewConnectedSlot(s.testUDevBadValue1Info, nil, nil)
   163  	s.testUDevBadValue2Info = gadgetSnapInfo.Slots["test-udev-bad-value-2"]
   164  	s.testUDevBadValue2 = interfaces.NewConnectedSlot(s.testUDevBadValue2Info, nil, nil)
   165  	s.testUDevBadValue3Info = gadgetSnapInfo.Slots["test-udev-bad-value-3"]
   166  	s.testUDevBadValue3 = interfaces.NewConnectedSlot(s.testUDevBadValue3Info, nil, nil)
   167  	s.testUDevBadValue4Info = gadgetSnapInfo.Slots["test-udev-bad-value-4"]
   168  	s.testUDevBadValue4 = interfaces.NewConnectedSlot(s.testUDevBadValue4Info, nil, nil)
   169  	s.testUDevBadValue5Info = gadgetSnapInfo.Slots["test-udev-bad-value-5"]
   170  	s.testUDevBadValue5 = interfaces.NewConnectedSlot(s.testUDevBadValue5Info, nil, nil)
   171  	s.testUDevBadValue6Info = gadgetSnapInfo.Slots["test-udev-bad-value-6"]
   172  	s.testUDevBadValue6 = interfaces.NewConnectedSlot(s.testUDevBadValue6Info, nil, nil)
   173  	s.testUDevBadValue7Info = gadgetSnapInfo.Slots["test-udev-bad-value-7"]
   174  	s.testUDevBadValue7 = interfaces.NewConnectedSlot(s.testUDevBadValue7Info, nil, nil)
   175  	s.testUDevBadInterface1Info = gadgetSnapInfo.Slots["test-udev-bad-interface-1"]
   176  	s.testSysfsNameBadValue1Info = gadgetSnapInfo.Slots["test-sysfs-name-bad-value-1"]
   177  	s.testSysfsNameBadValue1 = interfaces.NewConnectedSlot(s.testSysfsNameBadValue1Info, nil, nil)
   178  	s.testSysfsNameAndPathInfo = gadgetSnapInfo.Slots["test-sysfs-name-and-path"]
   179  	s.testSysfsNameAndPath = interfaces.NewConnectedSlot(s.testSysfsNameAndPathInfo, nil, nil)
   180  	s.testSysfsNameEmptyInfo = gadgetSnapInfo.Slots["test-sysfs-name-empty"]
   181  	s.testSysfsNameEmpty = interfaces.NewConnectedSlot(s.testSysfsNameEmptyInfo, nil, nil)
   182  
   183  	// Snap Consumers
   184  	consumingSnapInfo := snaptest.MockInfo(c, `
   185  name: client-snap
   186  version: 0
   187  plugs:
   188    plug-for-port-1:
   189      interface: i2c
   190  apps:
   191    app-accessing-1-port:
   192      command: foo
   193      plugs: [i2c]
   194  `, nil)
   195  	s.testPlugPort1Info = consumingSnapInfo.Plugs["plug-for-port-1"]
   196  	s.testPlugPort1 = interfaces.NewConnectedPlug(s.testPlugPort1Info, nil, nil)
   197  }
   198  
   199  func (s *I2cInterfaceSuite) TestName(c *C) {
   200  	c.Assert(s.iface.Name(), Equals, "i2c")
   201  }
   202  
   203  func (s *I2cInterfaceSuite) TestSanitizeCoreSnapSlot(c *C) {
   204  	c.Assert(interfaces.BeforePrepareSlot(s.iface, s.testSlot1Info), IsNil)
   205  	// Verify historically filepath.Clean()d paths are still valid
   206  	c.Assert(interfaces.BeforePrepareSlot(s.iface, s.testSlotCleanedInfo), IsNil)
   207  }
   208  
   209  func (s *I2cInterfaceSuite) TestSanitizeGadgetSnapSlot(c *C) {
   210  	c.Assert(interfaces.BeforePrepareSlot(s.iface, s.testUDev1Info), IsNil)
   211  	c.Assert(interfaces.BeforePrepareSlot(s.iface, s.testUDev2Info), IsNil)
   212  	c.Assert(interfaces.BeforePrepareSlot(s.iface, s.testUDev3Info), IsNil)
   213  	c.Assert(interfaces.BeforePrepareSlot(s.iface, s.testSysfsName1Info), IsNil)
   214  }
   215  
   216  func (s *I2cInterfaceSuite) TestSanitizeBadGadgetSnapSlot(c *C) {
   217  	c.Assert(interfaces.BeforePrepareSlot(s.iface, s.testUDevBadValue1Info), ErrorMatches, "i2c path attribute must be a valid device node")
   218  	c.Assert(interfaces.BeforePrepareSlot(s.iface, s.testUDevBadValue2Info), ErrorMatches, "i2c path attribute must be a valid device node")
   219  	c.Assert(interfaces.BeforePrepareSlot(s.iface, s.testUDevBadValue3Info), ErrorMatches, "i2c path attribute must be a valid device node")
   220  	c.Assert(interfaces.BeforePrepareSlot(s.iface, s.testUDevBadValue4Info), ErrorMatches, "i2c path attribute must be a valid device node")
   221  	c.Assert(interfaces.BeforePrepareSlot(s.iface, s.testUDevBadValue5Info), ErrorMatches, "i2c path attribute must be a valid device node")
   222  	c.Assert(interfaces.BeforePrepareSlot(s.iface, s.testUDevBadValue6Info), ErrorMatches, "i2c slot must have a path or sysfs-name attribute")
   223  	c.Assert(interfaces.BeforePrepareSlot(s.iface, s.testUDevBadValue7Info), ErrorMatches, "i2c slot must have a path or sysfs-name attribute")
   224  	c.Assert(interfaces.BeforePrepareSlot(s.iface, s.testSysfsNameBadValue1Info), ErrorMatches, "i2c sysfs-name attribute must be a valid sysfs-name")
   225  	c.Assert(interfaces.BeforePrepareSlot(s.iface, s.testSysfsNameAndPathInfo), ErrorMatches, "i2c slot can only use path or sysfs-name")
   226  	c.Assert(interfaces.BeforePrepareSlot(s.iface, s.testSysfsNameEmptyInfo), ErrorMatches, "i2c sysfs-name attribute must be a valid sysfs-name")
   227  }
   228  
   229  func (s *I2cInterfaceSuite) TestUDevSpec(c *C) {
   230  	spec := &udev.Specification{}
   231  	c.Assert(spec.AddConnectedPlug(s.iface, s.testPlugPort1, s.testUDev1), IsNil)
   232  	c.Assert(spec.Snippets(), HasLen, 2)
   233  	c.Assert(spec.Snippets(), testutil.Contains, `# i2c
   234  KERNEL=="i2c-1", TAG+="snap_client-snap_app-accessing-1-port"`)
   235  	c.Assert(spec.Snippets(), testutil.Contains, `TAG=="snap_client-snap_app-accessing-1-port", RUN+="/usr/lib/snapd/snap-device-helper $env{ACTION} snap_client-snap_app-accessing-1-port $devpath $major:$minor"`)
   236  }
   237  
   238  func (s *I2cInterfaceSuite) TestUDevSpecSysfsName(c *C) {
   239  	spec := &udev.Specification{}
   240  	c.Assert(spec.AddConnectedPlug(s.iface, s.testPlugPort1, s.testSysfsName1), IsNil)
   241  	c.Assert(spec.Snippets(), HasLen, 0)
   242  }
   243  
   244  func (s *I2cInterfaceSuite) TestAppArmorSpecPath(c *C) {
   245  	spec := &apparmor.Specification{}
   246  	c.Assert(spec.AddConnectedPlug(s.iface, s.testPlugPort1, s.testUDev1), IsNil)
   247  	c.Assert(spec.SecurityTags(), DeepEquals, []string{"snap.client-snap.app-accessing-1-port"})
   248  	c.Assert(spec.SnippetForTag("snap.client-snap.app-accessing-1-port"), testutil.Contains, `/dev/i2c-1 rw,`)
   249  	c.Assert(spec.SnippetForTag("snap.client-snap.app-accessing-1-port"), testutil.Contains, `/sys/devices/platform/{*,**.i2c}/i2c-1/** rw,  # Add any condensed parametric rules`)
   250  }
   251  
   252  func (s *I2cInterfaceSuite) TestAppArmorSpecPathMany(c *C) {
   253  	spec := &apparmor.Specification{}
   254  	c.Assert(spec.AddConnectedPlug(s.iface, s.testPlugPort1, s.testUDev1), IsNil)
   255  	c.Assert(spec.AddConnectedPlug(s.iface, s.testPlugPort1, s.testUDev2), IsNil)
   256  	// NOTE: the snap name is misleading.
   257  	c.Assert(spec.SecurityTags(), DeepEquals, []string{"snap.client-snap.app-accessing-1-port"})
   258  	c.Assert(spec.SnippetForTag("snap.client-snap.app-accessing-1-port"), testutil.Contains, `/dev/i2c-1 rw,`)
   259  	c.Assert(spec.SnippetForTag("snap.client-snap.app-accessing-1-port"), testutil.Contains, `/dev/i2c-2 rw,`)
   260  	c.Assert(spec.SnippetForTag("snap.client-snap.app-accessing-1-port"), testutil.Contains, `/sys/devices/platform/{*,**.i2c}/i2c-{1,2}/** rw,  # Add any condensed parametric rules`)
   261  }
   262  
   263  func (s *I2cInterfaceSuite) TestAppArmorSpecSysfsName(c *C) {
   264  	spec := &apparmor.Specification{}
   265  	c.Assert(spec.AddConnectedPlug(s.iface, s.testPlugPort1, s.testSysfsName1), IsNil)
   266  	c.Assert(spec.SecurityTags(), DeepEquals, []string{"snap.client-snap.app-accessing-1-port"})
   267  	c.Assert(spec.SnippetForTag("snap.client-snap.app-accessing-1-port"), Equals, `
   268  # Description: Can access I2C sysfs name
   269  
   270  /sys/bus/i2c/devices/1-0050/** rw,
   271  `)
   272  }
   273  
   274  func (s *I2cInterfaceSuite) TestAutoConnect(c *C) {
   275  	c.Check(s.iface.AutoConnect(nil, nil), Equals, true)
   276  }
   277  
   278  func (s *I2cInterfaceSuite) TestInterfaces(c *C) {
   279  	c.Check(builtin.Interfaces(), testutil.DeepContains, s.iface)
   280  }