| 1 | module coracle |
| 2 | |
| 3 | import nspkg as facade |
| 4 | import nspkg.tests.archivefixtures |
| 5 | import compress.gzip |
| 6 | import crypto.sha256 |
| 7 | import os |
| 8 | |
| 9 | #include "@DIR/../reference_log.h" |
| 10 | |
| 11 | type StreamLogReceiver = fn (voidptr, u32, &u8) |
| 12 | |
| 13 | fn C.v_alpm_reference_log_install(voidptr, StreamLogReceiver, voidptr) voidptr |
| 14 | fn C.v_alpm_reference_log_remove(voidptr, voidptr) |
| 15 | |
| 16 | struct StreamTrace { |
| 17 | root string |
| 18 | mut: |
| 19 | items []string |
| 20 | } |
| 21 | |
| 22 | fn (mut trace StreamTrace) log(level u32, message string) { |
| 23 | if level == 4 && (message.starts_with('returning error ') || message.starts_with('got error ')) { |
| 24 | return |
| 25 | } |
| 26 | mut text := message.replace(trace.root, 'ROOT') |
| 27 | if text.contains(': Error reading fd ') { |
| 28 | at := text.index(': Error reading fd ') or { return } |
| 29 | text = text[..at] + ': Error reading fd <fd>\n' |
| 30 | } |
| 31 | trace.items << 'log:${level}:' + text |
| 32 | } |
| 33 | |
| 34 | fn stream_reference_log(context voidptr, level u32, message &u8) { |
| 35 | mut trace := unsafe { &StreamTrace(context) } |
| 36 | trace.log(level, unsafe { cstring_to_vstring(message) }) |
| 37 | } |
| 38 | |
| 39 | fn stream_public_log(context voidptr, level facade.LogLevel, message &u8) { |
| 40 | stream_reference_log(context, u32(level), message) |
| 41 | } |
| 42 | |
| 43 | fn stream_close_fd(path string) !bool { |
| 44 | for name in os.ls('/proc/self/fd')! { |
| 45 | if os.readlink('/proc/self/fd/' + name) or { continue } == path { |
| 46 | assert C.close(i32(name.int())) == 0 |
| 47 | return true |
| 48 | } |
| 49 | } |
| 50 | return false |
| 51 | } |
| 52 | |
| 53 | fn stream_assert_closed(root string) ! { |
| 54 | $if linux { |
| 55 | for name in os.ls('/proc/self/fd')! { |
| 56 | path := os.readlink('/proc/self/fd/' + name) or { continue } |
| 57 | assert !path.starts_with(root + '/'), 'stream descriptor left open: ${path}' |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | fn stream_fixture(root string, scenario string, mtree bool) ! { |
| 63 | entry := os.join_path(root, 'db/local/app-1-1') |
| 64 | os.mkdir_all(entry)! |
| 65 | os.mkdir_all(os.join_path(root, 'db/sync'))! |
| 66 | os.write_file(os.join_path(root, 'db/local/ALPM_DB_VERSION'), '9\n')! |
| 67 | os.write_file(os.join_path(entry, 'desc'), '%NAME%\napp\n\n%VERSION%\n1-1\n\n')! |
| 68 | archivefixtures.write(os.join_path(root, 'db/sync/core.db'), [archivefixtures.Entry{ name: 'app-1-1/desc', text: '%NAME%\napp\n\n%VERSION%\n1-1\n\n%FILENAME%\napp.pkg.tar\n\n' }], false)! |
| 69 | text := if scenario == 'fd-error' { |
| 70 | 'x'.repeat(65536) |
| 71 | } else if scenario == 'empty' { |
| 72 | '' |
| 73 | } else { |
| 74 | 'one\n' |
| 75 | } |
| 76 | mut entries := [archivefixtures.Entry{ name: '.PKGINFO', text: 'pkgname = app\npkgver = 1-1\n' }] |
| 77 | if scenario != 'missing' { entries << archivefixtures.Entry{ name: '.CHANGELOG', text: text } } |
| 78 | archivefixtures.write(os.join_path(root, 'app.pkg.tar'), entries, false)! |
| 79 | path := os.join_path(entry, if mtree { 'mtree' } else { 'changelog' }) |
| 80 | if scenario == 'missing' { return } |
| 81 | if scenario == 'directory' { |
| 82 | os.mkdir(path)! |
| 83 | return |
| 84 | } |
| 85 | if mtree { |
| 86 | data := if scenario == 'invalid' { |
| 87 | 'not an mtree' |
| 88 | } else if scenario == 'empty' { |
| 89 | '' |
| 90 | } else if scenario == 'bad-entry' { |
| 91 | '#mtree\n./item type=bogus\n' |
| 92 | } else { |
| 93 | '#mtree\n./item type=file size=7\n' |
| 94 | } |
| 95 | if scenario == 'gzip' { |
| 96 | os.write_file_array(path, gzip.compress(data.bytes())!)! |
| 97 | } else { |
| 98 | os.write_file(path, data)! |
| 99 | } |
| 100 | } else { |
| 101 | os.write_file(path, text)! |
| 102 | } |
| 103 | if scenario == 'unreadable' { os.chmod(path, 0)! } |
| 104 | } |
| 105 | |
| 106 | fn stream_reference(root string, origin string, scenario string, prior bool, mtree bool) ![]string { |
| 107 | handle := initialize('/', os.join_path(root, 'db'))! |
| 108 | defer { handle.release() or { eprintln(err) } } |
| 109 | mut pkg := &Package(unsafe { nil }) |
| 110 | if origin == 'file' { |
| 111 | pkg = handle.pkg_load(os.join_path(root, 'app.pkg.tar'), 0, 0)! |
| 112 | } else if origin == 'local' { |
| 113 | db := handle.get_localdb()! |
| 114 | found := db.get_pkg('app') or { return error('missing local package') } |
| 115 | pkg = found |
| 116 | } else { |
| 117 | db := handle.register_syncdb('core', 0)! |
| 118 | found := db.get_pkg('app') or { return error('missing sync package') } |
| 119 | pkg = found |
| 120 | } |
| 121 | defer { if origin == 'file' { pkg.free_pkg() or { eprintln(err) } } } |
| 122 | mut trace := &StreamTrace{ root: root } |
| 123 | state := C.v_alpm_reference_log_install(handle, stream_reference_log, trace) |
| 124 | defer { C.v_alpm_reference_log_remove(handle, state) } |
| 125 | if prior { |
| 126 | if _ := handle.register_syncdb('', 0) { |
| 127 | assert false |
| 128 | } |
| 129 | } |
| 130 | if origin == 'file' && scenario.starts_with('package-') { |
| 131 | path := os.join_path(root, 'app.pkg.tar') |
| 132 | os.rm(path)! |
| 133 | if scenario == 'package-directory' { |
| 134 | os.mkdir(path)! |
| 135 | } else if scenario == 'package-corrupt' { |
| 136 | os.write_file(path, 'invalid archive')! |
| 137 | } |
| 138 | } |
| 139 | if mtree { |
| 140 | archive := pkg.mtree_open() |
| 141 | trace.items << 'open:${archive != unsafe { nil }}:${int(handle.errno())}' |
| 142 | if archive != unsafe { nil } { |
| 143 | mut entry := &C.Archive_entry(unsafe { nil }) |
| 144 | for _ in 0 .. 8 { |
| 145 | if prior { |
| 146 | if _ := handle.register_syncdb('', 0) { |
| 147 | assert false |
| 148 | } |
| 149 | } |
| 150 | if scenario == 'legacy' { |
| 151 | if _ := pkg.mtree_next(archive, entry) { |
| 152 | trace.items << 'legacy:ok:${int(handle.errno())}' |
| 153 | continue |
| 154 | } else { |
| 155 | trace.items << 'legacy:error:${err.code()}:${int(handle.errno())}' |
| 156 | break |
| 157 | } |
| 158 | } |
| 159 | if next := pkg.mtree_step(archive, &entry) { |
| 160 | trace.items << 'next:${next}:${int(handle.errno())}' |
| 161 | if !next { break } |
| 162 | } else { |
| 163 | trace.items << 'next-error:${err.code()}:${int(handle.errno())}:${err.msg()}' |
| 164 | break |
| 165 | } |
| 166 | } |
| 167 | if prior { |
| 168 | if _ := handle.register_syncdb('', 0) { |
| 169 | assert false |
| 170 | } |
| 171 | } |
| 172 | if _ := pkg.mtree_close(archive) { |
| 173 | trace.items << 'close:ok:${int(handle.errno())}' |
| 174 | } else { |
| 175 | trace.items << 'close:error:${err.code()}:${int(handle.errno())}' |
| 176 | } |
| 177 | } |
| 178 | return trace.items |
| 179 | } |
| 180 | stream := pkg.changelog_open() |
| 181 | trace.items << 'open:${stream != unsafe { nil }}:${int(handle.errno())}' |
| 182 | if stream == unsafe { nil } { return trace.items } |
| 183 | if origin == 'local' && scenario == 'append' { |
| 184 | os.write_file(os.join_path(root, 'db/local/app-1-1/changelog'), 'one\ntwo\n')! |
| 185 | } |
| 186 | if scenario == 'fd-error' { |
| 187 | target := if origin == 'local' { |
| 188 | os.join_path(root, 'db/local/app-1-1/changelog') |
| 189 | } else { |
| 190 | os.join_path(root, 'app.pkg.tar') |
| 191 | } |
| 192 | trace.items << 'closed-fd:${stream_close_fd(target)!}' |
| 193 | } |
| 194 | mut data := []u8{} |
| 195 | mut buffer := []u8{len: 4096} |
| 196 | for _ in 0 .. 40 { |
| 197 | if prior { |
| 198 | if _ := handle.register_syncdb('', 0) { |
| 199 | assert false |
| 200 | } |
| 201 | } |
| 202 | count := pkg.changelog_read(buffer.data, uint(buffer.len), stream) |
| 203 | trace.items << 'read:${count}:${int(handle.errno())}' |
| 204 | assert count <= uint(buffer.len) |
| 205 | if count == 0 { break } |
| 206 | data << buffer[..int(count)] |
| 207 | } |
| 208 | trace.items << 'data:${data.len}:${sha256.sum(data).hex()}' |
| 209 | if prior { |
| 210 | if _ := handle.register_syncdb('', 0) { |
| 211 | assert false |
| 212 | } |
| 213 | } |
| 214 | if _ := pkg.changelog_close(stream) { |
| 215 | trace.items << 'close:ok:${int(handle.errno())}' |
| 216 | } else { |
| 217 | trace.items << 'close:error:${err.code()}:${int(handle.errno())}' |
| 218 | } |
| 219 | return trace.items |
| 220 | } |
| 221 | |
| 222 | fn stream_public(root string, origin string, scenario string, prior bool, mtree bool) ![]string { |
| 223 | handle := facade.initialize('/', os.join_path(root, 'db'))! |
| 224 | defer { handle.release() or { eprintln(err) } } |
| 225 | mut pkg := &facade.Package(unsafe { nil }) |
| 226 | if origin == 'file' { |
| 227 | pkg = handle.pkg_load(os.join_path(root, 'app.pkg.tar'), 0, 0)! |
| 228 | } else if origin == 'local' { |
| 229 | db := handle.get_localdb()! |
| 230 | found := db.get_pkg('app') or { return error('missing local package') } |
| 231 | pkg = found |
| 232 | } else { |
| 233 | db := handle.register_syncdb('core', 0)! |
| 234 | found := db.get_pkg('app') or { return error('missing sync package') } |
| 235 | pkg = found |
| 236 | } |
| 237 | defer { if origin == 'file' { pkg.free_pkg() or { eprintln(err) } } } |
| 238 | mut trace := &StreamTrace{ root: root } |
| 239 | handle.option_set_logcb_line(stream_public_log, trace)! |
| 240 | if prior { |
| 241 | if _ := handle.register_syncdb('', 0) { |
| 242 | assert false |
| 243 | } |
| 244 | } |
| 245 | if origin == 'file' && scenario.starts_with('package-') { |
| 246 | path := os.join_path(root, 'app.pkg.tar') |
| 247 | os.rm(path)! |
| 248 | if scenario == 'package-directory' { |
| 249 | os.mkdir(path)! |
| 250 | } else if scenario == 'package-corrupt' { |
| 251 | os.write_file(path, 'invalid archive')! |
| 252 | } |
| 253 | } |
| 254 | if mtree { |
| 255 | archive := pkg.mtree_open() |
| 256 | trace.items << 'open:${archive != unsafe { nil }}:${int(handle.errno())}' |
| 257 | if archive != unsafe { nil } { |
| 258 | mut entry := &C.Archive_entry(unsafe { nil }) |
| 259 | for _ in 0 .. 8 { |
| 260 | if prior { |
| 261 | if _ := handle.register_syncdb('', 0) { |
| 262 | assert false |
| 263 | } |
| 264 | } |
| 265 | if scenario == 'legacy' { |
| 266 | if _ := pkg.mtree_next(archive, entry) { |
| 267 | trace.items << 'legacy:ok:${int(handle.errno())}' |
| 268 | continue |
| 269 | } else { |
| 270 | trace.items << 'legacy:error:${err.code()}:${int(handle.errno())}' |
| 271 | break |
| 272 | } |
| 273 | } |
| 274 | if next := pkg.mtree_step(archive, &entry) { |
| 275 | trace.items << 'next:${next}:${int(handle.errno())}' |
| 276 | if !next { break } |
| 277 | } else { |
| 278 | trace.items << 'next-error:${err.code()}:${int(handle.errno())}:${err.msg()}' |
| 279 | break |
| 280 | } |
| 281 | } |
| 282 | if prior { |
| 283 | if _ := handle.register_syncdb('', 0) { |
| 284 | assert false |
| 285 | } |
| 286 | } |
| 287 | if _ := pkg.mtree_close(archive) { |
| 288 | trace.items << 'close:ok:${int(handle.errno())}' |
| 289 | } else { |
| 290 | trace.items << 'close:error:${err.code()}:${int(handle.errno())}' |
| 291 | } |
| 292 | } |
| 293 | return trace.items |
| 294 | } |
| 295 | stream := pkg.changelog_open() |
| 296 | trace.items << 'open:${stream != unsafe { nil }}:${int(handle.errno())}' |
| 297 | if stream == unsafe { nil } { return trace.items } |
| 298 | if origin == 'local' && scenario == 'append' { |
| 299 | os.write_file(os.join_path(root, 'db/local/app-1-1/changelog'), 'one\ntwo\n')! |
| 300 | } |
| 301 | if scenario == 'fd-error' { |
| 302 | target := if origin == 'local' { |
| 303 | os.join_path(root, 'db/local/app-1-1/changelog') |
| 304 | } else { |
| 305 | os.join_path(root, 'app.pkg.tar') |
| 306 | } |
| 307 | trace.items << 'closed-fd:${stream_close_fd(target)!}' |
| 308 | } |
| 309 | mut data := []u8{} |
| 310 | mut buffer := []u8{len: 4096} |
| 311 | for _ in 0 .. 40 { |
| 312 | if prior { |
| 313 | if _ := handle.register_syncdb('', 0) { |
| 314 | assert false |
| 315 | } |
| 316 | } |
| 317 | count := pkg.changelog_read(buffer.data, uint(buffer.len), stream) |
| 318 | trace.items << 'read:${count}:${int(handle.errno())}' |
| 319 | assert count <= uint(buffer.len) |
| 320 | if count == 0 { break } |
| 321 | data << buffer[..int(count)] |
| 322 | } |
| 323 | trace.items << 'data:${data.len}:${sha256.sum(data).hex()}' |
| 324 | if prior { |
| 325 | if _ := handle.register_syncdb('', 0) { |
| 326 | assert false |
| 327 | } |
| 328 | } |
| 329 | if _ := pkg.changelog_close(stream) { |
| 330 | trace.items << 'close:ok:${int(handle.errno())}' |
| 331 | } else { |
| 332 | trace.items << 'close:error:${err.code()}:${int(handle.errno())}' |
| 333 | } |
| 334 | return trace.items |
| 335 | } |
| 336 | |
| 337 | fn test_changelog_stream_data_errors_and_logs_match_c() ! { |
| 338 | base := os.join_path(os.vtmp_dir(), 'alpm-changelog-streams-${os.getpid()}') |
| 339 | os.mkdir_all(base)! |
| 340 | defer { os.rmdir_all(base) or { eprintln(err) } } |
| 341 | mut mismatches := []string{} |
| 342 | for origin in ['local', 'file', 'sync'] { |
| 343 | for scenario in ['valid', 'empty', 'missing', 'directory', 'unreadable', 'append', 'fd-error', |
| 344 | 'package-missing', 'package-directory', 'package-corrupt'] { |
| 345 | $if !linux { |
| 346 | if scenario == 'fd-error' { continue } |
| 347 | } |
| 348 | if origin != 'file' && scenario.starts_with('package-') { continue } |
| 349 | if origin == 'file' && scenario in ['directory', 'unreadable'] { continue } |
| 350 | for prior in [false, true] { |
| 351 | old_root := os.join_path(base, origin, scenario, prior.str(), 'reference') |
| 352 | new_root := os.join_path(base, origin, scenario, prior.str(), 'public') |
| 353 | stream_fixture(old_root, scenario, false)! |
| 354 | stream_fixture(new_root, scenario, false)! |
| 355 | expected := stream_reference(old_root, origin, scenario, prior, false)! |
| 356 | actual := stream_public(new_root, origin, scenario, prior, false)! |
| 357 | stream_assert_closed(old_root)! |
| 358 | stream_assert_closed(new_root)! |
| 359 | if expected != actual { |
| 360 | mismatches << '${origin}/${scenario}/${prior}:\n${actual}\n!=\n${expected}' |
| 361 | } |
| 362 | } |
| 363 | } |
| 364 | } |
| 365 | assert mismatches.len == 0, mismatches.join('\n') |
| 366 | } |
| 367 | |
| 368 | fn test_mtree_stream_open_next_errno_and_logs_match_c() ! { |
| 369 | base := os.join_path(os.vtmp_dir(), 'alpm-mtree-streams-${os.getpid()}') |
| 370 | os.mkdir_all(base)! |
| 371 | defer { os.rmdir_all(base) or { eprintln(err) } } |
| 372 | mut mismatches := []string{} |
| 373 | for origin in ['local', 'file', 'sync'] { |
| 374 | for scenario in ['valid', 'empty', 'missing', 'directory', 'unreadable', 'invalid', 'gzip', |
| 375 | 'bad-entry', 'legacy'] { |
| 376 | for prior in [false, true] { |
| 377 | old_root := os.join_path(base, origin, scenario, prior.str(), 'reference') |
| 378 | new_root := os.join_path(base, origin, scenario, prior.str(), 'public') |
| 379 | stream_fixture(old_root, scenario, true)! |
| 380 | stream_fixture(new_root, scenario, true)! |
| 381 | expected := stream_reference(old_root, origin, scenario, prior, true)! |
| 382 | actual := stream_public(new_root, origin, scenario, prior, true)! |
| 383 | stream_assert_closed(old_root)! |
| 384 | stream_assert_closed(new_root)! |
| 385 | if expected != actual { |
| 386 | mismatches << '${origin}/${scenario}/${prior}:\n${actual}\n!=\n${expected}' |
| 387 | } |
| 388 | } |
| 389 | } |
| 390 | } |
| 391 | assert mismatches.len == 0, mismatches.join('\n') |
| 392 | } |
| 393 | |