github.com/meulengracht/snapd@v0.0.0-20210719210640-8bde69bcc84e/interfaces/ifacetest/testiface.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 2015-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 ifacetest 21 22 import ( 23 "github.com/snapcore/snapd/interfaces" 24 "github.com/snapcore/snapd/interfaces/apparmor" 25 "github.com/snapcore/snapd/interfaces/dbus" 26 "github.com/snapcore/snapd/interfaces/hotplug" 27 "github.com/snapcore/snapd/interfaces/kmod" 28 "github.com/snapcore/snapd/interfaces/mount" 29 "github.com/snapcore/snapd/interfaces/seccomp" 30 "github.com/snapcore/snapd/interfaces/systemd" 31 "github.com/snapcore/snapd/interfaces/udev" 32 "github.com/snapcore/snapd/snap" 33 ) 34 35 // TestInterface is a interface for various kind of tests. 36 // It is public so that it can be consumed from other packages. 37 type TestInterface struct { 38 // InterfaceName is the name of this interface 39 InterfaceName string 40 InterfaceStaticInfo interfaces.StaticInfo 41 // AutoConnectCallback is the callback invoked inside AutoConnect 42 AutoConnectCallback func(*snap.PlugInfo, *snap.SlotInfo) bool 43 // BeforePreparePlugCallback is the callback invoked inside BeforePreparePlug() 44 BeforePreparePlugCallback func(plug *snap.PlugInfo) error 45 // BeforePrepareSlotCallback is the callback invoked inside BeforePrepareSlot() 46 BeforePrepareSlotCallback func(slot *snap.SlotInfo) error 47 48 BeforeConnectPlugCallback func(plug *interfaces.ConnectedPlug) error 49 BeforeConnectSlotCallback func(slot *interfaces.ConnectedSlot) error 50 51 // Support for interacting with the test backend. 52 53 TestConnectedPlugCallback func(spec *Specification, plug *interfaces.ConnectedPlug, slot *interfaces.ConnectedSlot) error 54 TestConnectedSlotCallback func(spec *Specification, plug *interfaces.ConnectedPlug, slot *interfaces.ConnectedSlot) error 55 TestPermanentPlugCallback func(spec *Specification, plug *snap.PlugInfo) error 56 TestPermanentSlotCallback func(spec *Specification, slot *snap.SlotInfo) error 57 58 // Support for interacting with the mount backend. 59 60 MountConnectedPlugCallback func(spec *mount.Specification, plug *interfaces.ConnectedPlug, slot *interfaces.ConnectedSlot) error 61 MountConnectedSlotCallback func(spec *mount.Specification, plug *interfaces.ConnectedPlug, slot *interfaces.ConnectedSlot) error 62 MountPermanentPlugCallback func(spec *mount.Specification, plug *snap.PlugInfo) error 63 MountPermanentSlotCallback func(spec *mount.Specification, slot *snap.SlotInfo) error 64 65 // Support for interacting with the udev backend. 66 67 UDevConnectedPlugCallback func(spec *udev.Specification, plug *interfaces.ConnectedPlug, slot *interfaces.ConnectedSlot) error 68 UDevConnectedSlotCallback func(spec *udev.Specification, plug *interfaces.ConnectedPlug, slot *interfaces.ConnectedSlot) error 69 UDevPermanentPlugCallback func(spec *udev.Specification, plug *snap.PlugInfo) error 70 UDevPermanentSlotCallback func(spec *udev.Specification, slot *snap.SlotInfo) error 71 72 // Support for interacting with the apparmor backend. 73 74 AppArmorConnectedPlugCallback func(spec *apparmor.Specification, plug *interfaces.ConnectedPlug, slot *interfaces.ConnectedSlot) error 75 AppArmorConnectedSlotCallback func(spec *apparmor.Specification, plug *interfaces.ConnectedPlug, slot *interfaces.ConnectedSlot) error 76 AppArmorPermanentPlugCallback func(spec *apparmor.Specification, plug *snap.PlugInfo) error 77 AppArmorPermanentSlotCallback func(spec *apparmor.Specification, slot *snap.SlotInfo) error 78 79 // Support for interacting with the kmod backend. 80 81 KModConnectedPlugCallback func(spec *kmod.Specification, plug *interfaces.ConnectedPlug, slot *interfaces.ConnectedSlot) error 82 KModConnectedSlotCallback func(spec *kmod.Specification, plug *interfaces.ConnectedPlug, slot *interfaces.ConnectedSlot) error 83 KModPermanentPlugCallback func(spec *kmod.Specification, plug *snap.PlugInfo) error 84 KModPermanentSlotCallback func(spec *kmod.Specification, slot *snap.SlotInfo) error 85 86 // Support for interacting with the seccomp backend. 87 88 SecCompConnectedPlugCallback func(spec *seccomp.Specification, plug *interfaces.ConnectedPlug, slot *interfaces.ConnectedSlot) error 89 SecCompConnectedSlotCallback func(spec *seccomp.Specification, plug *interfaces.ConnectedPlug, slot *interfaces.ConnectedSlot) error 90 SecCompPermanentPlugCallback func(spec *seccomp.Specification, plug *snap.PlugInfo) error 91 SecCompPermanentSlotCallback func(spec *seccomp.Specification, slot *snap.SlotInfo) error 92 93 // Support for interacting with the dbus backend. 94 95 DBusConnectedPlugCallback func(spec *dbus.Specification, plug *interfaces.ConnectedPlug, slot *interfaces.ConnectedSlot) error 96 DBusConnectedSlotCallback func(spec *dbus.Specification, plug *interfaces.ConnectedPlug, slot *interfaces.ConnectedSlot) error 97 DBusPermanentPlugCallback func(spec *dbus.Specification, plug *snap.PlugInfo) error 98 DBusPermanentSlotCallback func(spec *dbus.Specification, slot *snap.SlotInfo) error 99 100 // Support for interacting with the systemd backend. 101 102 SystemdConnectedPlugCallback func(spec *systemd.Specification, plug *interfaces.ConnectedPlug, slot *interfaces.ConnectedSlot) error 103 SystemdConnectedSlotCallback func(spec *systemd.Specification, plug *interfaces.ConnectedPlug, slot *interfaces.ConnectedSlot) error 104 SystemdPermanentPlugCallback func(spec *systemd.Specification, plug *snap.PlugInfo) error 105 SystemdPermanentSlotCallback func(spec *systemd.Specification, slot *snap.SlotInfo) error 106 } 107 108 // TestHotplugInterface is an interface for various kinds of tests 109 // needing a hotplug-aware interface. It is public so that it can be 110 // consumed from other packages. 111 type TestHotplugInterface struct { 112 TestInterface 113 114 // Support for interacting with hotplug subsystem. 115 HotplugKeyCallback func(deviceInfo *hotplug.HotplugDeviceInfo) (snap.HotplugKey, error) 116 HandledByGadgetCallback func(deviceInfo *hotplug.HotplugDeviceInfo, slot *snap.SlotInfo) bool 117 HotplugDeviceDetectedCallback func(deviceInfo *hotplug.HotplugDeviceInfo) (*hotplug.ProposedSlot, error) 118 } 119 120 // String() returns the same value as Name(). 121 func (t *TestInterface) String() string { 122 return t.Name() 123 } 124 125 // Name returns the name of the test interface. 126 func (t *TestInterface) Name() string { 127 return t.InterfaceName 128 } 129 130 func (t *TestInterface) StaticInfo() interfaces.StaticInfo { 131 return t.InterfaceStaticInfo 132 } 133 134 // BeforePreparePlug checks and possibly modifies a plug. 135 func (t *TestInterface) BeforePreparePlug(plug *snap.PlugInfo) error { 136 if t.BeforePreparePlugCallback != nil { 137 return t.BeforePreparePlugCallback(plug) 138 } 139 return nil 140 } 141 142 // BeforePrepareSlot checks and possibly modifies a slot. 143 func (t *TestInterface) BeforePrepareSlot(slot *snap.SlotInfo) error { 144 if t.BeforePrepareSlotCallback != nil { 145 return t.BeforePrepareSlotCallback(slot) 146 } 147 return nil 148 } 149 150 func (t *TestInterface) BeforeConnectPlug(plug *interfaces.ConnectedPlug) error { 151 if t.BeforeConnectPlugCallback != nil { 152 return t.BeforeConnectPlugCallback(plug) 153 } 154 return nil 155 } 156 157 func (t *TestInterface) BeforeConnectSlot(slot *interfaces.ConnectedSlot) error { 158 if t.BeforeConnectSlotCallback != nil { 159 return t.BeforeConnectSlotCallback(slot) 160 } 161 return nil 162 } 163 164 // AutoConnect returns whether plug and slot should be implicitly 165 // auto-connected assuming they will be an unambiguous connection 166 // candidate. 167 func (t *TestInterface) AutoConnect(plug *snap.PlugInfo, slot *snap.SlotInfo) bool { 168 if t.AutoConnectCallback != nil { 169 return t.AutoConnectCallback(plug, slot) 170 } 171 return true 172 } 173 174 // Support for interacting with the test backend. 175 176 func (t *TestInterface) TestConnectedPlug(spec *Specification, plug *interfaces.ConnectedPlug, slot *interfaces.ConnectedSlot) error { 177 if t.TestConnectedPlugCallback != nil { 178 return t.TestConnectedPlugCallback(spec, plug, slot) 179 } 180 return nil 181 } 182 183 func (t *TestInterface) TestConnectedSlot(spec *Specification, plug *interfaces.ConnectedPlug, slot *interfaces.ConnectedSlot) error { 184 if t.TestConnectedSlotCallback != nil { 185 return t.TestConnectedSlotCallback(spec, plug, slot) 186 } 187 return nil 188 } 189 190 func (t *TestInterface) TestPermanentPlug(spec *Specification, plug *snap.PlugInfo) error { 191 if t.TestPermanentPlugCallback != nil { 192 return t.TestPermanentPlugCallback(spec, plug) 193 } 194 return nil 195 } 196 197 func (t *TestInterface) TestPermanentSlot(spec *Specification, slot *snap.SlotInfo) error { 198 if t.TestPermanentSlotCallback != nil { 199 return t.TestPermanentSlotCallback(spec, slot) 200 } 201 return nil 202 } 203 204 // Support for interacting with the mount backend. 205 206 func (t *TestInterface) MountConnectedPlug(spec *mount.Specification, plug *interfaces.ConnectedPlug, slot *interfaces.ConnectedSlot) error { 207 if t.MountConnectedPlugCallback != nil { 208 return t.MountConnectedPlugCallback(spec, plug, slot) 209 } 210 return nil 211 } 212 213 func (t *TestInterface) MountConnectedSlot(spec *mount.Specification, plug *interfaces.ConnectedPlug, slot *interfaces.ConnectedSlot) error { 214 if t.MountConnectedSlotCallback != nil { 215 return t.MountConnectedSlotCallback(spec, plug, slot) 216 } 217 return nil 218 } 219 220 func (t *TestInterface) MountPermanentPlug(spec *mount.Specification, plug *snap.PlugInfo) error { 221 if t.MountPermanentPlugCallback != nil { 222 return t.MountPermanentPlugCallback(spec, plug) 223 } 224 return nil 225 } 226 227 func (t *TestInterface) MountPermanentSlot(spec *mount.Specification, slot *snap.SlotInfo) error { 228 if t.MountPermanentSlotCallback != nil { 229 return t.MountPermanentSlotCallback(spec, slot) 230 } 231 return nil 232 } 233 234 // Support for interacting with the udev backend. 235 236 func (t *TestInterface) UDevConnectedPlug(spec *udev.Specification, plug *interfaces.ConnectedPlug, slot *interfaces.ConnectedSlot) error { 237 if t.UDevConnectedPlugCallback != nil { 238 return t.UDevConnectedPlugCallback(spec, plug, slot) 239 } 240 return nil 241 } 242 243 func (t *TestInterface) UDevPermanentPlug(spec *udev.Specification, plug *snap.PlugInfo) error { 244 if t.UDevPermanentPlugCallback != nil { 245 return t.UDevPermanentPlugCallback(spec, plug) 246 } 247 return nil 248 } 249 250 func (t *TestInterface) UDevPermanentSlot(spec *udev.Specification, slot *snap.SlotInfo) error { 251 if t.UDevPermanentSlotCallback != nil { 252 return t.UDevPermanentSlotCallback(spec, slot) 253 } 254 return nil 255 } 256 257 func (t *TestInterface) UDevConnectedSlot(spec *udev.Specification, plug *interfaces.ConnectedPlug, slot *interfaces.ConnectedSlot) error { 258 if t.UDevConnectedSlotCallback != nil { 259 return t.UDevConnectedSlotCallback(spec, plug, slot) 260 } 261 return nil 262 } 263 264 // Support for interacting with the apparmor backend. 265 266 func (t *TestInterface) AppArmorConnectedPlug(spec *apparmor.Specification, plug *interfaces.ConnectedPlug, slot *interfaces.ConnectedSlot) error { 267 if t.AppArmorConnectedPlugCallback != nil { 268 return t.AppArmorConnectedPlugCallback(spec, plug, slot) 269 } 270 return nil 271 } 272 273 func (t *TestInterface) AppArmorPermanentSlot(spec *apparmor.Specification, slot *snap.SlotInfo) error { 274 if t.AppArmorPermanentSlotCallback != nil { 275 return t.AppArmorPermanentSlotCallback(spec, slot) 276 } 277 return nil 278 } 279 280 func (t *TestInterface) AppArmorConnectedSlot(spec *apparmor.Specification, plug *interfaces.ConnectedPlug, slot *interfaces.ConnectedSlot) error { 281 if t.AppArmorConnectedSlotCallback != nil { 282 return t.AppArmorConnectedSlotCallback(spec, plug, slot) 283 284 } 285 return nil 286 } 287 288 func (t *TestInterface) AppArmorPermanentPlug(spec *apparmor.Specification, plug *snap.PlugInfo) error { 289 if t.AppArmorPermanentPlugCallback != nil { 290 return t.AppArmorPermanentPlugCallback(spec, plug) 291 } 292 return nil 293 } 294 295 // Support for interacting with the seccomp backend. 296 297 func (t *TestInterface) SecCompConnectedPlug(spec *seccomp.Specification, plug *interfaces.ConnectedPlug, slot *interfaces.ConnectedSlot) error { 298 if t.SecCompConnectedPlugCallback != nil { 299 return t.SecCompConnectedPlugCallback(spec, plug, slot) 300 } 301 return nil 302 } 303 304 func (t *TestInterface) SecCompConnectedSlot(spec *seccomp.Specification, plug *interfaces.ConnectedPlug, slot *interfaces.ConnectedSlot) error { 305 if t.SecCompConnectedSlotCallback != nil { 306 return t.SecCompConnectedSlotCallback(spec, plug, slot) 307 } 308 return nil 309 } 310 311 func (t *TestInterface) SecCompPermanentSlot(spec *seccomp.Specification, slot *snap.SlotInfo) error { 312 if t.SecCompPermanentSlotCallback != nil { 313 return t.SecCompPermanentSlotCallback(spec, slot) 314 } 315 return nil 316 } 317 318 func (t *TestInterface) SecCompPermanentPlug(spec *seccomp.Specification, plug *snap.PlugInfo) error { 319 if t.SecCompPermanentPlugCallback != nil { 320 return t.SecCompPermanentPlugCallback(spec, plug) 321 } 322 return nil 323 } 324 325 // Support for interacting with the kmod backend. 326 327 func (t *TestInterface) KModConnectedPlug(spec *kmod.Specification, plug *interfaces.ConnectedPlug, slot *interfaces.ConnectedSlot) error { 328 if t.KModConnectedPlugCallback != nil { 329 return t.KModConnectedPlugCallback(spec, plug, slot) 330 } 331 return nil 332 } 333 334 func (t *TestInterface) KModConnectedSlot(spec *kmod.Specification, plug *interfaces.ConnectedPlug, slot *interfaces.ConnectedSlot) error { 335 if t.KModConnectedSlotCallback != nil { 336 return t.KModConnectedSlotCallback(spec, plug, slot) 337 } 338 return nil 339 } 340 341 func (t *TestInterface) KModPermanentPlug(spec *kmod.Specification, plug *snap.PlugInfo) error { 342 if t.KModPermanentPlugCallback != nil { 343 return t.KModPermanentPlugCallback(spec, plug) 344 } 345 return nil 346 } 347 348 func (t *TestInterface) KModPermanentSlot(spec *kmod.Specification, slot *snap.SlotInfo) error { 349 if t.KModPermanentSlotCallback != nil { 350 return t.KModPermanentSlotCallback(spec, slot) 351 } 352 return nil 353 } 354 355 // Support for interacting with the dbus backend. 356 357 func (t *TestInterface) DBusConnectedPlug(spec *dbus.Specification, plug *interfaces.ConnectedPlug, slot *interfaces.ConnectedSlot) error { 358 if t.DBusConnectedPlugCallback != nil { 359 return t.DBusConnectedPlugCallback(spec, plug, slot) 360 } 361 return nil 362 } 363 364 func (t *TestInterface) DBusConnectedSlot(spec *dbus.Specification, plug *interfaces.ConnectedPlug, slot *interfaces.ConnectedSlot) error { 365 if t.DBusConnectedSlotCallback != nil { 366 return t.DBusConnectedSlotCallback(spec, plug, slot) 367 } 368 return nil 369 } 370 371 func (t *TestInterface) DBusPermanentSlot(spec *dbus.Specification, slot *snap.SlotInfo) error { 372 if t.DBusPermanentSlotCallback != nil { 373 return t.DBusPermanentSlotCallback(spec, slot) 374 } 375 return nil 376 } 377 378 func (t *TestInterface) DBusPermanentPlug(spec *dbus.Specification, plug *snap.PlugInfo) error { 379 if t.DBusPermanentPlugCallback != nil { 380 return t.DBusPermanentPlugCallback(spec, plug) 381 } 382 return nil 383 } 384 385 // Support for interacting with the systemd backend. 386 387 func (t *TestInterface) SystemdConnectedPlug(spec *systemd.Specification, plug *interfaces.ConnectedPlug, slot *interfaces.ConnectedSlot) error { 388 if t.SystemdConnectedPlugCallback != nil { 389 return t.SystemdConnectedPlugCallback(spec, plug, slot) 390 } 391 return nil 392 } 393 394 func (t *TestInterface) SystemdConnectedSlot(spec *systemd.Specification, plug *interfaces.ConnectedPlug, slot *interfaces.ConnectedSlot) error { 395 if t.SystemdConnectedSlotCallback != nil { 396 return t.SystemdConnectedSlotCallback(spec, plug, slot) 397 } 398 return nil 399 } 400 401 func (t *TestInterface) SystemdPermanentSlot(spec *systemd.Specification, slot *snap.SlotInfo) error { 402 if t.SystemdPermanentSlotCallback != nil { 403 return t.SystemdPermanentSlotCallback(spec, slot) 404 } 405 return nil 406 } 407 408 func (t *TestInterface) SystemdPermanentPlug(spec *systemd.Specification, plug *snap.PlugInfo) error { 409 if t.SystemdPermanentPlugCallback != nil { 410 return t.SystemdPermanentPlugCallback(spec, plug) 411 } 412 return nil 413 } 414 415 // Support for interacting with hotplug subsystem. 416 417 func (t *TestHotplugInterface) HotplugKey(deviceInfo *hotplug.HotplugDeviceInfo) (snap.HotplugKey, error) { 418 if t.HotplugKeyCallback != nil { 419 return t.HotplugKeyCallback(deviceInfo) 420 } 421 return "", nil 422 } 423 424 func (t *TestHotplugInterface) HotplugDeviceDetected(deviceInfo *hotplug.HotplugDeviceInfo) (*hotplug.ProposedSlot, error) { 425 if t.HotplugDeviceDetectedCallback != nil { 426 return t.HotplugDeviceDetectedCallback(deviceInfo) 427 } 428 return nil, nil 429 } 430 431 func (t *TestHotplugInterface) HandledByGadget(deviceInfo *hotplug.HotplugDeviceInfo, slot *snap.SlotInfo) bool { 432 if t.HandledByGadgetCallback != nil { 433 return t.HandledByGadgetCallback(deviceInfo, slot) 434 } 435 return false 436 }