module coracle import nspkg as facade import nspkg.tests.archivefixtures import nspkg.tests.signaturefixtures import os fn compare_public_metadata(actual &facade.Package, expected &Package) ! { assert actual.get_name() == expected.get_name() assert actual.get_version() == expected.get_version() assert actual.get_base() == expected.get_base() assert actual.get_desc() == expected.get_desc() assert actual.get_url() == expected.get_url() assert actual.get_arch() == expected.get_arch() assert actual.get_packager() == expected.get_packager() assert actual.get_filename() == expected.get_filename() assert actual.get_size() == expected.get_size() assert actual.get_isize() == expected.get_isize() assert actual.get_build_date() == expected.get_build_date() assert actual.get_install_date() == expected.get_install_date() assert int(actual.get_reason()) == int(expected.get_reason()) assert int(actual.get_origin()) == int(expected.get_origin()) assert int(actual.get_validation()) == int(expected.get_validation()) assert actual.get_base64_sig() == expected.get_base64_sig() assert actual.get_sha256_sum() == expected.get_sha256_sum() assert actual.has_scriptlet() == expected.has_scriptlet() assert actual.get_groups().strs() == expected.get_groups().strs() assert actual.get_licenses().strs() == expected.get_licenses().strs() assert actual.get_depends().depends().map(it.compute_string()) == expected.get_depends().depends().map(it.compute_string()) assert actual.get_opt_depends().depends().map(it.compute_string()) == expected.get_opt_depends().depends().map(it.compute_string()) assert actual.get_check_depends().depends().map(it.compute_string()) == expected.get_check_depends().depends().map(it.compute_string()) assert actual.get_make_depends().depends().map(it.compute_string()) == expected.get_make_depends().depends().map(it.compute_string()) assert actual.get_conflicts().depends().map(it.compute_string()) == expected.get_conflicts().depends().map(it.compute_string()) assert actual.get_provides().depends().map(it.compute_string()) == expected.get_provides().depends().map(it.compute_string()) assert actual.get_replaces().depends().map(it.compute_string()) == expected.get_replaces().depends().map(it.compute_string()) // the filelist and file records retain their original C-compatible layout. actual_files := unsafe { &Filelist(actual.get_files()) } expected_files := expected.get_files() assert actual_files.names() == expected_files.names() for index in 0 .. int(actual_files.count) { a := unsafe { actual_files.files[index] } b := unsafe { expected_files.files[index] } assert a.size == b.size assert a.mode == b.mode } mut a := actual.get_backup() mut b := expected.get_backup() for a != unsafe { nil } && b != unsafe { nil } { ab := a.get_backup() or { return error('missing native backup') } bb := b.get_backup() or { return error('missing C backup') } assert ab.get_name() == bb.get_name() assert ab.get_hash() == bb.get_hash() a = a.next b = b.next } assert a == unsafe { nil } && b == unsafe { nil } } fn test_public_metadata_views_match_c_for_local_sync_and_archive_packages() ! { base := os.join_path(os.vtmp_dir(), 'alpm-public-metadata-${os.getpid()}') os.mkdir_all(os.join_path(base, 'local', 'widget-2:1.2.3-4'))! os.mkdir_all(os.join_path(base, 'sync'))! defer { os.rmdir_all(base) or { eprintln(err) } } os.write_file(os.join_path(base, 'local', 'ALPM_DB_VERSION'), '9\n')! for name in ['desc', 'files'] { text := os.read_file(os.join_path(@VMODROOT, 'tests', 'fixtures', 'local_${name}'))! os.write_file(os.join_path(base, 'local', 'widget-2:1.2.3-4', name), text)! } os.write_file(os.join_path(base, 'local', 'widget-2:1.2.3-4', 'install'), '')! local_desc := os.read_file(os.join_path(@VMODROOT, 'tests', 'fixtures', 'local_desc'))! archivefixtures.write(os.join_path(base, 'sync', 'core.db'), [archivefixtures.Entry{ name: 'widget-2:1.2.3-4/desc', text: local_desc }], false)! info := os.read_file(os.join_path(@VMODROOT, 'tests', 'fixtures', 'pkginfo'))! archive := os.join_path(base, 'widget.pkg.tar') archivefixtures.write(archive, [ archivefixtures.Entry{ name: '.PKGINFO', text: info }, archivefixtures.Entry{ name: '.INSTALL', text: '' }, archivefixtures.Entry{ name: 'etc/widget.conf', text: 'config' }, ], false)! actual := facade.initialize('/', base)! defer { actual.release() or { eprintln(err) } } expected := initialize('/', base)! defer { expected.release() or { eprintln(err) } } local_a := actual.get_localdb()! local_b := expected.get_localdb()! local_actual := local_a.get_pkg('widget') or { return error('missing native local package') } local_expected := local_b.get_pkg('widget') or { return error('missing C local package') } compare_public_metadata(local_actual, local_expected)! sync_a := actual.register_syncdb('core', 0)! sync_b := expected.register_syncdb('core', 0)! sync_actual := sync_a.get_pkg('widget') or { return error('missing native sync package') } sync_expected := sync_b.get_pkg('widget') or { return error('missing C sync package') } compare_public_metadata(sync_actual, sync_expected)! for full in [0, 1] { a := actual.pkg_load(archive, full, 0)! b := expected.pkg_load(archive, full, 0)! compare_public_metadata(a, b)! a.free_pkg()! b.free_pkg()! } assert actual.option_get_root() == expected.option_get_root() assert actual.option_get_dbpath() == expected.option_get_dbpath() assert actual.option_get_lockfile() == expected.option_get_lockfile() assert sync_a.get_handle() == actual assert sync_a.get_name() == sync_b.get_name() assert sync_a.get_valid()! == sync_b.get_valid()! assert int(sync_a.get_siglevel()) == int(sync_b.get_siglevel()) assert int(sync_a.get_usage()?) == int(sync_b.get_usage()?) } fn test_public_signature_results_match_c_and_cleanup_owns_native_key_data() ! { base := os.join_path(os.vtmp_dir(), 'alpm-public-signatures-${os.getpid()}') os.mkdir_all(base)! defer { os.rmdir_all(base) or { eprintln(err) } } home := os.join_path(base, 'keys') defer { signaturefixtures.cleanup(home) or { eprintln(err) } } ring := signaturefixtures.create(home)! path := os.join_path(base, 'p.pkg.tar') archivefixtures.write(path, [archivefixtures.Entry{ name: '.PKGINFO', text: 'pkgname = p\npkgver = 1-1\n' }], false)! ring.sign(path, [ring.fingerprint], false)! actual := facade.initialize('/', base)! defer { actual.release() or { eprintln(err) } } expected := initialize('/', base)! defer { expected.release() or { eprintln(err) } } actual.option_set_gpgdir(home)! expected.option_set_gpgdir(home)! a := actual.pkg_load(path, 0, 0)! defer { a.free_pkg() or { eprintln(err) } } b := expected.pkg_load(path, 0, 0)! defer { b.free_pkg() or { eprintln(err) } } for trust in [2, 3, 4, 5, 6] { ring.trust(ring.fingerprint, trust)! ar := a.check_pgp_signature() or { return error('missing native results') } br := b.check_pgp_signature() or { return error('missing C results') } gc_collect() assert ar.count == br.count for index in 0 .. int(ar.count) { av := unsafe { ar.results[index] } bv := unsafe { br.results[index] } assert int(av.status) == int(bv.status) assert int(av.validity) == int(bv.validity) assert (av.key.data == unsafe { nil }) == (bv.key.data == unsafe { nil }) assert unsafe { cstring_to_vstring(av.key.fingerprint) } == unsafe { cstring_to_vstring(bv.key.fingerprint) } assert av.key.created == bv.key.created assert av.key.expires == bv.key.expires assert av.key.length == bv.key.length } ar.cleanup()! br.cleanup()! assert ar.count == 0 assert ar.results == unsafe { nil } } } fn test_public_action_log_prefix_rotation_and_errors_match_c() ! { base := os.join_path(os.vtmp_dir(), 'alpm-public-action-log-${os.getpid()}') os.mkdir_all(base)! defer { os.rmdir_all(base) or { eprintln(err) } } actual := facade.initialize('/', base)! defer { actual.release() or { eprintln(err) } } expected := initialize('/', base)! defer { expected.release() or { eprintln(err) } } actual.log_action('', 'no logfile configured\n')! expected.log_action('', 'no logfile configured\n')! a := os.join_path(base, 'native.log') b := os.join_path(base, 'c.log') actual.option_set_logfile(a)! expected.option_set_logfile(b)! actual.log_action('', 'first record\n')! expected.log_action('', 'first record\n')! os.rename(a, a + '.old')! os.rename(b, b + '.old')! actual.log_action('custom', 'second record\n')! expected.log_action('custom', 'second record\n')! assert !os.exists(a) && !os.exists(b) lines_a := os.read_lines(a + '.old')! lines_b := os.read_lines(b + '.old')! assert lines_a.map(it.all_after('] ')) == lines_b.map(it.all_after('] ')) assert lines_a[0].all_after('] ') == '[UNKNOWN] first record' assert os.stat(a + '.old')!.mode & 0o777 == os.stat(b + '.old')!.mode & 0o777 actual.option_set_logfile(a)! expected.option_set_logfile(b)! actual.log_action('rotated', 'new file\n')! expected.log_action('rotated', 'new file\n')! assert os.read_lines(a)!.map(it.all_after('] ')) == os.read_lines(b)!.map(it.all_after('] ')) // the V API accepts a complete message, so percent signs are literal data. actual.log_action('literal', '100% installed\n')! assert os.read_file(a)!.contains('[literal] 100% installed\n') for path in ['', os.join_path(base, 'missing', 'log')] { actual.option_set_logfile(path)! expected.option_set_logfile(path)! if _ := actual.log_action('failure', 'message') { assert false } if _ := expected.log_action('failure', 'message') { assert false } assert int(actual.errno()) == int(expected.errno()) } }