Skip to content

Commit

Permalink
add test for setfsuid
Browse files Browse the repository at this point in the history
  • Loading branch information
Marco Conte committed Jan 28, 2020
1 parent d4f242f commit 48c3a3e
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions src/unistd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2751,3 +2751,42 @@ impl Group {
})
}
}


#[cfg(test)]
mod tests {
use super::*;
use std::{fs, thread, io};

/// Tests setting the filesystem UID with `setfsuid`.
#[cfg(any(target_os = "linux", target_os = "android"))]
#[test]
fn test_setfsuid() {
use std::os::unix::fs::PermissionsExt;

// get the UID of the "nobody" user
let nobody = User::from_name("nobody").unwrap().unwrap();

// create a temporary file with permissions '-rw-r-----'
let file = tempfile::NamedTempFile::new().unwrap();
let temp_path = file.into_temp_path();
let temp_path_2 = (&temp_path).to_path_buf();
let mut permissions = fs::metadata(&temp_path).unwrap().permissions();
permissions.set_mode(640);

// spawn a new thread where to test setfsuid
thread::spawn(move || {
// set filesystem UID
let _ = setfsuid(nobody.uid);
// try to open the temporaty file should fail with EACCES
let res = fs::File::open(&temp_path);
assert!(res.is_err());
assert_eq!(res.err().unwrap().kind(), io::ErrorKind::PermissionDenied);
})
.join()
.unwrap();

// open the temporary file with the current thread filesystem UID
fs::File::open(temp_path_2).unwrap();
}
}

0 comments on commit 48c3a3e

Please sign in to comment.