File: test_reader.rs

package info (click to toggle)
rust-etherparse 0.13.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,464 kB
  • sloc: sh: 68; makefile: 2
file content (146 lines) | stat: -rw-r--r-- 4,180 bytes parent folder | download
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
use std::io;

/// A reader that also throws an error when a seek
/// to a non existing position is executed (not normally the behavior).
///
/// Note that this is not the default behavior of seek
/// but it is needed for testing purposes.
pub struct TestReader {
    data: Vec<u8>,
    cur_offset: usize
}

impl TestReader {
    /// Creates a reader with the given data
    pub fn new(data: &[u8]) -> TestReader {
        TestReader{
            data: {
                let mut v = Vec::with_capacity(data.len());
                v.extend_from_slice(data);
                v
            },
            cur_offset: 0
        }
    }

    /// Current offset from the start.
    pub fn cur_offset(&self) -> usize {
        self.cur_offset
    }
}

impl io::Read for TestReader {
    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
        if buf.len() > self.data.len() - self.cur_offset {
            Err(io::Error::new(io::ErrorKind::UnexpectedEof, "failed to fill whole buffer"))
        } else {
            buf.clone_from_slice(
                &self.data[
                    self.cur_offset..(self.cur_offset + buf.len())
                ]
            );
            self.cur_offset += buf.len();
            Ok(buf.len())
        }
    }
}

impl io::Seek for TestReader{
    fn seek(&mut self, pos: io::SeekFrom) -> io::Result<u64> {
        use io::SeekFrom::*;

        let new_offset = match pos {
            Start(start_offset) => start_offset as i64,
            End(end_offset) => (self.data.len() as i64) + end_offset,
            Current(offset) => (self.cur_offset as i64) + offset,
        };

        if new_offset < 0 {
            Err(
                io::Error::new(
                    io::ErrorKind::InvalidInput,
                    "invalid seek to a negative or overflowing position",
                )
            )
        } else if new_offset > (self.data.len() as i64) {
            // Note this is not default behavior but is usefull for
            // testing. Normally a seek over the end is allowed.
            Err(
                io::Error::new(
                    io::ErrorKind::InvalidInput,
                    "invalid seek to a negative or overflowing position",
                )
            )
        } else {
            self.cur_offset = new_offset as usize;
            Ok(self.cur_offset as u64)
        }
    }
}

#[test]
fn read() {
    use io::Read;
    {
        let mut reader = TestReader::new(&[1,2,3,4]);
        {
            let mut tar: [u8;4] = [0;4];
            assert_eq!(4, reader.read(&mut tar).unwrap());
            assert_eq!(&tar[..], &[1,2,3,4]);
        }
        {
            let mut tar: [u8;1] = [0];
            assert_eq!(
                io::ErrorKind::UnexpectedEof,
                reader.read(&mut tar).unwrap_err().kind()
            );
        }
    }
}

#[test]
fn seek() {
    use io::Seek;
    use io::SeekFrom::*;
    // ok seeks
    {
        let mut reader = TestReader::new(&[1,2,3,4]);
        assert_eq!(2, reader.seek(Start(2)).unwrap());
        assert_eq!(reader.cur_offset(), 2);
        assert_eq!(3, reader.seek(Current(1)).unwrap());
        assert_eq!(3, reader.cur_offset());
        assert_eq!(1, reader.seek(End(-3)).unwrap());
        assert_eq!(1, reader.cur_offset());
    }
    // bad seeks
    {
        let mut reader = TestReader::new(&[1,2,3,4]);
        assert_eq!(
            io::ErrorKind::InvalidInput,
            reader.seek(Start(5)).unwrap_err().kind()
        );
    }
    {
        let mut reader = TestReader::new(&[1,2,3,4]);
        reader.seek(Start(2)).unwrap();
        assert_eq!(
            io::ErrorKind::InvalidInput,
            reader.seek(Current(3)).unwrap_err().kind()
        );
        assert_eq!(
            io::ErrorKind::InvalidInput,
            reader.seek(Current(-3)).unwrap_err().kind()
        );
    }
    {
        let mut reader = TestReader::new(&[1,2,3,4]);
        assert_eq!(
            io::ErrorKind::InvalidInput,
            reader.seek(End(-5)).unwrap_err().kind()
        );
        assert_eq!(
            io::ErrorKind::InvalidInput,
            reader.seek(End(1)).unwrap_err().kind()
        );
    }
}