ostrya-remote 0.19.1

The push and pull protocol of ostrya: the wire codec, the push client session, and the ssh and HTTP transports
Documentation
//! Tests of the refusals of a tree push before a session.
//!
//! A walk error, a hash error, and an option that the push refuses before the
//! scan start no ssh client. These refusals send no request. For these
//! refusals, the push over a pair of streams writes no byte.

#![cfg(unix)]

use std::fs;
use std::io;
use std::os::unix::fs::PermissionsExt;
use std::path::{Path, PathBuf};
use std::pin::Pin;
use std::sync::Arc;
use std::sync::atomic::{AtomicU64, Ordering};
use std::task::{Context, Poll};

use futures_io::AsyncWrite;
use ostrya_core::Ref;
use ostrya_remote::{
    Compression, Endpoint, Error, SshAddress, SshOptions, TreePushOptions, push_tree,
    push_tree_over_stream,
};

/// A scratch directory under the temporary directory. A drop removes it.
struct Scratch {
    path: PathBuf,
}

impl Scratch {
    fn new(tag: &str) -> Scratch {
        static COUNTER: AtomicU64 = AtomicU64::new(0);
        let n = COUNTER.fetch_add(1, Ordering::Relaxed);
        let path = std::env::temp_dir().join(format!(
            "ostrya-remote-tree-push-{}-{tag}-{n}",
            std::process::id()
        ));
        let _ = fs::remove_dir_all(&path);
        fs::create_dir_all(&path).unwrap();
        Scratch { path }
    }
}

impl Drop for Scratch {
    fn drop(&mut self) {
        let _ = fs::remove_dir_all(&self.path);
    }
}

fn set_mode(path: &Path, mode: u32) {
    fs::set_permissions(path, fs::Permissions::from_mode(mode)).unwrap();
}

/// A tree of one directory and two files under `base/tree`.
fn tree(base: &Path) -> PathBuf {
    let root = base.join("tree");
    fs::create_dir_all(root.join("dir")).unwrap();
    fs::write(root.join("file"), b"file\n").unwrap();
    fs::write(root.join("dir/inner"), b"inner\n").unwrap();
    root
}

/// The endpoint of the ssh address `address` with a stand-in ssh client that
/// creates `marker` and exits.
fn stand_in(address: &str, marker: &Path) -> Endpoint {
    let script = format!("touch '{}'", marker.to_str().unwrap());
    Endpoint::Ssh {
        address: SshAddress::parse(address).unwrap(),
        options: SshOptions {
            command: Some(
                ["sh", "-c", &script, "stand-in"]
                    .iter()
                    .map(|s| s.to_string())
                    .collect(),
            ),
            ..Default::default()
        },
    }
}

/// Reads `text` as a ref. Text that is no ref is a local ref of that name,
/// which the push refuses.
fn r(text: &str) -> Ref {
    text.parse().unwrap_or_else(|_| Ref::local(text))
}

fn options(branch: &str) -> TreePushOptions {
    TreePushOptions {
        onto: r(branch),
        timestamp: Some(1_700_000_000),
        ..Default::default()
    }
}

/// An output stream that counts the bytes that it receives.
struct Counted(Arc<AtomicU64>);

impl AsyncWrite for Counted {
    fn poll_write(
        self: Pin<&mut Self>,
        _cx: &mut Context<'_>,
        buf: &[u8],
    ) -> Poll<io::Result<usize>> {
        self.0.fetch_add(buf.len() as u64, Ordering::Relaxed);
        Poll::Ready(Ok(buf.len()))
    }

