Skip to content

Commit

Permalink
tests ~ dis-allow invisible chars in return values
Browse files Browse the repository at this point in the history
  • Loading branch information
rivy committed Jan 15, 2023
1 parent e296871 commit 0f98fab
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 6 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
9 changes: 9 additions & 0 deletions src/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
45 changes: 39 additions & 6 deletions tests/integration_test.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use regex;

use platform_info::*;

#[test]
Expand All @@ -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());
Expand All @@ -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(())
}

0 comments on commit 0f98fab

Please sign in to comment.