Debian Codegraph @

tests/by-util/test_eintr_handling.rs

raw
1 // This file is part of the uutils coreutils package.
2 //
3 // For the full copyright and license information, please view the LICENSE
4 // file that was distributed with this source code.
5
6 //! Tests for EINTR (ErrorKind::Interrupted) handling across utilities
7 //!
8 //! This module provides test utilities and integration tests to verify that
9 //! utilities properly handle signal interruptions during I/O operations.
10 //!
11 //! # CI Integration
12 //! EINTR handling tests are NOW visible in CI logs through integration tests:
13 //! - `test_cat_eintr_handling` in `tests/by-util/test_cat.rs`
14 //! - `test_comm_eintr_handling` in `tests/by-util/test_comm.rs`
15 //! - `test_od_eintr_handling` in `tests/by-util/test_od.rs`
16 //!
17 //! These integration tests use the mock utilities from this module to verify
18 //! that each utility properly handles signal interruptions during I/O operations.
19 //! Test results appear in CI logs under the "Test" steps when running `cargo nextest run`.
20 //!
21 //! # Note
22 //! EINTR is a POSIX error code for interrupted system calls
23 //! cspell:ignore EINTR worl
24
25 use std::io::{self, Read, Write};
26
27 /// A mock reader that injects `ErrorKind::Interrupted` errors
28 ///
29 /// This reader wraps another reader and injects a specified number of
30 /// `Interrupted` errors before allowing reads to succeed. This simulates
31 /// the behavior of system calls being interrupted by signals (EINTR).
32 pub struct InterruptingReader<R: Read> {
33 inner: R,
34 interrupts_remaining: usize,
35 bytes_read: usize,
36 }
37
38 impl<R: Read> InterruptingReader<R> {
39 /// Create a new interrupting reader
40 ///
41 /// # Arguments
42 /// * `inner` - The underlying reader to wrap
43 /// * `num_interrupts` - Number of times to return `Interrupted` error before succeeding
44 pub fn new(inner: R, num_interrupts: usize) -> Self {
45 Self {
46 inner,
47 interrupts_remaining: num_interrupts,
48 bytes_read: 0,
49 }
50 }
51
52 /// Get the total number of bytes successfully read
53 pub fn bytes_read(&self) -> usize {
54 self.bytes_read
55 }
56 }
57
58 impl<R: Read> Read for InterruptingReader<R> {
59 fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
60 if self.interrupts_remaining > 0 {
61 self.interrupts_remaining -= 1;
62 return Err(io::Error::from(io::ErrorKind::Interrupted));
63 }
64 match self.inner.read(buf) {
65 Ok(n) => {
66 self.bytes_read += n;
67 Ok(n)
68 }
69 err => err,
70 }
71 }
72 }
73
74 /// A mock writer that injects `ErrorKind::Interrupted` errors
75 pub struct InterruptingWriter<W: Write> {
76 inner: W,
77 interrupts_remaining: usize,
78 bytes_written: usize,
79 }
80
81 impl<W: Write> InterruptingWriter<W> {
82 /// Create a new interrupting writer
83 pub fn new(inner: W, num_interrupts: usize) -> Self {
84 Self {
85 inner,
86 interrupts_remaining: num_interrupts,
87 bytes_written: 0,
88 }
89 }
90
91 /// Get the total number of bytes successfully written
92 pub fn bytes_written(&self) -> usize {
93 self.bytes_written
94 }
95 }
96
97 impl<W: Write> Write for InterruptingWriter<W> {
98 fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
99 if self.interrupts_remaining > 0 {
100 self.interrupts_remaining -= 1;
101 return Err(io::Error::from(io::ErrorKind::Interrupted));
102 }
103 match self.inner.write(buf) {
104 Ok(n) => {
105 self.bytes_written += n;
106 Ok(n)
107 }
108 err => err,
109 }
110 }
111
112 fn flush(&mut self) -> io::Result<()> {
113 self.inner.flush()
114 }
115 }
116
117 #[cfg(test)]
118 mod tests {
119 use super::*;
120 use std::io::Cursor;
121
122 #[test]
123 fn test_interrupting_reader_no_interrupts() {
124 let data = b"hello world";
125 let mut reader = InterruptingReader::new(Cursor::new(data), 0);
126 let mut buf = [0u8; 11];
127
128 let n = reader.read(&mut buf).unwrap();
129 assert_eq!(n, 11);
130 assert_eq!(&buf, data);
131 assert_eq!(reader.bytes_read(), 11);
132 }
133
134 #[test]
135 fn test_interrupting_reader_with_interrupts() {
136 let data = b"hello world";
137 let mut reader = InterruptingReader::new(Cursor::new(data), 3);
138 let mut buf = [0u8; 11];
139
140 // First 3 reads should fail with Interrupted
141 for i in 0..3 {
142 let result = reader.read(&mut buf);
143 assert!(result.is_err());
144 assert_eq!(
145 result.unwrap_err().kind(),
146 io::ErrorKind::Interrupted,
147 "Read {} should be interrupted",
148 i
149 );
150 }
151
152 // Fourth read should succeed
153 let n = reader.read(&mut buf).unwrap();
154 assert_eq!(n, 11);
155 assert_eq!(&buf, data);
156 assert_eq!(reader.bytes_read(), 11);
157 }
158
159 #[test]
160 fn test_interrupting_reader_multiple_chunks() {
161 let data = b"hello world";
162 let mut reader = InterruptingReader::new(Cursor::new(data), 2);
163 let mut buf = [0u8; 5];
164
165 // First 2 reads should fail
166 assert!(reader.read(&mut buf).is_err());
167 assert!(reader.read(&mut buf).is_err());
168
169 // Third read gets first 5 bytes
170 let n = reader.read(&mut buf).unwrap();
171 assert_eq!(n, 5);
172 assert_eq!(&buf, b"hello");
173
174 // Read rest of data without interruption
175 let n = reader.read(&mut buf).unwrap();
176 assert_eq!(n, 5);
177 assert_eq!(&buf, b" worl"); // Second chunk of "hello world"
178
179 let n = reader.read(&mut buf).unwrap();
180 assert_eq!(n, 1);
181 assert_eq!(buf[0], b'd'); // Final 'd' from "world"
182
183 assert_eq!(reader.bytes_read(), 11);
184 }
185
186 #[test]
187 fn test_interrupting_writer_no_interrupts() {
188 let mut buffer = Vec::new();
189 let mut writer = InterruptingWriter::new(&mut buffer, 0);
190
191 let n = writer.write(b"test").unwrap();
192 assert_eq!(n, 4);
193 writer.flush().unwrap();
194 assert_eq!(buffer, b"test");
195 assert_eq!(writer.bytes_written(), 4);
196 }
197
198 #[test]
199 fn test_interrupting_writer_with_interrupts() {
200 let mut buffer = Vec::new();
201 let mut writer = InterruptingWriter::new(&mut buffer, 2);
202
203 // First 2 writes should fail
204 assert!(writer.write(b"test").is_err());
205 assert!(writer.write(b"test").is_err());
206
207 // Third write should succeed
208 let n = writer.write(b"test").unwrap();
209 assert_eq!(n, 4);
210 writer.flush().unwrap();
211 assert_eq!(buffer, b"test");
212 assert_eq!(writer.bytes_written(), 4);
213 }
214 }