about summary refs log tree commit diff
path: root/tvix/nix-compat/src/nar/listing/test.rs
blob: 5b2ac3f166fe30895f637919cd718074353ddd55 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
use std::{collections::HashMap, path::PathBuf, str::FromStr};

use crate::nar;

#[test]
fn weird_paths() {
    let root = nar::listing::ListingEntry::Directory {
        entries: HashMap::new(),
    };

    root.locate("../../../../etc/passwd")
        .expect_err("Failed to reject `../` fragment in a path during traversal");

    // Gated on Windows as C:\\ is parsed as `Component::Normal(_)` on Linux.
    #[cfg(target_os = "windows")]
    root.locate("C:\\\\Windows\\System32")
        .expect_err("Failed to reject Windows-style prefixes");

    root.locate("/etc/passwd")
        .expect_err("Failed to reject absolute UNIX paths");
}

#[test]
fn nixos_release() {
    let listing_bytes = include_bytes!("../tests/nixos-release.ls");
    let listing: nar::listing::Listing = serde_json::from_slice(listing_bytes).unwrap();

    let nar::listing::Listing::V1 { root, .. } = listing;
    assert!(matches!(root, nar::listing::ListingEntry::Directory { .. }));

    let build_products = root
        .locate(PathBuf::from_str("nix-support/hydra-build-products").unwrap())
        .expect("Failed to locate a known file in a directory")
        .expect("File was unexpectedly not found in the listing");

    assert!(matches!(
        build_products,
        nar::listing::ListingEntry::Regular { .. }
    ));

    let nonexisting_file = root
        .locate(PathBuf::from_str("nix-support/does-not-exist").unwrap())
        .expect("Failed to locate an unknown file in a directory");

    assert!(
        nonexisting_file.is_none(),
        "Non-existing file was unexpectedly found in the listing"
    );

    let existing_dir = root
        .locate(PathBuf::from_str("nix-support").unwrap())
        .expect("Failed to locate a known directory in a directory")
        .expect("Directory was expectedly found in the listing");

    assert!(matches!(
        existing_dir,
        nar::listing::ListingEntry::Directory { .. }
    ));
}