-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathtest_utils.rs
108 lines (95 loc) · 2.9 KB
/
test_utils.rs
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#![allow(unused)]
pub use asn1rs::prelude::*;
pub fn serialize_uper(to_uper: &impl Writable) -> (usize, Vec<u8>) {
let mut writer = UperWriter::default();
writer.write(to_uper).unwrap();
let bits = writer.bit_len();
(bits, writer.into_bytes_vec())
}
pub fn deserialize_uper<T: Readable>(data: &[u8], bits: usize) -> T {
let mut reader = UperReader::from((data, bits));
let result = reader.read::<T>().unwrap();
assert_eq!(
0,
reader.bits_remaining(),
"After reading, there are still bits remaining!"
);
result
}
pub fn serialize_and_deserialize_uper<T: Readable + Writable + std::fmt::Debug + PartialEq>(
bits: usize,
data: &[u8],
uper: &T,
) {
let serialized = serialize_uper(uper);
assert_eq!(
(bits, data),
(serialized.0, &serialized.1[..]),
"Serialized binary data does not match"
);
assert_eq!(
uper,
&deserialize_uper::<T>(data, bits),
"Deserialized data struct does not match"
);
}
#[cfg(feature = "protobuf")]
pub fn serialize_protobuf(to_protobuf: &impl Writable) -> Vec<u8> {
let mut writer = ProtobufWriter::default();
writer.write(to_protobuf).unwrap();
let vec = writer.into_bytes_vec();
let mut vec2 = vec![0u8; vec.len()];
let mut writer2 = ProtobufWriter::from(&mut vec2[..]);
writer2.write(to_protobuf).unwrap();
assert_eq!(
&vec[..],
&vec2[..],
"ProtobufWriter output differs between Vec<u8> and &mut [u8] backend"
);
vec
}
#[cfg(feature = "protobuf")]
pub fn deserialize_protobuf<T: Readable>(data: &[u8]) -> T {
let mut reader = ProtobufReader::from(data);
T::read(&mut reader).unwrap()
}
#[cfg(feature = "protobuf")]
pub fn serialize_and_deserialize_protobuf<
#[cfg(feature = "legacy-protobuf-codegen")] T: Readable + Writable + std::fmt::Debug + PartialEq + asn1rs::io::protobuf::Protobuf,
#[cfg(not(feature = "legacy-protobuf-codegen"))] T: Readable + Writable + std::fmt::Debug + PartialEq,
>(
data: &[u8],
proto: &T,
) {
#[cfg(feature = "legacy-protobuf-codegen")]
legacy_protobuf_serialize_check(data, proto);
let serialized = serialize_protobuf(proto);
assert_eq!(
data,
&serialized[..],
"Serialized binary data does not match"
);
assert_eq!(
proto,
&deserialize_protobuf::<T>(data),
"Deserialized data struct does not match"
);
}
#[cfg(all(feature = "protobuf", feature = "legacy-protobuf-codegen"))]
pub fn legacy_protobuf_serialize_check<
T: asn1rs::io::protobuf::Protobuf + std::fmt::Debug + PartialEq,
>(
data: &[u8],
proto: &T,
) {
// legacy writer
let mut vec: Vec<u8> = Vec::default();
proto
.write_protobuf(&mut vec)
.expect("Legacy serializer failed");
assert_eq!(
&vec[..],
data,
"Given binary data does not match output of legacy serializer"
);
}