module main import nspkg import crypto.sha256 import encoding.base64 import os // a file on disk and the mtree entry that matches it exactly fn full_fixture(dir string) !(string, MtreeEntry) { path := os.join_path(dir, 'f') os.write_file(path, 'hello\n')! os.chmod(path, 0o644)! os.utime(path, 1700000000, 1700000000)! st := os.lstat(path)! return path, MtreeEntry{ path: 'f' kind: ae_ifreg mode: 0o644 uid: i64(st.uid) gid: i64(st.gid) mtime: 1700000000 size: 6 sha256: sha256.hexhash('hello\n') } } fn texts(findings []VerifyLine) []string { return findings.map(if it.problem { it.text.all_after('(') } else { 'note ${it.text.all_after('(')}' }) } fn test_check_file_full() ! { tmp := os.join_path(os.vtmp_dir(), 'vpac_test_verify_${os.getpid()}') os.mkdir_all(tmp)! defer { os.rmdir_all(tmp) or {} } path, e := full_fixture(tmp)! assert check_file_full(path, e, false) == [] assert texts(check_file_full(path, MtreeEntry{ ...e, size: 7 }, false)) == ['size mismatch)'] assert texts(check_file_full(path, MtreeEntry{ ...e, sha256: '00'.repeat(32) }, false)) == [ 'SHA256 checksum mismatch)', ] assert texts(check_file_full(path, MtreeEntry{ ...e, mtime: 1 }, false)) == [ 'modification time mismatch)', ] assert texts(check_file_full(path, MtreeEntry{ ...e, mode: 0o600 }, false)) == [ 'permissions mismatch)', ] assert texts(check_file_full(path, MtreeEntry{ ...e, uid: e.uid + 1 }, false)) == [ 'UID mismatch)', ] assert texts(check_file_full(path, MtreeEntry{ ...e, sha256: '' }, false)) == [ 'SHA256 checksum information not available)', ] // the wrong type stops the checks there assert texts(check_file_full(path, MtreeEntry{ ...e, kind: ae_ifdir, size: 0 }, false)) == [ 'file type mismatch)', ] assert texts(check_file_full(os.join_path(tmp, 'gone'), e, false)) == ['missing)'] // a backup file may change in time, size and content: notes, no problem changed := MtreeEntry{ ...e mtime: 1 size: 99 sha256: '00'.repeat(32) } findings := check_file_full(path, changed, true) assert !findings.any(it.problem) assert texts(findings) == ['note modification time mismatch)', 'note size mismatch)', 'note SHA256 checksum mismatch)'] // but not in mode assert texts(check_file_full(path, MtreeEntry{ ...e, mode: 0o600 }, true)) == [ 'permissions mismatch)', ] } fn test_check_file_full_symlink() ! { tmp := os.join_path(os.vtmp_dir(), 'vpac_test_verify_link_${os.getpid()}') os.mkdir_all(tmp)! defer { os.rmdir_all(tmp) or {} } link := os.join_path(tmp, 'l') os.symlink('f', link)! st := os.lstat(link)! e := MtreeEntry{ path: 'l' kind: ae_iflnk mode: st.mode & mode_bits uid: i64(st.uid) gid: i64(st.gid) mtime: st.mtime symlink: 'f' } // a link is checked as a link: its target need not exist assert check_file_full(link, e, false) == [] assert texts(check_file_full(link, MtreeEntry{ ...e, symlink: 'g' }, false)) == [ 'symlink path mismatch)', ] } fn test_digest_hex() { zero := []u8{len: 32} assert digest_hex(zero.data) == '' mut d := []u8{len: 32} d[0] = 0xab d[31] = 0x01 assert digest_hex(d.data) == 'ab' + '00'.repeat(30) + '01' assert digest_hex(unsafe { nil }) == '' } fn test_file_sha256() ! { tmp := os.join_path(os.vtmp_dir(), 'vpac_test_sha_${os.getpid()}') os.mkdir_all(tmp)! defer { os.rmdir_all(tmp) or {} } // larger than one 64 KiB chunk data := 'abcdefgh'.repeat(20000) path := os.join_path(tmp, 'big') os.write_file(path, data)! assert file_sha256(path)! == sha256.hexhash(data) assert file_sha256(os.join_path(tmp, 'nope')) or { 'error' } == 'error' } // a read that fails never comes back as the partial file's digest: a // truncated hash would pass itself off as the whole file's fn test_file_sha256_read_failure() ! { tmp := os.join_path(os.vtmp_dir(), 'vpac_test_sha_deny_${os.getpid()}') os.mkdir_all(tmp)! defer { os.rmdir_all(tmp) or {} } if sum := file_sha256(tmp) { assert false, 'a directory hashed as "${sum[0..8]}…"' } else { assert err.msg() != '', err.msg() } } // mtree_pkg installs one package in a temp local db with the given gzipped // mtree text, and returns the handle and the package libalpm loads for it fn mtree_pkg(tmp string, name string, mtree []u8) !(&nspkg.Handle, &nspkg.Package) { entry := os.join_path(tmp, 'db', 'local', '${name}-1.0-1') os.mkdir_all(entry)! os.mkdir_all(os.join_path(tmp, 'root'))! os.write_file(os.join_path(tmp, 'db', 'local', 'ALPM_DB_VERSION'), '9\n')! os.write_file(os.join_path(entry, 'desc'), '%NAME%\n${name}\n\n%VERSION%\n1.0-1\n\n')! os.write_file(os.join_path(entry, 'files'), '%FILES%\n\n')! if mtree.len > 0 { os.write_file_array(os.join_path(entry, 'mtree'), mtree)! } handle := nspkg.initialize(os.join_path(tmp, 'root') + '/', os.join_path(tmp, 'db') + '/')! pkg := handle.find_local_pkg(name) or { return error('${name} is not installed') } return handle, pkg } // gzipped is text compressed the way package mtrees ship fn gzipped(text string) ![]u8 { res := os.exec(['sh', '-c', 'printf %s "\$0" | gzip -9 | base64 -w0', text]) if res.exit_code != 0 { return error('gzip failed: ${res.output}') } return base64.decode(res.output.trim_space()) } fn test_verify_pkg_full_broken_mtree() ! { tmp := os.join_path(os.vtmp_dir(), 'vpac_test_mtree_${os.getpid()}') os.rmdir_all(tmp) or {} defer { os.rmdir_all(tmp) or {} } whole := gzipped('#mtree\n/set type=file uid=0 gid=0 mode=644\n./usr time=1.0 type=dir\n')! // cut off inside the compressed stream: the read fails part way, and // that failure — libalpm's reason — is the package's verdict, not a // package without an mtree handle, pkg := mtree_pkg(tmp, 'broken', whole[..whole.len / 2])! defer { handle.release() or {} } altered, lines := verify_pkg_full(handle, pkg) assert altered == 1, lines.str() assert lines.len == 1 assert lines[0].problem assert lines[0].text.starts_with('broken: the mtree file cannot be read (libarchive error): '), lines[0].text assert lines[0].text.ends_with('/local/broken-1.0-1/mtree'), lines[0].text } fn test_verify_pkg_full_no_mtree() ! { tmp := os.join_path(os.vtmp_dir(), 'vpac_test_nomtree_${os.getpid()}') os.rmdir_all(tmp) or {} defer { os.rmdir_all(tmp) or {} } // a package built before mtrees existed: noted, not a problem handle, pkg := mtree_pkg(tmp, 'old', [])! defer { handle.release() or {} } altered, lines := verify_pkg_full(handle, pkg) assert altered == 0 assert lines.map(it.text) == ['old: no mtree file'] assert !lines[0].problem } fn test_verify_pkg_full_mtree_read_failure() ! { tmp := os.join_path(os.vtmp_dir(), 'vpac_test_mtree_read_${os.getpid()}') os.rmdir_all(tmp) or {} defer { os.rmdir_all(tmp) or {} } // the mtree opens, and an entry of it is not one: the read stops part // way, and the package's verdict carries libarchive's own reason handle, pkg := mtree_pkg(tmp, 'midway', gzipped('#mtree\n./a type=bogus\n')!)! defer { handle.release() or {} } altered, lines := verify_pkg_full(handle, pkg) assert altered == 1, lines.str() assert lines.len == 1 assert lines[0].problem assert lines[0].text.starts_with('midway: reading the mtree failed: '), lines[0].text reason := lines[0].text.all_after('failed: ') assert reason != '' && reason != 'unexpected error', lines[0].text }