| 1 | module coracle |
| 2 | |
| 3 | import nspkg as facade |
| 4 | import nspkg.engine |
| 5 | import os |
| 6 | |
| 7 | #include "@DIR/../reference_log.h" |
| 8 | |
| 9 | type LocalMetadataLogReceiver = fn (voidptr, u32, &u8) |
| 10 | |
| 11 | fn C.v_alpm_reference_log_install(voidptr, LocalMetadataLogReceiver, voidptr) voidptr |
| 12 | fn C.v_alpm_reference_log_remove(voidptr, voidptr) |
| 13 | |
| 14 | struct LocalMetadataTrace { |
| 15 | base string |
| 16 | mut: |
| 17 | items []string |
| 18 | close_desc bool |
| 19 | did_close bool |
| 20 | } |
| 21 | |
| 22 | fn (mut trace LocalMetadataTrace) log(level u32, message string) { |
| 23 | if trace.close_desc && level == 2 && message.contains("unknown key '%FAULT%'") { |
| 24 | target := os.join_path(trace.base, 'local', 'app-1-1', 'desc') |
| 25 | for name in os.ls('/proc/self/fd') or { |
| 26 | assert false, err.msg() |
| 27 | []string{} |
| 28 | } { |
| 29 | actual := os.readlink('/proc/self/fd/' + name) or { continue } |
| 30 | if actual == target { |
| 31 | assert C.close(i32(name.int())) == 0 |
| 32 | trace.did_close = true |
| 33 | trace.close_desc = false |
| 34 | break |
| 35 | } |
| 36 | } |
| 37 | assert trace.did_close |
| 38 | } |
| 39 | if level !in [u32(1), 2, 8] { return } |
| 40 | if level == 8 && !message.starts_with('loading package data for ') { return } |
| 41 | trace.items << 'log:${level}:' + message.replace(trace.base, 'BASE') |
| 42 | } |
| 43 | |
| 44 | fn local_metadata_reference_log(context voidptr, level u32, message &u8) { |
| 45 | mut trace := unsafe { &LocalMetadataTrace(context) } |
| 46 | trace.log(level, unsafe { cstring_to_vstring(message) }) |
| 47 | } |
| 48 | |
| 49 | fn local_metadata_public_log(context voidptr, level facade.LogLevel, message &u8) { |
| 50 | mut trace := unsafe { &LocalMetadataTrace(context) } |
| 51 | trace.log(u32(level), unsafe { cstring_to_vstring(message) }) |
| 52 | } |
| 53 | |
| 54 | fn local_metadata_fixture(root string, scenario string) ! { |
| 55 | entry := os.join_path(root, 'local', 'app-1-1') |
| 56 | os.mkdir_all(entry)! |
| 57 | os.write_file(os.join_path(root, 'local', 'ALPM_DB_VERSION'), '9\n')! |
| 58 | mut desc := '%NAME%\napp\n\n%VERSION%\n1-1\n\n%DESC%\nkept\n\n%REASON%\n1\n\n' |
| 59 | mut files := '%FILES%\nusr/bin/app\netc/app\n\n%BACKUP%\netc/app\tabc\n\n' |
| 60 | match scenario { |
| 61 | 'unknown-key' { desc += '%FUTURE%\na\nb\n\n' } |
| 62 | 'unknown-line' { desc += 'future line\na\nb\n\n' } |
| 63 | 'name-mismatch' { desc += '%NAME%\nother\n\n' } |
| 64 | 'version-mismatch' { desc += '%VERSION%\n2-1\n\n' } |
| 65 | 'unknown-reason' { desc += '%REASON%\nfuture\n\n' } |
| 66 | 'unknown-validation' { desc += '%VALIDATION%\nnone\nsha256\nfuture\npgp\n\n' } |
| 67 | 'ordered-diagnostics' { |
| 68 | desc += '%NAME%\nother\n\n%FUTURE%\na\n\n%REASON%\nfuture\n\n%VALIDATION%\nwhat\n\n%VERSION%\n2-1\n\n' |
| 69 | } |
| 70 | 'bad-xdata' { desc += '%XDATA%\na=b\ninvalid\nc=d\n\n' } |
| 71 | 'empty-xdata-name' { desc += '%XDATA%\n=value\n\n' } |
| 72 | 'empty-xdata-value' { desc += '%XDATA%\nname=\n\n' } |
| 73 | 'duplicate-desc' { desc += '%DESC%\nlast\n\n' } |
| 74 | 'duplicate-lists' { |
| 75 | desc += '%GROUPS%\na\n\n%GROUPS%\nb\n\n%LICENSE%\nx\n\n%LICENSE%\ny\n\n' |
| 76 | } |
| 77 | 'scalar-eof' { desc = '%DESC%\nvalue' } |
| 78 | 'scalar-newline-eof' { desc = '%DESC%\nvalue\n' } |
| 79 | 'header-eof' { desc = '%DESC%' } |
| 80 | 'header-newline-eof' { desc = '%DESC%\n' } |
| 81 | 'list-eof' { desc = '%GROUPS%\na\nb' } |
| 82 | 'list-newline-eof' { desc = '%GROUPS%\na\nb\n' } |
| 83 | 'empty-desc' { desc = '' } |
| 84 | 'crlf' { desc = '%DESC%\r\nvalue\r\n\r\n%REASON%\n1\r\n\n' } |
| 85 | 'nul-value' { desc += '%DESC%\na\x00b\n\n' } |
| 86 | 'long-1022', 'long-1023', 'long-1024', 'long-2046' { |
| 87 | desc = '%DESC%\n' + 'x'.repeat(scenario.all_after('long-').int()) + '\n\n' |
| 88 | } |
| 89 | 'long-no-newline' { desc = '%DESC%\n' + 'x'.repeat(1023) } |
| 90 | 'long-key' { desc += '%'.repeat(1100) + '\nignored\n\n' } |
| 91 | 'files-long' { files = '%FILES%\n' + 'x'.repeat(1100) + '\n\n' } |
| 92 | 'files-empty' { files = '' } |
| 93 | 'files-eof' { files = '%FILES%\na\nb' } |
| 94 | 'files-unknown' { files = '%FUTURE%\na\n%FILES%\nb\n\n' } |
| 95 | 'files-duplicates' { files += '%FILES%\nlast\n\n%BACKUP%\nother\tdef\n\n' } |
| 96 | 'read-error-scalar' { desc += '%FAULT%\nignored\n\n%DESC%\n' + 'x'.repeat(6000) + '\n\n' } |
| 97 | 'read-error-groups', 'read-error-licenses', 'read-error-depends', |
| 98 | 'read-error-validation', 'read-error-xdata' { |
| 99 | key := scenario.all_after('read-error-').to_upper() |
| 100 | desc += '%FAULT%\nignored\n\n%${key}%\n' |
| 101 | for index in 0 .. 70 { |
| 102 | desc += if key == 'XDATA' { 'key${index}=value' } else { 'value${index}' } |
| 103 | desc += 'x'.repeat(90) + '\n' |
| 104 | } |
| 105 | desc += '\n' |
| 106 | } |
| 107 | else {} |
| 108 | } |
| 109 | if scenario == 'desc-directory' { |
| 110 | os.mkdir(os.join_path(entry, 'desc'))! |
| 111 | } else if scenario == 'desc-dangling' { |
| 112 | os.symlink('absent', os.join_path(entry, 'desc'))! |
| 113 | } else if scenario != 'desc-missing' { |
| 114 | os.write_file(os.join_path(entry, 'desc'), desc)! |
| 115 | if scenario == 'desc-unreadable' { os.chmod(os.join_path(entry, 'desc'), 0)! } |
| 116 | } |
| 117 | if scenario == 'files-dangling' { |
| 118 | os.symlink('absent', os.join_path(entry, 'files'))! |
| 119 | } else if scenario != 'files-missing' { |
| 120 | os.write_file(os.join_path(entry, 'files'), files)! |
| 121 | if scenario == 'files-unreadable' { os.chmod(os.join_path(entry, 'files'), 0)! } |
| 122 | } |
| 123 | os.write_file(os.join_path(entry, 'install'), 'scriptlet')! |
| 124 | } |
| 125 | |
| 126 | fn (mut trace LocalMetadataTrace) result(name string, value string, code int) { |
| 127 | trace.items << 'result:${name}:${value}:${code}' |
| 128 | } |
| 129 | |
| 130 | fn local_metadata_reference_query(mut trace LocalMetadataTrace, pkg &Package, handle &Handle, operation string) { |
| 131 | trace.items << 'call:' + operation |
| 132 | match operation { |
| 133 | 'desc' { |
| 134 | value := pkg.get_desc() |
| 135 | trace.result(operation, value, int(handle.errno())) |
| 136 | } |
| 137 | 'reason' { |
| 138 | value := int(pkg.get_reason()).str() |
| 139 | trace.result(operation, value, int(handle.errno())) |
| 140 | } |
| 141 | 'groups' { |
| 142 | value := pkg.get_groups().strs().join('|') |
| 143 | trace.result(operation, value, int(handle.errno())) |
| 144 | } |
| 145 | 'licenses' { |
| 146 | value := pkg.get_licenses().strs().join('|') |
| 147 | trace.result(operation, value, int(handle.errno())) |
| 148 | } |
| 149 | 'depends' { |
| 150 | value := pkg.get_depends().depends().map(it.compute_string()).join('|') |
| 151 | trace.result(operation, value, int(handle.errno())) |
| 152 | } |
| 153 | 'validation' { |
| 154 | value := int(pkg.get_validation()).str() |
| 155 | trace.result(operation, value, int(handle.errno())) |
| 156 | } |
| 157 | 'files' { |
| 158 | value := pkg.get_files().names().join('|') |
| 159 | trace.result(operation, value, int(handle.errno())) |
| 160 | } |
| 161 | 'xdata' { |
| 162 | mut values := []string{} |
| 163 | mut cursor := pkg.get_xdata() |
| 164 | for cursor != unsafe { nil } { |
| 165 | item := unsafe { &PkgXdata(cursor.data) } |
| 166 | values << cstring_to_vstring_or_none(item.name) + '=' + cstring_to_vstring_or_none(item.value) |
| 167 | cursor = cursor.next |
| 168 | } |
| 169 | trace.result(operation, values.join('|'), int(handle.errno())) |
| 170 | } |
| 171 | 'scriptlet' { |
| 172 | value := pkg.has_scriptlet().str() |
| 173 | trace.result(operation, value, int(handle.errno())) |
| 174 | } |
| 175 | else { |
| 176 | assert false |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | fn local_metadata_public_query(mut trace LocalMetadataTrace, pkg &facade.Package, handle &facade.Handle, operation string) { |
| 182 | trace.items << 'call:' + operation |
| 183 | match operation { |
| 184 | 'desc' { |
| 185 | value := pkg.get_desc() |
| 186 | trace.result(operation, value, int(handle.errno())) |
| 187 | } |
| 188 | 'reason' { |
| 189 | value := int(pkg.get_reason()).str() |
| 190 | trace.result(operation, value, int(handle.errno())) |
| 191 | } |
| 192 | 'groups' { |
| 193 | value := pkg.get_groups().strs().join('|') |
| 194 | trace.result(operation, value, int(handle.errno())) |
| 195 | } |
| 196 | 'licenses' { |
| 197 | value := pkg.get_licenses().strs().join('|') |
| 198 | trace.result(operation, value, int(handle.errno())) |
| 199 | } |
| 200 | 'depends' { |
| 201 | value := pkg.get_depends().depends().map(it.compute_string()).join('|') |
| 202 | trace.result(operation, value, int(handle.errno())) |
| 203 | } |
| 204 | 'validation' { |
| 205 | value := int(pkg.get_validation()).str() |
| 206 | trace.result(operation, value, int(handle.errno())) |
| 207 | } |
| 208 | 'files' { |
| 209 | value := pkg.get_files().names().join('|') |
| 210 | trace.result(operation, value, int(handle.errno())) |
| 211 | } |
| 212 | 'xdata' { |
| 213 | mut values := []string{} |
| 214 | mut cursor := pkg.get_xdata() |
| 215 | for cursor != unsafe { nil } { |
| 216 | item := unsafe { &PkgXdata(cursor.data) } |
| 217 | values << cstring_to_vstring_or_none(item.name) + '=' + cstring_to_vstring_or_none(item.value) |
| 218 | cursor = cursor.next |
| 219 | } |
| 220 | trace.result(operation, values.join('|'), int(handle.errno())) |
| 221 | } |
| 222 | 'scriptlet' { |
| 223 | value := pkg.has_scriptlet().str() |
| 224 | trace.result(operation, value, int(handle.errno())) |
| 225 | } |
| 226 | else { |
| 227 | assert false |
| 228 | } |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | fn test_local_metadata_diagnostics_partial_fields_and_fragment_reads_match_c() ! { |
| 233 | base := os.join_path(os.vtmp_dir(), 'alpm-local-metadata-logs-${os.getpid()}') |
| 234 | os.mkdir_all(base)! |
| 235 | defer { os.rmdir_all(base) or { eprintln(err) } } |
| 236 | scenarios := ['valid', 'unknown-key', 'unknown-line', 'name-mismatch', 'version-mismatch', |
| 237 | 'unknown-reason', 'unknown-validation', 'ordered-diagnostics', 'bad-xdata', 'empty-xdata-name', |
| 238 | 'empty-xdata-value', 'duplicate-desc', 'duplicate-lists', 'scalar-eof', 'scalar-newline-eof', |
| 239 | 'header-eof', 'header-newline-eof', 'list-eof', 'list-newline-eof', 'empty-desc', 'crlf', |
| 240 | 'nul-value', 'long-1022', 'long-1023', 'long-1024', 'long-2046', 'long-no-newline', 'long-key', |
| 241 | 'files-long', 'files-empty', 'files-eof', 'files-unknown', 'files-duplicates', 'desc-missing', |
| 242 | 'desc-directory', 'desc-unreadable', 'desc-dangling', 'files-missing', 'files-unreadable', |
| 243 | 'files-dangling', 'read-error-scalar', 'read-error-groups', 'read-error-licenses', |
| 244 | 'read-error-depends', 'read-error-validation', 'read-error-xdata'] |
| 245 | for scenario in scenarios { |
| 246 | for first in ['desc', 'files', 'scriptlet'] { |
| 247 | for prior in [false, true] { |
| 248 | mut expected := []string{} |
| 249 | for backend in ['reference', 'public'] { |
| 250 | root := os.join_path(base, scenario, first, prior.str(), backend) |
| 251 | local_metadata_fixture(root, scenario)! |
| 252 | mut trace := &LocalMetadataTrace{ base: root, close_desc: scenario.starts_with('read-error-') } |
| 253 | operations := [first, 'desc', 'reason', 'groups', 'licenses', 'depends', |
| 254 | 'validation', 'xdata', 'files', 'scriptlet', 'desc', 'files'] |
| 255 | if backend == 'reference' { |
| 256 | handle := initialize('/', root)! |
| 257 | defer { handle.release() or { eprintln(err) } } |
| 258 | db := handle.get_localdb()! |
| 259 | pkg := db.get_pkg('app') or { return error('missing C package') } |
| 260 | state := C.v_alpm_reference_log_install(handle, local_metadata_reference_log, trace) |
| 261 | defer { C.v_alpm_reference_log_remove(handle, state) } |
| 262 | if prior { |
| 263 | if _ := handle.register_syncdb('', 0) { |
| 264 | assert false |
| 265 | } |
| 266 | } |
| 267 | for operation in operations { |
| 268 | local_metadata_reference_query(mut trace, pkg, handle, operation) |
| 269 | } |
| 270 | assert !scenario.starts_with('read-error-') || trace.did_close |
| 271 | expected = trace.items.clone() |
| 272 | } else { |
| 273 | handle := facade.initialize('/', root)! |
| 274 | defer { handle.release() or { eprintln(err) } } |
| 275 | db := handle.get_localdb()! |
| 276 | pkg := db.get_pkg('app') or { return error('missing native package') } |
| 277 | handle.option_set_logcb_line(local_metadata_public_log, trace)! |
| 278 | if prior { |
| 279 | if _ := handle.register_syncdb('', 0) { |
| 280 | assert false |
| 281 | } |
| 282 | } |
| 283 | for operation in operations { |
| 284 | local_metadata_public_query(mut trace, pkg, handle, operation) |
| 285 | } |
| 286 | assert !scenario.starts_with('read-error-') || trace.did_close |
| 287 | assert trace.items == expected, '${scenario}/${first}/${prior}:\n${trace.items}\n!=\n${expected}' |
| 288 | } |
| 289 | } |
| 290 | } |
| 291 | } |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | fn test_metadata_failure_stays_strict_for_native_transaction_commit() ! { |
| 296 | base := os.join_path(os.vtmp_dir(), 'alpm-local-metadata-strict-${os.getpid()}') |
| 297 | os.mkdir_all(base)! |
| 298 | defer { os.rmdir_all(base) or { eprintln(err) } } |
| 299 | for scenario in ['desc-missing', 'bad-xdata', 'desc-directory', 'files-directory'] { |
| 300 | path := os.join_path(base, scenario) |
| 301 | local_metadata_fixture(path, scenario)! |
| 302 | if scenario == 'files-directory' { |
| 303 | os.rm(os.join_path(path, 'local/app-1-1/files'))! |
| 304 | os.mkdir(os.join_path(path, 'local/app-1-1/files'))! |
| 305 | } |
| 306 | root := os.join_path(path, 'rootfs') |
| 307 | os.mkdir_all(os.join_path(root, 'usr/bin'))! |
| 308 | os.write_file(os.join_path(root, 'usr/bin/app'), 'keep')! |
| 309 | mut handle := engine.initialize(root, path)! |
| 310 | defer { handle.release() or { eprintln(err) } } |
| 311 | mut db := handle.local_database()! |
| 312 | pkg := db.get_pkg('app') or { return error('missing strict native package') } |
| 313 | handle.start_transaction(.no_deps | .no_hooks | .no_scriptlet)! |
| 314 | defer { handle.release_transaction() or { eprintln(err) } } |
| 315 | handle.remove_package(pkg)! |
| 316 | assert handle.prepare_transaction()!.success |
| 317 | if _ := handle.commit_transaction() { |
| 318 | assert false, scenario |
| 319 | } |
| 320 | assert handle.errno() == .db_invalid, scenario |
| 321 | assert os.read_file(os.join_path(root, 'usr/bin/app'))! == 'keep' |
| 322 | assert os.is_dir(os.join_path(path, 'local/app-1-1')) |
| 323 | } |
| 324 | } |
| 325 | |