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