1pub mod buffer;
2pub mod error;
3pub mod fixint;
4mod json;
5pub mod protobuf;
6pub mod varint;
7
8#[cfg(test)]
9mod tests {
10 use super::*;
11 use crate::buffer::Reader;
12 use crate::protobuf::ProtoData::Message;
13 use crate::protobuf::{decode_protobuf, Map, ProtoData};
14 use std::vec;
15
16 #[test]
17 fn test_decode_protobuf() {
18 let pb = read_protobuf(
19 "08d2fe061d424b1d002952bf0100000000003a0d3131343531343139313938313042047468697342026973420161420872657065617465644206737472696e674a10ec9962d372ce9be816be0b7fdea0127b5210292c88aab6b4386c5259b3db5bb3d83552106c67dcd74452b07dbab383af60213edc5210625b2652e828a520e2d3738fbc7623f952105c4627267501260e1635696f21df442352100e4f254dabe6afe218d47229213fce2e5210d61fea3ad5f6c7de29cb530974daebe85210bd7969536028618e616db54cc5bc17ab5210b82c3166710a0373d0fae7bfa5da6a405210aacb0a4e4eb2400cf3a9674cbd9fac30521056b3e167c737ac1d52770b789ba1e04a5210d99426dc60b67af15121a38816fa33c35210328fce15be2219c6cd950241d9db083652102db0c75a59f5527584b2924a32995f625210258d410b8fcf29b7c8405ae99560d43a5210eeafec9d7d49109a08d5d36f26e5f94852107aa31e3a68c0ea5cf197a9613a68de46",
20 )
21 .unwrap();
22
23 println!("{}", pb);
24
25 let mut expect_pb = Map::new();
26 expect_pb.insert(1, ProtoData::Varint(114514));
27 expect_pb.insert(3, ProtoData::Fix32(1919810));
28 expect_pb.insert(5, ProtoData::Fix64(114514));
29 expect_pb.insert(7, ProtoData::String("1145141919810".to_string()));
30 expect_pb.insert(
31 8,
32 ProtoData::Repeated(vec![
33 ProtoData::String("this".to_string()),
34 ProtoData::String("is".to_string()),
35 ProtoData::String("a".to_string()),
36 ProtoData::String("repeated".to_string()),
37 ProtoData::String("string".to_string()),
38 ]),
39 );
40 expect_pb.insert(
41 9,
42 ProtoData::Bytes(
43 hex::decode("ec 99 62 d3 72 ce 9b e8 16 be 0b 7f de a0 12 7b".replace(" ", ""))
44 .unwrap(),
45 ),
46 );
47 expect_pb.insert(
48 10,
49 ProtoData::Repeated(vec![
50 ProtoData::Bytes(
51 hex::decode("29 2c 88 aa b6 b4 38 6c 52 59 b3 db 5b b3 d8 35".replace(" ", ""))
52 .unwrap(),
53 ),
54 ProtoData::Bytes(
55 hex::decode("6c 67 dc d7 44 52 b0 7d ba b3 83 af 60 21 3e dc".replace(" ", ""))
56 .unwrap(),
57 ),
58 ProtoData::Bytes(
59 hex::decode("62 5b 26 52 e8 28 a5 20 e2 d3 73 8f bc 76 23 f9".replace(" ", ""))
60 .unwrap(),
61 ),
62 ProtoData::Bytes(
63 hex::decode("5c 46 27 26 75 01 26 0e 16 35 69 6f 21 df 44 23".replace(" ", ""))
64 .unwrap(),
65 ),
66 ProtoData::Bytes(
67 hex::decode("0e 4f 25 4d ab e6 af e2 18 d4 72 29 21 3f ce 2e".replace(" ", ""))
68 .unwrap(),
69 ),
70 ProtoData::Bytes(
71 hex::decode("d6 1f ea 3a d5 f6 c7 de 29 cb 53 09 74 da eb e8".replace(" ", ""))
72 .unwrap(),
73 ),
74 ProtoData::Bytes(
75 hex::decode("bd 79 69 53 60 28 61 8e 61 6d b5 4c c5 bc 17 ab".replace(" ", ""))
76 .unwrap(),
77 ),
78 ProtoData::Bytes(
79 hex::decode("b8 2c 31 66 71 0a 03 73 d0 fa e7 bf a5 da 6a 40".replace(" ", ""))
80 .unwrap(),
81 ),
82 ProtoData::Bytes(
83 hex::decode("aa cb 0a 4e 4e b2 40 0c f3 a9 67 4c bd 9f ac 30".replace(" ", ""))
84 .unwrap(),
85 ),
86 ProtoData::Bytes(
87 hex::decode("56 b3 e1 67 c7 37 ac 1d 52 77 0b 78 9b a1 e0 4a".replace(" ", ""))
88 .unwrap(),
89 ),
90 ProtoData::Bytes(
91 hex::decode("d9 94 26 dc 60 b6 7a f1 51 21 a3 88 16 fa 33 c3".replace(" ", ""))
92 .unwrap(),
93 ),
94 ProtoData::Bytes(
95 hex::decode("32 8f ce 15 be 22 19 c6 cd 95 02 41 d9 db 08 36".replace(" ", ""))
96 .unwrap(),
97 ),
98 ProtoData::Bytes(
99 hex::decode("2d b0 c7 5a 59 f5 52 75 84 b2 92 4a 32 99 5f 62".replace(" ", ""))
100 .unwrap(),
101 ),
102 ProtoData::Bytes(
103 hex::decode("25 8d 41 0b 8f cf 29 b7 c8 40 5a e9 95 60 d4 3a".replace(" ", ""))
104 .unwrap(),
105 ),
106 ProtoData::Bytes(
107 hex::decode("ee af ec 9d 7d 49 10 9a 08 d5 d3 6f 26 e5 f9 48".replace(" ", ""))
108 .unwrap(),
109 ),
110 ProtoData::Bytes(
111 hex::decode("7a a3 1e 3a 68 c0 ea 5c f1 97 a9 61 3a 68 de 46".replace(" ", ""))
112 .unwrap(),
113 ),
114 ]),
115 );
116
117 assert_eq!(pb, Message(expect_pb));
118 }
119
120 fn read_protobuf(hex_str: &str) -> anyhow::Result<ProtoData> {
121 let bytes = hex::decode(hex_str.replace(" ", ""))?;
122 decode_protobuf(&mut Reader::new(&bytes.as_slice()))
123 }
124
125 #[test]
126 fn fixint() {
127 let nums: Vec<i64> = vec![-100, -10, 0, 10, 100];
128 for &num in &nums {
129 let data = fixint::encode_fix64(num);
130 assert_eq!(
131 num,
132 fixint::read_fix64(&mut Reader::new(data.as_slice())).unwrap()
133 )
134 }
135
136 for &num in &nums {
137 let data = fixint::encode_fix32(num as i32);
138 assert_eq!(
139 num as i32,
140 fixint::read_fix32(&mut Reader::new(data.as_slice())).unwrap()
141 )
142 }
143 }
144
145 #[test]
146 fn uvarint() {
147 let nums: Vec<u64> = vec![0, 10, 1000, 10000, 123456, 128, 256, 512];
148 for num in nums {
149 let data = varint::encode_uvarint(num);
150 assert_eq!(
151 num,
152 varint::read_uvarint(&mut Reader::new(data.as_slice())).unwrap()
153 );
154 }
155
156 let data: Vec<u8> = vec![192, 196, 7];
157 assert_eq!(
158 123456,
159 varint::read_uvarint(&mut Reader::new(data.as_slice())).unwrap()
160 );
161
162 let data: Vec<u8> = vec![192, 196, 7, 1, 1, 1, 1, 1, 1, 1, 1];
163 assert_eq!(
164 123456,
165 varint::read_uvarint(&mut Reader::new(data.as_slice())).unwrap()
166 );
167 }
168
169 #[test]
170 fn varint() {
171 let nums: Vec<i64> = vec![
172 0, 10, 1000, 10000, 128, 256, 512, -10, -1000, -10000, -128, -256, -512,
173 ];
174 for num in nums {
175 let data = varint::encode_varint(num);
176 assert_eq!(
177 num,
178 varint::read_varint(&mut Reader::new(data.as_slice())).unwrap()
179 );
180 }
181 }
182}