| 2 | #[cfg(linux_kernel)] |
| 3 | #[test] |
| 4 | fn test_tee() { |
| 5 | use rustix::io::{read, write}; |
| 6 | use rustix::pipe::{pipe, tee, SpliceFlags}; |
| 7 | |
| 8 | let message = b"Hello, tee!"; |
| 9 | assert!(message.len() <= rustix::pipe::PIPE_BUF); |
| 10 | |
| 11 | // Create two pipes. |
| 12 | let (read_a, write_a) = pipe().unwrap(); |
| 13 | let (read_b, write_b) = pipe().unwrap(); |
| 14 | |
| 15 | // Write a message into one of the pipes. |
| 16 | let n = write(&write_a, message).unwrap(); |
| 17 | assert_eq!(n, message.len()); |
| 18 | |
| 19 | // "Tee" the message into the other pipe. |
| 20 | let n = tee(&read_a, &write_b, 256, SpliceFlags::empty()).unwrap(); |
| 21 | assert_eq!(n, message.len()); |
| 22 | |
| 23 | // Check that the "tee" wrote our message to the other pipe. |
| 24 | let mut buf = vec![0_u8; 256]; |
| 25 | let n = read(&read_b, &mut buf).unwrap(); |
| 26 | assert_eq!(n, message.len()); |
| 27 | assert_eq!(&buf[..n], message); |
| 28 | |
| 29 | // Check that the "tee" left our message in the first pipe. |
| 30 | let mut buf = vec![0_u8; 256]; |
| 31 | let n = read(&read_a, &mut buf).unwrap(); |
| 32 | assert_eq!(n, message.len()); |
| 33 | assert_eq!(&buf[..n], message); |
| 34 | } |