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