1use super::{Deserialize, Error, Serialize};
2use crate::{elements, io};
3use alloc::{string::String, vec::Vec};
4
5#[cfg(feature = "reduced-stack-buffer")]
6const PRIMITIVES_BUFFER_LENGTH: usize = 256;
7
8#[cfg(not(feature = "reduced-stack-buffer"))]
9const PRIMITIVES_BUFFER_LENGTH: usize = 1024;
10
11#[derive(Debug, Copy, Clone, PartialEq)]
14pub struct VarUint32(u32);
15
16impl From<VarUint32> for usize {
17 fn from(var: VarUint32) -> usize {
18 var.0 as usize
19 }
20}
21
22impl From<VarUint32> for u32 {
23 fn from(var: VarUint32) -> u32 {
24 var.0
25 }
26}
27
28impl From<u32> for VarUint32 {
29 fn from(i: u32) -> VarUint32 {
30 VarUint32(i)
31 }
32}
33
34impl From<usize> for VarUint32 {
35 fn from(i: usize) -> VarUint32 {
36 assert!(i <= u32::max_value() as usize);
37 VarUint32(i as u32)
38 }
39}
40
41impl Deserialize for VarUint32 {
42 type Error = Error;
43
44 fn deserialize<R: io::Read>(reader: &mut R) -> Result<Self, Self::Error> {
45 let mut res = 0;
46 let mut shift = 0;
47 let mut u8buf = [0u8; 1];
48 loop {
49 if shift > 31 {
50 return Err(Error::InvalidVarUint32)
51 }
52
53 reader.read(&mut u8buf)?;
54 let b = u8buf[0] as u32;
55 res |= (b & 0x7f).checked_shl(shift).ok_or(Error::InvalidVarUint32)?;
56 shift += 7;
57 if (b >> 7) == 0 {
58 if shift >= 32 && (b as u8).leading_zeros() < 4 {
59 return Err(Error::InvalidVarInt32)
60 }
61 break
62 }
63 }
64 Ok(VarUint32(res))
65 }
66}
67
68impl Serialize for VarUint32 {
69 type Error = Error;
70
71 fn serialize<W: io::Write>(self, writer: &mut W) -> Result<(), Self::Error> {
72 let mut buf = [0u8; 1];
73 let mut v = self.0;
74 loop {
75 buf[0] = (v & 0b0111_1111) as u8;
76 v >>= 7;
77 if v > 0 {
78 buf[0] |= 0b1000_0000;
79 }
80 writer.write(&buf[..])?;
81 if v == 0 {
82 break
83 }
84 }
85
86 Ok(())
87 }
88}
89
90#[derive(Debug, Copy, Clone, PartialEq)]
93pub struct VarUint64(u64);
94
95impl From<VarUint64> for u64 {
96 fn from(var: VarUint64) -> u64 {
97 var.0
98 }
99}
100
101impl Deserialize for VarUint64 {
102 type Error = Error;
103
104 fn deserialize<R: io::Read>(reader: &mut R) -> Result<Self, Self::Error> {
105 let mut res = 0;
106 let mut shift = 0;
107 let mut u8buf = [0u8; 1];
108 loop {
109 if shift > 63 {
110 return Err(Error::InvalidVarUint64)
111 }
112
113 reader.read(&mut u8buf)?;
114 let b = u8buf[0] as u64;
115 res |= (b & 0x7f).checked_shl(shift).ok_or(Error::InvalidVarUint64)?;
116 shift += 7;
117 if (b >> 7) == 0 {
118 if shift >= 64 && (b as u8).leading_zeros() < 7 {
119 return Err(Error::InvalidVarInt64)
120 }
121 break
122 }
123 }
124 Ok(VarUint64(res))
125 }
126}
127
128impl Serialize for VarUint64 {
129 type Error = Error;
130
131 fn serialize<W: io::Write>(self, writer: &mut W) -> Result<(), Self::Error> {
132 let mut buf = [0u8; 1];
133 let mut v = self.0;
134 loop {
135 buf[0] = (v & 0b0111_1111) as u8;
136 v >>= 7;
137 if v > 0 {
138 buf[0] |= 0b1000_0000;
139 }
140 writer.write(&buf[..])?;
141 if v == 0 {
142 break
143 }
144 }
145
146 Ok(())
147 }
148}
149
150impl From<u64> for VarUint64 {
151 fn from(u: u64) -> VarUint64 {
152 VarUint64(u)
153 }
154}
155
156#[derive(Debug, Copy, Clone, PartialEq)]
158pub struct VarUint7(u8);
159
160impl From<VarUint7> for u8 {
161 fn from(v: VarUint7) -> u8 {
162 v.0
163 }
164}
165
166impl From<u8> for VarUint7 {
167 fn from(v: u8) -> Self {
168 VarUint7(v)
169 }
170}
171
172impl Deserialize for VarUint7 {
173 type Error = Error;
174
175 fn deserialize<R: io::Read>(reader: &mut R) -> Result<Self, Self::Error> {
176 let mut u8buf = [0u8; 1];
177 reader.read(&mut u8buf)?;
178 Ok(VarUint7(u8buf[0]))
179 }
180}
181
182impl Serialize for VarUint7 {
183 type Error = Error;
184
185 fn serialize<W: io::Write>(self, writer: &mut W) -> Result<(), Self::Error> {
186 writer.write(&[self.0])?;
188 Ok(())
189 }
190}
191
192#[derive(Debug, Copy, Clone, PartialEq)]
194pub struct VarInt7(i8);
195
196impl From<VarInt7> for i8 {
197 fn from(v: VarInt7) -> i8 {
198 v.0
199 }
200}
201
202impl From<i8> for VarInt7 {
203 fn from(v: i8) -> VarInt7 {
204 VarInt7(v)
205 }
206}
207
208impl Deserialize for VarInt7 {
209 type Error = Error;
210
211 fn deserialize<R: io::Read>(reader: &mut R) -> Result<Self, Self::Error> {
212 let mut u8buf = [0u8; 1];
213 reader.read(&mut u8buf)?;
214
215 if u8buf[0] & 0b1000_0000 != 0 {
217 return Err(Error::InvalidVarInt7(u8buf[0]))
218 }
219
220 if u8buf[0] & 0b0100_0000 == 0b0100_0000 {
222 u8buf[0] |= 0b1000_0000
223 }
224
225 Ok(VarInt7(u8buf[0] as i8))
226 }
227}
228
229impl Serialize for VarInt7 {
230 type Error = Error;
231
232 fn serialize<W: io::Write>(self, writer: &mut W) -> Result<(), Self::Error> {
233 let mut b: u8 = self.0 as u8;
235 if self.0 < 0 {
236 b |= 0b0100_0000;
237 b &= 0b0111_1111;
238 }
239 writer.write(&[b])?;
240 Ok(())
241 }
242}
243
244#[derive(Debug, Copy, Clone, PartialEq)]
247pub struct Uint8(u8);
248
249impl From<Uint8> for u8 {
250 fn from(v: Uint8) -> u8 {
251 v.0
252 }
253}
254
255impl From<u8> for Uint8 {
256 fn from(v: u8) -> Self {
257 Uint8(v)
258 }
259}
260
261impl Deserialize for Uint8 {
262 type Error = Error;
263
264 fn deserialize<R: io::Read>(reader: &mut R) -> Result<Self, Self::Error> {
265 let mut u8buf = [0u8; 1];
266 reader.read(&mut u8buf)?;
267 Ok(Uint8(u8buf[0]))
268 }
269}
270
271impl Serialize for Uint8 {
272 type Error = Error;
273
274 fn serialize<W: io::Write>(self, writer: &mut W) -> Result<(), Self::Error> {
275 writer.write(&[self.0])?;
276 Ok(())
277 }
278}
279
280#[derive(Debug, Copy, Clone, PartialEq)]
282pub struct VarInt32(i32);
283
284impl From<VarInt32> for i32 {
285 fn from(v: VarInt32) -> i32 {
286 v.0
287 }
288}
289
290impl From<i32> for VarInt32 {
291 fn from(v: i32) -> VarInt32 {
292 VarInt32(v)
293 }
294}
295
296impl Deserialize for VarInt32 {
297 type Error = Error;
298
299 fn deserialize<R: io::Read>(reader: &mut R) -> Result<Self, Self::Error> {
300 let mut res = 0;
301 let mut shift = 0;
302 let mut u8buf = [0u8; 1];
303 loop {
304 if shift > 31 {
305 return Err(Error::InvalidVarInt32)
306 }
307 reader.read(&mut u8buf)?;
308 let b = u8buf[0];
309
310 res |= ((b & 0x7f) as i32).checked_shl(shift).ok_or(Error::InvalidVarInt32)?;
311
312 shift += 7;
313 if (b >> 7) == 0 {
314 if shift < 32 && b & 0b0100_0000 == 0b0100_0000 {
315 res |= (1i32 << shift).wrapping_neg();
316 } else if shift >= 32 && b & 0b0100_0000 == 0b0100_0000 {
317 if (!(b | 0b1000_0000)).leading_zeros() < 5 {
318 return Err(Error::InvalidVarInt32)
319 }
320 } else if shift >= 32 && b & 0b0100_0000 == 0 && b.leading_zeros() < 5 {
321 return Err(Error::InvalidVarInt32)
322 }
323 break
324 }
325 }
326 Ok(VarInt32(res))
327 }
328}
329
330impl Serialize for VarInt32 {
331 type Error = Error;
332
333 fn serialize<W: io::Write>(self, writer: &mut W) -> Result<(), Self::Error> {
334 let mut buf = [0u8; 1];
335 let mut v = self.0;
336 let mut more = true;
337 while more {
338 buf[0] = (v & 0b0111_1111) as u8;
339 v >>= 7;
340 if (v == 0 && buf[0] & 0b0100_0000 == 0) ||
341 (v == -1 && buf[0] & 0b0100_0000 == 0b0100_0000)
342 {
343 more = false
344 } else {
345 buf[0] |= 0b1000_0000
346 }
347
348 writer.write(&buf[..])?;
349 }
350
351 Ok(())
352 }
353}
354
355#[derive(Debug, Copy, Clone, PartialEq)]
357pub struct VarInt64(i64);
358
359impl From<VarInt64> for i64 {
360 fn from(v: VarInt64) -> i64 {
361 v.0
362 }
363}
364
365impl From<i64> for VarInt64 {
366 fn from(v: i64) -> VarInt64 {
367 VarInt64(v)
368 }
369}
370
371impl Deserialize for VarInt64 {
372 type Error = Error;
373
374 fn deserialize<R: io::Read>(reader: &mut R) -> Result<Self, Self::Error> {
375 let mut res = 0i64;
376 let mut shift = 0;
377 let mut u8buf = [0u8; 1];
378
379 loop {
380 if shift > 63 {
381 return Err(Error::InvalidVarInt64)
382 }
383 reader.read(&mut u8buf)?;
384 let b = u8buf[0];
385
386 res |= ((b & 0x7f) as i64).checked_shl(shift).ok_or(Error::InvalidVarInt64)?;
387
388 shift += 7;
389 if (b >> 7) == 0 {
390 if shift < 64 && b & 0b0100_0000 == 0b0100_0000 {
391 res |= (1i64 << shift).wrapping_neg();
392 } else if shift >= 64 && b & 0b0100_0000 == 0b0100_0000 {
393 if (b | 0b1000_0000) as i8 != -1 {
394 return Err(Error::InvalidVarInt64)
395 }
396 } else if shift >= 64 && b != 0 {
397 return Err(Error::InvalidVarInt64)
398 }
399 break
400 }
401 }
402 Ok(VarInt64(res))
403 }
404}
405
406impl Serialize for VarInt64 {
407 type Error = Error;
408
409 fn serialize<W: io::Write>(self, writer: &mut W) -> Result<(), Self::Error> {
410 let mut buf = [0u8; 1];
411 let mut v = self.0;
412 let mut more = true;
413 while more {
414 buf[0] = (v & 0b0111_1111) as u8;
415 v >>= 7;
416 if (v == 0 && buf[0] & 0b0100_0000 == 0) ||
417 (v == -1 && buf[0] & 0b0100_0000 == 0b0100_0000)
418 {
419 more = false
420 } else {
421 buf[0] |= 0b1000_0000
422 }
423
424 writer.write(&buf[..])?;
425 }
426
427 Ok(())
428 }
429}
430
431#[derive(Debug, Copy, Clone, PartialEq)]
433pub struct Uint32(u32);
434
435impl Deserialize for Uint32 {
436 type Error = Error;
437
438 fn deserialize<R: io::Read>(reader: &mut R) -> Result<Self, Self::Error> {
439 let mut buf = [0u8; 4];
440 reader.read(&mut buf)?;
441 Ok(u32::from_le_bytes(buf).into())
443 }
444}
445
446impl From<Uint32> for u32 {
447 fn from(var: Uint32) -> u32 {
448 var.0
449 }
450}
451
452impl Serialize for Uint32 {
453 type Error = Error;
454
455 fn serialize<W: io::Write>(self, writer: &mut W) -> Result<(), Self::Error> {
456 writer.write(&self.0.to_le_bytes())?;
457 Ok(())
458 }
459}
460
461impl From<u32> for Uint32 {
462 fn from(u: u32) -> Self {
463 Uint32(u)
464 }
465}
466
467#[derive(Debug, Copy, Clone, PartialEq)]
469pub struct Uint64(u64);
470
471impl Deserialize for Uint64 {
472 type Error = Error;
473
474 fn deserialize<R: io::Read>(reader: &mut R) -> Result<Self, Self::Error> {
475 let mut buf = [0u8; 8];
476 reader.read(&mut buf)?;
477 Ok(u64::from_le_bytes(buf).into())
479 }
480}
481
482impl Serialize for Uint64 {
483 type Error = Error;
484
485 fn serialize<W: io::Write>(self, writer: &mut W) -> Result<(), Self::Error> {
486 writer.write(&self.0.to_le_bytes())?;
487 Ok(())
488 }
489}
490
491impl From<u64> for Uint64 {
492 fn from(u: u64) -> Self {
493 Uint64(u)
494 }
495}
496
497impl From<Uint64> for u64 {
498 fn from(var: Uint64) -> u64 {
499 var.0
500 }
501}
502
503#[derive(Debug, Copy, Clone, PartialEq)]
505pub struct VarUint1(bool);
506
507impl From<VarUint1> for bool {
508 fn from(v: VarUint1) -> bool {
509 v.0
510 }
511}
512
513impl From<bool> for VarUint1 {
514 fn from(b: bool) -> Self {
515 VarUint1(b)
516 }
517}
518
519impl Deserialize for VarUint1 {
520 type Error = Error;
521
522 fn deserialize<R: io::Read>(reader: &mut R) -> Result<Self, Self::Error> {
523 let mut u8buf = [0u8; 1];
524 reader.read(&mut u8buf)?;
525 match u8buf[0] {
526 0 => Ok(VarUint1(false)),
527 1 => Ok(VarUint1(true)),
528 v => Err(Error::InvalidVarUint1(v)),
529 }
530 }
531}
532
533impl Serialize for VarUint1 {
534 type Error = Error;
535
536 fn serialize<W: io::Write>(self, writer: &mut W) -> Result<(), Self::Error> {
537 writer.write(&[if self.0 { 1u8 } else { 0u8 }])?;
538 Ok(())
539 }
540}
541
542impl Deserialize for String {
543 type Error = Error;
544
545 fn deserialize<R: io::Read>(reader: &mut R) -> Result<Self, Self::Error> {
546 let length = u32::from(VarUint32::deserialize(reader)?) as usize;
547 if length > 0 {
548 String::from_utf8(buffered_read!(PRIMITIVES_BUFFER_LENGTH, length, reader))
549 .map_err(|_| Error::NonUtf8String)
550 } else {
551 Ok(String::new())
552 }
553 }
554}
555
556impl Serialize for String {
557 type Error = Error;
558
559 fn serialize<W: io::Write>(self, writer: &mut W) -> Result<(), Error> {
560 VarUint32::from(self.len()).serialize(writer)?;
561 writer.write(&self.into_bytes()[..])?;
562 Ok(())
563 }
564}
565
566#[derive(Debug, Clone)]
569pub struct CountedList<T: Deserialize>(Vec<T>);
570
571impl<T: Deserialize> CountedList<T> {
572 pub fn into_inner(self) -> Vec<T> {
574 self.0
575 }
576}
577
578impl<T: Deserialize> Deserialize for CountedList<T>
579where
580 T::Error: From<Error>,
581{
582 type Error = T::Error;
583
584 fn deserialize<R: io::Read>(reader: &mut R) -> Result<Self, Self::Error> {
585 let count: usize = VarUint32::deserialize(reader)?.into();
586 let mut result = Vec::new();
587 for _ in 0..count {
588 result.push(T::deserialize(reader)?);
589 }
590 Ok(CountedList(result))
591 }
592}
593
594#[derive(Debug)]
597pub struct CountedWriter<'a, W: 'a + io::Write> {
598 writer: &'a mut W,
599 data: Vec<u8>,
600}
601
602impl<'a, W: 'a + io::Write> CountedWriter<'a, W> {
603 pub fn new(writer: &'a mut W) -> Self {
605 CountedWriter { writer, data: Vec::new() }
606 }
607
608 pub fn done(self) -> io::Result<()> {
611 let writer = self.writer;
612 let data = self.data;
613 VarUint32::from(data.len())
614 .serialize(writer)
615 .map_err(|_| io::Error::InvalidData)?;
616 writer.write(&data[..])?;
617 Ok(())
618 }
619}
620
621impl<'a, W: 'a + io::Write> io::Write for CountedWriter<'a, W> {
622 fn write(&mut self, buf: &[u8]) -> io::Result<()> {
623 self.data.extend_from_slice(buf);
624 Ok(())
625 }
626}
627
628#[derive(Debug, Clone)]
631pub struct CountedListWriter<I: Serialize<Error = elements::Error>, T: IntoIterator<Item = I>>(
632 pub usize,
633 pub T,
634);
635
636impl<I: Serialize<Error = elements::Error>, T: IntoIterator<Item = I>> Serialize
637 for CountedListWriter<I, T>
638{
639 type Error = Error;
640
641 fn serialize<W: io::Write>(self, writer: &mut W) -> Result<(), Self::Error> {
642 let len_us = self.0;
643 let data = self.1;
644 let len: VarUint32 = len_us.into();
645 len.serialize(writer)?;
646 for data_element in data {
647 data_element.serialize(writer)?
648 }
649
650 Ok(())
651 }
652}
653
654#[cfg(test)]
655mod tests {
656
657 use super::{
658 super::{deserialize_buffer, Serialize},
659 CountedList, VarInt32, VarInt64, VarInt7, VarUint32, VarUint64,
660 };
661 use crate::elements::Error;
662
663 fn varuint32_ser_test(val: u32, expected: Vec<u8>) {
664 let mut buf = Vec::new();
665 let v1: VarUint32 = val.into();
666 v1.serialize(&mut buf).expect("to be serialized ok");
667 assert_eq!(expected, buf);
668 }
669
670 fn varuint32_de_test(dt: Vec<u8>, expected: u32) {
671 let val: VarUint32 = super::super::deserialize_buffer(&dt).expect("buf to be serialized");
672 assert_eq!(expected, val.into());
673 }
674
675 fn varuint32_serde_test(dt: Vec<u8>, val: u32) {
676 varuint32_de_test(dt.clone(), val);
677 varuint32_ser_test(val, dt);
678 }
679
680 fn varint32_ser_test(val: i32, expected: Vec<u8>) {
681 let mut buf = Vec::new();
682 let v1: VarInt32 = val.into();
683 v1.serialize(&mut buf).expect("to be serialized ok");
684 assert_eq!(expected, buf);
685 }
686
687 fn varint32_de_test(dt: Vec<u8>, expected: i32) {
688 let val: VarInt32 = super::super::deserialize_buffer(&dt).expect("buf to be serialized");
689 assert_eq!(expected, val.into());
690 }
691
692 fn varint32_serde_test(dt: Vec<u8>, val: i32) {
693 varint32_de_test(dt.clone(), val);
694 varint32_ser_test(val, dt);
695 }
696
697 fn varuint64_ser_test(val: u64, expected: Vec<u8>) {
698 let mut buf = Vec::new();
699 let v1: VarUint64 = val.into();
700 v1.serialize(&mut buf).expect("to be serialized ok");
701 assert_eq!(expected, buf);
702 }
703
704 fn varuint64_de_test(dt: Vec<u8>, expected: u64) {
705 let val: VarUint64 = super::super::deserialize_buffer(&dt).expect("buf to be serialized");
706 assert_eq!(expected, val.into());
707 }
708
709 fn varuint64_serde_test(dt: Vec<u8>, val: u64) {
710 varuint64_de_test(dt.clone(), val);
711 varuint64_ser_test(val, dt);
712 }
713
714 fn varint64_ser_test(val: i64, expected: Vec<u8>) {
715 let mut buf = Vec::new();
716 let v1: VarInt64 = val.into();
717 v1.serialize(&mut buf).expect("to be serialized ok");
718 assert_eq!(expected, buf);
719 }
720
721 fn varint64_de_test(dt: Vec<u8>, expected: i64) {
722 let val: VarInt64 = super::super::deserialize_buffer(&dt).expect("buf to be serialized");
723 assert_eq!(expected, val.into());
724 }
725
726 fn varint64_serde_test(dt: Vec<u8>, val: i64) {
727 varint64_de_test(dt.clone(), val);
728 varint64_ser_test(val, dt);
729 }
730
731 #[test]
732 fn varuint32_0() {
733 varuint32_serde_test(vec![0u8; 1], 0);
734 }
735
736 #[test]
737 fn varuint32_1() {
738 varuint32_serde_test(vec![1u8; 1], 1);
739 }
740
741 #[test]
742 fn varuint32_135() {
743 varuint32_serde_test(vec![135u8, 0x01], 135);
744 }
745
746 #[test]
747 fn varuint32_8192() {
748 varuint32_serde_test(vec![0x80, 0x40], 8192);
749 }
750
751 #[test]
752 fn varint32_8192() {
753 varint32_serde_test(vec![0x80, 0xc0, 0x00], 8192);
754 }
755
756 #[test]
757 fn varint32_neg_8192() {
758 varint32_serde_test(vec![0x80, 0x40], -8192);
759 }
760
761 #[test]
762 fn varuint64_0() {
763 varuint64_serde_test(vec![0u8; 1], 0);
764 }
765
766 #[test]
767 fn varuint64_1() {
768 varuint64_serde_test(vec![1u8; 1], 1);
769 }
770
771 #[test]
772 fn varuint64_135() {
773 varuint64_serde_test(vec![135u8, 0x01], 135);
774 }
775
776 #[test]
777 fn varuint64_8192() {
778 varuint64_serde_test(vec![0x80, 0x40], 8192);
779 }
780
781 #[test]
782 fn varint64_8192() {
783 varint64_serde_test(vec![0x80, 0xc0, 0x00], 8192);
784 }
785
786 #[test]
787 fn varint64_neg_8192() {
788 varint64_serde_test(vec![0x80, 0x40], -8192);
789 }
790
791 #[test]
792 fn varint64_min() {
793 varint64_serde_test(
794 vec![0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x7f],
795 -9223372036854775808,
796 );
797 }
798
799 #[test]
800 fn varint64_bad_extended() {
801 let res = deserialize_buffer::<VarInt64>(
802 &[0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x6f][..],
803 );
804 assert!(res.is_err());
805 }
806
807 #[test]
808 fn varint32_bad_extended() {
809 let res = deserialize_buffer::<VarInt32>(&[0x80, 0x80, 0x80, 0x80, 0x6f][..]);
810 assert!(res.is_err());
811 }
812
813 #[test]
814 fn varint32_bad_extended2() {
815 let res = deserialize_buffer::<VarInt32>(&[0x80, 0x80, 0x80, 0x80, 0x41][..]);
816 assert!(res.is_err());
817 }
818
819 #[test]
820 fn varint64_max() {
821 varint64_serde_test(
822 vec![0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00],
823 9223372036854775807,
824 );
825 }
826
827 #[test]
828 fn varint64_too_long() {
829 assert!(deserialize_buffer::<VarInt64>(
830 &[0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00][..],
831 )
832 .is_err());
833 }
834
835 #[test]
836 fn varint32_too_long() {
837 assert!(deserialize_buffer::<VarInt32>(&[0xff, 0xff, 0xff, 0xff, 0xff, 0x00][..],).is_err());
838 }
839
840 #[test]
841 fn varuint64_too_long() {
842 assert!(deserialize_buffer::<VarUint64>(
843 &[0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00][..],
844 )
845 .is_err());
846 }
847
848 #[test]
849 fn varuint32_too_long() {
850 assert!(
851 deserialize_buffer::<VarUint32>(&[0xff, 0xff, 0xff, 0xff, 0xff, 0x00][..],).is_err()
852 );
853 }
854
855 #[test]
856 fn varuint32_too_long_trailing() {
857 assert!(deserialize_buffer::<VarUint32>(&[0xff, 0xff, 0xff, 0xff, 0x7f][..],).is_err());
858 }
859
860 #[test]
861 fn varuint64_too_long_trailing() {
862 assert!(deserialize_buffer::<VarUint64>(
863 &[0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x04][..],
864 )
865 .is_err());
866 }
867
868 #[test]
869 fn varint32_min() {
870 varint32_serde_test(vec![0x80, 0x80, 0x80, 0x80, 0x78], -2147483648);
871 }
872
873 #[test]
874 fn varint7_invalid() {
875 match deserialize_buffer::<VarInt7>(&[240]) {
876 Err(Error::InvalidVarInt7(_)) => {},
877 _ => panic!("Should be invalid varint7 error!"),
878 }
879 }
880
881 #[test]
882 fn varint7_neg() {
883 assert_eq!(-0x10i8, deserialize_buffer::<VarInt7>(&[0x70]).expect("fail").into());
884 }
885
886 #[test]
887 fn varuint32_too_long_nulled() {
888 match deserialize_buffer::<VarUint32>(&[
889 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x78,
890 ]) {
891 Err(Error::InvalidVarUint32) => {},
892 _ => panic!("Should be invalid varuint32"),
893 }
894 }
895
896 #[test]
897 fn varint32_max() {
898 varint32_serde_test(vec![0xff, 0xff, 0xff, 0xff, 0x07], 2147483647);
899 }
900
901 #[test]
902 fn counted_list() {
903 let payload = [
904 133u8, 0x80, 0x80, 0x80, 0x0, 0x01, 0x7d, 0x05, 0x07, 0x09,
907 ];
908
909 let list: CountedList<VarInt7> =
910 deserialize_buffer(&payload).expect("type_section be deserialized");
911
912 let vars = list.into_inner();
913 assert_eq!(5, vars.len());
914 let v3: i8 = (*vars.get(1).unwrap()).into();
915 assert_eq!(-0x03i8, v3);
916 }
917}