| 1 | module main |
| 2 | |
| 3 | import nspkg as facade |
| 4 | import nspkg.engine |
| 5 | import nspkg.tests.archivefixtures |
| 6 | import nspkg.tests.coracle as reference |
| 7 | import nspkg.tests.installfixtures |
| 8 | import nspkg.tests.localefixtures |
| 9 | import os |
| 10 | |
| 11 | #include "@DIR/reference_log.h" |
| 12 | #include <locale.h> |
| 13 | #include <libintl.h> |
| 14 | |
| 15 | type InstallLogReceiver = fn (voidptr, u32, &u8) |
| 16 | |
| 17 | fn C.v_alpm_reference_log_install(voidptr, InstallLogReceiver, voidptr) voidptr |
| 18 | fn C.v_alpm_reference_log_remove(voidptr, voidptr) |
| 19 | fn C.setlocale(i32, &char) &char |
| 20 | fn C.bindtextdomain(const_domain &char, const_directory &char) &char |
| 21 | |
| 22 | struct InstallLogResult { |
| 23 | mut: |
| 24 | success bool |
| 25 | code int |
| 26 | logs []string |
| 27 | states []int |
| 28 | events []string |
| 29 | event_states []int |
| 30 | recorded bool |
| 31 | } |
| 32 | |
| 33 | struct InstallLogTrace { |
| 34 | root string |
| 35 | scenario string |
| 36 | reenter bool |
| 37 | old &reference.Handle = unsafe { nil } |
| 38 | public &facade.Handle = unsafe { nil } |
| 39 | mut: |
| 40 | native &engine.Handle = unsafe { nil } |
| 41 | result InstallLogResult |
| 42 | injected bool |
| 43 | } |
| 44 | |
| 45 | fn (trace &InstallLogTrace) state() int { |
| 46 | if trace.old != unsafe { nil } { return int(trace.old.errno()) } |
| 47 | if trace.public != unsafe { nil } { return int(trace.public.errno()) } |
| 48 | return int(trace.native.errno()) |
| 49 | } |
| 50 | |
| 51 | fn (mut trace InstallLogTrace) log(level u32, message string) { |
| 52 | if level !in [u32(1), 2] { return } |
| 53 | trace.result.logs << '${level}:' + message.replace(trace.root + '/', 'ROOT/') |
| 54 | trace.result.states << trace.state() |
| 55 | if !trace.reenter { return } |
| 56 | if trace.old != unsafe { nil } { |
| 57 | trace.old.register_syncdb('', 0) or { return } |
| 58 | } else if trace.public != unsafe { nil } { |
| 59 | trace.public.register_syncdb('', 0) or { return } |
| 60 | } else { |
| 61 | trace.native.register_syncdb('', 0) or { return } |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | fn install_reference_log(context voidptr, level u32, message &u8) { |
| 66 | mut trace := unsafe { &InstallLogTrace(context) } |
| 67 | trace.log(level, unsafe { tos_clone(message) }) |
| 68 | } |
| 69 | |
| 70 | fn install_public_log(context voidptr, level facade.LogLevel, message &u8) { |
| 71 | install_reference_log(context, u32(level), message) |
| 72 | } |
| 73 | |
| 74 | fn install_core_log(context voidptr, level engine.LogLevel, message string) { |
| 75 | mut trace := unsafe { &InstallLogTrace(context) } |
| 76 | trace.log(u32(level), message) |
| 77 | } |
| 78 | |
| 79 | fn (mut trace InstallLogTrace) event(kind string) { |
| 80 | trace.result.events << kind |
| 81 | trace.result.event_states << trace.state() |
| 82 | if kind != 'package:start' || !trace.scenario.starts_with('directory-') { return } |
| 83 | path := os.join_path(trace.root, 'usr/bin/app') |
| 84 | if os.exists(path) { os.rm(path) or { panic(err) } } |
| 85 | os.mkdir(path) or { panic(err) } |
| 86 | trace.injected = true |
| 87 | } |
| 88 | |
| 89 | fn install_log_reference_event(context voidptr, event &reference.Event) { |
| 90 | mut trace := unsafe { &InstallLogTrace(context) } |
| 91 | match unsafe { event.type_ } { |
| 92 | .transaction_start { trace.event('transaction:start') } |
| 93 | .transaction_done { trace.event('transaction:done') } |
| 94 | .package_operation_start { trace.event('package:start') } |
| 95 | .package_operation_done { trace.event('package:done') } |
| 96 | else {} |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | fn install_log_public_event(context voidptr, event &facade.Event) { |
| 101 | install_log_reference_event(context, unsafe { &reference.Event(event) }) |
| 102 | } |
| 103 | |
| 104 | fn install_log_core_event(context voidptr, event engine.CommitEvent) { |
| 105 | mut trace := unsafe { &InstallLogTrace(context) } |
| 106 | match event.kind { |
| 107 | .transaction_start { trace.event('transaction:start') } |
| 108 | .transaction_done { trace.event('transaction:done') } |
| 109 | .package_install_start { trace.event('package:start') } |
| 110 | .package_install_done { trace.event('package:done') } |
| 111 | else {} |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | fn install_log_fixture(base string, scenario string) !installfixtures.Fixture { |
| 116 | mut extra := []archivefixtures.Entry{} |
| 117 | if scenario == 'missing-multiple' { |
| 118 | extra = [archivefixtures.Entry{ name: 'omitted/', directory: true }, |
| 119 | archivefixtures.Entry{ name: 'omitted/second', text: 'must not appear' }] |
| 120 | } |
| 121 | return installfixtures.create(base, installfixtures.Scenario{ |
| 122 | name: scenario |
| 123 | installed: scenario == 'directory-upgrade' |
| 124 | missing_local: scenario != 'directory-upgrade' |
| 125 | mtree: scenario.starts_with('missing-') |
| 126 | extra_entries: extra |
| 127 | }) |
| 128 | } |
| 129 | |
| 130 | fn install_log_catalog() { |
| 131 | directory := os.getenv('ALPM_INSTALL_LOG_LOCALE') |
| 132 | assert C.bindtextdomain(c'libalpm', unsafe { &char(directory.str) }) != unsafe { nil } |
| 133 | } |
| 134 | |
| 135 | fn install_log_reference(fixture installfixtures.Fixture, scenario string, reenter bool) !InstallLogResult { |
| 136 | handle := reference.initialize(fixture.root, fixture.db)! |
| 137 | defer { handle.release() or { eprintln(err) } } |
| 138 | install_log_catalog() |
| 139 | handle.option_add_cachedir(fixture.cache)! |
| 140 | if scenario.ends_with('noextract') { handle.option_add_no_extract('*')! } |
| 141 | mut flags := reference.TransFlag.no_deps | .no_hooks | .no_scriptlet |
| 142 | if scenario.ends_with('db-only') { flags.set(.db_only) } |
| 143 | handle.trans_init(flags)! |
| 144 | defer { handle.trans_release() or { eprintln(err) } } |
| 145 | handle.add_pkg(handle.pkg_load(fixture.archives[0], 1, 0)!)! |
| 146 | handle.trans_prepare()!.free() |
| 147 | mut trace := &InstallLogTrace{ root: fixture.root, scenario: scenario, reenter: reenter, old: handle } |
| 148 | handle.option_set_eventcb(install_log_reference_event, trace)! |
| 149 | adapter := C.v_alpm_reference_log_install(handle, install_reference_log, trace) |
| 150 | defer { C.v_alpm_reference_log_remove(handle, adapter) } |
| 151 | mut succeeded := true |
| 152 | handle.trans_commit() or { succeeded = false } |
| 153 | trace.result.success = succeeded |
| 154 | trace.result.code = int(handle.errno()) |
| 155 | trace.result.recorded = os.exists(os.join_path(fixture.db, 'local/app-2-1/desc')) |
| 156 | assert trace.injected == scenario.starts_with('directory-') |
| 157 | return trace.result |
| 158 | } |
| 159 | |
| 160 | fn install_log_public(fixture installfixtures.Fixture, scenario string, reenter bool) !InstallLogResult { |
| 161 | handle := facade.initialize(fixture.root, fixture.db)! |
| 162 | defer { handle.release() or { eprintln(err) } } |
| 163 | install_log_catalog() |
| 164 | handle.option_add_cachedir(fixture.cache)! |
| 165 | if scenario.ends_with('noextract') { handle.option_add_no_extract('*')! } |
| 166 | mut flags := facade.TransFlag.no_deps | .no_hooks | .no_scriptlet |
| 167 | if scenario.ends_with('db-only') { flags.set(.db_only) } |
| 168 | handle.trans_init(flags)! |
| 169 | defer { handle.trans_release() or { eprintln(err) } } |
| 170 | handle.add_pkg(handle.pkg_load(fixture.archives[0], 1, 0)!)! |
| 171 | handle.trans_prepare()!.free() |
| 172 | mut trace := &InstallLogTrace{ root: fixture.root, scenario: scenario, reenter: reenter, public: handle } |
| 173 | handle.option_set_eventcb(install_log_public_event, trace)! |
| 174 | handle.option_set_logcb_line(install_public_log, trace)! |
| 175 | mut succeeded := true |
| 176 | handle.trans_commit() or { succeeded = false } |
| 177 | trace.result.success = succeeded |
| 178 | trace.result.code = int(handle.errno()) |
| 179 | trace.result.recorded = os.exists(os.join_path(fixture.db, 'local/app-2-1/desc')) |
| 180 | assert trace.injected == scenario.starts_with('directory-') |
| 181 | return trace.result |
| 182 | } |
| 183 | |
| 184 | fn install_log_core(fixture installfixtures.Fixture, scenario string, reenter bool) !InstallLogResult { |
| 185 | mut handle := engine.initialize(fixture.root, fixture.db)! |
| 186 | defer { handle.release() or { eprintln(err) } } |
| 187 | install_log_catalog() |
| 188 | handle.add_string_list(.cache_dirs, fixture.cache)! |
| 189 | if scenario.ends_with('noextract') { handle.add_string_list(.no_extract, '*')! } |
| 190 | mut flags := engine.TransactionFlag.no_deps | .no_hooks | .no_scriptlet |
| 191 | if scenario.ends_with('db-only') { flags.set(.db_only) } |
| 192 | handle.start_transaction(flags)! |
| 193 | defer { handle.release_transaction() or { eprintln(err) } } |
| 194 | handle.add_package(handle.load_package(fixture.archives[0], true, 0)!)! |
| 195 | assert handle.prepare_transaction()!.success |
| 196 | mut trace := &InstallLogTrace{ root: fixture.root, scenario: scenario, reenter: reenter, native: handle } |
| 197 | handle.set_commit_callback(install_log_core_event, trace)! |
| 198 | handle.set_log_callback(install_core_log, trace)! |
| 199 | mut succeeded := true |
| 200 | handle.commit_transaction() or { succeeded = false } |
| 201 | trace.result.success = succeeded |
| 202 | trace.result.code = int(handle.errno()) |
| 203 | trace.result.recorded = os.exists(os.join_path(fixture.db, 'local/app-2-1/desc')) |
| 204 | assert trace.injected == scenario.starts_with('directory-') |
| 205 | return trace.result |
| 206 | } |
| 207 | |
| 208 | fn run() ! { |
| 209 | if os.args.len != 3 || os.readlink('/proc/self/ns/mnt')! == os.args[2] { |
| 210 | return error('requires a private mount namespace') |
| 211 | } |
| 212 | assert os.setenv('LC_ALL', 'C', true) == 0 |
| 213 | assert os.setenv('SOURCE_DATE_EPOCH', '1700000000', true) == 0 |
| 214 | base := os.args[1] |
| 215 | locale_dir := os.join_path(base, 'locale') |
| 216 | localefixtures.write(os.join_path(locale_dir, 'zz/LC_MESSAGES/libalpm.mo'), { |
| 217 | 'file not found in file list for package %s. skipping extraction of %s\n': 'missing entry: %2$s in %1$s\n' |
| 218 | 'extract: not overwriting dir with file %s\n': 'preserving directory: %s\n' |
| 219 | 'problem occurred while installing %s\n': 'installation failed: %s\n' |
| 220 | 'problem occurred while upgrading %s\n': 'upgrade failed: %s\n' |
| 221 | })! |
| 222 | assert os.setenv('ALPM_INSTALL_LOG_LOCALE', locale_dir, true) == 0 |
| 223 | assert os.setenv('LANGUAGE', 'zz', true) == 0 |
| 224 | mut cases := 0 |
| 225 | for locale in ['C', 'en_US.UTF-8'] { |
| 226 | assert C.setlocale(C.LC_ALL, unsafe { &char(locale.str) }) != unsafe { nil } |
| 227 | for reenter in [false, true] { |
| 228 | for scenario in ['missing-list', 'missing-noextract', 'missing-db-only', 'missing-multiple', |
| 229 | 'directory-fresh', 'directory-upgrade', 'directory-noextract'] { |
| 230 | path := os.join_path(base, '${locale}-${reenter}-${scenario}') |
| 231 | old := install_log_fixture(os.join_path(path, 'reference'), scenario)! |
| 232 | expected := install_log_reference(old, scenario, reenter)! |
| 233 | failed := scenario in ['directory-fresh', 'directory-upgrade'] |
| 234 | assert expected.success == !failed, '${scenario}: ${expected}' |
| 235 | count := if failed || scenario == 'missing-multiple' { |
| 236 | 3 |
| 237 | } else if scenario in ['missing-list', 'missing-noextract'] { |
| 238 | 1 |
| 239 | } else { |
| 240 | 0 |
| 241 | } |
| 242 | assert expected.logs.len == count, '${scenario}: ${expected}' |
| 243 | if locale != 'C' && count > 0 { |
| 244 | prefix := if failed { '1:preserving directory:' } else { '2:missing entry:' } |
| 245 | assert expected.logs[0].starts_with(prefix), '${scenario}: ${expected}' |
| 246 | } |
| 247 | for backend in ['core', 'public'] { |
| 248 | fixture := install_log_fixture(os.join_path(path, backend), scenario)! |
| 249 | actual := if backend == 'core' { |
| 250 | install_log_core(fixture, scenario, reenter)! |
| 251 | } else { |
| 252 | install_log_public(fixture, scenario, reenter)! |
| 253 | } |
| 254 | context := '${locale}/${reenter}/${scenario}/${backend}\nnative: ${actual}\nreference: ${expected}' |
| 255 | assert actual.logs == expected.logs, context |
| 256 | assert actual.states == expected.states, context |
| 257 | assert actual.success == expected.success && actual.code == expected.code, context |
| 258 | if failed { |
| 259 | // native failures must not record an incomplete install or emit package completion. |
| 260 | assert expected.recorded && !actual.recorded, context |
| 261 | assert 'package:done' in expected.events && 'package:done' !in actual.events, context |
| 262 | assert os.is_dir(os.join_path(fixture.root, 'usr/bin/app')) |
| 263 | } else { |
| 264 | assert actual == expected, context |
| 265 | assert installfixtures.snapshot(fixture.root)! == installfixtures.snapshot(old.root)!, context |
| 266 | assert installfixtures.snapshot(fixture.db)! == installfixtures.snapshot(old.db)!, context |
| 267 | } |
| 268 | } |
| 269 | cases++ |
| 270 | } |
| 271 | } |
| 272 | } |
| 273 | println('${cases} install diagnostic comparisons verified') |
| 274 | } |
| 275 | |
| 276 | fn main() { |
| 277 | run() or { |
| 278 | eprintln(err) |
| 279 | exit(1) |
| 280 | } |
| 281 | } |
| 282 | |