nspkg / engine / package_archive_test.v
106 lines · 102 sloc · 4.7 KB · c01b0795d1db56c7bce2539b2c46b110637fdc1a
Raw
1module engine
2
3import nspkg.tests.archivefixtures
4import compress.gzip
5import os
6
7fn test_package_archive_scan_and_members() ! {
8 dir := os.join_path(os.vtmp_dir(), 'native-pkg-${os.getpid()}')
9 os.mkdir_all(dir)!
10 defer { os.rmdir_all(dir) or { eprintln(err) } }
11 info := os.read_file(os.join_path(@VMODROOT, 'tests', 'fixtures', 'pkginfo'))!
12 for compressed in [false, true] {
13 path := os.join_path(dir, '${compressed}.pkg.tar')
14 archivefixtures.write(path, [
15 archivefixtures.Entry{ name: 'etc/widget.conf', text: 'config' },
16 archivefixtures.Entry{ name: '.INSTALL', text: 'post_install() { :; }' },
17 archivefixtures.Entry{ name: '.PKGINFO', text: info },
18 archivefixtures.Entry{ name: '.CHANGELOG', text: 'release one\nrelease two\n' },
19 archivefixtures.Entry{ name: 'usr/bin', directory: true },
20 archivefixtures.Entry{ name: 'usr/bin/widget', text: 'payload', mode: 0o755 },
21 archivefixtures.Entry{ name: 'usr/bin/alias', symlink: 'widget' },
22 ], compressed)!
23 pkg := read_package_archive(path, true)!
24 assert pkg.name == 'widget'
25 assert pkg.filename == path
26 assert pkg.scriptlet
27 assert pkg.origin == .file
28 assert pkg.validation == 1
29 assert pkg.size == i64(os.file_size(path))
30 assert pkg.files.map(it.name) == ['etc/widget.conf', 'usr/bin/', 'usr/bin/alias',
31 'usr/bin/widget']
32 assert pkg.find_file('usr/bin/widget')?.size == 7
33 assert pkg.find_file('usr/bin/widget')?.mode == 0o100755
34 assert pkg.find_file('usr/bin/alias')?.mode & 0o170000 == 0o120000
35 changelog := pkg.read_changelog()!
36 assert changelog.found
37 assert changelog.data.bytestr() == 'release one\nrelease two\n'
38 assert !read_archive_member(path, '.MISSING')!.found
39 partial := read_package_archive(path, false)!
40 assert partial.name == pkg.name
41 assert partial.files.len == 0
42 }
43}
44
45fn test_mtree_overrides_scanned_file_metadata() ! {
46 dir := os.join_path(os.vtmp_dir(), 'native-pkg-mtree-${os.getpid()}')
47 os.mkdir_all(dir)!
48 defer { os.rmdir_all(dir) or { eprintln(err) } }
49 info := 'pkgname = foo\npkgver = 1-1\n'
50 mtree := '#mtree\n/set type=file uid=12 gid=34 mode=0640\n./.INSTALL size=0\n./usr type=dir mode=0755\n./usr/bin type=dir mode=0755\n./usr/bin/foo size=42 time=1234.0 sha256digest=' + 'ab'.repeat(32) + '\n./usr/bin/link type=link link=foo\n'
51 for compressed in [false, true] {
52 data := if compressed { gzip.compress(mtree.bytes())! } else { mtree.bytes() }
53 entries := read_mtree(data)!
54 foo := entries.filter(it.path == 'usr/bin/foo')[0]
55 assert foo.size == 42
56 assert foo.uid == 12
57 assert foo.gid == 34
58 assert foo.mtime == 1234
59 assert foo.sha256 == 'ab'.repeat(32)
60 assert entries.filter(it.path == 'usr/bin/link')[0].symlink == 'foo'
61 path := os.join_path(dir, '${compressed}.pkg.tar')
62 archivefixtures.write(path, [
63 archivefixtures.Entry{ name: 'discarded', text: 'old' },
64 archivefixtures.Entry{ name: '.PKGINFO', text: info },
65 archivefixtures.Entry{ name: '.MTREE', text: data.bytestr() },
66 archivefixtures.Entry{ name: 'usr/bin/foo', text: 'short' },
67 ], false)!
68 pkg := read_package_archive(path, true)!
69 assert pkg.scriptlet
70 assert pkg.find_file('discarded') == none
71 assert pkg.files.map(it.name) == ['usr/', 'usr/bin/', 'usr/bin/foo', 'usr/bin/link']
72 assert pkg.find_file('usr/bin/foo')?.size == 42
73 assert pkg.find_file('usr/bin/foo')?.mode == 0o100640
74 }
75}
76
77fn test_invalid_mtree_fallback_and_invalid_package_failures() ! {
78 dir := os.join_path(os.vtmp_dir(), 'native-pkg-bad-${os.getpid()}')
79 os.mkdir_all(dir)!
80 defer { os.rmdir_all(dir) or { eprintln(err) } }
81 path := os.join_path(dir, 'test.pkg.tar')
82 archivefixtures.write(path, [
83 archivefixtures.Entry{ name: '.PKGINFO', text: 'pkgname = foo\npkgver = 1-1\n' },
84 archivefixtures.Entry{ name: '.MTREE', text: 'not an mtree' },
85 archivefixtures.Entry{ name: 'usr/bin/foo', text: 'data' },
86 ], false)!
87 pkg := read_package_archive(path, true)!
88 assert pkg.files.map(it.name) == ['usr/bin/foo']
89 assert pkg.diagnostics.len > 0
90 for info in ['', 'pkgname = foo\n', 'pkgname = foo\npkgver = 1\n',
91 'pkgname = .invalid\npkgver = 1-1\n', 'pkgname = foo\npkgver = 1-1\nxdata = invalid\n'] {
92 archivefixtures.write(path, [archivefixtures.Entry{ name: '.PKGINFO', text: info }], false)!
93 if _ := read_package_archive(path, true) {
94 assert false, 'accepted invalid package metadata'
95 }
96 }
97 archivefixtures.write(path, [archivefixtures.Entry{ name: 'usr/bin/foo', text: 'data' }], false)!
98 if _ := read_package_archive(path, true) {
99 assert false, 'accepted missing .PKGINFO'
100 }
101 bytes := archivefixtures.tar_bytes([archivefixtures.Entry{ name: '.PKGINFO', text: 'pkgname = foo\npkgver = 1-1\n' + '# padding\n'.repeat(100) }])
102 os.write_file_array(path, bytes[..700])!
103 if _ := read_package_archive(path, true) {
104 assert false, 'accepted truncated package metadata'
105 }
106}
107