nspkg / tests / list_parity.v
58 lines · 55 sloc · 2.6 KB · c01b0795d1db56c7bce2539b2c46b110637fdc1a
Raw
1// use checked-out C list algorithms and the existing allocation-failure harness.
2module main
3
4import nspkg.tests.tooling
5import os
6
7fn main() {
8 run() or {
9 eprintln(err)
10 exit(1)
11 }
12}
13
14fn run() ! {
15 args := tooling.options(true)!
16 base := tooling.temporary('alpm-list-parity')!
17 defer { os.rmdir_all(base) or { eprintln(err) } }
18 for name in ['alpm_list.c', 'alpm_list.h'] {
19 source := os.read_file(os.join_path(args.pacman, 'lib/libalpm', name))!.replace('alpm_list_', 'reference_list_').replace('"alpm_list.h"', '"reference_list.h"')
20 os.write_file(os.join_path(base, name.replace('alpm_list', 'reference_list')), source)!
21 }
22 native := os.read_file(os.join_path(@VMODROOT, 'list.v'))!
23 os.write_file(os.join_path(base, 'list.v'), native)!
24 test_source := os.read_file(os.join_path(@VMODROOT, 'list_test.v'))!.replace('C.alpm_list_', 'C.reference_list_').replace('#flag -lalpm\n', '').replace('#include <alpm.h>\n', '').replace('#include <alpm_list.h>', '#flag ${base}/reference_list.c\n#include "${base}/reference_list.h"')
25 test_file := os.join_path(base, 'list_test.v')
26 os.write_file(test_file, test_source)!
27 mut flags := []string{}
28 if args.sanitize {
29 flags << ['-cc', 'gcc', '-cflags', '-fsanitize=address,undefined -fno-omit-frame-pointer',
30 '-ldflags', '-fsanitize=address,undefined']
31 // the runtime owns GC allocations; this pass checks invalid memory access.
32 if os.setenv('ASAN_OPTIONS', 'detect_leaks=0', true) != 0 || os.setenv('UBSAN_OPTIONS', 'halt_on_error=1', true) != 0 {
33 return os.last_error()
34 }
35 }
36 list_binary := os.join_path(base, 'list-parity')
37 mut build := flags.clone()
38 build << ['-o', list_binary, test_file]
39 tooling.command(args.compiler, build)!
40 print(tooling.command(list_binary, [])!)
41 tooling.require_no_libalpm(list_binary)!
42 println('Native lists matched checked-out alpm_list.c; no libalpm linked')
43 mut faults := native
44 for name in ['malloc', 'calloc', 'strdup', 'free'] {
45 faults = faults.replace('C.' + name, 'C.list_alloc_' + name)
46 }
47 os.write_file(os.join_path(base, 'list.v'), faults)!
48 header := os.join_path(base, 'list_alloc.h')
49 os.cp(os.join_path(@VMODROOT, 'tests', 'list_alloc.h'), header)!
50 fault_file := os.join_path(base, 'list_alloc_test.v')
51 os.write_file(fault_file, os.read_file(os.join_path(@VMODROOT, 'tests', 'list_alloc_test.v.in'))!.replace('@ALLOC_HEADER@', header))!
52 mut fault_build := flags.clone()
53 fault_binary := os.join_path(base, 'list-faults')
54 fault_build << ['-o', fault_binary, fault_file]
55 tooling.command(args.compiler, fault_build)!
56 print(tooling.command(fault_binary, [])!)
57 println('Allocation-failure tests passed with no tracked leaks or invalid frees')
58}
59