module engine import nspkg.tests.installfixtures import nspkg.tests.transactionfixtures import nspkg.tests.signaturefixtures import os #include "@VMODROOT/tests/local_storage_probe.h" fn C.v_alpm_limit_database_write() i32 fn test_installed_cache_stays_sorted_and_owns_snapshots_after_each_insert() ! { base := os.join_path(os.vtmp_dir(), 'native-install-cache-order-${os.getpid()}') os.mkdir_all(base)! defer { os.rmdir_all(base) or { eprintln(err) } } mut handle := initialize('/', base)! defer { handle.release() or { eprintln(err) } } mut local := handle.local_database()! assert local.package_cache()!.len == 0 mut names := []string{} for name in ['middle', 'z-last', 'a-first', 'lower', 'upper'] { mut target := &Package{ name: name, version: '1-1', origin: .file, groups: ['test'] } local.groups_loaded = true installed := handle.cache_installed_package(target)! names << name names.sort() assert local.packages.map(it.name) == names assert local.by_name[name] or { return error('missing cached package') } == installed assert installed != target assert installed.owner == handle && installed.database == local assert !local.groups_loaded target.groups << 'changed' assert installed.groups == ['test'] } } fn test_native_install_from_repository_updates_payload_database_and_owned_cache() ! { base := os.join_path(os.vtmp_dir(), 'native-install-repository-${os.getpid()}') os.mkdir_all(base)! defer { os.rmdir_all(base) or { eprintln(err) } } fixture := transactionfixtures.create(base, 'fetch')! mut handle := initialize(fixture.root, fixture.db)! defer { handle.release() or { eprintln(err) } } handle.add_string_list(.cache_dirs, fixture.cache)! handle.set_integer_option(.check_space, 1)! mut repo := handle.register_syncdb('core', 0)! repo.add_server('file://' + fixture.server, false)! mut local := handle.local_database()! assert local.group_cache()!.len == 0 handle.start_transaction(.no_deps | .no_conflicts | .no_hooks | .no_scriptlet)! handle.add_package(repo.get_pkg('app') or { return error('missing target') })! assert handle.prepare_transaction()!.success handle.commit_transaction()! assert handle.transaction_state()! == .committed mut installed := local.get_pkg('app') or { return error('missing installed app') } assert installed.origin == .local_db assert installed.version == '2-1' assert installed.reason == .depend assert installed.validation == 4 assert installed.files_loaded && installed.desc_loaded && installed.scriptlet_loaded assert voidptr(installed.owner_database()!) == voidptr(local) assert os.read_file(os.join_path(fixture.root, 'usr/bin/app'))! == 'binary' assert !os.exists(os.join_path(fixture.root, 'old-payload')) assert !os.exists(os.join_path(fixture.db, 'local', 'app-1-1')) assert os.read_file(os.join_path(fixture.db, 'local', 'app-2-1', 'desc'))!.contains('%VALIDATION%\nsha256\n') assert local.group_cache()!.map(it.name) == ['alpha', 'beta'] mut target := handle.transaction_add()![0] assert voidptr(target) != voidptr(installed) target.reason = .explicit target.groups << 'changed' assert installed.reason == .depend assert installed.groups == ['alpha', 'beta'] handle.release_transaction()! assert installed.old_package == unsafe { nil } assert local.get_pkg('app') or { return error('lost package on release') } == installed local.invalidate()! mut reopened := local.get_pkg('app') or { return error('missing reopened app') } reopened.load_description()! reopened.load_files()! assert reopened.version == '2-1' assert reopened.reason == .depend assert reopened.validation == 4 assert reopened.files.map(it.name) == ['usr/', 'usr/bin/', 'usr/bin/app'] } fn test_partial_file_targets_are_completed_before_native_installation() ! { base := os.join_path(os.vtmp_dir(), 'native-install-partial-${os.getpid()}') defer { os.rmdir_all(base) or { eprintln(err) } } fixture := installfixtures.create(base, installfixtures.Scenario{ name: 'partial', partial: true })! mut handle := initialize(fixture.root, fixture.db)! defer { handle.release() or { eprintln(err) } } handle.add_string_list(.cache_dirs, fixture.cache)! handle.start_transaction(.no_deps | .no_conflicts | .no_hooks | .no_scriptlet)! defer { handle.release_transaction() or { eprintln(err) } } pkg := handle.load_package(fixture.archives[0], false, 0)! assert !pkg.files_loaded && pkg.files.len == 0 handle.add_package(pkg)! assert handle.prepare_transaction()!.success handle.commit_transaction()! assert os.read_file(os.join_path(fixture.root, 'usr/bin/app'))! == 'new binary' assert os.read_file(os.join_path(fixture.root, 'etc/app.conf'))! == 'incoming' assert handle.transaction_add()![0].files_loaded } fn test_install_time_parsing_and_mount_accounting_boundaries() ! { assert installation_timestamp(none, 42) == 42 for text in ['', ' ', '+', '-', '12 ', '12\n', '0x12', '18446744073709551616'] { assert installation_timestamp(text, 42) == 42, text } assert installation_timestamp(' \n+123', 42) == 123 assert installation_timestamp('-1', 42) == -1 assert installation_timestamp('18446744073709551615', 42) == -1 assert installation_timestamp('9223372036854775808', 42) == -max_i64 - 1 assert installation_blocks(0, 4096)! == 0 assert installation_blocks(4095, 4096)! == 1 assert installation_blocks(4096, 4096)! == 1 assert installation_blocks(4097, 4096)! == 2 assert installation_blocks(max_i64, 1)! == max_i64 if _ := installation_blocks(-1, 4096) { assert false } if _ := installation_blocks(1, 0) { assert false } mounts := installation_mounts('root / rootfs rw 0 0\n# comment\ntmp /usr tmpfs rw 0 0\nother /usr2 tmpfs rw 0 0\nspace /space\\040dir tmpfs rw 0 0\n') assert mounts[installation_mount_index(mounts, '/usr/bin/app')?].path == '/usr' assert mounts[installation_mount_index(mounts, '/usr2/file')?].path == '/usr2' assert mounts[installation_mount_index(mounts, '/usr20/file')?].path == '/' assert mounts[installation_mount_index(mounts, '/space dir/file')?].path == '/space dir' assert mount_path_unescape(r'/back\134slash\011tab') == '/back\\slash\ttab' } fn test_native_installation_verifies_signed_repository_targets() ! { base := os.join_path(os.vtmp_dir(), 'native-install-signed-${os.getpid()}') os.mkdir_all(base)! defer { os.rmdir_all(base) or { eprintln(err) } } fixture := transactionfixtures.create(base, 'multiple')! ring := signaturefixtures.create(os.join_path(base, 'signer'))! defer { signaturefixtures.cleanup(ring.path) or { eprintln(err) } } for name in fixture.names { ring.sign(os.join_path(fixture.cache, '${name}-2-1.pkg.tar'), [ring.fingerprint], false)! } mut handle := initialize(fixture.root, fixture.db)! defer { handle.release() or { eprintln(err) } } handle.add_string_list(.cache_dirs, fixture.cache)! handle.set_string_option(.gpgdir, ring.path)! mut repo := handle.register_syncdb('core', 1)! repo.add_server('file://' + fixture.server, false)! handle.start_transaction(.no_deps | .no_conflicts | .no_hooks | .no_scriptlet)! defer { handle.release_transaction() or { eprintln(err) } } for name in fixture.names { handle.add_package(repo.get_pkg(name) or { return error('missing signed target') })! } assert handle.prepare_transaction()!.success handle.commit_transaction()! mut local := handle.local_database()! for name in fixture.names { installed := local.get_pkg(name) or { return error('missing signed installation') } assert installed.validation == 12 assert os.read_file(os.join_path(fixture.root, 'usr/bin', name))! == 'binary' } } struct InstallationGuardTrace { mut: handle &Handle protected bool succeeded bool } fn installation_guard(context voidptr, event CommitEvent) { mut trace := unsafe { &InstallationGuardTrace(context) } if event.kind in [.package_install_done, .transaction_done] { trace.succeeded = true } if event.kind != .package_install_start { return } trace.handle.release_transaction() or { trace.protected = err.code() == int(Errno.trans_not_null) } assert trace.protected if _ := trace.handle.commit_transaction() { assert false } assert trace.handle.errno() == .trans_not_prepared } fn test_failed_installation_has_no_database_record_cache_entry_or_success_event() ! { base := os.join_path(os.vtmp_dir(), 'native-install-write-failure-${os.getpid()}') defer { os.rmdir_all(base) or { eprintln(err) } } fixture := installfixtures.create(base, installfixtures.Scenario{ name: 'write-failure', installed: false, missing_local: true, payload: 'payload'.repeat(4096) })! mut handle := initialize(fixture.root, fixture.db)! defer { handle.release() or { eprintln(err) } } handle.add_string_list(.cache_dirs, fixture.cache)! handle.start_transaction(.no_deps | .no_conflicts | .no_hooks | .no_scriptlet)! defer { handle.release_transaction() or { eprintln(err) } } handle.add_package(handle.load_package(fixture.archives[0], true, 0)!)! assert handle.prepare_transaction()!.success mut state := &InstallationGuardTrace{ handle: handle } handle.set_commit_callback(installation_guard, state)! child := C.fork() assert child >= 0 if child == 0 { if C.v_alpm_limit_database_write() != 0 { C._exit(2) } handle.commit_transaction() or { if err.code() != int(Errno.trans_abort) { C._exit(3) } if handle.transaction.state != .interrupted || handle.execution_active || !state.protected || state.succeeded { C._exit(4) } if 'app' in handle.local.by_name { C._exit(5) } if os.exists(os.join_path(fixture.db, 'local', 'app-2-1')) { C._exit(6) } C._exit(0) return } C._exit(7) } 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 !os.exists(os.join_path(fixture.db, 'local', 'app-2-1')) }