module engine import nspkg.tests.archivefixtures import compress.gzip import os fn test_package_archive_scan_and_members() ! { dir := os.join_path(os.vtmp_dir(), 'native-pkg-${os.getpid()}') os.mkdir_all(dir)! defer { os.rmdir_all(dir) or { eprintln(err) } } info := os.read_file(os.join_path(@VMODROOT, 'tests', 'fixtures', 'pkginfo'))! for compressed in [false, true] { path := os.join_path(dir, '${compressed}.pkg.tar') archivefixtures.write(path, [ archivefixtures.Entry{ name: 'etc/widget.conf', text: 'config' }, archivefixtures.Entry{ name: '.INSTALL', text: 'post_install() { :; }' }, archivefixtures.Entry{ name: '.PKGINFO', text: info }, archivefixtures.Entry{ name: '.CHANGELOG', text: 'release one\nrelease two\n' }, archivefixtures.Entry{ name: 'usr/bin', directory: true }, archivefixtures.Entry{ name: 'usr/bin/widget', text: 'payload', mode: 0o755 }, archivefixtures.Entry{ name: 'usr/bin/alias', symlink: 'widget' }, ], compressed)! pkg := read_package_archive(path, true)! assert pkg.name == 'widget' assert pkg.filename == path assert pkg.scriptlet assert pkg.origin == .file assert pkg.validation == 1 assert pkg.size == i64(os.file_size(path)) assert pkg.files.map(it.name) == ['etc/widget.conf', 'usr/bin/', 'usr/bin/alias', 'usr/bin/widget'] assert pkg.find_file('usr/bin/widget')?.size == 7 assert pkg.find_file('usr/bin/widget')?.mode == 0o100755 assert pkg.find_file('usr/bin/alias')?.mode & 0o170000 == 0o120000 changelog := pkg.read_changelog()! assert changelog.found assert changelog.data.bytestr() == 'release one\nrelease two\n' assert !read_archive_member(path, '.MISSING')!.found partial := read_package_archive(path, false)! assert partial.name == pkg.name assert partial.files.len == 0 } } fn test_mtree_overrides_scanned_file_metadata() ! { dir := os.join_path(os.vtmp_dir(), 'native-pkg-mtree-${os.getpid()}') os.mkdir_all(dir)! defer { os.rmdir_all(dir) or { eprintln(err) } } info := 'pkgname = foo\npkgver = 1-1\n' 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' for compressed in [false, true] { data := if compressed { gzip.compress(mtree.bytes())! } else { mtree.bytes() } entries := read_mtree(data)! foo := entries.filter(it.path == 'usr/bin/foo')[0] assert foo.size == 42 assert foo.uid == 12 assert foo.gid == 34 assert foo.mtime == 1234 assert foo.sha256 == 'ab'.repeat(32) assert entries.filter(it.path == 'usr/bin/link')[0].symlink == 'foo' path := os.join_path(dir, '${compressed}.pkg.tar') archivefixtures.write(path, [ archivefixtures.Entry{ name: 'discarded', text: 'old' }, archivefixtures.Entry{ name: '.PKGINFO', text: info }, archivefixtures.Entry{ name: '.MTREE', text: data.bytestr() }, archivefixtures.Entry{ name: 'usr/bin/foo', text: 'short' }, ], false)! pkg := read_package_archive(path, true)! assert pkg.scriptlet assert pkg.find_file('discarded') == none assert pkg.files.map(it.name) == ['usr/', 'usr/bin/', 'usr/bin/foo', 'usr/bin/link'] assert pkg.find_file('usr/bin/foo')?.size == 42 assert pkg.find_file('usr/bin/foo')?.mode == 0o100640 } } fn test_invalid_mtree_fallback_and_invalid_package_failures() ! { dir := os.join_path(os.vtmp_dir(), 'native-pkg-bad-${os.getpid()}') os.mkdir_all(dir)! defer { os.rmdir_all(dir) or { eprintln(err) } } path := os.join_path(dir, 'test.pkg.tar') archivefixtures.write(path, [ archivefixtures.Entry{ name: '.PKGINFO', text: 'pkgname = foo\npkgver = 1-1\n' }, archivefixtures.Entry{ name: '.MTREE', text: 'not an mtree' }, archivefixtures.Entry{ name: 'usr/bin/foo', text: 'data' }, ], false)! pkg := read_package_archive(path, true)! assert pkg.files.map(it.name) == ['usr/bin/foo'] assert pkg.diagnostics.len > 0 for info in ['', 'pkgname = foo\n', 'pkgname = foo\npkgver = 1\n', 'pkgname = .invalid\npkgver = 1-1\n', 'pkgname = foo\npkgver = 1-1\nxdata = invalid\n'] { archivefixtures.write(path, [archivefixtures.Entry{ name: '.PKGINFO', text: info }], false)! if _ := read_package_archive(path, true) { assert false, 'accepted invalid package metadata' } } archivefixtures.write(path, [archivefixtures.Entry{ name: 'usr/bin/foo', text: 'data' }], false)! if _ := read_package_archive(path, true) { assert false, 'accepted missing .PKGINFO' } bytes := archivefixtures.tar_bytes([archivefixtures.Entry{ name: '.PKGINFO', text: 'pkgname = foo\npkgver = 1-1\n' + '# padding\n'.repeat(100) }]) os.write_file_array(path, bytes[..700])! if _ := read_package_archive(path, true) { assert false, 'accepted truncated package metadata' } }