Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
43550c3
X-Smart-Branch-Parent: main
JoukoVirtanen Feb 11, 2026
742009a
Added unit tests and parameterized integration tests
JoukoVirtanen Feb 5, 2026
1c81096
Reduced duplication of helper functions
JoukoVirtanen Feb 6, 2026
dce40f1
cargo fmt --all
JoukoVirtanen Feb 6, 2026
928f41d
Reverted changes to test_multiple
JoukoVirtanen Feb 6, 2026
92bc1e3
Added integration test case with invalid utf-8
JoukoVirtanen Feb 6, 2026
bf7cb83
Fixed how file paths are joined when there are bytes. Invalid utf-8 i…
JoukoVirtanen Feb 8, 2026
4b4bd97
Added tests for test_path_chown and test_path_unlink
JoukoVirtanen Feb 8, 2026
bf790c0
Added invalid utf-8 test case to test_path_chown
JoukoVirtanen Feb 8, 2026
6a96ed6
Apply suggestions from code review
JoukoVirtanen Feb 9, 2026
d1a1cf8
Created helper functions join_path_with_filename and path_to_string
JoukoVirtanen Feb 9, 2026
f613f1c
Using { PATH_MAX as usize } instead of other values for the template
JoukoVirtanen Feb 9, 2026
cfbcae4
Using regex instead of checking for contains
JoukoVirtanen Feb 10, 2026
ca13e31
Removed default_process_t
JoukoVirtanen Feb 10, 2026
58104a3
Refactored unit tests in fact/src/event/process.rs
JoukoVirtanen Feb 10, 2026
b989782
Test case names are consistent and uppercase
JoukoVirtanen Feb 10, 2026
17f0d46
cargo fmt --check
JoukoVirtanen Feb 10, 2026
52f1836
Apply suggestion from @Molter73
JoukoVirtanen Feb 11, 2026
4e15bc7
Apply suggestion from @Molter73
JoukoVirtanen Feb 11, 2026
cffc869
Apply suggestion from @Molter73
JoukoVirtanen Feb 11, 2026
1ef880f
Apply suggestion from @Molter73
JoukoVirtanen Feb 11, 2026
361abb8
Minor follow on changes for suggested changes
JoukoVirtanen Feb 11, 2026
e7c5c1b
Minor suggested change to tests/test_path_chown.py
JoukoVirtanen Feb 11, 2026
dc43848
Renamed test_file to fut in tests/test_path_unlink.py
JoukoVirtanen Feb 11, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ uuid = { version = "1.17.0", features = ["v4"] }
bindgen = "0.72.0"
tempfile = { version = "3.20.0", default-features = false }
yaml-rust2 = "0.11.0"
regex = "1.11.1"

[profile.release]
debug = "line-tables-only"
1 change: 1 addition & 0 deletions fact/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ fact-ebpf = { path = "../fact-ebpf" }

[dev-dependencies]
tempfile = { workspace = true }
regex = { workspace = true }

[build-dependencies]
anyhow = { workspace = true }
Expand Down
190 changes: 190 additions & 0 deletions fact/src/event/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,3 +358,193 @@ impl From<ChownFileData> for fact_api::FileOwnershipChange {
}
}
}

#[cfg(test)]
mod test_utils {
use std::os::raw::c_char;

/// Helper function to convert raw bytes to a c_char array for testing
pub fn bytes_to_c_char_array<const N: usize>(bytes: &[u8]) -> [c_char; N] {
let mut array = [0 as c_char; N];
let len = bytes.len().min(N - 1);
for (i, &byte) in bytes.iter().take(len).enumerate() {
array[i] = byte as c_char;
}
array
}

/// Helper function to convert a Rust string to a c_char array for testing
pub fn string_to_c_char_array<const N: usize>(s: &str) -> [c_char; N] {
bytes_to_c_char_array(s.as_bytes())
}
}

