Skip to content

Commit ef486cf

Browse files
committed
test(env): Add tests for invalid Unicode handling
1 parent 370c939 commit ef486cf

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed

tests/testsuite/env.rs

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -721,3 +721,79 @@ fn test_parse_uint_default() {
721721
let config: TestUint = config.try_deserialize().unwrap();
722722
assert_eq!(config.int_val, 42);
723723
}
724+
725+
#[cfg(any(unix, windows))]
726+
#[cfg(test)]
727+
mod unicode_tests {
728+
use std::ffi::OsString;
729+
730+
use super::*;
731+
732+
fn make_invalid_unicode_os_string() -> OsString {
733+
let string = {
734+
#[cfg(unix)]
735+
{
736+
use std::os::unix::ffi::OsStringExt;
737+
738+
OsString::from_vec(vec![0xff])
739+
}
740+
#[cfg(windows)]
741+
{
742+
use std::os::windows::ffi::OsStringExt;
743+
744+
OsString::from_wide(&[0xd800]) // unpaired high surrogate
745+
}
746+
};
747+
748+
assert!(string.to_str().is_none());
749+
750+
string
751+
}
752+
753+
#[test]
754+
#[should_panic(expected = r#"`Result::unwrap()` on an `Err` value: "\xFF""#)]
755+
fn test_invalid_unicode_key_ignored() {
756+
temp_env::with_vars(
757+
vec![
758+
(make_invalid_unicode_os_string(), Some("abc")),
759+
("A_B_C".into(), Some("abc")),
760+
],
761+
|| {
762+
let vars = Environment::default().collect().unwrap();
763+
764+
assert!(vars.contains_key("a_b_c"));
765+
},
766+
);
767+
}
768+
769+
#[test]
770+
#[should_panic(expected = r#"`Result::unwrap()` on an `Err` value: "\xFF""#)]
771+
fn test_invalid_unicode_value_filtered() {
772+
temp_env::with_vars(
773+
vec![
774+
("invalid_value1", Some(make_invalid_unicode_os_string())),
775+
("valid_value2", Some("valid".into())),
776+
],
777+
|| {
778+
let vars = Environment::with_prefix("valid")
779+
.keep_prefix(true)
780+
.collect()
781+
.unwrap();
782+
783+
assert!(!vars.contains_key("invalid_value1"));
784+
assert!(vars.contains_key("valid_value2"));
785+
},
786+
);
787+
}
788+
789+
#[test]
790+
#[should_panic(expected = r#"`Result::unwrap()` on an `Err` value: "\xFF""#)]
791+
fn test_invalid_unicode_value_not_filtered() {
792+
temp_env::with_vars(
793+
vec![("invalid_value1", Some(make_invalid_unicode_os_string()))],
794+
|| {
795+
Environment::default().collect().unwrap();
796+
},
797+
);
798+
}
799+
}

0 commit comments

Comments
 (0)