github.com/bugraaydogar/snapd@v0.0.0-20210315170335-8c70bb858939/interfaces/ifacetest/backend.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 2016 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/snap" 25 "github.com/snapcore/snapd/timings" 26 ) 27 28 // TestSecurityBackend is a security backend intended for testing. 29 type TestSecurityBackend struct { 30 BackendName interfaces.SecuritySystem 31 // SetupCalls stores information about all calls to Setup 32 SetupCalls []TestSetupCall 33 // RemoveCalls stores information about all calls to Remove 34 RemoveCalls []string 35 // SetupCallback is an callback that is optionally called in Setup 36 SetupCallback func(snapInfo *snap.Info, opts interfaces.ConfinementOptions, repo *interfaces.Repository) error 37 // RemoveCallback is a callback that is optionally called in Remove 38 RemoveCallback func(snapName string) error 39 // SandboxFeaturesCallback is a callback that is optionally called in SandboxFeatures 40 SandboxFeaturesCallback func() []string 41 } 42 43 // TestSetupCall stores details about calls to TestSecurityBackend.Setup 44 type TestSetupCall struct { 45 // SnapInfo is a copy of the snapInfo argument to a particular call to Setup 46 SnapInfo *snap.Info 47 // Options is a copy of the confinement options to a particular call to Setup 48 Options interfaces.ConfinementOptions 49 } 50 51 // Initialize does nothing. 52 func (b *TestSecurityBackend) Initialize(*interfaces.SecurityBackendOptions) error { 53 return nil 54 } 55 56 // Name returns the name of the security backend. 57 func (b *TestSecurityBackend) Name() interfaces.SecuritySystem { 58 return b.BackendName 59 } 60 61 // Setup records information about the call and calls the setup callback if one is defined. 62 func (b *TestSecurityBackend) Setup(snapInfo *snap.Info, opts interfaces.ConfinementOptions, repo *interfaces.Repository, tm timings.Measurer) error { 63 b.SetupCalls = append(b.SetupCalls, TestSetupCall{SnapInfo: snapInfo, Options: opts}) 64 if b.SetupCallback == nil { 65 return nil 66 } 67 return b.SetupCallback(snapInfo, opts, repo) 68 } 69 70 // Remove records information about the call and calls the remove callback if one is defined 71 func (b *TestSecurityBackend) Remove(snapName string) error { 72 b.RemoveCalls = append(b.RemoveCalls, snapName) 73 if b.RemoveCallback == nil { 74 return nil 75 } 76 return b.RemoveCallback(snapName) 77 } 78 79 func (b *TestSecurityBackend) NewSpecification() interfaces.Specification { 80 return &Specification{} 81 } 82 83 func (b *TestSecurityBackend) SandboxFeatures() []string { 84 if b.SandboxFeaturesCallback == nil { 85 return nil 86 } 87 return b.SandboxFeaturesCallback() 88 } 89 90 // TestSecurityBackendSetupMany is a security backend that implements SetupMany on top of TestSecurityBackend.s 91 type TestSecurityBackendSetupMany struct { 92 TestSecurityBackend 93 94 // SetupManyCalls stores information about all calls to Setup 95 SetupManyCalls []TestSetupManyCall 96 97 // SetupManyCallback is an callback that is optionally called in Setup 98 SetupManyCallback func(snapInfo []*snap.Info, confinement func(snapName string) interfaces.ConfinementOptions, repo *interfaces.Repository, tm timings.Measurer) []error 99 } 100 101 // TestSetupManyCall stores details about calls to TestSecurityBackendMany.SetupMany 102 type TestSetupManyCall struct { 103 // SnapInfos is a copy of the snapInfo arguments to a particular call to SetupMany 104 SnapInfos []*snap.Info 105 } 106 107 func (b *TestSecurityBackendSetupMany) SetupMany(snaps []*snap.Info, confinement func(snapName string) interfaces.ConfinementOptions, repo *interfaces.Repository, tm timings.Measurer) []error { 108 b.SetupManyCalls = append(b.SetupManyCalls, TestSetupManyCall{SnapInfos: snaps}) 109 if b.SetupManyCallback == nil { 110 return nil 111 } 112 return b.SetupManyCallback(snaps, confinement, repo, tm) 113 }