gitee.com/mysnapcore/mysnapd@v0.1.0/interfaces/builtin/gpio_test.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2016-2017 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  	"os"
    24  
    25  	. "gopkg.in/check.v1"
    26  
    27  	"gitee.com/mysnapcore/mysnapd/interfaces"
    28  	"gitee.com/mysnapcore/mysnapd/interfaces/apparmor"
    29  	"gitee.com/mysnapcore/mysnapd/interfaces/builtin"
    30  	"gitee.com/mysnapcore/mysnapd/interfaces/systemd"
    31  	"gitee.com/mysnapcore/mysnapd/logger"
    32  	"gitee.com/mysnapcore/mysnapd/snap"
    33  	"gitee.com/mysnapcore/mysnapd/snap/snaptest"
    34  	"gitee.com/mysnapcore/mysnapd/testutil"
    35  )
    36  
    37  type GpioInterfaceSuite struct {
    38  	testutil.BaseTest
    39  
    40  	iface                       interfaces.Interface
    41  	gadgetGpioSlotInfo          *snap.SlotInfo
    42  	gadgetGpioSlot              *interfaces.ConnectedSlot
    43  	gadgetMissingNumberSlotInfo *snap.SlotInfo
    44  	gadgetMissingNumberSlot     *interfaces.ConnectedSlot
    45  	gadgetBadNumberSlotInfo     *snap.SlotInfo
    46  	gadgetBadNumberSlot         *interfaces.ConnectedSlot
    47  	gadgetBadInterfaceSlotInfo  *snap.SlotInfo
    48  	gadgetBadInterfaceSlot      *interfaces.ConnectedSlot
    49  	gadgetPlugInfo              *snap.PlugInfo
    50  	gadgetPlug                  *interfaces.ConnectedPlug
    51  	gadgetBadInterfacePlugInfo  *snap.PlugInfo
    52  	gadgetBadInterfacePlug      *interfaces.ConnectedPlug
    53  	osGpioSlotInfo              *snap.SlotInfo
    54  	osGpioSlot                  *interfaces.ConnectedSlot
    55  }
    56  
    57  var _ = Suite(&GpioInterfaceSuite{
    58  	iface: builtin.MustInterface("gpio"),
    59  })
    60  
    61  func (s *GpioInterfaceSuite) SetUpTest(c *C) {
    62  	gadgetInfo := snaptest.MockInfo(c, `
    63  name: my-device
    64  version: 0
    65  type: gadget
    66  slots:
    67      my-pin:
    68          interface: gpio
    69          number: 100
    70      missing-number:
    71          interface: gpio
    72      bad-number:
    73          interface: gpio
    74          number: forty-two
    75      bad-interface-slot: other-interface
    76  plugs:
    77      plug: gpio
    78      bad-interface-plug: other-interface
    79  apps:
    80      svc:
    81          command: bin/foo.sh
    82  `, nil)
    83  	s.gadgetGpioSlotInfo = gadgetInfo.Slots["my-pin"]
    84  	s.gadgetGpioSlot = interfaces.NewConnectedSlot(s.gadgetGpioSlotInfo, nil, nil)
    85  	s.gadgetMissingNumberSlotInfo = gadgetInfo.Slots["missing-number"]
    86  	s.gadgetMissingNumberSlot = interfaces.NewConnectedSlot(s.gadgetMissingNumberSlotInfo, nil, nil)
    87  	s.gadgetBadNumberSlotInfo = gadgetInfo.Slots["bad-number"]
    88  	s.gadgetBadNumberSlot = interfaces.NewConnectedSlot(s.gadgetBadNumberSlotInfo, nil, nil)
    89  	s.gadgetBadInterfaceSlotInfo = gadgetInfo.Slots["bad-interface-slot"]
    90  	s.gadgetBadInterfaceSlot = interfaces.NewConnectedSlot(s.gadgetBadInterfaceSlotInfo, nil, nil)
    91  	s.gadgetPlugInfo = gadgetInfo.Plugs["plug"]
    92  	s.gadgetPlug = interfaces.NewConnectedPlug(s.gadgetPlugInfo, nil, nil)
    93  	s.gadgetBadInterfacePlugInfo = gadgetInfo.Plugs["bad-interface-plug"]
    94  	s.gadgetBadInterfacePlug = interfaces.NewConnectedPlug(s.gadgetBadInterfacePlugInfo, nil, nil)
    95  
    96  	osInfo := snaptest.MockInfo(c, `
    97  name: my-core
    98  version: 0
    99  type: os
   100  slots:
   101      my-pin:
   102          interface: gpio
   103          number: 777
   104          direction: out
   105  `, nil)
   106  	s.osGpioSlotInfo = osInfo.Slots["my-pin"]
   107  	s.osGpioSlot = interfaces.NewConnectedSlot(s.osGpioSlotInfo, nil, nil)
   108  }
   109  
   110  func (s *GpioInterfaceSuite) TestName(c *C) {
   111  	c.Assert(s.iface.Name(), Equals, "gpio")
   112  }
   113  
   114  func (s *GpioInterfaceSuite) TestSanitizeSlotGadgetSnap(c *C) {
   115  	// gpio slot on gadget accepeted
   116  	c.Assert(interfaces.BeforePrepareSlot(s.iface, s.gadgetGpioSlotInfo), IsNil)
   117  
   118  	// slots without number attribute are rejected
   119  	c.Assert(interfaces.BeforePrepareSlot(s.iface, s.gadgetMissingNumberSlotInfo), ErrorMatches,
   120  		"gpio slot must have a number attribute")
   121  
   122  	// slots with number attribute that isnt a number
   123  	c.Assert(interfaces.BeforePrepareSlot(s.iface, s.gadgetBadNumberSlotInfo), ErrorMatches,
   124  		"gpio slot number attribute must be an int")
   125  }
   126  
   127  func (s *GpioInterfaceSuite) TestSanitizeSlotOsSnap(c *C) {
   128  	// gpio slot on OS accepeted
   129  	c.Assert(interfaces.BeforePrepareSlot(s.iface, s.osGpioSlotInfo), IsNil)
   130  }
   131  
   132  func (s *GpioInterfaceSuite) TestSanitizePlug(c *C) {
   133  	c.Assert(interfaces.BeforePreparePlug(s.iface, s.gadgetPlugInfo), IsNil)
   134  }
   135  
   136  func (s *GpioInterfaceSuite) TestSystemdConnectedSlot(c *C) {
   137  	spec := &systemd.Specification{}
   138  	err := spec.AddConnectedSlot(s.iface, s.gadgetPlug, s.gadgetGpioSlot)
   139  	c.Assert(err, IsNil)
   140  	c.Assert(spec.Services(), DeepEquals, map[string]*systemd.Service{
   141  		"gpio-100": {
   142  			Type:            "oneshot",
   143  			RemainAfterExit: true,
   144  			ExecStart:       `/bin/sh -c 'test -e /sys/class/gpio/gpio100 || echo 100 > /sys/class/gpio/export'`,
   145  			ExecStop:        `/bin/sh -c 'test ! -e /sys/class/gpio/gpio100 || echo 100 > /sys/class/gpio/unexport'`,
   146  		},
   147  	})
   148  }
   149  
   150  func (s *GpioInterfaceSuite) TestApparmorConnectedPlugIgnoresMissingSymlink(c *C) {
   151  	log, restore := logger.MockLogger()
   152  	defer restore()
   153  
   154  	builtin.MockEvalSymlinks(&s.BaseTest, func(path string) (string, error) {
   155  		c.Assert(path, Equals, "/sys/class/gpio/gpio100")
   156  		return "", os.ErrNotExist
   157  	})
   158  
   159  	spec := &apparmor.Specification{}
   160  	err := spec.AddConnectedPlug(s.iface, s.gadgetPlug, s.gadgetGpioSlot)
   161  	c.Assert(err, IsNil)
   162  	c.Assert(spec.Snippets(), HasLen, 0)
   163  	c.Assert(log.String(), testutil.Contains, "cannot export not existing gpio /sys/class/gpio/gpio100")
   164  }
   165  
   166  func (s *GpioInterfaceSuite) TestApparmorConnectedPlug(c *C) {
   167  	builtin.MockEvalSymlinks(&s.BaseTest, func(path string) (string, error) {
   168  		c.Assert(path, Equals, "/sys/class/gpio/gpio100")
   169  		// TODO: what is this actually a symlink to on a real device?
   170  		return "/sys/dev/foo/class/gpio/gpio100", nil
   171  	})
   172  
   173  	spec := &apparmor.Specification{}
   174  	err := spec.AddConnectedPlug(s.iface, s.gadgetPlug, s.gadgetGpioSlot)
   175  	c.Assert(err, IsNil)
   176  	c.Assert(spec.SnippetForTag("snap.my-device.svc"), testutil.Contains, `/sys/dev/foo/class/gpio/gpio100/* rwk`)
   177  }
   178  
   179  func (s *GpioInterfaceSuite) TestInterfaces(c *C) {
   180  	c.Check(builtin.Interfaces(), testutil.DeepContains, s.iface)
   181  }