use std::env::*;
use std::ffi::OsString;
use std::iter::once;
use crate::arbitrary::*;
use crate::strategy::statics::static_map;
use crate::strategy::*;
lazy_just!(
Args, args;
ArgsOs, args_os;
Vars, vars;
VarsOs, vars_os;
JoinPathsError, jpe
);
#[cfg(not(target_os = "windows"))]
fn jpe() -> JoinPathsError {
join_paths(once(":")).unwrap_err()
}
#[cfg(target_os = "windows")]
fn jpe() -> JoinPathsError {
join_paths(once("\"")).unwrap_err()
}
#[cfg(any(target_os = "windows", test))]
fn make_utf16_invalid(buf: &mut [u16], p: usize) {
assert!(buf.len() > 0);
let gen_trail = 0 == p || 0xd800 != (buf[p - 1] & 0xfc00);
let gen_lead = p == buf.len() - 1 || 0xdc00 != (buf[p + 1] & 0xfc00);
let (force_bits_mask, force_bits_value) = if gen_trail {
if gen_lead {
(0xf800, 0xd800)
} else {
(0xfc00, 0xdc00)
}
} else {
(0xfc00, 0xd800)
};
debug_assert_eq!(0, (force_bits_value & !force_bits_mask));
buf[p] = (buf[p] & !force_bits_mask) | force_bits_value;
}
#[cfg(not(target_arch = "wasm32"))]
mod var_error {
use super::*;
#[cfg(target_os = "windows")]
fn osstring_invalid_string() -> impl Strategy<Value = OsString> {
use std::os::windows::ffi::OsStringExt;
let size = 1..::std::u16::MAX as usize;
let vec_gen = crate::collection::vec(..::std::u16::MAX, size.clone());
(size, vec_gen).prop_map(|(p, mut sbuf)| {
let p = ::std::cmp::min(p, sbuf.len() - 1);
make_utf16_invalid(&mut sbuf, p);
OsString::from_wide(sbuf.as_slice())
.into_string()
.unwrap_err()
})
}
#[cfg(not(target_os = "windows"))]
fn osstring_invalid_string() -> impl Strategy<Value = OsString> {
use crate::arbitrary::_std::string::not_utf8_bytes;
use std::os::unix::ffi::OsStringExt;
static_map(not_utf8_bytes(true), OsString::from_vec)
}
arbitrary!(VarError,
TupleUnion<(
WA<Just<Self>>,
WA<SFnPtrMap<BoxedStrategy<OsString>, Self>>
)>;
prop_oneof![
Just(VarError::NotPresent),
static_map(osstring_invalid_string().boxed(), VarError::NotUnicode)
]
);
}
#[cfg(test)]
mod test {
use super::*;
use crate::num;
use crate::test_runner::Config;
no_panic_test!(
args => Args,
args_os => ArgsOs,
vars => Vars,
vars_os => VarsOs,
join_paths_error => JoinPathsError,
var_error => VarError
);
proptest! {
#![proptest_config(Config {
cases: 65536,
.. Config::default()
})]
#[test]
fn make_utf16_invalid_doesnt_panic(
mut buf in [num::u16::ANY; 3],
p in 0usize..3
) {
make_utf16_invalid(&mut buf, p);
}
}
}