| 1 | module engine |
| 2 | |
| 3 | import nspkg.tests.installfixtures |
| 4 | import nspkg.tests.transactionfixtures |
| 5 | import nspkg.tests.signaturefixtures |
| 6 | import os |
| 7 | |
| 8 | #include "@VMODROOT/tests/local_storage_probe.h" |
| 9 | |
| 10 | fn C.v_alpm_limit_database_write() i32 |
| 11 | |
| 12 | fn test_installed_cache_stays_sorted_and_owns_snapshots_after_each_insert() ! { |
| 13 | base := os.join_path(os.vtmp_dir(), 'native-install-cache-order-${os.getpid()}') |
| 14 | os.mkdir_all(base)! |
| 15 | defer { os.rmdir_all(base) or { eprintln(err) } } |
| 16 | mut handle := initialize('/', base)! |
| 17 | defer { handle.release() or { eprintln(err) } } |
| 18 | mut local := handle.local_database()! |
| 19 | assert local.package_cache()!.len == 0 |
| 20 | mut names := []string{} |
| 21 | for name in ['middle', 'z-last', 'a-first', 'lower', 'upper'] { |
| 22 | mut target := &Package{ name: name, version: '1-1', origin: .file, groups: ['test'] } |
| 23 | local.groups_loaded = true |
| 24 | installed := handle.cache_installed_package(target)! |
| 25 | names << name |
| 26 | names.sort() |
| 27 | assert local.packages.map(it.name) == names |
| 28 | assert local.by_name[name] or { return error('missing cached package') } == installed |
| 29 | assert installed != target |
| 30 | assert installed.owner == handle && installed.database == local |
| 31 | assert !local.groups_loaded |
| 32 | target.groups << 'changed' |
| 33 | assert installed.groups == ['test'] |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | fn test_native_install_from_repository_updates_payload_database_and_owned_cache() ! { |
| 38 | base := os.join_path(os.vtmp_dir(), 'native-install-repository-${os.getpid()}') |
| 39 | os.mkdir_all(base)! |
| 40 | defer { os.rmdir_all(base) or { eprintln(err) } } |
| 41 | fixture := transactionfixtures.create(base, 'fetch')! |
| 42 | mut handle := initialize(fixture.root, fixture.db)! |
| 43 | defer { handle.release() or { eprintln(err) } } |
| 44 | handle.add_string_list(.cache_dirs, fixture.cache)! |
| 45 | handle.set_integer_option(.check_space, 1)! |
| 46 | mut repo := handle.register_syncdb('core', 0)! |
| 47 | repo.add_server('file://' + fixture.server, false)! |
| 48 | mut local := handle.local_database()! |
| 49 | assert local.group_cache()!.len == 0 |
| 50 | handle.start_transaction(.no_deps | .no_conflicts | .no_hooks | .no_scriptlet)! |
| 51 | handle.add_package(repo.get_pkg('app') or { return error('missing target') })! |
| 52 | assert handle.prepare_transaction()!.success |
| 53 | handle.commit_transaction()! |
| 54 | assert handle.transaction_state()! == .committed |
| 55 | mut installed := local.get_pkg('app') or { return error('missing installed app') } |
| 56 | assert installed.origin == .local_db |
| 57 | assert installed.version == '2-1' |
| 58 | assert installed.reason == .depend |
| 59 | assert installed.validation == 4 |
| 60 | assert installed.files_loaded && installed.desc_loaded && installed.scriptlet_loaded |
| 61 | assert voidptr(installed.owner_database()!) == voidptr(local) |
| 62 | assert os.read_file(os.join_path(fixture.root, 'usr/bin/app'))! == 'binary' |
| 63 | assert !os.exists(os.join_path(fixture.root, 'old-payload')) |
| 64 | assert !os.exists(os.join_path(fixture.db, 'local', 'app-1-1')) |
| 65 | assert os.read_file(os.join_path(fixture.db, 'local', 'app-2-1', 'desc'))!.contains('%VALIDATION%\nsha256\n') |
| 66 | assert local.group_cache()!.map(it.name) == ['alpha', 'beta'] |
| 67 | mut target := handle.transaction_add()![0] |
| 68 | assert voidptr(target) != voidptr(installed) |
| 69 | target.reason = .explicit |
| 70 | target.groups << 'changed' |
| 71 | assert installed.reason == .depend |
| 72 | assert installed.groups == ['alpha', 'beta'] |
| 73 | handle.release_transaction()! |
| 74 | assert installed.old_package == unsafe { nil } |
| 75 | assert local.get_pkg('app') or { return error('lost package on release') } == installed |
| 76 | local.invalidate()! |
| 77 | mut reopened := local.get_pkg('app') or { return error('missing reopened app') } |
| 78 | reopened.load_description()! |
| 79 | reopened.load_files()! |
| 80 | assert reopened.version == '2-1' |
| 81 | assert reopened.reason == .depend |
| 82 | assert reopened.validation == 4 |
| 83 | assert reopened.files.map(it.name) == ['usr/', 'usr/bin/', 'usr/bin/app'] |
| 84 | } |
| 85 | |
| 86 | fn test_partial_file_targets_are_completed_before_native_installation() ! { |
| 87 | base := os.join_path(os.vtmp_dir(), 'native-install-partial-${os.getpid()}') |
| 88 | defer { os.rmdir_all(base) or { eprintln(err) } } |
| 89 | fixture := installfixtures.create(base, installfixtures.Scenario{ name: 'partial', partial: true })! |
| 90 | mut handle := initialize(fixture.root, fixture.db)! |
| 91 | defer { handle.release() or { eprintln(err) } } |
| 92 | handle.add_string_list(.cache_dirs, fixture.cache)! |
| 93 | handle.start_transaction(.no_deps | .no_conflicts | .no_hooks | .no_scriptlet)! |
| 94 | defer { handle.release_transaction() or { eprintln(err) } } |
| 95 | pkg := handle.load_package(fixture.archives[0], false, 0)! |
| 96 | assert !pkg.files_loaded && pkg.files.len == 0 |
| 97 | handle.add_package(pkg)! |
| 98 | assert handle.prepare_transaction()!.success |
| 99 | handle.commit_transaction()! |
| 100 | assert os.read_file(os.join_path(fixture.root, 'usr/bin/app'))! == 'new binary' |
| 101 | assert os.read_file(os.join_path(fixture.root, 'etc/app.conf'))! == 'incoming' |
| 102 | assert handle.transaction_add()![0].files_loaded |
| 103 | } |
| 104 | |
| 105 | fn test_install_time_parsing_and_mount_accounting_boundaries() ! { |
| 106 | assert installation_timestamp(none, 42) == 42 |
| 107 | for text in ['', ' ', '+', '-', '12 ', '12\n', '0x12', '18446744073709551616'] { |
| 108 | assert installation_timestamp(text, 42) == 42, text |
| 109 | } |
| 110 | assert installation_timestamp(' \n+123', 42) == 123 |
| 111 | assert installation_timestamp('-1', 42) == -1 |
| 112 | assert installation_timestamp('18446744073709551615', 42) == -1 |
| 113 | assert installation_timestamp('9223372036854775808', 42) == -max_i64 - 1 |
| 114 | assert installation_blocks(0, 4096)! == 0 |
| 115 | assert installation_blocks(4095, 4096)! == 1 |
| 116 | assert installation_blocks(4096, 4096)! == 1 |
| 117 | assert installation_blocks(4097, 4096)! == 2 |
| 118 | assert installation_blocks(max_i64, 1)! == max_i64 |
| 119 | if _ := installation_blocks(-1, 4096) { |
| 120 | assert false |
| 121 | } |
| 122 | if _ := installation_blocks(1, 0) { |
| 123 | assert false |
| 124 | } |
| 125 | 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') |
| 126 | assert mounts[installation_mount_index(mounts, '/usr/bin/app')?].path == '/usr' |
| 127 | assert mounts[installation_mount_index(mounts, '/usr2/file')?].path == '/usr2' |
| 128 | assert mounts[installation_mount_index(mounts, '/usr20/file')?].path == '/' |
| 129 | assert mounts[installation_mount_index(mounts, '/space dir/file')?].path == '/space dir' |
| 130 | assert mount_path_unescape(r'/back\134slash\011tab') == '/back\\slash\ttab' |
| 131 | } |
| 132 | |
| 133 | fn test_native_installation_verifies_signed_repository_targets() ! { |
| 134 | base := os.join_path(os.vtmp_dir(), 'native-install-signed-${os.getpid()}') |
| 135 | os.mkdir_all(base)! |
| 136 | defer { os.rmdir_all(base) or { eprintln(err) } } |
| 137 | fixture := transactionfixtures.create(base, 'multiple')! |
| 138 | ring := signaturefixtures.create(os.join_path(base, 'signer'))! |
| 139 | defer { signaturefixtures.cleanup(ring.path) or { eprintln(err) } } |
| 140 | for name in fixture.names { |
| 141 | ring.sign(os.join_path(fixture.cache, '${name}-2-1.pkg.tar'), [ring.fingerprint], false)! |
| 142 | } |
| 143 | mut handle := initialize(fixture.root, fixture.db)! |
| 144 | defer { handle.release() or { eprintln(err) } } |
| 145 | handle.add_string_list(.cache_dirs, fixture.cache)! |
| 146 | handle.set_string_option(.gpgdir, ring.path)! |
| 147 | mut repo := handle.register_syncdb('core', 1)! |
| 148 | repo.add_server('file://' + fixture.server, false)! |
| 149 | handle.start_transaction(.no_deps | .no_conflicts | .no_hooks | .no_scriptlet)! |
| 150 | defer { handle.release_transaction() or { eprintln(err) } } |
| 151 | for name in fixture.names { |
| 152 | handle.add_package(repo.get_pkg(name) or { return error('missing signed target') })! |
| 153 | } |
| 154 | assert handle.prepare_transaction()!.success |
| 155 | handle.commit_transaction()! |
| 156 | mut local := handle.local_database()! |
| 157 | for name in fixture.names { |
| 158 | installed := local.get_pkg(name) or { return error('missing signed installation') } |
| 159 | assert installed.validation == 12 |
| 160 | assert os.read_file(os.join_path(fixture.root, 'usr/bin', name))! == 'binary' |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | struct InstallationGuardTrace { |
| 165 | mut: |
| 166 | handle &Handle |
| 167 | protected bool |
| 168 | succeeded bool |
| 169 | } |
| 170 | |
| 171 | fn installation_guard(context voidptr, event CommitEvent) { |
| 172 | mut trace := unsafe { &InstallationGuardTrace(context) } |
| 173 | if event.kind in [.package_install_done, .transaction_done] { trace.succeeded = true } |
| 174 | if event.kind != .package_install_start { return } |
| 175 | trace.handle.release_transaction() or { trace.protected = err.code() == int(Errno.trans_not_null) } |
| 176 | assert trace.protected |
| 177 | if _ := trace.handle.commit_transaction() { |
| 178 | assert false |
| 179 | } |
| 180 | assert trace.handle.errno() == .trans_not_prepared |
| 181 | } |
| 182 | |
| 183 | fn test_failed_installation_has_no_database_record_cache_entry_or_success_event() ! { |
| 184 | base := os.join_path(os.vtmp_dir(), 'native-install-write-failure-${os.getpid()}') |
| 185 | defer { os.rmdir_all(base) or { eprintln(err) } } |
| 186 | fixture := installfixtures.create(base, installfixtures.Scenario{ name: 'write-failure', installed: false, missing_local: true, payload: 'payload'.repeat(4096) })! |
| 187 | mut handle := initialize(fixture.root, fixture.db)! |
| 188 | defer { handle.release() or { eprintln(err) } } |
| 189 | handle.add_string_list(.cache_dirs, fixture.cache)! |
| 190 | handle.start_transaction(.no_deps | .no_conflicts | .no_hooks | .no_scriptlet)! |
| 191 | defer { handle.release_transaction() or { eprintln(err) } } |
| 192 | handle.add_package(handle.load_package(fixture.archives[0], true, 0)!)! |
| 193 | assert handle.prepare_transaction()!.success |
| 194 | mut state := &InstallationGuardTrace{ handle: handle } |
| 195 | handle.set_commit_callback(installation_guard, state)! |
| 196 | child := C.fork() |
| 197 | assert child >= 0 |
| 198 | if child == 0 { |
| 199 | if C.v_alpm_limit_database_write() != 0 { C._exit(2) } |
| 200 | handle.commit_transaction() or { |
| 201 | if err.code() != int(Errno.trans_abort) { C._exit(3) } |
| 202 | if handle.transaction.state != .interrupted || handle.execution_active || !state.protected || state.succeeded { |
| 203 | C._exit(4) |
| 204 | } |
| 205 | if 'app' in handle.local.by_name { C._exit(5) } |
| 206 | if os.exists(os.join_path(fixture.db, 'local', 'app-2-1')) { C._exit(6) } |
| 207 | C._exit(0) |
| 208 | return |
| 209 | } |
| 210 | C._exit(7) |
| 211 | } |
| 212 | mut status := i32(0) |
| 213 | for { |
| 214 | if C.v_alpm_wait_child(child, &status, 0) >= 0 { break } |
| 215 | if C.errno != C.EINTR { return os.last_error() } |
| 216 | } |
| 217 | assert C.v_alpm_exit_status(status) == 0 |
| 218 | assert !os.exists(os.join_path(fixture.db, 'local', 'app-2-1')) |
| 219 | } |
| 220 | |