xref: /third_party/rust/crates/rustix/tests/io/pipe.rs (revision b8a62b91)
1#[cfg(feature = "fs")]
2#[cfg(any(target_os = "android", target_os = "linux"))]
3#[test]
4fn test_splice_cursor() {
5    use rustix::io::{pipe, splice, SpliceFlags};
6    use std::io::{Read, Seek, SeekFrom, Write};
7
8    let mut src = tempfile::tempfile().unwrap();
9    let mut dest = tempfile::tempfile().unwrap();
10    let (read_p, write_p) = pipe().unwrap();
11    let mut buff = vec![];
12
13    writeln!(src, "hello world").unwrap();
14
15    src.seek(SeekFrom::Start(6)).unwrap();
16
17    splice(&src, None, &write_p, None, 5, SpliceFlags::empty()).unwrap();
18    splice(&read_p, None, &dest, None, 5, SpliceFlags::empty()).unwrap();
19
20    // When we can depend on Rust 1.55, we can use `rewind` here.
21    dest.seek(SeekFrom::Start(0)).unwrap();
22
23    dest.read_to_end(&mut buff).unwrap();
24    assert_eq!(buff, b"world");
25}
26
27#[cfg(feature = "fs")]
28#[cfg(any(target_os = "android", target_os = "linux"))]
29#[test]
30fn test_splice_offset() {
31    use rustix::io::{pipe, splice, SpliceFlags};
32    use std::io::{Read, Write};
33
34    let mut src = tempfile::tempfile().unwrap();
35    let mut dest = tempfile::tempfile().unwrap();
36    let (read_p, write_p) = pipe().unwrap();
37    let mut buff = vec![];
38    let mut off_w = 0;
39    let mut off_r = 0;
40
41    writeln!(src, "hello world").unwrap();
42
43    splice(
44        &src,
45        Some(&mut off_w),
46        &write_p,
47        None,
48        5,
49        SpliceFlags::empty(),
50    )
51    .unwrap();
52    splice(
53        &read_p,
54        None,
55        &dest,
56        Some(&mut off_r),
57        5,
58        SpliceFlags::empty(),
59    )
60    .unwrap();
61
62    dest.read_to_end(&mut buff).unwrap();
63    assert_eq!(buff, b"hello");
64    assert_eq!(off_w, 5);
65    assert_eq!(off_r, 5);
66}
67
68#[cfg(feature = "fs")]
69#[cfg(any(target_os = "android", target_os = "linux"))]
70#[test]
71fn test_splice_pipe2pipe() {
72    use rustix::io::{pipe, read, splice, write, SpliceFlags};
73
74    let (read_p1, write_p1) = pipe().unwrap();
75    let (read_p2, write_p2) = pipe().unwrap();
76    let mut buff = [0; 5];
77
78    write(&write_p1, b"hello").unwrap();
79    splice(&read_p1, None, write_p2, None, 5, SpliceFlags::empty()).unwrap();
80    read(&read_p2, &mut buff).unwrap();
81
82    assert_eq!(&buff, b"hello");
83}
84
85#[cfg(feature = "fs")]
86#[cfg(any(target_os = "android", target_os = "linux"))]
87#[test]
88fn test_vmsplice_write() {
89    use rustix::io::{pipe, read, vmsplice, IoSliceRaw, SpliceFlags};
90
91    let (read_p, write_p) = pipe().unwrap();
92    let mut output = [0; 11];
93    let input = [
94        IoSliceRaw::from_slice(b"hello"),
95        IoSliceRaw::from_slice(b" "),
96        IoSliceRaw::from_slice(b"world"),
97    ];
98
99    unsafe { vmsplice(&write_p, &input, SpliceFlags::empty()).unwrap() };
100    read(&read_p, &mut output).unwrap();
101
102    assert_eq!(&output, b"hello world");
103}
104
105#[cfg(feature = "fs")]
106#[cfg(any(target_os = "android", target_os = "linux"))]
107#[test]
108fn test_vmsplice_read() {
109    use rustix::io::{pipe, vmsplice, write, IoSliceRaw, SpliceFlags};
110
111    let (read_p, write_p) = pipe().unwrap();
112    let mut outputs = ([0; 5], [0; 1], [0; 5]);
113    let outputs_slices = [
114        IoSliceRaw::from_slice_mut(&mut outputs.0),
115        IoSliceRaw::from_slice_mut(&mut outputs.1),
116        IoSliceRaw::from_slice_mut(&mut outputs.2),
117    ];
118
119    write(&write_p, b"hello world").unwrap();
120    unsafe { vmsplice(&read_p, &outputs_slices, SpliceFlags::empty()).unwrap() };
121
122    assert_eq!(&outputs.0, b"hello");
123    assert_eq!(&outputs.1, b" ");
124    assert_eq!(&outputs.2, b"world");
125}
126