nspkg / engine / local_storage_test.v
225 lines · 217 sloc · 8.71 KB · c01b0795d1db56c7bce2539b2c46b110637fdc1a
Raw
1module engine
2
3import os
4
5#include "@VMODROOT/tests/local_storage_probe.h"
6
7fn C.v_alpm_limit_database_write() i32
8
9fn test_local_writer_and_reason_changes_round_trip() ! {
10 dir := os.join_path(os.vtmp_dir(), 'native-local-writer-${os.getpid()}')
11 os.mkdir_all(dir)!
12 defer { os.rmdir_all(dir) or { eprintln(err) } }
13 mut handle := initialize('/', dir)!
14 defer { handle.release() or { eprintln(err) } }
15 mut db := handle.local_database()!
16 mut pkg := parse_pkginfo('pkgname = app\npkgver = 2:1-2\npkgdesc = \nurl = \narch = any\ndepend = base>=2\noptdepend = tool: helps\nxdata = pkgtype=pkg\nbackup = etc/app.conf\n')!
17 pkg.files = [PackageFile{ name: 'etc/' }, PackageFile{ name: 'etc/app.conf' }]
18 pkg.reason = .depend
19 pkg.validation = 13
20 pkg.build_date = -1
21 pkg.install_date = 1700000000
22 old_mask := C.umask(0o777)
23 defer { C.umask(old_mask) }
24 db.prepare_local_package(&pkg)!
25 db.write_local_package(mut pkg, true, true)!
26 entry := os.join_path(db.path, 'app-2:1-2')
27 assert os.stat(entry)!.mode & 0o777 == 0o755
28 assert os.stat(os.join_path(entry, 'desc'))!.mode & 0o777 == 0o644
29 assert os.stat(os.join_path(entry, 'files'))!.mode & 0o777 == 0o644
30 assert os.read_file(os.join_path(entry, 'files'))! == '%FILES%\netc/\netc/app.conf\n\n%BACKUP%\netc/app.conf\t(null)\n\n'
31 description := os.read_file(os.join_path(entry, 'desc'))!
32 assert description.contains('%DESC%\n\n\n%URL%\n\n\n')
33 assert description.contains('%BUILDDATE%\n-1\n\n')
34 assert description.contains('%DEPENDS%\nbase>=2\n\n%OPTDEPENDS%\ntool: helps\n\n')
35 mut installed := db.get_pkg('app') or { return error('missing installed package') }
36 installed.set_reason(.explicit)!
37 assert installed.reason == .explicit
38 assert !os.read_file(os.join_path(entry, 'desc'))!.contains('%REASON%')
39 assert os.read_file(os.join_path(entry, 'files'))! == pkg.local_files()
40 os.chmod(os.join_path(entry, 'desc'), 0o600)!
41 installed.set_reason(.unknown)!
42 assert os.stat(os.join_path(entry, 'desc'))!.mode & 0o777 == 0o600
43 assert os.read_file(os.join_path(entry, 'desc'))!.contains('%REASON%\n2\n\n')
44 assert C.umask(0o777) == 0o777
45 os.write_file(os.join_path(entry, 'install'), 'script')!
46 os.write_file(os.join_path(entry, 'changelog'), 'changes')!
47 db.remove_local_package(installed)!
48 assert !os.exists(entry)
49 db.invalidate()!
50 assert db.get_pkg('app') == none
51}
52
53fn test_local_reason_failure_preserves_cache_and_rejects_unsafe_records() ! {
54 dir := os.join_path(os.vtmp_dir(), 'native-local-writer-failure-${os.getpid()}')
55 os.mkdir_all(dir)!
56 defer { os.rmdir_all(dir) or { eprintln(err) } }
57 mut handle := initialize('/', dir)!
58 defer { handle.release() or { eprintln(err) } }
59 mut db := handle.local_database()!
60 mut pkg := parse_pkginfo('pkgname = app\npkgver = 1-1\n')!
61 db.prepare_local_package(&pkg)!
62 db.write_local_package(mut pkg, true, true)!
63 entry := os.join_path(db.path, 'app-1-1')
64 path := os.join_path(entry, 'desc')
65 mut installed := db.get_pkg('app') or { return error('missing package') }
66 installed.load_description()!
67 victim := os.join_path(dir, 'victim')
68 os.write_file(victim, 'untouched')!
69 for kind in ['symlink', 'hardlink', 'directory'] {
70 os.rm(path)!
71 match kind {
72 'symlink' { os.symlink(victim, path)! }
73 'hardlink' {
74 assert C.link(unsafe { &char(victim.str) }, unsafe { &char(path.str) }) == 0
75 }
76 else { os.mkdir(path)! }
77 }
78 if _ := installed.set_reason(.depend) {
79 assert false, kind
80 }
81 assert handle.errno() == .db_write
82 assert installed.reason == .explicit
83 assert os.read_file(victim)! == 'untouched'
84 if kind == 'directory' { os.rmdir(path)! } else { os.rm(path)! }
85 os.write_file(path, pkg.local_description())!
86 }
87 // a loaded no-op never opens or recreates the record.
88 os.rm(path)!
89 installed.set_reason(.explicit)!
90 assert !os.exists(path)
91 os.write_file(path, pkg.local_description())!
92 if C.geteuid() != 0 {
93 os.chmod(path, 0o400)!
94 if _ := installed.set_reason(.depend) {
95 assert false
96 }
97 assert handle.errno() == .db_write
98 assert installed.reason == .explicit
99 os.chmod(path, 0o644)!
100 os.chmod(entry, 0o555)!
101 if _ := installed.set_reason(.depend) {
102 assert false
103 }
104 assert handle.errno() == .db_write
105 assert installed.reason == .explicit
106 os.chmod(entry, 0o755)!
107 assert os.read_file(path)! == pkg.local_description()
108 assert os.ls(entry)!.all(!it.starts_with('.alpm-'))
109 }
110 mut unowned := Package{ origin: .local_db }
111 if _ := unowned.set_reason(.depend) {
112 assert false
113 }
114 mut archive := handle.load_package('missing', false, 0) or { &Package{ owner: handle, origin: .file } }
115 if _ := archive.set_reason(.depend) {
116 assert false
117 }
118 assert handle.errno() == .wrong_args
119 os.mv(entry, entry + '.saved')!
120 os.symlink(entry + '.saved', entry)!
121 if _ := installed.set_reason(.depend) {
122 assert false
123 }
124 assert handle.errno() == .db_write
125 assert os.read_file(os.join_path(entry + '.saved', 'desc'))! == pkg.local_description()
126 os.rm(entry)!
127 os.mv(entry + '.saved', entry)!
128 local_path := db.path.trim_right('/')
129 os.mv(local_path, local_path + '.saved')!
130 os.symlink(local_path + '.saved', local_path)!
131 if _ := installed.set_reason(.depend) {
132 assert false
133 }
134 assert handle.errno() == .db_write
135 assert os.read_file(os.join_path(local_path + '.saved', 'app-1-1', 'desc'))! == pkg.local_description()
136 os.rm(local_path)!
137 os.mv(local_path + '.saved', local_path)!
138 os.mkdir(os.join_path(entry, 'nested'))!
139 if _ := db.remove_local_package(installed) {
140 assert false
141 }
142 assert handle.errno() == .db_remove
143 assert os.is_dir(os.join_path(entry, 'nested'))
144}
145
146fn test_local_writer_cannot_escape_package_identity() ! {
147 dir := os.join_path(os.vtmp_dir(), 'native-local-writer-names-${os.getpid()}')
148 os.mkdir_all(dir)!
149 defer { os.rmdir_all(dir) or { eprintln(err) } }
150 mut handle := initialize('/', dir)!
151 defer { handle.release() or { eprintln(err) } }
152 mut db := handle.local_database()!
153 for name in ['../escape', '.', '', 'bad\x00name', 'a'.repeat(256)] {
154 pkg := Package{ name: name, version: '1-1' }
155 if _ := db.prepare_local_package(&pkg) {
156 assert false
157 }
158 assert handle.errno() == .db_write
159 }
160 assert os.ls(db.path)! == ['ALPM_DB_VERSION']
161}
162
163fn test_partial_database_write_failure_never_replaces_the_old_record() ! {
164 dir := os.join_path(os.vtmp_dir(), 'native-local-writer-limit-${os.getpid()}')
165 os.mkdir_all(dir)!
166 defer { os.rmdir_all(dir) or { eprintln(err) } }
167 mut handle := initialize('/', dir)!
168 defer { handle.release() or { eprintln(err) } }
169 mut db := handle.local_database()!
170 mut pkg := parse_pkginfo('pkgname = app\npkgver = 1-1\npkgdesc = ${'x'.repeat(512)}\n')!
171 db.prepare_local_package(&pkg)!
172 db.write_local_package(mut pkg, true, true)!
173 mut installed := db.get_pkg('app') or { return error('missing package') }
174 installed.load_description()!
175 entry := os.join_path(db.path, 'app-1-1')
176 before := os.read_file(os.join_path(entry, 'desc'))!
177 child := C.fork()
178 assert child >= 0
179 if child == 0 {
180 if C.v_alpm_limit_database_write() != 0 { C._exit(2) }
181 if _ := installed.set_reason(.depend) { C._exit(3) }
182 if handle.errno() != .db_write || installed.reason != .explicit { C._exit(4) }
183 C._exit(0)
184 }
185 mut status := i32(0)
186 for {
187 if C.v_alpm_wait_child(child, &status, 0) >= 0 { break }
188 if C.errno != C.EINTR { return os.last_error() }
189 }
190 assert C.v_alpm_exit_status(status) == 0
191 assert os.read_file(os.join_path(entry, 'desc'))! == before
192 assert os.ls(entry)!.all(!it.starts_with('.alpm-'))
193}
194
195fn test_local_writer_matches_reference_records_when_supplied() ! {
196 // the root parity driver supplies C-written records; the native suite needs no libalpm.
197 reference := os.getenv('ALPM_LOCAL_WRITE_REFERENCE')
198 if reference == '' { return }
199 dir := os.join_path(os.vtmp_dir(), 'native-local-c-records-${os.getpid()}')
200 os.mkdir_all(dir)!
201 defer { os.rmdir_all(dir) or { eprintln(err) } }
202 mut handle := initialize('/', dir)!
203 defer { handle.release() or { eprintln(err) } }
204 mut db := handle.local_database()!
205 mut count := 0
206 for entry in os.ls(reference)! {
207 if entry == 'ALPM_DB_VERSION' { continue }
208 name, version := split_package_entry(entry)!
209 mut reference_pkg := Package{ name: name, version: version, origin: .local_db, desc_loaded: true, files_loaded: true }
210 description := os.read_file(os.join_path(reference, entry, 'desc'))!
211 files := os.read_file(os.join_path(reference, entry, 'files'))!
212 reference_pkg.parse_db_metadata(description)!
213 path := os.join_path(os.getenv('ALPM_LOCAL_WRITE_ARCHIVES'), '${name}.pkg.tar')
214 mut pkg := handle.load_package(path, true, 0)!
215 pkg.install_date = reference_pkg.install_date
216 pkg.reason = reference_pkg.reason
217 db.prepare_local_package(pkg)!
218 db.write_local_package(mut pkg, true, true)!
219 assert os.read_file(os.join_path(db.path, entry, 'desc'))! == description, entry
220 assert os.read_file(os.join_path(db.path, entry, 'files'))! == files, entry
221 count++
222 }
223 assert count == 8
224 println('native local writer matched 8 C records')
225}
226