1 /*
2 * Copyright (c) 2019, The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <iomanip>
18 #include <sstream>
19
20 #include <gmock/gmock.h>
21 #include <gtest/gtest.h>
22
23 #include "cppbor.h"
24 #include "cppbor_parse.h"
25
26 using namespace cppbor;
27 using namespace std;
28
29 using ::testing::_;
30 using ::testing::AllOf;
31 using ::testing::ByRef;
32 using ::testing::InSequence;
33 using ::testing::IsNull;
34 using ::testing::NotNull;
35 using ::testing::Return;
36 using ::testing::Truly;
37 using ::testing::Unused;
38
hexDump(const string & str)39 string hexDump(const string& str) {
40 stringstream s;
41 for (auto c : str) {
42 s << setfill('0') << setw(2) << hex << (static_cast<unsigned>(c) & 0xff);
43 }
44 return s.str();
45 }
46
TEST(SimpleValueTest,UnsignedValueSizes)47 TEST(SimpleValueTest, UnsignedValueSizes) {
48 // Check that unsigned integers encode to correct lengths, and that encodedSize() is correct.
49 vector<pair<uint64_t /* value */, size_t /* expected encoded size */>> testCases{
50 {0, 1},
51 {1, 1},
52 {23, 1},
53 {24, 2},
54 {255, 2},
55 {256, 3},
56 {65535, 3},
57 {65536, 5},
58 {4294967295, 5},
59 {4294967296, 9},
60 {std::numeric_limits<uint64_t>::max(), 9},
61 };
62 for (auto& testCase : testCases) {
63 Uint val(testCase.first);
64 EXPECT_EQ(testCase.second, val.encodedSize()) << "Wrong size for value " << testCase.first;
65 EXPECT_EQ(val.encodedSize(), val.toString().size())
66 << "encodedSize and encoding disagree for value " << testCase.first;
67 }
68 }
69
TEST(SimpleValueTest,UnsignedValueEncodings)70 TEST(SimpleValueTest, UnsignedValueEncodings) {
71 EXPECT_EQ("\x00"s, Uint(0u).toString());
72 EXPECT_EQ("\x01"s, Uint(1u).toString());
73 EXPECT_EQ("\x0a"s, Uint(10u).toString());
74 EXPECT_EQ("\x17"s, Uint(23u).toString());
75 EXPECT_EQ("\x18\x18"s, Uint(24u).toString());
76 EXPECT_EQ("\x18\x19"s, Uint(25u).toString());
77 EXPECT_EQ("\x18\x64"s, Uint(100u).toString());
78 EXPECT_EQ("\x19\x03\xe8"s, Uint(1000u).toString());
79 EXPECT_EQ("\x1a\x00\x0f\x42\x40"s, Uint(1000000u).toString());
80 EXPECT_EQ("\x1b\x00\x00\x00\xe8\xd4\xa5\x10\x00"s, Uint(1000000000000u).toString());
81 EXPECT_EQ("\x1B\x7f\xff\xff\xff\xff\xff\xff\xff"s,
82 Uint(std::numeric_limits<int64_t>::max()).toString());
83 }
84
TEST(SimpleValueTest,NegativeValueEncodings)85 TEST(SimpleValueTest, NegativeValueEncodings) {
86 EXPECT_EQ("\x20"s, Nint(-1).toString());
87 EXPECT_EQ("\x28"s, Nint(-9).toString());
88 EXPECT_EQ("\x29"s, Nint(-10).toString());
89 EXPECT_EQ("\x36"s, Nint(-23).toString());
90 EXPECT_EQ("\x37"s, Nint(-24).toString());
91 EXPECT_EQ("\x38\x18"s, Nint(-25).toString());
92 EXPECT_EQ("\x38\x62"s, Nint(-99).toString());
93 EXPECT_EQ("\x38\x63"s, Nint(-100).toString());
94 EXPECT_EQ("\x39\x03\xe6"s, Nint(-999).toString());
95 EXPECT_EQ("\x39\x03\xe7"s, Nint(-1000).toString());
96 EXPECT_EQ("\x3a\x00\x0f\x42\x3F"s, Nint(-1000000).toString());
97 EXPECT_EQ("\x3b\x00\x00\x00\xe8\xd4\xa5\x0f\xff"s, Nint(-1000000000000).toString());
98 EXPECT_EQ("\x3B\x7f\xff\xff\xff\xff\xff\xff\xff"s,
99 Nint(std::numeric_limits<int64_t>::min()).toString());
100 }
101
TEST(SimpleValueDeathTest,NegativeValueEncodings)102 TEST(SimpleValueDeathTest, NegativeValueEncodings) {
103 EXPECT_DEATH(Nint(0), "");
104 EXPECT_DEATH(Nint(1), "");
105 }
106
TEST(SimpleValueTest,BooleanEncodings)107 TEST(SimpleValueTest, BooleanEncodings) {
108 EXPECT_EQ("\xf4"s, Bool(false).toString());
109 EXPECT_EQ("\xf5"s, Bool(true).toString());
110 }
111
TEST(SimpleValueTest,ByteStringEncodings)112 TEST(SimpleValueTest, ByteStringEncodings) {
113 EXPECT_EQ("\x40", Bstr("").toString());
114 EXPECT_EQ("\x41\x61", Bstr("a").toString());
115 EXPECT_EQ("\x41\x41", Bstr("A").toString());
116 EXPECT_EQ("\x44\x49\x45\x54\x46", Bstr("IETF").toString());
117 EXPECT_EQ("\x42\x22\x5c", Bstr("\"\\").toString());
118 EXPECT_EQ("\x42\xc3\xbc", Bstr("\xc3\xbc").toString());
119 EXPECT_EQ("\x43\xe6\xb0\xb4", Bstr("\xe6\xb0\xb4").toString());
120 EXPECT_EQ("\x44\xf0\x90\x85\x91", Bstr("\xf0\x90\x85\x91").toString());
121 EXPECT_EQ("\x44\x01\x02\x03\x04", Bstr("\x01\x02\x03\x04").toString());
122 EXPECT_EQ("\x44\x40\x40\x40\x40", Bstr("@@@@").toString());
123 }
124
TEST(SimpleValueTest,TextStringEncodings)125 TEST(SimpleValueTest, TextStringEncodings) {
126 EXPECT_EQ("\x60"s, Tstr("").toString());
127 EXPECT_EQ("\x61\x61"s, Tstr("a").toString());
128 EXPECT_EQ("\x61\x41"s, Tstr("A").toString());
129 EXPECT_EQ("\x64\x49\x45\x54\x46"s, Tstr("IETF").toString());
130 EXPECT_EQ("\x62\x22\x5c"s, Tstr("\"\\").toString());
131 EXPECT_EQ("\x62\xc3\xbc"s, Tstr("\xc3\xbc").toString());
132 EXPECT_EQ("\x63\xe6\xb0\xb4"s, Tstr("\xe6\xb0\xb4").toString());
133 EXPECT_EQ("\x64\xf0\x90\x85\x91"s, Tstr("\xf0\x90\x85\x91").toString());
134 EXPECT_EQ("\x64\x01\x02\x03\x04"s, Tstr("\x01\x02\x03\x04").toString());
135 }
136
TEST(IsIteratorPairOverTest,All)137 TEST(IsIteratorPairOverTest, All) {
138 EXPECT_TRUE((
139 details::is_iterator_pair_over<pair<string::iterator, string::iterator>, char>::value));
140 EXPECT_TRUE((details::is_iterator_pair_over<pair<string::const_iterator, string::iterator>,
141 char>::value));
142 EXPECT_TRUE((details::is_iterator_pair_over<pair<string::iterator, string::const_iterator>,
143 char>::value));
144 EXPECT_TRUE((details::is_iterator_pair_over<pair<char*, char*>, char>::value));
145 EXPECT_TRUE((details::is_iterator_pair_over<pair<const char*, char*>, char>::value));
146 EXPECT_TRUE((details::is_iterator_pair_over<pair<char*, const char*>, char>::value));
147 EXPECT_FALSE((details::is_iterator_pair_over<pair<string::iterator, string::iterator>,
148 uint8_t>::value));
149 EXPECT_FALSE((details::is_iterator_pair_over<pair<char*, char*>, uint8_t>::value));
150 EXPECT_TRUE((details::is_iterator_pair_over<
151 pair<vector<uint8_t>::iterator, vector<uint8_t>::iterator>, uint8_t>::value));
152 EXPECT_TRUE((details::is_iterator_pair_over<
153 pair<vector<uint8_t>::const_iterator, vector<uint8_t>::iterator>,
154 uint8_t>::value));
155 EXPECT_TRUE((details::is_iterator_pair_over<
156 pair<vector<uint8_t>::iterator, vector<uint8_t>::const_iterator>,
157 uint8_t>::value));
158 EXPECT_TRUE((details::is_iterator_pair_over<pair<uint8_t*, uint8_t*>, uint8_t>::value));
159 EXPECT_TRUE((details::is_iterator_pair_over<pair<const uint8_t*, uint8_t*>, uint8_t>::value));
160 EXPECT_TRUE((details::is_iterator_pair_over<pair<uint8_t*, const uint8_t*>, uint8_t>::value));
161 EXPECT_FALSE((details::is_iterator_pair_over<
162 pair<vector<uint8_t>::iterator, vector<uint8_t>::iterator>, char>::value));
163 EXPECT_FALSE((details::is_iterator_pair_over<pair<uint8_t*, const uint8_t*>, char>::value));
164 }
165
TEST(MakeEntryTest,Boolean)166 TEST(MakeEntryTest, Boolean) {
167 EXPECT_EQ("\xf4"s, details::makeItem(false)->toString());
168 }
169
TEST(MakeEntryTest,Integers)170 TEST(MakeEntryTest, Integers) {
171 EXPECT_EQ("\x00"s, details::makeItem(static_cast<uint8_t>(0))->toString());
172 EXPECT_EQ("\x00"s, details::makeItem(static_cast<uint16_t>(0))->toString());
173 EXPECT_EQ("\x00"s, details::makeItem(static_cast<uint32_t>(0))->toString());
174 EXPECT_EQ("\x00"s, details::makeItem(static_cast<uint64_t>(0))->toString());
175 EXPECT_EQ("\x00"s, details::makeItem(static_cast<int8_t>(0))->toString());
176 EXPECT_EQ("\x00"s, details::makeItem(static_cast<int16_t>(0))->toString());
177 EXPECT_EQ("\x00"s, details::makeItem(static_cast<int32_t>(0))->toString());
178 EXPECT_EQ("\x00"s, details::makeItem(static_cast<int64_t>(0))->toString());
179 EXPECT_EQ("\x20"s, details::makeItem(static_cast<int8_t>(-1))->toString());
180 EXPECT_EQ("\x20"s, details::makeItem(static_cast<int16_t>(-1))->toString());
181 EXPECT_EQ("\x20"s, details::makeItem(static_cast<int32_t>(-1))->toString());
182 EXPECT_EQ("\x20"s, details::makeItem(static_cast<int64_t>(-1))->toString());
183
184 EXPECT_EQ("\x1b\xff\xff\xff\xff\xff\xff\xff\xff"s,
185 details::makeItem(static_cast<uint64_t>(std::numeric_limits<uint64_t>::max()))
186 ->toString());
187 }
188
TEST(MakeEntryTest,StdStrings)189 TEST(MakeEntryTest, StdStrings) {
190 string s1("hello");
191 const string s2("hello");
192 EXPECT_EQ("\x65\x68\x65\x6c\x6c\x6f"s, details::makeItem(s1)->toString()); // copy of string
193 EXPECT_EQ("\x65\x68\x65\x6c\x6c\x6f"s,
194 details::makeItem(s2)->toString()); // copy of const string
195 EXPECT_EQ("\x65\x68\x65\x6c\x6c\x6f"s,
196 details::makeItem(std::move(s1))->toString()); // move string
197 EXPECT_EQ(0U, s1.size()); // Prove string was moved, not copied.
198 }
199
TEST(MakeEntryTest,StdStringViews)200 TEST(MakeEntryTest, StdStringViews) {
201 string_view s1("hello");
202 const string_view s2("hello");
203 EXPECT_EQ("\x65\x68\x65\x6c\x6c\x6f"s, details::makeItem(s1)->toString());
204 EXPECT_EQ("\x65\x68\x65\x6c\x6c\x6f"s, details::makeItem(s2)->toString());
205 }
206
TEST(MakeEntryTest,CStrings)207 TEST(MakeEntryTest, CStrings) {
208 char s1[] = "hello";
209 const char s2[] = "hello";
210 const char* s3 = "hello";
211 EXPECT_EQ("\x65\x68\x65\x6c\x6c\x6f"s, details::makeItem(s1)->toString());
212 EXPECT_EQ("\x65\x68\x65\x6c\x6c\x6f"s, details::makeItem(s2)->toString());
213 EXPECT_EQ("\x65\x68\x65\x6c\x6c\x6f"s, details::makeItem(s3)->toString());
214 }
215
TEST(MakeEntryTest,StringIteratorPairs)216 TEST(MakeEntryTest, StringIteratorPairs) {
217 // Use iterators from string to prove that "real" iterators work
218 string s1 = "hello"s;
219 pair<string::iterator, string::iterator> p1 = make_pair(s1.begin(), s1.end());
220
221 const pair<string::iterator, string::iterator> p2 = p1;
222 EXPECT_EQ("\x65\x68\x65\x6c\x6c\x6f"s, details::makeItem(p1)->toString());
223 EXPECT_EQ("\x65\x68\x65\x6c\x6c\x6f"s, details::makeItem(p2)->toString());
224
225 // Use char*s as iterators
226 const char* s2 = "hello";
227 pair p3 = make_pair(s2, s2 + 5);
228 const pair p4 = p3;
229 EXPECT_EQ("\x65\x68\x65\x6c\x6c\x6f"s, details::makeItem(p3)->toString());
230 EXPECT_EQ("\x65\x68\x65\x6c\x6c\x6f"s, details::makeItem(p4)->toString());
231 }
232
TEST(MakeEntryTest,ByteStrings)233 TEST(MakeEntryTest, ByteStrings) {
234 vector<uint8_t> v1 = {0x00, 0x01, 0x02};
235 const vector<uint8_t> v2 = {0x00, 0x01, 0x02};
236 EXPECT_EQ("\x43\x00\x01\x02"s, details::makeItem(v1)->toString()); // copy of vector
237 EXPECT_EQ("\x43\x00\x01\x02"s, details::makeItem(v2)->toString()); // copy of const vector
238 EXPECT_EQ("\x43\x00\x01\x02"s, details::makeItem(std::move(v1))->toString()); // move vector
239 EXPECT_EQ(0U, v1.size()); // Prove vector was moved, not copied.
240 }
241
TEST(MakeEntryTest,ByteStringIteratorPairs)242 TEST(MakeEntryTest, ByteStringIteratorPairs) {
243 using vec = vector<uint8_t>;
244 using iter = vec::iterator;
245 vec v1 = {0x00, 0x01, 0x02};
246 pair<iter, iter> p1 = make_pair(v1.begin(), v1.end());
247 const pair<iter, iter> p2 = make_pair(v1.begin(), v1.end());
248 EXPECT_EQ("\x43\x00\x01\x02"s, details::makeItem(p1)->toString());
249 EXPECT_EQ("\x43\x00\x01\x02"s, details::makeItem(p2)->toString());
250
251 // Use uint8_t*s as iterators
252 uint8_t v2[] = {0x00, 0x01, 0x02};
253 uint8_t* v3 = v2;
254 pair<uint8_t*, uint8_t*> p3 = make_pair(v2, v2 + 3);
255 const pair<uint8_t*, uint8_t*> p4 = make_pair(v2, v2 + 3);
256 pair<uint8_t*, uint8_t*> p5 = make_pair(v3, v3 + 3);
257 const pair<uint8_t*, uint8_t*> p6 = make_pair(v3, v3 + 3);
258 EXPECT_EQ("\x43\x00\x01\x02"s, details::makeItem(p3)->toString());
259 EXPECT_EQ("\x43\x00\x01\x02"s, details::makeItem(p4)->toString());
260 EXPECT_EQ("\x43\x00\x01\x02"s, details::makeItem(p5)->toString());
261 EXPECT_EQ("\x43\x00\x01\x02"s, details::makeItem(p6)->toString());
262 }
263
TEST(MakeEntryTest,ByteStringBuffers)264 TEST(MakeEntryTest, ByteStringBuffers) {
265 uint8_t v1[] = {0x00, 0x01, 0x02};
266 EXPECT_EQ("\x43\x00\x01\x02"s, details::makeItem(make_pair(v1, 3))->toString());
267 }
268
TEST(MakeEntryTest,ItemPointer)269 TEST(MakeEntryTest, ItemPointer) {
270 Uint* p1 = new Uint(0);
271 EXPECT_EQ("\x00"s, details::makeItem(p1)->toString());
272 EXPECT_EQ("\x60"s, details::makeItem(new Tstr(string()))->toString());
273 }
274
TEST(MakeEntryTest,ItemReference)275 TEST(MakeEntryTest, ItemReference) {
276 Tstr str("hello"s);
277 Tstr& strRef = str;
278 const Tstr& strConstRef = str;
279 EXPECT_EQ("\x65\x68\x65\x6c\x6c\x6f"s, details::makeItem(str)->toString());
280 EXPECT_EQ("\x65\x68\x65\x6c\x6c\x6f"s, details::makeItem(strRef)->toString());
281 EXPECT_EQ("\x65\x68\x65\x6c\x6c\x6f"s, details::makeItem(strConstRef)->toString());
282 EXPECT_EQ("\x65\x68\x65\x6c\x6c\x6f"s, details::makeItem(std::move(str))->toString());
283 EXPECT_EQ("\x60"s, details::makeItem(str)->toString()); // Prove that it moved
284
285 EXPECT_EQ("\x00"s, details::makeItem(Uint(0))->toString());
286
287 EXPECT_EQ("\x43\x00\x01\x02"s,
288 details::makeItem(Bstr(vector<uint8_t>{0x00, 0x01, 0x02}))->toString());
289
290 EXPECT_EQ("\x80"s, details::makeItem(Array())->toString());
291 EXPECT_EQ("\xa0"s, details::makeItem(Map())->toString());
292 }
293
TEST(CompoundValueTest,ArrayOfInts)294 TEST(CompoundValueTest, ArrayOfInts) {
295 EXPECT_EQ("\x80"s, Array().toString());
296 Array(Uint(0)).toString();
297
298 EXPECT_EQ("\x81\x00"s, Array(Uint(0U)).toString());
299 EXPECT_EQ("\x82\x00\x01"s, Array(Uint(0), Uint(1)).toString());
300 EXPECT_EQ("\x83\x00\x01\x38\x62"s, Array(Uint(0), Uint(1), Nint(-99)).toString());
301
302 EXPECT_EQ("\x81\x00"s, Array(0).toString());
303 EXPECT_EQ("\x82\x00\x01"s, Array(0, 1).toString());
304 EXPECT_EQ("\x83\x00\x01\x38\x62"s, Array(0, 1, -99).toString());
305 }
306
TEST(CompoundValueTest,MapOfInts)307 TEST(CompoundValueTest, MapOfInts) {
308 EXPECT_EQ("\xA0"s, Map().toString());
309 EXPECT_EQ("\xA1\x00\x01"s, Map(Uint(0), Uint(1)).toString());
310 // Maps with an odd number of arguments will fail to compile. Uncomment the next lines to test.
311 // EXPECT_EQ("\xA1\x00"s, Map(Int(0)).toString());
312 // EXPECT_EQ("\xA1\x00\x01\x02"s, Map(Int(0), Int(1), Int(2)).toString());
313 }
314
TEST(CompoundValueTest,MixedArray)315 TEST(CompoundValueTest, MixedArray) {
316 vector<uint8_t> vec = {3, 2, 1};
317 EXPECT_EQ("\x84\x01\x20\x43\x03\x02\x01\x65\x68\x65\x6C\x6C\x6F"s,
318 Array(Uint(1), Nint(-1), Bstr(vec), Tstr("hello")).toString());
319
320 EXPECT_EQ("\x84\x01\x20\x43\x03\x02\x01\x65\x68\x65\x6C\x6C\x6F"s,
321 Array(1, -1, vec, "hello").toString());
322 }
323
TEST(CompoundValueTest,MixedMap)324 TEST(CompoundValueTest, MixedMap) {
325 vector<uint8_t> vec = {3, 2, 1};
326 EXPECT_EQ("\xA2\x01\x20\x43\x03\x02\x01\x65\x68\x65\x6C\x6C\x6F"s,
327 Map(Uint(1), Nint(-1), Bstr(vec), Tstr("hello")).toString());
328
329 EXPECT_EQ("\xA2\x01\x20\x43\x03\x02\x01\x65\x68\x65\x6C\x6C\x6F"s,
330 Map(1, -1, vec, "hello").toString());
331 }
332
TEST(CompoundValueTest,NestedStructures)333 TEST(CompoundValueTest, NestedStructures) {
334 vector<uint8_t> vec = {3, 2, 1};
335
336 string expectedEncoding =
337 "\xA2\x66\x4F\x75\x74\x65\x72\x31\x82\xA2\x66\x49\x6E\x6E\x65\x72\x31\x18\x63\x66\x49"
338 "\x6E"
339 "\x6E\x65\x72\x32\x43\x03\x02\x01\x63\x66\x6F\x6F\x66\x4F\x75\x74\x65\x72\x32\x0A"s;
340
341 // Do it with explicitly-created Items
342 EXPECT_EQ(expectedEncoding,
343 Map(Tstr("Outer1"),
344 Array( //
345 Map(Tstr("Inner1"), Uint(99), Tstr("Inner2"), Bstr(vec)), Tstr("foo")),
346 Tstr("Outer2"), //
347 Uint(10))
348 .toString());
349 EXPECT_EQ(3U, vec.size());
350
351 // Now just use convertible types
352 EXPECT_EQ(expectedEncoding, Map("Outer1",
353 Array(Map("Inner1", 99, //
354 "Inner2", vec),
355 "foo"),
356 "Outer2", 10)
357 .toString());
358 EXPECT_EQ(3U, vec.size());
359
360 // Finally, do it with the .add() method. This is slightly less efficient, but has the
361 // advantage you can build a structure up incrementally, or somewhat fluently if you like.
362 // First, fluently.
363 EXPECT_EQ(expectedEncoding, Map().add("Outer1", Array().add(Map() //
364 .add("Inner1", 99)
365 .add("Inner2", vec))
366 .add("foo"))
367 .add("Outer2", 10)
368 .toString());
369 EXPECT_EQ(3U, vec.size());
370
371 // Next, more incrementally
372 Array arr;
373 arr.add(Map() //
374 .add("Inner1", 99)
375 .add("Inner2", vec))
376 .add("foo");
377 EXPECT_EQ(3U, vec.size());
378
379 Map m;
380 m.add("Outer1", std::move(arr)); // Moving is necessary; Map and Array cannot be copied.
381 m.add("Outer2", 10);
382 auto s = m.toString();
383 EXPECT_EQ(expectedEncoding, s);
384 }
385
TEST(EncodingMethodsTest,AllVariants)386 TEST(EncodingMethodsTest, AllVariants) {
387 Map map;
388 map.add("key1", Array().add(Map() //
389 .add("key_a", 9999999)
390 .add("key_b", std::vector<uint8_t>{0x01, 0x02, 0x03})
391 .add("key_c", std::numeric_limits<uint64_t>::max())
392 .add("key_d", std::numeric_limits<int16_t>::min()))
393 .add("foo"))
394 .add("key2", true);
395
396 std::vector<uint8_t> buf;
397 buf.resize(map.encodedSize());
398
399 EXPECT_EQ(buf.data() + buf.size(), map.encode(buf.data(), buf.data() + buf.size()));
400
401 EXPECT_EQ(buf, map.encode());
402
403 std::vector<uint8_t> buf2;
404 map.encode(std::back_inserter(buf2));
405 EXPECT_EQ(buf, buf2);
406
407 auto iter = buf.begin();
408 map.encode([&](uint8_t c) { EXPECT_EQ(c, *iter++); });
409 }
410
TEST(EncodingMethodsTest,UintWithTooShortBuf)411 TEST(EncodingMethodsTest, UintWithTooShortBuf) {
412 Uint val(100000);
413 vector<uint8_t> buf(val.encodedSize() - 1);
414 EXPECT_EQ(nullptr, val.encode(buf.data(), buf.data() + buf.size()));
415 }
416
TEST(EncodingMethodsTest,TstrWithTooShortBuf)417 TEST(EncodingMethodsTest, TstrWithTooShortBuf) {
418 Tstr val("01234567890123456789012345"s);
419 vector<uint8_t> buf(1);
420 EXPECT_EQ(nullptr, val.encode(buf.data(), buf.data() + buf.size()));
421
422 buf.resize(val.encodedSize() - 1);
423 EXPECT_EQ(nullptr, val.encode(buf.data(), buf.data() + buf.size()));
424 }
425
TEST(EncodingMethodsTest,BstrWithTooShortBuf)426 TEST(EncodingMethodsTest, BstrWithTooShortBuf) {
427 Bstr val("01234567890123456789012345"s);
428 vector<uint8_t> buf(1);
429 EXPECT_EQ(nullptr, val.encode(buf.data(), buf.data() + buf.size()));
430
431 buf.resize(val.encodedSize() - 1);
432 EXPECT_EQ(nullptr, val.encode(buf.data(), buf.data() + buf.size()));
433 }
434
TEST(EncodingMethodsTest,ArrayWithTooShortBuf)435 TEST(EncodingMethodsTest, ArrayWithTooShortBuf) {
436 Array val("a", 5, -100);
437
438 std::vector<uint8_t> buf(val.encodedSize() - 1);
439 EXPECT_EQ(nullptr, val.encode(buf.data(), buf.data() + buf.size()));
440 }
441
TEST(EncodingMethodsTest,MapWithTooShortBuf)442 TEST(EncodingMethodsTest, MapWithTooShortBuf) {
443 Map map;
444 map.add("key1", Array().add(Map() //
445 .add("key_a", 99)
446 .add("key_b", std::vector<uint8_t>{0x01, 0x02, 0x03}))
447 .add("foo"))
448 .add("key2", true);
449
450 std::vector<uint8_t> buf(map.encodedSize() - 1);
451 EXPECT_EQ(nullptr, map.encode(buf.data(), buf.data() + buf.size()));
452 }
453
TEST(EqualityTest,Uint)454 TEST(EqualityTest, Uint) {
455 Uint val(99);
456 EXPECT_EQ(val, Uint(99));
457
458 EXPECT_NE(val, Uint(98));
459 EXPECT_NE(val, Nint(-1));
460 EXPECT_NE(val, Tstr("99"));
461 EXPECT_NE(val, Bstr("99"));
462 EXPECT_NE(val, Bool(false));
463 EXPECT_NE(val, Array(99, 1));
464 EXPECT_NE(val, Map(99, 1));
465 }
466
TEST(EqualityTest,Nint)467 TEST(EqualityTest, Nint) {
468 Nint val(-1);
469 EXPECT_EQ(val, Nint(-1));
470
471 EXPECT_NE(val, Uint(99));
472 EXPECT_NE(val, Nint(-4));
473 EXPECT_NE(val, Tstr("99"));
474 EXPECT_NE(val, Bstr("99"));
475 EXPECT_NE(val, Bool(false));
476 EXPECT_NE(val, Array(99));
477 EXPECT_NE(val, Map(99, 1));
478 }
479
TEST(EqualityTest,Tstr)480 TEST(EqualityTest, Tstr) {
481 Tstr val("99");
482 EXPECT_EQ(val, Tstr("99"));
483
484 EXPECT_NE(val, Uint(99));
485 EXPECT_NE(val, Nint(-1));
486 EXPECT_NE(val, Nint(-4));
487 EXPECT_NE(val, Tstr("98"));
488 EXPECT_NE(val, Bstr("99"));
489 EXPECT_NE(val, Bool(false));
490 EXPECT_NE(val, Array(99, 1));
491 EXPECT_NE(val, Map(99, 1));
492 }
493
TEST(EqualityTest,Bstr)494 TEST(EqualityTest, Bstr) {
495 Bstr val("99");
496 EXPECT_EQ(val, Bstr("99"));
497
498 EXPECT_NE(val, Uint(99));
499 EXPECT_NE(val, Nint(-1));
500 EXPECT_NE(val, Nint(-4));
501 EXPECT_NE(val, Tstr("99"));
502 EXPECT_NE(val, Bstr("98"));
503 EXPECT_NE(val, Bool(false));
504 EXPECT_NE(val, Array(99, 1));
505 EXPECT_NE(val, Map(99, 1));
506 }
507
TEST(EqualityTest,Bool)508 TEST(EqualityTest, Bool) {
509 Bool val(false);
510 EXPECT_EQ(val, Bool(false));
511
512 EXPECT_NE(val, Uint(99));
513 EXPECT_NE(val, Nint(-1));
514 EXPECT_NE(val, Nint(-4));
515 EXPECT_NE(val, Tstr("99"));
516 EXPECT_NE(val, Bstr("98"));
517 EXPECT_NE(val, Bool(true));
518 EXPECT_NE(val, Array(99, 1));
519 EXPECT_NE(val, Map(99, 1));
520 }
521
TEST(EqualityTest,Array)522 TEST(EqualityTest, Array) {
523 Array val(99, 1);
524 EXPECT_EQ(val, Array(99, 1));
525
526 EXPECT_NE(val, Uint(99));
527 EXPECT_NE(val, Nint(-1));
528 EXPECT_NE(val, Nint(-4));
529 EXPECT_NE(val, Tstr("99"));
530 EXPECT_NE(val, Bstr("98"));
531 EXPECT_NE(val, Bool(true));
532 EXPECT_NE(val, Array(99, 2));
533 EXPECT_NE(val, Array(98, 1));
534 EXPECT_NE(val, Array(99, 1, 2));
535 EXPECT_NE(val, Map(99, 1));
536 }
537
TEST(EqualityTest,Map)538 TEST(EqualityTest, Map) {
539 Map val(99, 1);
540 EXPECT_EQ(val, Map(99, 1));
541
542 EXPECT_NE(val, Uint(99));
543 EXPECT_NE(val, Nint(-1));
544 EXPECT_NE(val, Nint(-4));
545 EXPECT_NE(val, Tstr("99"));
546 EXPECT_NE(val, Bstr("98"));
547 EXPECT_NE(val, Bool(true));
548 EXPECT_NE(val, Array(99, 1));
549 EXPECT_NE(val, Map(99, 2));
550 EXPECT_NE(val, Map(99, 1, 99, 2));
551 }
552
TEST(ConvertTest,Uint)553 TEST(ConvertTest, Uint) {
554 unique_ptr<Item> item = details::makeItem(10);
555
556 EXPECT_EQ(UINT, item->type());
557 EXPECT_NE(nullptr, item->asInt());
558 EXPECT_NE(nullptr, item->asUint());
559 EXPECT_EQ(nullptr, item->asNint());
560 EXPECT_EQ(nullptr, item->asTstr());
561 EXPECT_EQ(nullptr, item->asBstr());
562 EXPECT_EQ(nullptr, item->asSimple());
563 EXPECT_EQ(nullptr, item->asMap());
564 EXPECT_EQ(nullptr, item->asArray());
565
566 EXPECT_EQ(10, item->asInt()->value());
567 EXPECT_EQ(10, item->asUint()->value());
568 }
569
TEST(ConvertTest,Nint)570 TEST(ConvertTest, Nint) {
571 unique_ptr<Item> item = details::makeItem(-10);
572
573 EXPECT_EQ(NINT, item->type());
574 EXPECT_NE(nullptr, item->asInt());
575 EXPECT_EQ(nullptr, item->asUint());
576 EXPECT_NE(nullptr, item->asNint());
577 EXPECT_EQ(nullptr, item->asTstr());
578 EXPECT_EQ(nullptr, item->asBstr());
579 EXPECT_EQ(nullptr, item->asSimple());
580 EXPECT_EQ(nullptr, item->asMap());
581 EXPECT_EQ(nullptr, item->asArray());
582
583 EXPECT_EQ(-10, item->asInt()->value());
584 EXPECT_EQ(-10, item->asNint()->value());
585 }
586
TEST(ConvertTest,Tstr)587 TEST(ConvertTest, Tstr) {
588 unique_ptr<Item> item = details::makeItem("hello");
589
590 EXPECT_EQ(TSTR, item->type());
591 EXPECT_EQ(nullptr, item->asInt());
592 EXPECT_EQ(nullptr, item->asUint());
593 EXPECT_EQ(nullptr, item->asNint());
594 EXPECT_NE(nullptr, item->asTstr());
595 EXPECT_EQ(nullptr, item->asBstr());
596 EXPECT_EQ(nullptr, item->asSimple());
597 EXPECT_EQ(nullptr, item->asMap());
598 EXPECT_EQ(nullptr, item->asArray());
599
600 EXPECT_EQ("hello"s, item->asTstr()->value());
601 }
602
TEST(ConvertTest,Bstr)603 TEST(ConvertTest, Bstr) {
604 vector<uint8_t> vec{0x23, 0x24, 0x22};
605 unique_ptr<Item> item = details::makeItem(vec);
606
607 EXPECT_EQ(BSTR, item->type());
608 EXPECT_EQ(nullptr, item->asInt());
609 EXPECT_EQ(nullptr, item->asUint());
610 EXPECT_EQ(nullptr, item->asNint());
611 EXPECT_EQ(nullptr, item->asTstr());
612 EXPECT_NE(nullptr, item->asBstr());
613 EXPECT_EQ(nullptr, item->asSimple());
614 EXPECT_EQ(nullptr, item->asMap());
615 EXPECT_EQ(nullptr, item->asArray());
616
617 EXPECT_EQ(vec, item->asBstr()->value());
618 }
619
TEST(ConvertTest,Bool)620 TEST(ConvertTest, Bool) {
621 unique_ptr<Item> item = details::makeItem(false);
622
623 EXPECT_EQ(SIMPLE, item->type());
624 EXPECT_EQ(nullptr, item->asInt());
625 EXPECT_EQ(nullptr, item->asUint());
626 EXPECT_EQ(nullptr, item->asNint());
627 EXPECT_EQ(nullptr, item->asTstr());
628 EXPECT_EQ(nullptr, item->asBstr());
629 EXPECT_NE(nullptr, item->asSimple());
630 EXPECT_EQ(nullptr, item->asMap());
631 EXPECT_EQ(nullptr, item->asArray());
632
633 EXPECT_EQ(BOOLEAN, item->asSimple()->simpleType());
634 EXPECT_NE(nullptr, item->asSimple()->asBool());
635
636 EXPECT_FALSE(item->asSimple()->asBool()->value());
637 }
638
TEST(ConvertTest,Map)639 TEST(ConvertTest, Map) {
640 unique_ptr<Item> item(new Map);
641
642 EXPECT_EQ(MAP, item->type());
643 EXPECT_EQ(nullptr, item->asInt());
644 EXPECT_EQ(nullptr, item->asUint());
645 EXPECT_EQ(nullptr, item->asNint());
646 EXPECT_EQ(nullptr, item->asTstr());
647 EXPECT_EQ(nullptr, item->asBstr());
648 EXPECT_EQ(nullptr, item->asSimple());
649 EXPECT_NE(nullptr, item->asMap());
650 EXPECT_EQ(nullptr, item->asArray());
651
652 EXPECT_EQ(0U, item->asMap()->size());
653 }
654
TEST(ConvertTest,Array)655 TEST(ConvertTest, Array) {
656 unique_ptr<Item> item(new Array);
657
658 EXPECT_EQ(ARRAY, item->type());
659 EXPECT_EQ(nullptr, item->asInt());
660 EXPECT_EQ(nullptr, item->asUint());
661 EXPECT_EQ(nullptr, item->asNint());
662 EXPECT_EQ(nullptr, item->asTstr());
663 EXPECT_EQ(nullptr, item->asBstr());
664 EXPECT_EQ(nullptr, item->asSimple());
665 EXPECT_EQ(nullptr, item->asMap());
666 EXPECT_NE(nullptr, item->asArray());
667
668 EXPECT_EQ(0U, item->asArray()->size());
669 }
670
671 class MockParseClient : public ParseClient {
672 public:
673 MOCK_METHOD4(item, ParseClient*(std::unique_ptr<Item>& item, const uint8_t* hdrBegin,
674 const uint8_t* valueBegin, const uint8_t* end));
675 MOCK_METHOD4(itemEnd, ParseClient*(std::unique_ptr<Item>& item, const uint8_t* hdrBegin,
676 const uint8_t* valueBegin, const uint8_t* end));
677 MOCK_METHOD2(error, void(const uint8_t* position, const std::string& errorMessage));
678 };
679
680 MATCHER_P(IsType, value, std::string("Type ") + (negation ? "doesn't match" : "matches")) {
681 return arg->type() == value;
682 }
683
684 MATCHER_P(MatchesItem, value, "") {
685 return arg && *arg == value;
686 }
687
688 MATCHER_P(IsArrayOfSize, value, "") {
689 return arg->type() == ARRAY && arg->asArray()->size() == value;
690 }
691
692 MATCHER_P(IsMapOfSize, value, "") {
693 return arg->type() == MAP && arg->asMap()->size() == value;
694 }
695
TEST(StreamParseTest,Uint)696 TEST(StreamParseTest, Uint) {
697 MockParseClient mpc;
698
699 Uint val(100);
700 auto encoded = val.encode();
701 uint8_t* encBegin = encoded.data();
702 uint8_t* encEnd = encoded.data() + encoded.size();
703
704 EXPECT_CALL(mpc, item(MatchesItem(val), encBegin, encEnd, encEnd)).WillOnce(Return(&mpc));
705 EXPECT_CALL(mpc, itemEnd(_, _, _, _)).Times(0);
706 EXPECT_CALL(mpc, error(_, _)).Times(0);
707
708 parse(encoded.data(), encoded.data() + encoded.size(), &mpc);
709 }
710
TEST(StreamParseTest,Nint)711 TEST(StreamParseTest, Nint) {
712 MockParseClient mpc;
713
714 Nint val(-10);
715 auto encoded = val.encode();
716 uint8_t* encBegin = encoded.data();
717 uint8_t* encEnd = encoded.data() + encoded.size();
718
719 EXPECT_CALL(mpc, item(MatchesItem(val), encBegin, encEnd, encEnd)).WillOnce(Return(&mpc));
720
721 EXPECT_CALL(mpc, itemEnd(_, _, _, _)).Times(0);
722 EXPECT_CALL(mpc, error(_, _)).Times(0);
723
724 parse(encoded.data(), encoded.data() + encoded.size(), &mpc);
725 }
726
TEST(StreamParseTest,Bool)727 TEST(StreamParseTest, Bool) {
728 MockParseClient mpc;
729
730 Bool val(true);
731 auto encoded = val.encode();
732 uint8_t* encBegin = encoded.data();
733 uint8_t* encEnd = encoded.data() + encoded.size();
734
735 EXPECT_CALL(mpc, item(MatchesItem(val), encBegin, encEnd, encEnd)).WillOnce(Return(&mpc));
736 EXPECT_CALL(mpc, itemEnd(_, _, _, _)).Times(0);
737 EXPECT_CALL(mpc, error(_, _)).Times(0);
738
739 parse(encoded.data(), encoded.data() + encoded.size(), &mpc);
740 }
741
TEST(StreamParseTest,Tstr)742 TEST(StreamParseTest, Tstr) {
743 MockParseClient mpc;
744
745 Tstr val("Hello");
746 auto encoded = val.encode();
747 uint8_t* encBegin = encoded.data();
748 uint8_t* encEnd = encoded.data() + encoded.size();
749
750 EXPECT_CALL(mpc, item(MatchesItem(val), encBegin, encBegin + 1, encEnd)).WillOnce(Return(&mpc));
751 EXPECT_CALL(mpc, itemEnd(_, _, _, _)).Times(0);
752 EXPECT_CALL(mpc, error(_, _)).Times(0);
753
754 parse(encoded.data(), encoded.data() + encoded.size(), &mpc);
755 }
756
TEST(StreamParseTest,Bstr)757 TEST(StreamParseTest, Bstr) {
758 MockParseClient mpc;
759
760 Bstr val("Hello");
761 auto encoded = val.encode();
762 uint8_t* encBegin = encoded.data();
763 uint8_t* encEnd = encoded.data() + encoded.size();
764
765 EXPECT_CALL(mpc, item(MatchesItem(val), encBegin, encBegin + 1, encEnd)).WillOnce(Return(&mpc));
766 EXPECT_CALL(mpc, itemEnd(_, _, _, _)).Times(0);
767 EXPECT_CALL(mpc, error(_, _)).Times(0);
768
769 parse(encoded.data(), encoded.data() + encoded.size(), &mpc);
770 }
771
TEST(StreamParseTest,Array)772 TEST(StreamParseTest, Array) {
773 MockParseClient mpc;
774
775 Array val("Hello", 4, Array(-9, "Goodbye"), std::numeric_limits<uint64_t>::max());
776 ASSERT_NE(val[2]->asArray(), nullptr);
777 const Array& interior = *(val[2]->asArray());
778 auto encoded = val.encode();
779 uint8_t* encBegin = encoded.data();
780 uint8_t* encEnd = encoded.data() + encoded.size();
781
782 {
783 InSequence s;
784 const uint8_t* pos = encBegin;
785 EXPECT_CALL(mpc, item(IsArrayOfSize(val.size()), pos, pos + 1, pos + 1))
786 .WillOnce(Return(&mpc));
787 ++pos;
788 EXPECT_CALL(mpc, item(MatchesItem(ByRef(*val[0])), pos, pos + 1, pos + 6))
789 .WillOnce(Return(&mpc));
790 pos += 6;
791 EXPECT_CALL(mpc, item(MatchesItem(ByRef(*val[1])), pos, pos + 1, pos + 1))
792 .WillOnce(Return(&mpc));
793 ++pos;
794 const uint8_t* innerArrayBegin = pos;
795 EXPECT_CALL(mpc, item(IsArrayOfSize(interior.size()), pos, pos + 1, pos + 1))
796 .WillOnce(Return(&mpc));
797 ++pos;
798 EXPECT_CALL(mpc, item(MatchesItem(ByRef(*interior[0])), pos, pos + 1, pos + 1))
799 .WillOnce(Return(&mpc));
800 ++pos;
801 EXPECT_CALL(mpc, item(MatchesItem(ByRef(*interior[1])), pos, pos + 1, pos + 8))
802 .WillOnce(Return(&mpc));
803 pos += 8;
804 EXPECT_CALL(mpc, itemEnd(IsArrayOfSize(interior.size()), innerArrayBegin,
805 innerArrayBegin + 1, pos))
806 .WillOnce(Return(&mpc));
807 EXPECT_CALL(mpc, item(MatchesItem(ByRef(*val[3])), pos, pos + 9, pos + 9))
808 .WillOnce(Return(&mpc));
809 EXPECT_CALL(mpc, itemEnd(IsArrayOfSize(val.size()), encBegin, encBegin + 1, encEnd))
810 .WillOnce(Return(&mpc));
811 }
812
813 EXPECT_CALL(mpc, error(_, _)) //
814 .Times(0);
815
816 parse(encoded.data(), encoded.data() + encoded.size(), &mpc);
817 }
818
TEST(StreamParseTest,Map)819 TEST(StreamParseTest, Map) {
820 MockParseClient mpc;
821
822 Map val("Hello", 4, Array(-9, "Goodbye"), std::numeric_limits<uint64_t>::max());
823 ASSERT_NE(val[1].first->asArray(), nullptr);
824 const Array& interior = *(val[1].first->asArray());
825 auto encoded = val.encode();
826 uint8_t* encBegin = encoded.data();
827 uint8_t* encEnd = encoded.data() + encoded.size();
828
829 {
830 InSequence s;
831 const uint8_t* pos = encBegin;
832 EXPECT_CALL(mpc, item(_, pos, pos + 1, pos + 1)).WillOnce(Return(&mpc));
833 ++pos;
834 EXPECT_CALL(mpc, item(MatchesItem(ByRef(*val[0].first)), pos, pos + 1, pos + 6))
835 .WillOnce(Return(&mpc));
836 pos += 6;
837 EXPECT_CALL(mpc, item(MatchesItem(ByRef(*val[0].second)), pos, pos + 1, pos + 1))
838 .WillOnce(Return(&mpc));
839 ++pos;
840 const uint8_t* innerArrayBegin = pos;
841 EXPECT_CALL(mpc, item(IsArrayOfSize(interior.size()), pos, pos + 1, pos + 1))
842 .WillOnce(Return(&mpc));
843 ++pos;
844 EXPECT_CALL(mpc, item(MatchesItem(ByRef(*interior[0])), pos, pos + 1, pos + 1))
845 .WillOnce(Return(&mpc));
846 ++pos;
847 EXPECT_CALL(mpc, item(MatchesItem(ByRef(*interior[1])), pos, pos + 1, pos + 8))
848 .WillOnce(Return(&mpc));
849 pos += 8;
850 EXPECT_CALL(mpc, itemEnd(IsArrayOfSize(interior.size()), innerArrayBegin,
851 innerArrayBegin + 1, pos))
852 .WillOnce(Return(&mpc));
853 EXPECT_CALL(mpc, item(MatchesItem(ByRef(*val[1].second)), pos, pos + 9, pos + 9))
854 .WillOnce(Return(&mpc));
855 EXPECT_CALL(mpc, itemEnd(IsMapOfSize(val.size()), encBegin, encBegin + 1, encEnd))
856 .WillOnce(Return(&mpc));
857 }
858
859 EXPECT_CALL(mpc, error(_, _)) //
860 .Times(0);
861
862 parse(encoded.data(), encoded.data() + encoded.size(), &mpc);
863 }
864
TEST(StreamParseTest,Semantic)865 TEST(StreamParseTest, Semantic) {
866 MockParseClient mpc;
867
868 vector<uint8_t> encoded;
869 auto iter = back_inserter(encoded);
870 encodeHeader(SEMANTIC, 0, iter);
871 Uint(999).encode(iter);
872
873 EXPECT_CALL(mpc, item(_, _, _, _)).Times(0);
874 EXPECT_CALL(mpc, itemEnd(_, _, _, _)).Times(0);
875 EXPECT_CALL(mpc, error(encoded.data(), "Semantic tags not supported"));
876
877 parse(encoded.data(), encoded.data() + encoded.size(), &mpc);
878 }
879
TEST(FullParserTest,Uint)880 TEST(FullParserTest, Uint) {
881 Uint val(10);
882
883 auto [item, pos, message] = parse(val.encode());
884 EXPECT_THAT(item, MatchesItem(val));
885 }
886
TEST(FullParserTest,Nint)887 TEST(FullParserTest, Nint) {
888 Nint val(-10);
889
890 auto [item, pos, message] = parse(val.encode());
891 EXPECT_THAT(item, MatchesItem(val));
892
893 vector<uint8_t> minNint = {0x3B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
894
895 std::tie(item, pos, message) = parse(minNint);
896 EXPECT_THAT(item, NotNull());
897 EXPECT_EQ(item->asNint()->value(), std::numeric_limits<int64_t>::min());
898 }
899
TEST(FullParserTest,NintOutOfRange)900 TEST(FullParserTest, NintOutOfRange) {
901 vector<uint8_t> outOfRangeNint = {0x3B, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
902
903 auto [item, pos, message] = parse(outOfRangeNint);
904 EXPECT_THAT(item, IsNull());
905 EXPECT_EQ(pos, outOfRangeNint.data());
906 EXPECT_EQ(message, "NINT values that don't fit in int64_t are not supported.");
907 }
908
TEST(FullParserTest,Tstr)909 TEST(FullParserTest, Tstr) {
910 Tstr val("Hello");
911
912 auto [item, pos, message] = parse(val.encode());
913 EXPECT_THAT(item, MatchesItem(val));
914 }
915
TEST(FullParserTest,Bstr)916 TEST(FullParserTest, Bstr) {
917 Bstr val("\x00\x01\0x02"s);
918
919 auto [item, pos, message] = parse(val.encode());
920 EXPECT_THAT(item, MatchesItem(val));
921 }
922
TEST(FullParserTest,Array)923 TEST(FullParserTest, Array) {
924 Array val("hello", -4, 3);
925
926 auto encoded = val.encode();
927 auto [item, pos, message] = parse(encoded);
928 EXPECT_THAT(item, MatchesItem(ByRef(val)));
929 EXPECT_EQ(pos, encoded.data() + encoded.size());
930 EXPECT_EQ("", message);
931
932 // We've already checked it all, but walk it just for fun.
933 ASSERT_NE(nullptr, item->asArray());
934 const Array& arr = *(item->asArray());
935 ASSERT_EQ(arr[0]->type(), TSTR);
936 EXPECT_EQ(arr[0]->asTstr()->value(), "hello");
937 }
938
TEST(FullParserTest,Map)939 TEST(FullParserTest, Map) {
940 Map val("hello", -4, 3, Bstr("hi"));
941
942 auto [item, pos, message] = parse(val.encode());
943 EXPECT_THAT(item, MatchesItem(ByRef(val)));
944 }
945
TEST(FullParserTest,Complex)946 TEST(FullParserTest, Complex) {
947 vector<uint8_t> vec = {0x01, 0x02, 0x08, 0x03};
948 Map val("Outer1",
949 Array(Map("Inner1", 99, //
950 "Inner2", vec),
951 "foo"),
952 "Outer2", 10);
953
954 std::unique_ptr<Item> item;
955 const uint8_t* pos;
956 std::string message;
957 std::tie(item, pos, message) = parse(val.encode());
958 EXPECT_THAT(item, MatchesItem(ByRef(val)));
959 }
960
TEST(FullParserTest,IncompleteUint)961 TEST(FullParserTest, IncompleteUint) {
962 Uint val(1000);
963
964 auto encoding = val.encode();
965 auto [item, pos, message] = parse(encoding.data(), encoding.size() - 1);
966 EXPECT_EQ(nullptr, item.get());
967 EXPECT_EQ(encoding.data(), pos);
968 EXPECT_EQ("Need 2 byte(s) for length field, have 1.", message);
969 }
970
TEST(FullParserTest,IncompleteString)971 TEST(FullParserTest, IncompleteString) {
972 Tstr val("hello");
973
974 auto encoding = val.encode();
975 auto [item, pos, message] = parse(encoding.data(), encoding.size() - 2);
976 EXPECT_EQ(nullptr, item.get());
977 EXPECT_EQ(encoding.data(), pos);
978 EXPECT_EQ("Need 5 byte(s) for text string, have 3.", message);
979 }
980
TEST(FullParserTest,ArrayWithInsufficientEntries)981 TEST(FullParserTest, ArrayWithInsufficientEntries) {
982 Array val(1, 2, 3, 4);
983
984 auto encoding = val.encode();
985 auto [item, pos, message] = parse(encoding.data(), encoding.size() - 1);
986 EXPECT_EQ(nullptr, item.get());
987 EXPECT_EQ(encoding.data(), pos);
988 EXPECT_EQ("Not enough entries for array.", message);
989 }
990
TEST(FullParserTest,ArrayWithTruncatedEntry)991 TEST(FullParserTest, ArrayWithTruncatedEntry) {
992 Array val(1, 2, 3, 400000);
993
994 auto encoding = val.encode();
995 auto [item, pos, message] = parse(encoding.data(), encoding.size() - 1);
996 EXPECT_EQ(nullptr, item.get());
997 EXPECT_EQ(encoding.data() + encoding.size() - 5, pos);
998 EXPECT_EQ("Need 4 byte(s) for length field, have 3.", message);
999 }
1000
TEST(FullParserTest,MapWithTruncatedEntry)1001 TEST(FullParserTest, MapWithTruncatedEntry) {
1002 Map val(1, 2, 300000, 4);
1003
1004 auto encoding = val.encode();
1005 auto [item, pos, message] = parse(encoding.data(), encoding.size() - 2);
1006 EXPECT_EQ(nullptr, item.get());
1007 EXPECT_EQ(encoding.data() + 3, pos);
1008 EXPECT_EQ("Need 4 byte(s) for length field, have 3.", message);
1009 }
main(int argc,char ** argv)1010 int main(int argc, char** argv) {
1011 ::testing::InitGoogleTest(&argc, argv);
1012 return RUN_ALL_TESTS();
1013 }
1014