nspkg / tests / coracle / option_state_compatibility_test.v
354 lines · 347 sloc · 10.24 KB · c01b0795d1db56c7bce2539b2c46b110637fdc1a
Raw
1module coracle
2
3import nspkg as facade
4import nspkg.engine
5import os
6
7fn option_state_reference(handle &Handle, option engine.StringListOption, remove bool, value string) ! {
8 match option {
9 .cache_dirs {
10 if remove {
11 handle.option_remove_cachedir(value)!
12 } else {
13 handle.option_add_cachedir(value)!
14 }
15 }
16 .hook_dirs {
17 if remove {
18 handle.option_remove_hookdir(value)!
19 } else {
20 handle.option_add_hookdir(value)!
21 }
22 }
23 .no_upgrade {
24 if remove {
25 handle.option_remove_noupgrade(value)!
26 } else {
27 handle.option_add_noupgrade(value)!
28 }
29 }
30 .no_extract {
31 if remove {
32 handle.option_remove_no_extract(value)!
33 } else {
34 handle.option_add_no_extract(value)!
35 }
36 }
37 .ignore_packages {
38 if remove {
39 handle.option_remove_ignore_pkg(value)!
40 } else {
41 handle.option_add_ignore_pkg(value)!
42 }
43 }
44 .ignore_groups {
45 if remove {
46 handle.option_remove_ignore_group(value)!
47 } else {
48 handle.option_add_ignore_group(value)!
49 }
50 }
51 .overwrite_files {
52 if remove {
53 handle.option_remove_overwrite_file(value)!
54 } else {
55 handle.option_add_overwrite_file(value)!
56 }
57 }
58 .architectures {
59 if remove {
60 handle.option_remove_architecture(value)!
61 } else {
62 handle.option_add_architecture(value)!
63 }
64 }
65 }
66}
67
68fn option_state_public(handle &facade.Handle, option engine.StringListOption, remove bool, value string) ! {
69 match option {
70 .cache_dirs {
71 if remove {
72 handle.option_remove_cachedir(value)!
73 } else {
74 handle.option_add_cachedir(value)!
75 }
76 }
77 .hook_dirs {
78 if remove {
79 handle.option_remove_hookdir(value)!
80 } else {
81 handle.option_add_hookdir(value)!
82 }
83 }
84 .no_upgrade {
85 if remove {
86 handle.option_remove_noupgrade(value)!
87 } else {
88 handle.option_add_noupgrade(value)!
89 }
90 }
91 .no_extract {
92 if remove {
93 handle.option_remove_no_extract(value)!
94 } else {
95 handle.option_add_no_extract(value)!
96 }
97 }
98 .ignore_packages {
99 if remove {
100 handle.option_remove_ignore_pkg(value)!
101 } else {
102 handle.option_add_ignore_pkg(value)!
103 }
104 }
105 .ignore_groups {
106 if remove {
107 handle.option_remove_ignore_group(value)!
108 } else {
109 handle.option_add_ignore_group(value)!
110 }
111 }
112 .overwrite_files {
113 if remove {
114 handle.option_remove_overwrite_file(value)!
115 } else {
116 handle.option_add_overwrite_file(value)!
117 }
118 }
119 .architectures {
120 if remove {
121 handle.option_remove_architecture(value)!
122 } else {
123 handle.option_add_architecture(value)!
124 }
125 }
126 }
127}
128
129fn option_state_reference_values(handle &Handle, option engine.StringListOption) []string {
130 match option {
131 .cache_dirs { return handle.option_get_cachedirs().strs() }
132 .hook_dirs {
133 mut list := handle.option_get_hookdirs() or { return []string{} }
134 return list.strs()
135 }
136 .no_upgrade { return handle.option_get_noupgrades().strs() }
137 .no_extract { return handle.option_get_no_extracts().strs() }
138 .ignore_packages { return handle.option_get_ignore_pkgs().strs() }
139 .ignore_groups { return handle.option_get_ignore_groups().strs() }
140 .overwrite_files { return handle.option_get_overwrite_files().strs() }
141 .architectures {
142 mut list := handle.option_get_architectures() or { return []string{} }
143 return list.strs()
144 }
145 }
146}
147
148fn option_state_public_values(handle &facade.Handle, option engine.StringListOption) []string {
149 match option {
150 .cache_dirs { return handle.option_get_cachedirs().strs() }
151 .hook_dirs {
152 mut list := handle.option_get_hookdirs() or { return []string{} }
153 return list.strs()
154 }
155 .no_upgrade { return handle.option_get_noupgrades().strs() }
156 .no_extract { return handle.option_get_no_extracts().strs() }
157 .ignore_packages { return handle.option_get_ignore_pkgs().strs() }
158 .ignore_groups { return handle.option_get_ignore_groups().strs() }
159 .overwrite_files { return handle.option_get_overwrite_files().strs() }
160 .architectures {
161 mut list := handle.option_get_architectures() or { return []string{} }
162 return list.strs()
163 }
164 }
165}
166
167fn test_option_string_lists_preserve_c_boundaries_and_architecture_errors() ! {
168 root := os.join_path(os.vtmp_dir(), 'alpm-option-state-${os.getpid()}')
169 os.mkdir_all(root)!
170 defer { os.rmdir_all(root) or { eprintln(err) } }
171 old := initialize('/', root)!
172 defer { old.release() or { eprintln(err) } }
173 public := facade.initialize('/', root)!
174 defer { public.release() or { eprintln(err) } }
175 mut native := engine.initialize('/', root)!
176 defer { native.release() or { eprintln(err) } }
177 mut mismatches := []string{}
178 for option in [engine.StringListOption.cache_dirs, .hook_dirs, .no_upgrade, .no_extract,
179 .ignore_packages, .ignore_groups, .overwrite_files, .architectures] {
180 // a repeated add and a missing remove must retain list order and duplicates.
181 for remove in [false, true] {
182 mut values := ['relative', 'relative\x00suffix', '/absolute/', '/absolute/\x00suffix',
183 'missing']
184 if option !in [.cache_dirs, .hook_dirs] { values << ['', '\x00suffix'] }
185 for value in values {
186 for prior in [false, true] {
187 old.option_get_root()
188 public.option_get_root()
189 native.validate_handle()!
190 if prior {
191 if _ := old.register_syncdb('', 0) {
192 assert false
193 }
194 if _ := public.register_syncdb('', 0) {
195 assert false
196 }
197 if _ := native.register_syncdb('', 0) {
198 assert false
199 }
200 }
201 option_state_reference(old, option, remove, value)!
202 option_state_public(public, option, remove, value)!
203 if remove {
204 native.remove_string_list(option, value)!
205 } else {
206 native.add_string_list(option, value)!
207 }
208 expected_error := int(old.errno())
209 actual_errors := [int(public.errno()), int(native.errno())]
210 if actual_errors != [expected_error, expected_error] {
211 mismatches << '${option}/${remove}/${prior}/${value.bytes()}: errno ${actual_errors} != ${expected_error}'
212 }
213 expected := option_state_reference_values(old, option)
214 actual := option_state_public_values(public, option)
215 core_values := native.string_list(option)!
216 if actual != expected || core_values != expected {
217 mismatches << '${option}/${remove}/${prior}/${value.bytes()}: ${actual} / ${core_values} != ${expected}'
218 }
219 }
220 }
221 }
222 }
223 assert mismatches.len == 0, mismatches[..int_min(8, mismatches.len)].join('\n')
224}
225
226fn test_string_option_boundaries_and_missing_sandbox_user_match_c() ! {
227 root := os.join_path(os.vtmp_dir(), 'alpm-string-option-state-${os.getpid()}')
228 os.mkdir_all(root)!
229 defer { os.rmdir_all(root) or { eprintln(err) } }
230 old := initialize('/', root)!
231 defer { old.release() or { eprintln(err) } }
232 public := facade.initialize('/', root)!
233 defer { public.release() or { eprintln(err) } }
234 mut native := engine.initialize('/', root)!
235 defer { native.release() or { eprintln(err) } }
236 mut mismatches := []string{}
237 for option in [engine.StringOption.logfile, .gpgdir, .db_extension, .sandbox_user] {
238 mut values := [os.join_path(root, 'option'), os.join_path(root, 'option') + '\x00suffix']
239 if option == .sandbox_user {
240 values = ['root', 'root\x00suffix', 'alpm/no/such/user', 'root', '']
241 } else if option != .gpgdir {
242 values << ['', '\x00suffix']
243 }
244 for value in values {
245 for prior in [false, true] {
246 old.option_get_root()
247 public.option_get_root()
248 native.validate_handle()!
249 if prior {
250 if _ := old.register_syncdb('', 0) {
251 assert false
252 }
253 if _ := public.register_syncdb('', 0) {
254 assert false
255 }
256 if _ := native.register_syncdb('', 0) {
257 assert false
258 }
259 }
260 mut expected_code := -1
261 mut expected_message := ''
262 match option {
263 .logfile {
264 old.option_set_logfile(value) or {
265 expected_code = err.code()
266 expected_message = err.msg()
267 }
268 }
269 .gpgdir {
270 old.option_set_gpgdir(value) or {
271 expected_code = err.code()
272 expected_message = err.msg()
273 }
274 }
275 .db_extension {
276 old.option_set_dbext(value) or {
277 expected_code = err.code()
278 expected_message = err.msg()
279 }
280 }
281 .sandbox_user {
282 old.option_set_sandboxuser(value) or {
283 expected_code = err.code()
284 expected_message = err.msg()
285 }
286 }
287 }
288 mut actual_code := -1
289 mut actual_message := ''
290 match option {
291 .logfile {
292 public.option_set_logfile(value) or {
293 actual_code = err.code()
294 actual_message = err.msg()
295 }
296 }
297 .gpgdir {
298 public.option_set_gpgdir(value) or {
299 actual_code = err.code()
300 actual_message = err.msg()
301 }
302 }
303 .db_extension {
304 public.option_set_dbext(value) or {
305 actual_code = err.code()
306 actual_message = err.msg()
307 }
308 }
309 .sandbox_user {
310 public.option_set_sandboxuser(value) or {
311 actual_code = err.code()
312 actual_message = err.msg()
313 }
314 }
315 }
316 mut core_code := -1
317 mut core_message := ''
318 native.set_string_option(option, value) or {
319 core_code = err.code()
320 core_message = err.msg()
321 }
322 if [actual_code, core_code] != [expected_code, expected_code]
323 || [actual_message, core_message] != [expected_message, expected_message]
324 || [int(public.errno()), int(native.errno())] != [
325 int(old.errno()),
326 int(old.errno()),
327 ] {
328 mismatches << '${option}/${prior}/${value.bytes()}: errors ${actual_code}/${core_code}/${int(public.errno())}/${int(native.errno())} != ${expected_code}/${int(old.errno())}'
329 }
330 expected := match option {
331 .logfile { old.option_get_logfile() }
332 .gpgdir { old.option_get_gpgdir() }
333 .db_extension { old.option_get_dbext() }
334 .sandbox_user {
335 // the frozen getter dereferences NULL after a failed setter.
336 ptr := C.oracle_alpm_option_get_sandboxuser(old)
337 if ptr == unsafe { nil } { '' } else { unsafe { tos_clone(ptr) } }
338 }
339 }
340 actual := match option {
341 .logfile { public.option_get_logfile() }
342 .gpgdir { public.option_get_gpgdir() }
343 .db_extension { public.option_get_dbext() }
344 .sandbox_user { public.option_get_sandboxuser() }
345 }
346 core_value := native.string_option(option)!
347 if actual != expected || core_value != expected {
348 mismatches << '${option}/${prior}/${value.bytes()}: values ${actual.bytes()}/${core_value.bytes()} != ${expected.bytes()}'
349 }
350 }
351 }
352 }
353 assert mismatches.len == 0, mismatches[..int_min(8, mismatches.len)].join('\n')
354}
355