Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_core/tests/path_canonical.rs

1.9 KiB, 1 run

created by r1870400018:75193, which is this file's identity for as long as the history lasts, whatever it is later renamed to

download · who wrote it · its history

1//! `path::canonicalise_allow_missing` follows a symlink in the part of a path that exists and
2//! places the part that does not, so a containment check cannot be fooled by either.
3//!
4//! [Written with AI entirely](https://need2know.ai/entirely-ai/code)\
5//! Anthropic Claude
6
7use oxedyne_fe2o3_core::{
8 prelude::*,
9 path::canonicalise_allow_missing,
10};
11
12use std::path::PathBuf;
13
14/// A fresh directory under the cargo target, never `/tmp` (a tmpfs).
15fn scratch(label: &str) -> PathBuf {
16 let dir = PathBuf::from(env!("CARGO_TARGET_TMPDIR"))
17 .join(fmt!("{}_{}", label, std::process::id()));
18 let _ = std::fs::remove_dir_all(&dir);
19 std::fs::create_dir_all(&dir).expect("scratch dir");
20 std::fs::canonicalize(&dir).expect("canonical scratch dir")
21}
22
23#[test]
24fn test_canonicalise_allow_missing_places_a_path_that_is_not_there() -> Outcome<()> {
25 let root = scratch("cam_missing");
26 let got = res!(canonicalise_allow_missing(&root.join("keys/./oregami/../oregami/service_key")));
27 assert_eq!(got, root.join("keys/oregami/service_key"));
28 Ok(())
29}
30
31#[test]
32fn test_canonicalise_allow_missing_follows_a_symlink_out() -> Outcome<()> {
33 let root = scratch("cam_link");
34 let outside = scratch("cam_outside");
35 std::os::unix::fs::symlink(&outside, root.join("keys")).expect("symlink");
36 // The link exists, the file below it does not: the answer is where the link points.
37 let got = res!(canonicalise_allow_missing(&root.join("keys/sk")));
38 assert_eq!(got, outside.join("sk"));
39 assert!(!got.starts_with(&root), "a symlink out must not read as under the root");
40 Ok(())
41}
42
43#[test]
44fn test_canonicalise_allow_missing_climbs_out_with_dot_dot() -> Outcome<()> {
45 let root = scratch("cam_up");
46 let got = res!(canonicalise_allow_missing(&root.join("nope/../../elsewhere")));
47 assert_eq!(Some(got.as_path()), root.parent().map(|p| p.join("elsewhere")).as_deref());
48 Ok(())
49}