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