| 1 | module coracle |
| 2 | |
| 3 | import nspkg as facade |
| 4 | import nspkg.engine |
| 5 | import nspkg.tests.archivefixtures |
| 6 | import os |
| 7 | |
| 8 | struct CachedDatabaseTrace { |
| 9 | mut: |
| 10 | items []string |
| 11 | } |
| 12 | |
| 13 | fn cached_database_reference_event(context voidptr, event &Event) { |
| 14 | if unsafe { event.type_ } == .database_missing { |
| 15 | mut trace := unsafe { &CachedDatabaseTrace(context) } |
| 16 | trace.items << 'event:missing' |
| 17 | } |
| 18 | } |
| 19 | |
| 20 | fn cached_database_public_event(context voidptr, event &facade.Event) { |
| 21 | cached_database_reference_event(context, unsafe { &Event(event) }) |
| 22 | } |
| 23 | |
| 24 | fn cached_database_core_event(context voidptr, event engine.RetrievalEvent) { |
| 25 | if event.kind == .database_missing { |
| 26 | mut trace := unsafe { &CachedDatabaseTrace(context) } |
| 27 | trace.items << 'event:missing' |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | fn cached_database_bytes(text string) []u8 { |
| 32 | return archivefixtures.tar_bytes([archivefixtures.Entry{ |
| 33 | name: 'app-1-1/desc' |
| 34 | text: text |
| 35 | }]) |
| 36 | } |
| 37 | |
| 38 | fn cached_database_valid_bytes() []u8 { |
| 39 | return cached_database_bytes('%NAME%\napp\n\n%VERSION%\n1-1\n\n%FILENAME%\napp.pkg.tar\n\n%GROUPS%\ntools\n\n') |
| 40 | } |
| 41 | |
| 42 | fn cached_database_fixture(root string, scenario string) ! { |
| 43 | os.mkdir_all(os.join_path(root, 'keys'))! |
| 44 | if scenario == 'parent-file' { |
| 45 | os.write_file(os.join_path(root, 'sync'), 'keep')! |
| 46 | return |
| 47 | } |
| 48 | os.mkdir(os.join_path(root, 'sync'))! |
| 49 | path := os.join_path(root, 'sync/core.db') |
| 50 | match scenario { |
| 51 | 'missing' {} |
| 52 | 'empty' { os.write_file(path, '')! } |
| 53 | 'garbage' { os.write_file(path, '\xff\x00not-an-archive')! } |
| 54 | 'directory' { os.mkdir(path)! } |
| 55 | 'dangling' { os.symlink('absent', path)! } |
| 56 | 'loop' { os.symlink('core.db', path)! } |
| 57 | 'link' { |
| 58 | os.write_file_array(os.join_path(root, 'real.db'), cached_database_valid_bytes())! |
| 59 | os.symlink('../real.db', path)! |
| 60 | } |
| 61 | 'bad-metadata' { |
| 62 | os.write_file_array(path, cached_database_bytes('%FILENAME%\n/illegal\n\n'))! |
| 63 | } |
| 64 | 'bad-data' { os.write_file_array(path, cached_database_bytes('%DATA%\ninvalid\n\n'))! } |
| 65 | 'header-truncated' { |
| 66 | bytes := archivefixtures.tar_bytes([ |
| 67 | archivefixtures.Entry{ name: 'app-1-1/desc', text: '%FILENAME%\napp.pkg.tar\n\n%GROUPS%\ntools\n\n' }, |
| 68 | archivefixtures.Entry{ name: 'next-1-1/desc', text: '%FILENAME%\nnext.pkg.tar\n\n' }, |
| 69 | ]) |
| 70 | os.write_file_array(path, bytes[..1124])! |
| 71 | } |
| 72 | 'data-truncated' { |
| 73 | bytes := cached_database_bytes('%DESC%\n' + 'x'.repeat(4096) + '\n\n') |
| 74 | os.write_file_array(path, bytes[..612])! |
| 75 | } |
| 76 | else { |
| 77 | os.write_file_array(path, cached_database_valid_bytes())! |
| 78 | if scenario == 'unreadable' { os.chmod(path, 0)! } |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | fn cached_database_repair(root string, scenario string) ! { |
| 84 | path := os.join_path(root, 'sync/core.db') |
| 85 | if scenario == 'parent-file' { |
| 86 | assert os.read_file(os.join_path(root, 'sync'))! == 'keep' |
| 87 | os.rm(os.join_path(root, 'sync'))! |
| 88 | os.mkdir(os.join_path(root, 'sync'))! |
| 89 | } else { |
| 90 | if scenario == 'directory' { |
| 91 | os.rmdir(path)! |
| 92 | } else if scenario != 'missing' { |
| 93 | os.rm(path)! |
| 94 | } |
| 95 | } |
| 96 | os.write_file_array(path, cached_database_valid_bytes())! |
| 97 | } |
| 98 | |
| 99 | fn cached_database_reference_queries(mut trace CachedDatabaseTrace, mut db DB, handle &Handle) { |
| 100 | mut names := []string{} |
| 101 | mut cache_code := 0 |
| 102 | if values := db.get_pkgcache() { |
| 103 | cache_code = int(handle.errno()) |
| 104 | for pkg in values.pkgs() { names << pkg.get_name() + ':' + pkg.get_version() } |
| 105 | } else { |
| 106 | cache_code = int(handle.errno()) |
| 107 | } |
| 108 | trace.items << 'cache:${cache_code}:${names.join(',')}' |
| 109 | if valid := db.get_valid() { |
| 110 | trace.items << 'valid:${valid}:${int(handle.errno())}' |
| 111 | } else { |
| 112 | trace.items << 'valid:false:${int(handle.errno())}' |
| 113 | } |
| 114 | found := if _ := db.get_pkg('app') { true } else { false } |
| 115 | trace.items << 'lookup:${found}:${int(handle.errno())}' |
| 116 | group := if _ := db.get_group('tools') { true } else { false } |
| 117 | trace.items << 'group:${group}:${int(handle.errno())}' |
| 118 | } |
| 119 | |
| 120 | fn cached_database_reference(root string, scenario string, level int, prior bool) ![]string { |
| 121 | handle := initialize('/', root)! |
| 122 | defer { handle.release() or { eprintln(err) } } |
| 123 | handle.option_set_gpgdir(os.join_path(root, 'keys'))! |
| 124 | mut trace := &CachedDatabaseTrace{} |
| 125 | handle.option_set_eventcb(cached_database_reference_event, trace)! |
| 126 | if prior { |
| 127 | if _ := handle.register_syncdb('', 0) { |
| 128 | assert false |
| 129 | } |
| 130 | } |
| 131 | mut db := handle.register_syncdb('core', level)! |
| 132 | trace.items << 'registered:${int(handle.errno())}' |
| 133 | if valid := db.get_valid() { |
| 134 | trace.items << 'initial-valid:${valid}:${int(handle.errno())}' |
| 135 | } else { |
| 136 | trace.items << 'initial-valid:false:${int(handle.errno())}' |
| 137 | } |
| 138 | for _ in 0 .. 2 { cached_database_reference_queries(mut trace, mut db, handle) } |
| 139 | cached_database_repair(root, scenario)! |
| 140 | trace.items << 'repaired' |
| 141 | cached_database_reference_queries(mut trace, mut db, handle) |
| 142 | db.unregister()! |
| 143 | mut fresh := handle.register_syncdb('core', 0)! |
| 144 | trace.items << 'fresh:${int(handle.errno())}' |
| 145 | cached_database_reference_queries(mut trace, mut fresh, handle) |
| 146 | return trace.items.clone() |
| 147 | } |
| 148 | |
| 149 | fn cached_database_core_queries(mut trace CachedDatabaseTrace, mut db engine.Database, handle &engine.Handle) { |
| 150 | mut names := []string{} |
| 151 | mut cache_code := 0 |
| 152 | if values := db.package_cache() { |
| 153 | cache_code = int(handle.errno()) |
| 154 | for pkg in values { names << pkg.name + ':' + pkg.version } |
| 155 | } else { |
| 156 | cache_code = int(handle.errno()) |
| 157 | } |
| 158 | trace.items << 'cache:${cache_code}:${names.join(',')}' |
| 159 | if valid := db.get_valid() { |
| 160 | trace.items << 'valid:${valid}:${int(handle.errno())}' |
| 161 | } else { |
| 162 | trace.items << 'valid:false:${int(handle.errno())}' |
| 163 | } |
| 164 | found := if _ := db.get_pkg('app') { true } else { false } |
| 165 | trace.items << 'lookup:${found}:${int(handle.errno())}' |
| 166 | group := if _ := db.get_group('tools') { true } else { false } |
| 167 | trace.items << 'group:${group}:${int(handle.errno())}' |
| 168 | } |
| 169 | |
| 170 | fn cached_database_core(root string, scenario string, level int, prior bool) ![]string { |
| 171 | mut handle := engine.initialize('/', root)! |
| 172 | defer { handle.release() or { eprintln(err) } } |
| 173 | handle.set_string_option(.gpgdir, os.join_path(root, 'keys'))! |
| 174 | mut trace := &CachedDatabaseTrace{} |
| 175 | handle.set_retrieval_callback(cached_database_core_event, trace)! |
| 176 | if prior { |
| 177 | if _ := handle.register_syncdb('', 0) { |
| 178 | assert false |
| 179 | } |
| 180 | } |
| 181 | mut db := handle.register_syncdb('core', u32(level))! |
| 182 | trace.items << 'registered:${int(handle.errno())}' |
| 183 | if valid := db.get_valid() { |
| 184 | trace.items << 'initial-valid:${valid}:${int(handle.errno())}' |
| 185 | } else { |
| 186 | trace.items << 'initial-valid:false:${int(handle.errno())}' |
| 187 | } |
| 188 | for _ in 0 .. 2 { cached_database_core_queries(mut trace, mut db, handle) } |
| 189 | cached_database_repair(root, scenario)! |
| 190 | trace.items << 'repaired' |
| 191 | cached_database_core_queries(mut trace, mut db, handle) |
| 192 | db.unregister()! |
| 193 | mut fresh := handle.register_syncdb('core', 0)! |
| 194 | trace.items << 'fresh:${int(handle.errno())}' |
| 195 | cached_database_core_queries(mut trace, mut fresh, handle) |
| 196 | return trace.items.clone() |
| 197 | } |
| 198 | |
| 199 | fn cached_database_public_queries(mut trace CachedDatabaseTrace, mut db facade.DB, handle &facade.Handle) { |
| 200 | mut names := []string{} |
| 201 | mut cache_code := 0 |
| 202 | if values := db.get_pkgcache() { |
| 203 | cache_code = int(handle.errno()) |
| 204 | for pkg in values.pkgs() { names << pkg.get_name() + ':' + pkg.get_version() } |
| 205 | } else { |
| 206 | cache_code = int(handle.errno()) |
| 207 | } |
| 208 | trace.items << 'cache:${cache_code}:${names.join(',')}' |
| 209 | if valid := db.get_valid() { |
| 210 | trace.items << 'valid:${valid}:${int(handle.errno())}' |
| 211 | } else { |
| 212 | trace.items << 'valid:false:${int(handle.errno())}' |
| 213 | } |
| 214 | found := if _ := db.get_pkg('app') { true } else { false } |
| 215 | trace.items << 'lookup:${found}:${int(handle.errno())}' |
| 216 | group := if _ := db.get_group('tools') { true } else { false } |
| 217 | trace.items << 'group:${group}:${int(handle.errno())}' |
| 218 | } |
| 219 | |
| 220 | fn cached_database_public(root string, scenario string, level int, prior bool) ![]string { |
| 221 | handle := facade.initialize('/', root)! |
| 222 | defer { handle.release() or { eprintln(err) } } |
| 223 | handle.option_set_gpgdir(os.join_path(root, 'keys'))! |
| 224 | mut trace := &CachedDatabaseTrace{} |
| 225 | handle.option_set_eventcb(cached_database_public_event, trace)! |
| 226 | if prior { |
| 227 | if _ := handle.register_syncdb('', 0) { |
| 228 | assert false |
| 229 | } |
| 230 | } |
| 231 | mut db := handle.register_syncdb('core', level)! |
| 232 | trace.items << 'registered:${int(handle.errno())}' |
| 233 | if valid := db.get_valid() { |
| 234 | trace.items << 'initial-valid:${valid}:${int(handle.errno())}' |
| 235 | } else { |
| 236 | trace.items << 'initial-valid:false:${int(handle.errno())}' |
| 237 | } |
| 238 | for _ in 0 .. 2 { cached_database_public_queries(mut trace, mut db, handle) } |
| 239 | cached_database_repair(root, scenario)! |
| 240 | trace.items << 'repaired' |
| 241 | cached_database_public_queries(mut trace, mut db, handle) |
| 242 | db.unregister()! |
| 243 | mut fresh := handle.register_syncdb('core', 0)! |
| 244 | trace.items << 'fresh:${int(handle.errno())}' |
| 245 | cached_database_public_queries(mut trace, mut fresh, handle) |
| 246 | return trace.items.clone() |
| 247 | } |
| 248 | |
| 249 | fn test_cached_database_failures_revalidation_and_re_registration_match_c() ! { |
| 250 | base := os.join_path(os.vtmp_dir(), 'alpm-cached-db-state-${os.getpid()}') |
| 251 | os.mkdir_all(base)! |
| 252 | defer { os.rmdir_all(base) or { eprintln(err) } } |
| 253 | mut mismatches := []string{} |
| 254 | for scenario in ['valid', 'empty', 'garbage', 'directory', 'unreadable', 'missing', 'dangling', |
| 255 | 'link', 'loop', 'parent-file', 'bad-metadata', 'bad-data', 'header-truncated', 'data-truncated'] { |
| 256 | for level in [0, 1024, 3072] { |
| 257 | for prior in [false, true] { |
| 258 | mut expected := []string{} |
| 259 | for backend in ['reference', 'core', 'public'] { |
| 260 | root := os.join_path(base, scenario, level.str(), prior.str(), backend) |
| 261 | cached_database_fixture(root, scenario)! |
| 262 | result := match backend { |
| 263 | 'reference' { cached_database_reference(root, scenario, level, prior)! } |
| 264 | 'core' { cached_database_core(root, scenario, level, prior)! } |
| 265 | else { cached_database_public(root, scenario, level, prior)! } |
| 266 | } |
| 267 | if backend == 'reference' { |
| 268 | expected = result.clone() |
| 269 | } else { |
| 270 | mut wanted := expected.clone() |
| 271 | if scenario == 'missing' { |
| 272 | // source caches an empty group list after failure and then clears errno. |
| 273 | assert expected.filter(it == 'group:false:0').len == 2 |
| 274 | assert result.filter(it == 'group:false:15').len == 3 |
| 275 | wanted = expected.map(if it == 'group:false:0' { |
| 276 | 'group:false:15' |
| 277 | } else { |
| 278 | it |
| 279 | }) |
| 280 | } else if scenario == 'header-truncated' && level != 1024 { |
| 281 | // a failed group load must not hide groups once the archive is repaired. |
| 282 | assert expected.contains('cache:51:') |
| 283 | assert expected.filter(it == 'group:false:0').len == 1 |
| 284 | assert result.filter(it == 'group:true:0').len == 2 |
| 285 | wanted = expected.map(if it == 'group:false:0' { |
| 286 | 'group:true:0' |
| 287 | } else { |
| 288 | it |
| 289 | }) |
| 290 | } |
| 291 | if result != wanted { |
| 292 | mismatches << '${scenario}/${level}/${prior}/${backend}:\n${result}\n!=\n${wanted}' |
| 293 | } |
| 294 | } |
| 295 | } |
| 296 | } |
| 297 | } |
| 298 | } |
| 299 | assert mismatches.len == 0, mismatches.join('\n') |
| 300 | } |
| 301 | |
| 302 | fn test_sync_archive_descriptors_close_after_success_and_failure() ! { |
| 303 | $if !linux { |
| 304 | return |
| 305 | } |
| 306 | base := os.join_path(os.vtmp_dir(), 'alpm-cached-db-descriptors-${os.getpid()}') |
| 307 | os.mkdir_all(base)! |
| 308 | defer { os.rmdir_all(base) or { eprintln(err) } } |
| 309 | for scenario in ['valid', 'garbage', 'directory', 'header-truncated', 'data-truncated'] { |
| 310 | root := os.join_path(base, scenario) |
| 311 | cached_database_fixture(root, scenario)! |
| 312 | mut handle := engine.initialize('/', root)! |
| 313 | defer { handle.release() or { eprintln(err) } } |
| 314 | // warm up archive support before measuring repeated owned descriptor lifetimes. |
| 315 | mut warm := handle.register_syncdb('core', 0)! |
| 316 | warm.package_cache() or { assert scenario != 'valid' } |
| 317 | warm.unregister()! |
| 318 | before := os.ls('/proc/self/fd')!.len |
| 319 | for _ in 0 .. 32 { |
| 320 | mut db := handle.register_syncdb('core', 0)! |
| 321 | if packages := db.package_cache() { |
| 322 | assert scenario == 'valid' |
| 323 | assert packages.len == 1 |
| 324 | } else { |
| 325 | assert scenario != 'valid' |
| 326 | assert int(handle.errno()) == match scenario { |
| 327 | 'header-truncated' { 51 } |
| 328 | 'data-truncated' { 16 } |
| 329 | else { 11 } |
| 330 | } |
| 331 | } |
| 332 | db.unregister()! |
| 333 | } |
| 334 | assert os.ls('/proc/self/fd')!.len == before, scenario |
| 335 | } |
| 336 | } |
| 337 | |