github.com/bugraaydogar/snapd@v0.0.0-20210315170335-8c70bb858939/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 . "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/systemd" 29 "github.com/snapcore/snapd/logger" 30 "github.com/snapcore/snapd/snap" 31 "github.com/snapcore/snapd/snap/snaptest" 32 "github.com/snapcore/snapd/testutil" 33 ) 34 35 type GpioInterfaceSuite struct { 36 iface interfaces.Interface 37 gadgetGpioSlotInfo *snap.SlotInfo 38 gadgetGpioSlot *interfaces.ConnectedSlot 39 gadgetMissingNumberSlotInfo *snap.SlotInfo 40 gadgetMissingNumberSlot *interfaces.ConnectedSlot 41 gadgetBadNumberSlotInfo *snap.SlotInfo 42 gadgetBadNumberSlot *interfaces.ConnectedSlot 43 gadgetBadInterfaceSlotInfo *snap.SlotInfo 44 gadgetBadInterfaceSlot *interfaces.ConnectedSlot 45 gadgetPlugInfo *snap.PlugInfo 46 gadgetPlug *interfaces.ConnectedPlug 47 gadgetBadInterfacePlugInfo *snap.PlugInfo 48 gadgetBadInterfacePlug *interfaces.ConnectedPlug 49 osGpioSlotInfo *snap.SlotInfo 50 osGpioSlot *interfaces.ConnectedSlot 51 } 52 53 var _ = Suite(&GpioInterfaceSuite{ 54 iface: builtin.MustInterface("gpio"), 55 }) 56 57 func (s *GpioInterfaceSuite) SetUpTest(c *C) { 58 gadgetInfo := snaptest.MockInfo(c, ` 59 name: my-device 60 version: 0 61 type: gadget 62 slots: 63 my-pin: 64 interface: gpio 65 number: 100 66 missing-number: 67 interface: gpio 68 bad-number: 69 interface: gpio 70 number: forty-two 71 bad-interface-slot: other-interface 72 plugs: 73 plug: gpio 74 bad-interface-plug: other-interface 75 `, nil) 76 s.gadgetGpioSlotInfo = gadgetInfo.Slots["my-pin"] 77 s.gadgetGpioSlot = interfaces.NewConnectedSlot(s.gadgetGpioSlotInfo, nil, nil) 78 s.gadgetMissingNumberSlotInfo = gadgetInfo.Slots["missing-number"] 79 s.gadgetMissingNumberSlot = interfaces.NewConnectedSlot(s.gadgetMissingNumberSlotInfo, nil, nil) 80 s.gadgetBadNumberSlotInfo = gadgetInfo.Slots["bad-number"] 81 s.gadgetBadNumberSlot = interfaces.NewConnectedSlot(s.gadgetBadNumberSlotInfo, nil, nil) 82 s.gadgetBadInterfaceSlotInfo = gadgetInfo.Slots["bad-interface-slot"] 83 s.gadgetBadInterfaceSlot = interfaces.NewConnectedSlot(s.gadgetBadInterfaceSlotInfo, nil, nil) 84 s.gadgetPlugInfo = gadgetInfo.Plugs["plug"] 85 s.gadgetPlug = interfaces.NewConnectedPlug(s.gadgetPlugInfo, nil, nil) 86 s.gadgetBadInterfacePlugInfo = gadgetInfo.Plugs["bad-interface-plug"] 87 s.gadgetBadInterfacePlug = interfaces.NewConnectedPlug(s.gadgetBadInterfacePlugInfo, nil, nil) 88 89 osInfo := snaptest.MockInfo(c, ` 90 name: my-core 91 version: 0 92 type: os 93 slots: 94 my-pin: 95 interface: gpio 96 number: 777 97 direction: out 98 `, nil) 99 s.osGpioSlotInfo = osInfo.Slots["my-pin"] 100 s.osGpioSlot = interfaces.NewConnectedSlot(s.osGpioSlotInfo, nil, nil) 101 } 102 103 func (s *GpioInterfaceSuite) TestName(c *C) { 104 c.Assert(s.iface.Name(), Equals, "gpio") 105 } 106 107 func (s *GpioInterfaceSuite) TestSanitizeSlotGadgetSnap(c *C) { 108 // gpio slot on gadget accepeted 109 c.Assert(interfaces.BeforePrepareSlot(s.iface, s.gadgetGpioSlotInfo), IsNil) 110 111 // slots without number attribute are rejected 112 c.Assert(interfaces.BeforePrepareSlot(s.iface, s.gadgetMissingNumberSlotInfo), ErrorMatches, 113 "gpio slot must have a number attribute") 114 115 // slots with number attribute that isnt a number 116 c.Assert(interfaces.BeforePrepareSlot(s.iface, s.gadgetBadNumberSlotInfo), ErrorMatches, 117 "gpio slot number attribute must be an int") 118 } 119 120 func (s *GpioInterfaceSuite) TestSanitizeSlotOsSnap(c *C) { 121 // gpio slot on OS accepeted 122 c.Assert(interfaces.BeforePrepareSlot(s.iface, s.osGpioSlotInfo), IsNil) 123 } 124 125 func (s *GpioInterfaceSuite) TestSanitizePlug(c *C) { 126 c.Assert(interfaces.BeforePreparePlug(s.iface, s.gadgetPlugInfo), IsNil) 127 } 128 129 func (s *GpioInterfaceSuite) TestSystemdConnectedSlot(c *C) { 130 spec := &systemd.Specification{} 131 err := spec.AddConnectedSlot(s.iface, s.gadgetPlug, s.gadgetGpioSlot) 132 c.Assert(err, IsNil) 133 c.Assert(spec.Services(), DeepEquals, map[string]*systemd.Service{ 134 "snap.my-device.interface.gpio-100.service": { 135 Type: "oneshot", 136 RemainAfterExit: true, 137 ExecStart: `/bin/sh -c 'test -e /sys/class/gpio/gpio100 || echo 100 > /sys/class/gpio/export'`, 138 ExecStop: `/bin/sh -c 'test ! -e /sys/class/gpio/gpio100 || echo 100 > /sys/class/gpio/unexport'`, 139 }, 140 }) 141 } 142 143 func (s *GpioInterfaceSuite) TestApparmorConnectedPlugIgnoresMissingSymlink(c *C) { 144 log, restore := logger.MockLogger() 145 defer restore() 146 147 spec := &apparmor.Specification{} 148 err := spec.AddConnectedPlug(s.iface, s.gadgetPlug, s.gadgetGpioSlot) 149 c.Assert(err, IsNil) 150 c.Assert(spec.Snippets(), HasLen, 0) 151 c.Assert(log.String(), testutil.Contains, "cannot export not existing gpio /sys/class/gpio/gpio100") 152 } 153 154 func (s *GpioInterfaceSuite) TestInterfaces(c *C) { 155 c.Check(builtin.Interfaces(), testutil.DeepContains, s.iface) 156 }