vpac / src / remote_test.v
173 lines · 159 sloc · 6.4 KB · 3609dfbd82867dc1034a443f8f9ced529ddbbedd
Raw
1module main
2
3import alpm
4import os
5
6fn test_install_reason() {
7 assert install_reason(ConfigInstall{}) == none
8 assert install_reason(ConfigInstall{ asdeps: true }) or { panic('none') } == .depend
9 assert install_reason(ConfigInstall{ asexplicit: true }) or { panic('none') } == .explicit
10}
11
12// reason_db writes a libalpm local db with two packages, one of each reason
13fn reason_db(dbpath string) ! {
14 local := os.join_path(dbpath, 'local')
15 os.mkdir_all(local)!
16 os.write_file(os.join_path(local, 'ALPM_DB_VERSION'), '9\n')!
17 for name, reason in {
18 'dep': 1
19 'top': 0
20 } {
21 dir := os.join_path(local, '${name}-1.0-1')
22 os.mkdir_all(dir)!
23 os.write_file(os.join_path(dir, 'desc'), '%NAME%\n${name}\n\n%VERSION%\n1.0-1\n\n%REASON%\n${reason}\n\n')!
24 os.write_file(os.join_path(dir, 'files'), '%FILES%\n\n')!
25 }
26}
27
28fn local_reason(root string, dbpath string, name string) !alpm.PackageReason {
29 handle := alpm.initialize(root, dbpath)!
30 defer { handle.release() or {} }
31 pkg := handle.find_local_pkg(name) or { return error('${name} is not installed') }
32 return pkg.get_reason()
33}
34
35fn test_mark_installed() ! {
36 tmp := os.join_path(os.vtmp_dir(), 'vpac_test_remote_${os.getpid()}')
37 root := os.join_path(tmp, 'root') + '/'
38 dbpath := os.join_path(tmp, 'db') + '/'
39 os.mkdir_all(root)!
40 reason_db(dbpath)!
41 defer { os.rmdir_all(tmp) or {} }
42
43 handle := alpm.initialize(root, dbpath)!
44 // --asexplicit on the dependency, --asdeps on the top package; a name
45 // that is not installed (a target that failed) is passed over
46 mark_installed(handle, ['dep', 'missing'], .explicit)!
47 mark_installed(handle, ['top'], .depend)!
48 handle.release()!
49 // the lock is gone again, and a fresh handle reads the new reasons back
50 // from disk
51 assert !os.exists(os.join_path(tmp, 'db', 'db.lck'))
52 assert local_reason(root, dbpath, 'dep')! == .explicit
53 assert local_reason(root, dbpath, 'top')! == .depend
54}
55
56// SigFixture is a package archive with no signature beside it yet, and the
57// root, db and keyring dirs a handle needs, all under one temp dir
58struct SigFixture {
59 tmp string
60 pkg string
61}
62
63// sig_fixture writes foo-1.0-1-any.pkg.tar, a one-entry archive libalpm
64// loads as a package: its .PKGINFO
65fn sig_fixture(name string) !SigFixture {
66 tmp := os.join_path(os.vtmp_dir(), 'vpac_test_sig_${name}_${os.getpid()}')
67 os.rmdir_all(tmp) or {}
68 for d in ['root', 'db/local', 'gpg', 'cache', 'src'] {
69 os.mkdir_all(os.join_path(tmp, d))!
70 }
71 os.write_file(os.join_path(tmp, 'db/local/ALPM_DB_VERSION'), '9\n')!
72 os.write_file(os.join_path(tmp, 'src/.PKGINFO'), 'pkgname = foo\npkgbase = foo\npkgver = 1.0-1\narch = any\nsize = 0\n')!
73 pkg := os.join_path(tmp, 'foo-1.0-1-any.pkg.tar')
74 res := os.exec(['tar', '-cf', pkg, '-C', os.join_path(tmp, 'src'), '.PKGINFO'])
75 if res.exit_code != 0 {
76 return error('tar failed: ${res.output}')
77 }
78 return SigFixture{
79 tmp: tmp
80 pkg: pkg
81 }
82}
83
84// handle opens the fixture's root with its own keyring dir, so the gpg a
85// signature check starts reads and writes nothing outside the fixture
86fn (fx SigFixture) handle(local alpm.SigLevel, remote alpm.SigLevel) !&alpm.Handle {
87 handle := alpm.initialize(os.join_path(fx.tmp, 'root') + '/', os.join_path(fx.tmp, 'db') + '/')!
88 handle.option_set_gpgdir(os.join_path(fx.tmp, 'gpg') + '/')!
89 handle.option_set_local_file_siglevel(local)!
90 handle.option_set_remote_file_siglevel(remote)!
91 return handle
92}
93
94// loads tells whether the fixture's archive loads as t's origin
95fn loads(handle &alpm.Handle, path string, fetched bool) bool {
96 pkg := load_file_target(handle, FileTarget{
97 path: path
98 fetched: fetched
99 }) or { return false }
100 pkg.free_pkg() or {}
101 return true
102}
103
104const sig_optional = alpm.SigLevel.package | .package_optional
105const sig_required = alpm.SigLevel.package
106
107fn test_load_file_target_verifies_a_signature_beside_the_archive() ! {
108 fx := sig_fixture('present')!
109 defer { os.rmdir_all(fx.tmp) or {} }
110 handle := fx.handle(sig_optional, sig_optional)!
111 defer { handle.release() or {} }
112 // optional: an unsigned archive is taken as it is
113 assert loads(handle, fx.pkg, false)
114 assert loads(handle, fx.pkg, true)
115 // a signature beside it is read and must hold. libalpm checks a file
116 // package at its load only, so a load at level 0 installed this one
117 os.write_file(fx.pkg + '.sig', 'not a signature')!
118 for fetched in [false, true] {
119 load_file_target(handle, FileTarget{ path: fx.pkg, fetched: fetched }) or {
120 assert err.msg().starts_with('failed to load ${fx.pkg}: ')
121 assert err.msg().contains('signature'), err.msg()
122 continue
123 }
124 assert false, 'a corrupt signature loaded (fetched: ${fetched})'
125 }
126}
127
128fn test_load_file_target_takes_the_level_of_its_origin() ! {
129 fx := sig_fixture('origin')!
130 defer { os.rmdir_all(fx.tmp) or {} }
131 // a file named takes the local level, a fetched one the remote level, as
132 // pacman's -U loads them: the unsigned archive fails where its origin's
133 // level requires a signature, and only there
134 local_strict := fx.handle(sig_required, sig_optional)!
135 assert !loads(local_strict, fx.pkg, false)
136 assert loads(local_strict, fx.pkg, true)
137 local_strict.release()!
138 remote_strict := fx.handle(sig_optional, sig_required)!
139 assert loads(remote_strict, fx.pkg, false)
140 assert !loads(remote_strict, fx.pkg, true)
141 remote_strict.release()!
142}
143
144fn test_init_handle_checks_file_targets_at_the_configured_level() ! {
145 // the whole way vpac opens a handle for an install: the configured level
146 // (the default here, pacman's) reaches both kinds of file target
147 fx := sig_fixture('config')!
148 defer { os.rmdir_all(fx.tmp) or {} }
149 saved_root, saved_db, saved_gpg := root_path, db_path, cfg_gpgdir
150 saved_cache, saved_user, saved_level := cfg_cachedirs.clone(), download_user, cfg_siglevel
151 saved_hooks := cfg_hookdirs.clone()
152 cfg_hookdirs = []string{}
153 defer {
154 root_path, db_path, cfg_gpgdir = saved_root, saved_db, saved_gpg
155 cfg_cachedirs = saved_cache.clone()
156 cfg_hookdirs = saved_hooks.clone()
157 download_user, cfg_siglevel = saved_user, saved_level
158 }
159 root_path = os.join_path(fx.tmp, 'root') + '/'
160 db_path = os.join_path(fx.tmp, 'db') + '/'
161 cfg_gpgdir = os.join_path(fx.tmp, 'gpg') + '/'
162 cfg_cachedirs = [os.join_path(fx.tmp, 'cache') + '/']
163 // the sandbox user need not exist where the tests run
164 download_user = ''
165 ui := new_trans_ui()
166 handle := init_handle(ui)!
167 defer { handle.release() or {} }
168 assert loads(handle, fx.pkg, false)
169 assert loads(handle, fx.pkg, true)
170 os.write_file(fx.pkg + '.sig', 'not a signature')!
171 assert !loads(handle, fx.pkg, false)
172 assert !loads(handle, fx.pkg, true)
173}
174