1 #![allow(clippy::empty_enum, clippy::unreadable_literal)]
2 #![cfg_attr(feature = "unstable", feature(never_type))]
3
4 use serde::de::IntoDeserializer;
5 use serde::Deserialize;
6 use serde_test::{assert_de_tokens_error, Token};
7 use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
8 use std::ffi::{CStr, CString};
9 use std::num::{
10 NonZeroI128, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI8, NonZeroIsize, NonZeroU128,
11 NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU8, NonZeroUsize, Wrapping,
12 };
13 use std::time::{Duration, SystemTime};
14
15 #[derive(Copy, Clone, PartialEq, Debug, Deserialize)]
16 struct UnitStruct;
17
18 #[derive(PartialEq, Debug, Deserialize)]
19 struct Struct {
20 a: i32,
21 b: i32,
22 #[serde(skip_deserializing)]
23 c: i32,
24 }
25
26 #[derive(PartialEq, Debug, Deserialize)]
27 #[serde(deny_unknown_fields)]
28 struct StructDenyUnknown {
29 a: i32,
30 #[serde(skip_deserializing)]
31 b: i32,
32 }
33
34 #[derive(PartialEq, Debug, Deserialize)]
35 #[serde(deny_unknown_fields)]
36 struct StructSkipAllDenyUnknown {
37 #[serde(skip_deserializing)]
38 a: i32,
39 }
40
41 #[derive(Default, PartialEq, Debug)]
42 struct NotDeserializable;
43
44 #[derive(PartialEq, Debug, Deserialize)]
45 enum Enum {
46 #[allow(dead_code)]
47 #[serde(skip_deserializing)]
48 Skipped,
49 Unit,
50 Simple(i32),
51 Seq(i32, i32, i32),
52 Map {
53 a: i32,
54 b: i32,
55 c: i32,
56 },
57 SimpleWithSkipped(#[serde(skip_deserializing)] NotDeserializable),
58 }
59
60 #[derive(PartialEq, Debug, Deserialize)]
61 enum EnumSkipAll {
62 #[allow(dead_code)]
63 #[serde(skip_deserializing)]
64 Skipped,
65 }
66
67 #[test]
test_i8()68 fn test_i8() {
69 let test = assert_de_tokens_error::<i8>;
70
71 // from signed
72 test(
73 &[Token::I16(-129)],
74 "invalid value: integer `-129`, expected i8",
75 );
76 test(
77 &[Token::I32(-129)],
78 "invalid value: integer `-129`, expected i8",
79 );
80 test(
81 &[Token::I64(-129)],
82 "invalid value: integer `-129`, expected i8",
83 );
84 test(
85 &[Token::I16(128)],
86 "invalid value: integer `128`, expected i8",
87 );
88 test(
89 &[Token::I32(128)],
90 "invalid value: integer `128`, expected i8",
91 );
92 test(
93 &[Token::I64(128)],
94 "invalid value: integer `128`, expected i8",
95 );
96
97 // from unsigned
98 test(
99 &[Token::U8(128)],
100 "invalid value: integer `128`, expected i8",
101 );
102 test(
103 &[Token::U16(128)],
104 "invalid value: integer `128`, expected i8",
105 );
106 test(
107 &[Token::U32(128)],
108 "invalid value: integer `128`, expected i8",
109 );
110 test(
111 &[Token::U64(128)],
112 "invalid value: integer `128`, expected i8",
113 );
114 }
115
116 #[test]
test_i16()117 fn test_i16() {
118 let test = assert_de_tokens_error::<i16>;
119
120 // from signed
121 test(
122 &[Token::I32(-32769)],
123 "invalid value: integer `-32769`, expected i16",
124 );
125 test(
126 &[Token::I64(-32769)],
127 "invalid value: integer `-32769`, expected i16",
128 );
129 test(
130 &[Token::I32(32768)],
131 "invalid value: integer `32768`, expected i16",
132 );
133 test(
134 &[Token::I64(32768)],
135 "invalid value: integer `32768`, expected i16",
136 );
137
138 // from unsigned
139 test(
140 &[Token::U16(32768)],
141 "invalid value: integer `32768`, expected i16",
142 );
143 test(
144 &[Token::U32(32768)],
145 "invalid value: integer `32768`, expected i16",
146 );
147 test(
148 &[Token::U64(32768)],
149 "invalid value: integer `32768`, expected i16",
150 );
151 }
152
153 #[test]
test_i32()154 fn test_i32() {
155 let test = assert_de_tokens_error::<i32>;
156
157 // from signed
158 test(
159 &[Token::I64(-2147483649)],
160 "invalid value: integer `-2147483649`, expected i32",
161 );
162 test(
163 &[Token::I64(2147483648)],
164 "invalid value: integer `2147483648`, expected i32",
165 );
166
167 // from unsigned
168 test(
169 &[Token::U32(2147483648)],
170 "invalid value: integer `2147483648`, expected i32",
171 );
172 test(
173 &[Token::U64(2147483648)],
174 "invalid value: integer `2147483648`, expected i32",
175 );
176 }
177
178 #[test]
test_i64()179 fn test_i64() {
180 let test = assert_de_tokens_error::<i64>;
181
182 // from unsigned
183 test(
184 &[Token::U64(9223372036854775808)],
185 "invalid value: integer `9223372036854775808`, expected i64",
186 );
187 }
188
189 #[test]
test_i128()190 fn test_i128() {
191 let deserializer = <i128 as IntoDeserializer>::into_deserializer(1);
192 let error = <&str>::deserialize(deserializer).unwrap_err();
193 assert_eq!(
194 error.to_string(),
195 "invalid type: integer `1` as i128, expected a borrowed string",
196 );
197 }
198
199 #[test]
test_u8()200 fn test_u8() {
201 let test = assert_de_tokens_error::<u8>;
202
203 // from signed
204 test(&[Token::I8(-1)], "invalid value: integer `-1`, expected u8");
205 test(
206 &[Token::I16(-1)],
207 "invalid value: integer `-1`, expected u8",
208 );
209 test(
210 &[Token::I32(-1)],
211 "invalid value: integer `-1`, expected u8",
212 );
213 test(
214 &[Token::I64(-1)],
215 "invalid value: integer `-1`, expected u8",
216 );
217 test(
218 &[Token::I16(256)],
219 "invalid value: integer `256`, expected u8",
220 );
221 test(
222 &[Token::I32(256)],
223 "invalid value: integer `256`, expected u8",
224 );
225 test(
226 &[Token::I64(256)],
227 "invalid value: integer `256`, expected u8",
228 );
229
230 // from unsigned
231 test(
232 &[Token::U16(256)],
233 "invalid value: integer `256`, expected u8",
234 );
235 test(
236 &[Token::U32(256)],
237 "invalid value: integer `256`, expected u8",
238 );
239 test(
240 &[Token::U64(256)],
241 "invalid value: integer `256`, expected u8",
242 );
243 }
244
245 #[test]
test_u16()246 fn test_u16() {
247 let test = assert_de_tokens_error::<u16>;
248
249 // from signed
250 test(
251 &[Token::I8(-1)],
252 "invalid value: integer `-1`, expected u16",
253 );
254 test(
255 &[Token::I16(-1)],
256 "invalid value: integer `-1`, expected u16",
257 );
258 test(
259 &[Token::I32(-1)],
260 "invalid value: integer `-1`, expected u16",
261 );
262 test(
263 &[Token::I64(-1)],
264 "invalid value: integer `-1`, expected u16",
265 );
266 test(
267 &[Token::I32(65536)],
268 "invalid value: integer `65536`, expected u16",
269 );
270 test(
271 &[Token::I64(65536)],
272 "invalid value: integer `65536`, expected u16",
273 );
274
275 // from unsigned
276 test(
277 &[Token::U32(65536)],
278 "invalid value: integer `65536`, expected u16",
279 );
280 test(
281 &[Token::U64(65536)],
282 "invalid value: integer `65536`, expected u16",
283 );
284 }
285
286 #[test]
test_u32()287 fn test_u32() {
288 let test = assert_de_tokens_error::<u32>;
289
290 // from signed
291 test(
292 &[Token::I8(-1)],
293 "invalid value: integer `-1`, expected u32",
294 );
295 test(
296 &[Token::I16(-1)],
297 "invalid value: integer `-1`, expected u32",
298 );
299 test(
300 &[Token::I32(-1)],
301 "invalid value: integer `-1`, expected u32",
302 );
303 test(
304 &[Token::I64(-1)],
305 "invalid value: integer `-1`, expected u32",
306 );
307 test(
308 &[Token::I64(4294967296)],
309 "invalid value: integer `4294967296`, expected u32",
310 );
311
312 // from unsigned
313 test(
314 &[Token::U64(4294967296)],
315 "invalid value: integer `4294967296`, expected u32",
316 );
317 }
318
319 #[test]
test_u64()320 fn test_u64() {
321 let test = assert_de_tokens_error::<u64>;
322
323 // from signed
324 test(
325 &[Token::I8(-1)],
326 "invalid value: integer `-1`, expected u64",
327 );
328 test(
329 &[Token::I16(-1)],
330 "invalid value: integer `-1`, expected u64",
331 );
332 test(
333 &[Token::I32(-1)],
334 "invalid value: integer `-1`, expected u64",
335 );
336 test(
337 &[Token::I64(-1)],
338 "invalid value: integer `-1`, expected u64",
339 );
340
341 let deserializer = <u64 as IntoDeserializer>::into_deserializer(1);
342 let error = <&str>::deserialize(deserializer).unwrap_err();
343 assert_eq!(
344 error.to_string(),
345 "invalid type: integer `1`, expected a borrowed string",
346 );
347 }
348
349 #[test]
test_u128()350 fn test_u128() {
351 let test = assert_de_tokens_error::<u128>;
352
353 // from signed
354 test(
355 &[Token::I8(-1)],
356 "invalid value: integer `-1`, expected u128",
357 );
358 test(
359 &[Token::I16(-1)],
360 "invalid value: integer `-1`, expected u128",
361 );
362 test(
363 &[Token::I32(-1)],
364 "invalid value: integer `-1`, expected u128",
365 );
366 test(
367 &[Token::I64(-1)],
368 "invalid value: integer `-1`, expected u128",
369 );
370
371 let deserializer = <u128 as IntoDeserializer>::into_deserializer(1);
372 let error = <&str>::deserialize(deserializer).unwrap_err();
373 assert_eq!(
374 error.to_string(),
375 "invalid type: integer `1` as u128, expected a borrowed string",
376 );
377 }
378
379 #[test]
test_usize()380 fn test_usize() {
381 let test = assert_de_tokens_error::<usize>;
382
383 // from signed
384 test(
385 &[Token::I8(-1)],
386 "invalid value: integer `-1`, expected usize",
387 );
388 test(
389 &[Token::I16(-1)],
390 "invalid value: integer `-1`, expected usize",
391 );
392 test(
393 &[Token::I32(-1)],
394 "invalid value: integer `-1`, expected usize",
395 );
396 test(
397 &[Token::I64(-1)],
398 "invalid value: integer `-1`, expected usize",
399 );
400 }
401
402 #[test]
test_nonzero_i8()403 fn test_nonzero_i8() {
404 let test = assert_de_tokens_error::<NonZeroI8>;
405
406 // from zero
407 test(
408 &[Token::I8(0)],
409 "invalid value: integer `0`, expected a nonzero i8",
410 );
411 test(
412 &[Token::I16(0)],
413 "invalid value: integer `0`, expected a nonzero i8",
414 );
415 test(
416 &[Token::I32(0)],
417 "invalid value: integer `0`, expected a nonzero i8",
418 );
419 test(
420 &[Token::I64(0)],
421 "invalid value: integer `0`, expected a nonzero i8",
422 );
423 test(
424 &[Token::U8(0)],
425 "invalid value: integer `0`, expected a nonzero i8",
426 );
427 test(
428 &[Token::U16(0)],
429 "invalid value: integer `0`, expected a nonzero i8",
430 );
431 test(
432 &[Token::U32(0)],
433 "invalid value: integer `0`, expected a nonzero i8",
434 );
435 test(
436 &[Token::U64(0)],
437 "invalid value: integer `0`, expected a nonzero i8",
438 );
439
440 // from signed
441 test(
442 &[Token::I16(-129)],
443 "invalid value: integer `-129`, expected a nonzero i8",
444 );
445 test(
446 &[Token::I32(-129)],
447 "invalid value: integer `-129`, expected a nonzero i8",
448 );
449 test(
450 &[Token::I64(-129)],
451 "invalid value: integer `-129`, expected a nonzero i8",
452 );
453 test(
454 &[Token::I16(128)],
455 "invalid value: integer `128`, expected a nonzero i8",
456 );
457 test(
458 &[Token::I32(128)],
459 "invalid value: integer `128`, expected a nonzero i8",
460 );
461 test(
462 &[Token::I64(128)],
463 "invalid value: integer `128`, expected a nonzero i8",
464 );
465
466 // from unsigned
467 test(
468 &[Token::U8(128)],
469 "invalid value: integer `128`, expected a nonzero i8",
470 );
471 test(
472 &[Token::U16(128)],
473 "invalid value: integer `128`, expected a nonzero i8",
474 );
475 test(
476 &[Token::U32(128)],
477 "invalid value: integer `128`, expected a nonzero i8",
478 );
479 test(
480 &[Token::U64(128)],
481 "invalid value: integer `128`, expected a nonzero i8",
482 );
483 }
484
485 #[test]
test_nonzero_i16()486 fn test_nonzero_i16() {
487 let test = assert_de_tokens_error::<NonZeroI16>;
488
489 // from zero
490 test(
491 &[Token::I8(0)],
492 "invalid value: integer `0`, expected a nonzero i16",
493 );
494 test(
495 &[Token::I16(0)],
496 "invalid value: integer `0`, expected a nonzero i16",
497 );
498 test(
499 &[Token::I32(0)],
500 "invalid value: integer `0`, expected a nonzero i16",
501 );
502 test(
503 &[Token::I64(0)],
504 "invalid value: integer `0`, expected a nonzero i16",
505 );
506 test(
507 &[Token::U8(0)],
508 "invalid value: integer `0`, expected a nonzero i16",
509 );
510 test(
511 &[Token::U16(0)],
512 "invalid value: integer `0`, expected a nonzero i16",
513 );
514 test(
515 &[Token::U32(0)],
516 "invalid value: integer `0`, expected a nonzero i16",
517 );
518 test(
519 &[Token::U64(0)],
520 "invalid value: integer `0`, expected a nonzero i16",
521 );
522
523 // from signed
524 test(
525 &[Token::I32(-32769)],
526 "invalid value: integer `-32769`, expected a nonzero i16",
527 );
528 test(
529 &[Token::I64(-32769)],
530 "invalid value: integer `-32769`, expected a nonzero i16",
531 );
532 test(
533 &[Token::I32(32768)],
534 "invalid value: integer `32768`, expected a nonzero i16",
535 );
536 test(
537 &[Token::I64(32768)],
538 "invalid value: integer `32768`, expected a nonzero i16",
539 );
540
541 // from unsigned
542 test(
543 &[Token::U16(32768)],
544 "invalid value: integer `32768`, expected a nonzero i16",
545 );
546 test(
547 &[Token::U32(32768)],
548 "invalid value: integer `32768`, expected a nonzero i16",
549 );
550 test(
551 &[Token::U64(32768)],
552 "invalid value: integer `32768`, expected a nonzero i16",
553 );
554 }
555
556 #[test]
test_nonzero_i32()557 fn test_nonzero_i32() {
558 let test = assert_de_tokens_error::<NonZeroI32>;
559
560 // from zero
561 test(
562 &[Token::I8(0)],
563 "invalid value: integer `0`, expected a nonzero i32",
564 );
565 test(
566 &[Token::I16(0)],
567 "invalid value: integer `0`, expected a nonzero i32",
568 );
569 test(
570 &[Token::I32(0)],
571 "invalid value: integer `0`, expected a nonzero i32",
572 );
573 test(
574 &[Token::I64(0)],
575 "invalid value: integer `0`, expected a nonzero i32",
576 );
577 test(
578 &[Token::U8(0)],
579 "invalid value: integer `0`, expected a nonzero i32",
580 );
581 test(
582 &[Token::U16(0)],
583 "invalid value: integer `0`, expected a nonzero i32",
584 );
585 test(
586 &[Token::U32(0)],
587 "invalid value: integer `0`, expected a nonzero i32",
588 );
589 test(
590 &[Token::U64(0)],
591 "invalid value: integer `0`, expected a nonzero i32",
592 );
593
594 // from signed
595 test(
596 &[Token::I64(-2147483649)],
597 "invalid value: integer `-2147483649`, expected a nonzero i32",
598 );
599 test(
600 &[Token::I64(2147483648)],
601 "invalid value: integer `2147483648`, expected a nonzero i32",
602 );
603
604 // from unsigned
605 test(
606 &[Token::U32(2147483648)],
607 "invalid value: integer `2147483648`, expected a nonzero i32",
608 );
609 test(
610 &[Token::U64(2147483648)],
611 "invalid value: integer `2147483648`, expected a nonzero i32",
612 );
613 }
614
615 #[test]
test_nonzero_i64()616 fn test_nonzero_i64() {
617 let test = assert_de_tokens_error::<NonZeroI64>;
618
619 // from zero
620 test(
621 &[Token::I8(0)],
622 "invalid value: integer `0`, expected a nonzero i64",
623 );
624 test(
625 &[Token::I16(0)],
626 "invalid value: integer `0`, expected a nonzero i64",
627 );
628 test(
629 &[Token::I32(0)],
630 "invalid value: integer `0`, expected a nonzero i64",
631 );
632 test(
633 &[Token::I64(0)],
634 "invalid value: integer `0`, expected a nonzero i64",
635 );
636 test(
637 &[Token::U8(0)],
638 "invalid value: integer `0`, expected a nonzero i64",
639 );
640 test(
641 &[Token::U16(0)],
642 "invalid value: integer `0`, expected a nonzero i64",
643 );
644 test(
645 &[Token::U32(0)],
646 "invalid value: integer `0`, expected a nonzero i64",
647 );
648 test(
649 &[Token::U64(0)],
650 "invalid value: integer `0`, expected a nonzero i64",
651 );
652
653 // from unsigned
654 test(
655 &[Token::U64(9223372036854775808)],
656 "invalid value: integer `9223372036854775808`, expected a nonzero i64",
657 );
658 }
659
660 #[test]
test_nonzero_i128()661 fn test_nonzero_i128() {
662 let test = assert_de_tokens_error::<NonZeroI128>;
663
664 // from zero
665 test(
666 &[Token::I8(0)],
667 "invalid value: integer `0`, expected a nonzero i128",
668 );
669 test(
670 &[Token::I16(0)],
671 "invalid value: integer `0`, expected a nonzero i128",
672 );
673 test(
674 &[Token::I32(0)],
675 "invalid value: integer `0`, expected a nonzero i128",
676 );
677 test(
678 &[Token::I64(0)],
679 "invalid value: integer `0`, expected a nonzero i128",
680 );
681 test(
682 &[Token::U8(0)],
683 "invalid value: integer `0`, expected a nonzero i128",
684 );
685 test(
686 &[Token::U16(0)],
687 "invalid value: integer `0`, expected a nonzero i128",
688 );
689 test(
690 &[Token::U32(0)],
691 "invalid value: integer `0`, expected a nonzero i128",
692 );
693 test(
694 &[Token::U64(0)],
695 "invalid value: integer `0`, expected a nonzero i128",
696 );
697 }
698
699 #[test]
test_nonzero_isize()700 fn test_nonzero_isize() {
701 let test = assert_de_tokens_error::<NonZeroIsize>;
702
703 // from zero
704 test(
705 &[Token::I8(0)],
706 "invalid value: integer `0`, expected a nonzero isize",
707 );
708 test(
709 &[Token::I16(0)],
710 "invalid value: integer `0`, expected a nonzero isize",
711 );
712 test(
713 &[Token::I32(0)],
714 "invalid value: integer `0`, expected a nonzero isize",
715 );
716 test(
717 &[Token::I64(0)],
718 "invalid value: integer `0`, expected a nonzero isize",
719 );
720 test(
721 &[Token::U8(0)],
722 "invalid value: integer `0`, expected a nonzero isize",
723 );
724 test(
725 &[Token::U16(0)],
726 "invalid value: integer `0`, expected a nonzero isize",
727 );
728 test(
729 &[Token::U32(0)],
730 "invalid value: integer `0`, expected a nonzero isize",
731 );
732 test(
733 &[Token::U64(0)],
734 "invalid value: integer `0`, expected a nonzero isize",
735 );
736 }
737
738 #[test]
test_nonzero_u8()739 fn test_nonzero_u8() {
740 let test = assert_de_tokens_error::<NonZeroU8>;
741
742 // from zero
743 test(
744 &[Token::I8(0)],
745 "invalid value: integer `0`, expected a nonzero u8",
746 );
747 test(
748 &[Token::I16(0)],
749 "invalid value: integer `0`, expected a nonzero u8",
750 );
751 test(
752 &[Token::I32(0)],
753 "invalid value: integer `0`, expected a nonzero u8",
754 );
755 test(
756 &[Token::I64(0)],
757 "invalid value: integer `0`, expected a nonzero u8",
758 );
759 test(
760 &[Token::U8(0)],
761 "invalid value: integer `0`, expected a nonzero u8",
762 );
763 test(
764 &[Token::U16(0)],
765 "invalid value: integer `0`, expected a nonzero u8",
766 );
767 test(
768 &[Token::U32(0)],
769 "invalid value: integer `0`, expected a nonzero u8",
770 );
771 test(
772 &[Token::U64(0)],
773 "invalid value: integer `0`, expected a nonzero u8",
774 );
775
776 // from signed
777 test(
778 &[Token::I8(-1)],
779 "invalid value: integer `-1`, expected a nonzero u8",
780 );
781 test(
782 &[Token::I16(-1)],
783 "invalid value: integer `-1`, expected a nonzero u8",
784 );
785 test(
786 &[Token::I32(-1)],
787 "invalid value: integer `-1`, expected a nonzero u8",
788 );
789 test(
790 &[Token::I64(-1)],
791 "invalid value: integer `-1`, expected a nonzero u8",
792 );
793 test(
794 &[Token::I16(256)],
795 "invalid value: integer `256`, expected a nonzero u8",
796 );
797 test(
798 &[Token::I32(256)],
799 "invalid value: integer `256`, expected a nonzero u8",
800 );
801 test(
802 &[Token::I64(256)],
803 "invalid value: integer `256`, expected a nonzero u8",
804 );
805
806 // from unsigned
807 test(
808 &[Token::U16(256)],
809 "invalid value: integer `256`, expected a nonzero u8",
810 );
811 test(
812 &[Token::U32(256)],
813 "invalid value: integer `256`, expected a nonzero u8",
814 );
815 test(
816 &[Token::U64(256)],
817 "invalid value: integer `256`, expected a nonzero u8",
818 );
819 }
820
821 #[test]
test_nonzero_u16()822 fn test_nonzero_u16() {
823 let test = assert_de_tokens_error::<NonZeroU16>;
824
825 // from zero
826 test(
827 &[Token::I8(0)],
828 "invalid value: integer `0`, expected a nonzero u16",
829 );
830 test(
831 &[Token::I16(0)],
832 "invalid value: integer `0`, expected a nonzero u16",
833 );
834 test(
835 &[Token::I32(0)],
836 "invalid value: integer `0`, expected a nonzero u16",
837 );
838 test(
839 &[Token::I64(0)],
840 "invalid value: integer `0`, expected a nonzero u16",
841 );
842 test(
843 &[Token::U8(0)],
844 "invalid value: integer `0`, expected a nonzero u16",
845 );
846 test(
847 &[Token::U16(0)],
848 "invalid value: integer `0`, expected a nonzero u16",
849 );
850 test(
851 &[Token::U32(0)],
852 "invalid value: integer `0`, expected a nonzero u16",
853 );
854 test(
855 &[Token::U64(0)],
856 "invalid value: integer `0`, expected a nonzero u16",
857 );
858
859 // from signed
860 test(
861 &[Token::I8(-1)],
862 "invalid value: integer `-1`, expected a nonzero u16",
863 );
864 test(
865 &[Token::I16(-1)],
866 "invalid value: integer `-1`, expected a nonzero u16",
867 );
868 test(
869 &[Token::I32(-1)],
870 "invalid value: integer `-1`, expected a nonzero u16",
871 );
872 test(
873 &[Token::I64(-1)],
874 "invalid value: integer `-1`, expected a nonzero u16",
875 );
876 test(
877 &[Token::I32(65536)],
878 "invalid value: integer `65536`, expected a nonzero u16",
879 );
880 test(
881 &[Token::I64(65536)],
882 "invalid value: integer `65536`, expected a nonzero u16",
883 );
884
885 // from unsigned
886 test(
887 &[Token::U32(65536)],
888 "invalid value: integer `65536`, expected a nonzero u16",
889 );
890 test(
891 &[Token::U64(65536)],
892 "invalid value: integer `65536`, expected a nonzero u16",
893 );
894 }
895
896 #[test]
test_nonzero_u32()897 fn test_nonzero_u32() {
898 let test = assert_de_tokens_error::<NonZeroU32>;
899
900 // from zero
901 test(
902 &[Token::I8(0)],
903 "invalid value: integer `0`, expected a nonzero u32",
904 );
905 test(
906 &[Token::I16(0)],
907 "invalid value: integer `0`, expected a nonzero u32",
908 );
909 test(
910 &[Token::I32(0)],
911 "invalid value: integer `0`, expected a nonzero u32",
912 );
913 test(
914 &[Token::I64(0)],
915 "invalid value: integer `0`, expected a nonzero u32",
916 );
917 test(
918 &[Token::U8(0)],
919 "invalid value: integer `0`, expected a nonzero u32",
920 );
921 test(
922 &[Token::U16(0)],
923 "invalid value: integer `0`, expected a nonzero u32",
924 );
925 test(
926 &[Token::U32(0)],
927 "invalid value: integer `0`, expected a nonzero u32",
928 );
929 test(
930 &[Token::U64(0)],
931 "invalid value: integer `0`, expected a nonzero u32",
932 );
933
934 // from signed
935 test(
936 &[Token::I8(-1)],
937 "invalid value: integer `-1`, expected a nonzero u32",
938 );
939 test(
940 &[Token::I16(-1)],
941 "invalid value: integer `-1`, expected a nonzero u32",
942 );
943 test(
944 &[Token::I32(-1)],
945 "invalid value: integer `-1`, expected a nonzero u32",
946 );
947 test(
948 &[Token::I64(-1)],
949 "invalid value: integer `-1`, expected a nonzero u32",
950 );
951 test(
952 &[Token::I64(4294967296)],
953 "invalid value: integer `4294967296`, expected a nonzero u32",
954 );
955
956 // from unsigned
957 test(
958 &[Token::U64(4294967296)],
959 "invalid value: integer `4294967296`, expected a nonzero u32",
960 );
961 }
962
963 #[test]
test_nonzero_u64()964 fn test_nonzero_u64() {
965 let test = assert_de_tokens_error::<NonZeroU64>;
966
967 // from zero
968 test(
969 &[Token::I8(0)],
970 "invalid value: integer `0`, expected a nonzero u64",
971 );
972 test(
973 &[Token::I16(0)],
974 "invalid value: integer `0`, expected a nonzero u64",
975 );
976 test(
977 &[Token::I32(0)],
978 "invalid value: integer `0`, expected a nonzero u64",
979 );
980 test(
981 &[Token::I64(0)],
982 "invalid value: integer `0`, expected a nonzero u64",
983 );
984 test(
985 &[Token::U8(0)],
986 "invalid value: integer `0`, expected a nonzero u64",
987 );
988 test(
989 &[Token::U16(0)],
990 "invalid value: integer `0`, expected a nonzero u64",
991 );
992 test(
993 &[Token::U32(0)],
994 "invalid value: integer `0`, expected a nonzero u64",
995 );
996 test(
997 &[Token::U64(0)],
998 "invalid value: integer `0`, expected a nonzero u64",
999 );
1000
1001 // from signed
1002 test(
1003 &[Token::I8(-1)],
1004 "invalid value: integer `-1`, expected a nonzero u64",
1005 );
1006 test(
1007 &[Token::I16(-1)],
1008 "invalid value: integer `-1`, expected a nonzero u64",
1009 );
1010 test(
1011 &[Token::I32(-1)],
1012 "invalid value: integer `-1`, expected a nonzero u64",
1013 );
1014 test(
1015 &[Token::I64(-1)],
1016 "invalid value: integer `-1`, expected a nonzero u64",
1017 );
1018 }
1019
1020 #[test]
test_nonzero_u128()1021 fn test_nonzero_u128() {
1022 let test = assert_de_tokens_error::<NonZeroU128>;
1023
1024 // from zero
1025 test(
1026 &[Token::I8(0)],
1027 "invalid value: integer `0`, expected a nonzero u128",
1028 );
1029 test(
1030 &[Token::I16(0)],
1031 "invalid value: integer `0`, expected a nonzero u128",
1032 );
1033 test(
1034 &[Token::I32(0)],
1035 "invalid value: integer `0`, expected a nonzero u128",
1036 );
1037 test(
1038 &[Token::I64(0)],
1039 "invalid value: integer `0`, expected a nonzero u128",
1040 );
1041 test(
1042 &[Token::U8(0)],
1043 "invalid value: integer `0`, expected a nonzero u128",
1044 );
1045 test(
1046 &[Token::U16(0)],
1047 "invalid value: integer `0`, expected a nonzero u128",
1048 );
1049 test(
1050 &[Token::U32(0)],
1051 "invalid value: integer `0`, expected a nonzero u128",
1052 );
1053 test(
1054 &[Token::U64(0)],
1055 "invalid value: integer `0`, expected a nonzero u128",
1056 );
1057
1058 // from signed
1059 test(
1060 &[Token::I8(-1)],
1061 "invalid value: integer `-1`, expected a nonzero u128",
1062 );
1063 test(
1064 &[Token::I16(-1)],
1065 "invalid value: integer `-1`, expected a nonzero u128",
1066 );
1067 test(
1068 &[Token::I32(-1)],
1069 "invalid value: integer `-1`, expected a nonzero u128",
1070 );
1071 test(
1072 &[Token::I64(-1)],
1073 "invalid value: integer `-1`, expected a nonzero u128",
1074 );
1075 }
1076
1077 #[test]
test_nonzero_usize()1078 fn test_nonzero_usize() {
1079 let test = assert_de_tokens_error::<NonZeroUsize>;
1080
1081 // from zero
1082 test(
1083 &[Token::I8(0)],
1084 "invalid value: integer `0`, expected a nonzero usize",
1085 );
1086 test(
1087 &[Token::I16(0)],
1088 "invalid value: integer `0`, expected a nonzero usize",
1089 );
1090 test(
1091 &[Token::I32(0)],
1092 "invalid value: integer `0`, expected a nonzero usize",
1093 );
1094 test(
1095 &[Token::I64(0)],
1096 "invalid value: integer `0`, expected a nonzero usize",
1097 );
1098 test(
1099 &[Token::U8(0)],
1100 "invalid value: integer `0`, expected a nonzero usize",
1101 );
1102 test(
1103 &[Token::U16(0)],
1104 "invalid value: integer `0`, expected a nonzero usize",
1105 );
1106 test(
1107 &[Token::U32(0)],
1108 "invalid value: integer `0`, expected a nonzero usize",
1109 );
1110 test(
1111 &[Token::U64(0)],
1112 "invalid value: integer `0`, expected a nonzero usize",
1113 );
1114
1115 // from signed
1116 test(
1117 &[Token::I8(-1)],
1118 "invalid value: integer `-1`, expected a nonzero usize",
1119 );
1120 test(
1121 &[Token::I16(-1)],
1122 "invalid value: integer `-1`, expected a nonzero usize",
1123 );
1124 test(
1125 &[Token::I32(-1)],
1126 "invalid value: integer `-1`, expected a nonzero usize",
1127 );
1128 test(
1129 &[Token::I64(-1)],
1130 "invalid value: integer `-1`, expected a nonzero usize",
1131 );
1132 }
1133
1134 #[test]
test_unknown_field()1135 fn test_unknown_field() {
1136 assert_de_tokens_error::<StructDenyUnknown>(
1137 &[
1138 Token::Struct {
1139 name: "StructDenyUnknown",
1140 len: 1,
1141 },
1142 Token::Str("a"),
1143 Token::I32(0),
1144 Token::Str("d"),
1145 ],
1146 "unknown field `d`, expected `a`",
1147 );
1148 }
1149
1150 #[test]
test_skipped_field_is_unknown()1151 fn test_skipped_field_is_unknown() {
1152 assert_de_tokens_error::<StructDenyUnknown>(
1153 &[
1154 Token::Struct {
1155 name: "StructDenyUnknown",
1156 len: 1,
1157 },
1158 Token::Str("b"),
1159 ],
1160 "unknown field `b`, expected `a`",
1161 );
1162 }
1163
1164 #[test]
test_skip_all_deny_unknown()1165 fn test_skip_all_deny_unknown() {
1166 assert_de_tokens_error::<StructSkipAllDenyUnknown>(
1167 &[
1168 Token::Struct {
1169 name: "StructSkipAllDenyUnknown",
1170 len: 0,
1171 },
1172 Token::Str("a"),
1173 ],
1174 "unknown field `a`, there are no fields",
1175 );
1176 }
1177
1178 #[test]
test_unknown_variant()1179 fn test_unknown_variant() {
1180 assert_de_tokens_error::<Enum>(
1181 &[
1182 Token::UnitVariant { name: "Enum", variant: "Foo" },
1183 ],
1184 "unknown variant `Foo`, expected one of `Unit`, `Simple`, `Seq`, `Map`, `SimpleWithSkipped`",
1185 );
1186 }
1187
1188 #[test]
test_enum_skipped_variant()1189 fn test_enum_skipped_variant() {
1190 assert_de_tokens_error::<Enum>(
1191 &[
1192 Token::UnitVariant { name: "Enum", variant: "Skipped" },
1193 ],
1194 "unknown variant `Skipped`, expected one of `Unit`, `Simple`, `Seq`, `Map`, `SimpleWithSkipped`",
1195 );
1196 }
1197
1198 #[test]
test_enum_skip_all()1199 fn test_enum_skip_all() {
1200 assert_de_tokens_error::<EnumSkipAll>(
1201 &[Token::UnitVariant {
1202 name: "EnumSkipAll",
1203 variant: "Skipped",
1204 }],
1205 "unknown variant `Skipped`, there are no variants",
1206 );
1207 }
1208
1209 #[test]
test_duplicate_field_struct()1210 fn test_duplicate_field_struct() {
1211 assert_de_tokens_error::<Struct>(
1212 &[
1213 Token::Map { len: Some(3) },
1214 Token::Str("a"),
1215 Token::I32(1),
1216 Token::Str("a"),
1217 ],
1218 "duplicate field `a`",
1219 );
1220 }
1221
1222 #[test]
test_duplicate_field_enum()1223 fn test_duplicate_field_enum() {
1224 assert_de_tokens_error::<Enum>(
1225 &[
1226 Token::StructVariant {
1227 name: "Enum",
1228 variant: "Map",
1229 len: 3,
1230 },
1231 Token::Str("a"),
1232 Token::I32(1),
1233 Token::Str("a"),
1234 ],
1235 "duplicate field `a`",
1236 );
1237 }
1238
1239 #[test]
test_enum_out_of_range()1240 fn test_enum_out_of_range() {
1241 assert_de_tokens_error::<Enum>(
1242 &[Token::Enum { name: "Enum" }, Token::U32(5), Token::Unit],
1243 "invalid value: integer `5`, expected variant index 0 <= i < 5",
1244 );
1245 }
1246
1247 #[test]
test_short_tuple()1248 fn test_short_tuple() {
1249 assert_de_tokens_error::<(u8, u8, u8)>(
1250 &[Token::Tuple { len: 1 }, Token::U8(1), Token::TupleEnd],
1251 "invalid length 1, expected a tuple of size 3",
1252 );
1253 }
1254
1255 #[test]
test_short_array()1256 fn test_short_array() {
1257 assert_de_tokens_error::<[u8; 3]>(
1258 &[Token::Seq { len: Some(1) }, Token::U8(1), Token::SeqEnd],
1259 "invalid length 1, expected an array of length 3",
1260 );
1261 }
1262
1263 #[test]
test_cstring_internal_null()1264 fn test_cstring_internal_null() {
1265 assert_de_tokens_error::<CString>(
1266 &[Token::Bytes(b"a\0c")],
1267 "nul byte found in provided data at position: 1",
1268 );
1269 }
1270
1271 #[test]
test_cstring_internal_null_end()1272 fn test_cstring_internal_null_end() {
1273 assert_de_tokens_error::<CString>(
1274 &[Token::Bytes(b"ac\0")],
1275 "nul byte found in provided data at position: 2",
1276 );
1277 }
1278
1279 #[test]
test_unit_from_empty_seq()1280 fn test_unit_from_empty_seq() {
1281 assert_de_tokens_error::<()>(
1282 &[Token::Seq { len: Some(0) }, Token::SeqEnd],
1283 "invalid type: sequence, expected unit",
1284 );
1285 }
1286
1287 #[test]
test_unit_from_empty_seq_without_len()1288 fn test_unit_from_empty_seq_without_len() {
1289 assert_de_tokens_error::<()>(
1290 &[Token::Seq { len: None }, Token::SeqEnd],
1291 "invalid type: sequence, expected unit",
1292 );
1293 }
1294
1295 #[test]
test_unit_from_tuple_struct()1296 fn test_unit_from_tuple_struct() {
1297 assert_de_tokens_error::<()>(
1298 &[
1299 Token::TupleStruct {
1300 name: "Anything",
1301 len: 0,
1302 },
1303 Token::TupleStructEnd,
1304 ],
1305 "invalid type: sequence, expected unit",
1306 );
1307 }
1308
1309 #[test]
test_string_from_unit()1310 fn test_string_from_unit() {
1311 assert_de_tokens_error::<String>(
1312 &[Token::Unit],
1313 "invalid type: unit value, expected a string",
1314 );
1315 }
1316
1317 #[test]
test_btreeset_from_unit()1318 fn test_btreeset_from_unit() {
1319 assert_de_tokens_error::<BTreeSet<isize>>(
1320 &[Token::Unit],
1321 "invalid type: unit value, expected a sequence",
1322 );
1323 }
1324
1325 #[test]
test_btreeset_from_unit_struct()1326 fn test_btreeset_from_unit_struct() {
1327 assert_de_tokens_error::<BTreeSet<isize>>(
1328 &[Token::UnitStruct { name: "Anything" }],
1329 "invalid type: unit value, expected a sequence",
1330 );
1331 }
1332
1333 #[test]
test_hashset_from_unit()1334 fn test_hashset_from_unit() {
1335 assert_de_tokens_error::<HashSet<isize>>(
1336 &[Token::Unit],
1337 "invalid type: unit value, expected a sequence",
1338 );
1339 }
1340
1341 #[test]
test_hashset_from_unit_struct()1342 fn test_hashset_from_unit_struct() {
1343 assert_de_tokens_error::<HashSet<isize>>(
1344 &[Token::UnitStruct { name: "Anything" }],
1345 "invalid type: unit value, expected a sequence",
1346 );
1347 }
1348
1349 #[test]
test_vec_from_unit()1350 fn test_vec_from_unit() {
1351 assert_de_tokens_error::<Vec<isize>>(
1352 &[Token::Unit],
1353 "invalid type: unit value, expected a sequence",
1354 );
1355 }
1356
1357 #[test]
test_vec_from_unit_struct()1358 fn test_vec_from_unit_struct() {
1359 assert_de_tokens_error::<Vec<isize>>(
1360 &[Token::UnitStruct { name: "Anything" }],
1361 "invalid type: unit value, expected a sequence",
1362 );
1363 }
1364
1365 #[test]
test_zero_array_from_unit()1366 fn test_zero_array_from_unit() {
1367 assert_de_tokens_error::<[isize; 0]>(
1368 &[Token::Unit],
1369 "invalid type: unit value, expected an empty array",
1370 );
1371 }
1372
1373 #[test]
test_zero_array_from_unit_struct()1374 fn test_zero_array_from_unit_struct() {
1375 assert_de_tokens_error::<[isize; 0]>(
1376 &[Token::UnitStruct { name: "Anything" }],
1377 "invalid type: unit value, expected an empty array",
1378 );
1379 }
1380
1381 #[test]
test_btreemap_from_unit()1382 fn test_btreemap_from_unit() {
1383 assert_de_tokens_error::<BTreeMap<isize, isize>>(
1384 &[Token::Unit],
1385 "invalid type: unit value, expected a map",
1386 );
1387 }
1388
1389 #[test]
test_btreemap_from_unit_struct()1390 fn test_btreemap_from_unit_struct() {
1391 assert_de_tokens_error::<BTreeMap<isize, isize>>(
1392 &[Token::UnitStruct { name: "Anything" }],
1393 "invalid type: unit value, expected a map",
1394 );
1395 }
1396
1397 #[test]
test_hashmap_from_unit()1398 fn test_hashmap_from_unit() {
1399 assert_de_tokens_error::<HashMap<isize, isize>>(
1400 &[Token::Unit],
1401 "invalid type: unit value, expected a map",
1402 );
1403 }
1404
1405 #[test]
test_hashmap_from_unit_struct()1406 fn test_hashmap_from_unit_struct() {
1407 assert_de_tokens_error::<HashMap<isize, isize>>(
1408 &[Token::UnitStruct { name: "Anything" }],
1409 "invalid type: unit value, expected a map",
1410 );
1411 }
1412
1413 #[test]
test_bool_from_string()1414 fn test_bool_from_string() {
1415 assert_de_tokens_error::<bool>(
1416 &[Token::Str("false")],
1417 "invalid type: string \"false\", expected a boolean",
1418 );
1419 }
1420
1421 #[test]
test_number_from_string()1422 fn test_number_from_string() {
1423 assert_de_tokens_error::<isize>(
1424 &[Token::Str("1")],
1425 "invalid type: string \"1\", expected isize",
1426 );
1427 }
1428
1429 #[test]
test_integer_from_float()1430 fn test_integer_from_float() {
1431 assert_de_tokens_error::<isize>(
1432 &[Token::F32(0.0)],
1433 "invalid type: floating point `0`, expected isize",
1434 );
1435 }
1436
1437 #[test]
test_unit_struct_from_seq()1438 fn test_unit_struct_from_seq() {
1439 assert_de_tokens_error::<UnitStruct>(
1440 &[Token::Seq { len: Some(0) }, Token::SeqEnd],
1441 "invalid type: sequence, expected unit struct UnitStruct",
1442 );
1443 }
1444
1445 #[test]
test_wrapping_overflow()1446 fn test_wrapping_overflow() {
1447 assert_de_tokens_error::<Wrapping<u16>>(
1448 &[Token::U32(65_536)],
1449 "invalid value: integer `65536`, expected u16",
1450 );
1451 }
1452
1453 #[test]
test_duration_overflow_seq()1454 fn test_duration_overflow_seq() {
1455 assert_de_tokens_error::<Duration>(
1456 &[
1457 Token::Seq { len: Some(2) },
1458 Token::U64(u64::max_value()),
1459 Token::U32(1_000_000_000),
1460 Token::SeqEnd,
1461 ],
1462 "overflow deserializing Duration",
1463 );
1464 }
1465
1466 #[test]
test_duration_overflow_struct()1467 fn test_duration_overflow_struct() {
1468 assert_de_tokens_error::<Duration>(
1469 &[
1470 Token::Struct {
1471 name: "Duration",
1472 len: 2,
1473 },
1474 Token::Str("secs"),
1475 Token::U64(u64::max_value()),
1476 Token::Str("nanos"),
1477 Token::U32(1_000_000_000),
1478 Token::StructEnd,
1479 ],
1480 "overflow deserializing Duration",
1481 );
1482 }
1483
1484 #[test]
test_systemtime_overflow_seq()1485 fn test_systemtime_overflow_seq() {
1486 assert_de_tokens_error::<SystemTime>(
1487 &[
1488 Token::Seq { len: Some(2) },
1489 Token::U64(u64::max_value()),
1490 Token::U32(1_000_000_000),
1491 Token::SeqEnd,
1492 ],
1493 "overflow deserializing SystemTime epoch offset",
1494 );
1495 }
1496
1497 #[test]
test_systemtime_overflow_struct()1498 fn test_systemtime_overflow_struct() {
1499 assert_de_tokens_error::<SystemTime>(
1500 &[
1501 Token::Struct {
1502 name: "SystemTime",
1503 len: 2,
1504 },
1505 Token::Str("secs_since_epoch"),
1506 Token::U64(u64::max_value()),
1507 Token::Str("nanos_since_epoch"),
1508 Token::U32(1_000_000_000),
1509 Token::StructEnd,
1510 ],
1511 "overflow deserializing SystemTime epoch offset",
1512 );
1513 }
1514
1515 #[cfg(systemtime_checked_add)]
1516 #[test]
test_systemtime_overflow()1517 fn test_systemtime_overflow() {
1518 assert_de_tokens_error::<SystemTime>(
1519 &[
1520 Token::Seq { len: Some(2) },
1521 Token::U64(u64::max_value()),
1522 Token::U32(0),
1523 Token::SeqEnd,
1524 ],
1525 "overflow deserializing SystemTime",
1526 );
1527 }
1528
1529 #[test]
test_cstr_internal_null()1530 fn test_cstr_internal_null() {
1531 assert_de_tokens_error::<Box<CStr>>(
1532 &[Token::Bytes(b"a\0c")],
1533 "nul byte found in provided data at position: 1",
1534 );
1535 }
1536
1537 #[test]
test_cstr_internal_null_end()1538 fn test_cstr_internal_null_end() {
1539 assert_de_tokens_error::<Box<CStr>>(
1540 &[Token::Bytes(b"ac\0")],
1541 "nul byte found in provided data at position: 2",
1542 );
1543 }
1544
1545 #[cfg(feature = "unstable")]
1546 #[test]
test_never_type()1547 fn test_never_type() {
1548 assert_de_tokens_error::<!>(&[], "cannot deserialize `!`");
1549
1550 assert_de_tokens_error::<Result<u8, !>>(
1551 &[Token::NewtypeVariant {
1552 name: "Result",
1553 variant: "Err",
1554 }],
1555 "cannot deserialize `!`",
1556 );
1557 }
1558