about summary refs log tree commit diff
path: root/tvix/nix-compat/src/nix_daemon/de/mock.rs
blob: 31cc3a4897baa77148e30b5319fbfe5f89a32480 (plain) (blame)
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
use std::collections::VecDeque;
use std::fmt;
use std::io;
use std::thread;

use bytes::Bytes;
use thiserror::Error;

use crate::nix_daemon::ProtocolVersion;

use super::NixRead;

#[derive(Debug, Error, PartialEq, Eq, Clone)]
pub enum Error {
    #[error("custom error '{0}'")]
    Custom(String),
    #[error("invalid data '{0}'")]
    InvalidData(String),
    #[error("missing data '{0}'")]
    MissingData(String),
    #[error("IO error {0} '{1}'")]
    IO(io::ErrorKind, String),
    #[error("wrong read: expected {0} got {1}")]
    WrongRead(OperationType, OperationType),
}

impl Error {
    pub fn expected_read_number() -> Error {
        Error::WrongRead(OperationType::ReadNumber, OperationType::ReadBytes)
    }

    pub fn expected_read_bytes() -> Error {
        Error::WrongRead(OperationType::ReadBytes, OperationType::ReadNumber)
    }
}

impl super::Error for Error {
    fn custom<T: fmt::Display>(msg: T) -> Self {
        Self::Custom(msg.to_string())
    }

    fn io_error(err: std::io::Error) -> Self {
        Self::IO(err.kind(), err.to_string())
    }

    fn invalid_data<T: fmt::Display>(msg: T) -> Self {
        Self::InvalidData(msg.to_string())
    }

    fn missing_data<T: fmt::Display>(msg: T) -> Self {
        Self::MissingData(msg.to_string())
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum OperationType {
    ReadNumber,
    ReadBytes,
}

impl fmt::Display for OperationType {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::ReadNumber => write!(f, "read_number"),
            Self::ReadBytes => write!(f, "read_bytess"),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
enum Operation {
    ReadNumber(Result<u64, Error>),
    ReadBytes(Result<Bytes, Error>),
}

impl From<Operation> for OperationType {
    fn from(value: Operation) -> Self {
        match value {
            Operation::ReadNumber(_) => OperationType::ReadNumber,
            Operation::ReadBytes(_) => OperationType::ReadBytes,
        }
    }
}

pub struct Builder {
    version: ProtocolVersion,
    ops: VecDeque<Operation>,
}

impl Builder {
    pub fn new() -> Builder {
        Builder {
            version: Default::default(),
            ops: VecDeque::new(),
        }
    }

    pub fn version<V: Into<ProtocolVersion>>(&mut self, version: V) -> &mut Self {
        self.version = version.into();
        self
    }

    pub fn read_number(&mut self, value: u64) -> &mut Self {
        self.ops.push_back(Operation::ReadNumber(Ok(value)));
        self
    }

    pub fn read_number_error(&mut self, err: Error) -> &mut Self {
        self.ops.push_back(Operation::ReadNumber(Err(err)));
        self
    }

    pub fn read_bytes(&mut self, value: Bytes) -> &mut Self {
        self.ops.push_back(Operation::ReadBytes(Ok(value)));
        self
    }

    pub fn read_slice(&mut self, data: &[u8]) -> &mut Self {
        let value = Bytes::copy_from_slice(data);
        self.ops.push_back(Operation::ReadBytes(Ok(value)));
        self
    }

    pub fn read_bytes_error(&mut self, err: Error) -> &mut Self {
        self.ops.push_back(Operation::ReadBytes(Err(err)));
        self
    }

    pub fn build(&mut self) -> Mock {
        Mock {
            version: self.version,
            ops: self.ops.clone(),
        }
    }
}

impl Default for Builder {
    fn default() -> Self {
        Self::new()
    }
}

pub struct Mock {
    version: ProtocolVersion,
    ops: VecDeque<Operation>,
}

impl NixRead for Mock {
    type Error = Error;

    fn version(&self) -> ProtocolVersion {
        self.version
    }

    async fn try_read_number(&mut self) -> Result<Option<u64>, Self::Error> {
        match self.ops.pop_front() {
            Some(Operation::ReadNumber(ret)) => ret.map(Some),
            Some(Operation::ReadBytes(_)) => Err(Error::expected_read_bytes()),
            None => Ok(None),
        }
    }

    async fn try_read_bytes_limited(
        &mut self,
        _limit: std::ops::RangeInclusive<usize>,
    ) -> Result<Option<Bytes>, Self::Error> {
        match self.ops.pop_front() {
            Some(Operation::ReadBytes(ret)) => ret.map(Some),
            Some(Operation::ReadNumber(_)) => Err(Error::expected_read_number()),
            None => Ok(None),
        }
    }
}

impl Drop for Mock {
    fn drop(&mut self) {
        // No need to panic again
        if thread::panicking() {
            return;
        }
        if let Some(op) = self.ops.front() {
            panic!("reader dropped with {op:?} operation still unread")
        }
    }
}

#[cfg(test)]
mod test {
    use bytes::Bytes;
    use hex_literal::hex;

    use crate::nix_daemon::de::NixRead;

    use super::{Builder, Error};

    #[tokio::test]
    async fn read_slice() {
        let mut mock = Builder::new()
            .read_number(10)
            .read_slice(&[])
            .read_slice(&hex!("0000 1234 5678 9ABC DEFF"))
            .build();
        assert_eq!(10, mock.read_number().await.unwrap());
        assert_eq!(&[] as &[u8], &mock.read_bytes().await.unwrap()[..]);
        assert_eq!(
            &hex!("0000 1234 5678 9ABC DEFF"),
            &mock.read_bytes().await.unwrap()[..]
        );
        assert_eq!(None, mock.try_read_number().await.unwrap());
        assert_eq!(None, mock.try_read_bytes().await.unwrap());
    }

    #[tokio::test]
    async fn read_bytes() {
        let mut mock = Builder::new()
            .read_number(10)
            .read_bytes(Bytes::from_static(&[]))
            .read_bytes(Bytes::from_static(&hex!("0000 1234 5678 9ABC DEFF")))
            .build();
        assert_eq!(10, mock.read_number().await.unwrap());
        assert_eq!(&[] as &[u8], &mock.read_bytes().await.unwrap()[..]);
        assert_eq!(
            &hex!("0000 1234 5678 9ABC DEFF"),
            &mock.read_bytes().await.unwrap()[..]
        );
        assert_eq!(None, mock.try_read_number().await.unwrap());
        assert_eq!(None, mock.try_read_bytes().await.unwrap());
    }

    #[tokio::test]
    async fn read_number() {
        let mut mock = Builder::new().read_number(10).build();
        assert_eq!(10, mock.read_number().await.unwrap());
        assert_eq!(None, mock.try_read_number().await.unwrap());
        assert_eq!(None, mock.try_read_bytes().await.unwrap());
    }

    #[tokio::test]
    async fn expect_number() {
        let mut mock = Builder::new().read_number(10).build();
        assert_eq!(
            Error::expected_read_number(),
            mock.read_bytes().await.unwrap_err()
        );
    }

    #[tokio::test]
    async fn expect_bytes() {
        let mut mock = Builder::new().read_slice(&[]).build();
        assert_eq!(
            Error::expected_read_bytes(),
            mock.read_number().await.unwrap_err()
        );
    }

    #[test]
    #[should_panic]
    fn operations_left() {
        let _ = Builder::new().read_number(10).build();
    }
}