blob: 4c74ecf627330c781efcd0d0ddd601abc626da01 [file] [log] [blame]
Joel Galenson981b40a2021-08-09 10:34:35 -07001use crate::*;
2use nix::{
3 errno::Errno,
4 mount::{MntFlags, Nmount, unmount}
5};
6use std::{
7 ffi::CString,
8 fs::File,
9 path::Path
10};
11use tempfile::tempdir;
12
13#[test]
14fn ok() {
15 require_mount!("nullfs");
16
17 let mountpoint = tempdir().unwrap();
18 let target = tempdir().unwrap();
19 let _sentry = File::create(target.path().join("sentry")).unwrap();
20
21 let fstype = CString::new("fstype").unwrap();
22 let nullfs = CString::new("nullfs").unwrap();
23 Nmount::new()
24 .str_opt(&fstype, &nullfs)
25 .str_opt_owned("fspath", mountpoint.path().to_str().unwrap())
26 .str_opt_owned("target", target.path().to_str().unwrap())
27 .nmount(MntFlags::empty()).unwrap();
28
29 // Now check that the sentry is visible through the mountpoint
30 let exists = Path::exists(&mountpoint.path().join("sentry"));
31
32 // Cleanup the mountpoint before asserting
33 unmount(mountpoint.path(), MntFlags::empty()).unwrap();
34
35 assert!(exists);
36}
37
38#[test]
39fn bad_fstype() {
40 let mountpoint = tempdir().unwrap();
41 let target = tempdir().unwrap();
42 let _sentry = File::create(target.path().join("sentry")).unwrap();
43
44 let e = Nmount::new()
45 .str_opt_owned("fspath", mountpoint.path().to_str().unwrap())
46 .str_opt_owned("target", target.path().to_str().unwrap())
47 .nmount(MntFlags::empty()).unwrap_err();
48
49 assert_eq!(e.error(), Errno::EINVAL);
50 assert_eq!(e.errmsg(), Some("Invalid fstype"));
51}