vpac / src / files_test.v
231 lines · 214 sloc · 8.95 KB · 841315c100c81d9e6b14c4b55a25d4e3d12db762
Raw
1module main
2
3import nspkg
4import os
5import testdb
6
7// OwnerFixture is a small filespace laid out the way a merged-/usr system
8// lays one out, with the links pacman's owner query has to see through or
9// keep: bin is a link to usr/bin, usr/bin/alias a link to a file beside it
10struct OwnerFixture {
11 tmp string
12 real string // tmp with its own links resolved: what canonical paths start with
13}
14
15fn owner_fixture(name string) !OwnerFixture {
16 tmp := os.join_path(os.vtmp_dir(), 'vpac_test_files_${name}_${os.getpid()}')
17 os.rmdir_all(tmp) or {}
18 for d in ['usr/bin', 'usr/share/doc', 'etc', 'nopath'] {
19 os.mkdir_all(os.join_path(tmp, d))!
20 }
21 os.write_file(os.join_path(tmp, 'usr/bin/tool'), '#!/bin/sh\n')!
22 os.chmod(os.join_path(tmp, 'usr/bin/tool'), 0o755)!
23 // pacman's PATH search takes any name that exists, executable or not
24 os.write_file(os.join_path(tmp, 'usr/bin/plain'), '')!
25 os.write_file(os.join_path(tmp, 'etc/fstab'), '')!
26 os.symlink('usr/bin', os.join_path(tmp, 'bin'))!
27 os.symlink('tool', os.join_path(tmp, 'usr/bin/alias'))!
28 os.symlink('nowhere', os.join_path(tmp, 'dangling'))!
29 return OwnerFixture{
30 tmp: tmp
31 real: os.real_path(tmp)
32 }
33}
34
35// in_fixture runs f with the root at the fixture, the working directory in
36// it and PATH set; the globals and the process state are put back after
37fn in_fixture(fx OwnerFixture, cwd string, path string, f fn () !) ! {
38 old_root := root_path
39 old_path := os.getenv('PATH')
40 old_cwd := os.getwd()
41 defer {
42 root_path = old_root
43 os.setenv('PATH', old_path, true)
44 os.chdir(old_cwd) or {}
45 }
46 root_path = fx.real + '/'
47 os.setenv('PATH', path, true)
48 os.chdir(os.join_path(fx.tmp, cwd))!
49 f()!
50}
51
52fn oq(target string) !OwnerQuery {
53 return owner_query(target)!
54}
55
56fn test_owner_query_resolves_the_directory_not_the_name() ! {
57 fx := owner_fixture('resolve')!
58 defer { os.rmdir_all(fx.tmp) or {} }
59 in_fixture(fx, '', '/nonexistent', fn [fx] () ! {
60 // a merged-/usr link in the directory part is seen through: pacman
61 // answers /bin/ls as /usr/bin/ls
62 assert oq('${fx.tmp}/bin/tool')! == OwnerQuery{'usr/bin/tool', false}
63 // '..' and '.' in the directory part are resolved too
64 assert oq('${fx.tmp}/usr/../usr/bin/tool')! == OwnerQuery{'usr/bin/tool', false}
65 assert oq('${fx.tmp}/usr/./bin/tool')! == OwnerQuery{'usr/bin/tool', false}
66 // the name itself stays the link: a filelist lists the link, and its
67 // owner is the answer, not the target's
68 assert oq('${fx.tmp}/usr/bin/alias')! == OwnerQuery{'usr/bin/alias', false}
69 assert oq('./usr/bin/alias')! == OwnerQuery{'usr/bin/alias', false}
70 assert oq('${fx.tmp}/bin/alias')! == OwnerQuery{'usr/bin/alias', false}
71 // a dangling link exists as itself
72 assert oq('dangling')! == OwnerQuery{'dangling', false}
73 // a relative path, through a link
74 assert oq('bin/tool')! == OwnerQuery{'usr/bin/tool', false}
75 })!
76}
77
78fn test_owner_query_directories() ! {
79 fx := owner_fixture('dirs')!
80 defer { os.rmdir_all(fx.tmp) or {} }
81 in_fixture(fx, '', '/nonexistent', fn [fx] () ! {
82 // a directory is asked for with its trailing slash, as filelists
83 // spell one, given with slashes or not
84 assert oq('${fx.tmp}/usr/share/doc')! == OwnerQuery{'usr/share/doc/', true}
85 assert oq('${fx.tmp}/usr/share/doc///')! == OwnerQuery{'usr/share/doc/', true}
86 // a link to a directory is the link, even with a slash after it: the
87 // slash is dropped so lstat does not follow it, and what is there,
88 // a link, says it is no directory
89 assert oq('${fx.tmp}/bin')! == OwnerQuery{'bin', false}
90 assert oq('${fx.tmp}/bin/')! == OwnerQuery{'bin', false}
91 // a missing path is a directory by its slash alone
92 assert oq('${fx.tmp}/usr/share/gone/')! == OwnerQuery{'usr/share/gone/', true}
93 assert oq('${fx.tmp}/usr/share/gone')! == OwnerQuery{'usr/share/gone', false}
94 // a path ending in '.' or '..' is resolved whole
95 os.chdir(os.join_path(fx.tmp, 'usr/bin'))!
96 assert oq('.')! == OwnerQuery{'usr/bin/', true}
97 assert oq('..')! == OwnerQuery{'usr/', true}
98 assert oq('../bin/..')! == OwnerQuery{'usr/', true}
99 })!
100}
101
102fn test_owner_query_bare_names() ! {
103 fx := owner_fixture('names')!
104 defer { os.rmdir_all(fx.tmp) or {} }
105 in_fixture(fx, 'etc', '/nonexistent::${fx.tmp}/bin/', fn [fx] () ! {
106 // a name the working directory holds is that file, not a command
107 assert oq('fstab')! == OwnerQuery{'etc/fstab', false}
108 // a name it lacks is looked up in PATH, empty entries and trailing
109 // slashes and all; the PATH entry's link is seen through
110 assert oq('tool')! == OwnerQuery{'usr/bin/tool', false}
111 // a PATH hit need not be executable, and a link found there stays
112 // the link
113 assert oq('plain')! == OwnerQuery{'usr/bin/plain', false}
114 assert oq('alias')! == OwnerQuery{'usr/bin/alias', false}
115 // a name found nowhere is the working directory's missing file, as
116 // in pacman, so a deleted file still finds the package that listed it
117 assert oq('nosuch')! == OwnerQuery{'etc/nosuch', false}
118 })!
119 in_fixture(fx, 'nopath', '', fn [fx] () ! {
120 // no PATH at all: the name stays in the working directory
121 assert oq('tool')! == OwnerQuery{'nopath/tool', false}
122 })!
123}
124
125fn test_owner_query_unresolvable_directory() ! {
126 fx := owner_fixture('missing')!
127 defer { os.rmdir_all(fx.tmp) or {} }
128 in_fixture(fx, '', '/nonexistent', fn [fx] () ! {
129 // a directory that does not exist cannot be resolved: the path is
130 // made absolute as named, so it still answers inside the root
131 assert oq('${fx.tmp}/gone/dir/file')! == OwnerQuery{'gone/dir/file', false}
132 assert oq('gone/dir/file')! == OwnerQuery{'gone/dir/file', false}
133 assert oq('gone/../usr/bin/tool')! == OwnerQuery{'usr/bin/tool', false}
134 })!
135}
136
137fn test_owner_query_root_and_outside() ! {
138 old := root_path
139 defer { root_path = old }
140
141 root_path = '/'
142 // the root itself: no package lists it, but it is not outside the root
143 assert owner_query('/')!.rel == ''
144 assert owner_query('///')!.rel == ''
145
146 fx := owner_fixture('root')!
147 defer { os.rmdir_all(fx.tmp) or {} }
148 root_path = fx.real + '/'
149 assert owner_query(fx.real)!.rel == ''
150 assert owner_query(fx.real + '/')!.rel == ''
151 // a fake root that does not exist (yet) is still the root itself
152 root_path = '/vpac-no-such-root-${os.getpid()}/sys/'
153 assert owner_query(root_path)!.rel == ''
154 assert owner_query(root_path.trim_right('/'))!.rel == ''
155 for outside in ['/vpac-no-such-root-${os.getpid()}/sysfoo', '/etc', '/'] {
156 if got := owner_query(outside) {
157 assert false, '${outside} is outside ${root_path}, yet gave "${got.rel}"'
158 } else {
159 assert err.msg().starts_with('file is outside of root')
160 }
161 }
162
163 if _ := owner_query('') {
164 assert false, 'an empty path has no owner query'
165 }
166}
167
168fn test_lrealpath() ! {
169 fx := owner_fixture('lrealpath')!
170 defer { os.rmdir_all(fx.tmp) or {} }
171 assert lrealpath('${fx.tmp}/bin/alias')? == '${fx.real}/usr/bin/alias'
172 // '..' after a link climbs from the link's target, as the kernel does:
173 // bin is usr/bin, so bin/.. is usr, not the fixture's top
174 assert lrealpath('${fx.tmp}/bin/..')? == '${fx.real}/usr'
175 assert lrealpath('/')? == '/'
176 assert lrealpath('/usr')? == '/usr'
177 if got := lrealpath('${fx.tmp}/gone/file') {
178 assert false, 'an unresolvable directory gave ${got}'
179 }
180 assert realpath(fx.tmp + '/bin')? == fx.real + '/usr/bin'
181 if got := realpath(fx.tmp + '/dangling') {
182 assert false, 'a dangling link resolved to ${got}'
183 }
184}
185
186// write_owner_db lays out a local db whose packages own files
187fn write_owner_db(dbpath string, pkgs map[string][]string) ! {
188 mut out := []testdb.Pkg{}
189 for name, files in pkgs {
190 out << testdb.Pkg{
191 name: name
192 files: files
193 }
194 }
195 testdb.write_local(dbpath, out)!
196}
197
198fn test_file_owners() ! {
199 tmp := os.join_path(os.vtmp_dir(), 'vpac_test_files_owners_${os.getpid()}')
200 os.mkdir_all(os.join_path(tmp, 'root'))!
201 defer { os.rmdir_all(tmp) or {} }
202 write_owner_db(os.join_path(tmp, 'db'), {
203 'filesystem': ['bin', 'usr/', 'usr/bin/', 'usr/share/']
204 'coreutils': ['usr/', 'usr/bin/', 'usr/bin/ls', 'usr/share/', 'usr/share/man/']
205 'zsh': ['usr/', 'usr/bin/', 'usr/bin/zsh', 'usr/share/', 'usr/share/man/']
206 })!
207 handle := nspkg.initialize(os.join_path(tmp, 'root') + '/', os.join_path(tmp, 'db') + '/')!
208 defer { handle.release() or {} }
209 mut localdb := handle.get_localdb()!
210 mut pkgcache := localdb.get_pkgcache() or { return error('the test db has no pkgcache') }
211 pkgs := pkgcache.pkgs()
212 owners := fn [pkgs] (rel string, dir bool) []string {
213 mut names := file_owners(pkgs, OwnerQuery{rel, dir}).map(it.get_name())
214 names.sort()
215 return names
216 }
217
218 // a file has its one owner
219 assert owners('usr/bin/ls', false) == ['coreutils']
220 // a directory answers every package that lists it
221 assert owners('usr/share/man/', true) == ['coreutils', 'zsh']
222 assert owners('usr/bin/', true) == ['coreutils', 'filesystem', 'zsh']
223 // the match is exact: a directory is not found without its slash, a file
224 // not with one, and a link is owned as itself
225 assert owners('usr/bin', false) == []
226 assert owners('usr/bin/ls/', true) == []
227 assert owners('bin', false) == ['filesystem']
228 assert owners('usr/bin/nope', false) == []
229 // the root itself is listed by nobody
230 assert owners('', true) == []
231}
232