github.com/bugraaydogar/snapd@v0.0.0-20210315170335-8c70bb858939/interfaces/builtin/spi_test.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 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 "fmt" 24 25 . "gopkg.in/check.v1" 26 27 "github.com/snapcore/snapd/dirs" 28 "github.com/snapcore/snapd/interfaces" 29 "github.com/snapcore/snapd/interfaces/apparmor" 30 "github.com/snapcore/snapd/interfaces/builtin" 31 "github.com/snapcore/snapd/interfaces/udev" 32 "github.com/snapcore/snapd/snap" 33 "github.com/snapcore/snapd/snap/snaptest" 34 "github.com/snapcore/snapd/testutil" 35 ) 36 37 type spiInterfaceSuite struct { 38 testutil.BaseTest 39 iface interfaces.Interface 40 41 slotOs1Info *snap.SlotInfo 42 slotOs1 *interfaces.ConnectedSlot 43 slotOs2Info *snap.SlotInfo 44 slotOs2 *interfaces.ConnectedSlot 45 slotOs3Info *snap.SlotInfo 46 slotOs3 *interfaces.ConnectedSlot 47 slotOsCleanedInfo *snap.SlotInfo 48 slotOsCleaned *interfaces.ConnectedSlot 49 50 slotGadget1Info *snap.SlotInfo 51 slotGadget1 *interfaces.ConnectedSlot 52 slotGadget2Info *snap.SlotInfo 53 slotGadget2 *interfaces.ConnectedSlot 54 slotGadget3Info *snap.SlotInfo 55 slotGadget3 *interfaces.ConnectedSlot 56 slotGadgetBad1Info *snap.SlotInfo 57 slotGadgetBad1 *interfaces.ConnectedSlot 58 slotGadgetBad2Info *snap.SlotInfo 59 slotGadgetBad2 *interfaces.ConnectedSlot 60 slotGadgetBad3Info *snap.SlotInfo 61 slotGadgetBad3 *interfaces.ConnectedSlot 62 slotGadgetBad4Info *snap.SlotInfo 63 slotGadgetBad4 *interfaces.ConnectedSlot 64 slotGadgetBad5Info *snap.SlotInfo 65 slotGadgetBad5 *interfaces.ConnectedSlot 66 slotGadgetBad6Info *snap.SlotInfo 67 slotGadgetBad6 *interfaces.ConnectedSlot 68 69 plug1Info *snap.PlugInfo 70 plug1 *interfaces.ConnectedPlug 71 plug2Info *snap.PlugInfo 72 plug2 *interfaces.ConnectedPlug 73 plug3Info *snap.PlugInfo 74 plug3 *interfaces.ConnectedPlug 75 } 76 77 var _ = Suite(&spiInterfaceSuite{ 78 iface: builtin.MustInterface("spi"), 79 }) 80 81 func (s *spiInterfaceSuite) SetUpTest(c *C) { 82 info := snaptest.MockInfo(c, ` 83 name: core 84 version: 0 85 type: os 86 slots: 87 spi-1: 88 interface: spi 89 path: /dev/spidev0.0 90 spi-2: 91 interface: spi 92 path: /dev/spidev0.1 93 spi-3: 94 interface: spi 95 path: /dev/spidev33566.0 96 spi-unclean: 97 interface: spi 98 path: /dev/./spidev33567.0 99 `, nil) 100 s.slotOs1Info = info.Slots["spi-1"] 101 s.slotOs1 = interfaces.NewConnectedSlot(s.slotOs1Info, nil, nil) 102 s.slotOs2Info = info.Slots["spi-2"] 103 s.slotOs2 = interfaces.NewConnectedSlot(s.slotOs2Info, nil, nil) 104 s.slotOs3Info = info.Slots["spi-3"] 105 s.slotOs3 = interfaces.NewConnectedSlot(s.slotOs3Info, nil, nil) 106 s.slotOsCleanedInfo = info.Slots["spi-unclean"] 107 s.slotOsCleaned = interfaces.NewConnectedSlot(s.slotOsCleanedInfo, nil, nil) 108 109 info = snaptest.MockInfo(c, ` 110 name: gadget 111 version: 0 112 type: gadget 113 slots: 114 spi-1: 115 interface: spi 116 path: /dev/spidev0.0 117 spi-2: 118 interface: spi 119 path: /dev/spidev0.1 120 spi-3: 121 interface: spi 122 path: /dev/spidev33566.0 123 bad-spi-1: 124 interface: spi 125 path: /dev/spev0.0 126 bad-spi-2: 127 interface: spi 128 path: /dev/sidv0.0 129 bad-spi-3: 130 interface: spi 131 path: /dev/slpiv0.3 132 bad-spi-4: 133 interface: spi 134 path: /dev/sdev-00 135 bad-spi-5: 136 interface: spi 137 path: /dev/spi-foo 138 bad-spi-6: 139 interface: spi 140 `, nil) 141 s.slotGadget1Info = info.Slots["spi-1"] 142 s.slotGadget1 = interfaces.NewConnectedSlot(s.slotGadget1Info, nil, nil) 143 s.slotGadget2Info = info.Slots["spi-2"] 144 s.slotGadget2 = interfaces.NewConnectedSlot(s.slotGadget2Info, nil, nil) 145 s.slotGadget3Info = info.Slots["spi-3"] 146 s.slotGadget3 = interfaces.NewConnectedSlot(s.slotGadget3Info, nil, nil) 147 s.slotGadgetBad1Info = info.Slots["bad-spi-1"] 148 s.slotGadgetBad1 = interfaces.NewConnectedSlot(s.slotGadgetBad1Info, nil, nil) 149 s.slotGadgetBad2Info = info.Slots["bad-spi-2"] 150 s.slotGadgetBad2 = interfaces.NewConnectedSlot(s.slotGadgetBad2Info, nil, nil) 151 s.slotGadgetBad3Info = info.Slots["bad-spi-3"] 152 s.slotGadgetBad3 = interfaces.NewConnectedSlot(s.slotGadgetBad3Info, nil, nil) 153 s.slotGadgetBad4Info = info.Slots["bad-spi-4"] 154 s.slotGadgetBad4 = interfaces.NewConnectedSlot(s.slotGadgetBad4Info, nil, nil) 155 s.slotGadgetBad5Info = info.Slots["bad-spi-5"] 156 s.slotGadgetBad5 = interfaces.NewConnectedSlot(s.slotGadgetBad5Info, nil, nil) 157 s.slotGadgetBad6Info = info.Slots["bad-spi-6"] 158 s.slotGadgetBad6 = interfaces.NewConnectedSlot(s.slotGadgetBad6Info, nil, nil) 159 160 info = snaptest.MockInfo(c, ` 161 name: consumer 162 version: 0 163 plugs: 164 spi-1: 165 interface: spi 166 path: /dev/spidev.0.0 167 spi-2: 168 interface: spi 169 path: /dev/spidev0.1 170 spi-3: 171 interface: spi 172 path: /dev/spidev33566.0 173 apps: 174 app: 175 command: foo 176 plugs: [spi-1] 177 `, nil) 178 s.plug1Info = info.Plugs["spi-1"] 179 s.plug1 = interfaces.NewConnectedPlug(s.plug1Info, nil, nil) 180 s.plug2Info = info.Plugs["spi-2"] 181 s.plug2 = interfaces.NewConnectedPlug(s.plug2Info, nil, nil) 182 s.plug3Info = info.Plugs["spi-3"] 183 s.plug3 = interfaces.NewConnectedPlug(s.plug3Info, nil, nil) 184 } 185 186 func (s *spiInterfaceSuite) TestName(c *C) { 187 c.Assert(s.iface.Name(), Equals, "spi") 188 } 189 190 func (s *spiInterfaceSuite) TestSanitizeSlot(c *C) { 191 c.Assert(interfaces.BeforePrepareSlot(s.iface, s.slotOs1Info), IsNil) 192 c.Assert(interfaces.BeforePrepareSlot(s.iface, s.slotOs2Info), IsNil) 193 c.Assert(interfaces.BeforePrepareSlot(s.iface, s.slotOs3Info), IsNil) 194 // Verify historically filepath.Clean()d paths are still valid 195 c.Assert(interfaces.BeforePrepareSlot(s.iface, s.slotOsCleanedInfo), IsNil) 196 c.Assert(interfaces.BeforePrepareSlot(s.iface, s.slotGadget1Info), IsNil) 197 c.Assert(interfaces.BeforePrepareSlot(s.iface, s.slotGadget2Info), IsNil) 198 c.Assert(interfaces.BeforePrepareSlot(s.iface, s.slotGadget3Info), IsNil) 199 err := interfaces.BeforePrepareSlot(s.iface, s.slotGadgetBad1Info) 200 c.Assert(err, ErrorMatches, `"/dev/spev0.0" is not a valid SPI device`) 201 err = interfaces.BeforePrepareSlot(s.iface, s.slotGadgetBad2Info) 202 c.Assert(err, ErrorMatches, `"/dev/sidv0.0" is not a valid SPI device`) 203 err = interfaces.BeforePrepareSlot(s.iface, s.slotGadgetBad3Info) 204 c.Assert(err, ErrorMatches, `"/dev/slpiv0.3" is not a valid SPI device`) 205 err = interfaces.BeforePrepareSlot(s.iface, s.slotGadgetBad4Info) 206 c.Assert(err, ErrorMatches, `"/dev/sdev-00" is not a valid SPI device`) 207 err = interfaces.BeforePrepareSlot(s.iface, s.slotGadgetBad5Info) 208 c.Assert(err, ErrorMatches, `"/dev/spi-foo" is not a valid SPI device`) 209 err = interfaces.BeforePrepareSlot(s.iface, s.slotGadgetBad6Info) 210 c.Assert(err, ErrorMatches, `slot "gadget:bad-spi-6" must have a path attribute`) 211 } 212 213 func (s *spiInterfaceSuite) TestUDevSpec(c *C) { 214 spec := &udev.Specification{} 215 c.Assert(spec.AddConnectedPlug(s.iface, s.plug1, s.slotGadget1), IsNil) 216 c.Assert(spec.Snippets(), HasLen, 2) 217 c.Assert(spec.Snippets(), testutil.Contains, `# spi 218 KERNEL=="spidev0.0", TAG+="snap_consumer_app"`) 219 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)) 220 } 221 222 func (s *spiInterfaceSuite) TestAppArmorSpec(c *C) { 223 spec := &apparmor.Specification{} 224 c.Assert(spec.AddConnectedPlug(s.iface, s.plug1, s.slotGadget1), IsNil) 225 c.Assert(spec.SecurityTags(), DeepEquals, []string{"snap.consumer.app"}) 226 c.Assert(spec.SnippetForTag("snap.consumer.app"), Equals, ""+ 227 "/dev/spidev0.0 rw,\n"+ 228 "/sys/devices/platform/**/**.spi/**/spidev0.0/** rw, # Add any condensed parametric rules") 229 c.Assert(spec.AddConnectedPlug(s.iface, s.plug1, s.slotGadget2), IsNil) 230 c.Assert(spec.SnippetForTag("snap.consumer.app"), Equals, ""+ 231 "/dev/spidev0.0 rw,\n"+ 232 "/dev/spidev0.1 rw,\n"+ 233 "/sys/devices/platform/**/**.spi/**/spidev{0.0,0.1}/** rw, # Add any condensed parametric rules") 234 } 235 236 func (s *spiInterfaceSuite) TestStaticInfo(c *C) { 237 si := interfaces.StaticInfoOf(s.iface) 238 c.Assert(si.ImplicitOnCore, Equals, false) 239 c.Assert(si.ImplicitOnClassic, Equals, false) 240 c.Assert(si.Summary, Equals, "allows access to specific spi controller") 241 c.Assert(si.BaseDeclarationSlots, testutil.Contains, "spi") 242 } 243 244 func (s *spiInterfaceSuite) TestAutoConnect(c *C) { 245 c.Check(s.iface.AutoConnect(nil, nil), Equals, true) 246 } 247 248 func (s *spiInterfaceSuite) TestInterfaces(c *C) { 249 c.Check(builtin.Interfaces(), testutil.DeepContains, s.iface) 250 }