summaryrefslogtreecommitdiffstats
path: root/vendor/rustix/tests/path/arg.rs
blob: 0eb511be1b7e5fe6971b6e637d6e973203d7500f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
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);
}