-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rs
182 lines (148 loc) · 5.5 KB
/
main.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
fn main() {
println!("Run the tests with `cargo test`");
}
pub mod example3 {
include!(concat!(env!("OUT_DIR"), "/example3.rs"));
}
pub mod example3optional {
include!(concat!(env!("OUT_DIR"), "/example3optional.rs"));
}
pub mod example2 {
include!(concat!(env!("OUT_DIR"), "/example2.rs"));
}
#[cfg(test)]
mod tests {
use std::{collections::VecDeque, io::Read, vec};
use super::*;
use bytes::Bytes;
use prost::Message;
#[test]
fn empty_bytes() {
let hs2 = example2::NoiseHandshakePayload {
identity_key: Some(vec![]),
identity_sig: Some(vec![]),
extensions: None,
};
let b2 = hs2.encode_to_vec();
let hs3 = example3::NoiseHandshakePayload {
identity_key: vec![],
identity_sig: vec![],
extensions: None,
};
let b3 = hs3.encode_to_vec();
// These are different on the wire
assert_ne!(b2, b3);
assert_eq!(b2, vec![10, 0, 18, 0]);
assert_eq!(b3, vec![]);
// But we should be able to interpret them regardless of proto2 vs proto3
let b2to3 = example3::NoiseHandshakePayload::decode(bytes::Bytes::from(b2))
.expect("should decode bytes from 2");
assert_eq!(b2to3, hs3);
let b3to2 = example2::NoiseHandshakePayload::decode(bytes::Bytes::from(b3))
.expect("should decode bytes from 3");
// proto2 code will think this was unset since it was the default val (empty byte vec)
assert_eq!(b3to2, example2::NoiseHandshakePayload::default());
}
#[test]
fn none_vals() {
let hs2 = example2::NoiseHandshakePayload {
identity_key: None,
identity_sig: None,
extensions: None,
};
let b2 = hs2.encode_to_vec();
let hs3 = example3::NoiseHandshakePayload {
identity_key: vec![],
identity_sig: vec![],
extensions: None,
};
let b3 = hs3.encode_to_vec();
// These are the same on the wire
assert_eq!(b2, b3);
// But we should be able to interpret them regardless of proto2 vs proto3
let b2to3 = example3::NoiseHandshakePayload::decode(bytes::Bytes::from(b2))
.expect("should decode bytes from 2");
assert_eq!(b2to3, hs3);
let b3to2 = example2::NoiseHandshakePayload::decode(bytes::Bytes::from(b3))
.expect("should decode bytes from 3");
assert_eq!(b3to2, hs2);
}
#[test]
fn some_vals() {
let hs2 = example2::NoiseHandshakePayload {
identity_key: Some(vec![1, 2, 3]),
identity_sig: Some(vec![4, 5, 6]),
extensions: Some(example2::NoiseExtensions {
webtransport_certhashes: vec![vec![0, 1, 2]],
}),
};
let b2 = hs2.encode_to_vec();
let hs3 = example3::NoiseHandshakePayload {
identity_key: vec![1, 2, 3],
identity_sig: vec![4, 5, 6],
extensions: Some(example3::NoiseExtensions {
webtransport_certhashes: vec![vec![0, 1, 2]],
}),
};
let b3 = hs3.encode_to_vec();
// These are the same on the wire
assert_eq!(b2, b3);
// But we should be able to interpret them regardless of proto2 vs proto3
let b2to3 = example3::NoiseHandshakePayload::decode(bytes::Bytes::from(b2))
.expect("should decode bytes from 2");
assert_eq!(b2to3, hs3);
let b3to2 = example2::NoiseHandshakePayload::decode(bytes::Bytes::from(b3))
.expect("should decode bytes from 3");
assert_eq!(b3to2, hs2);
}
#[test]
fn empty_bytes_proto3_optional() {
let hs2 = example2::NoiseHandshakePayload {
identity_key: Some(vec![]),
identity_sig: Some(vec![]),
extensions: None,
};
let b2 = hs2.encode_to_vec();
let hs3 = example3optional::NoiseHandshakePayload {
identity_key: Some(vec![]),
identity_sig: Some(vec![]),
extensions: None,
};
let b3 = hs3.encode_to_vec();
// These are the same on the wire
assert_eq!(b2, b3);
assert_eq!(b2, vec![10, 0, 18, 0]);
// Round trip is fine
let b2to3 = example3optional::NoiseHandshakePayload::decode(bytes::Bytes::from(b2))
.expect("should decode bytes from 2");
assert_eq!(b2to3, hs3);
let b3to2 = example2::NoiseHandshakePayload::decode(bytes::Bytes::from(b3))
.expect("should decode bytes from 3");
assert_eq!(b3to2, hs2);
}
#[test]
fn unset_bytes_proto3_optional() {
let hs2 = example2::NoiseHandshakePayload {
identity_key: None,
identity_sig: None,
extensions: None,
};
let b2 = hs2.encode_to_vec();
let hs3 = example3optional::NoiseHandshakePayload {
identity_key: None,
identity_sig: None,
extensions: None,
};
let b3 = hs3.encode_to_vec();
// These are the same on the wire
assert_eq!(b2, b3);
assert_eq!(b2, vec![]);
// Round trip is fine
let b2to3 = example3optional::NoiseHandshakePayload::decode(bytes::Bytes::from(b2))
.expect("should decode bytes from 2");
assert_eq!(b2to3, hs3);
let b3to2 = example2::NoiseHandshakePayload::decode(bytes::Bytes::from(b3))
.expect("should decode bytes from 3");
assert_eq!(b3to2, hs2);
}
}