github.com/anonymouse64/snapd@v0.0.0-20210824153203-04c4c42d842d/interfaces/apparmor/export_test.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 apparmor 21 22 import ( 23 "os" 24 25 "github.com/snapcore/snapd/snap" 26 "github.com/snapcore/snapd/testutil" 27 ) 28 29 var ( 30 NsProfile = nsProfile 31 ProfileGlobs = profileGlobs 32 SnapConfineFromSnapProfile = snapConfineFromSnapProfile 33 LoadProfiles = loadProfiles 34 UnloadProfiles = unloadProfiles 35 MaybeSetNumberOfJobs = maybeSetNumberOfJobs 36 DefaultCoreRuntimeTemplateRules = defaultCoreRuntimeTemplateRules 37 DefaultOtherBaseTemplateRules = defaultOtherBaseTemplateRules 38 ) 39 40 func MockRuntimeNumCPU(new func() int) (restore func()) { 41 old := runtimeNumCPU 42 runtimeNumCPU = new 43 return func() { 44 runtimeNumCPU = old 45 } 46 } 47 48 // MockIsRootWritableOverlay mocks the real implementation of osutil.IsRootWritableOverlay 49 func MockIsRootWritableOverlay(new func() (string, error)) (restore func()) { 50 old := isRootWritableOverlay 51 isRootWritableOverlay = new 52 return func() { 53 isRootWritableOverlay = old 54 } 55 } 56 57 // MockProcSelfExe mocks the location of /proc/self/exe read by setupSnapConfineGeneratedPolicy. 58 func MockProcSelfExe(symlink string) (restore func()) { 59 old := procSelfExe 60 procSelfExe = symlink 61 return func() { 62 os.Remove(procSelfExe) 63 procSelfExe = old 64 } 65 } 66 67 // MockProfilesPath mocks the file read by LoadedProfiles() 68 func MockProfilesPath(t *testutil.BaseTest, profiles string) { 69 profilesPath = profiles 70 t.AddCleanup(func() { 71 profilesPath = realProfilesPath 72 }) 73 } 74 75 // MockTemplate replaces apprmor template. 76 // 77 // NOTE: The real apparmor template is long. For testing it is convenient for 78 // replace it with a shorter snippet. 79 func MockTemplate(fakeTemplate string) (restore func()) { 80 orig := defaultCoreRuntimeTemplate 81 defaultCoreRuntimeTemplate = fakeTemplate 82 return func() { defaultCoreRuntimeTemplate = orig } 83 } 84 85 // MockClassicTemplate replaces the classic apprmor template. 86 func MockClassicTemplate(fakeTemplate string) (restore func()) { 87 orig := classicTemplate 88 classicTemplate = fakeTemplate 89 return func() { classicTemplate = orig } 90 } 91 92 // SetSpecScope sets the scope of a given specification 93 func SetSpecScope(spec *Specification, securityTags []string) (restore func()) { 94 return spec.setScope(securityTags) 95 } 96 97 func MockKernelFeatures(f func() ([]string, error)) (resture func()) { 98 old := kernelFeatures 99 kernelFeatures = f 100 return func() { 101 kernelFeatures = old 102 } 103 } 104 105 func MockParserFeatures(f func() ([]string, error)) (resture func()) { 106 old := parserFeatures 107 parserFeatures = f 108 return func() { 109 parserFeatures = old 110 } 111 } 112 113 func (b *Backend) SetupSnapConfineReexec(info *snap.Info) error { 114 return b.setupSnapConfineReexec(info) 115 } 116 117 func (s *Specification) SnippetsForTag(tag string) []string { 118 return s.snippetsForTag(tag) 119 }