Skip to main content

osdp/reply/
crauth.rs

1//! `osdp_CRAUTHR` (`0x82`) — challenge response.
2//!
3//! # Spec: §7.22
4
5use crate::error::Error;
6use alloc::vec::Vec;
7
8/// `osdp_CRAUTHR` body.
9#[derive(Debug, Clone, PartialEq, Eq)]
10pub struct CrAuthR {
11    /// Challenge response payload.
12    pub response: Vec<u8>,
13}
14
15impl CrAuthR {
16    /// Encode.
17    pub fn encode(&self) -> Result<Vec<u8>, Error> {
18        Ok(self.response.clone())
19    }
20
21    /// Decode.
22    pub fn decode(data: &[u8]) -> Result<Self, Error> {
23        Ok(Self {
24            response: data.to_vec(),
25        })
26    }
27}
28
29#[cfg(test)]
30mod tests {
31    use super::*;
32
33    #[test]
34    fn roundtrip() {
35        let body = CrAuthR {
36            response: alloc::vec![0xDE, 0xAD, 0xBE, 0xEF],
37        };
38        let bytes = body.encode().unwrap();
39        assert_eq!(bytes, [0xDE, 0xAD, 0xBE, 0xEF]);
40        assert_eq!(CrAuthR::decode(&bytes).unwrap(), body);
41    }
42
43    #[test]
44    fn empty_response_is_valid() {
45        assert_eq!(
46            CrAuthR::decode(&[]).unwrap(),
47            CrAuthR {
48                response: Vec::new()
49            }
50        );
51    }
52}