github.com/rigado/snapd@v2.42.5-go-mod+incompatible/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  	testSlot1     *interfaces.ConnectedSlot
    40  	testSlot1Info *snap.SlotInfo
    41  
    42  	// Gadget Snap
    43  	testUDev1                 *interfaces.ConnectedSlot
    44  	testUDev1Info             *snap.SlotInfo
    45  	testUDev2                 *interfaces.ConnectedSlot
    46  	testUDev2Info             *snap.SlotInfo
    47  	testUDev3                 *interfaces.ConnectedSlot
    48  	testUDev3Info             *snap.SlotInfo
    49  	testUDevBadValue1         *interfaces.ConnectedSlot
    50  	testUDevBadValue1Info     *snap.SlotInfo
    51  	testUDevBadValue2         *interfaces.ConnectedSlot
    52  	testUDevBadValue2Info     *snap.SlotInfo
    53  	testUDevBadValue3         *interfaces.ConnectedSlot
    54  	testUDevBadValue3Info     *snap.SlotInfo
    55  	testUDevBadValue4         *interfaces.ConnectedSlot
    56  	testUDevBadValue4Info     *snap.SlotInfo
    57  	testUDevBadValue5         *interfaces.ConnectedSlot
    58  	testUDevBadValue5Info     *snap.SlotInfo
    59  	testUDevBadValue6         *interfaces.ConnectedSlot
    60  	testUDevBadValue6Info     *snap.SlotInfo
    61  	testUDevBadValue7         *interfaces.ConnectedSlot
    62  	testUDevBadValue7Info     *snap.SlotInfo
    63  	testUDevBadValue8         *interfaces.ConnectedSlot
    64  	testUDevBadValue8Info     *snap.SlotInfo
    65  	testUDevBadInterface1     *interfaces.ConnectedSlot
    66  	testUDevBadInterface1Info *snap.SlotInfo
    67  
    68  	// Consuming Snap
    69  	testPlugPort1     *interfaces.ConnectedPlug
    70  	testPlugPort1Info *snap.PlugInfo
    71  }
    72  
    73  var _ = Suite(&IioInterfaceSuite{
    74  	iface: builtin.MustInterface("iio"),
    75  })
    76  
    77  func (s *IioInterfaceSuite) SetUpTest(c *C) {
    78  	// Mock for OS Snap
    79  	osSnapInfo := snaptest.MockInfo(c, `
    80  name: ubuntu-core
    81  version: 0
    82  type: os
    83  slots:
    84    test-port-1:
    85      interface: iio
    86      path: /dev/iio:device0
    87  `, nil)
    88  	s.testSlot1Info = osSnapInfo.Slots["test-port-1"]
    89  
    90  	// Mock for Gadget Snap
    91  	gadgetSnapInfo := snaptest.MockInfo(c, `
    92  name: some-device
    93  version: 0
    94  type: gadget
    95  slots:
    96    test-udev-1:
    97      interface: iio
    98      path: /dev/iio:device1
    99    test-udev-2:
   100      interface: iio
   101      path: /dev/iio:device2
   102    test-udev-3:
   103      interface: iio
   104      path: /dev/iio:device10000
   105    test-udev-bad-value-1:
   106      interface: iio
   107      path: /dev/iio
   108    test-udev-bad-value-2:
   109      interface: iio
   110      path: /dev/iio:devicea
   111    test-udev-bad-value-3:
   112      interface: iio
   113      path: /dev/iio:device2a
   114    test-udev-bad-value-4:
   115      interface: iio
   116      path: /dev/foo-0
   117    test-udev-bad-value-5:
   118      interface: iio
   119      path: /dev/iio:devicefoo
   120    test-udev-bad-value-6:
   121      interface: iio
   122      path: /dev/iio-device0
   123    test-udev-bad-value-7:
   124      interface: iio
   125      path: ""
   126    test-udev-bad-value-8:
   127      interface: iio
   128    test-udev-bad-interface-1:
   129      interface: other-interface
   130  `, nil)
   131  	s.testUDev1Info = gadgetSnapInfo.Slots["test-udev-1"]
   132  	s.testUDev1 = interfaces.NewConnectedSlot(s.testUDev1Info, nil, nil)
   133  	s.testUDev2Info = gadgetSnapInfo.Slots["test-udev-2"]
   134  	s.testUDev2 = interfaces.NewConnectedSlot(s.testUDev2Info, nil, nil)
   135  	s.testUDev3Info = gadgetSnapInfo.Slots["test-udev-3"]
   136  	s.testUDev3 = interfaces.NewConnectedSlot(s.testUDev3Info, nil, nil)
   137  	s.testUDevBadValue1Info = gadgetSnapInfo.Slots["test-udev-bad-value-1"]
   138  	s.testUDevBadValue1 = interfaces.NewConnectedSlot(s.testUDevBadValue1Info, nil, nil)
   139  	s.testUDevBadValue2Info = gadgetSnapInfo.Slots["test-udev-bad-value-2"]
   140  	s.testUDevBadValue2 = interfaces.NewConnectedSlot(s.testUDevBadValue2Info, nil, nil)
   141  	s.testUDevBadValue3Info = gadgetSnapInfo.Slots["test-udev-bad-value-3"]
   142  	s.testUDevBadValue3 = interfaces.NewConnectedSlot(s.testUDevBadValue3Info, nil, nil)
   143  	s.testUDevBadValue4Info = gadgetSnapInfo.Slots["test-udev-bad-value-4"]
   144  	s.testUDevBadValue4 = interfaces.NewConnectedSlot(s.testUDevBadValue4Info, nil, nil)
   145  	s.testUDevBadValue5Info = gadgetSnapInfo.Slots["test-udev-bad-value-5"]
   146  	s.testUDevBadValue5 = interfaces.NewConnectedSlot(s.testUDevBadValue5Info, nil, nil)
   147  	s.testUDevBadValue6Info = gadgetSnapInfo.Slots["test-udev-bad-value-6"]
   148  	s.testUDevBadValue6 = interfaces.NewConnectedSlot(s.testUDevBadValue6Info, nil, nil)
   149  	s.testUDevBadValue7Info = gadgetSnapInfo.Slots["test-udev-bad-value-7"]
   150  	s.testUDevBadValue7 = interfaces.NewConnectedSlot(s.testUDevBadValue7Info, nil, nil)
   151  	s.testUDevBadValue8Info = gadgetSnapInfo.Slots["test-udev-bad-value-8"]
   152  	s.testUDevBadValue8 = interfaces.NewConnectedSlot(s.testUDevBadValue8Info, nil, nil)
   153  	s.testUDevBadInterface1Info = gadgetSnapInfo.Slots["test-udev-bad-interface-1"]
   154  	s.testUDevBadInterface1 = interfaces.NewConnectedSlot(s.testUDevBadInterface1Info, nil, nil)
   155  
   156  	// Snap Consumers
   157  	consumingSnapInfo := snaptest.MockInfo(c, `
   158  name: client-snap
   159  version: 0
   160  plugs:
   161    plug-for-port-1:
   162      interface: iio
   163  apps:
   164    app-accessing-1-port:
   165      command: foo
   166      plugs: [iio]
   167  `, nil)
   168  	s.testPlugPort1Info = consumingSnapInfo.Plugs["plug-for-port-1"]
   169  	s.testPlugPort1 = interfaces.NewConnectedPlug(s.testPlugPort1Info, nil, nil)
   170  }
   171  
   172  func (s *IioInterfaceSuite) TestName(c *C) {
   173  	c.Assert(s.iface.Name(), Equals, "iio")
   174  }
   175  
   176  func (s *IioInterfaceSuite) TestSanitizeBadGadgetSnapSlot(c *C) {
   177  	c.Assert(interfaces.BeforePrepareSlot(s.iface, s.testUDevBadValue1Info), ErrorMatches, "iio path attribute must be a valid device node")
   178  	c.Assert(interfaces.BeforePrepareSlot(s.iface, s.testUDevBadValue2Info), ErrorMatches, "iio path attribute must be a valid device node")
   179  	c.Assert(interfaces.BeforePrepareSlot(s.iface, s.testUDevBadValue3Info), ErrorMatches, "iio path attribute must be a valid device node")
   180  	c.Assert(interfaces.BeforePrepareSlot(s.iface, s.testUDevBadValue4Info), ErrorMatches, "iio path attribute must be a valid device node")
   181  	c.Assert(interfaces.BeforePrepareSlot(s.iface, s.testUDevBadValue5Info), ErrorMatches, "iio path attribute must be a valid device node")
   182  	c.Assert(interfaces.BeforePrepareSlot(s.iface, s.testUDevBadValue6Info), ErrorMatches, "iio path attribute must be a valid device node")
   183  	c.Assert(interfaces.BeforePrepareSlot(s.iface, s.testUDevBadValue7Info), ErrorMatches, "iio slot must have a path attribute")
   184  	c.Assert(interfaces.BeforePrepareSlot(s.iface, s.testUDevBadValue8Info), ErrorMatches, "iio slot must have a path attribute")
   185  }
   186  
   187  func (s *IioInterfaceSuite) TestConnectedPlugUDevSnippets(c *C) {
   188  	spec := &udev.Specification{}
   189  	c.Assert(spec.AddConnectedPlug(s.iface, s.testPlugPort1, s.testUDev1), IsNil)
   190  	c.Assert(spec.Snippets(), HasLen, 2)
   191  	c.Assert(spec.Snippets(), testutil.Contains, `# iio
   192  KERNEL=="iio:device1", TAG+="snap_client-snap_app-accessing-1-port"`)
   193  	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"`)
   194  }
   195  
   196  func (s *IioInterfaceSuite) TestConnectedPlugAppArmorSnippets(c *C) {
   197  	expectedSnippet1 := `
   198  # Description: Give access to a specific IIO device on the system.
   199  
   200  /dev/iio:device1 rw,
   201  /sys/bus/iio/devices/iio:device1/ r,
   202  /sys/bus/iio/devices/iio:device1/** rwk,
   203  /sys/devices/**/iio:device1/ r,
   204  /sys/devices/**/iio:device1/** rwk,
   205  `
   206  	apparmorSpec := &apparmor.Specification{}
   207  	err := apparmorSpec.AddConnectedPlug(s.iface, s.testPlugPort1, s.testUDev1)
   208  	c.Assert(err, IsNil)
   209  	c.Assert(apparmorSpec.SecurityTags(), DeepEquals, []string{"snap.client-snap.app-accessing-1-port"})
   210  	snippet := apparmorSpec.SnippetForTag("snap.client-snap.app-accessing-1-port")
   211  	c.Assert(snippet, Equals, expectedSnippet1)
   212  }
   213  
   214  func (s *IioInterfaceSuite) TestAutoConnect(c *C) {
   215  	c.Check(s.iface.AutoConnect(nil, nil), Equals, true)
   216  }
   217  
   218  func (s *IioInterfaceSuite) TestInterfaces(c *C) {
   219  	c.Check(builtin.Interfaces(), testutil.DeepContains, s.iface)
   220  }