yadon/
lib.rs

1use thiserror::Error;
2use std::io::{Seek, SeekFrom, Write};
3use std::fmt::Debug;
4
5#[derive(Debug, Default)]
6/// Stores write and seek operations to be replayed later.
7/// # Example
8/// ```
9/// use yadon::Yadon;
10/// use std::io::{Cursor, Write, Seek, SeekFrom};
11/// let mut target = vec![0u8; 8];
12/// let mut yadon = Yadon::new(Some(0), Some(target.len() as u64));
13/// assert_eq!(yadon.seek(SeekFrom::Start(4)).unwrap(), 4);
14/// assert_eq!(yadon.write(&[1,2,3]).unwrap(), 3);
15/// assert_eq!(yadon.seek(SeekFrom::Current(0)).unwrap(), 7);
16/// assert_eq!(yadon.seek(SeekFrom::End(-6)).unwrap(), 2);
17/// assert_eq!(yadon.write(&[4,5]).unwrap(), 2);
18/// assert_eq!(yadon.seek(SeekFrom::Current(0)).unwrap(), 4);
19/// 
20/// let mut target_writer = Cursor::new(&mut target);
21/// // Apply the stored operations to our target writer.
22/// // Pass `true` so the return values of the seeks and writes are compared with the simulated values.
23/// yadon.apply(&mut target_writer, true).unwrap();
24/// assert_eq!(target, &[0, 0, 4, 5, 1, 2, 3, 0]);
25/// ```
26/// # Remarks
27/// * If a start position is set when apply() is called, the target will seek to the start position.
28/// * Boundary checks are only performed during `write()` if length is specified.
29pub struct Yadon {
30    /// Stored operations
31    pub operations: Vec<WriteOperation>,
32    /// Virtual position to use for emulating the return values of another Write + Seek
33    virtual_position: Option<u64>,
34    /// If set, used to set the initial virtual cursor position. `apply()` will seek to this position before applying.
35    pub start: Option<u64>,
36    /// If set, used to emulate cursor position for SeekFrom::End operations. If not set, seeks involving SeekFrom::End will fail, returning `Err(std::io::ErrorKind::Unsupported)`
37    pub length: Option<u64>,
38}
39
40/// Errors that may occur while applying `Yadon`.
41#[derive(Error, Debug)]
42pub enum ApplyError {
43    /// IO error while trying to replay operations.
44    #[error("io error while trying to replay operations")]
45    Io(#[from] std::io::Error),
46    /// Seek position diverged while trying to replay operations.
47    #[error("seek position diverged while trying to replay operations")]
48    SeekDiverged(Confusion<u64>),
49    /// Number of bytes written diverged while trying to replay operations.
50    #[error("number of bytes written diverged while trying to replay operations")]
51    NumBytesWrittenDiverge(Confusion<usize>)
52}
53
54/// During apply, there was divergence between the expected return value of an operation, and its result.
55#[derive(Debug)]
56pub struct Confusion<T>
57where T: Debug {
58    /// The value which we returned when the operation was first simulated.
59    pub expected: T,
60    /// The value which was returned when trying to apply this operation to another Write + Seek.
61    pub actual: T,
62}
63
64#[derive(Debug)]
65/// Write + Seek operations which were called on Yadon
66pub enum WriteOperation {
67    /// Write something, and check that the number of bytes written matches.
68    Write(Vec<u8>, usize),
69    /// Seek somewhere, and check that the resulting position matches.
70    Seek(SeekFrom, u64)
71}
72
73impl Yadon {
74    /// Constructs an instance of `Yadon` with optional `start` position and `length`, which, if set, should match
75    /// whatever you plan to apply `Yadon` to later.
76    pub fn new(start: Option<u64>, length: Option<u64>) -> Self {
77        Yadon {
78            operations: vec![],
79            virtual_position: None,
80            start,
81            length,
82        }
83    }
84
85    /// Applies the stored operations on a target writer. Operations are not consumed, and may be replayed again.
86    /// If a `start` position was specified, this will seek to that position before applying.
87    /// If `check_return_values` is set, the result of each seek / write will be compared to the
88    /// simulated return value, and the apply will fail if it is different.
89    pub fn apply<T>(&self, target: &mut T, check_return_values: bool) -> Result<usize, ApplyError> where T: Write + Seek {
90        if let Some(start) = self.start {
91            let seek_pos = target.seek(SeekFrom::Start(start))?;
92            if check_return_values && seek_pos != start {
93                // Something is wrong with the seek.
94                return Err(ApplyError::SeekDiverged(Confusion {
95                    expected: start,
96                    actual: seek_pos
97                }));
98            }
99        }
100        let mut total_bytes_written: usize = 0;
101        for operation in &self.operations {
102            match operation {
103                WriteOperation::Write(data, expected_bytes_written) => {
104                    let bytes_written = target.write(data)?;
105                    if check_return_values && *expected_bytes_written != bytes_written {
106                        return Err(ApplyError::NumBytesWrittenDiverge(Confusion{
107                            expected: *expected_bytes_written,
108                            actual: bytes_written
109                        }));
110                    }
111                    total_bytes_written += bytes_written;
112                },
113                WriteOperation::Seek(pos, expected_position) => {
114                    let new_position = target.seek(*pos)?;
115                    if check_return_values && new_position != *expected_position {
116                        return Err(ApplyError::SeekDiverged(Confusion{
117                            expected: *expected_position,
118                            actual: new_position
119                        }));
120                    }
121                }
122            }
123        }
124        target.flush()?;
125        Ok(total_bytes_written)
126    }
127}
128
129impl Write for Yadon {
130    fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
131        if let (None, Some(start), Some(_)) = (self.virtual_position, self.start, self.length) {
132            // If the start position is specified and this is the first operation, and we're doing length
133            // emulation, the virtual position must be initialized.
134            self.virtual_position = Some(start);
135        }
136
137        let buf = match self.length {
138            Some(max_length) => { // Emulate writing into something with a max length
139                let available_space = match self.virtual_position {
140                    Some(current_position) => (max_length - current_position) as usize,
141                    None => max_length as usize
142                };
143
144                if buf.len() > available_space {
145                    &buf[0..available_space]
146                } else {
147                    buf
148                }
149            },
150            None => {
151                buf
152            }
153        };
154
155
156        self.virtual_position = match self.virtual_position {
157            Some(current_position) => Some(current_position + buf.len() as u64),
158            None => Some(buf.len() as u64)
159        };
160
161        self.operations.push(WriteOperation::Write(buf.into(), buf.len()));
162        Ok(buf.len())
163    }
164
165    fn flush(&mut self) -> std::io::Result<()> {
166        Ok(())
167    }
168}
169
170impl Seek for Yadon {
171    fn seek(&mut self, pos: std::io::SeekFrom) -> std::io::Result<u64> {
172
173        match (self.virtual_position, pos, self.start, self.length) {
174            (_, SeekFrom::Start(from_start), _, _) => {
175                self.virtual_position = Some(from_start);
176            }
177            (None, SeekFrom::Current(from_current), Some(start_position), _) => {
178                self.virtual_position = Some((start_position as i64 + from_current) as u64);
179            }
180            (_, SeekFrom::End(from_end), _, Some(length)) => {
181                self.virtual_position = Some((length as i64 + from_end) as u64);
182            }
183            (Some(current_pos), SeekFrom::Current(from_current), _, _) => {
184                self.virtual_position = Some((current_pos as i64 + from_current) as u64)
185            }
186            (_, SeekFrom::End(_), _, None) => {
187                self.virtual_position = None; // This will return an ErrorKind::Unsupported.
188            }
189            (None, SeekFrom::Current(from_current), None, _) => {
190                self.virtual_position = Some(from_current as u64); // If a start waas not specified, assume we're at position 0.
191            }
192        }
193
194        match self.virtual_position {
195            Some(resulting_position) => {
196                self.operations.push(WriteOperation::Seek(pos, resulting_position));
197                Ok(resulting_position)
198            },
199            None => Err(std::io::ErrorKind::Unsupported.into()),
200        }
201    }
202}
203
204#[cfg(test)]
205mod tests {
206    use std::io::{Cursor, Seek, SeekFrom, Write};
207    use crate::{ApplyError, Yadon};
208
209    #[test]
210    fn delayed_write() {
211        let mut yadon = Yadon::new(Some(0), Some(16));
212        assert_eq!(yadon.seek(SeekFrom::Start(4)).unwrap(), 4);
213        assert_eq!(yadon.write(&[1,2,3]).unwrap(), 3);
214        assert_eq!(yadon.seek(SeekFrom::End(-2)).unwrap(), 14);
215        assert_eq!(yadon.write(&[4,5]).unwrap(), 2);
216        assert_eq!(yadon.seek(SeekFrom::Current(-6)).unwrap(), 10);
217        assert_eq!(yadon.write(&[6,7,8]).unwrap(), 3);
218
219        let mut target = vec![0u8; 16];
220        let mut target_writer = Cursor::new(&mut target);
221        yadon.apply(&mut target_writer, true).unwrap();
222        assert_eq!(target, &[0, 0, 0, 0, 1, 2, 3, 0, 0, 0, 6, 7, 8, 0, 4, 5]);
223    }
224
225    #[test]
226    fn start_and_end() {
227        let mut yadon = Yadon::new(Some(1), Some(4));
228        assert_eq!(yadon.seek(SeekFrom::Current(2)).unwrap(), 3);
229        yadon.write(&[1]).unwrap();
230        assert_eq!(yadon.seek(SeekFrom::End(-3)).unwrap(), 1);
231        yadon.write(&[2]).unwrap();
232
233        let mut target = vec![0u8; 4];
234        let mut target_writer = Cursor::new(&mut target);
235        yadon.apply(&mut target_writer, true).unwrap();
236        assert_eq!(target, &[0, 2, 0, 1]);
237    }
238
239    #[test]
240    fn unspecified_length_end_seek_fails() {
241        let mut yadon = Yadon::new(None, None);
242        assert_eq!(yadon.seek(SeekFrom::End(-3)).map_err(|e| e.kind()), Err(std::io::ErrorKind::Unsupported.into()));
243    }
244
245    #[test]
246    fn unspecified_start_current_seek_assumes_0() {
247        let mut yadon = Yadon::new(None, None);
248        assert_eq!(yadon.seek(SeekFrom::Current(2)).unwrap(), 2);
249    }
250
251    #[test]
252    fn too_big_write() {
253        let mut now_target = [0u8; 32];
254        let mut now = Cursor::new(&mut now_target[..]);
255        now.seek(SeekFrom::Start(1)).unwrap();
256        let mut yadon = Yadon::new(Some(1), Some(32));
257        // Too big since we start at position 1
258        assert_eq!(assert_multi_write(&mut now, &mut yadon, &[1u8; 64]).unwrap(), 31);
259        assert_multi_seek(&mut now, &mut yadon, SeekFrom::End(-16)).unwrap();
260        assert_eq!(assert_multi_write(&mut now, &mut yadon, &[2u8; 17]).unwrap(), 16);
261        assert_multi_seek(&mut now, &mut yadon, SeekFrom::Start(31)).unwrap();
262        assert_eq!(assert_multi_write(&mut now, &mut yadon, &[3u8; 2]).unwrap(), 1);
263        assert_eq!(assert_multi_write(&mut now, &mut yadon, &[4u8; 2]).unwrap(), 0);
264        assert_multi_seek(&mut now, &mut yadon, SeekFrom::End(0)).unwrap();
265        assert_eq!(assert_multi_write(&mut now, &mut yadon, &[5u8; 2]).unwrap(), 0);
266        assert_multi_write(&mut now, &mut yadon, &[1]).unwrap();
267        assert_multi_write(&mut now, &mut yadon, &[1]).unwrap();
268
269        println!("{:?}", yadon);
270        let mut later_target = vec![0u8; 32]; // Using a vec here because it can grow if we've made a mistake
271        let mut later_writer = Cursor::new(&mut later_target[..]);
272        yadon.apply(&mut later_writer, true).unwrap();
273        assert_eq!(&later_target, &now_target);
274    }
275
276    #[test]
277    fn return_values() {
278        let mut now_target = [0u8; 128];
279        let mut now = Cursor::new(&mut now_target[..]);
280        now.seek(SeekFrom::Start(27)).unwrap();
281        let mut yadon = Yadon::new(Some(27), Some(128));
282
283        assert_multi_seek(&mut now, &mut yadon, SeekFrom::Current(0)).unwrap();
284        assert_multi_seek(&mut now, &mut yadon, SeekFrom::Current(4)).unwrap();
285        assert_multi_write(&mut now, &mut yadon, &[1,2,3,4,5]).unwrap();
286        assert_multi_seek(&mut now, &mut yadon, SeekFrom::Current(-2)).unwrap();
287        assert_multi_write(&mut now, &mut yadon, &[1,2,3,4,5]).unwrap();
288        assert_multi_seek(&mut now, &mut yadon, SeekFrom::Start(27)).unwrap();
289        assert_multi_seek(&mut now, &mut yadon, SeekFrom::Current(2)).unwrap();
290        assert_multi_write(&mut now, &mut yadon, &[1,2]).unwrap();
291        assert_multi_seek(&mut now, &mut yadon, SeekFrom::End(-12)).unwrap();
292        assert_multi_write(&mut now, &mut yadon, &[12; 14]).unwrap();
293
294        let mut later_target = vec![0u8; 128]; // Using a vec here because it can grow if we've made a mistake
295        let mut later_writer = Cursor::new(&mut later_target[..]);
296        yadon.apply(&mut later_writer, true).unwrap();
297        assert_eq!(&later_target, &now_target);
298    }
299
300    #[test]
301    fn failed_apply_write_end() {
302        // mismatched sizes between yadon and the target
303        let mut yadon = Yadon::new(Some(1), Some(4));
304        assert_eq!(yadon.seek(SeekFrom::End(-3)).unwrap(), 1);
305        yadon.write(&[2]).unwrap();
306
307        let mut target = vec![0u8; 8];
308        let mut target_writer = Cursor::new(&mut target);
309        // we should be able to successfully apply this if check_return_values is false.
310        yadon.apply(&mut target_writer, false).unwrap();
311
312        // but if it's true, we should get an error
313        match yadon.apply(&mut target_writer, true) {
314            Err(ApplyError::SeekDiverged(diff)) => {
315                assert_eq!(diff.expected, 1);
316                assert_eq!(diff.actual, 5);
317            },
318            res => {
319                assert!(false, "Apply did not fail with a diverged seek: {:?}", res);
320            }
321        }
322    }
323
324    #[test]
325    fn apply_smaller_than_target() {
326        let mut yadon = Yadon::new(Some(1), Some(4));
327        yadon.write(&[0; 6]).unwrap();
328
329        let mut target = vec![0u8; 8];
330        let mut target_writer = Cursor::new(&mut target);
331        assert_eq!(yadon.apply(&mut target_writer, true).unwrap(), 3);
332    }
333
334    #[test]
335    fn failed_apply_write_too_much() {
336        // mismatched sizes between yadon and the target
337        let mut yadon = Yadon::new(Some(1), Some(8));
338        yadon.write(&[0; 6]).unwrap();
339
340        let mut target = [0u8; 4];
341        let mut target_writer = Cursor::new(&mut target[..]);
342
343        // we should be able to successfully write this if check_return_values is false.
344        yadon.apply(&mut target_writer, false).unwrap();
345
346        // but if it's true, we should get an error
347        match yadon.apply(&mut target_writer, true) {
348            Err(ApplyError::NumBytesWrittenDiverge(diff)) => {
349                assert_eq!(diff.expected, 6);
350                assert_eq!(diff.actual, 3);
351            },
352            res => {
353                assert!(false, "Apply did not fail with a diverged write: {:?}", res);
354            }
355        }
356    }
357    
358    #[test]
359    fn cannot_seek_end_without_length() {
360        // mismatched sizes between yadon and the target
361        let mut yadon = Yadon::new(Some(3), None);
362        yadon.write(&[0; 6]).unwrap();
363        assert_eq!(yadon.seek(SeekFrom::End(-2)).map_err(|e| e.kind()), Err(std::io::ErrorKind::Unsupported.into()));
364    }
365
366    fn assert_multi_write<T1, T2>(a: &mut T1, b: &mut T2, buf: &[u8]) -> std::io::Result<usize>
367    where T1: Write + Seek, T2: Write + Seek {
368        let result1 = a.write(buf);
369        let result2 = b.write(buf);
370
371        match (result1, result2) {
372            (Ok(a_bytes), Ok(b_bytes)) => {
373                println!("{}, {} written", a_bytes, b_bytes);
374                assert_eq!(a_bytes, b_bytes);
375                Ok(a_bytes)
376            },
377            (a_res, b_res) => {
378                assert!(false, "results differ: {:?} and {:?}", a_res, b_res);
379                a_res
380            }
381        }
382    }
383
384    fn assert_multi_seek<T1, T2>(a: &mut T1, b: &mut T2, pos: SeekFrom) -> std::io::Result<u64>
385    where T1: Write + Seek, T2: Write + Seek {
386        let result1 = a.seek(pos);
387        let result2 = b.seek(pos);
388
389        match (result1, result2) {
390            (Ok(a_pos), Ok(b_pos)) => {
391                println!("{}, {} seeked", a_pos, b_pos);
392                assert_eq!(a_pos, b_pos);
393                Ok(a_pos)
394            },
395            (a_res, b_res) => {
396                assert!(false, "results differ: {:?} and {:?}", a_res, b_res);
397                a_res
398            }
399        }
400    }
401}