| 1 | module coracle |
| 2 | |
| 3 | import nspkg as facade |
| 4 | import nspkg.tests.archivefixtures |
| 5 | import os |
| 6 | |
| 7 | #include "@DIR/legacy_md5_reference.h" |
| 8 | |
| 9 | fn C.v_alpm_legacy_checkmd5sum(&Handle, i32, &u8, &u8, &i32) i32 |
| 10 | |
| 11 | struct LegacyDigest { |
| 12 | name string |
| 13 | value ?string |
| 14 | } |
| 15 | |
| 16 | fn test_public_legacy_md5_cache_checks_match_the_removed_c_implementation() ! { |
| 17 | base := os.join_path(os.vtmp_dir(), 'alpm-legacy-md5-${os.getpid()}') |
| 18 | os.mkdir_all(os.join_path(base, 'sync'))! |
| 19 | defer { os.rmdir_all(base) or { eprintln(err) } } |
| 20 | digest := '9a0364b9e99bb480dd25e1f0284c8555' |
| 21 | digests := [LegacyDigest{ name: 'absent' }, LegacyDigest{ name: 'correct', value: digest }, |
| 22 | LegacyDigest{ name: 'different', value: '0'.repeat(32) }, |
| 23 | LegacyDigest{ name: 'upper-case', value: digest.to_upper() }, |
| 24 | LegacyDigest{ name: 'empty', value: '' }, |
| 25 | LegacyDigest{ name: 'malformed', value: 'not-a-digest' }] |
| 26 | for expected in digests { |
| 27 | for cache_state in ['correct', 'wrong', 'missing', 'directory', 'broken-link', 'symlink', |
| 28 | 'first-wins', 'empty-file'] { |
| 29 | dir := os.join_path(base, expected.name + '-' + cache_state) |
| 30 | for name in ['sync', 'cache-a', 'cache-b'] { os.mkdir_all(os.join_path(dir, name))! } |
| 31 | first := os.join_path(dir, 'cache-a', 'p.pkg.tar') |
| 32 | second := os.join_path(dir, 'cache-b', 'p.pkg.tar') |
| 33 | match cache_state { |
| 34 | 'correct' { os.write_file(first, 'content')! } |
| 35 | 'wrong' { os.write_file(first, 'wrong')! } |
| 36 | 'directory' { |
| 37 | os.mkdir(first)! |
| 38 | os.write_file(second, 'content')! |
| 39 | } |
| 40 | 'broken-link' { |
| 41 | os.symlink('absent', first)! |
| 42 | os.write_file(second, 'content')! |
| 43 | } |
| 44 | 'symlink' { |
| 45 | os.write_file(second, 'content')! |
| 46 | os.symlink(second, first)! |
| 47 | } |
| 48 | 'first-wins' { |
| 49 | os.write_file(first, 'wrong')! |
| 50 | os.write_file(second, 'content')! |
| 51 | } |
| 52 | 'empty-file' { os.write_file(first, '')! } |
| 53 | else {} |
| 54 | } |
| 55 | mut metadata := '%FILENAME%\np.pkg.tar\n\n' |
| 56 | if value := expected.value { metadata += '%MD5SUM%\n${value}\n\n' } |
| 57 | archivefixtures.write(os.join_path(dir, 'sync', 'core.db'), [archivefixtures.Entry{ name: 'p-1-1/desc', text: metadata }], false)! |
| 58 | actual := facade.initialize('/', dir)! |
| 59 | reference := initialize('/', dir)! |
| 60 | for cache in ['cache-a', 'cache-b'] { |
| 61 | actual.option_add_cachedir(os.join_path(dir, cache))! |
| 62 | reference.option_add_cachedir(os.join_path(dir, cache))! |
| 63 | } |
| 64 | db := actual.register_syncdb('core', 0)! |
| 65 | pkg := db.get_pkg('p') or { return error('missing native sync package') } |
| 66 | assert pkg.get_md5_sum() == (expected.value or { '' }) |
| 67 | // merely retaining an old checksum does not enable it for normal transactions. |
| 68 | assert int(pkg.get_validation()) == int(facade.PkgValidation.none) |
| 69 | mut want_errno := i32(-1) |
| 70 | pointer := if value := expected.value { value.str } else { unsafe { nil } } |
| 71 | want := C.v_alpm_legacy_checkmd5sum(reference, 3, c'p.pkg.tar', pointer, &want_errno) |
| 72 | got := pkg.checkmd5sum() |
| 73 | assert got == int(want), '${expected.name}/${cache_state}' |
| 74 | assert int(actual.errno()) == int(want_errno), '${expected.name}/${cache_state}' |
| 75 | if expected.name == 'correct' && cache_state in ['correct', 'directory', 'broken-link', |
| 76 | 'symlink'] { |
| 77 | assert got == 0 |
| 78 | } |
| 79 | actual.release()! |
| 80 | reference.release()! |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | fn test_public_legacy_md5_rejects_file_and_local_packages_as_before() ! { |
| 86 | base := os.join_path(os.vtmp_dir(), 'alpm-legacy-md5-origins-${os.getpid()}') |
| 87 | os.mkdir_all(os.join_path(base, 'local', 'p-1-1'))! |
| 88 | defer { os.rmdir_all(base) or { eprintln(err) } } |
| 89 | os.write_file(os.join_path(base, 'local', 'ALPM_DB_VERSION'), '9\n')! |
| 90 | os.write_file(os.join_path(base, 'local', 'p-1-1', 'desc'), '')! |
| 91 | path := os.join_path(base, 'p.pkg.tar') |
| 92 | archivefixtures.write(path, [archivefixtures.Entry{ name: '.PKGINFO', text: 'pkgname = p\npkgver = 1-1\n' }], false)! |
| 93 | actual := facade.initialize('/', base)! |
| 94 | defer { actual.release() or { eprintln(err) } } |
| 95 | reference := initialize('/', base)! |
| 96 | defer { reference.release() or { eprintln(err) } } |
| 97 | file := actual.pkg_load(path, 1, 0)! |
| 98 | defer { file.free_pkg() or { eprintln(err) } } |
| 99 | local := actual.get_localdb()!.get_pkg('p') or { return error('missing local package') } |
| 100 | for pkg in [file, local] { |
| 101 | assert pkg.get_md5_sum() == '' |
| 102 | mut want_errno := i32(0) |
| 103 | assert C.v_alpm_legacy_checkmd5sum(reference, i32(pkg.get_origin()), c'p.pkg.tar', unsafe { nil }, &want_errno) == -1 |
| 104 | assert pkg.checkmd5sum() == -1 |
| 105 | assert int(actual.errno()) == int(want_errno) |
| 106 | } |
| 107 | } |
| 108 | |