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
use super::shared_internal::*;
use failure::Error;
use futures_io::AsyncRead;

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AuthMethodNegotiationRequest {
    pub methods: Vec<u8>,
}

impl AuthMethodNegotiationRequest {
    pub async fn read<AR>(reader: &mut AR) -> Result<Self, Error>
    where
        AR: AsyncRead + Unpin,
    {
        read_version(reader).await?;

        let nmethods = read_u8(reader).await?;
        let methods = read_vec(reader, nmethods as usize).await?;

        Ok(Self { methods })
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use crate::test_util::*;
    use futures_util::io::Cursor;

    #[test]
    fn happy_path() {
        let mut reader = Cursor::new(&[
            5, // Version
            2, // Auth Methods Number
            0xBE, 0xEF, // The methods
        ]);
        let future = AuthMethodNegotiationRequest::read(&mut reader);
        let result = extract_future_output(future);
        assert_eq!(
            result.unwrap(),
            AuthMethodNegotiationRequest {
                methods: vec![0xBE, 0xEF]
            }
        )
    }

    #[test]
    fn invalid_version() {
        let mut reader = Cursor::new(&[1]);
        let future = AuthMethodNegotiationRequest::read(&mut reader);
        let result = extract_future_output(future);
        let err = result.unwrap_err();
        let err = err
            .downcast::<crate::error::InvalidProtocolVersionError>()
            .unwrap();
        assert_eq!(err, crate::error::InvalidProtocolVersionError(1))
    }

    #[test]
    fn not_enough_data() {
        let mut reader = Cursor::new(&[]);
        let future = AuthMethodNegotiationRequest::read(&mut reader);
        let result = extract_future_output(future);
        let err = result.unwrap_err();
        let err = err.downcast::<std::io::Error>().unwrap();
        assert_eq!(err.kind(), std::io::ErrorKind::UnexpectedEof);
    }
}