| 1 | module coracle |
| 2 | |
| 3 | import nspkg as facade |
| 4 | import nspkg.tests.archivefixtures |
| 5 | import os |
| 6 | |
| 7 | #include "@DIR/../reference_log.h" |
| 8 | |
| 9 | $if gcboehm ? { |
| 10 | #include <gc/gc.h> |
| 11 | } |
| 12 | |
| 13 | fn C.GC_gcollect() |
| 14 | |
| 15 | type GroupOwnerLog = fn (voidptr, u32, &u8) |
| 16 | |
| 17 | fn C.v_alpm_reference_log_install(voidptr, GroupOwnerLog, voidptr) voidptr |
| 18 | fn C.v_alpm_reference_log_remove(voidptr, voidptr) |
| 19 | |
| 20 | struct GroupOwnerTrace { |
| 21 | mut: |
| 22 | items []string |
| 23 | } |
| 24 | |
| 25 | struct GroupOwnerContext { |
| 26 | index int |
| 27 | allowed bool |
| 28 | trace &GroupOwnerTrace |
| 29 | expected_owner voidptr |
| 30 | } |
| 31 | |
| 32 | fn group_owner_log(context voidptr, level u32, message &u8) { |
| 33 | if level != 2 { return } |
| 34 | state := unsafe { &GroupOwnerContext(context) } |
| 35 | mut trace := unsafe { state.trace } |
| 36 | trace.items << 'warning:${state.index}:' + unsafe { tos_clone(message) } |
| 37 | } |
| 38 | |
| 39 | fn group_owner_public_log(context voidptr, level facade.LogLevel, message &u8) { |
| 40 | group_owner_log(context, u32(level), message) |
| 41 | } |
| 42 | |
| 43 | fn group_owner_question(context voidptr, question &Question) { |
| 44 | state := unsafe { &GroupOwnerContext(context) } |
| 45 | assert unsafe { question.type_ } == .install_ignorepkg |
| 46 | pkg := unsafe { question.install_ignorepkg.pkg } |
| 47 | if state.expected_owner != unsafe { nil } { |
| 48 | assert voidptr(pkg.get_handle()) == state.expected_owner |
| 49 | } |
| 50 | mut trace := unsafe { state.trace } |
| 51 | trace.items << 'question:${state.index}:${pkg.get_name()}:${pkg.get_version()}' |
| 52 | mut result := unsafe { &QuestionAny(question) } |
| 53 | result.answer = i32(state.allowed) |
| 54 | } |
| 55 | |
| 56 | fn group_owner_public_question(context voidptr, question &facade.Question) { |
| 57 | state := unsafe { &GroupOwnerContext(context) } |
| 58 | assert unsafe { question.type_ } == .install_ignorepkg |
| 59 | pkg := unsafe { question.install_ignorepkg.pkg } |
| 60 | if state.expected_owner != unsafe { nil } { |
| 61 | assert voidptr(pkg.get_handle()) == state.expected_owner |
| 62 | } |
| 63 | mut trace := unsafe { state.trace } |
| 64 | trace.items << 'question:${state.index}:${pkg.get_name()}:${pkg.get_version()}' |
| 65 | mut result := unsafe { &facade.QuestionAny(question) } |
| 66 | result.answer = i32(state.allowed) |
| 67 | } |
| 68 | |
| 69 | fn group_owner_fixture(base string) ! { |
| 70 | for index in 0 .. 2 { |
| 71 | root := os.join_path(base, index.str()) |
| 72 | os.mkdir_all(os.join_path(root, 'sync'))! |
| 73 | os.mkdir_all(os.join_path(root, 'local'))! |
| 74 | os.write_file(os.join_path(root, 'local/ALPM_DB_VERSION'), '9\n')! |
| 75 | names := if index == 0 { ['a', 'shared'] } else { ['b', 'shared'] } |
| 76 | mut entries := []archivefixtures.Entry{} |
| 77 | for name in names { |
| 78 | version := if name == 'shared' && index == 1 { '2-1' } else { '1-1' } |
| 79 | text := '%NAME%\n${name}\n\n%VERSION%\n${version}\n\n%GROUPS%\ntools\n\n' + if name == 'a' { |
| 80 | '%CONFLICTS%\nb\n\n' |
| 81 | } else { |
| 82 | '' |
| 83 | } |
| 84 | path := os.join_path(root, 'local', '${name}-${version}', 'desc') |
| 85 | os.mkdir_all(os.dir(path))! |
| 86 | os.write_file(path, text)! |
| 87 | entries << archivefixtures.Entry{ name: '${name}-${version}/desc', text: text } |
| 88 | } |
| 89 | repo := if index == 0 { 'first' } else { 'second' } |
| 90 | archivefixtures.write(os.join_path(root, 'sync', repo + '.db'), entries, false)! |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | fn group_owner_reference(base string, needed []bool, ignores []bool, allowed bool, release_first bool) ![]string { |
| 95 | first := initialize('/', os.join_path(base, '0'))! |
| 96 | mut first_alive := true |
| 97 | defer { if first_alive { first.release() or { eprintln(err) } } } |
| 98 | second := initialize('/', os.join_path(base, '1'))! |
| 99 | defer { second.release() or { eprintln(err) } } |
| 100 | mut trace := &GroupOwnerTrace{} |
| 101 | first_context := &GroupOwnerContext{ index: 0, allowed: allowed, trace: trace } |
| 102 | second_context := &GroupOwnerContext{ index: 1, allowed: allowed, trace: trace } |
| 103 | state0 := C.v_alpm_reference_log_install(first, group_owner_log, first_context) |
| 104 | mut first_log_live := true |
| 105 | defer { if first_log_live { C.v_alpm_reference_log_remove(first, state0) } } |
| 106 | state1 := C.v_alpm_reference_log_install(second, group_owner_log, second_context) |
| 107 | defer { C.v_alpm_reference_log_remove(second, state1) } |
| 108 | first.option_set_questioncb(group_owner_question, first_context)! |
| 109 | second.option_set_questioncb(group_owner_question, second_context)! |
| 110 | if ignores[0] { first.option_add_ignore_pkg('shared')! } |
| 111 | if ignores[1] { second.option_add_ignore_pkg('shared')! } |
| 112 | db0 := first.register_syncdb('first', 0)! |
| 113 | db1 := second.register_syncdb('second', 0)! |
| 114 | first.trans_init(if needed[0] { TransFlag.nolock | .needed } else { TransFlag.nolock })! |
| 115 | mut first_transaction := true |
| 116 | defer { if first_transaction { first.trans_release() or { eprintln(err) } } } |
| 117 | second.trans_init(if needed[1] { TransFlag.nolock | .needed } else { TransFlag.nolock })! |
| 118 | defer { second.trans_release() or { eprintln(err) } } |
| 119 | mut dbs := &List(unsafe { nil }) |
| 120 | assert (&dbs).append(db0) != unsafe { nil } |
| 121 | assert (&dbs).append(db1) != unsafe { nil } |
| 122 | defer { dbs.free() } |
| 123 | mut result := dbs.find_group_pkgs('tools') |
| 124 | defer { result.free() } |
| 125 | if release_first { |
| 126 | first.trans_release()! |
| 127 | first_transaction = false |
| 128 | C.v_alpm_reference_log_remove(first, state0) |
| 129 | first_log_live = false |
| 130 | first.release()! |
| 131 | first_alive = false |
| 132 | } |
| 133 | for pkg in result.pkgs() { |
| 134 | owner := pkg.get_handle() |
| 135 | db := pkg.get_db()! |
| 136 | index := if voidptr(owner) == voidptr(first) { |
| 137 | 0 |
| 138 | } else if voidptr(owner) == voidptr(second) { |
| 139 | 1 |
| 140 | } else { |
| 141 | -1 |
| 142 | } |
| 143 | identity := (index == 0 && voidptr(db) == voidptr(db0)) || (index == 1 && voidptr(db) == voidptr(db1)) |
| 144 | trace.items << 'package:${index}:${db.get_name()}:${pkg.get_name()}:${pkg.get_version()}:${identity}' |
| 145 | } |
| 146 | return trace.items |
| 147 | } |
| 148 | |
| 149 | fn group_owner_public(base string, needed []bool, ignores []bool, allowed bool, release_first bool) ![]string { |
| 150 | first := facade.initialize('/', os.join_path(base, '0'))! |
| 151 | mut first_alive := true |
| 152 | defer { if first_alive { first.release() or { eprintln(err) } } } |
| 153 | second := facade.initialize('/', os.join_path(base, '1'))! |
| 154 | defer { second.release() or { eprintln(err) } } |
| 155 | mut trace := &GroupOwnerTrace{} |
| 156 | first_context := &GroupOwnerContext{ index: 0, allowed: allowed, trace: trace } |
| 157 | second_context := &GroupOwnerContext{ index: 1, allowed: allowed, trace: trace } |
| 158 | first.option_set_logcb_line(group_owner_public_log, first_context)! |
| 159 | second.option_set_logcb_line(group_owner_public_log, second_context)! |
| 160 | first.option_set_questioncb(group_owner_public_question, first_context)! |
| 161 | second.option_set_questioncb(group_owner_public_question, second_context)! |
| 162 | if ignores[0] { first.option_add_ignore_pkg('shared')! } |
| 163 | if ignores[1] { second.option_add_ignore_pkg('shared')! } |
| 164 | db0 := first.register_syncdb('first', 0)! |
| 165 | db1 := second.register_syncdb('second', 0)! |
| 166 | first.trans_init(if needed[0] { |
| 167 | facade.TransFlag.nolock | .needed |
| 168 | } else { |
| 169 | facade.TransFlag.nolock |
| 170 | })! |
| 171 | mut first_transaction := true |
| 172 | defer { if first_transaction { first.trans_release() or { eprintln(err) } } } |
| 173 | second.trans_init(if needed[1] { |
| 174 | facade.TransFlag.nolock | .needed |
| 175 | } else { |
| 176 | facade.TransFlag.nolock |
| 177 | })! |
| 178 | defer { second.trans_release() or { eprintln(err) } } |
| 179 | mut dbs := &facade.List(unsafe { nil }) |
| 180 | assert (&dbs).append(db0) != unsafe { nil } |
| 181 | assert (&dbs).append(db1) != unsafe { nil } |
| 182 | defer { dbs.free() } |
| 183 | mut result := dbs.find_group_pkgs('tools') |
| 184 | defer { result.free() } |
| 185 | if release_first { |
| 186 | first.trans_release()! |
| 187 | first_transaction = false |
| 188 | first.release()! |
| 189 | first_alive = false |
| 190 | $if gcboehm ? { |
| 191 | C.GC_gcollect() |
| 192 | } |
| 193 | } |
| 194 | for pkg in result.pkgs() { |
| 195 | owner := pkg.get_handle() |
| 196 | db := pkg.get_db()! |
| 197 | index := if voidptr(owner) == voidptr(first) { |
| 198 | 0 |
| 199 | } else if voidptr(owner) == voidptr(second) { |
| 200 | 1 |
| 201 | } else { |
| 202 | -1 |
| 203 | } |
| 204 | identity := (index == 0 && voidptr(db) == voidptr(db0)) || (index == 1 && voidptr(db) == voidptr(db1)) |
| 205 | trace.items << 'package:${index}:${db.get_name()}:${pkg.get_name()}:${pkg.get_version()}:${identity}' |
| 206 | } |
| 207 | return trace.items |
| 208 | } |
| 209 | |
| 210 | fn test_group_queries_use_each_database_owner() ! { |
| 211 | root := os.join_path(os.vtmp_dir(), 'alpm-group-owners-${os.getpid()}') |
| 212 | os.mkdir_all(root)! |
| 213 | defer { os.rmdir_all(root) or { eprintln(err) } } |
| 214 | group_owner_fixture(root)! |
| 215 | mut mismatches := []string{} |
| 216 | for mode in 0 .. 32 { |
| 217 | needed := [mode & 1 != 0, mode & 2 != 0] |
| 218 | ignores := [mode & 4 != 0, mode & 8 != 0] |
| 219 | allowed := mode & 16 != 0 |
| 220 | expected := group_owner_reference(root, needed, ignores, allowed, false)! |
| 221 | actual := group_owner_public(root, needed, ignores, allowed, false)! |
| 222 | if actual != expected { |
| 223 | mismatches << '${mode}:\n${actual}\n!=\n${expected}' |
| 224 | } |
| 225 | } |
| 226 | assert mismatches.len == 0, mismatches[..int_min(4, mismatches.len)].join('\n') |
| 227 | // the returned list owns its nodes; a package from the other handle stays live. |
| 228 | expected := group_owner_reference(root, [true, false], [false, false], false, true)! |
| 229 | actual := group_owner_public(root, [true, false], [false, false], false, true)! |
| 230 | assert actual == expected, '${actual}\n!=\n${expected}' |
| 231 | assert actual.count(it.starts_with('package:1:second:b:')) == 1 |
| 232 | } |
| 233 | |
| 234 | fn test_foreign_query_results_keep_canonical_package_views() ! { |
| 235 | root := os.join_path(os.vtmp_dir(), 'alpm-query-owners-${os.getpid()}') |
| 236 | os.mkdir_all(root)! |
| 237 | defer { os.rmdir_all(root) or { eprintln(err) } } |
| 238 | group_owner_fixture(root)! |
| 239 | first := initialize('/', os.join_path(root, '0'))! |
| 240 | defer { first.release() or { eprintln(err) } } |
| 241 | second := initialize('/', os.join_path(root, '1'))! |
| 242 | defer { second.release() or { eprintln(err) } } |
| 243 | public_first := facade.initialize('/', os.join_path(root, '0'))! |
| 244 | defer { public_first.release() or { eprintln(err) } } |
| 245 | public_second := facade.initialize('/', os.join_path(root, '1'))! |
| 246 | defer { public_second.release() or { eprintln(err) } } |
| 247 | db0 := first.register_syncdb('first', 0)! |
| 248 | db1 := second.register_syncdb('second', 0)! |
| 249 | public_db0 := public_first.register_syncdb('first', 0)! |
| 250 | public_db1 := public_second.register_syncdb('second', 0)! |
| 251 | mut databases := &List(unsafe { nil }) |
| 252 | assert (&databases).append(db1) != unsafe { nil } |
| 253 | defer { databases.free() } |
| 254 | mut public_databases := &facade.List(unsafe { nil }) |
| 255 | assert (&public_databases).append(public_db1) != unsafe { nil } |
| 256 | defer { public_databases.free() } |
| 257 | mut expected_trace := &GroupOwnerTrace{} |
| 258 | mut actual_trace := &GroupOwnerTrace{} |
| 259 | context := &GroupOwnerContext{ index: 0, allowed: true, trace: expected_trace, expected_owner: second } |
| 260 | public_context := &GroupOwnerContext{ index: 0, allowed: true, trace: actual_trace, expected_owner: public_second } |
| 261 | first.option_set_questioncb(group_owner_question, context)! |
| 262 | public_first.option_set_questioncb(group_owner_public_question, public_context)! |
| 263 | first.option_add_ignore_pkg('b')! |
| 264 | public_first.option_add_ignore_pkg('b')! |
| 265 | expected := first.find_dbs_satisfier(databases, 'b') or { return error('missing C satisfier') } |
| 266 | actual := public_first.find_dbs_satisfier(public_databases, 'b') or { return error('missing public satisfier') } |
| 267 | assert voidptr(expected.get_handle()) == voidptr(second) |
| 268 | assert voidptr(actual.get_handle()) == voidptr(public_second) |
| 269 | public_b := public_db1.get_pkg('b') or { return error('missing public b') } |
| 270 | assert voidptr(actual) == voidptr(public_b) |
| 271 | assert voidptr(actual.get_db()!) == voidptr(public_db1) |
| 272 | assert actual_trace.items == expected_trace.items |
| 273 | assert actual_trace.items == ['question:0:b:1-1'] |
| 274 | if _ := first.add_pkg(expected) { |
| 275 | assert false |
| 276 | } else { |
| 277 | assert err.code() == int(Errno.wrong_args) |
| 278 | } |
| 279 | if _ := public_first.add_pkg(actual) { |
| 280 | assert false |
| 281 | } else { |
| 282 | assert err.code() == int(facade.Errno.wrong_args) |
| 283 | } |
| 284 | local0 := first.get_localdb()! |
| 285 | public_local0 := public_first.get_localdb()! |
| 286 | installed := local0.get_pkg('shared') or { return error('missing C installed package') } |
| 287 | public_installed := public_local0.get_pkg('shared') or { return error('missing public installed package') } |
| 288 | upgrade := databases.sync_get_new_version(installed) |
| 289 | public_upgrade := public_databases.sync_get_new_version(public_installed) |
| 290 | assert upgrade != unsafe { nil } && public_upgrade != unsafe { nil } |
| 291 | assert voidptr(upgrade.get_handle()) == voidptr(second) |
| 292 | assert voidptr(public_upgrade.get_handle()) == voidptr(public_second) |
| 293 | assert upgrade.get_version() == public_upgrade.get_version() |
| 294 | public_shared := public_db1.get_pkg('shared') or { return error('missing public shared') } |
| 295 | assert voidptr(public_upgrade) == voidptr(public_shared) |
| 296 | mut packages := &List(unsafe { nil }) |
| 297 | reference_a := db0.get_pkg('a') or { return error('missing C a') } |
| 298 | assert (&packages).append(reference_a) != unsafe { nil } |
| 299 | assert (&packages).append(expected) != unsafe { nil } |
| 300 | defer { packages.free() } |
| 301 | mut public_packages := &facade.List(unsafe { nil }) |
| 302 | public_a := public_db0.get_pkg('a') or { return error('missing public a') } |
| 303 | assert (&public_packages).append(public_a) != unsafe { nil } |
| 304 | assert (&public_packages).append(public_b) != unsafe { nil } |
| 305 | defer { public_packages.free() } |
| 306 | conflicts := first.checkconflicts(packages) |
| 307 | defer { conflicts.free() } |
| 308 | public_conflicts := public_first.checkconflicts(public_packages) |
| 309 | defer { public_conflicts.free() } |
| 310 | assert conflicts.count() == 1 && public_conflicts.count() == 1 |
| 311 | conflict := unsafe { &Conflict(conflicts.data) } |
| 312 | defer { conflict.free() } |
| 313 | public_conflict := unsafe { &facade.Conflict(public_conflicts.data) } |
| 314 | defer { public_conflict.free() } |
| 315 | assert voidptr(conflict.package1.get_handle()) == voidptr(first) |
| 316 | assert voidptr(conflict.package2.get_handle()) == voidptr(second) |
| 317 | assert voidptr(public_conflict.package1) == voidptr(public_a) |
| 318 | assert voidptr(public_conflict.package2) == voidptr(public_b) |
| 319 | assert public_conflict.reason.compute_string() == conflict.reason.compute_string() |
| 320 | } |
| 321 | |