v2 / vlib / dl / loader / loader_test.v
83 lines · 76 sloc · 1.9 KB · c51d30bf5309653c6b573ec815268e69a78ea8cc
Raw
1import dl.loader
2import dl
3
4fn test_dl_loader() ! {
5 $if linux {
6 run_test_invalid_lib_linux()!
7 return
8 }
9 $if windows {
10 run_test_invalid_lib_windows()!
11 run_test_valid_lib_windows()!
12 run_test_invalid_sym_windows()!
13 run_test_valid_sym_windows()!
14 return
15 } $else {
16 eprint('currently not implemented on this platform')
17 }
18}
19
20fn get_or_create_loader(name string, paths []string) !&loader.DynamicLibLoader {
21 return loader.get_or_create_dynamic_lib_loader(
22 key: name
23 paths: paths
24 flags: dl.rtld_now
25 )
26}
27
28fn run_test_invalid_lib_linux() ! {
29 // ensure a not-existing dl won't be loaded
30 mut dl_loader := get_or_create_loader(@MOD + '.' + @FN + '.' + 'lib', [
31 'not-existing-dynamic-link-library',
32 ])!
33 defer {
34 dl_loader.unregister()
35 }
36 h := dl_loader.open() or { unsafe { nil } }
37 assert isnil(h)
38}
39
40fn run_test_invalid_lib_windows() ! {
41 // ensure a not-existing dl won't be loaded
42 mut dl_loader := get_or_create_loader(@MOD + '.' + @FN + '.' + 'lib', [
43 'not-existing-dynamic-link-library',
44 ])!
45 defer {
46 dl_loader.unregister()
47 }
48 h := dl_loader.open() or { unsafe { nil } }
49 assert isnil(h)
50}
51
52fn run_test_valid_lib_windows() ! {
53 mut dl_loader := get_or_create_loader(@MOD + '.' + @FN + '.' + 'lib', [
54 'not-existing-dynamic-link-library',
55 'shell32',
56 ])!
57 defer {
58 dl_loader.unregister()
59 }
60 h := dl_loader.open() or { unsafe { nil } }
61 assert !isnil(h)
62}
63
64fn run_test_invalid_sym_windows() ! {
65 mut dl_loader := get_or_create_loader(@MOD + '.' + @FN + '.' + 'lib', ['shell32'])!
66 defer {
67 dl_loader.unregister()
68 }
69 proc := dl_loader.get_sym('CommandLineToArgvW2') or { unsafe { nil } }
70 assert isnil(proc)
71}
72
73fn run_test_valid_sym_windows() ! {
74 mut dl_loader := get_or_create_loader(@MOD + '.' + @FN + '.' + 'lib', [
75 'not-existing-dynamic-link-library',
76 'shell32',
77 ])!
78 defer {
79 dl_loader.unregister()
80 }
81 proc := dl_loader.get_sym('CommandLineToArgvW') or { unsafe { nil } }
82 assert !isnil(proc)
83}
84