-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcustom.rs
More file actions
82 lines (68 loc) · 2.44 KB
/
custom.rs
File metadata and controls
82 lines (68 loc) · 2.44 KB
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
use binary_codec::{FromBytes, ToBytes};
use serde::{Deserialize, Serialize};
use serde_with::base64::{Base64, UrlSafe};
use serde_with::formats::Unpadded;
use serde_with::serde_as;
/// Custom request/response body for sub-protocols, containing a protocol ID and raw data
#[serde_as]
#[derive(Debug, FromBytes, ToBytes, Serialize, Deserialize, PartialEq, Clone)]
pub struct CustomBody {
/// Protocol ID to identify the sub-protocol this body belongs to
pub protocol: u16,
/// Raw data for the sub-protocol, which can be parsed according to the protocol ID
/// Represented as base64url (no padding) in TOML/JSON.
#[serde_as(as = "Base64<UrlSafe, Unpadded>")]
pub data: Vec<u8>,
}
#[cfg(test)]
mod tests {
use binary_codec::{BinaryDeserializer, BinarySerializer};
use crate::packets::{request::PlabbleRequestPacket, response::PlabbleResponsePacket};
#[test]
fn can_serialize_and_deserialize_custom_request() {
let packet: PlabbleRequestPacket = toml::from_str(
r#"
version = 1
[header]
packet_type = "Custom"
flag1 = true
flag2 = false
flag3 = true
flag4 = false
[body]
protocol = 42
data = "AQIDBAU"
"#,
)
.unwrap();
let serialized = packet.to_bytes(None).unwrap();
assert_eq!("01011110", format!("{:08b}", serialized[1]));
assert_eq!("015e002a0102030405", hex::encode(&serialized));
let deserialized = PlabbleRequestPacket::from_bytes(&serialized, None).unwrap();
assert_eq!(packet, deserialized);
}
#[test]
fn can_serialize_and_deserialize_custom_response() {
let packet: PlabbleResponsePacket = toml::from_str(
r#"
version = 1
[header]
packet_type = "Custom"
request_counter = 7
flag1 = true
flag2 = false
flag3 = true
flag4 = false
[body]
protocol = 42
data = "AQIDBAU"
"#,
)
.unwrap();
let serialized = packet.to_bytes(None).unwrap();
assert_eq!("01011110", format!("{:08b}", serialized[1]));
assert_eq!("015e0007002a0102030405", hex::encode(&serialized));
let deserialized = PlabbleResponsePacket::from_bytes(&serialized, None).unwrap();
assert_eq!(packet, deserialized);
}
}