servlet 1.1.0

A small application runtime with an explicit operational contract
Documentation
//! Actual secret files, projected symlinks, invalid inputs, and nonblocking FIFOs.

use super::*;
use crate::settings::{MAX_VALUE, Settings, Snapshot};
use std::{
    fs,
    path::PathBuf,
    sync::atomic::{AtomicU64, Ordering},
};
static NEXT: AtomicU64 = AtomicU64::new(0);
struct Directory(PathBuf);
impl Directory {
    fn new() -> Self {
        let base =
            PathBuf::from(std::env::var_os("CARGO_TARGET_DIR").unwrap_or_else(|| "target".into()));
        let path = base.join("settings-tests").join(format!(
            "{}-{}",
            std::process::id(),
            NEXT.fetch_add(1, Ordering::Relaxed)
        ));
        fs::create_dir_all(&path).unwrap();
        Self(path)
    }
    fn path(&self, name: &str) -> PathBuf {
        self.0.join(name)
    }
}
impl Drop for Directory {
    fn drop(&mut self) {
        let _ = fs::remove_dir_all(&self.0);
    }
}
fn load(path: &Path) -> Result<Snapshot, SettingsError> {
    Settings::new()
        .secret("KEY", Some("fallback"))?
        .secret_file("KEY", path)?
        .capture_from([] as [(&str, &str); 0])
}

#[test]
fn file_precedence_exact_content_and_snapshot_isolation() {
    let dir = Directory::new();
    let path = dir.path("private-path");
    fs::write(&path, b"private-value\r\n").unwrap();
    let snapshot = load(&path).unwrap();
    fs::write(&path, "changed").unwrap();
    assert_eq!(
        snapshot.secret::<String>("KEY").unwrap().expose(),
        "private-value\r\n"
    );
    assert_eq!(
        load(&path)
            .unwrap()
            .secret::<String>("KEY")
            .unwrap()
            .expose(),
        "changed"
    );
    fs::remove_file(&path).unwrap();
    let snapshot = Settings::new()
        .secret("KEY", None)
        .unwrap()
        .secret_file("KEY", &path)
        .unwrap()
        .capture_from([("KEY", "environment")])
        .unwrap();
    assert_eq!(
        snapshot.secret::<String>("KEY").unwrap().expose(),
        "environment"
    );
    let snapshot = Settings::new()
        .secret("KEY", None)
        .unwrap()
        .secret_file("KEY", &path)
        .unwrap()
        .override_value("KEY", "override")
        .unwrap()
        .capture_from([("KEY", "environment")])
        .unwrap();
    assert_eq!(
        snapshot.secret::<String>("KEY").unwrap().expose(),
        "override"
    );
    let error = load(&path).unwrap_err();
    assert_eq!(error.kind(), Kind::SecretFile);
    assert!(!format!("{error} {error:?}").contains("private-path"));
}

#[test]
fn file_bounds_encoding_regular_descriptor_and_symlinks() {
    let dir = Directory::new();
    let path = dir.path("source");
    fs::write(&path, "x".repeat(MAX_VALUE)).unwrap();
    assert_eq!(
        load(&path)
            .unwrap()
            .secret::<String>("KEY")
            .unwrap()
            .expose()
            .len(),
        MAX_VALUE
    );
    fs::write(&path, "x".repeat(MAX_VALUE + 1)).unwrap();
    assert_eq!(load(&path).unwrap_err().kind(), Kind::Limit);
    fs::write(&path, b"private\xff").unwrap();
    assert_eq!(load(&path).unwrap_err().kind(), Kind::Encoding);
    fs::write(&path, "").unwrap();
    assert_eq!(
        load(&path)
            .unwrap()
            .secret::<String>("KEY")
            .unwrap()
            .expose(),
        ""
    );
    assert_eq!(load(&dir.0).unwrap_err().kind(), Kind::SecretFile);
    let link = dir.path("projected");
    std::os::unix::fs::symlink(path.file_name().unwrap(), &link).unwrap();
    assert_eq!(
        load(&link)
            .unwrap()
            .secret::<String>("KEY")
            .unwrap()
            .expose(),
        ""
    );
    assert_eq!(
        load(Path::new("/dev/null")).unwrap_err().kind(),
        Kind::SecretFile
    );
    let fifo = dir.path("fifo");
    assert!(
        std::process::Command::new("mkfifo")
            .arg(&fifo)
            .status()
            .unwrap()
            .success()
    );
    assert_eq!(load(&fifo).unwrap_err().kind(), Kind::SecretFile);
}