| 1 | module engine |
| 2 | |
| 3 | import os |
| 4 | |
| 5 | fn test_sandbox_staging_resumes_and_promotes_fresh_inodes() ! { |
| 6 | dir := os.join_path(os.vtmp_dir(), 'native-sandbox-stage-${os.getpid()}') |
| 7 | os.mkdir_all(dir)! |
| 8 | defer { os.rmdir_all(dir) or { eprintln(err) } } |
| 9 | partial := os.join_path(dir, 'app.pkg.tar.part') |
| 10 | os.write_file(partial, 'prefix')! |
| 11 | old := C.open(unsafe { &char(partial.str) }, C.O_WRONLY) |
| 12 | assert old >= 0 |
| 13 | defer { assert C.close(old) == 0 } |
| 14 | mut stage := create_sandbox_stage(dir, u32(C.getuid()), u32(C.getgid()))! |
| 15 | defer { stage.cleanup() or { eprintln(err) } } |
| 16 | stage.prepare([DownloadRequest{ filename: 'app.pkg.tar', allow_resume: true }])! |
| 17 | staged_partial := os.join_path(stage.directory, 'app.pkg.tar.part') |
| 18 | assert os.read_file(staged_partial)! == 'prefix' |
| 19 | assert !os.exists(partial) |
| 20 | assert C.write(old, c'ruined', 6) == 6 |
| 21 | assert os.read_file(staged_partial)! == 'prefix' |
| 22 | complete := os.join_path(stage.directory, 'app.pkg.tar') |
| 23 | os.mv(staged_partial, complete)! |
| 24 | os.utime(complete, 1700000000, 1700000000)! |
| 25 | worker := C.open(unsafe { &char(complete.str) }, C.O_WRONLY) |
| 26 | assert worker >= 0 |
| 27 | defer { assert C.close(worker) == 0 } |
| 28 | stage.finalize()! |
| 29 | cache := os.join_path(dir, 'app.pkg.tar') |
| 30 | assert os.read_file(cache)! == 'prefix' |
| 31 | assert os.stat(cache)!.mtime == 1700000000 |
| 32 | assert C.write(worker, c'ruined', 6) == 6 |
| 33 | assert os.read_file(cache)! == 'prefix' |
| 34 | assert !os.exists(partial) |
| 35 | } |
| 36 | |
| 37 | fn test_sandbox_finalization_rejects_links_and_special_files() ! { |
| 38 | dir := os.join_path(os.vtmp_dir(), 'native-sandbox-links-${os.getpid()}') |
| 39 | os.mkdir_all(dir)! |
| 40 | defer { os.rmdir_all(dir) or { eprintln(err) } } |
| 41 | victim := os.join_path(dir, 'victim') |
| 42 | os.write_file(victim, 'untouched')! |
| 43 | os.chmod(victim, 0o600)! |
| 44 | for kind in ['symlink', 'hardlink', 'directory', 'oversized'] { |
| 45 | mut stage := create_sandbox_stage(dir, u32(C.getuid()), u32(C.getgid()))! |
| 46 | stage.prepare([DownloadRequest{ filename: 'output', max_size: 4 }])! |
| 47 | output := os.join_path(stage.directory, 'output') |
| 48 | match kind { |
| 49 | 'symlink' { os.symlink(victim, output)! } |
| 50 | 'hardlink' { |
| 51 | assert C.link(unsafe { &char(victim.str) }, unsafe { &char(output.str) }) == 0 |
| 52 | } |
| 53 | 'directory' { os.mkdir(output)! } |
| 54 | else { os.write_file(output, 'too large')! } |
| 55 | } |
| 56 | if _ := stage.finalize() { |
| 57 | assert false, kind |
| 58 | } |
| 59 | assert !os.exists(os.join_path(dir, 'output')) |
| 60 | assert os.read_file(victim)! == 'untouched' |
| 61 | assert os.stat(victim)!.mode & 0o777 == 0o600 |
| 62 | if kind == 'directory' { os.rmdir(output)! } |
| 63 | stage.cleanup()! |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | fn test_sandbox_failure_preserves_existing_partials_and_restores_progress() ! { |
| 68 | dir := os.join_path(os.vtmp_dir(), 'native-sandbox-partials-${os.getpid()}') |
| 69 | os.mkdir_all(dir)! |
| 70 | defer { os.rmdir_all(dir) or { eprintln(err) } } |
| 71 | partial := os.join_path(dir, 'app.part') |
| 72 | os.write_file(partial, 'prefix')! |
| 73 | mut stage := create_sandbox_stage(dir, u32(C.getuid()), u32(C.getgid()))! |
| 74 | defer { stage.cleanup() or { eprintln(err) } } |
| 75 | if _ := stage.prepare([DownloadRequest{ filename: 'app', max_size: 2 }]) { |
| 76 | assert false |
| 77 | } |
| 78 | assert os.read_file(partial)! == 'prefix' |
| 79 | stage.finalize()! |
| 80 | assert os.read_file(partial)! == 'prefix' |
| 81 | } |
| 82 | |