Skip to content

Commit 0b0c096

Browse files
committed
lib/permissions: Add unit tests
Signed-off-by: Anderson Toshiyuki Sasaki <[email protected]>
1 parent 93c359c commit 0b0c096

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

keylime/src/permissions.rs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,3 +244,72 @@ pub fn set_mode(path: &Path, mode: u32) -> Result<(), PermissionError> {
244244
}
245245
})
246246
}
247+
248+
#[cfg(test)]
249+
mod test {
250+
use super::*;
251+
use std::{fs::File, io::Write};
252+
253+
#[test]
254+
fn test_get_uid() {
255+
let _uid = get_uid();
256+
}
257+
258+
#[test]
259+
fn test_get_gid() {
260+
let _gid = get_gid();
261+
}
262+
263+
#[test]
264+
fn test_get_euid() {
265+
let _euid = get_euid();
266+
}
267+
268+
#[test]
269+
fn test_chown() {
270+
// Only test chown when running as root
271+
if get_euid() == 0 {
272+
let temp_dir = tempfile::tempdir()
273+
.expect("failed to create temporary directory");
274+
let p = temp_dir.path().join("testfile.txt");
275+
let mut f = File::create(&p).expect("failed to create file");
276+
f.write_all(b"test content\n")
277+
.expect("failed to write to file");
278+
let r = chown("root:root", &p);
279+
assert!(r.is_ok());
280+
}
281+
}
282+
283+
#[test]
284+
fn test_set_mode() {
285+
let temp_dir = tempfile::tempdir()
286+
.expect("failed to create temporary directory");
287+
let p = temp_dir.path().join("testfile.txt");
288+
let mut f = File::create(&p).expect("failed to create file");
289+
f.write_all(b"test content\n")
290+
.expect("failed to write to file");
291+
292+
let r = set_mode(&p, 0o777);
293+
assert!(r.is_ok());
294+
}
295+
296+
#[test]
297+
fn test_try_from_str_for_userids() {
298+
let r = UserIds::try_from("root:root");
299+
assert!(r.is_ok());
300+
let r = UserIds::try_from("invalid:root");
301+
assert!(r.is_err());
302+
let r = UserIds::try_from("root:invalid");
303+
assert!(r.is_err());
304+
let r = UserIds::try_from("invalid");
305+
assert!(r.is_err());
306+
let r = UserIds::try_from("invalid:invalid");
307+
assert!(r.is_err());
308+
let r = UserIds::try_from("");
309+
assert!(r.is_err());
310+
let r = UserIds::try_from(":invalid");
311+
assert!(r.is_err());
312+
let r = UserIds::try_from("invalid:");
313+
assert!(r.is_err());
314+
}
315+
}

0 commit comments

Comments
 (0)