#[cfg(test)]
mod tests {
use super::test_utils::*;
use super::*;

#[test]
fn slice_to_string_valid_utf8() {
let tests = [
("hello", "ASCII"),
("café", "French"),
("файл", "Cyrillic"),
("测试文件", "Chinese"),
("test🚀file", "Emoji"),
("test-файл-测试-🐛.txt", "Mixed Unicode"),
("ملف", "Arabic"),
("קובץ", "Hebrew"),
("ファイル", "Japanese"),
];

for (input, description) in tests {
let arr = string_to_c_char_array::<{ PATH_MAX as usize }>(input);
assert_eq!(
slice_to_string(&arr).unwrap(),
input,
"Failed for {}",
description
);
}
}

#[test]
fn slice_to_string_invalid_utf8() {
let tests: &[(&[u8], &str)] = &[
(&[0xFF, 0xFE, 0xFD], "Invalid continuation bytes"),
(b"test\xE2", "Truncated multi-byte sequence"),
(&[0xC0, 0x80], "Overlong encoding"),
(b"hello\x80world", "Invalid start byte"),
(&[0x80], "Lone continuation byte"),
(b"test\xFF\xFE", "Mixed valid and invalid bytes"),
];

for (bytes, description) in tests {
let arr = bytes_to_c_char_array::<{ PATH_MAX as usize }>(bytes);
assert!(
slice_to_string(&arr).is_err(),
"Should fail for {}",
description
);
}
}

#[test]
fn sanitize_d_path_valid_utf8() {
let tests = [
("/etc/test", "/etc/test", "ASCII"),
("/tmp/файл.txt", "/tmp/файл.txt", "Cyrillic"),
(
"/home/user/测试文件.log",
"/home/user/测试文件.log",
"Chinese",
),
("/data/🚀rocket.dat", "/data/🚀rocket.dat", "Emoji"),
(
"/var/log/app-данные-数据-🐛.log",
"/var/log/app-данные-数据-🐛.log",
"Mixed Unicode",
),
("/home/ملف.txt", "/home/ملف.txt", "Arabic"),
("/opt/ファイル.conf", "/opt/ファイル.conf", "Japanese"),
];

for (input, expected, description) in tests {
let arr = string_to_c_char_array::<{ PATH_MAX as usize }>(input);
assert_eq!(
sanitize_d_path(&arr),
PathBuf::from(expected),
"Failed for {}",
description
);
}
}

#[test]
fn sanitize_d_path_deleted_suffix() {
let tests = [
(
"/tmp/test.txt (deleted)",
"/tmp/test.txt",
"ASCII with deleted suffix",
),
(
"/tmp/файл.txt (deleted)",
"/tmp/файл.txt",
"Unicode with deleted suffix",
),
("/etc/config.yaml", "/etc/config.yaml", "No deleted suffix"),
(
"/var/log/app/debug.log (deleted)",
"/var/log/app/debug.log",
"Nested path with deleted suffix",
),
];

for (input, expected, description) in tests {
let arr = string_to_c_char_array::<{ PATH_MAX as usize }>(input);
assert_eq!(
sanitize_d_path(&arr),
PathBuf::from(expected),
"Failed for {}",
description
);
}
}

#[test]
fn sanitize_d_path_invalid_utf8() {
use regex::Regex;

let tests: &[(&[u8], &str, &str)] = &[
(
b"/tmp/\xFF\xFE.txt",
r"^/tmp/\u{FFFD}+\.txt$",
"Invalid continuation bytes",
),
(
b"/var/test\xE2\x80",
r"^/var/test\u{FFFD}+$",
"Truncated multi-byte sequence",
),
(
b"/home/file\x80.log",
r"^/home/file\u{FFFD}\.log$",
"Invalid start byte",
),
(
b"/tmp/\xD1\x84\xFF\xD0\xBB.txt",
r"^/tmp/ф\u{FFFD}л\.txt$",
"Mixed valid and invalid UTF-8",
),
];

for (bytes, pattern, description) in tests {
let arr = bytes_to_c_char_array::<{ PATH_MAX as usize }>(bytes);
let result = sanitize_d_path(&arr);
let result_str = result.to_string_lossy();

let re = Regex::new(pattern).expect("Invalid regex pattern");
assert!(
re.is_match(&result_str),
"Failed for {}: expected pattern '{}', got '{}'",
description,
pattern,
result_str
);
}
}

#[test]
fn sanitize_d_path_invalid_utf8_with_deleted_suffix() {
let invalid_with_deleted =
bytes_to_c_char_array::<{ PATH_MAX as usize }>(b"/tmp/\xFF\xFE (deleted)");
let result = sanitize_d_path(&invalid_with_deleted);
let result_str = result.to_string_lossy();

assert!(result_str.contains("/tmp/"));
assert!(!result_str.ends_with(" (deleted)"));
assert!(result_str.contains('\u{FFFD}'));
}
}
Loading