Skip to content

Commit 49b2466

Browse files
committed
uucore: selinux: Add tests for symlinks and fifo
Check that we can follow (or not) symlinks to get their SElinux context. Also, check that we can get context from a fifo (we used to hang).
1 parent 8752c0c commit 49b2466

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

src/uucore/src/lib/features/selinux.rs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,65 @@ mod tests {
489489
assert!(result.is_err());
490490
}
491491

492+
#[test]
493+
fn test_get_selinux_context_symlink() {
494+
use std::os::unix::fs::symlink;
495+
use tempfile::tempdir;
496+
497+
if !is_selinux_enabled() {
498+
println!("test skipped: Kernel has no support for SElinux context");
499+
return;
500+
}
501+
502+
let tmp_dir = tempdir().expect("Failed to create temporary directory");
503+
let dir_path = tmp_dir.path();
504+
505+
// Create a normal file
506+
let file_path = dir_path.join("file");
507+
std::fs::File::create(&file_path).expect("Failed to create file");
508+
509+
// Create a symlink to the file
510+
let symlink_path = dir_path.join("symlink");
511+
symlink(&file_path, &symlink_path).expect("Failed to create symlink");
512+
513+
// Set a different context for the file (but not the symlink)
514+
let file_context = String::from("system_u:object_r:user_tmp_t:s0");
515+
set_selinux_security_context(&file_path, Some(&file_context))
516+
.expect("Failed to set security context.");
517+
518+
// Context must be different if we don't follow the link
519+
let file_context = get_selinux_security_context(&file_path, false)
520+
.expect("Failed to get security context.");
521+
let symlink_context = get_selinux_security_context(&symlink_path, false)
522+
.expect("Failed to get security context.");
523+
assert_ne!(file_context.to_string(), symlink_context.to_string());
524+
525+
// Context must be the same if we follow the link
526+
let symlink_follow_context = get_selinux_security_context(&symlink_path, true)
527+
.expect("Failed to get security context.");
528+
assert_eq!(file_context.to_string(), symlink_follow_context.to_string());
529+
}
530+
531+
#[test]
532+
fn test_get_selinux_context_fifo() {
533+
use tempfile::tempdir;
534+
535+
if !is_selinux_enabled() {
536+
println!("test skipped: Kernel has no support for SElinux context");
537+
return;
538+
}
539+
540+
let tmp_dir = tempdir().expect("Failed to create temporary directory");
541+
let dir_path = tmp_dir.path();
542+
543+
// Create a FIFO (pipe)
544+
let fifo_path = dir_path.join("my_fifo");
545+
crate::fs::make_fifo(&fifo_path).expect("Failed to create FIFO");
546+
547+
// Just getting a context is good enough
548+
get_selinux_security_context(&fifo_path, false).expect("Cannot get fifo context");
549+
}
550+
492551
#[test]
493552
fn test_contexts_differ() {
494553
let file1 = NamedTempFile::new().expect("Failed to create first tempfile");

0 commit comments

Comments
 (0)