-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathtest_utils.rs
142 lines (124 loc) · 3.6 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#![allow(unused)]
use asn1rs::prelude::basic::DER;
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, bad-hex: {:02x?}",
&serialized.1[..]
);
assert_eq!(
uper,
&deserialize_uper::<T>(data, bits),
"Deserialized data struct does not match"
);
}
pub fn serialize_der(to_der: &impl Writable) -> Vec<u8> {
let mut writer = DER::writer(Vec::new());
writer.write(to_der).unwrap();
writer.into_inner()
}
pub fn deserialize_der<T: Readable>(data: &[u8]) -> T {
let mut reader = DER::reader(data);
let result = reader.read::<T>().unwrap();
assert_eq!(
0,
reader.into_inner().len(),
"After reading, there are still bytes remaining!"
);
result
}
pub fn serialize_and_deserialize_der<T: Readable + Writable + std::fmt::Debug + PartialEq>(
data: &[u8],
value: &T,
) {
let serialized = serialize_der(value);
assert_eq!(
data,
&serialized[..],
"Serialized binary data does not match, bad-hex: {:02x?}",
&serialized[..]
);
assert_eq!(
value,
&deserialize_der::<T>(data),
"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();
let len_written = writer2.len_written();
let as_bytes_vec = writer2.as_bytes().to_vec();
let into_bytes_vec = writer2.into_bytes_vec();
assert_eq!(
&vec[..],
&vec2[..],
"ProtobufWriter output differs between Vec<u8> and &mut [u8] backend"
);
assert_eq!(
&vec[..],
&as_bytes_vec[..],
"ProtobufWriter::as_bytes returns wrong byte slice"
);
assert_eq!(
&vec[..],
&into_bytes_vec[..],
"ProtobufWriter::into_bytes_vec returns wrong vec"
);
assert_eq!(
vec.len(),
len_written,
"ProtobufWriter::len_written returns wrong value"
);
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<T: Readable + Writable + std::fmt::Debug + PartialEq>(
data: &[u8],
proto: &T,
) {
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"
);
}