nspkg / engine / sync_database_test.v
131 lines · 125 sloc · 5.21 KB · c01b0795d1db56c7bce2539b2c46b110637fdc1a
Raw
1module engine
2
3import nspkg.tests.archivefixtures
4import os
5
6fn test_sync_database_archive_loading() ! {
7 dir := os.join_path(os.vtmp_dir(), 'native-sync-${os.getpid()}')
8 os.mkdir_all(dir)!
9 defer { os.rmdir_all(dir) or { eprintln(err) } }
10 for compressed in [false, true] {
11 path := os.join_path(dir, '${compressed}.db')
12 archivefixtures.write(path, [
13 archivefixtures.Entry{ name: 'foo-bar-1-2/', directory: true },
14 archivefixtures.Entry{ name: 'zebra-2-1/desc', text: '%NAME%\nzebra\n\n%VERSION%\n2-1\n\n%DESC%\nA zebra\n\n' },
15 archivefixtures.Entry{ name: 'foo-bar-1-2/desc', text: '%NAME%\nfoo-bar\n\n%VERSION%\n1-2\n\n%FILENAME%\nfoo-bar-1-2-any.pkg.tar.zst\n\n%CSIZE%\n1234\n\n%ISIZE%\n5678\n\n%SHA256SUM%\nabc123\n\n' },
16 archivefixtures.Entry{ name: 'foo-bar-1-2/depends', text: '%DEPENDS%\nlibc>=2\n\n%PROVIDES%\nvirtual-foo=1\n\n' },
17 archivefixtures.Entry{ name: 'foo-bar-1-2/files', text: '%FILES%\nusr/bin/foo\netc/foo.conf\n\n' },
18 ], compressed)!
19 db := read_sync_database(path, 'core')!
20 assert db.packages.map(it.name) == ['foo-bar', 'zebra']
21 pkg := db.get_pkg('foo-bar') or { return error('missing sync package') }
22 assert pkg.version == '1-2'
23 assert pkg.filename == 'foo-bar-1-2-any.pkg.tar.zst'
24 assert pkg.size == 1234
25 assert pkg.isize == 5678
26 assert pkg.validation == 4
27 assert pkg.files.map(it.name) == ['etc/foo.conf', 'usr/bin/foo']
28 assert pkg.depends[0].compute_string() == 'libc>=2'
29 assert pkg.satisfies(dep_from_string('virtual-foo>=1'))
30 assert db.get_pkg('missing') == none
31 zebra := db.get_pkg('zebra') or { return error('missing zebra') }
32 assert zebra.validation == 1
33 }
34}
35
36fn test_invalid_archives_and_metadata_fail() ! {
37 dir := os.join_path(os.vtmp_dir(), 'native-bad-sync-${os.getpid()}')
38 os.mkdir_all(dir)!
39 defer { os.rmdir_all(dir) or { eprintln(err) } }
40 path := os.join_path(dir, 'bad.db')
41 for text in ['%FILENAME%\n../bad.pkg.tar.zst\n\n', '%DEPENDS%\nfoo', '%DATA%\nno-equals\n\n',
42 '%DESC%\n' + 'x'.repeat(512 * 1024 + 1) + '\n\n'] {
43 archivefixtures.write(path, [archivefixtures.Entry{ name: 'foo-1-1/desc', text: text }], false)!
44 if _ := read_sync_database(path, 'core') {
45 assert false, 'accepted invalid metadata'
46 }
47 }
48 archivefixtures.write(path, [archivefixtures.Entry{ name: 'invalid/desc', text: '' }], false)!
49 if _ := read_sync_database(path, 'core') {
50 assert false, 'accepted an invalid package entry'
51 }
52 bytes := archivefixtures.tar_bytes([archivefixtures.Entry{ name: 'foo-1-1/desc', text: '%DESC%\n' + 'x'.repeat(2000) + '\n\n' }])
53 os.write_file_array(path, bytes[..700])!
54 if _ := read_sync_database(path, 'core') {
55 assert false, 'accepted a truncated archive'
56 }
57 os.write_file(path, 'not an archive')!
58 if _ := read_sync_database(path, 'core') {
59 assert false
60 }
61 if _ := read_sync_database(path + '-missing', 'core') {
62 assert false
63 }
64 // archive_fgets supplies an empty final line after a newline-terminated value.
65 archivefixtures.write(path, [archivefixtures.Entry{ name: 'foo-1-1/desc', text: '%DEPENDS%\nbar\n' }], false)!
66 complete := read_sync_database(path, 'core')!
67 pkg := complete.get_pkg('foo') or { return error('missing newline-terminated package') }
68 assert pkg.depends.map(it.compute_string()) == ['bar']
69}
70
71fn check_sync_reader_gc_restoration(path string, invalid bool, already_disabled bool) ! {
72 if already_disabled { gc_disable() }
73 defer { if already_disabled { gc_enable() } }
74 before := gc_is_enabled()
75 if invalid {
76 if _ := read_sync_database(path, 'core') {
77 assert false, 'accepted invalid metadata'
78 }
79 } else {
80 db := read_sync_database(path, 'core')!
81 assert db.packages.len == 1
82 }
83 assert gc_is_enabled() == before
84}
85
86fn test_sync_reader_restores_collection_on_success_error_and_nested_disable() ! {
87 dir := os.join_path(os.vtmp_dir(), 'native-sync-gc-state-${os.getpid()}')
88 os.mkdir_all(dir)!
89 defer { os.rmdir_all(dir) or { eprintln(err) } }
90 before := gc_is_enabled()
91 for invalid in [false, true] {
92 path := os.join_path(dir, '${invalid}.db')
93 archivefixtures.write(path, [archivefixtures.Entry{
94 name: 'app-1-1/desc'
95 text: if invalid { '%FILENAME%\n../invalid\n\n' } else { '%DESC%\nkept\n\n' }
96 }], true)!
97 for already_disabled in [false, true] {
98 check_sync_reader_gc_restoration(path, invalid, already_disabled)!
99 assert gc_is_enabled() == before
100 }
101 }
102}
103
104fn test_sync_reader_keeps_large_cache_alive_across_collections() ! {
105 dir := os.join_path(os.vtmp_dir(), 'native-sync-large-cache-${os.getpid()}')
106 os.mkdir_all(dir)!
107 defer { os.rmdir_all(dir) or { eprintln(err) } }
108 mut entries := []archivefixtures.Entry{cap: 10000}
109 for i in 0 .. 10000 {
110 name := 'package-${i:05}'
111 entries << archivefixtures.Entry{
112 name: '${name}-1-1/desc'
113 text: '%DESC%\ndescription-${i:05}\n\n%DEPENDS%\nlib-${i:05}>=1\n\n'
114 }
115 }
116 path := os.join_path(dir, 'core.db')
117 archivefixtures.write(path, entries, true)!
118 before := gc_is_enabled()
119 db := read_sync_database(path, 'core')!
120 assert gc_is_enabled() == before
121 gc_collect()
122 assert db.packages.len == entries.len
123 for i in 0 .. entries.len {
124 name := 'package-${i:05}'
125 pkg := db.get_pkg(name) or { return error('missing package ${name}') }
126 assert pkg.name == name
127 assert pkg.version == '1-1'
128 assert pkg.desc == 'description-${i:05}'
129 assert pkg.depends[0].compute_string() == 'lib-${i:05}>=1'
130 }
131}
132