mirror of
https://github.com/clap-rs/clap
synced 2024-12-13 22:32:33 +00:00
aeaf01e3e7
Before, validating UTF-8 was all-or-nothing and would cause a `panic` if someone used the right API with non-UTF-8 input. Now, all arguments are validated for UTF-8, unless opted-out. This ensures a non-panicing path forward at the cost of people using the builder API that previously did `value_of_os` need to now set this flag. Fixes #751
226 lines
5.4 KiB
Rust
226 lines
5.4 KiB
Rust
#![cfg(not(windows))]
|
|
|
|
use clap::{Clap, ErrorKind};
|
|
use std::ffi::OsString;
|
|
use std::os::unix::ffi::OsStringExt;
|
|
|
|
#[derive(Clap, Debug, PartialEq, Eq)]
|
|
struct Positional {
|
|
arg: String,
|
|
}
|
|
|
|
#[derive(Clap, Debug, PartialEq, Eq)]
|
|
struct Named {
|
|
#[clap(short, long)]
|
|
arg: String,
|
|
}
|
|
|
|
#[test]
|
|
fn invalid_utf8_strict_positional() {
|
|
let m = Positional::try_parse_from(vec![OsString::from(""), OsString::from_vec(vec![0xe9])]);
|
|
assert!(m.is_err());
|
|
assert_eq!(m.unwrap_err().kind, ErrorKind::InvalidUtf8);
|
|
}
|
|
|
|
#[test]
|
|
fn invalid_utf8_strict_option_short_space() {
|
|
let m = Named::try_parse_from(vec![
|
|
OsString::from(""),
|
|
OsString::from("-a"),
|
|
OsString::from_vec(vec![0xe9]),
|
|
]);
|
|
assert!(m.is_err());
|
|
assert_eq!(m.unwrap_err().kind, ErrorKind::InvalidUtf8);
|
|
}
|
|
|
|
#[test]
|
|
fn invalid_utf8_strict_option_short_equals() {
|
|
let m = Named::try_parse_from(vec![
|
|
OsString::from(""),
|
|
OsString::from_vec(vec![0x2d, 0x61, 0x3d, 0xe9]),
|
|
]);
|
|
assert!(m.is_err());
|
|
assert_eq!(m.unwrap_err().kind, ErrorKind::InvalidUtf8);
|
|
}
|
|
|
|
#[test]
|
|
fn invalid_utf8_strict_option_short_no_space() {
|
|
let m = Named::try_parse_from(vec![
|
|
OsString::from(""),
|
|
OsString::from_vec(vec![0x2d, 0x61, 0xe9]),
|
|
]);
|
|
assert!(m.is_err());
|
|
assert_eq!(m.unwrap_err().kind, ErrorKind::InvalidUtf8);
|
|
}
|
|
|
|
#[test]
|
|
fn invalid_utf8_strict_option_long_space() {
|
|
let m = Named::try_parse_from(vec![
|
|
OsString::from(""),
|
|
OsString::from("--arg"),
|
|
OsString::from_vec(vec![0xe9]),
|
|
]);
|
|
assert!(m.is_err());
|
|
assert_eq!(m.unwrap_err().kind, ErrorKind::InvalidUtf8);
|
|
}
|
|
|
|
#[test]
|
|
fn invalid_utf8_strict_option_long_equals() {
|
|
let m = Named::try_parse_from(vec![
|
|
OsString::from(""),
|
|
OsString::from_vec(vec![0x2d, 0x2d, 0x61, 0x72, 0x67, 0x3d, 0xe9]),
|
|
]);
|
|
assert!(m.is_err());
|
|
assert_eq!(m.unwrap_err().kind, ErrorKind::InvalidUtf8);
|
|
}
|
|
|
|
#[derive(Clap, Debug, PartialEq, Eq)]
|
|
struct PositionalOs {
|
|
#[clap(parse(from_os_str))]
|
|
arg: OsString,
|
|
}
|
|
|
|
#[derive(Clap, Debug, PartialEq, Eq)]
|
|
struct NamedOs {
|
|
#[clap(short, long, parse(from_os_str))]
|
|
arg: OsString,
|
|
}
|
|
|
|
#[test]
|
|
fn invalid_utf8_positional() {
|
|
let r = PositionalOs::try_parse_from(vec![OsString::from(""), OsString::from_vec(vec![0xe9])]);
|
|
assert_eq!(
|
|
r.unwrap(),
|
|
PositionalOs {
|
|
arg: OsString::from_vec(vec![0xe9])
|
|
}
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn invalid_utf8_option_short_space() {
|
|
let r = NamedOs::try_parse_from(vec![
|
|
OsString::from(""),
|
|
OsString::from("-a"),
|
|
OsString::from_vec(vec![0xe9]),
|
|
]);
|
|
assert_eq!(
|
|
r.unwrap(),
|
|
NamedOs {
|
|
arg: OsString::from_vec(vec![0xe9])
|
|
}
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn invalid_utf8_option_short_equals() {
|
|
let r = NamedOs::try_parse_from(vec![
|
|
OsString::from(""),
|
|
OsString::from_vec(vec![0x2d, 0x61, 0x3d, 0xe9]),
|
|
]);
|
|
assert_eq!(
|
|
r.unwrap(),
|
|
NamedOs {
|
|
arg: OsString::from_vec(vec![0xe9])
|
|
}
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn invalid_utf8_option_short_no_space() {
|
|
let r = NamedOs::try_parse_from(vec![
|
|
OsString::from(""),
|
|
OsString::from_vec(vec![0x2d, 0x61, 0xe9]),
|
|
]);
|
|
assert_eq!(
|
|
r.unwrap(),
|
|
NamedOs {
|
|
arg: OsString::from_vec(vec![0xe9])
|
|
}
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn invalid_utf8_option_long_space() {
|
|
let r = NamedOs::try_parse_from(vec![
|
|
OsString::from(""),
|
|
OsString::from("--arg"),
|
|
OsString::from_vec(vec![0xe9]),
|
|
]);
|
|
assert_eq!(
|
|
r.unwrap(),
|
|
NamedOs {
|
|
arg: OsString::from_vec(vec![0xe9])
|
|
}
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn invalid_utf8_option_long_equals() {
|
|
let r = NamedOs::try_parse_from(vec![
|
|
OsString::from(""),
|
|
OsString::from_vec(vec![0x2d, 0x2d, 0x61, 0x72, 0x67, 0x3d, 0xe9]),
|
|
]);
|
|
assert_eq!(
|
|
r.unwrap(),
|
|
NamedOs {
|
|
arg: OsString::from_vec(vec![0xe9])
|
|
}
|
|
);
|
|
}
|
|
|
|
#[derive(Debug, PartialEq, Clap)]
|
|
enum External {
|
|
#[clap(external_subcommand)]
|
|
Other(Vec<String>),
|
|
}
|
|
|
|
#[test]
|
|
fn refuse_invalid_utf8_subcommand_with_allow_external_subcommands() {
|
|
let m = External::try_parse_from(vec![
|
|
OsString::from(""),
|
|
OsString::from_vec(vec![0xe9]),
|
|
OsString::from("normal"),
|
|
]);
|
|
assert!(m.is_err());
|
|
assert_eq!(m.unwrap_err().kind, ErrorKind::InvalidUtf8);
|
|
}
|
|
|
|
#[test]
|
|
fn refuse_invalid_utf8_subcommand_args_with_allow_external_subcommands() {
|
|
let m = External::try_parse_from(vec![
|
|
OsString::from(""),
|
|
OsString::from("subcommand"),
|
|
OsString::from("normal"),
|
|
OsString::from_vec(vec![0xe9]),
|
|
OsString::from("--another_normal"),
|
|
]);
|
|
assert!(m.is_err());
|
|
assert_eq!(m.unwrap_err().kind, ErrorKind::InvalidUtf8);
|
|
}
|
|
|
|
#[derive(Debug, PartialEq, Clap)]
|
|
enum ExternalOs {
|
|
#[clap(external_subcommand)]
|
|
Other(Vec<OsString>),
|
|
}
|
|
|
|
#[test]
|
|
fn allow_invalid_utf8_subcommand_args_with_allow_external_subcommands() {
|
|
let m = ExternalOs::try_parse_from(vec![
|
|
OsString::from(""),
|
|
OsString::from("subcommand"),
|
|
OsString::from("normal"),
|
|
OsString::from_vec(vec![0xe9]),
|
|
OsString::from("--another_normal"),
|
|
]);
|
|
assert_eq!(
|
|
m.unwrap(),
|
|
ExternalOs::Other(vec![
|
|
OsString::from("subcommand"),
|
|
OsString::from("normal"),
|
|
OsString::from_vec(vec![0xe9]),
|
|
OsString::from("--another_normal"),
|
|
])
|
|
);
|
|
}
|