    fn poll_flush(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<io::Result<()>> {
        Poll::Ready(Ok(()))
    }

    fn poll_close(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<io::Result<()>> {
        Poll::Ready(Ok(()))
    }
}

/// Runs `push_tree` of `root` over the stand-in and returns its error.
///
/// The function checks that the stand-in did not start. It also runs
/// `push_tree_over_stream` with the same options. That push must write no byte
/// and must fail with the same `Error` variant. `opts` builds the options for
/// each push, because the options are not `Clone`.
fn refused_with_no_start(
    tag: &str,
    address: &str,
    root: &Path,
    opts: impl Fn() -> TreePushOptions,
) -> Error {
    let scratch = Scratch::new(tag);
    let marker = scratch.path.join("started");
    let result = ostrya_rt::block_on(push_tree(stand_in(address, &marker), root, opts()));
    assert!(!marker.exists(), "{tag}: the ssh client started");

    let written = Arc::new(AtomicU64::new(0));
    let over_stream = ostrya_rt::block_on(push_tree_over_stream(
        futures_lite::io::empty(),
        Counted(written.clone()),
        root,
        opts(),
    ));
    assert_eq!(written.load(Ordering::Relaxed), 0, "{tag}: bytes written");
    let e = match result {
        Err(e) => e,
        Ok(outcome) => panic!("{tag}: pushed: {outcome:?}"),
    };
    match over_stream {
        Err(other) => assert_eq!(
            std::mem::discriminant(&other),
            std::mem::discriminant(&e),
            "{tag}: {other:?} against {e:?}"
        ),
        Ok(outcome) => panic!("{tag}: pushed over the streams: {outcome:?}"),
    }
    e
}

/// Returns the path and the I/O error kind of an `Error::Walk`.
fn walk_error(tag: &str, e: Error) -> (PathBuf, io::ErrorKind) {
    match e {
        Error::Walk { path, source } => (path, source.kind()),
        other => panic!("{tag}: expected Error::Walk, got {other:?}"),
    }
}

fn invalid_input(tag: &str, e: Error, part: &str) {
    match e {
        Error::InvalidInput(m) => assert!(m.contains(part), "{tag}: {m}"),
        other => panic!("{tag}: expected Error::InvalidInput, got {other:?}"),
    }
}

const ADDRESS: &str = "host:repo";

/// A builder of the options of one push.
type MakeOptions = Box<dyn Fn() -> TreePushOptions>;

#[test]
fn a_tree_that_scans_starts_the_ssh_client() {
    let scratch = Scratch::new("control");
    let root = tree(&scratch.path);
    let marker = scratch.path.join("started");
    let result = ostrya_rt::block_on(push_tree(
        stand_in(ADDRESS, &marker),
        &root,
        options("main"),
    ));
    assert!(marker.exists(), "the ssh client did not start");
    // The stand-in exits with no reply, so the session cannot open.
    assert!(
        matches!(result, Err(Error::Io(_) | Error::Transport(_))),
        "{result:?}"
    );
}

#[test]
fn a_walk_error_starts_no_ssh_client() {
    let scratch = Scratch::new("walk");
    let missing = scratch.path.join("missing");
    let e = refused_with_no_start("missing root", ADDRESS, &missing, || options("main"));
    assert_eq!(
        walk_error("missing root", e),
        (missing.clone(), io::ErrorKind::NotFound)
    );

    let root = tree(&scratch.path);
    let fifo = root.join("dir/fifo");
    match std::process::Command::new("mkfifo").arg(&fifo).status() {
        Ok(status) if status.success() => {
            let e = refused_with_no_start("fifo", ADDRESS, &root, || options("main"));
            assert_eq!(
                walk_error("fifo", e),
                (fifo.clone(), io::ErrorKind::Unsupported)
            );
        }
        other => eprintln!("skipping the fifo: mkfifo did not create one: {other:?}"),
    }
}

#[test]
fn a_hash_error_starts_no_ssh_client() {
    let scratch = Scratch::new("hash");
    let root = tree(&scratch.path);
    let file = root.join("dir/inner");
    set_mode(&file, 0o000);
    if fs::File::open(&file).is_ok() {
        eprintln!("skipping: the process opens a file at mode 0000");
        return;
    }
    let e = refused_with_no_start("hash", ADDRESS, &root, || options("main"));
    assert_eq!(
        walk_error("hash", e),
        (file.clone(), io::ErrorKind::PermissionDenied)
    );
}

#[test]
fn options_refused_before_the_scan_start_no_ssh_client() {
    let scratch = Scratch::new("options");
    let root = tree(&scratch.path);
    let variant =
        || ostrya_core::Value::variant(ostrya_core::Type::Str, ostrya_core::Value::Str("v".into()));
    let cases: Vec<(&str, MakeOptions, &str)> = vec![
        (
            "hash_jobs 0",
            Box::new(|| TreePushOptions {
                hash_jobs: Some(0),
                ..options("main")
            }),
            "hash_jobs",
        ),
        (
            "no branch",
            Box::new(|| options("")),
            "not a valid ref name",
        ),
        (
            "a tag twice",
            Box::new(|| TreePushOptions {
                tags: vec![r("a"), r("a")],
                ..options("main")
            }),
            "twice",
        ),
        (
            "a tag that is the branch",
            Box::new(|| TreePushOptions {
                tags: vec![r("main")],
                ..options("main")
            }),
            "also the branch",
        ),
        (
            "a bad branch",
            Box::new(|| options("a/../b")),
            "not a valid ref name",
        ),
        (
            "a name outside the rule",
            Box::new(|| options("a b")),
            "not a valid ref name",
        ),
        (
            "a remote ref",
            Box::new(|| options("origin:main")),
            "is a remote ref",
        ),
        (
            "a ref of another collection",
            Box::new(|| TreePushOptions {
                collection: Some("org.example.X".into()),
                ..options("@org.example.Y:main")
            }),
            "names the collection 'org.example.Y'",
        ),
        (
            "refs of two collections",
            Box::new(|| TreePushOptions {
                tags: vec![r("@org.example.Y:t")],
                ..options("@org.example.X:main")
            }),
            "in two namespaces",
        ),
        (
            "a parent revision",
            Box::new(|| TreePushOptions {
                tags: vec![r("main^")],
                ..options("main")
            }),
            "not a valid ref name",
        ),
        (
            "a bad bound name",
            Box::new(|| TreePushOptions {
                bind_refs: vec!["o:main".into()],
                ..options("main")
            }),
            "bound name",
        ),
        (
            "a bad collection",
            Box::new(|| TreePushOptions {
                collection: Some("no-dot".into()),
                ..options("main")
            }),
            "invalid collection id",
        ),
        (
            "a level out of range",
            Box::new(|| TreePushOptions {
                compression: Compression::Deflate { level: 10 },
                ..options("main")
            }),
            "compression level",
        ),
        (
            "an empty metadata key",
            Box::new(move || TreePushOptions {
                metadata: vec![(String::new(), variant())],
                ..options("main")
            }),
            "empty key",
        ),
        (
            "a binding key",
            Box::new(move || TreePushOptions {
                metadata: vec![("ostree.ref-binding".into(), variant())],
                ..options("main")
            }),
            "which the server writes",
        ),
    ];
    for (tag, opts, part) in cases {
        let e = refused_with_no_start(tag, ADDRESS, &root, opts);
        invalid_input(tag, e, part);
    }

    // Before it sends a request, the push refuses HTTP options with no mirror
    // or with two mirrors. The fetcher refuses a token to an `http://`
    // address without the cleartext permission.
    for mirrors in [
        Vec::new(),
        vec!["http://a/".to_owned(), "http://b/".to_owned()],
    ] {
        let endpoint = Endpoint::Http(ostrya_fetch::FetcherOptions {
            mirrors,
            ..Default::default()
        });
        let result = ostrya_rt::block_on(push_tree(endpoint, &root, options("main")));
        invalid_input("mirrors", result.unwrap_err(), "exactly one mirror");
    }
    let mut http = ostrya_fetch::FetcherOptions::new("http://host/repo");
    http.bearer_token = Some(ostrya_fetch::BearerToken {
        token: "token".into(),
    });
    let result = ostrya_rt::block_on(push_tree(Endpoint::Http(http), &root, options("main")));
    match result {
        Err(Error::Fetch(e)) => assert!(e.to_string().contains("cleartext"), "{e}"),
        other => panic!("cleartext: {other:?}"),
    }
}