diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:02:58 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:02:58 +0000 |
commit | 698f8c2f01ea549d77d7dc3338a12e04c11057b9 (patch) | |
tree | 173a775858bd501c378080a10dca74132f05bc50 /vendor/rustix/tests/path/arg.rs | |
parent | Initial commit. (diff) | |
download | rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.tar.xz rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.zip |
Adding upstream version 1.64.0+dfsg1.upstream/1.64.0+dfsg1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/rustix/tests/path/arg.rs')
-rw-r--r-- | vendor/rustix/tests/path/arg.rs | 167 |
1 files changed, 167 insertions, 0 deletions
diff --git a/vendor/rustix/tests/path/arg.rs b/vendor/rustix/tests/path/arg.rs new file mode 100644 index 000000000..0eb511be1 --- /dev/null +++ b/vendor/rustix/tests/path/arg.rs @@ -0,0 +1,167 @@ +use rustix::ffi::{CStr, CString}; +use rustix::io; +use rustix::path::Arg; +#[cfg(feature = "itoa")] +use rustix::path::DecInt; +use std::borrow::Cow; +use std::ffi::{OsStr, OsString}; +use std::path::{Component, Components, Iter, Path, PathBuf}; + +#[test] +fn test_arg() { + use rustix::cstr; + use std::borrow::Borrow; + + let t: &str = "hello"; + assert_eq!("hello", t.as_str().unwrap()); + assert_eq!("hello".to_owned(), Arg::to_string_lossy(&t)); + assert_eq!(cstr!("hello"), Borrow::borrow(&t.as_cow_c_str().unwrap())); + assert_eq!(cstr!("hello"), Borrow::borrow(&t.into_c_str().unwrap())); + + let t: String = "hello".to_owned(); + assert_eq!("hello", Arg::as_str(&t).unwrap()); + assert_eq!("hello".to_owned(), Arg::to_string_lossy(&t)); + assert_eq!(cstr!("hello"), Borrow::borrow(&t.as_cow_c_str().unwrap())); + assert_eq!(cstr!("hello"), Borrow::borrow(&t.into_c_str().unwrap())); + + let t: &OsStr = OsStr::new("hello"); + assert_eq!("hello", t.as_str().unwrap()); + assert_eq!("hello".to_owned(), Arg::to_string_lossy(&t)); + assert_eq!(cstr!("hello"), Borrow::borrow(&t.as_cow_c_str().unwrap())); + assert_eq!(cstr!("hello"), Borrow::borrow(&t.into_c_str().unwrap())); + + let t: OsString = OsString::from("hello".to_owned()); + assert_eq!("hello", t.as_str().unwrap()); + assert_eq!("hello".to_owned(), Arg::to_string_lossy(&t)); + assert_eq!(cstr!("hello"), Borrow::borrow(&t.as_cow_c_str().unwrap())); + assert_eq!(cstr!("hello"), Borrow::borrow(&t.into_c_str().unwrap())); + + let t: &Path = Path::new("hello"); + assert_eq!("hello", t.as_str().unwrap()); + assert_eq!("hello".to_owned(), Arg::to_string_lossy(&t)); + assert_eq!(cstr!("hello"), Borrow::borrow(&t.as_cow_c_str().unwrap())); + assert_eq!(cstr!("hello"), Borrow::borrow(&t.into_c_str().unwrap())); + + let t: PathBuf = PathBuf::from("hello".to_owned()); + assert_eq!("hello", t.as_str().unwrap()); + assert_eq!("hello".to_owned(), Arg::to_string_lossy(&t)); + assert_eq!(cstr!("hello"), Borrow::borrow(&t.as_cow_c_str().unwrap())); + assert_eq!(cstr!("hello"), Borrow::borrow(&t.into_c_str().unwrap())); + + let t: &CStr = cstr!("hello"); + assert_eq!("hello", t.as_str().unwrap()); + assert_eq!("hello".to_owned(), Arg::to_string_lossy(&t)); + assert_eq!(cstr!("hello"), Borrow::borrow(&t.as_cow_c_str().unwrap())); + assert_eq!(cstr!("hello"), Borrow::borrow(&t.into_c_str().unwrap())); + + let t: CString = cstr!("hello").to_owned(); + assert_eq!("hello", t.as_str().unwrap()); + assert_eq!("hello".to_owned(), Arg::to_string_lossy(&t)); + assert_eq!( + cstr!("hello"), + Borrow::borrow(&Arg::as_cow_c_str(&t).unwrap()) + ); + assert_eq!(cstr!("hello"), Borrow::borrow(&t.into_c_str().unwrap())); + + let t: Components = Path::new("hello").components(); + assert_eq!("hello", t.as_str().unwrap()); + assert_eq!("hello".to_owned(), Arg::to_string_lossy(&t)); + assert_eq!(cstr!("hello"), Borrow::borrow(&t.as_cow_c_str().unwrap())); + assert_eq!(cstr!("hello"), Borrow::borrow(&t.into_c_str().unwrap())); + + let t: Component = Path::new("hello").components().next().unwrap(); + assert_eq!("hello", t.as_str().unwrap()); + assert_eq!("hello".to_owned(), Arg::to_string_lossy(&t)); + assert_eq!(cstr!("hello"), Borrow::borrow(&t.as_cow_c_str().unwrap())); + assert_eq!(cstr!("hello"), Borrow::borrow(&t.into_c_str().unwrap())); + + let t: Iter = Path::new("hello").iter(); + assert_eq!("hello", t.as_str().unwrap()); + assert_eq!("hello".to_owned(), Arg::to_string_lossy(&t)); + assert_eq!(cstr!("hello"), Borrow::borrow(&t.as_cow_c_str().unwrap())); + assert_eq!(cstr!("hello"), Borrow::borrow(&t.into_c_str().unwrap())); + + let t: Cow<'_, str> = Cow::Borrowed("hello"); + assert_eq!("hello", t.as_str().unwrap()); + assert_eq!("hello".to_owned(), Arg::to_string_lossy(&t)); + assert_eq!(cstr!("hello"), Borrow::borrow(&t.as_cow_c_str().unwrap())); + assert_eq!(cstr!("hello"), Borrow::borrow(&t.into_c_str().unwrap())); + + let t: Cow<'_, str> = Cow::Owned("hello".to_owned()); + assert_eq!("hello", t.as_str().unwrap()); + assert_eq!("hello".to_owned(), Arg::to_string_lossy(&t)); + assert_eq!(cstr!("hello"), Borrow::borrow(&t.as_cow_c_str().unwrap())); + assert_eq!(cstr!("hello"), Borrow::borrow(&t.into_c_str().unwrap())); + + let t: Cow<'_, OsStr> = Cow::Borrowed(OsStr::new("hello")); + assert_eq!("hello", t.as_str().unwrap()); + assert_eq!("hello".to_owned(), Arg::to_string_lossy(&t)); + assert_eq!(cstr!("hello"), Borrow::borrow(&t.as_cow_c_str().unwrap())); + assert_eq!(cstr!("hello"), Borrow::borrow(&t.into_c_str().unwrap())); + + let t: Cow<'_, OsStr> = Cow::Owned(OsString::from("hello".to_owned())); + assert_eq!("hello", t.as_str().unwrap()); + assert_eq!("hello".to_owned(), Arg::to_string_lossy(&t)); + assert_eq!(cstr!("hello"), Borrow::borrow(&t.as_cow_c_str().unwrap())); + assert_eq!(cstr!("hello"), Borrow::borrow(&t.into_c_str().unwrap())); + + let t: Cow<'_, CStr> = Cow::Borrowed(cstr!("hello")); + assert_eq!("hello", t.as_str().unwrap()); + assert_eq!("hello".to_owned(), Arg::to_string_lossy(&t)); + assert_eq!(cstr!("hello"), Borrow::borrow(&t.as_cow_c_str().unwrap())); + assert_eq!(cstr!("hello"), Borrow::borrow(&t.into_c_str().unwrap())); + + let t: Cow<'_, CStr> = Cow::Owned(cstr!("hello").to_owned()); + assert_eq!("hello", t.as_str().unwrap()); + assert_eq!("hello".to_owned(), Arg::to_string_lossy(&t)); + assert_eq!(cstr!("hello"), Borrow::borrow(&t.as_cow_c_str().unwrap())); + assert_eq!(cstr!("hello"), Borrow::borrow(&t.into_c_str().unwrap())); + + let t: &[u8] = b"hello"; + assert_eq!("hello", t.as_str().unwrap()); + assert_eq!("hello".to_owned(), Arg::to_string_lossy(&t)); + assert_eq!(cstr!("hello"), Borrow::borrow(&t.as_cow_c_str().unwrap())); + assert_eq!(cstr!("hello"), Borrow::borrow(&t.into_c_str().unwrap())); + + let t: Vec<u8> = b"hello".to_vec(); + assert_eq!("hello", t.as_str().unwrap()); + assert_eq!("hello".to_owned(), Arg::to_string_lossy(&t)); + assert_eq!(cstr!("hello"), Borrow::borrow(&t.as_cow_c_str().unwrap())); + assert_eq!(cstr!("hello"), Borrow::borrow(&t.into_c_str().unwrap())); + + #[cfg(feature = "itoa")] + { + let t: DecInt = DecInt::new(43110); + assert_eq!("43110", t.as_str()); + assert_eq!("43110".to_owned(), Arg::to_string_lossy(&t)); + assert_eq!(cstr!("43110"), Borrow::borrow(&t.as_cow_c_str().unwrap())); + assert_eq!(cstr!("43110"), t.as_c_str()); + assert_eq!(cstr!("43110"), Borrow::borrow(&t.into_c_str().unwrap())); + } +} + +#[test] +fn test_invalid() { + use std::borrow::Borrow; + + let t: &[u8] = b"hello\xc0world"; + assert_eq!(t.as_str().unwrap_err(), io::Errno::INVAL); + assert_eq!("hello\u{fffd}world".to_owned(), Arg::to_string_lossy(&t)); + assert_eq!( + b"hello\xc0world\0", + Borrow::<CStr>::borrow(&t.as_cow_c_str().unwrap()).to_bytes_with_nul() + ); + assert_eq!( + b"hello\xc0world\0", + Borrow::<CStr>::borrow(&t.into_c_str().unwrap()).to_bytes_with_nul() + ); +} + +#[test] +fn test_embedded_nul() { + let t: &[u8] = b"hello\0world"; + assert_eq!("hello\0world", t.as_str().unwrap()); + assert_eq!("hello\0world".to_owned(), Arg::to_string_lossy(&t)); + assert_eq!(t.as_cow_c_str().unwrap_err(), io::Errno::INVAL); + assert_eq!(t.into_c_str().unwrap_err(), io::Errno::INVAL); +} |