| 1 | module engine |
| 2 | |
| 3 | import nspkg.tests.installfixtures |
| 4 | import os |
| 5 | |
| 6 | enum ActionFault { |
| 7 | archive_removed |
| 8 | payload_directory |
| 9 | database_entry |
| 10 | metadata_link |
| 11 | } |
| 12 | |
| 13 | struct ActionFaultTrace { |
| 14 | fault ActionFault |
| 15 | archive string |
| 16 | payload string |
| 17 | entry string |
| 18 | sentinel string |
| 19 | mut: |
| 20 | injected bool |
| 21 | } |
| 22 | |
| 23 | fn inject_action_fault(context voidptr, event CommitEvent) { |
| 24 | mut trace := unsafe { &ActionFaultTrace(context) } |
| 25 | if event.kind != .package_install_start || trace.injected { return } |
| 26 | match trace.fault { |
| 27 | .archive_removed { os.rm(trace.archive) or { assert false, err.msg() } } |
| 28 | .payload_directory { os.mkdir(trace.payload) or { assert false, err.msg() } } |
| 29 | .database_entry { os.write_file(trace.entry, 'occupied') or { assert false, err.msg() } } |
| 30 | .metadata_link { return } |
| 31 | } |
| 32 | trace.injected = true |
| 33 | } |
| 34 | |
| 35 | fn inject_action_metadata_fault(context voidptr, event CommitProgress) { |
| 36 | mut trace := unsafe { &ActionFaultTrace(context) } |
| 37 | if trace.fault != .metadata_link || trace.injected || event.kind != .add { return } |
| 38 | if !os.is_dir(trace.entry) { return } |
| 39 | os.symlink(trace.sentinel, os.join_path(trace.entry, 'desc')) or { assert false, err.msg() } |
| 40 | trace.injected = true |
| 41 | } |
| 42 | |
| 43 | fn test_commit_failure_logs_cannot_claim_success_or_replace_the_transaction_error() ! { |
| 44 | base := os.join_path(os.vtmp_dir(), 'native-action-failures-${os.getpid()}') |
| 45 | defer { os.rmdir_all(base) or { eprintln(err) } } |
| 46 | for fault in [ActionFault.archive_removed, .payload_directory, .database_entry, .metadata_link] { |
| 47 | directory := os.join_path(base, fault.str()) |
| 48 | fixture := installfixtures.create(directory, installfixtures.Scenario{ name: fault.str(), installed: false, missing_local: true, new_backup: false })! |
| 49 | mut handle := initialize(fixture.root, fixture.db)! |
| 50 | logfile := if fault == .archive_removed { |
| 51 | os.join_path(directory, 'absent', 'actions.log') |
| 52 | } else { |
| 53 | os.join_path(directory, 'actions.log') |
| 54 | } |
| 55 | handle.set_string_option(.logfile, logfile)! |
| 56 | mut trace := &ActionFaultTrace{ |
| 57 | fault: fault |
| 58 | archive: fixture.archives[0] |
| 59 | payload: os.join_path(fixture.root, 'usr/bin/app') |
| 60 | entry: os.join_path(fixture.db, 'local', 'app-2-1') |
| 61 | sentinel: os.join_path(directory, 'outside-sentinel') |
| 62 | } |
| 63 | os.write_file(trace.sentinel, 'untouched')! |
| 64 | handle.set_commit_callback(inject_action_fault, trace)! |
| 65 | handle.set_commit_progress_callback(inject_action_metadata_fault, trace)! |
| 66 | handle.start_transaction(.no_deps | .no_hooks | .no_scriptlet)! |
| 67 | handle.add_package(handle.load_package(fixture.archives[0], true, 0)!)! |
| 68 | assert handle.prepare_transaction()!.success |
| 69 | mut failed := false |
| 70 | handle.commit_transaction() or { |
| 71 | failed = true |
| 72 | assert err.code() == int(Errno.trans_abort), '${fault}: ${err}' |
| 73 | assert handle.errno() == .trans_abort, '${fault}: ${handle.error()}' |
| 74 | } |
| 75 | assert failed, fault.str() |
| 76 | assert trace.injected |
| 77 | assert !handle.execution_active |
| 78 | assert 'app' !in handle.local.by_name |
| 79 | assert os.read_file(trace.sentinel)! == 'untouched' |
| 80 | if fault == .archive_removed { |
| 81 | assert handle.diagnostics.any(it.starts_with('could not write action log:')) |
| 82 | assert !os.exists(logfile) |
| 83 | } else { |
| 84 | lines := os.read_lines(logfile)!.map(it.all_after('] ').replace(handle.root, 'ROOT/')) |
| 85 | assert lines[0] == '[ALPM] transaction started' |
| 86 | assert lines.last() == '[ALPM] transaction failed' |
| 87 | assert !lines.any(it.starts_with('[ALPM] installed ') || it == '[ALPM] transaction completed') |
| 88 | match fault { |
| 89 | .payload_directory { |
| 90 | assert '[ALPM] error: problem occurred while installing app' in lines |
| 91 | } |
| 92 | .database_entry { |
| 93 | assert '[ALPM] error: could not create database entry app-2-1' in lines |
| 94 | } |
| 95 | .metadata_link { |
| 96 | assert '[ALPM] error: could not update database entry app-2-1' in lines |
| 97 | } |
| 98 | else {} |
| 99 | } |
| 100 | } |
| 101 | handle.release_transaction()! |
| 102 | handle.release()! |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | fn test_preflight_failure_does_not_write_transaction_started_or_failed() ! { |
| 107 | base := os.join_path(os.vtmp_dir(), 'native-action-preflight-${os.getpid()}') |
| 108 | defer { os.rmdir_all(base) or { eprintln(err) } } |
| 109 | fixture := installfixtures.create(base, installfixtures.Scenario{ name: 'preflight', installed: false, missing_local: true })! |
| 110 | os.write_file(os.join_path(fixture.root, 'usr/bin/app'), 'unowned')! |
| 111 | mut handle := initialize(fixture.root, fixture.db)! |
| 112 | defer { handle.release() or { eprintln(err) } } |
| 113 | logfile := os.join_path(base, 'actions.log') |
| 114 | handle.set_string_option(.logfile, logfile)! |
| 115 | handle.start_transaction(.no_deps | .no_hooks | .no_scriptlet)! |
| 116 | defer { handle.release_transaction() or { eprintln(err) } } |
| 117 | handle.add_package(handle.load_package(fixture.archives[0], true, 0)!)! |
| 118 | assert handle.prepare_transaction()!.success |
| 119 | mut failed := false |
| 120 | handle.commit_transaction() or { |
| 121 | failed = true |
| 122 | assert err.code() == int(Errno.file_conflicts) |
| 123 | } |
| 124 | assert failed |
| 125 | assert !os.exists(logfile) |
| 126 | } |
| 127 | |
| 128 | struct RemovalLogFault { |
| 129 | directory string |
| 130 | } |
| 131 | |
| 132 | fn block_action_removal(context voidptr, event CommitEvent) { |
| 133 | if event.kind != .package_remove_start { return } |
| 134 | trace := unsafe { &RemovalLogFault(context) } |
| 135 | os.chmod(trace.directory, 0o500) or { assert false, err.msg() } |
| 136 | } |
| 137 | |
| 138 | fn test_failed_unlink_records_failure_and_keeps_the_installed_database_entry() ! { |
| 139 | // a root caller can bypass the directory's owner write restriction. |
| 140 | if C.geteuid() == 0 { return } |
| 141 | base := os.join_path(os.vtmp_dir(), 'native-action-remove-failure-${os.getpid()}') |
| 142 | defer { os.rmdir_all(base) or { eprintln(err) } } |
| 143 | fixture := installfixtures.create(base, installfixtures.Scenario{ name: 'remove-failure' })! |
| 144 | directory := os.join_path(fixture.root, 'usr/bin') |
| 145 | defer { os.chmod(directory, 0o755) or { eprintln(err) } } |
| 146 | mut handle := initialize(fixture.root, fixture.db)! |
| 147 | defer { handle.release() or { eprintln(err) } } |
| 148 | logfile := os.join_path(base, 'actions.log') |
| 149 | handle.set_string_option(.logfile, logfile)! |
| 150 | handle.set_commit_callback(block_action_removal, &RemovalLogFault{ directory: directory })! |
| 151 | handle.start_transaction(.no_deps | .no_hooks | .no_scriptlet)! |
| 152 | defer { handle.release_transaction() or { eprintln(err) } } |
| 153 | mut local := handle.local_database()! |
| 154 | target := local.get_pkg('app') or { return error('missing removal target') } |
| 155 | handle.remove_package(target)! |
| 156 | assert handle.prepare_transaction()!.success |
| 157 | mut failed := false |
| 158 | handle.commit_transaction() or { |
| 159 | failed = true |
| 160 | assert err.code() == int(Errno.pkg_cant_remove) |
| 161 | assert handle.errno() == .pkg_cant_remove |
| 162 | } |
| 163 | assert failed |
| 164 | assert 'app' in handle.local.by_name |
| 165 | assert os.exists(os.join_path(fixture.db, 'local', 'app-1-1', 'desc')) |
| 166 | lines := os.read_lines(logfile)!.map(it.all_after('] ').replace(handle.root, 'ROOT/')) |
| 167 | assert lines[0] == '[ALPM] transaction started' |
| 168 | assert lines.any(it.starts_with('[ALPM] error: cannot remove ROOT/usr/bin/app (')) |
| 169 | assert lines.last() == '[ALPM] transaction failed' |
| 170 | assert !lines.any(it.starts_with('[ALPM] removed ') || it == '[ALPM] transaction completed') |
| 171 | } |
| 172 | |