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