#[must_use]
pub fn sanitize_path_component(s: &str) -> String {
let s = s.replace("..", "__");
s.replace(['/', '\\', ':', '*', '?', '"', '<', '>', '|'], "_")
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_sanitize_normal_name() {
assert_eq!(sanitize_path_component("tokio"), "tokio");
assert_eq!(sanitize_path_component("my-crate"), "my-crate");
assert_eq!(sanitize_path_component("my.crate"), "my.crate");
}
#[test]
fn test_sanitize_path_traversal() {
assert_eq!(sanitize_path_component(".."), "__");
assert_eq!(sanitize_path_component("../etc"), "___etc");
assert_eq!(sanitize_path_component("../../etc/passwd"), "______etc_passwd");
}
#[test]
fn test_sanitize_dangerous_chars() {
assert_eq!(sanitize_path_component("foo/bar"), "foo_bar");
assert_eq!(sanitize_path_component("foo\\bar"), "foo_bar");
assert_eq!(sanitize_path_component("foo:bar"), "foo_bar");
assert_eq!(sanitize_path_component("foo*bar"), "foo_bar");
assert_eq!(sanitize_path_component("foo?bar"), "foo_bar");
assert_eq!(sanitize_path_component("foo\"bar"), "foo_bar");
assert_eq!(sanitize_path_component("foo<bar"), "foo_bar");
assert_eq!(sanitize_path_component("foo>bar"), "foo_bar");
assert_eq!(sanitize_path_component("foo|bar"), "foo_bar");
}
#[test]
fn test_sanitize_combined() {
assert_eq!(sanitize_path_component("../dangerous:file?.txt"), "___dangerous_file_.txt");
}
}