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