github.com/kubiko/snapd@v0.0.0-20201013125620-d4f3094d9ddf/cmd/snap-update-ns/export_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 main 21 22 import ( 23 "os" 24 "syscall" 25 26 "github.com/snapcore/snapd/osutil" 27 "github.com/snapcore/snapd/osutil/sys" 28 ) 29 30 var ( 31 // change 32 ValidateInstanceName = validateInstanceName 33 ProcessArguments = processArguments 34 35 // utils 36 PlanWritableMimic = planWritableMimic 37 ExecWritableMimic = execWritableMimic 38 39 // bootstrap 40 ClearBootstrapError = clearBootstrapError 41 42 // trespassing 43 IsReadOnly = isReadOnly 44 IsPrivateTmpfsCreatedBySnapd = isPrivateTmpfsCreatedBySnapd 45 46 // system 47 DesiredSystemProfilePath = desiredSystemProfilePath 48 CurrentSystemProfilePath = currentSystemProfilePath 49 50 // user 51 DesiredUserProfilePath = desiredUserProfilePath 52 CurrentUserProfilePath = currentUserProfilePath 53 54 // xdg 55 XdgRuntimeDir = xdgRuntimeDir 56 ExpandPrefixVariable = expandPrefixVariable 57 ExpandXdgRuntimeDir = expandXdgRuntimeDir 58 59 // update 60 ExecuteMountProfileUpdate = executeMountProfileUpdate 61 ) 62 63 // SystemCalls encapsulates various system interactions performed by this module. 64 type SystemCalls interface { 65 OsLstat(name string) (os.FileInfo, error) 66 SysLstat(name string, buf *syscall.Stat_t) error 67 ReadDir(dirname string) ([]os.FileInfo, error) 68 Symlinkat(oldname string, dirfd int, newname string) error 69 Readlinkat(dirfd int, path string, buf []byte) (int, error) 70 Remove(name string) error 71 72 Close(fd int) error 73 Fchdir(fd int) error 74 Fchown(fd int, uid sys.UserID, gid sys.GroupID) error 75 Mkdirat(dirfd int, path string, mode uint32) error 76 Mount(source string, target string, fstype string, flags uintptr, data string) (err error) 77 Open(path string, flags int, mode uint32) (fd int, err error) 78 Openat(dirfd int, path string, flags int, mode uint32) (fd int, err error) 79 Unmount(target string, flags int) error 80 Fstat(fd int, buf *syscall.Stat_t) error 81 Fstatfs(fd int, buf *syscall.Statfs_t) error 82 } 83 84 // MockSystemCalls replaces real system calls with those of the argument. 85 func MockSystemCalls(sc SystemCalls) (restore func()) { 86 // save 87 oldOsLstat := osLstat 88 oldRemove := osRemove 89 oldIoutilReadDir := ioutilReadDir 90 91 oldSysClose := sysClose 92 oldSysFchown := sysFchown 93 oldSysMkdirat := sysMkdirat 94 oldSysMount := sysMount 95 oldSysOpen := sysOpen 96 oldSysOpenat := sysOpenat 97 oldSysUnmount := sysUnmount 98 oldSysSymlinkat := sysSymlinkat 99 oldReadlinkat := sysReadlinkat 100 oldFstat := sysFstat 101 oldFstatfs := sysFstatfs 102 oldSysFchdir := sysFchdir 103 oldSysLstat := sysLstat 104 105 // override 106 osLstat = sc.OsLstat 107 osRemove = sc.Remove 108 ioutilReadDir = sc.ReadDir 109 110 sysClose = sc.Close 111 sysFchown = sc.Fchown 112 sysMkdirat = sc.Mkdirat 113 sysMount = sc.Mount 114 sysOpen = sc.Open 115 sysOpenat = sc.Openat 116 sysUnmount = sc.Unmount 117 sysSymlinkat = sc.Symlinkat 118 sysReadlinkat = sc.Readlinkat 119 sysFstat = sc.Fstat 120 sysFstatfs = sc.Fstatfs 121 sysFchdir = sc.Fchdir 122 sysLstat = sc.SysLstat 123 124 return func() { 125 // restore 126 osLstat = oldOsLstat 127 osRemove = oldRemove 128 ioutilReadDir = oldIoutilReadDir 129 130 sysClose = oldSysClose 131 sysFchown = oldSysFchown 132 sysMkdirat = oldSysMkdirat 133 sysMount = oldSysMount 134 sysOpen = oldSysOpen 135 sysOpenat = oldSysOpenat 136 sysUnmount = oldSysUnmount 137 sysSymlinkat = oldSysSymlinkat 138 sysReadlinkat = oldReadlinkat 139 sysFstat = oldFstat 140 sysFstatfs = oldFstatfs 141 sysFchdir = oldSysFchdir 142 sysLstat = oldSysLstat 143 } 144 } 145 146 func MockChangePerform(f func(chg *Change, as *Assumptions) ([]*Change, error)) func() { 147 origChangePerform := changePerform 148 changePerform = f 149 return func() { 150 changePerform = origChangePerform 151 } 152 } 153 154 func MockNeededChanges(f func(old, new *osutil.MountProfile) []*Change) (restore func()) { 155 origNeededChanges := NeededChanges 156 NeededChanges = f 157 return func() { 158 NeededChanges = origNeededChanges 159 } 160 } 161 162 func MockReadDir(fn func(string) ([]os.FileInfo, error)) (restore func()) { 163 old := ioutilReadDir 164 ioutilReadDir = fn 165 return func() { 166 ioutilReadDir = old 167 } 168 } 169 170 func MockReadlink(fn func(string) (string, error)) (restore func()) { 171 old := osReadlink 172 osReadlink = fn 173 return func() { 174 osReadlink = old 175 } 176 } 177 178 func (as *Assumptions) IsRestricted(path string) bool { 179 return as.isRestricted(path) 180 } 181 182 func (as *Assumptions) PastChanges() []*Change { 183 return as.pastChanges 184 } 185 186 func (as *Assumptions) CanWriteToDirectory(dirFd int, dirName string) (bool, error) { 187 return as.canWriteToDirectory(dirFd, dirName) 188 } 189 190 func (as *Assumptions) UnrestrictedPaths() []string { 191 return as.unrestrictedPaths 192 } 193 194 func (upCtx *CommonProfileUpdateContext) CurrentProfilePath() string { 195 return upCtx.currentProfilePath 196 } 197 198 func (upCtx *CommonProfileUpdateContext) DesiredProfilePath() string { 199 return upCtx.desiredProfilePath 200 } 201 202 func (upCtx *CommonProfileUpdateContext) FromSnapConfine() bool { 203 return upCtx.fromSnapConfine 204 } 205 206 func (upCtx *CommonProfileUpdateContext) SetFromSnapConfine(v bool) { 207 upCtx.fromSnapConfine = v 208 } 209 210 func NewCommonProfileUpdateContext(instanceName string, fromSnapConfine bool, currentProfilePath, desiredProfilePath string) *CommonProfileUpdateContext { 211 return &CommonProfileUpdateContext{ 212 instanceName: instanceName, 213 fromSnapConfine: fromSnapConfine, 214 currentProfilePath: currentProfilePath, 215 desiredProfilePath: desiredProfilePath, 216 } 217 }