@@ -5,6 +5,7 @@ package runner
55import (
66 "context"
77 "os/exec"
8+ "path/filepath"
89 "testing"
910
1011 "github.com/itchio/headway/state"
@@ -22,6 +23,24 @@ func bubblewrapSetenvValues(args []string, key string) []string {
2223 return out
2324}
2425
26+ func bubblewrapHasBind (args []string , source string , target string ) bool {
27+ for i := 0 ; i + 2 < len (args ); i ++ {
28+ if args [i ] == "--bind" && args [i + 1 ] == source && args [i + 2 ] == target {
29+ return true
30+ }
31+ }
32+ return false
33+ }
34+
35+ func bubblewrapBindIndex (args []string , source string , target string ) int {
36+ for i := 0 ; i + 2 < len (args ); i ++ {
37+ if args [i ] == "--bind" && args [i + 1 ] == source && args [i + 2 ] == target {
38+ return i
39+ }
40+ }
41+ return - 1
42+ }
43+
2544func TestEnsureSandboxParentDirs (t * testing.T ) {
2645 var args []string
2746 seen := make (map [string ]struct {})
@@ -212,3 +231,75 @@ func TestBubblewrapAllowEnvEmptyValueOverridesHost(t *testing.T) {
212231 require .NoError (t , br .Run ())
213232 assert .Equal (t , []string {"" }, bubblewrapSetenvValues (gotArgs , "SMAUG_ALLOW_ENV_EMPTY" ))
214233}
234+
235+ func TestBubblewrapMountsPersistentSandboxHome (t * testing.T ) {
236+ origCommand := bubblewrapCommand
237+ t .Cleanup (func () {
238+ bubblewrapCommand = origCommand
239+ })
240+
241+ var gotArgs []string
242+ bubblewrapCommand = func (name string , args ... string ) * exec.Cmd {
243+ gotArgs = append ([]string {}, args ... )
244+ return exec .Command ("sh" , "-c" , "true" )
245+ }
246+
247+ installFolder := t .TempDir ()
248+ homeTarget := "/home/sandbox-user"
249+ homeSource := filepath .Join (installFolder , ".itch" , "home" )
250+
251+ br := & bubblewrapRunner {
252+ params : RunnerParams {
253+ Consumer : & state.Consumer {OnMessage : func (string , string ) {}},
254+ Ctx : context .Background (),
255+ BubblewrapParams : BubblewrapParams {
256+ BinaryPath : "/fake/bwrap" ,
257+ },
258+ InstallFolder : installFolder ,
259+ Env : []string {"HOME=" + homeTarget },
260+ FullTargetPath : "/bin/true" ,
261+ },
262+ }
263+
264+ require .NoError (t , br .Run ())
265+ assert .DirExists (t , homeSource )
266+ assert .True (t , bubblewrapHasBind (gotArgs , homeSource , homeTarget ))
267+ }
268+
269+ func TestBubblewrapInstallBindComesAfterSandboxHomeBind (t * testing.T ) {
270+ origCommand := bubblewrapCommand
271+ t .Cleanup (func () {
272+ bubblewrapCommand = origCommand
273+ })
274+
275+ var gotArgs []string
276+ bubblewrapCommand = func (name string , args ... string ) * exec.Cmd {
277+ gotArgs = append ([]string {}, args ... )
278+ return exec .Command ("sh" , "-c" , "true" )
279+ }
280+
281+ installFolder := "/home/leafo/.config/kitch/apps/sample-evil-app 2"
282+ homeTarget := "/home/leafo"
283+ homeSource := filepath .Join (installFolder , ".itch" , "home" )
284+
285+ br := & bubblewrapRunner {
286+ params : RunnerParams {
287+ Consumer : & state.Consumer {OnMessage : func (string , string ) {}},
288+ Ctx : context .Background (),
289+ BubblewrapParams : BubblewrapParams {
290+ BinaryPath : "/fake/bwrap" ,
291+ },
292+ InstallFolder : installFolder ,
293+ Env : []string {"HOME=" + homeTarget },
294+ FullTargetPath : "/bin/true" ,
295+ },
296+ }
297+
298+ require .NoError (t , br .Run ())
299+
300+ homeBind := bubblewrapBindIndex (gotArgs , homeSource , homeTarget )
301+ installBind := bubblewrapBindIndex (gotArgs , installFolder , installFolder )
302+ require .NotEqual (t , - 1 , homeBind )
303+ require .NotEqual (t , - 1 , installBind )
304+ assert .Less (t , homeBind , installBind )
305+ }
0 commit comments