1b8a62b91Sopenharmony_ciuse rustix::ffi::{CStr, CString}; 2b8a62b91Sopenharmony_ciuse rustix::io; 3b8a62b91Sopenharmony_ciuse rustix::path::Arg; 4b8a62b91Sopenharmony_ci#[cfg(feature = "itoa")] 5b8a62b91Sopenharmony_ciuse rustix::path::DecInt; 6b8a62b91Sopenharmony_ciuse std::borrow::Cow; 7b8a62b91Sopenharmony_ciuse std::ffi::{OsStr, OsString}; 8b8a62b91Sopenharmony_ciuse std::path::{Component, Components, Iter, Path, PathBuf}; 9b8a62b91Sopenharmony_ci 10b8a62b91Sopenharmony_ci#[test] 11b8a62b91Sopenharmony_cifn test_arg() { 12b8a62b91Sopenharmony_ci use rustix::cstr; 13b8a62b91Sopenharmony_ci use std::borrow::Borrow; 14b8a62b91Sopenharmony_ci 15b8a62b91Sopenharmony_ci let t: &str = "hello"; 16b8a62b91Sopenharmony_ci assert_eq!("hello", t.as_str().unwrap()); 17b8a62b91Sopenharmony_ci assert_eq!("hello".to_owned(), Arg::to_string_lossy(&t)); 18b8a62b91Sopenharmony_ci assert_eq!(cstr!("hello"), Borrow::borrow(&t.as_cow_c_str().unwrap())); 19b8a62b91Sopenharmony_ci assert_eq!(cstr!("hello"), Borrow::borrow(&t.into_c_str().unwrap())); 20b8a62b91Sopenharmony_ci 21b8a62b91Sopenharmony_ci let t: String = "hello".to_owned(); 22b8a62b91Sopenharmony_ci assert_eq!("hello", Arg::as_str(&t).unwrap()); 23b8a62b91Sopenharmony_ci assert_eq!("hello".to_owned(), Arg::to_string_lossy(&t)); 24b8a62b91Sopenharmony_ci assert_eq!(cstr!("hello"), Borrow::borrow(&t.as_cow_c_str().unwrap())); 25b8a62b91Sopenharmony_ci assert_eq!(cstr!("hello"), Borrow::borrow(&t.into_c_str().unwrap())); 26b8a62b91Sopenharmony_ci 27b8a62b91Sopenharmony_ci let t: &OsStr = OsStr::new("hello"); 28b8a62b91Sopenharmony_ci assert_eq!("hello", t.as_str().unwrap()); 29b8a62b91Sopenharmony_ci assert_eq!("hello".to_owned(), Arg::to_string_lossy(&t)); 30b8a62b91Sopenharmony_ci assert_eq!(cstr!("hello"), Borrow::borrow(&t.as_cow_c_str().unwrap())); 31b8a62b91Sopenharmony_ci assert_eq!(cstr!("hello"), Borrow::borrow(&t.into_c_str().unwrap())); 32b8a62b91Sopenharmony_ci 33b8a62b91Sopenharmony_ci let t: OsString = OsString::from("hello".to_owned()); 34b8a62b91Sopenharmony_ci assert_eq!("hello", t.as_str().unwrap()); 35b8a62b91Sopenharmony_ci assert_eq!("hello".to_owned(), Arg::to_string_lossy(&t)); 36b8a62b91Sopenharmony_ci assert_eq!(cstr!("hello"), Borrow::borrow(&t.as_cow_c_str().unwrap())); 37b8a62b91Sopenharmony_ci assert_eq!(cstr!("hello"), Borrow::borrow(&t.into_c_str().unwrap())); 38b8a62b91Sopenharmony_ci 39b8a62b91Sopenharmony_ci let t: &Path = Path::new("hello"); 40b8a62b91Sopenharmony_ci assert_eq!("hello", t.as_str().unwrap()); 41b8a62b91Sopenharmony_ci assert_eq!("hello".to_owned(), Arg::to_string_lossy(&t)); 42b8a62b91Sopenharmony_ci assert_eq!(cstr!("hello"), Borrow::borrow(&t.as_cow_c_str().unwrap())); 43b8a62b91Sopenharmony_ci assert_eq!(cstr!("hello"), Borrow::borrow(&t.into_c_str().unwrap())); 44b8a62b91Sopenharmony_ci 45b8a62b91Sopenharmony_ci let t: PathBuf = PathBuf::from("hello".to_owned()); 46b8a62b91Sopenharmony_ci assert_eq!("hello", t.as_str().unwrap()); 47b8a62b91Sopenharmony_ci assert_eq!("hello".to_owned(), Arg::to_string_lossy(&t)); 48b8a62b91Sopenharmony_ci assert_eq!(cstr!("hello"), Borrow::borrow(&t.as_cow_c_str().unwrap())); 49b8a62b91Sopenharmony_ci assert_eq!(cstr!("hello"), Borrow::borrow(&t.into_c_str().unwrap())); 50b8a62b91Sopenharmony_ci 51b8a62b91Sopenharmony_ci let t: &CStr = cstr!("hello"); 52b8a62b91Sopenharmony_ci assert_eq!("hello", t.as_str().unwrap()); 53b8a62b91Sopenharmony_ci assert_eq!("hello".to_owned(), Arg::to_string_lossy(&t)); 54b8a62b91Sopenharmony_ci assert_eq!(cstr!("hello"), Borrow::borrow(&t.as_cow_c_str().unwrap())); 55b8a62b91Sopenharmony_ci assert_eq!(cstr!("hello"), Borrow::borrow(&t.into_c_str().unwrap())); 56b8a62b91Sopenharmony_ci 57b8a62b91Sopenharmony_ci let t: CString = cstr!("hello").to_owned(); 58b8a62b91Sopenharmony_ci assert_eq!("hello", t.as_str().unwrap()); 59b8a62b91Sopenharmony_ci assert_eq!("hello".to_owned(), Arg::to_string_lossy(&t)); 60b8a62b91Sopenharmony_ci assert_eq!( 61b8a62b91Sopenharmony_ci cstr!("hello"), 62b8a62b91Sopenharmony_ci Borrow::borrow(&Arg::as_cow_c_str(&t).unwrap()) 63b8a62b91Sopenharmony_ci ); 64b8a62b91Sopenharmony_ci assert_eq!(cstr!("hello"), Borrow::borrow(&t.into_c_str().unwrap())); 65b8a62b91Sopenharmony_ci 66b8a62b91Sopenharmony_ci let t: Components = Path::new("hello").components(); 67b8a62b91Sopenharmony_ci assert_eq!("hello", t.as_str().unwrap()); 68b8a62b91Sopenharmony_ci assert_eq!("hello".to_owned(), Arg::to_string_lossy(&t)); 69b8a62b91Sopenharmony_ci assert_eq!(cstr!("hello"), Borrow::borrow(&t.as_cow_c_str().unwrap())); 70b8a62b91Sopenharmony_ci assert_eq!(cstr!("hello"), Borrow::borrow(&t.into_c_str().unwrap())); 71b8a62b91Sopenharmony_ci 72b8a62b91Sopenharmony_ci let t: Component = Path::new("hello").components().next().unwrap(); 73b8a62b91Sopenharmony_ci assert_eq!("hello", t.as_str().unwrap()); 74b8a62b91Sopenharmony_ci assert_eq!("hello".to_owned(), Arg::to_string_lossy(&t)); 75b8a62b91Sopenharmony_ci assert_eq!(cstr!("hello"), Borrow::borrow(&t.as_cow_c_str().unwrap())); 76b8a62b91Sopenharmony_ci assert_eq!(cstr!("hello"), Borrow::borrow(&t.into_c_str().unwrap())); 77b8a62b91Sopenharmony_ci 78b8a62b91Sopenharmony_ci let t: Iter = Path::new("hello").iter(); 79b8a62b91Sopenharmony_ci assert_eq!("hello", t.as_str().unwrap()); 80b8a62b91Sopenharmony_ci assert_eq!("hello".to_owned(), Arg::to_string_lossy(&t)); 81b8a62b91Sopenharmony_ci assert_eq!(cstr!("hello"), Borrow::borrow(&t.as_cow_c_str().unwrap())); 82b8a62b91Sopenharmony_ci assert_eq!(cstr!("hello"), Borrow::borrow(&t.into_c_str().unwrap())); 83b8a62b91Sopenharmony_ci 84b8a62b91Sopenharmony_ci let t: Cow<'_, str> = Cow::Borrowed("hello"); 85b8a62b91Sopenharmony_ci assert_eq!("hello", t.as_str().unwrap()); 86b8a62b91Sopenharmony_ci assert_eq!("hello".to_owned(), Arg::to_string_lossy(&t)); 87b8a62b91Sopenharmony_ci assert_eq!(cstr!("hello"), Borrow::borrow(&t.as_cow_c_str().unwrap())); 88b8a62b91Sopenharmony_ci assert_eq!(cstr!("hello"), Borrow::borrow(&t.into_c_str().unwrap())); 89b8a62b91Sopenharmony_ci 90b8a62b91Sopenharmony_ci let t: Cow<'_, str> = Cow::Owned("hello".to_owned()); 91b8a62b91Sopenharmony_ci assert_eq!("hello", t.as_str().unwrap()); 92b8a62b91Sopenharmony_ci assert_eq!("hello".to_owned(), Arg::to_string_lossy(&t)); 93b8a62b91Sopenharmony_ci assert_eq!(cstr!("hello"), Borrow::borrow(&t.as_cow_c_str().unwrap())); 94b8a62b91Sopenharmony_ci assert_eq!(cstr!("hello"), Borrow::borrow(&t.into_c_str().unwrap())); 95b8a62b91Sopenharmony_ci 96b8a62b91Sopenharmony_ci let t: Cow<'_, OsStr> = Cow::Borrowed(OsStr::new("hello")); 97b8a62b91Sopenharmony_ci assert_eq!("hello", t.as_str().unwrap()); 98b8a62b91Sopenharmony_ci assert_eq!("hello".to_owned(), Arg::to_string_lossy(&t)); 99b8a62b91Sopenharmony_ci assert_eq!(cstr!("hello"), Borrow::borrow(&t.as_cow_c_str().unwrap())); 100b8a62b91Sopenharmony_ci assert_eq!(cstr!("hello"), Borrow::borrow(&t.into_c_str().unwrap())); 101b8a62b91Sopenharmony_ci 102b8a62b91Sopenharmony_ci let t: Cow<'_, OsStr> = Cow::Owned(OsString::from("hello".to_owned())); 103b8a62b91Sopenharmony_ci assert_eq!("hello", t.as_str().unwrap()); 104b8a62b91Sopenharmony_ci assert_eq!("hello".to_owned(), Arg::to_string_lossy(&t)); 105b8a62b91Sopenharmony_ci assert_eq!(cstr!("hello"), Borrow::borrow(&t.as_cow_c_str().unwrap())); 106b8a62b91Sopenharmony_ci assert_eq!(cstr!("hello"), Borrow::borrow(&t.into_c_str().unwrap())); 107b8a62b91Sopenharmony_ci 108b8a62b91Sopenharmony_ci let t: Cow<'_, CStr> = Cow::Borrowed(cstr!("hello")); 109b8a62b91Sopenharmony_ci assert_eq!("hello", t.as_str().unwrap()); 110b8a62b91Sopenharmony_ci assert_eq!("hello".to_owned(), Arg::to_string_lossy(&t)); 111b8a62b91Sopenharmony_ci assert_eq!(cstr!("hello"), Borrow::borrow(&t.as_cow_c_str().unwrap())); 112b8a62b91Sopenharmony_ci assert_eq!(cstr!("hello"), Borrow::borrow(&t.into_c_str().unwrap())); 113b8a62b91Sopenharmony_ci 114b8a62b91Sopenharmony_ci let t: Cow<'_, CStr> = Cow::Owned(cstr!("hello").to_owned()); 115b8a62b91Sopenharmony_ci assert_eq!("hello", t.as_str().unwrap()); 116b8a62b91Sopenharmony_ci assert_eq!("hello".to_owned(), Arg::to_string_lossy(&t)); 117b8a62b91Sopenharmony_ci assert_eq!(cstr!("hello"), Borrow::borrow(&t.as_cow_c_str().unwrap())); 118b8a62b91Sopenharmony_ci assert_eq!(cstr!("hello"), Borrow::borrow(&t.into_c_str().unwrap())); 119b8a62b91Sopenharmony_ci 120b8a62b91Sopenharmony_ci let t: &[u8] = b"hello"; 121b8a62b91Sopenharmony_ci assert_eq!("hello", t.as_str().unwrap()); 122b8a62b91Sopenharmony_ci assert_eq!("hello".to_owned(), Arg::to_string_lossy(&t)); 123b8a62b91Sopenharmony_ci assert_eq!(cstr!("hello"), Borrow::borrow(&t.as_cow_c_str().unwrap())); 124b8a62b91Sopenharmony_ci assert_eq!(cstr!("hello"), Borrow::borrow(&t.into_c_str().unwrap())); 125b8a62b91Sopenharmony_ci 126b8a62b91Sopenharmony_ci let t: Vec<u8> = b"hello".to_vec(); 127b8a62b91Sopenharmony_ci assert_eq!("hello", t.as_str().unwrap()); 128b8a62b91Sopenharmony_ci assert_eq!("hello".to_owned(), Arg::to_string_lossy(&t)); 129b8a62b91Sopenharmony_ci assert_eq!(cstr!("hello"), Borrow::borrow(&t.as_cow_c_str().unwrap())); 130b8a62b91Sopenharmony_ci assert_eq!(cstr!("hello"), Borrow::borrow(&t.into_c_str().unwrap())); 131b8a62b91Sopenharmony_ci 132b8a62b91Sopenharmony_ci #[cfg(feature = "itoa")] 133b8a62b91Sopenharmony_ci { 134b8a62b91Sopenharmony_ci let t: DecInt = DecInt::new(43110); 135b8a62b91Sopenharmony_ci assert_eq!("43110", t.as_str()); 136b8a62b91Sopenharmony_ci assert_eq!("43110".to_owned(), Arg::to_string_lossy(&t)); 137b8a62b91Sopenharmony_ci assert_eq!(cstr!("43110"), Borrow::borrow(&t.as_cow_c_str().unwrap())); 138b8a62b91Sopenharmony_ci assert_eq!(cstr!("43110"), t.as_c_str()); 139b8a62b91Sopenharmony_ci assert_eq!(cstr!("43110"), Borrow::borrow(&t.into_c_str().unwrap())); 140b8a62b91Sopenharmony_ci } 141b8a62b91Sopenharmony_ci} 142b8a62b91Sopenharmony_ci 143b8a62b91Sopenharmony_ci#[test] 144b8a62b91Sopenharmony_cifn test_invalid() { 145b8a62b91Sopenharmony_ci use std::borrow::Borrow; 146b8a62b91Sopenharmony_ci 147b8a62b91Sopenharmony_ci let t: &[u8] = b"hello\xc0world"; 148b8a62b91Sopenharmony_ci assert_eq!(t.as_str().unwrap_err(), io::Errno::INVAL); 149b8a62b91Sopenharmony_ci assert_eq!("hello\u{fffd}world".to_owned(), Arg::to_string_lossy(&t)); 150b8a62b91Sopenharmony_ci assert_eq!( 151b8a62b91Sopenharmony_ci b"hello\xc0world\0", 152b8a62b91Sopenharmony_ci Borrow::<CStr>::borrow(&t.as_cow_c_str().unwrap()).to_bytes_with_nul() 153b8a62b91Sopenharmony_ci ); 154b8a62b91Sopenharmony_ci assert_eq!( 155b8a62b91Sopenharmony_ci b"hello\xc0world\0", 156b8a62b91Sopenharmony_ci Borrow::<CStr>::borrow(&t.into_c_str().unwrap()).to_bytes_with_nul() 157b8a62b91Sopenharmony_ci ); 158b8a62b91Sopenharmony_ci} 159b8a62b91Sopenharmony_ci 160b8a62b91Sopenharmony_ci#[test] 161b8a62b91Sopenharmony_cifn test_embedded_nul() { 162b8a62b91Sopenharmony_ci let t: &[u8] = b"hello\0world"; 163b8a62b91Sopenharmony_ci assert_eq!("hello\0world", t.as_str().unwrap()); 164b8a62b91Sopenharmony_ci assert_eq!("hello\0world".to_owned(), Arg::to_string_lossy(&t)); 165b8a62b91Sopenharmony_ci assert_eq!(t.as_cow_c_str().unwrap_err(), io::Errno::INVAL); 166b8a62b91Sopenharmony_ci assert_eq!(t.into_c_str().unwrap_err(), io::Errno::INVAL); 167b8a62b91Sopenharmony_ci} 168