1use super::values::{Constants, SimpleValue, Value};
18use crate::{
19 cbor_array_vec, cbor_bytes_lit, cbor_map_collection, cbor_tagged, cbor_text, cbor_unsigned,
20};
21use alloc::str;
22use alloc::vec::Vec;
23
24#[derive(Debug, PartialEq)]
26pub enum DecoderError {
27 UnsupportedMajorType,
28 UnknownAdditionalInfo,
29 IncompleteCborData,
30 TooMuchNesting,
31 InvalidUtf8,
32 ExtraneousData,
33 OutOfOrderKey,
34 NonMinimalCborEncoding,
35 UnsupportedSimpleValue,
36 UnsupportedFloatingPointValue,
37 OutOfRangeIntegerValue,
38}
39
40pub fn read(encoded_cbor: &[u8]) -> Result<Value, DecoderError> {
43 read_nested(encoded_cbor, None)
44}
45
46pub fn read_nested(encoded_cbor: &[u8], max_nest: Option<i8>) -> Result<Value, DecoderError> {
50 let mut reader = Reader::new(encoded_cbor);
51 let value = reader.decode_complete_data_item(max_nest)?;
52 if !reader.remaining_cbor.is_empty() {
53 return Err(DecoderError::ExtraneousData);
54 }
55 Ok(value)
56}
57
58struct Reader<'a> {
59 remaining_cbor: &'a [u8],
60}
61
62impl<'a> Reader<'a> {
63 pub fn new(cbor: &'a [u8]) -> Reader<'a> {
64 Reader {
65 remaining_cbor: cbor,
66 }
67 }
68
69 pub fn decode_complete_data_item(
70 &mut self,
71 remaining_depth: Option<i8>,
72 ) -> Result<Value, DecoderError> {
73 if remaining_depth.map_or(false, |d| d < 0) {
74 return Err(DecoderError::TooMuchNesting);
75 }
76
77 match self.read_bytes(1) {
78 Some([first_byte]) => {
79 let major_type_value = first_byte >> Constants::MAJOR_TYPE_BIT_SHIFT;
81 let additional_info = first_byte & Constants::ADDITIONAL_INFORMATION_MASK;
82 let size_value = self.read_variadic_length_integer(additional_info)?;
83 match major_type_value {
84 0 => self.decode_value_to_unsigned(size_value),
85 1 => self.decode_value_to_negative(size_value),
86 2 => self.read_byte_string_content(size_value),
87 3 => self.read_text_string_content(size_value),
88 4 => self.read_array_content(size_value, remaining_depth),
89 5 => self.read_map_content(size_value, remaining_depth),
90 6 => self.read_tagged_content(size_value, remaining_depth),
91 7 => self.decode_to_simple_value(size_value, additional_info),
92 _ => Err(DecoderError::UnsupportedMajorType),
93 }
94 }
95 _ => Err(DecoderError::IncompleteCborData),
96 }
97 }
98
99 fn read_bytes(&mut self, num_bytes: usize) -> Option<&[u8]> {
100 if num_bytes > self.remaining_cbor.len() {
101 None
102 } else {
103 let (left, right) = self.remaining_cbor.split_at(num_bytes);
104 self.remaining_cbor = right;
105 Some(left)
106 }
107 }
108
109 fn read_variadic_length_integer(&mut self, additional_info: u8) -> Result<u64, DecoderError> {
110 let additional_bytes_num = match additional_info {
111 0..=Constants::ADDITIONAL_INFORMATION_MAX_INT => return Ok(additional_info as u64),
112 Constants::ADDITIONAL_INFORMATION_1_BYTE => 1,
113 Constants::ADDITIONAL_INFORMATION_2_BYTES => 2,
114 Constants::ADDITIONAL_INFORMATION_4_BYTES => 4,
115 Constants::ADDITIONAL_INFORMATION_8_BYTES => 8,
116 _ => return Err(DecoderError::UnknownAdditionalInfo),
117 };
118 match self.read_bytes(additional_bytes_num) {
119 Some(bytes) => {
120 let mut size_value = 0u64;
121 for byte in bytes {
122 size_value <<= 8;
123 size_value += *byte as u64;
124 }
125 if (additional_bytes_num == 1 && size_value < 24)
126 || size_value < (1u64 << (8 * (additional_bytes_num >> 1)))
127 {
128 Err(DecoderError::NonMinimalCborEncoding)
129 } else {
130 Ok(size_value)
131 }
132 }
133 None => Err(DecoderError::IncompleteCborData),
134 }
135 }
136
137 fn decode_value_to_unsigned(&self, size_value: u64) -> Result<Value, DecoderError> {
138 Ok(cbor_unsigned!(size_value))
139 }
140
141 fn decode_value_to_negative(&self, size_value: u64) -> Result<Value, DecoderError> {
142 let signed_size = size_value as i64;
143 if signed_size < 0 {
144 Err(DecoderError::OutOfRangeIntegerValue)
145 } else {
146 Ok(Value::Negative(-(size_value as i64) - 1))
147 }
148 }
149
150 fn read_byte_string_content(&mut self, size_value: u64) -> Result<Value, DecoderError> {
151 match self.read_bytes(size_value as usize) {
152 Some(bytes) => Ok(cbor_bytes_lit!(bytes)),
153 None => Err(DecoderError::IncompleteCborData),
154 }
155 }
156
157 fn read_text_string_content(&mut self, size_value: u64) -> Result<Value, DecoderError> {
158 match self.read_bytes(size_value as usize) {
159 Some(bytes) => match str::from_utf8(bytes) {
160 Ok(s) => Ok(cbor_text!(s)),
161 Err(_) => Err(DecoderError::InvalidUtf8),
162 },
163 None => Err(DecoderError::IncompleteCborData),
164 }
165 }
166
167 fn read_array_content(
168 &mut self,
169 size_value: u64,
170 remaining_depth: Option<i8>,
171 ) -> Result<Value, DecoderError> {
172 let mut value_array = Vec::new();
174 for _ in 0..size_value {
175 value_array.push(self.decode_complete_data_item(remaining_depth.map(|d| d - 1))?);
176 }
177 Ok(cbor_array_vec!(value_array))
178 }
179
180 fn read_map_content(
181 &mut self,
182 size_value: u64,
183 remaining_depth: Option<i8>,
184 ) -> Result<Value, DecoderError> {
185 let mut value_map = Vec::<(Value, Value)>::new();
186 for _ in 0..size_value {
187 let key = self.decode_complete_data_item(remaining_depth.map(|d| d - 1))?;
188 if let Some(last_item) = value_map.last() {
189 if last_item.0 >= key {
190 return Err(DecoderError::OutOfOrderKey);
191 }
192 }
193 value_map.push((
194 key,
195 self.decode_complete_data_item(remaining_depth.map(|d| d - 1))?,
196 ));
197 }
198 Ok(cbor_map_collection!(value_map))
199 }
200
201 fn read_tagged_content(
202 &mut self,
203 tag_value: u64,
204 remaining_depth: Option<i8>,
205 ) -> Result<Value, DecoderError> {
206 let inner_value = self.decode_complete_data_item(remaining_depth.map(|d| d - 1))?;
207 Ok(cbor_tagged!(tag_value, inner_value))
208 }
209
210 fn decode_to_simple_value(
211 &self,
212 size_value: u64,
213 additional_info: u8,
214 ) -> Result<Value, DecoderError> {
215 if additional_info > Constants::ADDITIONAL_INFORMATION_MAX_INT
216 && additional_info != Constants::ADDITIONAL_INFORMATION_1_BYTE
217 {
218 return Err(DecoderError::UnsupportedFloatingPointValue);
221 }
222 match SimpleValue::from_integer(size_value) {
223 Some(simple_value) => Ok(Value::Simple(simple_value)),
224 None => Err(DecoderError::UnsupportedSimpleValue),
225 }
226 }
227}
228
229#[cfg(test)]
230mod test {
231 use super::*;
232 use crate::{
233 cbor_array, cbor_bytes, cbor_false, cbor_int, cbor_map, cbor_null, cbor_true,
234 cbor_undefined,
235 };
236 use alloc::vec;
237
238 #[test]
239 fn test_read_unsigned() {
240 let cases = vec![
241 (0, vec![0x00]),
242 (1, vec![0x01]),
243 (10, vec![0x0A]),
244 (23, vec![0x17]),
245 (24, vec![0x18, 0x18]),
246 (255, vec![0x18, 0xFF]),
247 (256, vec![0x19, 0x01, 0x00]),
248 (65535, vec![0x19, 0xFF, 0xFF]),
249 (65536, vec![0x1A, 0x00, 0x01, 0x00, 0x00]),
250 (0xFFFFFFFF, vec![0x1A, 0xFF, 0xFF, 0xFF, 0xFF]),
251 (
252 0x100000000,
253 vec![0x1B, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00],
254 ),
255 (
256 core::i64::MAX,
257 vec![0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF],
258 ),
259 ];
260 for (unsigned, mut cbor) in cases {
261 assert_eq!(read(&cbor), Ok(cbor_int!(unsigned)));
262 cbor.push(0x01);
263 assert_eq!(read(&cbor), Err(DecoderError::ExtraneousData));
264 }
265 }
266
267 #[test]
268 fn test_read_unsigned_non_minimum_byte_length() {
269 let encodings = vec![
270 vec![0x18, 0x17],
272 vec![0x19, 0x00, 0xff],
274 vec![0x1a, 0x00, 0x00, 0xff, 0xff],
276 vec![0x1b, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff],
278 vec![
281 0xa2, 0x17, 0x61, 0x42, 0x18, 0x17, 0x61, 0x45, ],
287 vec![
288 0xa2, 0x18, 0x17, 0x61, 0x45, 0x17, 0x61, 0x42, ],
294 vec![
295 0xa2, 0x18, 0x17, 0x61, 0x45, 0x18, 0x17, 0x61, 0x42, ],
301 ];
302 for encoding in encodings {
303 assert_eq!(read(&encoding), Err(DecoderError::NonMinimalCborEncoding));
304 }
305 }
306
307 #[test]
308 fn test_read_negative() {
309 let cases = vec![
310 (-1, vec![0x20]),
311 (-24, vec![0x37]),
312 (-25, vec![0x38, 0x18]),
313 (-256, vec![0x38, 0xFF]),
314 (-1000, vec![0x39, 0x03, 0xE7]),
315 (-1000000, vec![0x3A, 0x00, 0x0F, 0x42, 0x3F]),
316 (-4294967296, vec![0x3A, 0xFF, 0xFF, 0xFF, 0xFF]),
317 (
318 core::i64::MIN,
319 vec![0x3B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF],
320 ),
321 ];
322 for (negative, mut cbor) in cases {
323 assert_eq!(read(&cbor), Ok(cbor_int!(negative)));
324 cbor.push(0x01);
325 assert_eq!(read(&cbor), Err(DecoderError::ExtraneousData));
326 }
327 }
328
329 #[test]
330 fn test_read_byte_string() {
331 let cases = vec![
332 (Vec::new(), vec![0x40]),
333 (
334 vec![0x01, 0x02, 0x03, 0x04],
335 vec![0x44, 0x01, 0x02, 0x03, 0x04],
336 ),
337 ];
338 for (byte_string, mut cbor) in cases {
339 assert_eq!(read(&cbor), Ok(cbor_bytes!(byte_string)));
340 cbor.push(0x01);
341 assert_eq!(read(&cbor), Err(DecoderError::ExtraneousData));
342 }
343 }
344
345 #[test]
346 fn test_read_text_string() {
347 let unicode_3byte = vec![0xE6, 0xB0, 0xB4];
348 let cases = vec![
349 ("", vec![0x60]),
350 ("a", vec![0x61, 0x61]),
351 ("IETF", vec![0x64, 0x49, 0x45, 0x54, 0x46]),
352 ("\"\\", vec![0x62, 0x22, 0x5C]),
353 ("ü", vec![0x62, 0xC3, 0xBC]),
354 (
355 core::str::from_utf8(&unicode_3byte).unwrap(),
356 vec![0x63, 0xE6, 0xB0, 0xB4],
357 ),
358 ("𐅑", vec![0x64, 0xF0, 0x90, 0x85, 0x91]),
359 ];
360 for (text_string, mut cbor) in cases {
361 assert_eq!(read(&cbor), Ok(cbor_text!(text_string)));
362 cbor.push(0x01);
363 assert_eq!(read(&cbor), Err(DecoderError::ExtraneousData));
364 }
365 }
366
367 #[test]
368 fn test_read_text_string_with_nul() {
369 let cases = vec![
370 (
371 "string_without_nul",
372 vec![
373 0x72, 0x73, 0x74, 0x72, 0x69, 0x6E, 0x67, 0x5F, 0x77, 0x69, 0x74, 0x68, 0x6F,
374 0x75, 0x74, 0x5F, 0x6E, 0x75, 0x6C,
375 ],
376 ),
377 (
378 "nul_terminated_string\0",
379 vec![
380 0x76, 0x6E, 0x75, 0x6C, 0x5F, 0x74, 0x65, 0x72, 0x6D, 0x69, 0x6E, 0x61, 0x74,
381 0x65, 0x64, 0x5F, 0x73, 0x74, 0x72, 0x69, 0x6E, 0x67, 0x00,
382 ],
383 ),
384 (
385 "embedded\0nul",
386 vec![
387 0x6C, 0x65, 0x6D, 0x62, 0x65, 0x64, 0x64, 0x65, 0x64, 0x00, 0x6E, 0x75, 0x6C,
388 ],
389 ),
390 (
391 "trailing_nuls\0\0",
392 vec![
393 0x6F, 0x74, 0x72, 0x61, 0x69, 0x6C, 0x69, 0x6E, 0x67, 0x5F, 0x6E, 0x75, 0x6C,
394 0x73, 0x00, 0x00,
395 ],
396 ),
397 ];
398 for (text_string, mut cbor) in cases {
399 assert_eq!(read(&cbor), Ok(cbor_text!(text_string)));
400 cbor.push(0x01);
401 assert_eq!(read(&cbor), Err(DecoderError::ExtraneousData));
402 }
403 }
404
405 #[test]
406 fn test_read_text_string_with_invalid_byte_sequence_after_nul() {
407 assert_eq!(
408 read(&vec![0x63, 0x00, 0x00, 0xA6]),
409 Err(DecoderError::InvalidUtf8)
410 );
411 }
412
413 #[test]
414 fn test_read_array() {
415 let value_vec: Vec<_> = (1..26).collect();
416 let mut test_cbor = vec![
417 0x98, 0x19, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c,
418 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x18, 0x18,
419 0x19,
420 ];
421 assert_eq!(read(&test_cbor.clone()), Ok(cbor_array_vec!(value_vec)));
422 test_cbor.push(0x01);
423 assert_eq!(read(&test_cbor), Err(DecoderError::ExtraneousData));
424 }
425
426 #[test]
427 fn test_read_map() {
428 let value_map = cbor_map! {
429 24 => "abc",
430 "" => ".",
431 "b" => "B",
432 "aa" => "AA",
433 };
434 let mut test_cbor = vec![
435 0xa4, 0x18, 0x18, 0x63, 0x61, 0x62, 0x63, 0x60, 0x61, 0x2e, 0x61, 0x62, 0x61, 0x42, 0x62, 0x61, 0x61, 0x62, 0x41, 0x41, ];
445 assert_eq!(read(&test_cbor), Ok(value_map));
446 test_cbor.push(0x01);
447 assert_eq!(read(&test_cbor), Err(DecoderError::ExtraneousData));
448 }
449
450 #[test]
451 fn test_read_map_with_unsigned_keys() {
452 let value_map = cbor_map! {
453 1 => "a",
454 9 => "b",
455 999 => "c",
456 1111 => "d",
457 };
458 let mut test_cbor = vec![
459 0xa4, 0x01, 0x61, 0x61, 0x09, 0x61, 0x62, 0x19, 0x03, 0xE7, 0x61, 0x63, 0x19, 0x04, 0x57, 0x61, 0x64, ];
469 assert_eq!(read(&test_cbor), Ok(value_map));
470 test_cbor.push(0x01);
471 assert_eq!(read(&test_cbor), Err(DecoderError::ExtraneousData));
472 }
473
474 #[test]
475 fn test_read_map_with_negative_keys() {
476 let value_map = cbor_map! {
477 -1 => 1,
478 -2 => 2,
479 -100 => 3,
480 };
481 let mut test_cbor = vec![
482 0xA3, 0x20, 0x01, 0x21, 0x02, 0x38, 0x63, 0x03, ];
490 assert_eq!(read(&test_cbor), Ok(value_map));
491 test_cbor.push(0x01);
492 assert_eq!(read(&test_cbor), Err(DecoderError::ExtraneousData));
493 }
494
495 #[test]
496 fn test_read_map_with_array() {
497 let value_map = cbor_map! {
498 "a" => 1,
499 "b" => cbor_array![2, 3],
500 };
501 let mut test_cbor = vec![
502 0xa2, 0x61, 0x61, 0x01, 0x61, 0x62, 0x82, 0x02, 0x03,
507 ];
508 assert_eq!(read(&test_cbor), Ok(value_map));
509 test_cbor.push(0x01);
510 assert_eq!(read(&test_cbor), Err(DecoderError::ExtraneousData));
511 }
512
513 #[test]
514 fn test_read_map_with_text_string_keys() {
515 let value_map = cbor_map! {
516 "k" => "v",
517 "foo" => "bar",
518 };
519 let mut test_cbor = vec![
520 0xa2, 0x61, b'k', 0x61, b'v', 0x63, b'f', b'o', b'o', 0x63, b'b', b'a', b'r',
524 ];
525 assert_eq!(read(&test_cbor), Ok(value_map));
526 test_cbor.push(0x01);
527 assert_eq!(read(&test_cbor), Err(DecoderError::ExtraneousData));
528 }
529
530 #[test]
531 fn test_read_map_with_byte_string_keys() {
532 let value_map = cbor_map! {
533 b"k" => b"v",
534 b"foo" => b"bar",
535 };
536 let mut test_cbor = vec![
537 0xa2, 0x41, b'k', 0x41, b'v', 0x43, b'f', b'o', b'o', 0x43, b'b', b'a', b'r',
541 ];
542 assert_eq!(read(&test_cbor), Ok(value_map));
543 test_cbor.push(0x01);
544 assert_eq!(read(&test_cbor), Err(DecoderError::ExtraneousData));
545 }
546
547 #[test]
548 fn test_read_nested_map() {
549 let value_map = cbor_map! {
550 "a" => 1,
551 "b" => cbor_map! {
552 "c" => 2,
553 "d" => 3,
554 },
555 };
556 let mut test_cbor = vec![
557 0xa2, 0x61, 0x61, 0x01, 0x61, 0x62, 0xa2, 0x61, 0x63, 0x02, 0x61, 0x64, 0x03, ];
564 assert_eq!(read(&test_cbor), Ok(value_map));
565 test_cbor.push(0x01);
566 assert_eq!(read(&test_cbor), Err(DecoderError::ExtraneousData));
567 }
568
569 #[test]
570 fn test_read_tagged() {
571 let cases = vec![
572 (cbor_tagged!(6, cbor_int!(0x42)), vec![0xc6, 0x18, 0x42]),
573 (cbor_tagged!(1, cbor_true!()), vec![0xc1, 0xf5]),
574 (
575 cbor_tagged!(
576 1000,
577 cbor_map! {
578 "a" => 1,
579 "b" => cbor_array![2, 3],
580 }
581 ),
582 vec![
583 0xd9, 0x03, 0xe8, 0xa2, 0x61, 0x61, 0x01, 0x61, 0x62, 0x82, 0x02, 0x03,
588 ],
589 ),
590 ];
591 for (value, mut cbor) in cases {
592 assert_eq!(read(&cbor), Ok(value));
593 cbor.push(0x01);
594 assert_eq!(read(&cbor), Err(DecoderError::ExtraneousData));
595 }
596 }
597
598 #[test]
599 fn test_read_integer_out_of_range() {
600 let cases = vec![
601 vec![0x3B, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
604 ];
605 for cbor in cases {
606 assert_eq!(read(&cbor), Err(DecoderError::OutOfRangeIntegerValue));
607 }
608 }
609
610 #[test]
611 fn test_read_simple_value() {
612 let cases = vec![
613 (cbor_false!(), vec![0xF4]),
614 (cbor_true!(), vec![0xF5]),
615 (cbor_null!(), vec![0xF6]),
616 (cbor_undefined!(), vec![0xF7]),
617 ];
618 for (simple, mut cbor) in cases {
619 assert_eq!(read(&cbor.clone()), Ok(simple));
620 cbor.push(0x01);
621 assert_eq!(read(&cbor), Err(DecoderError::ExtraneousData));
622 }
623 }
624
625 #[test]
626 fn test_read_unsupported_floating_point_numbers() {
627 let cases = vec![
628 vec![0xF9, 0x10, 0x00],
629 vec![0xFA, 0x10, 0x00, 0x00, 0x00],
630 vec![0xFB, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
631 ];
632 for cbor in cases {
633 assert_eq!(
634 read(&cbor),
635 Err(DecoderError::UnsupportedFloatingPointValue)
636 );
637 }
638 }
639
640 #[test]
641 fn test_read_incomplete_cbor_data_error() {
642 let cases = vec![
643 vec![0x19, 0x03],
644 vec![0x44, 0x01, 0x02, 0x03],
645 vec![0x65, 0x49, 0x45, 0x54, 0x46],
646 vec![0x82, 0x02],
647 vec![0xA2, 0x61, 0x61, 0x01],
648 vec![0x18],
649 vec![0x99],
650 vec![0xBA],
651 vec![0x5B],
652 vec![0x3B],
653 vec![0x99, 0x01],
654 vec![0xBA, 0x01, 0x02, 0x03],
655 vec![0x3B, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07],
656 ];
657 for cbor in cases {
658 assert_eq!(read(&cbor), Err(DecoderError::IncompleteCborData));
659 }
660 }
661
662 #[test]
663 fn test_read_unknown_additional_info_error() {
664 let cases = vec![
665 vec![0x7C, 0x49, 0x45, 0x54, 0x46],
666 vec![0x7D, 0x22, 0x5C],
667 vec![0x7E, 0xC3, 0xBC],
668 vec![0x7F, 0xE6, 0xB0, 0xB4],
669 vec![0xFC],
670 vec![0xFD],
671 vec![0xFE],
672 vec![0xFF],
673 ];
674 for cbor in cases {
675 assert_eq!(read(&cbor), Err(DecoderError::UnknownAdditionalInfo));
676 }
677 }
678
679 #[test]
680 fn test_read_too_much_nesting_error() {
681 let cases = vec![
682 vec![0x18, 0x64],
683 vec![0x44, 0x01, 0x02, 0x03, 0x04],
684 vec![0x64, 0x49, 0x45, 0x54, 0x46],
685 vec![0x80],
686 vec![0xA0],
687 ];
688 for cbor in cases {
689 let mut reader = Reader::new(&cbor);
690 assert!(reader.decode_complete_data_item(Some(0)).is_ok());
691 }
692 let map_cbor = vec![
693 0xa2, 0x61, 0x61, 0x01, 0x61, 0x62, 0x82, 0x02, 0x03,
698 ];
699 let mut reader = Reader::new(&map_cbor);
700 assert_eq!(
701 reader.decode_complete_data_item(Some(1)),
702 Err(DecoderError::TooMuchNesting)
703 );
704 reader = Reader::new(&map_cbor);
705 assert!(reader.decode_complete_data_item(Some(2)).is_ok());
706 }
707
708 #[test]
709 fn test_read_out_of_order_key_error() {
710 let cases = vec![
711 vec![
712 0xa2, 0x61, 0x62, 0x61, 0x42, 0x61, 0x61, 0x61, 0x45, ],
718 vec![
719 0xa2, 0x61, 0x62, 0x02, 0x19, 0x03, 0xe8, 0x61, 0x61, ],
725 vec![
726 0xa2, 0x19, 0x03, 0xe8, 0x61, 0x61, 0x0a, 0x61, 0x62, ],
732 vec![
733 0xa2, 0x62, b'a', b'a', 0x02, 0x61, b'b', 0x01,
738 ],
739 vec![
740 0xa2, 0x42, b'x', b'x', 0x02, 0x41, b'y', 0x01,
745 ],
746 ];
747 for cbor in cases {
748 assert_eq!(read(&cbor), Err(DecoderError::OutOfOrderKey));
749 }
750 }
751
752 #[test]
753 fn test_read_duplicate_key_error() {
754 let map_with_duplicate_key = vec![
755 0xa6, 0x60, 0x61, 0x2e, 0x61, 0x62, 0x61, 0x42, 0x61, 0x62, 0x61, 0x43, 0x61, 0x64, 0x61, 0x44, 0x61, 0x65, 0x61, 0x44, 0x62, 0x61, 0x61, 0x62, 0x41, 0x41, ];
769 assert_eq!(
770 read(&map_with_duplicate_key),
771 Err(DecoderError::OutOfOrderKey)
772 );
773 }
774
775 #[test]
776 fn test_read_incorrect_string_encoding_error() {
777 let cases = vec![
778 vec![0x63, 0xED, 0x9F, 0xBF],
779 vec![0x63, 0xEE, 0x80, 0x80],
780 vec![0x63, 0xEF, 0xBF, 0xBD],
781 ];
782 for cbor in cases {
783 assert!(read(&cbor).is_ok());
784 }
785 let impossible_utf_byte = vec![0x64, 0xFE, 0xFE, 0xFF, 0xFF];
786 assert_eq!(read(&impossible_utf_byte), Err(DecoderError::InvalidUtf8));
787 }
788
789 #[test]
790 fn test_read_extraneous_cbor_data_error() {
791 let cases = vec![
792 vec![0x19, 0x03, 0x05, 0x00],
793 vec![0x44, 0x01, 0x02, 0x03, 0x04, 0x00],
794 vec![0x64, 0x49, 0x45, 0x54, 0x46, 0x00],
795 vec![0x82, 0x01, 0x02, 0x00],
796 vec![0xa1, 0x61, 0x63, 0x02, 0x61, 0x64, 0x03],
797 ];
798 for cbor in cases {
799 assert_eq!(read(&cbor), Err(DecoderError::ExtraneousData));
800 }
801 }
802
803 #[test]
804 fn test_read_unsupported_simple_type() {
805 let cases = vec![
806 vec![0xE0],
807 vec![0xF3],
808 vec![0xF8, 0x18],
809 vec![0xF8, 0x1C],
810 vec![0xF8, 0x1D],
811 vec![0xF8, 0x1E],
812 vec![0xF8, 0x1F],
813 vec![0xF8, 0x20],
814 vec![0xF8, 0xFF],
815 ];
816 for cbor in cases {
817 assert_eq!(read(&cbor), Err(DecoderError::UnsupportedSimpleValue));
818 }
819 }
820
821 #[test]
822 fn test_read_super_long_content_dont_crash() {
823 let cases = vec![
824 vec![0x9B, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF],
825 vec![0xBB, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF],
826 ];
827 for cbor in cases {
828 assert_eq!(read(&cbor), Err(DecoderError::IncompleteCborData));
829 }
830 }
831}