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
mod posix_string;
mod wasi_string;

pub use posix_string::PosixString;
pub use wasi_string::WasiString;

#[test]
fn round_trip() {
    use std::ffi::CStr;
    assert_eq!(
        WasiString::from_maybe_nonutf8_cstr(PosixString::from_path_bytes(b"").unwrap().as_cstr())
            .as_str(),
        ""
    );
    assert_eq!(
        WasiString::from_maybe_nonutf8_cstr(
            PosixString::from_path_bytes(b"hello").unwrap().as_cstr()
        )
        .as_str(),
        "hello"
    );
    assert_eq!(
        PosixString::from_path_str(
            WasiString::from_maybe_nonutf8_cstr(CStr::from_bytes_with_nul(b"hello\0").unwrap())
                .as_str()
        )
        .unwrap()
        .as_cstr(),
        CStr::from_bytes_with_nul(b"hello\0").unwrap()
    );
    assert_eq!(
        PosixString::from_path_str(
            WasiString::from_maybe_nonutf8_cstr(
                CStr::from_bytes_with_nul(b"h\xc0ello\xc1\0").unwrap()
            )
            .as_str()
        )
        .unwrap()
        .as_cstr(),
        CStr::from_bytes_with_nul(b"h\xc0ello\xc1\0").unwrap()
    );
    assert_eq!(
        PosixString::from_path_str(
            WasiString::from_maybe_nonutf8_cstr(CStr::from_bytes_with_nul(b"\xf5\xff\0").unwrap())
                .as_str()
        )
        .unwrap()
        .as_cstr(),
        CStr::from_bytes_with_nul(b"\xf5\xff\0").unwrap()
    );
    assert_eq!(
        PosixString::from_path_str(
            WasiString::from_maybe_nonutf8_cstr(CStr::from_bytes_with_nul(b"\0").unwrap()).as_str()
        )
        .unwrap()
        .as_cstr(),
        CStr::from_bytes_with_nul(b"\0").unwrap()
    );
    assert_eq!(
        PosixString::from_path_str(
            WasiString::from_maybe_nonutf8_cstr(CStr::from_bytes_with_nul(b"\xe6\x96\0").unwrap())
                .as_str()
        )
        .unwrap()
        .as_cstr(),
        CStr::from_bytes_with_nul(b"\xe6\x96\0").unwrap()
    );
}