module engine import os struct CommandTrace { mut: lines []string } fn command_output(context voidptr, event CommitEvent) { mut trace := unsafe { &CommandTrace(context) } if event.kind == .scriptlet_info { trace.lines << event.line } } fn command_fixture(name string) !&Handle { db := os.join_path(os.vtmp_dir(), 'alpm-command-${name}-${os.getpid()}') os.mkdir_all(db)! return initialize('/', db) } fn cleanup_command_fixture(mut handle Handle) { path := handle.dbpath handle.release() or { eprintln(err) } os.rmdir_all(path) or { eprintln(err) } } fn test_command_combines_output_normalizes_partial_lines_and_reports_status() ! { mut handle := command_fixture('output')! defer { cleanup_command_fixture(mut handle) } mut root := handle.open_root_filesystem()! defer { root.close() } mut trace := &CommandTrace{} handle.set_commit_callback(command_output, trace)! handle.run_chroot_command(mut root, '/bin/sh', ['sh', '-c', "printf 'out\\n'; printf 'err\\n' >&2; printf tail"], [])! assert trace.lines == ['out\n', 'err\n', 'tail\n'] trace.lines.clear() handle.run_chroot_command(mut root, '/bin/sh', ['sh', '-c', "printf '%s' " + 'x'.repeat(5000)], [])! assert trace.lines.map(it.len) == [2047, 2047, 909] assert trace.lines.join('').replace('\n', '') == 'x'.repeat(5000) for script in ['exit 7', 'kill -TERM $$'] { if _ := handle.run_chroot_command(mut root, '/bin/sh', ['sh', '-c', script], []) { assert false } assert !handle.process_active } if _ := handle.run_chroot_command(mut root, '/missing-command', ['/missing-command'], []) { assert false } assert trace.lines.last().contains('execv failed') } fn test_command_pumps_both_directions_and_handles_closed_stdin_without_sigpipe() ! { mut handle := command_fixture('duplex')! defer { cleanup_command_fixture(mut handle) } mut root := handle.open_root_filesystem()! defer { root.close() } mut trace := &CommandTrace{} handle.set_commit_callback(command_output, trace)! input := 'after\n'.repeat(50000).bytes() handle.run_chroot_command(mut root, '/bin/sh', ['sh', '-c', 'awk \'BEGIN { for(i=0;i<50000;i++) print "before" }\'; cat'], input)! assert trace.lines.len == 100000 assert trace.lines[..50000].all(it == 'before\n') assert trace.lines[50000..].all(it == 'after\n') trace.lines.clear() handle.run_chroot_command(mut root, '/bin/sh', ['sh', '-c', 'exec 0<&-; printf done'], input)! assert trace.lines == ['done\n'] } fn test_command_child_environment_signals_and_descriptors_leave_parent_unchanged() ! { mut handle := command_fixture('environment')! defer { cleanup_command_fixture(mut handle) } mut root := handle.open_root_filesystem()! defer { root.close() } mut trace := &CommandTrace{} handle.set_commit_callback(command_output, trace)! old_environment := os.environ() assert os.setenv('BASH_ENV', '/not-an-alpm-startup-file', true) == 0 defer { if value := old_environment['BASH_ENV'] { assert os.setenv('BASH_ENV', value, true) == 0 } else { assert os.unsetenv('BASH_ENV') == 0 } } old_mask := C.umask(0o077) defer { C.umask(old_mask) } mut old_pipe := C.v_alpm_saved_signal{} assert C.v_alpm_signal_ignore(C.SIGPIPE, &old_pipe) == 0 defer { assert C.v_alpm_signal_restore(C.SIGPIPE, &old_pipe) == 0 } inherited := C.open(c'/dev/null', C.O_RDONLY) assert inherited >= 0 defer { assert C.close(inherited) == 0 } handle.run_chroot_command(mut root, '/bin/sh', ['sh', '-c', r'printf "%s\n" "${BASH_ENV-unset}"; umask; pwd; test ! -e /proc/self/fd/' + inherited.str()], [])! assert trace.lines == ['unset\n', '0022\n', '/\n'] assert os.getenv('BASH_ENV') == '/not-an-alpm-startup-file' assert C.umask(0o077) == 0o077 if _ := handle.run_chroot_command(mut root, '/bin/sh', ['sh', '-c', 'kill -PIPE $$'], []) { assert false } } fn test_command_accepts_closed_standard_descriptors_and_deleted_parent_cwd() ! { mut handle := command_fixture('descriptors')! defer { cleanup_command_fixture(mut handle) } mut root := handle.open_root_filesystem()! defer { root.close() } child := C.fork() assert child >= 0 if child == 0 { for fd in [i32(0), 1, 2] { if C.close(fd) != 0 { C._exit(2) } } mut child_root := handle.open_root_filesystem() or { C._exit(3) return } if child_root.fd != 0 { C._exit(4) } handle.run_chroot_command(mut child_root, '/bin/sh', ['sh', '-c', 'printf output'], []) or { C._exit(5) } C._exit(0) } 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 previous := os.getwd() defer { os.chdir(previous) or { eprintln(err) } } removed := os.join_path(handle.dbpath, 'removed-cwd') os.mkdir(removed)! os.chdir(removed)! os.rmdir(removed)! handle.run_chroot_command(mut root, '/bin/sh', ['sh', '-c', 'test "$PWD" = /'], [])! }