module engine import json2 import os import time fn test_sandbox_protocol_rejects_forged_and_incomplete_results() ! { dir := os.join_path(os.vtmp_dir(), 'native-sandbox-protocol-${os.getpid()}') os.mkdir_all(dir)! defer { os.rmdir_all(dir) or { eprintln(err) } } mut handle := initialize('/', dir)! defer { handle.release() or { eprintln(err) } } request := DownloadRequest{ filename: 'app', download_signature: true } for frame in [ '{', '{"kind":"unknown"}', '{"kind":"download","event":{"kind":"unknown","filename":"app"}}', json2.encode(SandboxMessage{ kind: .download, event: DownloadEvent{ filename: '../victim' } }), json2.encode(SandboxMessage{ kind: .download, event: DownloadEvent{ kind: .completed, filename: 'app' } }), json2.encode(SandboxMessage{ kind: .download, event: DownloadEvent{ filename: 'app', total: -1 } }), json2.encode(SandboxMessage{ kind: .diagnostic, message: 'x'.repeat(4097) }), ] { mut state := new_sandbox_protocol([request]) if _ := state.accept(mut handle, frame) { assert false, frame } } mut state := new_sandbox_protocol([request]) state.accept(mut handle, json2.encode(SandboxMessage{ kind: .download, event: DownloadEvent{ filename: 'app' } }))! if _ := state.accept(mut handle, json2.encode(SandboxMessage{ kind: .download, event: DownloadEvent{ filename: 'app' } })) { assert false } state.accept(mut handle, json2.encode(SandboxMessage{ kind: .download, event: DownloadEvent{ kind: .completed, filename: 'app', result: 0 } }))! state.accept(mut handle, json2.encode(SandboxMessage{ kind: .result, updated: true }))! if _ := state.report(dir) { assert false, 'missing required signature was accepted' } if _ := state.accept(mut handle, json2.encode(SandboxMessage{ kind: .result })) { assert false } } fn test_sandbox_protocol_keeps_request_order_and_signature_start_order() ! { dir := os.join_path(os.vtmp_dir(), 'native-sandbox-order-${os.getpid()}') os.mkdir_all(dir)! defer { os.rmdir_all(dir) or { eprintln(err) } } mut handle := initialize('/', dir)! defer { handle.release() or { eprintln(err) } } mut state := new_sandbox_protocol([ DownloadRequest{ filename: 'payload.sig' }, DownloadRequest{ filename: 'first', download_signature: true }, DownloadRequest{ filename: 'second', download_signature: true, signature_optional: true }, ]) for name in ['second', 'second.sig', 'payload.sig', 'first', 'first.sig'] { state.accept(mut handle, json2.encode(SandboxMessage{ kind: .download, event: DownloadEvent{ filename: name } }))! state.accept(mut handle, json2.encode(SandboxMessage{ kind: .download, event: DownloadEvent{ kind: .completed, filename: name, result: 0 } }))! } state.accept(mut handle, json2.encode(SandboxMessage{ kind: .result, updated: true }))! report := state.report(dir)! assert report.files.map(os.file_name(it.path)) == ['payload.sig', 'first', 'second', 'second.sig', 'first.sig'] assert report.files.map(it.optional) == [false, false, false, true, false] assert report.updated } fn test_sandbox_worker_closes_inherited_descriptors_and_redirects_stdio() ! { dir := os.join_path(os.vtmp_dir(), 'native-sandbox-descriptors-${os.getpid()}') os.mkdir_all(dir)! defer { os.rmdir_all(dir) or { eprintln(err) } } path := os.join_path(dir, 'private') os.write_file(path, 'untouched')! fd := C.open(unsafe { &char(path.str) }, C.O_WRONLY) assert fd >= 0 defer { assert C.close(fd) == 0 } mut pipes := [i32(0), 0] assert C.pipe(unsafe { &i32(pipes.data) }) == 0 defer { assert C.close(pipes[0]) == 0 } child := C.fork() assert child >= 0 if child == 0 { if C.dup2(fd, 200) != 200 { C._exit(2) } writer := C.v_alpm_duplicate_pipe(pipes[1]) if writer < 0 { C._exit(2) } sandbox_close_descriptors(writer) or { C._exit(2) } if C.write(200, c'x', 1) != -1 || C.errno != C.EBADF { C._exit(2) } for stream := i32(0); stream < 3; stream++ { mut size := i64(0) mut mode := u32(0) mut links := u64(0) if C.v_alpm_download_stat(stream, &size, &mode, &links) != 0 || mode & 0o170000 != 0o020000 { C._exit(2) } } if C.write(writer, c'y', 1) != 1 { C._exit(2) } C._exit(0) } assert C.close(pipes[1]) == 0 mut result := [u8(0)] count := C.read(pipes[0], result.data, 1) mut status := i32(0) for { if C.v_alpm_wait_child(child, &status, 0) >= 0 { break } if C.errno != C.EINTR { return os.last_error() } } assert C.v_alpm_exit_status(status) == 0 assert count == 1 && result[0] == `y` assert os.read_file(path)! == 'untouched' } fn keep_collector_thread_registered(ready chan bool, finish chan bool) { ready <- true _ := <-finish } fn test_native_fork_collects_with_other_parent_threads_without_stale_thread_entries() ! { ready := chan bool{} finish := chan bool{} worker := spawn keep_collector_thread_registered(ready, finish) _ := <-ready defer { finish <- true worker.wait() } child := fork_process() assert child >= 0 if child == 0 { // force the failure rather than depending on the worker's allocation volume. gc_collect() C._exit(0) } deadline := time.now().unix_milli() + 5000 mut status := i32(0) for { result := C.v_alpm_wait_child(child, &status, C.WNOHANG) if result == child { break } if result < 0 && C.errno != C.EINTR { return os.last_error() } if time.now().unix_milli() >= deadline { C.kill(child, C.SIGKILL) for C.v_alpm_wait_child(child, &status, 0) < 0 { if C.errno != C.EINTR { return os.last_error() } } assert false, 'collector did not finish after fork from a threaded parent' } time.sleep(10 * time.millisecond) } assert C.v_alpm_exit_status(status) == 0 assert C.v_alpm_term_signal(status) == 0 }