1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497
|
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2016 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package builtin_test
import (
"fmt"
"reflect"
"strings"
. "gopkg.in/check.v1"
"github.com/snapcore/snapd/interfaces"
"github.com/snapcore/snapd/interfaces/apparmor"
"github.com/snapcore/snapd/interfaces/builtin"
"github.com/snapcore/snapd/interfaces/dbus"
"github.com/snapcore/snapd/interfaces/ifacetest"
"github.com/snapcore/snapd/interfaces/kmod"
"github.com/snapcore/snapd/interfaces/mount"
"github.com/snapcore/snapd/interfaces/polkit"
"github.com/snapcore/snapd/interfaces/seccomp"
"github.com/snapcore/snapd/interfaces/systemd"
"github.com/snapcore/snapd/interfaces/udev"
"github.com/snapcore/snapd/snap"
"github.com/snapcore/snapd/snap/snaptest"
"github.com/snapcore/snapd/testutil"
)
type AllSuite struct{}
var _ = Suite(&AllSuite{})
// This section contains a list of *valid* defines that represent methods that
// backends recognize and call. They are in individual interfaces as each snapd
// interface can define a subset that it is interested in providing. Those are,
// essentially, the only valid methods that a snapd interface can have, apart
// from what is defined in the Interface golang interface.
type apparmorDefiner1 interface {
AppArmorConnectedPlug(spec *apparmor.Specification, plug *interfaces.ConnectedPlug, slot *interfaces.ConnectedSlot) error
}
type apparmorDefiner2 interface {
AppArmorConnectedSlot(spec *apparmor.Specification, plug *interfaces.ConnectedPlug, slot *interfaces.ConnectedSlot) error
}
type apparmorDefiner3 interface {
AppArmorPermanentPlug(spec *apparmor.Specification, plug *snap.PlugInfo) error
}
type apparmorDefiner4 interface {
AppArmorPermanentSlot(spec *apparmor.Specification, slot *snap.SlotInfo) error
}
type dbusDefiner1 interface {
DBusConnectedPlug(spec *dbus.Specification, plug *interfaces.ConnectedPlug, slot *interfaces.ConnectedSlot) error
}
type dbusDefiner2 interface {
DBusConnectedSlot(spec *dbus.Specification, plug *interfaces.ConnectedPlug, slot *interfaces.ConnectedSlot) error
}
type dbusDefiner3 interface {
DBusPermanentPlug(spec *dbus.Specification, plug *snap.PlugInfo) error
}
type dbusDefiner4 interface {
DBusPermanentSlot(spec *dbus.Specification, slot *snap.SlotInfo) error
}
type kmodDefiner1 interface {
KModConnectedPlug(spec *kmod.Specification, plug *interfaces.ConnectedPlug, slot *interfaces.ConnectedSlot) error
}
type kmodDefiner2 interface {
KModConnectedSlot(spec *kmod.Specification, plug *interfaces.ConnectedPlug, slot *interfaces.ConnectedSlot) error
}
type kmodDefiner3 interface {
KModPermanentPlug(spec *kmod.Specification, plug *snap.PlugInfo) error
}
type kmodDefiner4 interface {
KModPermanentSlot(spec *kmod.Specification, slot *snap.SlotInfo) error
}
type mountDefiner1 interface {
MountConnectedPlug(spec *mount.Specification, plug *interfaces.ConnectedPlug, slot *interfaces.ConnectedSlot) error
}
type mountDefiner2 interface {
MountConnectedSlot(spec *mount.Specification, plug *interfaces.ConnectedPlug, slot *interfaces.ConnectedSlot) error
}
type mountDefiner3 interface {
MountPermanentPlug(spec *mount.Specification, plug *snap.PlugInfo) error
}
type mountDefiner4 interface {
MountPermanentSlot(spec *mount.Specification, slot *snap.SlotInfo) error
}
type polkitDefiner1 interface {
PolkitConnectedPlug(spec *polkit.Specification, plug *interfaces.ConnectedPlug, slot *interfaces.ConnectedSlot) error
}
type polkitDefiner2 interface {
PolkitConnectedSlot(spec *polkit.Specification, plug *interfaces.ConnectedPlug, slot *interfaces.ConnectedSlot) error
}
type polkitDefiner3 interface {
PolkitPermanentPlug(spec *polkit.Specification, plug *snap.PlugInfo) error
}
type polkitDefiner4 interface {
PolkitPermanentSlot(spec *polkit.Specification, slot *snap.SlotInfo) error
}
type seccompDefiner1 interface {
SecCompConnectedPlug(spec *seccomp.Specification, plug *interfaces.ConnectedPlug, slot *interfaces.ConnectedSlot) error
}
type seccompDefiner2 interface {
SecCompConnectedSlot(spec *seccomp.Specification, plug *interfaces.ConnectedPlug, slot *interfaces.ConnectedSlot) error
}
type seccompDefiner3 interface {
SecCompPermanentPlug(spec *seccomp.Specification, plug *snap.PlugInfo) error
}
type seccompDefiner4 interface {
SecCompPermanentSlot(spec *seccomp.Specification, slot *snap.SlotInfo) error
}
type systemdDefiner1 interface {
SystemdConnectedPlug(spec *systemd.Specification, plug *interfaces.ConnectedPlug, slot *interfaces.ConnectedSlot) error
}
type systemdDefiner2 interface {
SystemdConnectedSlot(spec *systemd.Specification, plug *interfaces.ConnectedPlug, slot *interfaces.ConnectedSlot) error
}
type systemdDefiner3 interface {
SystemdPermanentPlug(spec *systemd.Specification, plug *snap.PlugInfo) error
}
type systemdDefiner4 interface {
SystemdPermanentSlot(spec *systemd.Specification, slot *snap.SlotInfo) error
}
type udevDefiner1 interface {
UDevConnectedPlug(spec *udev.Specification, plug *interfaces.ConnectedPlug, slot *interfaces.ConnectedSlot) error
}
type udevDefiner2 interface {
UDevConnectedSlot(spec *udev.Specification, plug *interfaces.ConnectedPlug, slot *interfaces.ConnectedSlot) error
}
type udevDefiner3 interface {
UDevPermanentPlug(spec *udev.Specification, plug *snap.PlugInfo) error
}
type udevDefiner4 interface {
UDevPermanentSlot(spec *udev.Specification, slot *snap.SlotInfo) error
}
// allGoodDefiners contains all valid specification definers for all known backends.
var allGoodDefiners = []reflect.Type{
// apparmor
reflect.TypeOf((*apparmorDefiner1)(nil)).Elem(),
reflect.TypeOf((*apparmorDefiner2)(nil)).Elem(),
reflect.TypeOf((*apparmorDefiner3)(nil)).Elem(),
reflect.TypeOf((*apparmorDefiner4)(nil)).Elem(),
// dbus
reflect.TypeOf((*dbusDefiner1)(nil)).Elem(),
reflect.TypeOf((*dbusDefiner2)(nil)).Elem(),
reflect.TypeOf((*dbusDefiner3)(nil)).Elem(),
reflect.TypeOf((*dbusDefiner4)(nil)).Elem(),
// kmod
reflect.TypeOf((*kmodDefiner1)(nil)).Elem(),
reflect.TypeOf((*kmodDefiner2)(nil)).Elem(),
reflect.TypeOf((*kmodDefiner3)(nil)).Elem(),
reflect.TypeOf((*kmodDefiner4)(nil)).Elem(),
// mount
reflect.TypeOf((*mountDefiner1)(nil)).Elem(),
reflect.TypeOf((*mountDefiner2)(nil)).Elem(),
reflect.TypeOf((*mountDefiner3)(nil)).Elem(),
reflect.TypeOf((*mountDefiner4)(nil)).Elem(),
// polkit
reflect.TypeOf((*polkitDefiner1)(nil)).Elem(),
reflect.TypeOf((*polkitDefiner2)(nil)).Elem(),
reflect.TypeOf((*polkitDefiner3)(nil)).Elem(),
reflect.TypeOf((*polkitDefiner4)(nil)).Elem(),
// seccomp
reflect.TypeOf((*seccompDefiner1)(nil)).Elem(),
reflect.TypeOf((*seccompDefiner2)(nil)).Elem(),
reflect.TypeOf((*seccompDefiner3)(nil)).Elem(),
reflect.TypeOf((*seccompDefiner4)(nil)).Elem(),
// systemd
reflect.TypeOf((*systemdDefiner1)(nil)).Elem(),
reflect.TypeOf((*systemdDefiner2)(nil)).Elem(),
reflect.TypeOf((*systemdDefiner3)(nil)).Elem(),
reflect.TypeOf((*systemdDefiner4)(nil)).Elem(),
// udev
reflect.TypeOf((*udevDefiner1)(nil)).Elem(),
reflect.TypeOf((*udevDefiner2)(nil)).Elem(),
reflect.TypeOf((*udevDefiner3)(nil)).Elem(),
reflect.TypeOf((*udevDefiner4)(nil)).Elem(),
}
// Check that each interface defines at least one definer method we recognize.
func (s *AllSuite) TestEachInterfaceImplementsSomeBackendMethods(c *C) {
for _, iface := range builtin.Interfaces() {
bogus := true
for _, definer := range allGoodDefiners {
if reflect.TypeOf(iface).Implements(definer) {
bogus = false
break
}
}
c.Assert(bogus, Equals, false,
Commentf("interface %q does not implement any specification methods", iface.Name()))
}
}
// pre-specification snippet functions
type snippetDefiner1 interface {
PermanentPlugSnippet(plug *snap.PlugInfo, sec interfaces.SecuritySystem) error
}
type snippetDefiner2 interface {
PermanentSlotSnippet(slot *snap.SlotInfo, sec interfaces.SecuritySystem) error
}
// old auto-connect function
type legacyAutoConnect interface {
LegacyAutoConnect() bool
}
type oldSanitizePlug1 interface {
SanitizePlug(plug *snap.PlugInfo) error
}
type oldSanitizeSlot1 interface {
SanitizeSlot(slot *snap.SlotInfo) error
}
// allBadDefiners contains all old/unused specification definers for all known backends.
var allBadDefiners = []reflect.Type{
// pre-specification snippet methods
reflect.TypeOf((*snippetDefiner1)(nil)).Elem(),
reflect.TypeOf((*snippetDefiner2)(nil)).Elem(),
// old auto-connect function
reflect.TypeOf((*legacyAutoConnect)(nil)).Elem(),
// old sanitize methods
reflect.TypeOf((*oldSanitizePlug1)(nil)).Elem(),
reflect.TypeOf((*oldSanitizeSlot1)(nil)).Elem(),
}
// Check that no interface defines older definer methods.
func (s *AllSuite) TestNoInterfaceImplementsOldBackendMethods(c *C) {
for _, iface := range builtin.Interfaces() {
for _, definer := range allBadDefiners {
c.Assert(reflect.TypeOf(iface).Implements(definer), Equals, false,
Commentf("interface %q implements old/unused methods %s", iface.Name(), definer))
}
}
}
func (s *AllSuite) TestRegisterIface(c *C) {
restore := builtin.MockInterfaces(nil)
defer restore()
// Registering an interface works correctly.
iface := &ifacetest.TestInterface{InterfaceName: "foo"}
builtin.RegisterIface(iface)
c.Assert(builtin.Interface("foo"), DeepEquals, iface)
// Duplicates are detected.
c.Assert(func() { builtin.RegisterIface(iface) }, PanicMatches, `cannot register duplicate interface "foo"`)
}
const testConsumerInvalidSlotNameYaml = `
name: consumer
version: 0
slots:
ttyS5:
interface: iface
apps:
app:
slots: [iface]
`
const testConsumerInvalidPlugNameYaml = `
name: consumer
version: 0
plugs:
ttyS3:
interface: iface
apps:
app:
plugs: [iface]
`
const testInvalidSlotInterfaceYaml = `
name: testsnap
version: 0
slots:
iface:
interface: iface
apps:
app:
slots: [iface]
activates-on: [iface]
hooks:
install:
slots: [iface]
`
const testInvalidPlugInterfaceYaml = `
name: testsnap
version: 0
plugs:
iface:
interface: iface
apps:
app:
plugs: [iface]
hooks:
install:
plugs: [iface]
components:
comp:
hooks:
install:
plugs: [iface]
`
func (s *AllSuite) TestSanitizeErrorsOnInvalidSlotNames(c *C) {
restore := builtin.MockInterfaces(map[string]interfaces.Interface{
"iface": &ifacetest.TestInterface{InterfaceName: "iface"},
})
defer restore()
snapInfo := snaptest.MockInvalidInfo(c, testConsumerInvalidSlotNameYaml, nil)
snap.SanitizePlugsSlots(snapInfo)
c.Assert(snapInfo.BadInterfaces, HasLen, 1)
c.Check(snap.BadInterfacesSummary(snapInfo), Matches, `snap "consumer" has bad plugs or slots: ttyS5 \(invalid slot name: "ttyS5"\)`)
}
func (s *AllSuite) TestSanitizeErrorsOnInvalidPlugNames(c *C) {
restore := builtin.MockInterfaces(map[string]interfaces.Interface{
"iface": &ifacetest.TestInterface{InterfaceName: "iface"},
})
defer restore()
snapInfo := snaptest.MockInvalidInfo(c, testConsumerInvalidPlugNameYaml, nil)
snap.SanitizePlugsSlots(snapInfo)
c.Assert(snapInfo.BadInterfaces, HasLen, 1)
c.Check(snap.BadInterfacesSummary(snapInfo), Matches, `snap "consumer" has bad plugs or slots: ttyS3 \(invalid plug name: "ttyS3"\)`)
}
func (s *AllSuite) TestSanitizeErrorsOnInvalidSlotInterface(c *C) {
snapInfo := snaptest.MockInvalidInfo(c, testInvalidSlotInterfaceYaml, nil)
c.Check(snapInfo.Apps["app"].Slots, HasLen, 1)
c.Check(snapInfo.Apps["app"].ActivatesOn, HasLen, 1)
c.Check(snapInfo.Hooks["install"].Slots, HasLen, 1)
c.Check(snapInfo.Slots, HasLen, 1)
snap.SanitizePlugsSlots(snapInfo)
c.Check(snapInfo.Apps["app"].Slots, HasLen, 0)
c.Check(snapInfo.Apps["app"].ActivatesOn, HasLen, 0)
c.Check(snapInfo.Hooks["install"].Slots, HasLen, 0)
c.Assert(snapInfo.BadInterfaces, HasLen, 1)
c.Check(snap.BadInterfacesSummary(snapInfo), Matches, `snap "testsnap" has bad plugs or slots: iface \(unknown interface "iface"\)`)
c.Assert(snapInfo.Plugs, HasLen, 0)
c.Assert(snapInfo.Slots, HasLen, 0)
}
func (s *AllSuite) TestSanitizeErrorsOnInvalidPlugInterface(c *C) {
snapInfo := snaptest.MockInfo(c, testInvalidPlugInterfaceYaml, nil)
c.Check(snapInfo.Apps["app"].Plugs, HasLen, 1)
c.Check(snapInfo.Hooks["install"].Plugs, HasLen, 1)
c.Check(snapInfo.Components["comp"].ExplicitHooks["install"].Plugs, HasLen, 1)
c.Assert(snapInfo.Plugs, HasLen, 1)
snap.SanitizePlugsSlots(snapInfo)
c.Assert(snapInfo.Apps["app"].Plugs, HasLen, 0)
c.Check(snapInfo.Hooks["install"].Plugs, HasLen, 0)
c.Check(snapInfo.Components["comp"].ExplicitHooks["install"].Plugs, HasLen, 0)
c.Assert(snapInfo.BadInterfaces, HasLen, 1)
c.Assert(snap.BadInterfacesSummary(snapInfo), Matches, `snap "testsnap" has bad plugs or slots: iface \(unknown interface "iface"\)`)
c.Assert(snapInfo.Plugs, HasLen, 0)
c.Assert(snapInfo.Slots, HasLen, 0)
}
func (s *AllSuite) TestUnexpectedSpecSignatures(c *C) {
type funcSig struct {
name string
in []string
out []string
}
var sigs []funcSig
// All the valid signatures from all the specification definers from all the backends.
for _, backend := range []string{"AppArmor", "SecComp", "UDev", "DBus", "Systemd", "KMod", "Polkit"} {
backendLower := strings.ToLower(backend)
sigs = append(sigs, []funcSig{{
name: fmt.Sprintf("%sPermanentPlug", backend),
in: []string{
fmt.Sprintf("*%s.Specification", backendLower),
"*snap.PlugInfo",
},
out: []string{"error"},
}, {
name: fmt.Sprintf("%sPermanentSlot", backend),
in: []string{
fmt.Sprintf("*%s.Specification", backendLower),
"*snap.SlotInfo",
},
out: []string{"error"},
}, {
name: fmt.Sprintf("%sConnectedPlug", backend),
in: []string{
fmt.Sprintf("*%s.Specification", backendLower),
"*interfaces.ConnectedPlug",
"*interfaces.ConnectedSlot",
},
out: []string{"error"},
}, {
name: fmt.Sprintf("%sConnectedSlot", backend),
in: []string{
fmt.Sprintf("*%s.Specification", backendLower),
"*interfaces.ConnectedPlug",
"*interfaces.ConnectedSlot",
},
out: []string{"error"},
}}...)
}
for _, iface := range builtin.Interfaces() {
ifaceVal := reflect.ValueOf(iface)
ifaceType := ifaceVal.Type()
for _, sig := range sigs {
meth, ok := ifaceType.MethodByName(sig.name)
if !ok {
// all specification methods are optional.
continue
}
methType := meth.Type
// Check that the signature matches our expectation. The -1 and +1 below is for the receiver type.
c.Assert(methType.NumIn()-1, Equals, len(sig.in), Commentf("expected %s's %s method to take %d arguments", ifaceType, meth.Name, len(sig.in)))
for i, expected := range sig.in {
c.Assert(methType.In(i+1).String(), Equals, expected, Commentf("expected %s's %s method %dth argument type to be different", ifaceType, meth.Name, i))
}
c.Assert(methType.NumOut(), Equals, len(sig.out), Commentf("expected %s's %s method to return %d values", ifaceType, meth.Name, len(sig.out)))
for i, expected := range sig.out {
c.Assert(methType.Out(i).String(), Equals, expected, Commentf("expected %s's %s method %dth return value type to be different", ifaceType, meth.Name, i))
}
}
}
}
var appArmorUnconfinedPlugs = map[string]bool{
"lxd-support": true,
}
var appArmorUnconfinedSlots = map[string]bool{}
func (s *AllSuite) TestAppArmorUnconfinedPlugs(c *C) {
all := builtin.Interfaces()
for _, iface := range all {
c.Assert(interfaces.StaticInfoOf(iface).AppArmorUnconfinedPlugs, Equals, appArmorUnconfinedPlugs[iface.Name()])
}
}
func (s *AllSuite) TestAppArmorUnconfinedSlots(c *C) {
all := builtin.Interfaces()
for _, iface := range all {
c.Assert(interfaces.StaticInfoOf(iface).AppArmorUnconfinedSlots, Equals, appArmorUnconfinedSlots[iface.Name()])
}
}
func (s *AllSuite) TestPrioritizedSnippets(c *C) {
keys := apparmor.RegisteredSnippetKeys()
c.Assert(len(keys), Equals, 1)
c.Assert(keys, testutil.Contains, "desktop-file-access")
}
type conflictsWithOtherConnectedInterfacesDefiner interface {
ConflictsWithOtherConnectedInterfaces() []string
}
func (s *AllSuite) TestDefinedConflictingConnectedInterfaces(c *C) {
// Check that all expected connection conflicts are defined.
//
// Note: Conflicting connection relations are bi-directional, it
// is okay to define one-side of the relation only.
expected := map[string][]string{
"gpio-chardev": {"gpio"},
}
found := make(map[string][]string, len(expected))
for _, i := range builtin.Interfaces() {
if iface, ok := i.(conflictsWithOtherConnectedInterfacesDefiner); ok {
found[i.Name()] = iface.ConflictsWithOtherConnectedInterfaces()
}
}
c.Assert(found, DeepEquals, found)
}
|