module coracle import nspkg as facade import nspkg.tests.archivefixtures import os #include "@DIR/legacy_md5_reference.h" fn C.v_alpm_legacy_checkmd5sum(&Handle, i32, &u8, &u8, &i32) i32 struct LegacyDigest { name string value ?string } fn test_public_legacy_md5_cache_checks_match_the_removed_c_implementation() ! { base := os.join_path(os.vtmp_dir(), 'alpm-legacy-md5-${os.getpid()}') os.mkdir_all(os.join_path(base, 'sync'))! defer { os.rmdir_all(base) or { eprintln(err) } } digest := '9a0364b9e99bb480dd25e1f0284c8555' digests := [LegacyDigest{ name: 'absent' }, LegacyDigest{ name: 'correct', value: digest }, LegacyDigest{ name: 'different', value: '0'.repeat(32) }, LegacyDigest{ name: 'upper-case', value: digest.to_upper() }, LegacyDigest{ name: 'empty', value: '' }, LegacyDigest{ name: 'malformed', value: 'not-a-digest' }] for expected in digests { for cache_state in ['correct', 'wrong', 'missing', 'directory', 'broken-link', 'symlink', 'first-wins', 'empty-file'] { dir := os.join_path(base, expected.name + '-' + cache_state) for name in ['sync', 'cache-a', 'cache-b'] { os.mkdir_all(os.join_path(dir, name))! } first := os.join_path(dir, 'cache-a', 'p.pkg.tar') second := os.join_path(dir, 'cache-b', 'p.pkg.tar') match cache_state { 'correct' { os.write_file(first, 'content')! } 'wrong' { os.write_file(first, 'wrong')! } 'directory' { os.mkdir(first)! os.write_file(second, 'content')! } 'broken-link' { os.symlink('absent', first)! os.write_file(second, 'content')! } 'symlink' { os.write_file(second, 'content')! os.symlink(second, first)! } 'first-wins' { os.write_file(first, 'wrong')! os.write_file(second, 'content')! } 'empty-file' { os.write_file(first, '')! } else {} } mut metadata := '%FILENAME%\np.pkg.tar\n\n' if value := expected.value { metadata += '%MD5SUM%\n${value}\n\n' } archivefixtures.write(os.join_path(dir, 'sync', 'core.db'), [archivefixtures.Entry{ name: 'p-1-1/desc', text: metadata }], false)! actual := facade.initialize('/', dir)! reference := initialize('/', dir)! for cache in ['cache-a', 'cache-b'] { actual.option_add_cachedir(os.join_path(dir, cache))! reference.option_add_cachedir(os.join_path(dir, cache))! } db := actual.register_syncdb('core', 0)! pkg := db.get_pkg('p') or { return error('missing native sync package') } assert pkg.get_md5_sum() == (expected.value or { '' }) // merely retaining an old checksum does not enable it for normal transactions. assert int(pkg.get_validation()) == int(facade.PkgValidation.none) mut want_errno := i32(-1) pointer := if value := expected.value { value.str } else { unsafe { nil } } want := C.v_alpm_legacy_checkmd5sum(reference, 3, c'p.pkg.tar', pointer, &want_errno) got := pkg.checkmd5sum() assert got == int(want), '${expected.name}/${cache_state}' assert int(actual.errno()) == int(want_errno), '${expected.name}/${cache_state}' if expected.name == 'correct' && cache_state in ['correct', 'directory', 'broken-link', 'symlink'] { assert got == 0 } actual.release()! reference.release()! } } } fn test_public_legacy_md5_rejects_file_and_local_packages_as_before() ! { base := os.join_path(os.vtmp_dir(), 'alpm-legacy-md5-origins-${os.getpid()}') os.mkdir_all(os.join_path(base, 'local', 'p-1-1'))! defer { os.rmdir_all(base) or { eprintln(err) } } os.write_file(os.join_path(base, 'local', 'ALPM_DB_VERSION'), '9\n')! os.write_file(os.join_path(base, 'local', 'p-1-1', 'desc'), '')! path := os.join_path(base, 'p.pkg.tar') archivefixtures.write(path, [archivefixtures.Entry{ name: '.PKGINFO', text: 'pkgname = p\npkgver = 1-1\n' }], false)! actual := facade.initialize('/', base)! defer { actual.release() or { eprintln(err) } } reference := initialize('/', base)! defer { reference.release() or { eprintln(err) } } file := actual.pkg_load(path, 1, 0)! defer { file.free_pkg() or { eprintln(err) } } local := actual.get_localdb()!.get_pkg('p') or { return error('missing local package') } for pkg in [file, local] { assert pkg.get_md5_sum() == '' mut want_errno := i32(0) assert C.v_alpm_legacy_checkmd5sum(reference, i32(pkg.get_origin()), c'p.pkg.tar', unsafe { nil }, &want_errno) == -1 assert pkg.checkmd5sum() == -1 assert int(actual.errno()) == int(want_errno) } }