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

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2019 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 uioInterfaceSuite struct {
    35  	testutil.BaseTest
    36  	iface           interfaces.Interface
    37  	slotGadgetInfo0 *snap.SlotInfo
    38  	slotGadgetInfo1 *snap.SlotInfo
    39  	slotGadget0     *interfaces.ConnectedSlot
    40  	slotGadget1     *interfaces.ConnectedSlot
    41  	plugInfo        *snap.PlugInfo
    42  	plug            *interfaces.ConnectedPlug
    43  }
    44  
    45  var _ = Suite(&uioInterfaceSuite{
    46  	iface: builtin.MustInterface("uio"),
    47  })
    48  
    49  func (s *uioInterfaceSuite) SetUpTest(c *C) {
    50  	info := snaptest.MockInfo(c, `
    51  name: gadget
    52  version: 0
    53  type: gadget
    54  slots:
    55    uio-0:
    56      interface: uio
    57      path: /dev/uio0
    58    uio-1:
    59      interface: uio
    60      path: /dev/uio1
    61  `, nil)
    62  	s.slotGadgetInfo0 = info.Slots["uio-0"]
    63  	s.slotGadgetInfo1 = info.Slots["uio-1"]
    64  	s.slotGadget0 = interfaces.NewConnectedSlot(s.slotGadgetInfo0, nil, nil)
    65  	s.slotGadget1 = interfaces.NewConnectedSlot(s.slotGadgetInfo1, nil, nil)
    66  
    67  	info = snaptest.MockInfo(c, `
    68  name: consumer
    69  version: 0
    70  plugs:
    71    uio:
    72      interface: uio
    73  apps:
    74    app:
    75      command: foo
    76  `, nil)
    77  	s.plugInfo = info.Plugs["uio"]
    78  	s.plug = interfaces.NewConnectedPlug(s.plugInfo, nil, nil)
    79  }
    80  
    81  func (s *uioInterfaceSuite) TestName(c *C) {
    82  	c.Assert(s.iface.Name(), Equals, "uio")
    83  }
    84  
    85  func (s *uioInterfaceSuite) TestSanitizeSlot(c *C) {
    86  	c.Assert(interfaces.BeforePrepareSlot(s.iface, s.slotGadgetInfo0), IsNil)
    87  	brokenSlot := snaptest.MockInfo(c, `
    88  name: broken-gadget
    89  version: 1
    90  type: gadget
    91  slots:
    92    uio:
    93      path: /dev/foo
    94  `, nil).Slots["uio"]
    95  	c.Assert(interfaces.BeforePrepareSlot(s.iface, brokenSlot), ErrorMatches, `slot "broken-gadget:uio" path attribute must be a valid UIO device node`)
    96  }
    97  
    98  func (s *uioInterfaceSuite) TestUDevSpec(c *C) {
    99  	spec := &udev.Specification{}
   100  	c.Assert(spec.AddConnectedPlug(s.iface, s.plug, s.slotGadget0), IsNil)
   101  	c.Assert(spec.Snippets(), HasLen, 2)
   102  	c.Assert(spec.Snippets(), testutil.Contains, `# uio
   103  SUBSYSTEM=="uio", KERNEL=="uio0", TAG+="snap_consumer_app"`)
   104  	c.Assert(spec.Snippets(), testutil.Contains, `TAG=="snap_consumer_app", RUN+="/usr/lib/snapd/snap-device-helper $env{ACTION} snap_consumer_app $devpath $major:$minor"`)
   105  }
   106  
   107  func (s *uioInterfaceSuite) TestAppArmorSpec(c *C) {
   108  	spec := &apparmor.Specification{}
   109  	// Simulate two UIO connections.
   110  	c.Assert(spec.AddConnectedPlug(s.iface, s.plug, s.slotGadget0), IsNil)
   111  	c.Assert(spec.AddConnectedPlug(s.iface, s.plug, s.slotGadget1), IsNil)
   112  	c.Assert(spec.SecurityTags(), DeepEquals, []string{"snap.consumer.app"})
   113  	c.Assert(spec.SnippetForTag("snap.consumer.app"), Equals, ""+
   114  		"/dev/uio0 rw,\n"+
   115  		"/dev/uio1 rw,\n"+
   116  		"/sys/devices/platform/**/uio/uio[0-9]** r,  # common rule for all uio connections")
   117  }
   118  
   119  func (s *uioInterfaceSuite) TestStaticInfo(c *C) {
   120  	si := interfaces.StaticInfoOf(s.iface)
   121  	c.Assert(si.ImplicitOnCore, Equals, false)
   122  	c.Assert(si.ImplicitOnClassic, Equals, false)
   123  	c.Assert(si.Summary, Equals, "allows access to specific uio device")
   124  	c.Assert(si.BaseDeclarationSlots, testutil.Contains, "uio")
   125  }
   126  
   127  func (s *uioInterfaceSuite) TestAutoConnect(c *C) {
   128  	c.Check(s.iface.AutoConnect(nil, nil), Equals, true)
   129  }
   130  
   131  func (s *uioInterfaceSuite) TestInterfaces(c *C) {
   132  	c.Check(builtin.Interfaces(), testutil.DeepContains, s.iface)
   133  }