Debian Codegraph @

third_party/rust/moz_cbor/src/test_serializer.rs

raw
1 use std::collections::BTreeMap;
2 use CborType;
3
4 #[test]
5 fn test_nint() {
6 struct Testcase {
7 value: i64,
8 expected: Vec<u8>,
9 }
10 let testcases: Vec<Testcase> = vec![
11 Testcase {
12 value: -1,
13 expected: vec![0x20],
14 },
15 Testcase {
16 value: -10,
17 expected: vec![0x29],
18 },
19 Testcase {
20 value: -100,
21 expected: vec![0x38, 0x63],
22 },
23 Testcase {
24 value: -1000,
25 expected: vec![0x39, 0x03, 0xe7],
26 },
27 Testcase {
28 value: -1000000,
29 expected: vec![0x3a, 0x00, 0x0f, 0x42, 0x3f],
30 },
31 Testcase {
32 value: -4611686018427387903,
33 expected: vec![0x3b, 0x3f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe],
34 },
35 ];
36 for testcase in testcases {
37 let cbor = CborType::SignedInteger(testcase.value);
38 assert_eq!(testcase.expected, cbor.serialize());
39 }
40 }
41
42 #[test]
43 fn test_bstr() {
44 struct Testcase {
45 value: Vec<u8>,
46 expected: Vec<u8>,
47 }
48 let testcases: Vec<Testcase> = vec![
49 Testcase {
50 value: vec![],
51 expected: vec![0x40],
52 },
53 Testcase {
54 value: vec![0x01, 0x02, 0x03, 0x04],
55 expected: vec![0x44, 0x01, 0x02, 0x03, 0x04],
56 },
57 Testcase {
58 value: vec![
59 0xaf, 0xaf, 0xaf, 0xaf, 0xaf, 0xaf, 0xaf, 0xaf, 0xaf, 0xaf, 0xaf, 0xaf, 0xaf, 0xaf,
60 0xaf, 0xaf, 0xaf, 0xaf, 0xaf, 0xaf, 0xaf, 0xaf, 0xaf, 0xaf, 0xaf,
61 ],
62 expected: vec![
63 0x58, 0x19, 0xaf, 0xaf, 0xaf, 0xaf, 0xaf, 0xaf, 0xaf, 0xaf, 0xaf, 0xaf, 0xaf, 0xaf,
64 0xaf, 0xaf, 0xaf, 0xaf, 0xaf, 0xaf, 0xaf, 0xaf, 0xaf, 0xaf, 0xaf, 0xaf, 0xaf,
65 ],
66 },
67 ];
68 for testcase in testcases {
69 let cbor = CborType::Bytes(testcase.value);
70 assert_eq!(testcase.expected, cbor.serialize());
71 }
72 }
73
74 #[test]
75 fn test_tstr() {
76 struct Testcase {
77 value: String,
78 expected: Vec<u8>,
79 }
80 let testcases: Vec<Testcase> = vec![
81 Testcase {
82 value: String::new(),
83 expected: vec![0x60],
84 },
85 Testcase {
86 value: String::from("a"),
87 expected: vec![0x61, 0x61],
88 },
89 Testcase {
90 value: String::from("IETF"),
91 expected: vec![0x64, 0x49, 0x45, 0x54, 0x46],
92 },
93 Testcase {
94 value: String::from("\"\\"),
95 expected: vec![0x62, 0x22, 0x5c],
96 },
97 Testcase {
98 value: String::from("水"),
99 expected: vec![0x63, 0xe6, 0xb0, 0xb4],
100 },
101 ];
102 for testcase in testcases {
103 let cbor = CborType::String(testcase.value);
104 assert_eq!(testcase.expected, cbor.serialize());
105 }
106 }
107
108 #[test]
109 fn test_arr() {
110 struct Testcase {
111 value: Vec<CborType>,
112 expected: Vec<u8>,
113 }
114 let nested_arr_1 = vec![CborType::Integer(2), CborType::Integer(3)];
115 let nested_arr_2 = vec![CborType::Integer(4), CborType::Integer(5)];
116 let testcases: Vec<Testcase> = vec![
117 Testcase {
118 value: vec![],
119 expected: vec![0x80],
120 },
121 Testcase {
122 value: vec![
123 CborType::Integer(1),
124 CborType::Integer(2),
125 CborType::Integer(3),
126 ],
127 expected: vec![0x83, 0x01, 0x02, 0x03],
128 },
129 Testcase {
130 value: vec![
131 CborType::Integer(1),
132 CborType::Array(nested_arr_1),
133 CborType::Array(nested_arr_2),
134 ],
135 expected: vec![0x83, 0x01, 0x82, 0x02, 0x03, 0x82, 0x04, 0x05],
136 },
137 Testcase {
138 value: vec![
139 CborType::Integer(1),
140 CborType::Integer(2),
141 CborType::Integer(3),
142 CborType::Integer(4),
143 CborType::Integer(5),
144 CborType::Integer(6),
145 CborType::Integer(7),
146 CborType::Integer(8),
147 CborType::Integer(9),
148 CborType::Integer(10),
149 CborType::Integer(11),
150 CborType::Integer(12),
151 CborType::Integer(13),
152 CborType::Integer(14),
153 CborType::Integer(15),
154 CborType::Integer(16),
155 CborType::Integer(17),
156 CborType::Integer(18),
157 CborType::Integer(19),
158 CborType::Integer(20),
159 CborType::Integer(21),
160 CborType::Integer(22),
161 CborType::Integer(23),
162 CborType::Integer(24),
163 CborType::Integer(25),
164 ],
165 expected: vec![
166 0x98, 0x19, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c,
167 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x18, 0x18,
168 0x19,
169 ],
170 },
171 ];
172 for testcase in testcases {
173 let cbor = CborType::Array(testcase.value);
174 assert_eq!(testcase.expected, cbor.serialize());
175 }
176 }
177
178 #[test]
179 fn test_map() {
180 let empty_map: BTreeMap<CborType, CborType> = BTreeMap::new();
181 assert_eq!(vec![0xa0], CborType::Map(empty_map).serialize());
182
183 let mut positive_map: BTreeMap<CborType, CborType> = BTreeMap::new();
184 positive_map.insert(CborType::Integer(20), CborType::Integer(10));
185 positive_map.insert(CborType::Integer(10), CborType::Integer(20));
186 positive_map.insert(CborType::Integer(15), CborType::Integer(15));
187 assert_eq!(
188 vec![0xa3, 0x0a, 0x14, 0x0f, 0x0f, 0x14, 0x0a],
189 CborType::Map(positive_map).serialize()
190 );
191
192 let mut negative_map: BTreeMap<CborType, CborType> = BTreeMap::new();
193 negative_map.insert(CborType::SignedInteger(-4), CborType::Integer(10));
194 negative_map.insert(CborType::SignedInteger(-1), CborType::Integer(20));
195 negative_map.insert(CborType::SignedInteger(-5), CborType::Integer(15));
196 negative_map.insert(CborType::SignedInteger(-6), CborType::Integer(10));
197 assert_eq!(
198 vec![0xa4, 0x20, 0x14, 0x23, 0x0a, 0x24, 0x0f, 0x25, 0x0a],
199 CborType::Map(negative_map).serialize()
200 );
201
202 let mut mixed_map: BTreeMap<CborType, CborType> = BTreeMap::new();
203 mixed_map.insert(CborType::Integer(0), CborType::Integer(10));
204 mixed_map.insert(CborType::SignedInteger(-10), CborType::Integer(20));
205 mixed_map.insert(CborType::Integer(15), CborType::Integer(15));
206 assert_eq!(
207 vec![0xa3, 0x00, 0x0a, 0x0f, 0x0f, 0x29, 0x14],
208 CborType::Map(mixed_map).serialize()
209 );
210
211 let mut very_mixed_map: BTreeMap<CborType, CborType> = BTreeMap::new();
212 very_mixed_map.insert(CborType::Integer(0), CborType::Integer(10));
213 very_mixed_map.insert(
214 CborType::SignedInteger(-10000),
215 CborType::String("low".to_string()),
216 );
217 very_mixed_map.insert(CborType::SignedInteger(-10), CborType::Integer(20));
218 very_mixed_map.insert(
219 CborType::Integer(10001),
220 CborType::String("high".to_string()),
221 );
222 very_mixed_map.insert(
223 CborType::Integer(10000),
224 CborType::String("high".to_string()),
225 );
226 very_mixed_map.insert(CborType::Integer(15), CborType::Integer(15));
227 let expected = vec![
228 0xa6, 0x00, 0x0a, 0x0f, 0x0f, 0x29, 0x14, 0x19, 0x27, 0x10, 0x64, 0x68, 0x69, 0x67, 0x68,
229 0x19, 0x27, 0x11, 0x64, 0x68, 0x69, 0x67, 0x68, 0x39, 0x27, 0x0F, 0x63, 0x6C, 0x6F, 0x77,
230 ];
231 assert_eq!(expected, CborType::Map(very_mixed_map).serialize());
232 }
233
234 #[test]
235 #[ignore]
236 // XXX: The string isn't put into the map at the moment, so we can't actually
237 // test this.
238 fn test_invalid_map() {
239 let mut invalid_map: BTreeMap<CborType, CborType> = BTreeMap::new();
240 invalid_map.insert(CborType::SignedInteger(-10), CborType::Integer(20));
241 invalid_map.insert(CborType::String("0".to_string()), CborType::Integer(10));
242 invalid_map.insert(CborType::Integer(15), CborType::Integer(15));
243 let expected: Vec<u8> = vec![];
244 assert_eq!(expected, CborType::Map(invalid_map).serialize());
245 }
246
247 #[test]
248 fn test_integer() {
249 struct Testcase {
250 value: u64,
251 expected: Vec<u8>,
252 }
253 let testcases: Vec<Testcase> = vec![
254 Testcase {
255 value: 0,
256 expected: vec![0],
257 },
258 Testcase {
259 value: 1,
260 expected: vec![1],
261 },
262 Testcase {
263 value: 10,
264 expected: vec![0x0a],
265 },
266 Testcase {
267 value: 23,
268 expected: vec![0x17],
269 },
270 Testcase {
271 value: 24,
272 expected: vec![0x18, 0x18],
273 },
274 Testcase {
275 value: 25,
276 expected: vec![0x18, 0x19],
277 },
278 Testcase {
279 value: 100,
280 expected: vec![0x18, 0x64],
281 },
282 Testcase {
283 value: 1000,
284 expected: vec![0x19, 0x03, 0xe8],
285 },
286 Testcase {
287 value: 1000000,
288 expected: vec![0x1a, 0x00, 0x0f, 0x42, 0x40],
289 },
290 Testcase {
291 value: 1000000000000,
292 expected: vec![0x1b, 0x00, 0x00, 0x00, 0xe8, 0xd4, 0xa5, 0x10, 0x00],
293 },
294 Testcase {
295 value: 18446744073709551615,
296 expected: vec![0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff],
297 },
298 ];
299 for testcase in testcases {
300 let cbor = CborType::Integer(testcase.value);
301 assert_eq!(testcase.expected, cbor.serialize());
302 }
303 }
304
305 #[test]
306 fn test_tagged_item() {
307 let cbor = CborType::Tag(0x12, Box::new(CborType::Integer(2).clone()));
308 assert_eq!(vec![0xD2, 0x02], cbor.serialize());
309
310 let cbor = CborType::Tag(0x62, Box::new(CborType::Array(vec![]).clone()));
311 assert_eq!(vec![0xD8, 0x62, 0x80], cbor.serialize());
312 }
313
314 #[test]
315 fn test_null() {
316 let cbor = CborType::Null;
317 assert_eq!(vec![0xf6], cbor.serialize());
318 }
319
320 #[test]
321 fn test_null_in_array() {
322 let cbor = CborType::Array(vec![CborType::Null, CborType::Null]);
323 assert_eq!(vec![0x82, 0xf6, 0xf6], cbor.serialize());
324 }