Debian Codegraph @

tests/cache.rs

raw symbols SCIP
1 mod cache {
2 use std::collections::HashMap;
3 use std::fmt::Write as _;
4
5 use cxx::{CxxVector, UniquePtr};
6 use rust_apt::cache::*;
7 use rust_apt::raw::{create_acquire, IntoRawIter, ItemDesc};
8 use rust_apt::util::*;
9 use rust_apt::{new_cache, DepType};
10
11 // This is a manual test. I don't know a good way to dynamically test this
12 // Maybe by installing a test-deb with certain depends and checking the
13 // packages?
14 #[test]
15 fn auto_removeable() {
16 let cache = new_cache!().unwrap();
17
18 let sort = PackageSort::default().auto_removable().names();
19
20 for pkg in cache.packages(&sort) {
21 println!("{}", pkg.name())
22 }
23 }
24
25 #[test]
26 fn time_cache_iter() {
27 let cache = new_cache!().unwrap();
28
29 let sort = PackageSort::default().include_virtual();
30
31 use std::time::Instant;
32
33 let now = Instant::now();
34 dbg!(cache.packages(&sort).count());
35 let elapsed = now.elapsed();
36 println!("Elapsed: {:.2?}", elapsed);
37
38 let now = Instant::now();
39 dbg!(cache.iter().count());
40 let elapsed = now.elapsed();
41 println!("Elapsed: {:.2?}", elapsed);
42
43 let now = Instant::now();
44 dbg!(unsafe { cache.begin().raw_iter().count() });
45 let elapsed = now.elapsed();
46 println!("Elapsed: {:.2?}", elapsed);
47 }
48
49 #[test]
50 fn with_debs() {
51 let cache = new_cache!(&[
52 "tests/files/cache/apt.deb",
53 "tests/files/cache/dep-pkg1_0.0.1.deb",
54 ])
55 .unwrap();
56
57 cache.get("apt").unwrap().get_version("5000:1.0.0").unwrap();
58 cache.get("dep-pkg1").unwrap();
59
60 assert!(new_cache!(&["tests/files/this-file-doesnt-exist.deb"]).is_err());
61
62 // Check if it errors on a garbage empty file as well
63 // signal: 11, SIGSEGV: invalid memory reference
64 assert!(new_cache!(&["tests/files/cache/pkg.deb"]).is_err());
65 }
66
67 #[test]
68 fn with_packages() {
69 let cache = new_cache!(&["tests/files/cache/Packages",]).unwrap();
70
71 cache.get("apt").unwrap().get_version("5000:1.0.0").unwrap();
72 cache.get("broken-or-dep").unwrap();
73 cache.get("dep-pkg1").unwrap().get_version("0.0.1").unwrap();
74 cache.get("dep-pkg1").unwrap().get_version("0.0.2").unwrap();
75 cache.get("dep-pkg2").unwrap();
76 cache.get("no-description").unwrap();
77
78 assert!(new_cache!(&["tests/files/cache/Packages.gz"]).is_err());
79 }
80
81 #[test]
82 fn empty_deps() {
83 // This would fail before https://gitlab.com/volian/rust-apt/-/merge_requests/29
84 let cache = new_cache!().unwrap();
85 let sort = PackageSort::default();
86
87 // Iterate through all of the package and versions
88 for pkg in cache.packages(&sort) {
89 for version in pkg.versions() {
90 // Call depends_map to check for panic on null dependencies.
91 version.depends_map();
92 }
93 }
94 }
95
96 #[test]
97 fn parent_pkg() {
98 let cache = new_cache!().unwrap();
99 let pkg = cache.get("apt").unwrap();
100 let version = pkg.versions().next().unwrap();
101 let parent = version.parent();
102 assert_eq!(pkg.index(), parent.index())
103 }
104
105 #[test]
106 fn get_version() {
107 let cache = new_cache!().unwrap();
108 let pkg = cache.get("apt").unwrap();
109
110 // The candidate for apt surely exists.
111 let cand = pkg.candidate().unwrap();
112 assert!(pkg.get_version(cand.version()).is_some());
113
114 // I sure hope this doesn't exist.
115 assert!(pkg.get_version("9.0.0.1").is_none());
116 }
117
118 #[test]
119 fn all_packages() {
120 let cache = new_cache!().unwrap();
121 let sort = PackageSort::default();
122
123 // All real packages should not be empty.
124 assert!(cache.packages(&sort).next().is_some());
125 for pkg in cache.packages(&sort) {
126 // impl display??
127 // println!("{pkg}")
128 println!("{}:{}", pkg.name(), pkg.arch())
129 }
130 }
131
132 #[test]
133 fn descriptions() {
134 let cache = new_cache!().unwrap();
135
136 // Apt should exist
137 let pkg = cache.get("apt").unwrap();
138 // Apt should have a candidate
139 let cand = pkg.candidate().unwrap();
140 // Apt should be installed
141 let inst = pkg.installed().unwrap();
142
143 // Assign installed descriptions
144 let inst_sum = inst.summary();
145 let inst_desc = inst.description();
146
147 // Assign candidate descriptions
148 let cand_sum = cand.summary();
149 let cand_desc = cand.description();
150
151 // If the lookup fails for whatever reason
152 // The summary and description are the same
153 assert_ne!(inst_sum, inst_desc);
154 assert_ne!(cand_sum, cand_desc);
155 dbg!(inst_desc);
156 dbg!(cand_desc);
157 }
158
159 // This should not segfault, but has in the past.
160 // See https://gitlab.com/volian/rust-apt/-/issues/28
161 #[test]
162 fn no_description() {
163 let cache = new_cache!(&["tests/files/cache/no-description_0.0.1.deb"]).unwrap();
164
165 let pkg = cache.get("no-description").unwrap();
166 let cand = pkg.candidate().unwrap();
167 if let Some(desc) = cand.description() {
168 println!("{desc}");
169 }
170 }
171
172 #[test]
173 fn version_uris() {
174 let cache = new_cache!().unwrap();
175 let pkg = cache.get("apt").unwrap();
176 // Only test the candidate.
177 // It's possible for the installed version to have no uris
178 let cand = pkg.candidate().unwrap();
179 assert!(cand.uris().next().is_some());
180 dbg!(cand.uris().collect::<Vec<_>>());
181 }
182
183 #[test]
184 fn depcache_marked() {
185 let cache = new_cache!().unwrap();
186 let pkg = cache.get("apt").unwrap();
187 assert!(!pkg.marked_install());
188 assert!(!pkg.marked_upgrade());
189 assert!(!pkg.marked_delete());
190 assert!(pkg.marked_keep());
191 assert!(!pkg.marked_downgrade());
192 assert!(!pkg.marked_reinstall());
193 assert!(!pkg.is_now_broken());
194 assert!(!pkg.is_inst_broken());
195 }
196
197 #[test]
198 fn hashes() {
199 let cache = new_cache!().unwrap();
200 let pkg = cache.get("apt").unwrap();
201 // Apt could be installed, and the package no longer exists
202 // In the cache. For this case we grab the candidate so it won't fail.
203 let version = pkg.candidate().unwrap();
204 assert!(version.sha256().is_some());
205 assert!(version.hash("sha256").is_some());
206 assert!(version.sha512().is_none());
207 assert!(version.hash("md5sum").is_some());
208 assert!(version.hash("sha1").is_none())
209 }
210
211 #[test]
212 fn shortname() {
213 let cache = new_cache!().unwrap();
214 let sort = PackageSort::default();
215 for pkg in cache.packages(&sort) {
216 assert!(!pkg.name().contains(':'))
217 }
218 }
219
220 #[test]
221 fn depends() {
222 let cache = new_cache!().unwrap();
223
224 let pkg = cache.get("apt").unwrap();
225 let cand = pkg.candidate().unwrap();
226 // Apt candidate should have dependencies
227 for deps in cand.dependencies().unwrap() {
228 for dep in deps.iter() {
229 // Apt Dependencies should have targets
230 assert!(dep.all_targets().first().is_some());
231 }
232 }
233 assert!(cand.recommends().is_some());
234 assert!(cand.suggests().is_some());
235 // TODO: Add these as methods
236 assert!(cand.get_depends(&DepType::Replaces).is_some());
237 // This test seems to work on Debian Sid desktop systems, but not in a Debian
238 // Sid Docker container (and potentially other distros too). Leaving this
239 // commented out until a solution is found.
240 // assert!(cand.get_depends("Conflicts").is_some());
241 assert!(cand.get_depends(&DepType::DpkgBreaks).is_some());
242
243 // This part is basically just formatting an apt depends String
244 // Like you would see in `apt show`
245 let mut dep_str = String::new();
246 dep_str.push_str("Depends: ");
247 dbg!(cand.depends_map().get(&DepType::Depends).unwrap());
248 for dep in cand.depends_map().get(&DepType::Depends).unwrap() {
249 if dep.is_or() {
250 let mut or_str = String::new();
251 let total = dep.len() - 1;
252 for (num, base_dep) in dep.iter().enumerate() {
253 or_str.push_str(base_dep.name());
254 if let Some(comp) = base_dep.comp_type() {
255 let _ = write!(or_str, "({} {})", comp, base_dep.version().unwrap());
256 }
257 if num != total {
258 or_str.push_str(" | ");
259 } else {
260 or_str.push_str(", ");
261 }
262 }
263 dep_str.push_str(&or_str)
264 } else {
265 let lone_dep = dep.first();
266 dep_str.push_str(lone_dep.name());
267
268 if let Some(comp) = lone_dep.comp_type() {
269 let _ = write!(dep_str, " ({} {})", comp, lone_dep.version().unwrap());
270 }
271 dep_str.push_str(", ");
272 }
273 }
274 println!("{dep_str}");
275 }
276
277 #[test]
278 fn test_hashmap() {
279 let cache = new_cache!().unwrap();
280
281 // clippy thinks that the package is mutable
282 // But it only hashes the ID and you can't really mutate a version
283 #[allow(clippy::mutable_key_type)]
284 let mut pkg_map = HashMap::new();
285
286 // clippy thinks that the version is mutable
287 // But it only hashes the ID and you can't really mutate a version
288 #[allow(clippy::mutable_key_type)]
289 let mut ver_map = HashMap::new();
290
291 let sort = PackageSort::default();
292
293 // Iterate the package cache and add them to a hashmap
294 for pkg in cache.packages(&sort) {
295 let value = pkg.arch().to_string();
296 pkg_map.insert(pkg, value);
297 }
298
299 // Iterate the package map and add all the candidates into a hashmap
300 for (pkg, _arch) in pkg_map.iter() {
301 if let Some(cand) = pkg.candidate() {
302 let value = cand.arch().to_string();
303 ver_map.insert(cand, value);
304 }
305 }
306 // Doesn't need an assert. It won't compile
307 // if the structs can't go into a hashmap
308 }
309
310 #[test]
311 fn debug_interfaces() {
312 let cache = new_cache!().unwrap();
313 let pkg = cache.get("apt").unwrap();
314 println!("{pkg:?}");
315 println!("{pkg:#?}");
316
317 let cand = pkg.candidate().unwrap();
318 println!("{cand:?}");
319 println!("{cand:#?}");
320
321 for dep_vec in cand.depends_map().values() {
322 for dep in dep_vec {
323 println!("{dep:#?}");
324 }
325 }
326
327 let pkg = cache.get("python3:any").unwrap();
328 for provider in pkg.provides() {
329 println!("{provider:#?}")
330 }
331 }
332
333 #[test]
334 fn display_interfaces() {
335 let cache = new_cache!().unwrap();
336 let pkg = cache.get("apt").unwrap();
337
338 println!("{pkg}");
339
340 let cand = pkg.candidate().unwrap();
341 println!("{cand}");
342
343 for dep_vec in cand.depends_map().values() {
344 for dep in dep_vec {
345 println!("{dep}");
346 }
347 }
348
349 let pkg = cache.get("python3:any").unwrap();
350 for provider in pkg.provides() {
351 println!("{provider}")
352 }
353 }
354
355 #[test]
356 fn parent_dep() {
357 let cache = new_cache!().unwrap();
358 let sort = PackageSort::default();
359
360 for pkg in cache.packages(&sort) {
361 // Iterate over the reverse depends
362 // Iterating rdepends could segfault.
363 // See: https://gitlab.com/volian/rust-apt/-/merge_requests/36
364 for deps in pkg.rdepends().values() {
365 for dep in deps {
366 let base_dep = dep.first();
367 // Reverse Dependencies always have a version
368 base_dep.version().unwrap();
369 }
370 }
371
372 // There should be a candidate to iterate its regular deps
373 if let Some(cand) = pkg.candidate() {
374 if let Some(deps) = cand.dependencies() {
375 for dep in &deps {
376 let base_dep = dep.first();
377 // Regular deps do not always have a version
378 base_dep.version();
379 }
380 }
381 }
382 }
383 }
384
385 #[test]
386 fn provides_list() {
387 let cache = new_cache!().unwrap();
388 let pkg = cache.get("apt").unwrap();
389 let cand = pkg.candidate().unwrap();
390 let provides_list: Vec<_> = cand.provides().collect();
391
392 assert!(provides_list.len() == 1);
393 // 'apt' seems to always provide for 'apt-transport-https' at APT's version.
394 // If it ever doesn't, this test will break.
395 let provide = provides_list.first().unwrap();
396 assert!(provide.name() == "apt-transport-https");
397 assert!(provide.version_str().unwrap() == cand.version());
398 }
399
400 // This Test is for https://gitlab.com/volian/rust-apt/-/issues/24
401 // TODO: refactor and enable this test so it can run in the CI to make sure we
402 // don't regress. We need to get the lists dir from the apt config, and then
403 // maybe pick a random InRelease file Back that up, do the editing and then
404 // restore it at the end of the test. cache.packages should be an error and not
405 // segfault.
406 //
407 // #[test]
408 // fn test_segfault() {
409 // use std::io::Write;
410
411 // let mut f = std::fs::OpenOptions::new()
412 // .write(true)
413 // .append(true)
414 // .open("/var/lib/apt/lists/deb.debian.org_debian_dists_sid_InRelease")
415 // .unwrap();
416
417 // f.write_all(b"\ndsadasdasdas\n").unwrap();
418 // f.flush().unwrap();
419
420 // drop(f);
421
422 // let cache = new_cache!().unwrap();
423
424 // let sort = PackageSort::default();
425
426 // assert!(cache.packages(&sort).is_err())
427
428 /// This test is tied pretty closely to the currently available versions in
429 /// the Ubuntu/Debian repos. Feel free to adjust if you can verify its
430 /// needed.
431 #[test]
432 fn rev_provides_list() {
433 // Test concrete packages with provides.
434 let cache = new_cache!().unwrap();
435 let apt = cache.get("apt").unwrap();
436 let ver = apt.candidate().unwrap();
437 let pkg = cache.get("apt-transport-https").unwrap();
438
439 {
440 let mut rev_provides_list = pkg.provides();
441 let provides_pkg = rev_provides_list.next().unwrap();
442
443 assert!(rev_provides_list.next().is_none());
444
445 let parent = unsafe { provides_pkg.target_pkg() };
446 assert!(parent.name().contains("apt"));
447 assert_eq!(provides_pkg.version_str().unwrap(), ver.version());
448 }
449
450 {
451 let mut rev_provides_list = pkg.provides().filter(|p| match p.version_str() {
452 Err(_) => false,
453 Ok(version) => version == ver.version(),
454 });
455
456 let provides_pkg = rev_provides_list.next().unwrap();
457 assert!(rev_provides_list.next().is_none());
458
459 let parent = unsafe { provides_pkg.target_pkg() };
460 assert_eq!(parent.name(), "apt");
461
462 assert_eq!(provides_pkg.version_str().unwrap(), ver.version());
463 }
464
465 {
466 let mut rev_provides_list = pkg.provides().filter(|p| match p.version_str() {
467 Err(_) => false,
468 Ok(version) => version == "50000000000.0.0",
469 });
470 assert!(rev_provides_list.next().is_none());
471 }
472
473 // Test a virtual package with provides.
474 {
475 let pkg = cache.get("www-browser").unwrap();
476 assert!(pkg.provides().next().is_some());
477 }
478 }
479
480 #[test]
481 fn sources() {
482 let cache = new_cache!().unwrap();
483
484 let uris: UniquePtr<CxxVector<ItemDesc>>;
485
486 let acquire = unsafe { create_acquire() };
487 cache.get_indexes(&acquire);
488
489 // If the source lists don't exists there is problems.
490 uris = unsafe { acquire.uris() };
491 assert!(!uris.is_empty());
492 // This is an example of why the uri function is unsafe.
493 // uncommenting drop will cause it to SIBABRT
494 // drop(acquire);
495
496 for item in uris.iter() {
497 println!("{} = {}", item.uri(), item.owner().dest_file())
498 }
499 }
500
501 #[test]
502 fn cache_count() {
503 let cache = new_cache!().unwrap();
504 match cache.depcache().disk_size() {
505 DiskSpace::Require(num) => {
506 assert_eq!(num, 0);
507 },
508 DiskSpace::Free(num) => {
509 panic!("Whoops it should be 0, not {num}.");
510 },
511 }
512 }
513
514 #[test]
515 fn test_unit_str() {
516 let testcase = [
517 (1649267441664_u64, "1.50 TiB", "1.65 TB"),
518 (1610612736_u64, "1.50 GiB", "1.61 GB"),
519 (1572864_u64, "1.50 MiB", "1.57 MB"),
520 (1536_u64, "1.50 KiB", "1.54 KB"),
521 (1024_u64, "1024 B", "1.02 KB"),
522 (1_u64, "1 B", "1 B"),
523 ];
524
525 for (num, binary, decimal) in testcase {
526 assert_eq!(binary, unit_str(num, NumSys::Binary));
527 assert_eq!(decimal, unit_str(num, NumSys::Decimal));
528 }
529 }
530
531 #[test]
532 // This test relies on the version of 'apt' being higher than 'dpkg'.
533 fn version_comparisons() {
534 let cache = new_cache!().unwrap();
535 let apt = cache.get("apt").unwrap();
536 let dpkg = cache.get("dpkg").unwrap();
537
538 let apt_ver = apt.candidate().unwrap();
539 let dpkg_ver = dpkg.candidate().unwrap();
540
541 assert!(apt_ver > dpkg_ver);
542 assert!(dpkg_ver < apt_ver);
543 assert!(apt_ver != dpkg_ver);
544 }
545
546 #[test]
547 // This test relies on 'neofetch' and 'gsasl-common' not being installed.
548 fn good_resolution() {
549 let cache = new_cache!().unwrap();
550 let pkg = cache.get("neofetch").unwrap();
551
552 pkg.mark_install(true, true);
553 pkg.protect();
554 cache.resolve(false).unwrap();
555
556 let pkg2 = cache.get("gsasl-common").unwrap();
557 pkg2.mark_install(true, true);
558 assert!(pkg2.marked_install())
559 }
560
561 // For now `zeek` has broken dependencies so the resolver errors.
562 // If this test fails, potentially find a reason.
563 #[test]
564 fn bad_resolution() {
565 let cache = new_cache!().unwrap();
566
567 let pkg = cache.get("zeek").unwrap();
568
569 pkg.mark_install(false, true);
570 pkg.protect();
571
572 assert!(cache.resolve(false).is_err());
573 }
574
575 #[test]
576 fn depcache_clear() {
577 let cache = new_cache!().unwrap();
578 let pkg = cache.get("apt").unwrap();
579
580 pkg.mark_delete(true);
581
582 assert!(pkg.marked_delete());
583
584 cache.depcache().clear_marked().unwrap();
585 assert!(!pkg.marked_delete());
586 }
587
588 #[test]
589 fn origins() {
590 let cache = new_cache!().unwrap();
591 let apt = cache.get("apt").unwrap();
592 let apt_ver = apt.candidate().unwrap();
593 let pkg_files = apt_ver.package_files().collect::<Vec<_>>();
594
595 // Package files should not be empty if we got a candidate from `apt`.
596 assert!(!pkg_files.is_empty());
597
598 for pkg_file in pkg_files {
599 // Apt should have all of these blocks in the package file.
600 assert!(pkg_file.filename().is_some());
601 assert!(pkg_file.archive().is_some());
602
603 println!("{}", pkg_file.filename().unwrap());
604
605 if pkg_file.is_downloadable() {
606 assert!(pkg_file.origin().is_some());
607 assert!(pkg_file.codename().is_some());
608 assert!(pkg_file.label().is_some());
609 assert!(pkg_file.site().is_some());
610 assert!(pkg_file.arch().is_some());
611 }
612
613 // These should be okay regardless.
614 assert!(pkg_file.component().is_some());
615 assert!(pkg_file.index_type().is_some());
616
617 // Index should not be 0.
618 assert_ne!(pkg_file.index(), 0);
619
620 // Apt should likely be from a trusted repository.
621 assert!(pkg_file.index_file().is_trusted());
622 // Print it in case I want to see.
623 // println!("{pkg_file}");
624 }
625 }
626
627 #[test]
628 fn depcache_install_ver() {
629 let cache = new_cache!(&[
630 "tests/files/cache/dep-pkg1_0.0.1.deb",
631 "tests/files/cache/dep-pkg1_0.0.2.deb",
632 ])
633 .unwrap();
634
635 let pkg = cache.get("dep-pkg1").unwrap();
636 pkg.mark_install(false, false);
637
638 // This package is not installed, only marked
639 assert!(pkg.installed().is_none());
640
641 // This package is not installed
642 // but this will return the version to be installed
643 let install_ver = pkg.install_version().unwrap();
644
645 // The version should match the latest because it's the default candidate.
646 assert!(install_ver.version() == "0.0.2");
647
648 let old_ver = pkg.get_version("0.0.1").unwrap();
649 old_ver.set_candidate();
650 pkg.mark_install(false, false);
651
652 let install_ver = pkg.install_version().unwrap();
653
654 // Now it should match the old version we just marked.
655 assert!(install_ver.version() == "0.0.1");
656 }
657
658 #[test]
659 fn broken_pkgs() {
660 let cache = new_cache!(&["tests/files/cache/broken-or-dep_0.0.1.deb"]).unwrap();
661
662 let pkg = cache.get("broken-or-dep").unwrap();
663
664 // let config = Config::new();
665 // config.set("Debug::pkgProblemResolver", "1");
666
667 pkg.protect();
668 pkg.mark_install(false, true);
669
670 let expected = concat!(
671 " broken-or-dep : Depends: not-exist (>= 3.6.1) but it is not installable or\n",
672 " really-not-exist but it is not installable\n",
673 " Depends: python3-not-exist but it is not installable\n",
674 );
675
676 let err = cache.resolve(false).unwrap_err();
677
678 for pkg in cache.iter() {
679 if let Some(broken) = show_broken_pkg(&cache, &pkg, false) {
680 assert_eq!(broken, expected);
681 println!("{broken}");
682 }
683 }
684 println!("{err}");
685 }
686 }