github.com/hugh712/snapd@v0.0.0-20200910133618-1a99902bd583/interfaces/builtin/iio_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 IioInterfaceSuite struct {
    35  	testutil.BaseTest
    36  	iface interfaces.Interface
    37  
    38  	// OS Snap
    39  	testSlot1Info       *snap.SlotInfo
    40  	testSlot2Info       *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  	testUDevBadValue1         *interfaces.ConnectedSlot
    52  	testUDevBadValue1Info     *snap.SlotInfo
    53  	testUDevBadValue2         *interfaces.ConnectedSlot
    54  	testUDevBadValue2Info     *snap.SlotInfo
    55  	testUDevBadValue3         *interfaces.ConnectedSlot
    56  	testUDevBadValue3Info     *snap.SlotInfo
    57  	testUDevBadValue4         *interfaces.ConnectedSlot
    58  	testUDevBadValue4Info     *snap.SlotInfo
    59  	testUDevBadValue5         *interfaces.ConnectedSlot
    60  	testUDevBadValue5Info     *snap.SlotInfo
    61  	testUDevBadValue6         *interfaces.ConnectedSlot
    62  	testUDevBadValue6Info     *snap.SlotInfo
    63  	testUDevBadValue7         *interfaces.ConnectedSlot
    64  	testUDevBadValue7Info     *snap.SlotInfo
    65  	testUDevBadValue8         *interfaces.ConnectedSlot
    66  	testUDevBadValue8Info     *snap.SlotInfo
    67  	testUDevBadInterface1     *interfaces.ConnectedSlot
    68  	testUDevBadInterface1Info *snap.SlotInfo
    69  
    70  	// Consuming Snap
    71  	testPlugPort1     *interfaces.ConnectedPlug
    72  	testPlugPort1Info *snap.PlugInfo
    73  }
    74  
    75  var _ = Suite(&IioInterfaceSuite{
    76  	iface: builtin.MustInterface("iio"),
    77  })
    78  
    79  func (s *IioInterfaceSuite) SetUpTest(c *C) {
    80  	// Mock for OS Snap
    81  	osSnapInfo := snaptest.MockInfo(c, `
    82  name: ubuntu-core
    83  version: 0
    84  type: os
    85  slots:
    86    test-port-1:
    87      interface: iio
    88      path: /dev/iio:device0
    89    test-port-unclean:
    90      interface: iio
    91      path: ///dev/iio:device1
    92  `, nil)
    93  	s.testSlot1Info = osSnapInfo.Slots["test-port-1"]
    94  	s.testSlotCleanedInfo = osSnapInfo.Slots["test-port-unclean"]
    95  
    96  	// Mock for Gadget Snap
    97  	gadgetSnapInfo := snaptest.MockInfo(c, `
    98  name: some-device
    99  version: 0
   100  type: gadget
   101  slots:
   102    test-udev-1:
   103      interface: iio
   104      path: /dev/iio:device1
   105    test-udev-2:
   106      interface: iio
   107      path: /dev/iio:device2
   108    test-udev-3:
   109      interface: iio
   110      path: /dev/iio:device10000
   111    test-udev-bad-value-1:
   112      interface: iio
   113      path: /dev/iio
   114    test-udev-bad-value-2:
   115      interface: iio
   116      path: /dev/iio:devicea
   117    test-udev-bad-value-3:
   118      interface: iio
   119      path: /dev/iio:device2a
   120    test-udev-bad-value-4:
   121      interface: iio
   122      path: /dev/foo-0
   123    test-udev-bad-value-5:
   124      interface: iio
   125      path: /dev/iio:devicefoo
   126    test-udev-bad-value-6:
   127      interface: iio
   128      path: /dev/iio-device0
   129    test-udev-bad-value-7:
   130      interface: iio
   131      path: ""
   132    test-udev-bad-value-8:
   133      interface: iio
   134    test-udev-bad-interface-1:
   135      interface: other-interface
   136  `, nil)
   137  	s.testUDev1Info = gadgetSnapInfo.Slots["test-udev-1"]
   138  	s.testUDev1 = interfaces.NewConnectedSlot(s.testUDev1Info, nil, nil)
   139  	s.testUDev2Info = gadgetSnapInfo.Slots["test-udev-2"]
   140  	s.testUDev2 = interfaces.NewConnectedSlot(s.testUDev2Info, nil, nil)
   141  	s.testUDev3Info = gadgetSnapInfo.Slots["test-udev-3"]
   142  	s.testUDev3 = interfaces.NewConnectedSlot(s.testUDev3Info, nil, nil)
   143  	s.testUDevBadValue1Info = gadgetSnapInfo.Slots["test-udev-bad-value-1"]
   144  	s.testUDevBadValue1 = interfaces.NewConnectedSlot(s.testUDevBadValue1Info, nil, nil)
   145  	s.testUDevBadValue2Info = gadgetSnapInfo.Slots["test-udev-bad-value-2"]
   146  	s.testUDevBadValue2 = interfaces.NewConnectedSlot(s.testUDevBadValue2Info, nil, nil)
   147  	s.testUDevBadValue3Info = gadgetSnapInfo.Slots["test-udev-bad-value-3"]
   148  	s.testUDevBadValue3 = interfaces.NewConnectedSlot(s.testUDevBadValue3Info, nil, nil)
   149  	s.testUDevBadValue4Info = gadgetSnapInfo.Slots["test-udev-bad-value-4"]
   150  	s.testUDevBadValue4 = interfaces.NewConnectedSlot(s.testUDevBadValue4Info, nil, nil)
   151  	s.testUDevBadValue5Info = gadgetSnapInfo.Slots["test-udev-bad-value-5"]
   152  	s.testUDevBadValue5 = interfaces.NewConnectedSlot(s.testUDevBadValue5Info, nil, nil)
   153  	s.testUDevBadValue6Info = gadgetSnapInfo.Slots["test-udev-bad-value-6"]
   154  	s.testUDevBadValue6 = interfaces.NewConnectedSlot(s.testUDevBadValue6Info, nil, nil)
   155  	s.testUDevBadValue7Info = gadgetSnapInfo.Slots["test-udev-bad-value-7"]
   156  	s.testUDevBadValue7 = interfaces.NewConnectedSlot(s.testUDevBadValue7Info, nil, nil)
   157  	s.testUDevBadValue8Info = gadgetSnapInfo.Slots["test-udev-bad-value-8"]
   158  	s.testUDevBadValue8 = interfaces.NewConnectedSlot(s.testUDevBadValue8Info, nil, nil)
   159  	s.testUDevBadInterface1Info = gadgetSnapInfo.Slots["test-udev-bad-interface-1"]
   160  	s.testUDevBadInterface1 = interfaces.NewConnectedSlot(s.testUDevBadInterface1Info, nil, nil)
   161  
   162  	// Snap Consumers
   163  	consumingSnapInfo := snaptest.MockInfo(c, `
   164  name: client-snap
   165  version: 0
   166  plugs:
   167    plug-for-port-1:
   168      interface: iio
   169  apps:
   170    app-accessing-1-port:
   171      command: foo
   172      plugs: [iio]
   173  `, nil)
   174  	s.testPlugPort1Info = consumingSnapInfo.Plugs["plug-for-port-1"]
   175  	s.testPlugPort1 = interfaces.NewConnectedPlug(s.testPlugPort1Info, nil, nil)
   176  }
   177  
   178  func (s *IioInterfaceSuite) TestName(c *C) {
   179  	c.Assert(s.iface.Name(), Equals, "iio")
   180  }
   181  
   182  func (s *IioInterfaceSuite) TestSanitizeCoreSnapSlot(c *C) {
   183  	c.Assert(interfaces.BeforePrepareSlot(s.iface, s.testSlot1Info), IsNil)
   184  	// Verify historically filepath.Clean()d paths are still valid
   185  	c.Assert(interfaces.BeforePrepareSlot(s.iface, s.testSlotCleanedInfo), IsNil)
   186  }
   187  
   188  func (s *IioInterfaceSuite) TestSanitizeBadGadgetSnapSlot(c *C) {
   189  	c.Assert(interfaces.BeforePrepareSlot(s.iface, s.testUDevBadValue1Info), ErrorMatches, "iio path attribute must be a valid device node")
   190  	c.Assert(interfaces.BeforePrepareSlot(s.iface, s.testUDevBadValue2Info), ErrorMatches, "iio path attribute must be a valid device node")
   191  	c.Assert(interfaces.BeforePrepareSlot(s.iface, s.testUDevBadValue3Info), ErrorMatches, "iio path attribute must be a valid device node")
   192  	c.Assert(interfaces.BeforePrepareSlot(s.iface, s.testUDevBadValue4Info), ErrorMatches, "iio path attribute must be a valid device node")
   193  	c.Assert(interfaces.BeforePrepareSlot(s.iface, s.testUDevBadValue5Info), ErrorMatches, "iio path attribute must be a valid device node")
   194  	c.Assert(interfaces.BeforePrepareSlot(s.iface, s.testUDevBadValue6Info), ErrorMatches, "iio path attribute must be a valid device node")
   195  	c.Assert(interfaces.BeforePrepareSlot(s.iface, s.testUDevBadValue7Info), ErrorMatches, "iio slot must have a path attribute")
   196  	c.Assert(interfaces.BeforePrepareSlot(s.iface, s.testUDevBadValue8Info), ErrorMatches, "iio slot must have a path attribute")
   197  }
   198  
   199  func (s *IioInterfaceSuite) TestConnectedPlugUDevSnippets(c *C) {
   200  	spec := &udev.Specification{}
   201  	c.Assert(spec.AddConnectedPlug(s.iface, s.testPlugPort1, s.testUDev1), IsNil)
   202  	c.Assert(spec.Snippets(), HasLen, 2)
   203  	c.Assert(spec.Snippets(), testutil.Contains, `# iio
   204  KERNEL=="iio:device1", TAG+="snap_client-snap_app-accessing-1-port"`)
   205  	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"`)
   206  }
   207  
   208  func (s *IioInterfaceSuite) TestConnectedPlugAppArmorSingleSnippet(c *C) {
   209  	expectedSnippet := `
   210  # Description: Give access to a specific IIO device on the system.
   211  
   212  /dev/iio:device1 rw,
   213  /sys/bus/iio/devices/iio:device1/ r,
   214  /sys/bus/iio/devices/iio:device1/** rwk,
   215  
   216  /sys/devices/**/iio:device1/ r,  # Add any condensed parametric rules
   217  /sys/devices/**/iio:device1/** rwk,  # Add any condensed parametric rules`
   218  	apparmorSpec := &apparmor.Specification{}
   219  	err := apparmorSpec.AddConnectedPlug(s.iface, s.testPlugPort1, s.testUDev1)
   220  	c.Assert(err, IsNil)
   221  	c.Assert(apparmorSpec.SecurityTags(), DeepEquals, []string{"snap.client-snap.app-accessing-1-port"})
   222  	snippet := apparmorSpec.SnippetForTag("snap.client-snap.app-accessing-1-port")
   223  	c.Assert(snippet, Equals, expectedSnippet)
   224  }
   225  
   226  func (s *IioInterfaceSuite) TestConnectedPlugAppArmorSnippetsMultipleOptimized(c *C) {
   227  	expectedSnippet := `
   228  # Description: Give access to a specific IIO device on the system.
   229  
   230  /dev/iio:device1 rw,
   231  /sys/bus/iio/devices/iio:device1/ r,
   232  /sys/bus/iio/devices/iio:device1/** rwk,
   233  
   234  
   235  # Description: Give access to a specific IIO device on the system.
   236  
   237  /dev/iio:device2 rw,
   238  /sys/bus/iio/devices/iio:device2/ r,
   239  /sys/bus/iio/devices/iio:device2/** rwk,
   240  
   241  /sys/devices/**/iio:device{1,2}/ r,  # Add any condensed parametric rules
   242  /sys/devices/**/iio:device{1,2}/** rwk,  # Add any condensed parametric rules`
   243  	apparmorSpec := &apparmor.Specification{}
   244  	err := apparmorSpec.AddConnectedPlug(s.iface, s.testPlugPort1, s.testUDev1)
   245  	c.Assert(err, IsNil)
   246  	err = apparmorSpec.AddConnectedPlug(s.iface, s.testPlugPort1, s.testUDev2)
   247  	c.Assert(err, IsNil)
   248  	c.Assert(apparmorSpec.SecurityTags(), DeepEquals, []string{"snap.client-snap.app-accessing-1-port"})
   249  	// XXX: the tag snap.client-snap.app-accessing-1-port is
   250  	// misleading when you are testing for '1' and '2' ports
   251  	snippet := apparmorSpec.SnippetForTag("snap.client-snap.app-accessing-1-port")
   252  	c.Assert(snippet, Equals, expectedSnippet)
   253  }
   254  
   255  func (s *IioInterfaceSuite) TestAutoConnect(c *C) {
   256  	c.Check(s.iface.AutoConnect(nil, nil), Equals, true)
   257  }
   258  
   259  func (s *IioInterfaceSuite) TestInterfaces(c *C) {
   260  	c.Check(builtin.Interfaces(), testutil.DeepContains, s.iface)
   261  }