module main import nspkg import os import testdb // OwnerFixture is a small filespace laid out the way a merged-/usr system // lays one out, with the links pacman's owner query has to see through or // keep: bin is a link to usr/bin, usr/bin/alias a link to a file beside it struct OwnerFixture { tmp string real string // tmp with its own links resolved: what canonical paths start with } fn owner_fixture(name string) !OwnerFixture { tmp := os.join_path(os.vtmp_dir(), 'vpac_test_files_${name}_${os.getpid()}') os.rmdir_all(tmp) or {} for d in ['usr/bin', 'usr/share/doc', 'etc', 'nopath'] { os.mkdir_all(os.join_path(tmp, d))! } os.write_file(os.join_path(tmp, 'usr/bin/tool'), '#!/bin/sh\n')! os.chmod(os.join_path(tmp, 'usr/bin/tool'), 0o755)! // pacman's PATH search takes any name that exists, executable or not os.write_file(os.join_path(tmp, 'usr/bin/plain'), '')! os.write_file(os.join_path(tmp, 'etc/fstab'), '')! os.symlink('usr/bin', os.join_path(tmp, 'bin'))! os.symlink('tool', os.join_path(tmp, 'usr/bin/alias'))! os.symlink('nowhere', os.join_path(tmp, 'dangling'))! return OwnerFixture{ tmp: tmp real: os.real_path(tmp) } } // in_fixture runs f with the root at the fixture, the working directory in // it and PATH set; the globals and the process state are put back after fn in_fixture(fx OwnerFixture, cwd string, path string, f fn () !) ! { old_root := root_path old_path := os.getenv('PATH') old_cwd := os.getwd() defer { root_path = old_root os.setenv('PATH', old_path, true) os.chdir(old_cwd) or {} } root_path = fx.real + '/' os.setenv('PATH', path, true) os.chdir(os.join_path(fx.tmp, cwd))! f()! } fn oq(target string) !OwnerQuery { return owner_query(target)! } fn test_owner_query_resolves_the_directory_not_the_name() ! { fx := owner_fixture('resolve')! defer { os.rmdir_all(fx.tmp) or {} } in_fixture(fx, '', '/nonexistent', fn [fx] () ! { // a merged-/usr link in the directory part is seen through: pacman // answers /bin/ls as /usr/bin/ls assert oq('${fx.tmp}/bin/tool')! == OwnerQuery{'usr/bin/tool', false} // '..' and '.' in the directory part are resolved too assert oq('${fx.tmp}/usr/../usr/bin/tool')! == OwnerQuery{'usr/bin/tool', false} assert oq('${fx.tmp}/usr/./bin/tool')! == OwnerQuery{'usr/bin/tool', false} // the name itself stays the link: a filelist lists the link, and its // owner is the answer, not the target's assert oq('${fx.tmp}/usr/bin/alias')! == OwnerQuery{'usr/bin/alias', false} assert oq('./usr/bin/alias')! == OwnerQuery{'usr/bin/alias', false} assert oq('${fx.tmp}/bin/alias')! == OwnerQuery{'usr/bin/alias', false} // a dangling link exists as itself assert oq('dangling')! == OwnerQuery{'dangling', false} // a relative path, through a link assert oq('bin/tool')! == OwnerQuery{'usr/bin/tool', false} })! } fn test_owner_query_directories() ! { fx := owner_fixture('dirs')! defer { os.rmdir_all(fx.tmp) or {} } in_fixture(fx, '', '/nonexistent', fn [fx] () ! { // a directory is asked for with its trailing slash, as filelists // spell one, given with slashes or not assert oq('${fx.tmp}/usr/share/doc')! == OwnerQuery{'usr/share/doc/', true} assert oq('${fx.tmp}/usr/share/doc///')! == OwnerQuery{'usr/share/doc/', true} // a link to a directory is the link, even with a slash after it: the // slash is dropped so lstat does not follow it, and what is there, // a link, says it is no directory assert oq('${fx.tmp}/bin')! == OwnerQuery{'bin', false} assert oq('${fx.tmp}/bin/')! == OwnerQuery{'bin', false} // a missing path is a directory by its slash alone assert oq('${fx.tmp}/usr/share/gone/')! == OwnerQuery{'usr/share/gone/', true} assert oq('${fx.tmp}/usr/share/gone')! == OwnerQuery{'usr/share/gone', false} // a path ending in '.' or '..' is resolved whole os.chdir(os.join_path(fx.tmp, 'usr/bin'))! assert oq('.')! == OwnerQuery{'usr/bin/', true} assert oq('..')! == OwnerQuery{'usr/', true} assert oq('../bin/..')! == OwnerQuery{'usr/', true} })! } fn test_owner_query_bare_names() ! { fx := owner_fixture('names')! defer { os.rmdir_all(fx.tmp) or {} } in_fixture(fx, 'etc', '/nonexistent::${fx.tmp}/bin/', fn [fx] () ! { // a name the working directory holds is that file, not a command assert oq('fstab')! == OwnerQuery{'etc/fstab', false} // a name it lacks is looked up in PATH, empty entries and trailing // slashes and all; the PATH entry's link is seen through assert oq('tool')! == OwnerQuery{'usr/bin/tool', false} // a PATH hit need not be executable, and a link found there stays // the link assert oq('plain')! == OwnerQuery{'usr/bin/plain', false} assert oq('alias')! == OwnerQuery{'usr/bin/alias', false} // a name found nowhere is the working directory's missing file, as // in pacman, so a deleted file still finds the package that listed it assert oq('nosuch')! == OwnerQuery{'etc/nosuch', false} })! in_fixture(fx, 'nopath', '', fn [fx] () ! { // no PATH at all: the name stays in the working directory assert oq('tool')! == OwnerQuery{'nopath/tool', false} })! } fn test_owner_query_unresolvable_directory() ! { fx := owner_fixture('missing')! defer { os.rmdir_all(fx.tmp) or {} } in_fixture(fx, '', '/nonexistent', fn [fx] () ! { // a directory that does not exist cannot be resolved: the path is // made absolute as named, so it still answers inside the root assert oq('${fx.tmp}/gone/dir/file')! == OwnerQuery{'gone/dir/file', false} assert oq('gone/dir/file')! == OwnerQuery{'gone/dir/file', false} assert oq('gone/../usr/bin/tool')! == OwnerQuery{'usr/bin/tool', false} })! } fn test_owner_query_root_and_outside() ! { old := root_path defer { root_path = old } root_path = '/' // the root itself: no package lists it, but it is not outside the root assert owner_query('/')!.rel == '' assert owner_query('///')!.rel == '' fx := owner_fixture('root')! defer { os.rmdir_all(fx.tmp) or {} } root_path = fx.real + '/' assert owner_query(fx.real)!.rel == '' assert owner_query(fx.real + '/')!.rel == '' // a fake root that does not exist (yet) is still the root itself root_path = '/vpac-no-such-root-${os.getpid()}/sys/' assert owner_query(root_path)!.rel == '' assert owner_query(root_path.trim_right('/'))!.rel == '' for outside in ['/vpac-no-such-root-${os.getpid()}/sysfoo', '/etc', '/'] { if got := owner_query(outside) { assert false, '${outside} is outside ${root_path}, yet gave "${got.rel}"' } else { assert err.msg().starts_with('file is outside of root') } } if _ := owner_query('') { assert false, 'an empty path has no owner query' } } fn test_lrealpath() ! { fx := owner_fixture('lrealpath')! defer { os.rmdir_all(fx.tmp) or {} } assert lrealpath('${fx.tmp}/bin/alias')? == '${fx.real}/usr/bin/alias' // '..' after a link climbs from the link's target, as the kernel does: // bin is usr/bin, so bin/.. is usr, not the fixture's top assert lrealpath('${fx.tmp}/bin/..')? == '${fx.real}/usr' assert lrealpath('/')? == '/' assert lrealpath('/usr')? == '/usr' if got := lrealpath('${fx.tmp}/gone/file') { assert false, 'an unresolvable directory gave ${got}' } assert realpath(fx.tmp + '/bin')? == fx.real + '/usr/bin' if got := realpath(fx.tmp + '/dangling') { assert false, 'a dangling link resolved to ${got}' } } // write_owner_db lays out a local db whose packages own files fn write_owner_db(dbpath string, pkgs map[string][]string) ! { mut out := []testdb.Pkg{} for name, files in pkgs { out << testdb.Pkg{ name: name files: files } } testdb.write_local(dbpath, out)! } fn test_file_owners() ! { tmp := os.join_path(os.vtmp_dir(), 'vpac_test_files_owners_${os.getpid()}') os.mkdir_all(os.join_path(tmp, 'root'))! defer { os.rmdir_all(tmp) or {} } write_owner_db(os.join_path(tmp, 'db'), { 'filesystem': ['bin', 'usr/', 'usr/bin/', 'usr/share/'] 'coreutils': ['usr/', 'usr/bin/', 'usr/bin/ls', 'usr/share/', 'usr/share/man/'] 'zsh': ['usr/', 'usr/bin/', 'usr/bin/zsh', 'usr/share/', 'usr/share/man/'] })! handle := nspkg.initialize(os.join_path(tmp, 'root') + '/', os.join_path(tmp, 'db') + '/')! defer { handle.release() or {} } mut localdb := handle.get_localdb()! mut pkgcache := localdb.get_pkgcache() or { return error('the test db has no pkgcache') } pkgs := pkgcache.pkgs() owners := fn [pkgs] (rel string, dir bool) []string { mut names := file_owners(pkgs, OwnerQuery{rel, dir}).map(it.get_name()) names.sort() return names } // a file has its one owner assert owners('usr/bin/ls', false) == ['coreutils'] // a directory answers every package that lists it assert owners('usr/share/man/', true) == ['coreutils', 'zsh'] assert owners('usr/bin/', true) == ['coreutils', 'filesystem', 'zsh'] // the match is exact: a directory is not found without its slash, a file // not with one, and a link is owned as itself assert owners('usr/bin', false) == [] assert owners('usr/bin/ls/', true) == [] assert owners('bin', false) == ['filesystem'] assert owners('usr/bin/nope', false) == [] // the root itself is listed by nobody assert owners('', true) == [] }