module main import alpm import os fn test_install_reason() { assert install_reason(ConfigInstall{}) == none assert install_reason(ConfigInstall{ asdeps: true }) or { panic('none') } == .depend assert install_reason(ConfigInstall{ asexplicit: true }) or { panic('none') } == .explicit } // reason_db writes a libalpm local db with two packages, one of each reason fn reason_db(dbpath string) ! { local := os.join_path(dbpath, 'local') os.mkdir_all(local)! os.write_file(os.join_path(local, 'ALPM_DB_VERSION'), '9\n')! for name, reason in { 'dep': 1 'top': 0 } { dir := os.join_path(local, '${name}-1.0-1') os.mkdir_all(dir)! os.write_file(os.join_path(dir, 'desc'), '%NAME%\n${name}\n\n%VERSION%\n1.0-1\n\n%REASON%\n${reason}\n\n')! os.write_file(os.join_path(dir, 'files'), '%FILES%\n\n')! } } fn local_reason(root string, dbpath string, name string) !alpm.PackageReason { handle := alpm.initialize(root, dbpath)! defer { handle.release() or {} } pkg := handle.find_local_pkg(name) or { return error('${name} is not installed') } return pkg.get_reason() } fn test_mark_installed() ! { tmp := os.join_path(os.vtmp_dir(), 'vpac_test_remote_${os.getpid()}') root := os.join_path(tmp, 'root') + '/' dbpath := os.join_path(tmp, 'db') + '/' os.mkdir_all(root)! reason_db(dbpath)! defer { os.rmdir_all(tmp) or {} } handle := alpm.initialize(root, dbpath)! // --asexplicit on the dependency, --asdeps on the top package; a name // that is not installed (a target that failed) is passed over mark_installed(handle, ['dep', 'missing'], .explicit)! mark_installed(handle, ['top'], .depend)! handle.release()! // the lock is gone again, and a fresh handle reads the new reasons back // from disk assert !os.exists(os.join_path(tmp, 'db', 'db.lck')) assert local_reason(root, dbpath, 'dep')! == .explicit assert local_reason(root, dbpath, 'top')! == .depend } // SigFixture is a package archive with no signature beside it yet, and the // root, db and keyring dirs a handle needs, all under one temp dir struct SigFixture { tmp string pkg string } // sig_fixture writes foo-1.0-1-any.pkg.tar, a one-entry archive libalpm // loads as a package: its .PKGINFO fn sig_fixture(name string) !SigFixture { tmp := os.join_path(os.vtmp_dir(), 'vpac_test_sig_${name}_${os.getpid()}') os.rmdir_all(tmp) or {} for d in ['root', 'db/local', 'gpg', 'cache', 'src'] { os.mkdir_all(os.join_path(tmp, d))! } os.write_file(os.join_path(tmp, 'db/local/ALPM_DB_VERSION'), '9\n')! os.write_file(os.join_path(tmp, 'src/.PKGINFO'), 'pkgname = foo\npkgbase = foo\npkgver = 1.0-1\narch = any\nsize = 0\n')! pkg := os.join_path(tmp, 'foo-1.0-1-any.pkg.tar') res := os.exec(['tar', '-cf', pkg, '-C', os.join_path(tmp, 'src'), '.PKGINFO']) if res.exit_code != 0 { return error('tar failed: ${res.output}') } return SigFixture{ tmp: tmp pkg: pkg } } // handle opens the fixture's root with its own keyring dir, so the gpg a // signature check starts reads and writes nothing outside the fixture fn (fx SigFixture) handle(local alpm.SigLevel, remote alpm.SigLevel) !&alpm.Handle { handle := alpm.initialize(os.join_path(fx.tmp, 'root') + '/', os.join_path(fx.tmp, 'db') + '/')! handle.option_set_gpgdir(os.join_path(fx.tmp, 'gpg') + '/')! handle.option_set_local_file_siglevel(local)! handle.option_set_remote_file_siglevel(remote)! return handle } // loads tells whether the fixture's archive loads as t's origin fn loads(handle &alpm.Handle, path string, fetched bool) bool { pkg := load_file_target(handle, FileTarget{ path: path fetched: fetched }) or { return false } pkg.free_pkg() or {} return true } const sig_optional = alpm.SigLevel.package | .package_optional const sig_required = alpm.SigLevel.package fn test_load_file_target_verifies_a_signature_beside_the_archive() ! { fx := sig_fixture('present')! defer { os.rmdir_all(fx.tmp) or {} } handle := fx.handle(sig_optional, sig_optional)! defer { handle.release() or {} } // optional: an unsigned archive is taken as it is assert loads(handle, fx.pkg, false) assert loads(handle, fx.pkg, true) // a signature beside it is read and must hold. libalpm checks a file // package at its load only, so a load at level 0 installed this one os.write_file(fx.pkg + '.sig', 'not a signature')! for fetched in [false, true] { load_file_target(handle, FileTarget{ path: fx.pkg, fetched: fetched }) or { assert err.msg().starts_with('failed to load ${fx.pkg}: ') assert err.msg().contains('signature'), err.msg() continue } assert false, 'a corrupt signature loaded (fetched: ${fetched})' } } fn test_load_file_target_takes_the_level_of_its_origin() ! { fx := sig_fixture('origin')! defer { os.rmdir_all(fx.tmp) or {} } // a file named takes the local level, a fetched one the remote level, as // pacman's -U loads them: the unsigned archive fails where its origin's // level requires a signature, and only there local_strict := fx.handle(sig_required, sig_optional)! assert !loads(local_strict, fx.pkg, false) assert loads(local_strict, fx.pkg, true) local_strict.release()! remote_strict := fx.handle(sig_optional, sig_required)! assert loads(remote_strict, fx.pkg, false) assert !loads(remote_strict, fx.pkg, true) remote_strict.release()! } fn test_init_handle_checks_file_targets_at_the_configured_level() ! { // the whole way vpac opens a handle for an install: the configured level // (the default here, pacman's) reaches both kinds of file target fx := sig_fixture('config')! defer { os.rmdir_all(fx.tmp) or {} } saved_root, saved_db, saved_gpg := root_path, db_path, cfg_gpgdir saved_cache, saved_user, saved_level := cfg_cachedirs.clone(), download_user, cfg_siglevel saved_hooks := cfg_hookdirs.clone() cfg_hookdirs = []string{} defer { root_path, db_path, cfg_gpgdir = saved_root, saved_db, saved_gpg cfg_cachedirs = saved_cache.clone() cfg_hookdirs = saved_hooks.clone() download_user, cfg_siglevel = saved_user, saved_level } root_path = os.join_path(fx.tmp, 'root') + '/' db_path = os.join_path(fx.tmp, 'db') + '/' cfg_gpgdir = os.join_path(fx.tmp, 'gpg') + '/' cfg_cachedirs = [os.join_path(fx.tmp, 'cache') + '/'] // the sandbox user need not exist where the tests run download_user = '' ui := new_trans_ui() handle := init_handle(ui)! defer { handle.release() or {} } assert loads(handle, fx.pkg, false) assert loads(handle, fx.pkg, true) os.write_file(fx.pkg + '.sig', 'not a signature')! assert !loads(handle, fx.pkg, false) assert !loads(handle, fx.pkg, true) }