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