| 1 | module main |
| 2 | |
| 3 | import alpm |
| 4 | import crypto.sha256 |
| 5 | import encoding.base64 |
| 6 | import os |
| 7 | |
| 8 | // a file on disk and the mtree entry that matches it exactly |
| 9 | fn full_fixture(dir string) !(string, MtreeEntry) { |
| 10 | path := os.join_path(dir, 'f') |
| 11 | os.write_file(path, 'hello\n')! |
| 12 | os.chmod(path, 0o644)! |
| 13 | os.utime(path, 1700000000, 1700000000)! |
| 14 | st := os.lstat(path)! |
| 15 | return path, MtreeEntry{ |
| 16 | path: 'f' |
| 17 | kind: ae_ifreg |
| 18 | mode: 0o644 |
| 19 | uid: i64(st.uid) |
| 20 | gid: i64(st.gid) |
| 21 | mtime: 1700000000 |
| 22 | size: 6 |
| 23 | sha256: sha256.hexhash('hello\n') |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | fn texts(findings []VerifyLine) []string { |
| 28 | return findings.map(if it.problem { |
| 29 | it.text.all_after('(') |
| 30 | } else { |
| 31 | 'note ${it.text.all_after('(')}' |
| 32 | }) |
| 33 | } |
| 34 | |
| 35 | fn test_check_file_full() ! { |
| 36 | tmp := os.join_path(os.vtmp_dir(), 'vpac_test_verify_${os.getpid()}') |
| 37 | os.mkdir_all(tmp)! |
| 38 | defer { os.rmdir_all(tmp) or {} } |
| 39 | path, e := full_fixture(tmp)! |
| 40 | |
| 41 | assert check_file_full(path, e, false) == [] |
| 42 | assert texts(check_file_full(path, MtreeEntry{ ...e, size: 7 }, false)) == ['size mismatch)'] |
| 43 | assert texts(check_file_full(path, MtreeEntry{ ...e, sha256: '00'.repeat(32) }, false)) == [ |
| 44 | 'SHA256 checksum mismatch)', |
| 45 | ] |
| 46 | assert texts(check_file_full(path, MtreeEntry{ ...e, mtime: 1 }, false)) == [ |
| 47 | 'modification time mismatch)', |
| 48 | ] |
| 49 | assert texts(check_file_full(path, MtreeEntry{ ...e, mode: 0o600 }, false)) == [ |
| 50 | 'permissions mismatch)', |
| 51 | ] |
| 52 | assert texts(check_file_full(path, MtreeEntry{ ...e, uid: e.uid + 1 }, false)) == [ |
| 53 | 'UID mismatch)', |
| 54 | ] |
| 55 | assert texts(check_file_full(path, MtreeEntry{ ...e, sha256: '' }, false)) == [ |
| 56 | 'SHA256 checksum information not available)', |
| 57 | ] |
| 58 | // the wrong type stops the checks there |
| 59 | assert texts(check_file_full(path, MtreeEntry{ ...e, kind: ae_ifdir, size: 0 }, false)) == [ |
| 60 | 'file type mismatch)', |
| 61 | ] |
| 62 | assert texts(check_file_full(os.join_path(tmp, 'gone'), e, false)) == ['missing)'] |
| 63 | |
| 64 | // a backup file may change in time, size and content: notes, no problem |
| 65 | changed := MtreeEntry{ |
| 66 | ...e |
| 67 | mtime: 1 |
| 68 | size: 99 |
| 69 | sha256: '00'.repeat(32) |
| 70 | } |
| 71 | findings := check_file_full(path, changed, true) |
| 72 | assert !findings.any(it.problem) |
| 73 | assert texts(findings) == ['note modification time mismatch)', 'note size mismatch)', |
| 74 | 'note SHA256 checksum mismatch)'] |
| 75 | // but not in mode |
| 76 | assert texts(check_file_full(path, MtreeEntry{ ...e, mode: 0o600 }, true)) == [ |
| 77 | 'permissions mismatch)', |
| 78 | ] |
| 79 | } |
| 80 | |
| 81 | fn test_check_file_full_symlink() ! { |
| 82 | tmp := os.join_path(os.vtmp_dir(), 'vpac_test_verify_link_${os.getpid()}') |
| 83 | os.mkdir_all(tmp)! |
| 84 | defer { os.rmdir_all(tmp) or {} } |
| 85 | link := os.join_path(tmp, 'l') |
| 86 | os.symlink('f', link)! |
| 87 | st := os.lstat(link)! |
| 88 | e := MtreeEntry{ |
| 89 | path: 'l' |
| 90 | kind: ae_iflnk |
| 91 | mode: st.mode & mode_bits |
| 92 | uid: i64(st.uid) |
| 93 | gid: i64(st.gid) |
| 94 | mtime: st.mtime |
| 95 | symlink: 'f' |
| 96 | } |
| 97 | // a link is checked as a link: its target need not exist |
| 98 | assert check_file_full(link, e, false) == [] |
| 99 | assert texts(check_file_full(link, MtreeEntry{ ...e, symlink: 'g' }, false)) == [ |
| 100 | 'symlink path mismatch)', |
| 101 | ] |
| 102 | } |
| 103 | |
| 104 | fn test_digest_hex() { |
| 105 | zero := []u8{len: 32} |
| 106 | assert digest_hex(zero.data) == '' |
| 107 | mut d := []u8{len: 32} |
| 108 | d[0] = 0xab |
| 109 | d[31] = 0x01 |
| 110 | assert digest_hex(d.data) == 'ab' + '00'.repeat(30) + '01' |
| 111 | assert digest_hex(unsafe { nil }) == '' |
| 112 | } |
| 113 | |
| 114 | fn test_file_sha256() ! { |
| 115 | tmp := os.join_path(os.vtmp_dir(), 'vpac_test_sha_${os.getpid()}') |
| 116 | os.mkdir_all(tmp)! |
| 117 | defer { os.rmdir_all(tmp) or {} } |
| 118 | // larger than one 64 KiB chunk |
| 119 | data := 'abcdefgh'.repeat(20000) |
| 120 | path := os.join_path(tmp, 'big') |
| 121 | os.write_file(path, data)! |
| 122 | assert file_sha256(path)! == sha256.hexhash(data) |
| 123 | assert file_sha256(os.join_path(tmp, 'nope')) or { 'error' } == 'error' |
| 124 | } |
| 125 | |
| 126 | // a read that fails never comes back as the partial file's digest: a |
| 127 | // truncated hash would pass itself off as the whole file's |
| 128 | fn test_file_sha256_read_failure() ! { |
| 129 | tmp := os.join_path(os.vtmp_dir(), 'vpac_test_sha_deny_${os.getpid()}') |
| 130 | os.mkdir_all(tmp)! |
| 131 | defer { os.rmdir_all(tmp) or {} } |
| 132 | if sum := file_sha256(tmp) { |
| 133 | assert false, 'a directory hashed as "${sum[0..8]}…"' |
| 134 | } else { |
| 135 | assert err.msg() != '', err.msg() |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | // mtree_pkg installs one package in a temp local db with the given gzipped |
| 140 | // mtree text, and returns the handle and the package libalpm loads for it |
| 141 | fn mtree_pkg(tmp string, name string, mtree []u8) !(&alpm.Handle, &alpm.Package) { |
| 142 | entry := os.join_path(tmp, 'db', 'local', '${name}-1.0-1') |
| 143 | os.mkdir_all(entry)! |
| 144 | os.mkdir_all(os.join_path(tmp, 'root'))! |
| 145 | os.write_file(os.join_path(tmp, 'db', 'local', 'ALPM_DB_VERSION'), '9\n')! |
| 146 | os.write_file(os.join_path(entry, 'desc'), '%NAME%\n${name}\n\n%VERSION%\n1.0-1\n\n')! |
| 147 | os.write_file(os.join_path(entry, 'files'), '%FILES%\n\n')! |
| 148 | if mtree.len > 0 { |
| 149 | os.write_file_array(os.join_path(entry, 'mtree'), mtree)! |
| 150 | } |
| 151 | handle := alpm.initialize(os.join_path(tmp, 'root') + '/', os.join_path(tmp, 'db') + '/')! |
| 152 | pkg := handle.find_local_pkg(name) or { return error('${name} is not installed') } |
| 153 | return handle, pkg |
| 154 | } |
| 155 | |
| 156 | // gzipped is text compressed the way package mtrees ship |
| 157 | fn gzipped(text string) ![]u8 { |
| 158 | res := os.exec(['sh', '-c', 'printf %s "\$0" | gzip -9 | base64 -w0', text]) |
| 159 | if res.exit_code != 0 { |
| 160 | return error('gzip failed: ${res.output}') |
| 161 | } |
| 162 | return base64.decode(res.output.trim_space()) |
| 163 | } |
| 164 | |
| 165 | fn test_verify_pkg_full_broken_mtree() ! { |
| 166 | tmp := os.join_path(os.vtmp_dir(), 'vpac_test_mtree_${os.getpid()}') |
| 167 | os.rmdir_all(tmp) or {} |
| 168 | defer { os.rmdir_all(tmp) or {} } |
| 169 | whole := gzipped('#mtree\n/set type=file uid=0 gid=0 mode=644\n./usr time=1.0 type=dir\n')! |
| 170 | // cut off inside the compressed stream: the read fails part way, and |
| 171 | // that failure — libalpm's reason — is the package's verdict, not a |
| 172 | // package without an mtree |
| 173 | handle, pkg := mtree_pkg(tmp, 'broken', whole[..whole.len / 2])! |
| 174 | defer { handle.release() or {} } |
| 175 | altered, lines := verify_pkg_full(handle, pkg) |
| 176 | assert altered == 1, lines.str() |
| 177 | assert lines.len == 1 |
| 178 | assert lines[0].problem |
| 179 | assert lines[0].text.starts_with('broken: the mtree file cannot be read (libarchive error): '), lines[0].text |
| 180 | assert lines[0].text.ends_with('/local/broken-1.0-1/mtree'), lines[0].text |
| 181 | } |
| 182 | |
| 183 | fn test_verify_pkg_full_no_mtree() ! { |
| 184 | tmp := os.join_path(os.vtmp_dir(), 'vpac_test_nomtree_${os.getpid()}') |
| 185 | os.rmdir_all(tmp) or {} |
| 186 | defer { os.rmdir_all(tmp) or {} } |
| 187 | // a package built before mtrees existed: noted, not a problem |
| 188 | handle, pkg := mtree_pkg(tmp, 'old', [])! |
| 189 | defer { handle.release() or {} } |
| 190 | altered, lines := verify_pkg_full(handle, pkg) |
| 191 | assert altered == 0 |
| 192 | assert lines.map(it.text) == ['old: no mtree file'] |
| 193 | assert !lines[0].problem |
| 194 | } |
| 195 | |
| 196 | fn test_verify_pkg_full_mtree_read_failure() ! { |
| 197 | tmp := os.join_path(os.vtmp_dir(), 'vpac_test_mtree_read_${os.getpid()}') |
| 198 | os.rmdir_all(tmp) or {} |
| 199 | defer { os.rmdir_all(tmp) or {} } |
| 200 | // the mtree opens, and an entry of it is not one: the read stops part |
| 201 | // way, and the package's verdict carries libarchive's own reason |
| 202 | handle, pkg := mtree_pkg(tmp, 'midway', gzipped('#mtree\n./a type=bogus\n')!)! |
| 203 | defer { handle.release() or {} } |
| 204 | altered, lines := verify_pkg_full(handle, pkg) |
| 205 | assert altered == 1, lines.str() |
| 206 | assert lines.len == 1 |
| 207 | assert lines[0].problem |
| 208 | assert lines[0].text.starts_with('midway: reading the mtree failed: '), lines[0].text |
| 209 | reason := lines[0].text.all_after('failed: ') |
| 210 | assert reason != '' && reason != 'unexpected error', lines[0].text |
| 211 | } |
| 212 | |