tox_packet/onion/
friend_request.rs

1/*! FriendRequest packet
2*/
3
4use super::*;
5use std::str;
6use crate::toxid::{NoSpam, NOSPAMBYTES};
7
8const MAX_FRIEND_REQUEST_MSG_SIZE: usize = MAX_ONION_CLIENT_DATA_SIZE - (1 + NOSPAMBYTES);
9
10/** Friend request that can be enclosed in onion data packet and sent through onion
11path.
12
13Length    | Content
14--------- | -------------------------
15`1`       | `0x20`
16`4`       | NoSpam
17`1..991`  | Message
18
19*/
20#[derive(Clone, Debug, Eq, PartialEq)]
21pub struct FriendRequest {
22    nospam: NoSpam,
23    msg: String,
24}
25
26impl FriendRequest {
27    /// Create new object
28    pub fn new(nospam: NoSpam, msg: String) -> Self {
29        FriendRequest {
30            nospam,
31            msg,
32        }
33    }
34}
35
36impl ToBytes for FriendRequest {
37    fn to_bytes<'a>(&self, buf: (&'a mut [u8], usize)) -> Result<(&'a mut [u8], usize), GenError> {
38        do_gen!(buf,
39            gen_be_u8!(0x20) >>
40            gen_slice!(self.nospam.0) >>
41            gen_cond!(self.msg.len() > MAX_FRIEND_REQUEST_MSG_SIZE || self.msg.is_empty(), |buf| gen_error(buf, 0)) >>
42            gen_slice!(self.msg.as_bytes())
43        )
44    }
45}
46
47impl FromBytes for FriendRequest {
48    named!(from_bytes<FriendRequest>, do_parse!(
49        tag!(&[0x20][..]) >>
50        nospam: call!(NoSpam::from_bytes) >>
51        msg: map_res!(verify!(rest, |msg: &[u8]| msg.len() <= MAX_FRIEND_REQUEST_MSG_SIZE && !msg.is_empty()), str::from_utf8) >>
52        (FriendRequest {
53            nospam,
54            msg: msg.to_string(),
55        })
56    ));
57}
58
59#[cfg(test)]
60mod tests {
61    use super::*;
62
63    encode_decode_test!(
64        tox_crypto::crypto_init().unwrap(),
65        friend_req_encode_decode,
66        FriendRequest {
67            nospam: NoSpam::random(),
68            msg: "1234".to_owned(),
69        }
70    );
71
72    #[test]
73    fn friend_request_from_bytes_encoding_error() {
74        let err_string = vec![0, 159, 146, 150]; // not UTF8 bytes.
75        let nospam = NoSpam::random();
76
77        let mut friend_req = vec![0x20];
78        friend_req.extend_from_slice(&nospam.0);
79        friend_req.extend(err_string);
80        assert!(FriendRequest::from_bytes(&friend_req).is_err());
81    }
82
83    #[test]
84    fn friend_request_from_bytes_overflow() {
85        let large_string = vec![32; MAX_FRIEND_REQUEST_MSG_SIZE + 1];
86        let nospam = NoSpam::random();
87
88        let mut friend_req = vec![0x20];
89        friend_req.extend_from_slice(&nospam.0);
90        friend_req.extend(large_string);
91        assert!(FriendRequest::from_bytes(&friend_req).is_err());
92    }
93
94    #[test]
95    fn friend_request_to_bytes_overflow() {
96        let large_string = String::from_utf8(vec![32u8; MAX_FRIEND_REQUEST_MSG_SIZE + 1]).unwrap();
97        let friend_req = FriendRequest {
98            nospam: NoSpam::random(),
99            msg: large_string
100        };
101        let mut buf = [0; MAX_ONION_CLIENT_DATA_SIZE + 1]; // `1` is to provide enough space for success of serializing
102        assert!(friend_req.to_bytes((&mut buf, 0)).is_err());
103    }
104
105    #[test]
106    fn friend_request_from_bytes_underflow() {
107        let nospam = NoSpam::random();
108
109        let mut friend_req = vec![0x20];
110        friend_req.extend_from_slice(&nospam.0);
111        assert!(FriendRequest::from_bytes(&friend_req).is_err());
112    }
113
114    #[test]
115    fn friend_request_to_bytes_underflow() {
116        let friend_req = FriendRequest {
117            nospam: NoSpam::random(),
118            msg: "".to_string(),
119        };
120        let mut buf = [0; MAX_ONION_CLIENT_DATA_SIZE]; // `1` is to provide enough space for success of serializing
121        assert!(friend_req.to_bytes((&mut buf, 0)).is_err());
122    }
123}