| 1 | module coracle |
| 2 | |
| 3 | import nspkg as facade |
| 4 | import nspkg.tests.archivefixtures |
| 5 | import nspkg.tests.signaturefixtures |
| 6 | import os |
| 7 | |
| 8 | fn compare_public_metadata(actual &facade.Package, expected &Package) ! { |
| 9 | assert actual.get_name() == expected.get_name() |
| 10 | assert actual.get_version() == expected.get_version() |
| 11 | assert actual.get_base() == expected.get_base() |
| 12 | assert actual.get_desc() == expected.get_desc() |
| 13 | assert actual.get_url() == expected.get_url() |
| 14 | assert actual.get_arch() == expected.get_arch() |
| 15 | assert actual.get_packager() == expected.get_packager() |
| 16 | assert actual.get_filename() == expected.get_filename() |
| 17 | assert actual.get_size() == expected.get_size() |
| 18 | assert actual.get_isize() == expected.get_isize() |
| 19 | assert actual.get_build_date() == expected.get_build_date() |
| 20 | assert actual.get_install_date() == expected.get_install_date() |
| 21 | assert int(actual.get_reason()) == int(expected.get_reason()) |
| 22 | assert int(actual.get_origin()) == int(expected.get_origin()) |
| 23 | assert int(actual.get_validation()) == int(expected.get_validation()) |
| 24 | assert actual.get_base64_sig() == expected.get_base64_sig() |
| 25 | assert actual.get_sha256_sum() == expected.get_sha256_sum() |
| 26 | assert actual.has_scriptlet() == expected.has_scriptlet() |
| 27 | assert actual.get_groups().strs() == expected.get_groups().strs() |
| 28 | assert actual.get_licenses().strs() == expected.get_licenses().strs() |
| 29 | assert actual.get_depends().depends().map(it.compute_string()) == expected.get_depends().depends().map(it.compute_string()) |
| 30 | assert actual.get_opt_depends().depends().map(it.compute_string()) == expected.get_opt_depends().depends().map(it.compute_string()) |
| 31 | assert actual.get_check_depends().depends().map(it.compute_string()) == expected.get_check_depends().depends().map(it.compute_string()) |
| 32 | assert actual.get_make_depends().depends().map(it.compute_string()) == expected.get_make_depends().depends().map(it.compute_string()) |
| 33 | assert actual.get_conflicts().depends().map(it.compute_string()) == expected.get_conflicts().depends().map(it.compute_string()) |
| 34 | assert actual.get_provides().depends().map(it.compute_string()) == expected.get_provides().depends().map(it.compute_string()) |
| 35 | assert actual.get_replaces().depends().map(it.compute_string()) == expected.get_replaces().depends().map(it.compute_string()) |
| 36 | |
| 37 | // the filelist and file records retain their original C-compatible layout. |
| 38 | actual_files := unsafe { &Filelist(actual.get_files()) } |
| 39 | expected_files := expected.get_files() |
| 40 | assert actual_files.names() == expected_files.names() |
| 41 | for index in 0 .. int(actual_files.count) { |
| 42 | a := unsafe { actual_files.files[index] } |
| 43 | b := unsafe { expected_files.files[index] } |
| 44 | assert a.size == b.size |
| 45 | assert a.mode == b.mode |
| 46 | } |
| 47 | mut a := actual.get_backup() |
| 48 | mut b := expected.get_backup() |
| 49 | for a != unsafe { nil } && b != unsafe { nil } { |
| 50 | ab := a.get_backup() or { return error('missing native backup') } |
| 51 | bb := b.get_backup() or { return error('missing C backup') } |
| 52 | assert ab.get_name() == bb.get_name() |
| 53 | assert ab.get_hash() == bb.get_hash() |
| 54 | a = a.next |
| 55 | b = b.next |
| 56 | } |
| 57 | assert a == unsafe { nil } && b == unsafe { nil } |
| 58 | } |
| 59 | |
| 60 | fn test_public_metadata_views_match_c_for_local_sync_and_archive_packages() ! { |
| 61 | base := os.join_path(os.vtmp_dir(), 'alpm-public-metadata-${os.getpid()}') |
| 62 | os.mkdir_all(os.join_path(base, 'local', 'widget-2:1.2.3-4'))! |
| 63 | os.mkdir_all(os.join_path(base, 'sync'))! |
| 64 | defer { os.rmdir_all(base) or { eprintln(err) } } |
| 65 | os.write_file(os.join_path(base, 'local', 'ALPM_DB_VERSION'), '9\n')! |
| 66 | for name in ['desc', 'files'] { |
| 67 | text := os.read_file(os.join_path(@VMODROOT, 'tests', 'fixtures', 'local_${name}'))! |
| 68 | os.write_file(os.join_path(base, 'local', 'widget-2:1.2.3-4', name), text)! |
| 69 | } |
| 70 | os.write_file(os.join_path(base, 'local', 'widget-2:1.2.3-4', 'install'), '')! |
| 71 | local_desc := os.read_file(os.join_path(@VMODROOT, 'tests', 'fixtures', 'local_desc'))! |
| 72 | archivefixtures.write(os.join_path(base, 'sync', 'core.db'), [archivefixtures.Entry{ name: 'widget-2:1.2.3-4/desc', text: local_desc }], false)! |
| 73 | info := os.read_file(os.join_path(@VMODROOT, 'tests', 'fixtures', 'pkginfo'))! |
| 74 | archive := os.join_path(base, 'widget.pkg.tar') |
| 75 | archivefixtures.write(archive, [ |
| 76 | archivefixtures.Entry{ name: '.PKGINFO', text: info }, |
| 77 | archivefixtures.Entry{ name: '.INSTALL', text: '' }, |
| 78 | archivefixtures.Entry{ name: 'etc/widget.conf', text: 'config' }, |
| 79 | ], false)! |
| 80 | actual := facade.initialize('/', base)! |
| 81 | defer { actual.release() or { eprintln(err) } } |
| 82 | expected := initialize('/', base)! |
| 83 | defer { expected.release() or { eprintln(err) } } |
| 84 | local_a := actual.get_localdb()! |
| 85 | local_b := expected.get_localdb()! |
| 86 | local_actual := local_a.get_pkg('widget') or { return error('missing native local package') } |
| 87 | local_expected := local_b.get_pkg('widget') or { return error('missing C local package') } |
| 88 | compare_public_metadata(local_actual, local_expected)! |
| 89 | sync_a := actual.register_syncdb('core', 0)! |
| 90 | sync_b := expected.register_syncdb('core', 0)! |
| 91 | sync_actual := sync_a.get_pkg('widget') or { return error('missing native sync package') } |
| 92 | sync_expected := sync_b.get_pkg('widget') or { return error('missing C sync package') } |
| 93 | compare_public_metadata(sync_actual, sync_expected)! |
| 94 | for full in [0, 1] { |
| 95 | a := actual.pkg_load(archive, full, 0)! |
| 96 | b := expected.pkg_load(archive, full, 0)! |
| 97 | compare_public_metadata(a, b)! |
| 98 | a.free_pkg()! |
| 99 | b.free_pkg()! |
| 100 | } |
| 101 | assert actual.option_get_root() == expected.option_get_root() |
| 102 | assert actual.option_get_dbpath() == expected.option_get_dbpath() |
| 103 | assert actual.option_get_lockfile() == expected.option_get_lockfile() |
| 104 | assert sync_a.get_handle() == actual |
| 105 | assert sync_a.get_name() == sync_b.get_name() |
| 106 | assert sync_a.get_valid()! == sync_b.get_valid()! |
| 107 | assert int(sync_a.get_siglevel()) == int(sync_b.get_siglevel()) |
| 108 | assert int(sync_a.get_usage()?) == int(sync_b.get_usage()?) |
| 109 | } |
| 110 | |
| 111 | fn test_public_signature_results_match_c_and_cleanup_owns_native_key_data() ! { |
| 112 | base := os.join_path(os.vtmp_dir(), 'alpm-public-signatures-${os.getpid()}') |
| 113 | os.mkdir_all(base)! |
| 114 | defer { os.rmdir_all(base) or { eprintln(err) } } |
| 115 | home := os.join_path(base, 'keys') |
| 116 | defer { signaturefixtures.cleanup(home) or { eprintln(err) } } |
| 117 | ring := signaturefixtures.create(home)! |
| 118 | path := os.join_path(base, 'p.pkg.tar') |
| 119 | archivefixtures.write(path, [archivefixtures.Entry{ name: '.PKGINFO', text: 'pkgname = p\npkgver = 1-1\n' }], false)! |
| 120 | ring.sign(path, [ring.fingerprint], false)! |
| 121 | actual := facade.initialize('/', base)! |
| 122 | defer { actual.release() or { eprintln(err) } } |
| 123 | expected := initialize('/', base)! |
| 124 | defer { expected.release() or { eprintln(err) } } |
| 125 | actual.option_set_gpgdir(home)! |
| 126 | expected.option_set_gpgdir(home)! |
| 127 | a := actual.pkg_load(path, 0, 0)! |
| 128 | defer { a.free_pkg() or { eprintln(err) } } |
| 129 | b := expected.pkg_load(path, 0, 0)! |
| 130 | defer { b.free_pkg() or { eprintln(err) } } |
| 131 | for trust in [2, 3, 4, 5, 6] { |
| 132 | ring.trust(ring.fingerprint, trust)! |
| 133 | ar := a.check_pgp_signature() or { return error('missing native results') } |
| 134 | br := b.check_pgp_signature() or { return error('missing C results') } |
| 135 | gc_collect() |
| 136 | assert ar.count == br.count |
| 137 | for index in 0 .. int(ar.count) { |
| 138 | av := unsafe { ar.results[index] } |
| 139 | bv := unsafe { br.results[index] } |
| 140 | assert int(av.status) == int(bv.status) |
| 141 | assert int(av.validity) == int(bv.validity) |
| 142 | assert (av.key.data == unsafe { nil }) == (bv.key.data == unsafe { nil }) |
| 143 | assert unsafe { cstring_to_vstring(av.key.fingerprint) } == unsafe { cstring_to_vstring(bv.key.fingerprint) } |
| 144 | assert av.key.created == bv.key.created |
| 145 | assert av.key.expires == bv.key.expires |
| 146 | assert av.key.length == bv.key.length |
| 147 | } |
| 148 | ar.cleanup()! |
| 149 | br.cleanup()! |
| 150 | assert ar.count == 0 |
| 151 | assert ar.results == unsafe { nil } |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | fn test_public_action_log_prefix_rotation_and_errors_match_c() ! { |
| 156 | base := os.join_path(os.vtmp_dir(), 'alpm-public-action-log-${os.getpid()}') |
| 157 | os.mkdir_all(base)! |
| 158 | defer { os.rmdir_all(base) or { eprintln(err) } } |
| 159 | actual := facade.initialize('/', base)! |
| 160 | defer { actual.release() or { eprintln(err) } } |
| 161 | expected := initialize('/', base)! |
| 162 | defer { expected.release() or { eprintln(err) } } |
| 163 | actual.log_action('', 'no logfile configured\n')! |
| 164 | expected.log_action('', 'no logfile configured\n')! |
| 165 | a := os.join_path(base, 'native.log') |
| 166 | b := os.join_path(base, 'c.log') |
| 167 | actual.option_set_logfile(a)! |
| 168 | expected.option_set_logfile(b)! |
| 169 | actual.log_action('', 'first record\n')! |
| 170 | expected.log_action('', 'first record\n')! |
| 171 | os.rename(a, a + '.old')! |
| 172 | os.rename(b, b + '.old')! |
| 173 | actual.log_action('custom', 'second record\n')! |
| 174 | expected.log_action('custom', 'second record\n')! |
| 175 | assert !os.exists(a) && !os.exists(b) |
| 176 | lines_a := os.read_lines(a + '.old')! |
| 177 | lines_b := os.read_lines(b + '.old')! |
| 178 | assert lines_a.map(it.all_after('] ')) == lines_b.map(it.all_after('] ')) |
| 179 | assert lines_a[0].all_after('] ') == '[UNKNOWN] first record' |
| 180 | assert os.stat(a + '.old')!.mode & 0o777 == os.stat(b + '.old')!.mode & 0o777 |
| 181 | actual.option_set_logfile(a)! |
| 182 | expected.option_set_logfile(b)! |
| 183 | actual.log_action('rotated', 'new file\n')! |
| 184 | expected.log_action('rotated', 'new file\n')! |
| 185 | assert os.read_lines(a)!.map(it.all_after('] ')) == os.read_lines(b)!.map(it.all_after('] ')) |
| 186 | // the V API accepts a complete message, so percent signs are literal data. |
| 187 | actual.log_action('literal', '100% installed\n')! |
| 188 | assert os.read_file(a)!.contains('[literal] 100% installed\n') |
| 189 | for path in ['', os.join_path(base, 'missing', 'log')] { |
| 190 | actual.option_set_logfile(path)! |
| 191 | expected.option_set_logfile(path)! |
| 192 | if _ := actual.log_action('failure', 'message') { |
| 193 | assert false |
| 194 | } |
| 195 | if _ := expected.log_action('failure', 'message') { |
| 196 | assert false |
| 197 | } |
| 198 | assert int(actual.errno()) == int(expected.errno()) |
| 199 | } |
| 200 | } |
| 201 | |