nspkg / engine / removal_execution_test.v
242 lines · 230 sloc · 9.14 KB · c01b0795d1db56c7bce2539b2c46b110637fdc1a
Raw
1module engine
2
3import os
4
5$if macos {
6 fn C.mkfifo(&char, u16) i32
7} $else {
8 fn C.mkfifo(&char, u32) i32
9}
10
11fn removal_native_package(mut handle Handle, name string, files []string, backups []PackageBackup) !&Package {
12 mut db := handle.local_database()!
13 mut pkg := Package{ name: name, version: '1-1', origin: .file, groups: ['group'], files: files.map(PackageFile{ name: it }), backup: backups }
14 db.prepare_local_package(&pkg)!
15 db.write_local_package(mut pkg, true, true)!
16 db.invalidate()!
17 return db.get_pkg(name) or { return error('missing fixture') }
18}
19
20fn removal_native_begin(mut handle Handle, packages []&Package, flags TransactionFlag) ! {
21 handle.start_transaction(flags)!
22 for pkg in packages { handle.remove_package(pkg)! }
23 assert handle.prepare_transaction()!.success
24}
25
26fn test_removal_execution_state_and_lock_guards() ! {
27 base := os.join_path(os.vtmp_dir(), 'native-remove-state-${os.getpid()}')
28 os.mkdir_all(os.join_path(base, 'root'))!
29 os.mkdir_all(os.join_path(base, 'db'))!
30 defer { os.rmdir_all(base) or { eprintln(err) } }
31 mut handle := initialize(os.join_path(base, 'root'), os.join_path(base, 'db'))!
32 defer { handle.release() or { eprintln(err) } }
33 pkg := removal_native_package(mut handle, 'app', [], [])!
34 if _ := handle.commit_transaction() {
35 assert false
36 }
37 assert handle.errno() == .trans_null
38 handle.start_transaction(transaction_flag_0)!
39 if _ := handle.commit_transaction() {
40 assert false
41 }
42 assert handle.errno() == .trans_not_prepared
43 assert handle.prepare_transaction()!.success
44 if _ := handle.commit_transaction() {
45 assert false
46 }
47 assert handle.errno() == .trans_not_prepared
48 handle.release_transaction()!
49 removal_native_begin(mut handle, [pkg], .no_hooks | .no_scriptlet | .nolock)!
50 if _ := handle.commit_transaction() {
51 assert false
52 }
53 assert handle.errno() == .trans_not_locked
54 handle.release_transaction()!
55 removal_native_begin(mut handle, [pkg], .no_hooks | .no_scriptlet)!
56 assert handle.unlock_async() == 0
57 if _ := handle.commit_transaction() {
58 assert false
59 }
60 assert handle.errno() == .trans_not_locked
61 handle.release_transaction()!
62}
63
64fn test_removal_failure_and_malformed_paths_keep_database_records() ! {
65 base := os.join_path(os.vtmp_dir(), 'native-remove-failure-${os.getpid()}')
66 root := os.join_path(base, 'root')
67 os.mkdir_all(os.join_path(root, 'etc'))!
68 os.mkdir_all(os.join_path(base, 'db'))!
69 defer {
70 os.chmod(os.join_path(root, 'etc'), 0o755) or { eprintln(err) }
71 os.rmdir_all(base) or { eprintln(err) }
72 }
73 mut handle := initialize(root, os.join_path(base, 'db'))!
74 defer { handle.release() or { eprintln(err) } }
75 file := os.join_path(root, 'etc', 'app')
76 os.write_file(file, 'retained')!
77 pkg := removal_native_package(mut handle, 'app', ['etc/app'], [])!
78 if C.geteuid() != 0 {
79 removal_native_begin(mut handle, [pkg], .no_hooks | .no_scriptlet)!
80 os.chmod(os.join_path(root, 'etc'), 0o555)!
81 if _ := handle.commit_transaction() {
82 assert false
83 }
84 assert handle.errno() == .pkg_cant_remove
85 assert os.read_file(file)! == 'retained'
86 assert os.is_dir(os.join_path(base, 'db', 'local', 'app-1-1'))
87 assert handle.local.get_pkg('app') != none
88 handle.release_transaction()!
89 os.chmod(os.join_path(root, 'etc'), 0o755)!
90 }
91 outside := os.join_path(base, 'outside')
92 os.write_file(outside, 'outside')!
93 bad := removal_native_package(mut handle, 'bad', ['../outside'], [])!
94 removal_native_begin(mut handle, [pkg, bad], .no_hooks | .no_scriptlet)!
95 defer { handle.release_transaction() or { eprintln(err) } }
96 if _ := handle.commit_transaction() {
97 assert false
98 }
99 assert handle.errno() == .pkg_cant_remove
100 assert handle.transaction_state()! == .prepared
101 assert os.read_file(outside)! == 'outside'
102 assert os.read_file(file)! == 'retained'
103}
104
105struct InterruptRemoval {
106mut:
107 handle &Handle
108 events []CommitEventKind
109 release_rejected bool
110}
111
112fn interrupt_removal_callback(context voidptr, event CommitEvent) {
113 mut state := unsafe { &InterruptRemoval(context) }
114 state.events << event.kind
115 if event.kind == .package_remove_start {
116 state.handle.release_transaction() or { state.release_rejected = err.code() == int(Errno.trans_not_null) }
117 assert state.handle.interrupt_transaction_async() == 0
118 }
119}
120
121fn test_removal_interruption_finishes_current_package_and_retains_the_next() ! {
122 base := os.join_path(os.vtmp_dir(), 'native-remove-interrupt-${os.getpid()}')
123 root := os.join_path(base, 'root')
124 os.mkdir_all(root)!
125 os.mkdir_all(os.join_path(base, 'db'))!
126 defer { os.rmdir_all(base) or { eprintln(err) } }
127 mut handle := initialize(root, os.join_path(base, 'db'))!
128 defer { handle.release() or { eprintln(err) } }
129 os.write_file(os.join_path(root, 'a'), 'a')!
130 os.write_file(os.join_path(root, 'b'), 'b')!
131 a := removal_native_package(mut handle, 'a', ['a'], [])!
132 b := removal_native_package(mut handle, 'b', ['b'], [])!
133 mut db := handle.local_database()!
134 assert db.group_cache()![0].packages.len == 2
135 mut state := &InterruptRemoval{ handle: handle }
136 handle.set_commit_callback(interrupt_removal_callback, state)!
137 removal_native_begin(mut handle, [a, b], .no_hooks | .no_scriptlet)!
138 defer { handle.release_transaction() or { eprintln(err) } }
139 first := handle.transaction_remove()![0].name
140 remaining := if first == 'a' { 'b' } else { 'a' }
141 handle.commit_transaction()!
142 assert state.release_rejected
143 assert state.events == [CommitEventKind.transaction_start, .package_remove_start,
144 .package_remove_done]
145 assert handle.transaction_state()! == .committed
146 assert !os.exists(os.join_path(root, first))
147 assert os.read_file(os.join_path(root, remaining))! == remaining
148 assert db.get_pkg(first) == none
149 assert db.get_pkg(remaining) != none
150 assert db.group_cache()![0].packages.map(it.name) == [remaining]
151}
152
153fn test_malformed_hooks_fail_before_mutating_files() ! {
154 base := os.join_path(os.vtmp_dir(), 'native-remove-extensions-${os.getpid()}')
155 os.mkdir_all(base)!
156 defer { os.rmdir_all(base) or { eprintln(err) } }
157 root := os.join_path(base, 'root')
158 dbpath := os.join_path(base, 'db')
159 os.mkdir_all(root)!
160 os.mkdir_all(dbpath)!
161 mut handle := initialize(root, dbpath)!
162 defer { handle.release() or { eprintln(err) } }
163 pkg := removal_native_package(mut handle, 'app', ['payload'], [])!
164 os.write_file(os.join_path(root, 'payload'), 'payload')!
165 path := os.join_path(root, 'usr', 'share', 'libalpm', 'hooks')
166 os.mkdir_all(path)!
167 os.write_file(os.join_path(path, 'test.hook'), 'invalid hook')!
168 removal_native_begin(mut handle, [pkg], transaction_flag_0)!
169 defer { handle.release_transaction() or { eprintln(err) } }
170 if _ := handle.commit_transaction() {
171 assert false
172 }
173 assert handle.errno() == .trans_hook_failed
174 assert handle.transaction_state()! == .prepared
175 assert os.read_file(os.join_path(root, 'payload'))! == 'payload'
176}
177
178fn test_missing_backup_hash_is_preserved_and_rotation_overflow_never_loops() ! {
179 base := os.join_path(os.vtmp_dir(), 'native-remove-backup-limit-${os.getpid()}')
180 os.mkdir_all(base)!
181 defer { os.rmdir_all(base) or { eprintln(err) } }
182 for overflow in [false, true] {
183 root := os.join_path(base, overflow.str(), 'root')
184 dbpath := os.join_path(base, overflow.str(), 'db')
185 os.mkdir_all(root)!
186 os.mkdir_all(dbpath)!
187 mut handle := initialize(root, dbpath)!
188 defer { handle.release() or { eprintln(err) } }
189 os.write_file(os.join_path(root, 'config'), 'modified')!
190 pkg := removal_native_package(mut handle, 'app', ['config'], [PackageBackup{ name: 'config', hash: '(null)', has_hash: true }])!
191 if !overflow {
192 os.write_file(os.join_path(dbpath, 'local', 'app-1-1', 'files'), '%FILES%\nconfig\n\n%BACKUP%\nconfig\n\n')!
193 }
194 if overflow {
195 os.write_file(os.join_path(root, 'config.pacsave.18446744073709551615'), 'old')!
196 }
197 removal_native_begin(mut handle, [pkg], .no_hooks | .no_scriptlet)!
198 if overflow {
199 if _ := handle.commit_transaction() {
200 assert false
201 }
202 assert handle.errno() == .pkg_cant_remove
203 assert os.read_file(os.join_path(root, 'config'))! == 'modified'
204 assert handle.local.get_pkg('app') != none
205 } else {
206 handle.commit_transaction()!
207 assert os.read_file(os.join_path(root, 'config.pacsave'))! == 'modified'
208 }
209 handle.release_transaction()!
210 }
211}
212
213fn test_special_or_unreadable_backup_never_blocks_or_discards_the_record() ! {
214 base := os.join_path(os.vtmp_dir(), 'native-remove-unreadable-${os.getpid()}')
215 os.mkdir_all(base)!
216 defer { os.rmdir_all(base) or { eprintln(err) } }
217 for special in [true, false] {
218 if !special && C.geteuid() == 0 { continue }
219 root := os.join_path(base, special.str(), 'root')
220 dbpath := os.join_path(base, special.str(), 'db')
221 os.mkdir_all(root)!
222 os.mkdir_all(dbpath)!
223 mut handle := initialize(root, dbpath)!
224 defer { handle.release() or { eprintln(err) } }
225 path := os.join_path(root, 'config')
226 if special {
227 assert C.mkfifo(unsafe { &char(path.str) }, 0o600) == 0
228 } else {
229 os.write_file(path, 'unreadable')!
230 os.chmod(path, 0o000)!
231 }
232 pkg := removal_native_package(mut handle, 'app', ['config'], [PackageBackup{ name: 'config', hash: 'stored', has_hash: true }])!
233 removal_native_begin(mut handle, [pkg], .no_hooks | .no_scriptlet)!
234 if _ := handle.commit_transaction() {
235 assert false
236 }
237 assert handle.errno() == .pkg_cant_remove
238 assert os.exists(path)
239 assert handle.local.get_pkg('app') != none
240 handle.release_transaction()!
241 }
242}
243