diff --git a/Cargo.toml b/Cargo.toml index 9d981e9..ac031f8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,3 +17,6 @@ libc = "0.2" [target.'cfg(target_os = "windows")'.dependencies] winapi = { version = "0.3", features = ["libloaderapi", "processthreadsapi", "sysinfoapi", "winbase", "winver"] } + +[dev-dependencies] +regex = "1" diff --git a/src/windows.rs b/src/windows.rs index 182272b..cc7f423 100644 --- a/src/windows.rs +++ b/src/windows.rs @@ -424,6 +424,15 @@ fn test_sysname() { assert_eq!(info.sysname(), expected); } +#[test] +#[allow(non_snake_case)] +fn test_nodename_no_trailing_NUL() { + let info = PlatformInfo::new().unwrap(); + let nodename = info.nodename(); + let trimmed = nodename.trim().trim_end_matches(|c| c == '\0'); + assert_eq!(nodename, trimmed); +} + #[test] fn test_machine() { let is_wow64 = is_wow64(); diff --git a/tests/integration_test.rs b/tests/integration_test.rs index 6b97238..f73bb6a 100644 --- a/tests/integration_test.rs +++ b/tests/integration_test.rs @@ -1,3 +1,5 @@ +use regex; + use platform_info::*; #[test] @@ -7,12 +9,19 @@ fn platform() -> Result<(), String> { Err(error) => panic!("{}", error), }; - println!("sysname = '{}'", uname.sysname()); - println!("nodename = '{}'", uname.nodename()); - println!("release = '{}'", uname.release()); - println!("version = '{}'", uname.version()); - println!("machine = '{}'", uname.machine()); - println!("osname = '{}'", uname.osname()); + let sysname = uname.sysname(); + let nodename = uname.nodename(); + let release = uname.release(); + let version = uname.version(); + let machine = uname.machine(); + let osname = uname.osname(); + + println!("sysname = [{}]'{}'", sysname.len(), sysname); + println!("nodename = [{}]'{}'", nodename.len(), nodename); + println!("release = [{}]'{}'", release.len(), release); + println!("version = [{}]'{}'", version.len(), version); + println!("machine = [{}]'{}'", machine.len(), machine); + println!("osname = [{}]'{}'", osname.len(), osname); assert!(!uname.sysname().is_empty()); assert!(!uname.nodename().is_empty()); @@ -23,3 +32,27 @@ fn platform() -> Result<(), String> { Ok(()) } + +#[test] +fn platform_no_invisible_contents() -> Result<(), String> { + let uname = match PlatformInfo::new() { + Ok(info) => info, + Err(error) => panic!("{}", error), + }; + + let sysname = uname.sysname(); + let nodename = uname.nodename(); + let release = uname.release(); + let version = uname.version(); + let machine = uname.machine(); + let osname = uname.osname(); + + let s = format!("sysname='{sysname}';nodename='{nodename}';release='{release}';version='{version}';machine='{machine}';osname={osname}"); + println!("s = [{}]\"{}\"", s.len(), s); + + // let re = regex::Regex::new("[^[[:print:]]]").unwrap(); // matches invisible (and emojis) + let re = regex::Regex::new("[^[[:print:]]\\p{Other_Symbol}]").unwrap(); // matches invisible only (not emojis) + assert_eq!(re.find(&s), None); + + Ok(()) +}