summaryrefslogtreecommitdiffstats
path: root/third_party/rust/nix/test/test_nmount.rs
blob: dec806a55fb1a69541b3d246faacaf2821304c40 (plain)
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
use crate::*;
use nix::{
    errno::Errno,
    mount::{unmount, MntFlags, Nmount},
};
use std::{ffi::CString, fs::File, path::Path};
use tempfile::tempdir;

#[test]
fn ok() {
    require_mount!("nullfs");

    let mountpoint = tempdir().unwrap();
    let target = tempdir().unwrap();
    let _sentry = File::create(target.path().join("sentry")).unwrap();

    let fstype = CString::new("fstype").unwrap();
    let nullfs = CString::new("nullfs").unwrap();
    Nmount::new()
        .str_opt(&fstype, &nullfs)
        .str_opt_owned("fspath", mountpoint.path().to_str().unwrap())
        .str_opt_owned("target", target.path().to_str().unwrap())
        .nmount(MntFlags::empty())
        .unwrap();

    // Now check that the sentry is visible through the mountpoint
    let exists = Path::exists(&mountpoint.path().join("sentry"));

    // Cleanup the mountpoint before asserting
    unmount(mountpoint.path(), MntFlags::empty()).unwrap();

    assert!(exists);
}

#[test]
fn bad_fstype() {
    let mountpoint = tempdir().unwrap();
    let target = tempdir().unwrap();
    let _sentry = File::create(target.path().join("sentry")).unwrap();

    let e = Nmount::new()
        .str_opt_owned("fspath", mountpoint.path().to_str().unwrap())
        .str_opt_owned("target", target.path().to_str().unwrap())
        .nmount(MntFlags::empty())
        .unwrap_err();

    assert_eq!(e.error(), Errno::EINVAL);
    assert_eq!(e.errmsg(), Some("Invalid fstype"));
}