cassandra_protocol/frame/
message_authenticate.rs

1use super::Serialize;
2use crate::error;
3use crate::frame::{FromCursor, Version};
4use crate::types::{from_cursor_str, serialize_str};
5use std::io::Cursor;
6
7/// A server authentication challenge.
8#[derive(Debug, PartialEq, Ord, PartialOrd, Eq, Hash, Clone)]
9pub struct BodyResAuthenticate {
10    pub data: String,
11}
12
13impl Serialize for BodyResAuthenticate {
14    fn serialize(&self, cursor: &mut Cursor<&mut Vec<u8>>, version: Version) {
15        serialize_str(cursor, &self.data, version);
16    }
17}
18
19impl FromCursor for BodyResAuthenticate {
20    fn from_cursor(
21        cursor: &mut Cursor<&[u8]>,
22        _version: Version,
23    ) -> error::Result<BodyResAuthenticate> {
24        Ok(BodyResAuthenticate {
25            data: from_cursor_str(cursor)?.to_string(),
26        })
27    }
28}
29
30#[cfg(test)]
31mod tests {
32    use super::*;
33    use crate::frame::traits::FromCursor;
34    use crate::frame::Version;
35    use std::io::Cursor;
36
37    #[test]
38    fn body_res_authenticate() {
39        // string "abcde"
40        let bytes = [0, 5, 97, 98, 99, 100, 101];
41        let expected = BodyResAuthenticate {
42            data: "abcde".into(),
43        };
44
45        {
46            let mut cursor: Cursor<&[u8]> = Cursor::new(&bytes);
47            let auth = BodyResAuthenticate::from_cursor(&mut cursor, Version::V4).unwrap();
48            assert_eq!(auth, expected);
49        }
50
51        {
52            let mut buffer = Vec::new();
53            let mut cursor = Cursor::new(&mut buffer);
54            expected.serialize(&mut cursor, Version::V4);
55            assert_eq!(buffer, bytes);
56        }
57    }
58}