2016-01-22 17:58:56 +00:00
|
|
|
#![cfg(not(windows))]
|
|
|
|
|
2018-08-02 03:13:51 +00:00
|
|
|
use clap::{App, AppSettings, Arg, ErrorKind};
|
2016-01-22 17:58:56 +00:00
|
|
|
use std::ffi::OsString;
|
|
|
|
use std::os::unix::ffi::OsStringExt;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn invalid_utf8_strict_positional() {
|
|
|
|
let m = App::new("bad_utf8")
|
2018-04-21 18:59:19 +00:00
|
|
|
.arg(Arg::from("<arg> 'some arg'"))
|
2016-01-22 17:58:56 +00:00
|
|
|
.setting(AppSettings::StrictUtf8)
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec![OsString::from(""), OsString::from_vec(vec![0xe9])]);
|
2016-01-22 17:58:56 +00:00
|
|
|
assert!(m.is_err());
|
|
|
|
assert_eq!(m.unwrap_err().kind, ErrorKind::InvalidUtf8);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn invalid_utf8_strict_option_short_space() {
|
|
|
|
let m = App::new("bad_utf8")
|
2018-04-21 18:59:19 +00:00
|
|
|
.arg(Arg::from("-a, --arg <arg> 'some arg'"))
|
2016-01-22 17:58:56 +00:00
|
|
|
.setting(AppSettings::StrictUtf8)
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec![
|
2018-01-25 04:05:05 +00:00
|
|
|
OsString::from(""),
|
|
|
|
OsString::from("-a"),
|
|
|
|
OsString::from_vec(vec![0xe9]),
|
|
|
|
]);
|
2016-01-22 17:58:56 +00:00
|
|
|
assert!(m.is_err());
|
|
|
|
assert_eq!(m.unwrap_err().kind, ErrorKind::InvalidUtf8);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn invalid_utf8_strict_option_short_equals() {
|
|
|
|
let m = App::new("bad_utf8")
|
2018-04-21 18:59:19 +00:00
|
|
|
.arg(Arg::from("-a, --arg <arg> 'some arg'"))
|
2016-01-22 17:58:56 +00:00
|
|
|
.setting(AppSettings::StrictUtf8)
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec![
|
2018-01-25 04:05:05 +00:00
|
|
|
OsString::from(""),
|
|
|
|
OsString::from_vec(vec![0x2d, 0x61, 0x3d, 0xe9]),
|
|
|
|
]);
|
2016-01-22 17:58:56 +00:00
|
|
|
assert!(m.is_err());
|
|
|
|
assert_eq!(m.unwrap_err().kind, ErrorKind::InvalidUtf8);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn invalid_utf8_strict_option_short_no_space() {
|
|
|
|
let m = App::new("bad_utf8")
|
2018-04-21 18:59:19 +00:00
|
|
|
.arg(Arg::from("-a, --arg <arg> 'some arg'"))
|
2016-01-22 17:58:56 +00:00
|
|
|
.setting(AppSettings::StrictUtf8)
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec![
|
2018-01-25 04:05:05 +00:00
|
|
|
OsString::from(""),
|
|
|
|
OsString::from_vec(vec![0x2d, 0x61, 0xe9]),
|
|
|
|
]);
|
2016-01-22 17:58:56 +00:00
|
|
|
assert!(m.is_err());
|
|
|
|
assert_eq!(m.unwrap_err().kind, ErrorKind::InvalidUtf8);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn invalid_utf8_strict_option_long_space() {
|
|
|
|
let m = App::new("bad_utf8")
|
2018-04-21 18:59:19 +00:00
|
|
|
.arg(Arg::from("-a, --arg <arg> 'some arg'"))
|
2016-01-22 17:58:56 +00:00
|
|
|
.setting(AppSettings::StrictUtf8)
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec![
|
2018-01-25 04:05:05 +00:00
|
|
|
OsString::from(""),
|
|
|
|
OsString::from("--arg"),
|
|
|
|
OsString::from_vec(vec![0xe9]),
|
|
|
|
]);
|
2016-01-22 17:58:56 +00:00
|
|
|
assert!(m.is_err());
|
|
|
|
assert_eq!(m.unwrap_err().kind, ErrorKind::InvalidUtf8);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn invalid_utf8_strict_option_long_equals() {
|
|
|
|
let m = App::new("bad_utf8")
|
2018-04-21 18:59:19 +00:00
|
|
|
.arg(Arg::from("-a, --arg <arg> 'some arg'"))
|
2016-01-22 17:58:56 +00:00
|
|
|
.setting(AppSettings::StrictUtf8)
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec![
|
2018-01-25 04:05:05 +00:00
|
|
|
OsString::from(""),
|
|
|
|
OsString::from_vec(vec![0x2d, 0x2d, 0x61, 0x72, 0x67, 0x3d, 0xe9]),
|
|
|
|
]);
|
2016-01-22 17:58:56 +00:00
|
|
|
assert!(m.is_err());
|
|
|
|
assert_eq!(m.unwrap_err().kind, ErrorKind::InvalidUtf8);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn invalid_utf8_lossy_positional() {
|
|
|
|
let r = App::new("bad_utf8")
|
2018-04-21 18:59:19 +00:00
|
|
|
.arg(Arg::from("<arg> 'some arg'"))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec![OsString::from(""), OsString::from_vec(vec![0xe9])]);
|
2016-01-22 17:58:56 +00:00
|
|
|
assert!(r.is_ok());
|
|
|
|
let m = r.unwrap();
|
|
|
|
assert!(m.is_present("arg"));
|
2016-01-29 02:51:50 +00:00
|
|
|
assert_eq!(&*m.value_of_lossy("arg").unwrap(), "\u{FFFD}");
|
2016-01-22 17:58:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn invalid_utf8_lossy_option_short_space() {
|
|
|
|
let r = App::new("bad_utf8")
|
2018-04-21 18:59:19 +00:00
|
|
|
.arg(Arg::from("-a, --arg <arg> 'some arg'"))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec![
|
2018-01-25 04:05:05 +00:00
|
|
|
OsString::from(""),
|
|
|
|
OsString::from("-a"),
|
|
|
|
OsString::from_vec(vec![0xe9]),
|
|
|
|
]);
|
2016-01-22 17:58:56 +00:00
|
|
|
assert!(r.is_ok());
|
|
|
|
let m = r.unwrap();
|
|
|
|
assert!(m.is_present("arg"));
|
2016-01-29 02:51:50 +00:00
|
|
|
assert_eq!(&*m.value_of_lossy("arg").unwrap(), "\u{FFFD}");
|
2016-01-22 17:58:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn invalid_utf8_lossy_option_short_equals() {
|
|
|
|
let r = App::new("bad_utf8")
|
2018-04-21 18:59:19 +00:00
|
|
|
.arg(Arg::from("-a, --arg <arg> 'some arg'"))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec![
|
2018-01-25 04:05:05 +00:00
|
|
|
OsString::from(""),
|
|
|
|
OsString::from_vec(vec![0x2d, 0x61, 0x3d, 0xe9]),
|
|
|
|
]);
|
2016-01-22 17:58:56 +00:00
|
|
|
assert!(r.is_ok());
|
|
|
|
let m = r.unwrap();
|
|
|
|
assert!(m.is_present("arg"));
|
2016-01-29 02:51:50 +00:00
|
|
|
assert_eq!(&*m.value_of_lossy("arg").unwrap(), "\u{FFFD}");
|
2016-01-22 17:58:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn invalid_utf8_lossy_option_short_no_space() {
|
|
|
|
let r = App::new("bad_utf8")
|
2018-04-21 18:59:19 +00:00
|
|
|
.arg(Arg::from("-a, --arg <arg> 'some arg'"))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec![
|
2018-01-25 04:05:05 +00:00
|
|
|
OsString::from(""),
|
|
|
|
OsString::from_vec(vec![0x2d, 0x61, 0xe9]),
|
|
|
|
]);
|
2016-01-22 17:58:56 +00:00
|
|
|
assert!(r.is_ok());
|
|
|
|
let m = r.unwrap();
|
|
|
|
assert!(m.is_present("arg"));
|
2016-01-29 02:51:50 +00:00
|
|
|
assert_eq!(&*m.value_of_lossy("arg").unwrap(), "\u{FFFD}");
|
2016-01-22 17:58:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn invalid_utf8_lossy_option_long_space() {
|
|
|
|
let r = App::new("bad_utf8")
|
2018-04-21 18:59:19 +00:00
|
|
|
.arg(Arg::from("-a, --arg <arg> 'some arg'"))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec![
|
2018-01-25 04:05:05 +00:00
|
|
|
OsString::from(""),
|
|
|
|
OsString::from("--arg"),
|
|
|
|
OsString::from_vec(vec![0xe9]),
|
|
|
|
]);
|
2016-01-22 17:58:56 +00:00
|
|
|
assert!(r.is_ok());
|
|
|
|
let m = r.unwrap();
|
|
|
|
assert!(m.is_present("arg"));
|
2016-01-29 02:51:50 +00:00
|
|
|
assert_eq!(&*m.value_of_lossy("arg").unwrap(), "\u{FFFD}");
|
2016-01-22 17:58:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn invalid_utf8_lossy_option_long_equals() {
|
|
|
|
let r = App::new("bad_utf8")
|
2018-04-21 18:59:19 +00:00
|
|
|
.arg(Arg::from("-a, --arg <arg> 'some arg'"))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec![
|
2018-01-25 04:05:05 +00:00
|
|
|
OsString::from(""),
|
|
|
|
OsString::from_vec(vec![0x2d, 0x2d, 0x61, 0x72, 0x67, 0x3d, 0xe9]),
|
|
|
|
]);
|
2016-01-22 17:58:56 +00:00
|
|
|
assert!(r.is_ok());
|
|
|
|
let m = r.unwrap();
|
|
|
|
assert!(m.is_present("arg"));
|
2016-01-29 02:51:50 +00:00
|
|
|
assert_eq!(&*m.value_of_lossy("arg").unwrap(), "\u{FFFD}");
|
2016-01-22 17:58:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn invalid_utf8_positional() {
|
|
|
|
let r = App::new("bad_utf8")
|
2018-04-21 18:59:19 +00:00
|
|
|
.arg(Arg::from("<arg> 'some arg'"))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec![OsString::from(""), OsString::from_vec(vec![0xe9])]);
|
2016-01-22 17:58:56 +00:00
|
|
|
assert!(r.is_ok());
|
|
|
|
let m = r.unwrap();
|
|
|
|
assert!(m.is_present("arg"));
|
2018-01-25 04:05:05 +00:00
|
|
|
assert_eq!(
|
|
|
|
&*m.value_of_os("arg").unwrap(),
|
|
|
|
&*OsString::from_vec(vec![0xe9])
|
|
|
|
);
|
2016-01-22 17:58:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn invalid_utf8_option_short_space() {
|
|
|
|
let r = App::new("bad_utf8")
|
2018-04-21 18:59:19 +00:00
|
|
|
.arg(Arg::from("-a, --arg <arg> 'some arg'"))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec![
|
2018-01-25 04:05:05 +00:00
|
|
|
OsString::from(""),
|
|
|
|
OsString::from("-a"),
|
|
|
|
OsString::from_vec(vec![0xe9]),
|
|
|
|
]);
|
2016-01-22 17:58:56 +00:00
|
|
|
assert!(r.is_ok());
|
|
|
|
let m = r.unwrap();
|
|
|
|
assert!(m.is_present("arg"));
|
2018-01-25 04:05:05 +00:00
|
|
|
assert_eq!(
|
|
|
|
&*m.value_of_os("arg").unwrap(),
|
|
|
|
&*OsString::from_vec(vec![0xe9])
|
|
|
|
);
|
2016-01-22 17:58:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn invalid_utf8_option_short_equals() {
|
|
|
|
let r = App::new("bad_utf8")
|
2018-04-21 18:59:19 +00:00
|
|
|
.arg(Arg::from("-a, --arg <arg> 'some arg'"))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec![
|
2018-01-25 04:05:05 +00:00
|
|
|
OsString::from(""),
|
|
|
|
OsString::from_vec(vec![0x2d, 0x61, 0x3d, 0xe9]),
|
|
|
|
]);
|
2016-01-22 17:58:56 +00:00
|
|
|
assert!(r.is_ok());
|
|
|
|
let m = r.unwrap();
|
|
|
|
assert!(m.is_present("arg"));
|
2018-01-25 04:05:05 +00:00
|
|
|
assert_eq!(
|
|
|
|
&*m.value_of_os("arg").unwrap(),
|
|
|
|
&*OsString::from_vec(vec![0xe9])
|
|
|
|
);
|
2016-01-22 17:58:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn invalid_utf8_option_short_no_space() {
|
|
|
|
let r = App::new("bad_utf8")
|
2018-04-21 18:59:19 +00:00
|
|
|
.arg(Arg::from("-a, --arg <arg> 'some arg'"))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec![
|
2018-01-25 04:05:05 +00:00
|
|
|
OsString::from(""),
|
|
|
|
OsString::from_vec(vec![0x2d, 0x61, 0xe9]),
|
|
|
|
]);
|
2016-01-22 17:58:56 +00:00
|
|
|
assert!(r.is_ok());
|
|
|
|
let m = r.unwrap();
|
|
|
|
assert!(m.is_present("arg"));
|
2018-01-25 04:05:05 +00:00
|
|
|
assert_eq!(
|
|
|
|
&*m.value_of_os("arg").unwrap(),
|
|
|
|
&*OsString::from_vec(vec![0xe9])
|
|
|
|
);
|
2016-01-22 17:58:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn invalid_utf8_option_long_space() {
|
|
|
|
let r = App::new("bad_utf8")
|
2018-04-21 18:59:19 +00:00
|
|
|
.arg(Arg::from("-a, --arg <arg> 'some arg'"))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec![
|
2018-01-25 04:05:05 +00:00
|
|
|
OsString::from(""),
|
|
|
|
OsString::from("--arg"),
|
|
|
|
OsString::from_vec(vec![0xe9]),
|
|
|
|
]);
|
2016-01-22 17:58:56 +00:00
|
|
|
assert!(r.is_ok());
|
|
|
|
let m = r.unwrap();
|
|
|
|
assert!(m.is_present("arg"));
|
2018-01-25 04:05:05 +00:00
|
|
|
assert_eq!(
|
|
|
|
&*m.value_of_os("arg").unwrap(),
|
|
|
|
&*OsString::from_vec(vec![0xe9])
|
|
|
|
);
|
2016-01-22 17:58:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn invalid_utf8_option_long_equals() {
|
|
|
|
let r = App::new("bad_utf8")
|
2018-04-21 18:59:19 +00:00
|
|
|
.arg(Arg::from("-a, --arg <arg> 'some arg'"))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec![
|
2018-01-25 04:05:05 +00:00
|
|
|
OsString::from(""),
|
|
|
|
OsString::from_vec(vec![0x2d, 0x2d, 0x61, 0x72, 0x67, 0x3d, 0xe9]),
|
|
|
|
]);
|
2016-01-22 17:58:56 +00:00
|
|
|
assert!(r.is_ok());
|
|
|
|
let m = r.unwrap();
|
|
|
|
assert!(m.is_present("arg"));
|
2018-01-25 04:05:05 +00:00
|
|
|
assert_eq!(
|
|
|
|
&*m.value_of_os("arg").unwrap(),
|
|
|
|
&*OsString::from_vec(vec![0xe9])
|
|
|
|
);
|
2016-01-22 17:58:56 +00:00
|
|
|
}
|