Bump version to 19.1.0-rc3
[llvm-project.git] / llvm / unittests / BinaryFormat / MsgPackReaderTest.cpp
blob06ae8b080d3a4236a78e075f4a871f1588ca0fa4
1 //===- MsgPackReaderTest.cpp ------------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
9 #include "llvm/BinaryFormat/MsgPackReader.h"
10 #include "llvm/BinaryFormat/MsgPack.h"
11 #include "gtest/gtest.h"
13 using namespace llvm;
14 using namespace llvm::msgpack;
16 struct MsgPackReader : testing::Test {
17 std::string Buffer;
18 Object Obj;
21 TEST_F(MsgPackReader, TestReadMultiple) {
22 Buffer = "\xc0\xc2";
23 Reader MPReader(Buffer);
25 auto ContinueOrErr = MPReader.read(Obj);
26 EXPECT_TRUE(static_cast<bool>(ContinueOrErr));
27 EXPECT_TRUE(*ContinueOrErr);
28 EXPECT_EQ(Obj.Kind, Type::Nil);
31 auto ContinueOrErr = MPReader.read(Obj);
32 EXPECT_TRUE(static_cast<bool>(ContinueOrErr));
33 EXPECT_TRUE(*ContinueOrErr);
34 EXPECT_EQ(Obj.Kind, Type::Boolean);
35 EXPECT_EQ(Obj.Bool, false);
38 auto ContinueOrErr = MPReader.read(Obj);
39 EXPECT_TRUE(static_cast<bool>(ContinueOrErr));
40 EXPECT_FALSE(*ContinueOrErr);
44 TEST_F(MsgPackReader, TestReadNil) {
45 Buffer = "\xc0";
46 Reader MPReader(Buffer);
47 auto ContinueOrErr = MPReader.read(Obj);
48 EXPECT_TRUE(static_cast<bool>(ContinueOrErr));
49 EXPECT_TRUE(*ContinueOrErr);
50 EXPECT_EQ(Obj.Kind, Type::Nil);
53 TEST_F(MsgPackReader, TestReadBoolFalse) {
54 Buffer = "\xc2";
55 Reader MPReader(Buffer);
56 auto ContinueOrErr = MPReader.read(Obj);
57 EXPECT_TRUE(static_cast<bool>(ContinueOrErr));
58 EXPECT_TRUE(*ContinueOrErr);
59 EXPECT_EQ(Obj.Kind, Type::Boolean);
60 EXPECT_EQ(Obj.Bool, false);
63 TEST_F(MsgPackReader, TestReadBoolTrue) {
64 Buffer = "\xc3";
65 Reader MPReader(Buffer);
66 auto ContinueOrErr = MPReader.read(Obj);
67 EXPECT_TRUE(static_cast<bool>(ContinueOrErr));
68 EXPECT_TRUE(*ContinueOrErr);
69 EXPECT_EQ(Obj.Kind, Type::Boolean);
70 EXPECT_EQ(Obj.Bool, true);
73 TEST_F(MsgPackReader, TestReadFixNegativeInt) {
74 // Positive values will be written in a UInt form, so max FixNegativeInt is -1
76 // FixNegativeInt form bitpattern starts with 111, so min FixNegativeInt
77 // is 11100000 = -32
78 for (int8_t i = -1; i >= -32; --i) {
79 Buffer.assign(1, static_cast<char>(i));
80 Reader MPReader(Buffer);
81 auto ContinueOrErr = MPReader.read(Obj);
82 EXPECT_TRUE(static_cast<bool>(ContinueOrErr));
83 EXPECT_TRUE(*ContinueOrErr);
84 EXPECT_EQ(Obj.Kind, Type::Int);
85 EXPECT_EQ(Obj.Int, i);
89 TEST_F(MsgPackReader, TestReadInt8Max) {
90 Buffer = "\xd0\x7f";
91 Reader MPReader(Buffer);
92 auto ContinueOrErr = MPReader.read(Obj);
93 EXPECT_TRUE(static_cast<bool>(ContinueOrErr));
94 EXPECT_TRUE(*ContinueOrErr);
95 EXPECT_EQ(Obj.Kind, Type::Int);
96 EXPECT_EQ(Obj.Int, INT8_MAX);
99 TEST_F(MsgPackReader, TestReadInt8Zero) {
100 Buffer.assign("\xd0\x00", 2);
101 Reader MPReader(Buffer);
102 auto ContinueOrErr = MPReader.read(Obj);
103 EXPECT_TRUE(static_cast<bool>(ContinueOrErr));
104 EXPECT_TRUE(*ContinueOrErr);
105 EXPECT_EQ(Obj.Kind, Type::Int);
106 EXPECT_EQ(Obj.Int, 0);
109 TEST_F(MsgPackReader, TestReadInt8Min) {
110 Buffer = "\xd0\x80";
111 Reader MPReader(Buffer);
112 auto ContinueOrErr = MPReader.read(Obj);
113 EXPECT_TRUE(static_cast<bool>(ContinueOrErr));
114 EXPECT_TRUE(*ContinueOrErr);
115 EXPECT_EQ(Obj.Kind, Type::Int);
116 EXPECT_EQ(Obj.Int, INT8_MIN);
119 TEST_F(MsgPackReader, TestReadInt16Max) {
120 Buffer = "\xd1\x7f\xff";
121 Reader MPReader(Buffer);
122 auto ContinueOrErr = MPReader.read(Obj);
123 EXPECT_TRUE(static_cast<bool>(ContinueOrErr));
124 EXPECT_TRUE(*ContinueOrErr);
125 EXPECT_EQ(Obj.Kind, Type::Int);
126 EXPECT_EQ(Obj.Int, INT16_MAX);
129 TEST_F(MsgPackReader, TestReadInt16Zero) {
130 Buffer.assign("\xd1\x00\x00", 3);
131 Reader MPReader(Buffer);
132 auto ContinueOrErr = MPReader.read(Obj);
133 EXPECT_TRUE(static_cast<bool>(ContinueOrErr));
134 EXPECT_TRUE(*ContinueOrErr);
135 EXPECT_EQ(Obj.Kind, Type::Int);
136 EXPECT_EQ(Obj.Int, 0);
139 TEST_F(MsgPackReader, TestReadInt16Min) {
140 Buffer.assign("\xd1\x80\x00", 3);
141 Reader MPReader(Buffer);
142 auto ContinueOrErr = MPReader.read(Obj);
143 EXPECT_TRUE(static_cast<bool>(ContinueOrErr));
144 EXPECT_TRUE(*ContinueOrErr);
145 EXPECT_EQ(Obj.Kind, Type::Int);
146 EXPECT_EQ(Obj.Int, INT16_MIN);
149 TEST_F(MsgPackReader, TestReadInt32Max) {
150 Buffer = "\xd2\x7f\xff\xff\xff";
151 Reader MPReader(Buffer);
152 auto ContinueOrErr = MPReader.read(Obj);
153 EXPECT_TRUE(static_cast<bool>(ContinueOrErr));
154 EXPECT_TRUE(*ContinueOrErr);
155 EXPECT_EQ(Obj.Kind, Type::Int);
156 EXPECT_EQ(Obj.Int, INT32_MAX);
159 TEST_F(MsgPackReader, TestReadInt32Zero) {
160 Buffer.assign("\xd2\x00\x00\x00\x00", 5);
161 Reader MPReader(Buffer);
162 auto ContinueOrErr = MPReader.read(Obj);
163 EXPECT_TRUE(static_cast<bool>(ContinueOrErr));
164 EXPECT_TRUE(*ContinueOrErr);
165 EXPECT_EQ(Obj.Kind, Type::Int);
166 EXPECT_EQ(Obj.Int, 0);
169 TEST_F(MsgPackReader, TestReadInt32Min) {
170 Buffer.assign("\xd2\x80\x00\x00\x00", 5);
171 Reader MPReader(Buffer);
172 auto ContinueOrErr = MPReader.read(Obj);
173 EXPECT_TRUE(static_cast<bool>(ContinueOrErr));
174 EXPECT_TRUE(*ContinueOrErr);
175 EXPECT_EQ(Obj.Kind, Type::Int);
176 EXPECT_EQ(Obj.Int, INT32_MIN);
179 TEST_F(MsgPackReader, TestReadInt64Max) {
180 Buffer = "\xd3\x7f\xff\xff\xff\xff\xff\xff\xff";
181 Reader MPReader(Buffer);
182 auto ContinueOrErr = MPReader.read(Obj);
183 EXPECT_TRUE(static_cast<bool>(ContinueOrErr));
184 EXPECT_TRUE(*ContinueOrErr);
185 EXPECT_EQ(Obj.Kind, Type::Int);
186 EXPECT_EQ(Obj.Int, INT64_MAX);
189 TEST_F(MsgPackReader, TestReadInt64Zero) {
190 Buffer.assign("\xd3\x00\x00\x00\x00\x00\x00\x00\x00", 9);
191 Reader MPReader(Buffer);
192 auto ContinueOrErr = MPReader.read(Obj);
193 EXPECT_TRUE(static_cast<bool>(ContinueOrErr));
194 EXPECT_TRUE(*ContinueOrErr);
195 EXPECT_EQ(Obj.Kind, Type::Int);
196 EXPECT_EQ(Obj.Int, 0);
199 TEST_F(MsgPackReader, TestReadInt64Min) {
200 Buffer.assign("\xd3\x80\x00\x00\x00\x00\x00\x00\x00", 9);
201 Reader MPReader(Buffer);
202 auto ContinueOrErr = MPReader.read(Obj);
203 EXPECT_TRUE(static_cast<bool>(ContinueOrErr));
204 EXPECT_TRUE(*ContinueOrErr);
205 EXPECT_EQ(Obj.Kind, Type::Int);
206 EXPECT_EQ(Obj.Int, INT64_MIN);
209 TEST_F(MsgPackReader, TestReadFixPositiveInt) {
210 // FixPositiveInt form bitpattern starts with 0, so max FixPositiveInt
211 // is 01111111 = 127
212 for (uint64_t u = 0; u <= 127; ++u) {
213 Buffer.assign(1, static_cast<char>(u));
214 Reader MPReader(Buffer);
215 auto ContinueOrErr = MPReader.read(Obj);
216 EXPECT_TRUE(static_cast<bool>(ContinueOrErr));
217 EXPECT_TRUE(*ContinueOrErr);
218 EXPECT_EQ(Obj.Kind, Type::UInt);
219 EXPECT_EQ(Obj.UInt, u);
223 TEST_F(MsgPackReader, TestReadUInt8Zero) {
224 Buffer.assign("\xcc\x00", 2);
225 Reader MPReader(Buffer);
226 auto ContinueOrErr = MPReader.read(Obj);
227 EXPECT_TRUE(static_cast<bool>(ContinueOrErr));
228 EXPECT_TRUE(*ContinueOrErr);
229 EXPECT_EQ(Obj.Kind, Type::UInt);
230 EXPECT_EQ(Obj.UInt, 0u);
233 TEST_F(MsgPackReader, TestReadUInt8One) {
234 Buffer = "\xcc\x01";
235 Reader MPReader(Buffer);
236 auto ContinueOrErr = MPReader.read(Obj);
237 EXPECT_TRUE(static_cast<bool>(ContinueOrErr));
238 EXPECT_TRUE(*ContinueOrErr);
239 EXPECT_EQ(Obj.Kind, Type::UInt);
240 EXPECT_EQ(Obj.UInt, 1u);
243 TEST_F(MsgPackReader, TestReadUInt8Max) {
244 Buffer = "\xcc\xff";
245 Reader MPReader(Buffer);
246 auto ContinueOrErr = MPReader.read(Obj);
247 EXPECT_TRUE(static_cast<bool>(ContinueOrErr));
248 EXPECT_TRUE(*ContinueOrErr);
249 EXPECT_EQ(Obj.Kind, Type::UInt);
250 EXPECT_EQ(Obj.UInt, static_cast<uint8_t>(UINT8_MAX));
253 TEST_F(MsgPackReader, TestReadUInt16Zero) {
254 Buffer.assign("\xcd\x00\x00", 3);
255 Reader MPReader(Buffer);
256 auto ContinueOrErr = MPReader.read(Obj);
257 EXPECT_TRUE(static_cast<bool>(ContinueOrErr));
258 EXPECT_TRUE(*ContinueOrErr);
259 EXPECT_EQ(Obj.Kind, Type::UInt);
260 EXPECT_EQ(Obj.UInt, 0u);
263 TEST_F(MsgPackReader, TestReadUInt16One) {
264 Buffer.assign("\xcd\x00\x01", 3);
265 Reader MPReader(Buffer);
266 auto ContinueOrErr = MPReader.read(Obj);
267 EXPECT_TRUE(static_cast<bool>(ContinueOrErr));
268 EXPECT_TRUE(*ContinueOrErr);
269 EXPECT_EQ(Obj.Kind, Type::UInt);
270 EXPECT_EQ(Obj.UInt, 1u);
273 TEST_F(MsgPackReader, TestReadUInt16Max) {
274 Buffer = "\xcd\xff\xff";
275 Reader MPReader(Buffer);
276 auto ContinueOrErr = MPReader.read(Obj);
277 EXPECT_TRUE(static_cast<bool>(ContinueOrErr));
278 EXPECT_TRUE(*ContinueOrErr);
279 EXPECT_EQ(Obj.Kind, Type::UInt);
280 EXPECT_EQ(Obj.UInt, static_cast<uint16_t>(UINT16_MAX));
283 TEST_F(MsgPackReader, TestReadUInt32Zero) {
284 Buffer.assign("\xce\x00\x00\x00\x00", 5);
285 Reader MPReader(Buffer);
286 auto ContinueOrErr = MPReader.read(Obj);
287 EXPECT_TRUE(static_cast<bool>(ContinueOrErr));
288 EXPECT_TRUE(*ContinueOrErr);
289 EXPECT_EQ(Obj.Kind, Type::UInt);
290 EXPECT_EQ(Obj.UInt, 0u);
293 TEST_F(MsgPackReader, TestReadUInt32One) {
294 Buffer.assign("\xce\x00\x00\x00\x01", 5);
295 Reader MPReader(Buffer);
296 auto ContinueOrErr = MPReader.read(Obj);
297 EXPECT_TRUE(static_cast<bool>(ContinueOrErr));
298 EXPECT_TRUE(*ContinueOrErr);
299 EXPECT_EQ(Obj.Kind, Type::UInt);
300 EXPECT_EQ(Obj.UInt, 1u);
303 TEST_F(MsgPackReader, TestReadUInt32Max) {
304 Buffer = "\xce\xff\xff\xff\xff";
305 Reader MPReader(Buffer);
306 auto ContinueOrErr = MPReader.read(Obj);
307 EXPECT_TRUE(static_cast<bool>(ContinueOrErr));
308 EXPECT_TRUE(*ContinueOrErr);
309 EXPECT_EQ(Obj.Kind, Type::UInt);
310 EXPECT_EQ(Obj.UInt, static_cast<uint32_t>(UINT32_MAX));
313 TEST_F(MsgPackReader, TestReadUInt64Zero) {
314 Buffer.assign("\xcf\x00\x00\x00\x00\x00\x00\x00\x00", 9);
315 Reader MPReader(Buffer);
316 auto ContinueOrErr = MPReader.read(Obj);
317 EXPECT_TRUE(static_cast<bool>(ContinueOrErr));
318 EXPECT_TRUE(*ContinueOrErr);
319 EXPECT_EQ(Obj.Kind, Type::UInt);
320 EXPECT_EQ(Obj.UInt, 0u);
323 TEST_F(MsgPackReader, TestReadUInt64One) {
324 Buffer.assign("\xcf\x00\x00\x00\x00\x00\x00\x00\x01", 9);
325 Reader MPReader(Buffer);
326 auto ContinueOrErr = MPReader.read(Obj);
327 EXPECT_TRUE(static_cast<bool>(ContinueOrErr));
328 EXPECT_TRUE(*ContinueOrErr);
329 EXPECT_EQ(Obj.Kind, Type::UInt);
330 EXPECT_EQ(Obj.UInt, 1u);
333 TEST_F(MsgPackReader, TestReadUInt64Max) {
334 Buffer = "\xcf\xff\xff\xff\xff\xff\xff\xff\xff";
335 Reader MPReader(Buffer);
336 auto ContinueOrErr = MPReader.read(Obj);
337 EXPECT_TRUE(static_cast<bool>(ContinueOrErr));
338 EXPECT_TRUE(*ContinueOrErr);
339 EXPECT_EQ(Obj.Kind, Type::UInt);
340 EXPECT_EQ(Obj.UInt, static_cast<uint64_t>(UINT64_MAX));
343 TEST_F(MsgPackReader, TestReadFloat32) {
344 Buffer = "\xca\xee\xee\xee\xef";
345 Reader MPReader(Buffer);
346 auto ContinueOrErr = MPReader.read(Obj);
347 EXPECT_TRUE(static_cast<bool>(ContinueOrErr));
348 EXPECT_TRUE(*ContinueOrErr);
349 EXPECT_EQ(Obj.Kind, Type::Float);
350 EXPECT_EQ(Obj.Float, -3.6973142664068907e+28f);
353 TEST_F(MsgPackReader, TestReadFloat64) {
354 Buffer = "\xcb\xee\xee\xee\xee\xee\xee\xee\xef";
355 Reader MPReader(Buffer);
356 auto ContinueOrErr = MPReader.read(Obj);
357 EXPECT_TRUE(static_cast<bool>(ContinueOrErr));
358 EXPECT_TRUE(*ContinueOrErr);
359 EXPECT_EQ(Obj.Kind, Type::Float);
360 EXPECT_EQ(Obj.Float, -2.2899894549927042e+226);
363 TEST_F(MsgPackReader, TestReadFixStrZero) {
364 Buffer = "\xa0";
365 Reader MPReader(Buffer);
366 auto ContinueOrErr = MPReader.read(Obj);
367 EXPECT_TRUE(static_cast<bool>(ContinueOrErr));
368 EXPECT_TRUE(*ContinueOrErr);
369 EXPECT_EQ(Obj.Kind, Type::String);
370 EXPECT_EQ(Obj.Raw, StringRef());
373 TEST_F(MsgPackReader, TestReadFixStrOne) {
374 std::string Result(1, 'a');
375 Buffer = std::string("\xa1") + Result;
376 Reader MPReader(Buffer);
377 auto ContinueOrErr = MPReader.read(Obj);
378 EXPECT_TRUE(static_cast<bool>(ContinueOrErr));
379 EXPECT_TRUE(*ContinueOrErr);
380 EXPECT_EQ(Obj.Kind, Type::String);
381 EXPECT_EQ(Obj.Raw, Result);
384 TEST_F(MsgPackReader, TestReadFixStrMax) {
385 // FixStr format's size is a 5 bit unsigned integer, so max is 11111 = 31
386 std::string Result(31, 'a');
387 Buffer = std::string("\xbf") + Result;
388 Reader MPReader(Buffer);
389 auto ContinueOrErr = MPReader.read(Obj);
390 EXPECT_TRUE(static_cast<bool>(ContinueOrErr));
391 EXPECT_TRUE(*ContinueOrErr);
392 EXPECT_EQ(Obj.Kind, Type::String);
393 EXPECT_EQ(Obj.Raw, Result);
396 TEST_F(MsgPackReader, TestReadStr8Zero) {
397 Buffer.assign("\xd9\x00", 2);
398 Reader MPReader(Buffer);
399 auto ContinueOrErr = MPReader.read(Obj);
400 EXPECT_TRUE(static_cast<bool>(ContinueOrErr));
401 EXPECT_TRUE(*ContinueOrErr);
402 EXPECT_EQ(Obj.Kind, Type::String);
403 EXPECT_EQ(Obj.Raw, StringRef());
406 TEST_F(MsgPackReader, TestReadStr8One) {
407 std::string Result(1, 'a');
408 Buffer = std::string("\xd9\x01") + Result;
409 Reader MPReader(Buffer);
410 auto ContinueOrErr = MPReader.read(Obj);
411 EXPECT_TRUE(static_cast<bool>(ContinueOrErr));
412 EXPECT_TRUE(*ContinueOrErr);
413 EXPECT_EQ(Obj.Kind, Type::String);
414 EXPECT_EQ(Obj.Raw, Result);
417 TEST_F(MsgPackReader, TestReadStr8Max) {
418 std::string Result(UINT8_MAX, 'a');
419 Buffer = std::string("\xd9\xff") + Result;
420 Reader MPReader(Buffer);
421 auto ContinueOrErr = MPReader.read(Obj);
422 EXPECT_TRUE(static_cast<bool>(ContinueOrErr));
423 EXPECT_TRUE(*ContinueOrErr);
424 EXPECT_EQ(Obj.Kind, Type::String);
425 EXPECT_EQ(Obj.Raw, Result);
428 TEST_F(MsgPackReader, TestReadStr16Zero) {
429 Buffer.assign("\xda\x00\x00", 3);
430 Reader MPReader(Buffer);
431 auto ContinueOrErr = MPReader.read(Obj);
432 EXPECT_TRUE(static_cast<bool>(ContinueOrErr));
433 EXPECT_TRUE(*ContinueOrErr);
434 EXPECT_EQ(Obj.Kind, Type::String);
435 EXPECT_EQ(Obj.Raw, StringRef());
438 TEST_F(MsgPackReader, TestReadStr16One) {
439 std::string Result(1, 'a');
440 Buffer = std::string("\xda\x00\x01", 3) + Result;
441 Reader MPReader(Buffer);
442 auto ContinueOrErr = MPReader.read(Obj);
443 EXPECT_TRUE(static_cast<bool>(ContinueOrErr));
444 EXPECT_TRUE(*ContinueOrErr);
445 EXPECT_EQ(Obj.Kind, Type::String);
446 EXPECT_EQ(Obj.Raw, Result);
449 TEST_F(MsgPackReader, TestReadStr16Max) {
450 std::string Result(UINT16_MAX, 'a');
451 Buffer = std::string("\xda\xff\xff") + Result;
452 Reader MPReader(Buffer);
453 auto ContinueOrErr = MPReader.read(Obj);
454 EXPECT_TRUE(static_cast<bool>(ContinueOrErr));
455 EXPECT_TRUE(*ContinueOrErr);
456 EXPECT_EQ(Obj.Kind, Type::String);
457 EXPECT_EQ(Obj.Raw, Result);
460 TEST_F(MsgPackReader, TestReadStr32Zero) {
461 Buffer.assign("\xdb\x00\x00\x00\x00", 5);
462 Reader MPReader(Buffer);
463 auto ContinueOrErr = MPReader.read(Obj);
464 EXPECT_TRUE(static_cast<bool>(ContinueOrErr));
465 EXPECT_TRUE(*ContinueOrErr);
466 EXPECT_EQ(Obj.Kind, Type::String);
467 EXPECT_EQ(Obj.Raw, StringRef());
470 TEST_F(MsgPackReader, TestReadStr32One) {
471 std::string Result(1, 'a');
472 Buffer = std::string("\xdb\x00\x00\x00\x01", 5) + Result;
473 Reader MPReader(Buffer);
474 auto ContinueOrErr = MPReader.read(Obj);
475 EXPECT_TRUE(static_cast<bool>(ContinueOrErr));
476 EXPECT_TRUE(*ContinueOrErr);
477 EXPECT_EQ(Obj.Kind, Type::String);
478 EXPECT_EQ(Obj.Raw, Result);
481 TEST_F(MsgPackReader, TestReadStr32Max) {
482 std::string Result(static_cast<uint32_t>(UINT16_MAX) + 1, 'a');
483 Buffer = std::string("\xdb\x00\x01\x00\x00", 5) + Result;
484 Reader MPReader(Buffer);
485 auto ContinueOrErr = MPReader.read(Obj);
486 EXPECT_TRUE(static_cast<bool>(ContinueOrErr));
487 EXPECT_TRUE(*ContinueOrErr);
488 EXPECT_EQ(Obj.Kind, Type::String);
489 EXPECT_EQ(Obj.Raw, Result);
492 TEST_F(MsgPackReader, TestReadBin8Zero) {
493 Buffer.assign("\xc4\x00", 2);
494 Reader MPReader(Buffer);
495 auto ContinueOrErr = MPReader.read(Obj);
496 EXPECT_TRUE(static_cast<bool>(ContinueOrErr));
497 EXPECT_TRUE(*ContinueOrErr);
498 EXPECT_EQ(Obj.Kind, Type::Binary);
499 EXPECT_EQ(Obj.Raw, StringRef());
502 TEST_F(MsgPackReader, TestReadBin8One) {
503 std::string Result(1, 'a');
504 Buffer = std::string("\xc4\x01") + Result;
505 Reader MPReader(Buffer);
506 auto ContinueOrErr = MPReader.read(Obj);
507 EXPECT_TRUE(static_cast<bool>(ContinueOrErr));
508 EXPECT_TRUE(*ContinueOrErr);
509 EXPECT_EQ(Obj.Kind, Type::Binary);
510 EXPECT_EQ(Obj.Raw, Result);
513 TEST_F(MsgPackReader, TestReadBin8Max) {
514 std::string Result(UINT8_MAX, 'a');
515 Buffer = std::string("\xc4\xff") + Result;
516 Reader MPReader(Buffer);
517 auto ContinueOrErr = MPReader.read(Obj);
518 EXPECT_TRUE(static_cast<bool>(ContinueOrErr));
519 EXPECT_TRUE(*ContinueOrErr);
520 EXPECT_EQ(Obj.Kind, Type::Binary);
521 EXPECT_EQ(Obj.Raw, Result);
524 TEST_F(MsgPackReader, TestReadBin16Zero) {
525 Buffer.assign("\xc5\x00\x00", 3);
526 Reader MPReader(Buffer);
527 auto ContinueOrErr = MPReader.read(Obj);
528 EXPECT_TRUE(static_cast<bool>(ContinueOrErr));
529 EXPECT_TRUE(*ContinueOrErr);
530 EXPECT_EQ(Obj.Kind, Type::Binary);
531 EXPECT_EQ(Obj.Raw, StringRef());
534 TEST_F(MsgPackReader, TestReadBin16One) {
535 std::string Result(1, 'a');
536 Buffer = std::string("\xc5\x00\x01", 3) + Result;
537 Reader MPReader(Buffer);
538 auto ContinueOrErr = MPReader.read(Obj);
539 EXPECT_TRUE(static_cast<bool>(ContinueOrErr));
540 EXPECT_TRUE(*ContinueOrErr);
541 EXPECT_EQ(Obj.Kind, Type::Binary);
542 EXPECT_EQ(Obj.Raw, Result);
545 TEST_F(MsgPackReader, TestReadBin16Max) {
546 std::string Result(UINT16_MAX, 'a');
547 Buffer = std::string("\xc5\xff\xff") + Result;
548 Reader MPReader(Buffer);
549 auto ContinueOrErr = MPReader.read(Obj);
550 EXPECT_TRUE(static_cast<bool>(ContinueOrErr));
551 EXPECT_TRUE(*ContinueOrErr);
552 EXPECT_EQ(Obj.Kind, Type::Binary);
553 EXPECT_EQ(Obj.Raw, Result);
556 TEST_F(MsgPackReader, TestReadBin32Zero) {
557 Buffer.assign("\xc6\x00\x00\x00\x00", 5);
558 Reader MPReader(Buffer);
559 auto ContinueOrErr = MPReader.read(Obj);
560 EXPECT_TRUE(static_cast<bool>(ContinueOrErr));
561 EXPECT_TRUE(*ContinueOrErr);
562 EXPECT_EQ(Obj.Kind, Type::Binary);
563 EXPECT_EQ(Obj.Raw, StringRef());
566 TEST_F(MsgPackReader, TestReadBin32One) {
567 std::string Result(1, 'a');
568 Buffer = std::string("\xc6\x00\x00\x00\x01", 5) + Result;
569 Reader MPReader(Buffer);
570 auto ContinueOrErr = MPReader.read(Obj);
571 EXPECT_TRUE(static_cast<bool>(ContinueOrErr));
572 EXPECT_TRUE(*ContinueOrErr);
573 EXPECT_EQ(Obj.Kind, Type::Binary);
574 EXPECT_EQ(Obj.Raw, Result);
577 TEST_F(MsgPackReader, TestReadBin32Max) {
578 std::string Result(static_cast<uint32_t>(UINT16_MAX) + 1, 'a');
579 Buffer = std::string("\xc6\x00\x01\x00\x00", 5) + Result;
580 Reader MPReader(Buffer);
581 auto ContinueOrErr = MPReader.read(Obj);
582 EXPECT_TRUE(static_cast<bool>(ContinueOrErr));
583 EXPECT_TRUE(*ContinueOrErr);
584 EXPECT_EQ(Obj.Kind, Type::Binary);
585 EXPECT_EQ(Obj.Raw, Result);
588 TEST_F(MsgPackReader, TestReadFixArrayZero) {
589 Buffer = "\x90";
590 Reader MPReader(Buffer);
591 auto ContinueOrErr = MPReader.read(Obj);
592 EXPECT_TRUE(static_cast<bool>(ContinueOrErr));
593 EXPECT_TRUE(*ContinueOrErr);
594 EXPECT_EQ(Obj.Kind, Type::Array);
595 EXPECT_EQ(Obj.Length, 0u);
598 TEST_F(MsgPackReader, TestReadFixArrayOne) {
599 Buffer = "\x91";
600 Reader MPReader(Buffer);
601 auto ContinueOrErr = MPReader.read(Obj);
602 EXPECT_TRUE(static_cast<bool>(ContinueOrErr));
603 EXPECT_TRUE(*ContinueOrErr);
604 EXPECT_EQ(Obj.Kind, Type::Array);
605 EXPECT_EQ(Obj.Length, 1u);
608 TEST_F(MsgPackReader, TestReadFixArrayMax) {
609 Buffer = "\x9f";
610 Reader MPReader(Buffer);
611 auto ContinueOrErr = MPReader.read(Obj);
612 EXPECT_TRUE(static_cast<bool>(ContinueOrErr));
613 EXPECT_TRUE(*ContinueOrErr);
614 EXPECT_EQ(Obj.Kind, Type::Array);
615 // FixArray format's size is a 4 bit unsigned integer, so max is 1111 = 15
616 EXPECT_EQ(Obj.Length, 15u);
619 TEST_F(MsgPackReader, TestReadArray16Zero) {
620 Buffer.assign("\xdc\x00\x00", 3);
621 Reader MPReader(Buffer);
622 auto ContinueOrErr = MPReader.read(Obj);
623 EXPECT_TRUE(static_cast<bool>(ContinueOrErr));
624 EXPECT_TRUE(*ContinueOrErr);
625 EXPECT_EQ(Obj.Kind, Type::Array);
626 EXPECT_EQ(Obj.Length, 0u);
629 TEST_F(MsgPackReader, TestReadArray16One) {
630 Buffer.assign("\xdc\x00\x01", 3);
631 Reader MPReader(Buffer);
632 auto ContinueOrErr = MPReader.read(Obj);
633 EXPECT_TRUE(static_cast<bool>(ContinueOrErr));
634 EXPECT_TRUE(*ContinueOrErr);
635 EXPECT_EQ(Obj.Kind, Type::Array);
636 EXPECT_EQ(Obj.Length, 1u);
639 TEST_F(MsgPackReader, TestReadArray16Max) {
640 Buffer = "\xdc\xff\xff";
641 Reader MPReader(Buffer);
642 auto ContinueOrErr = MPReader.read(Obj);
643 EXPECT_TRUE(static_cast<bool>(ContinueOrErr));
644 EXPECT_TRUE(*ContinueOrErr);
645 EXPECT_EQ(Obj.Kind, Type::Array);
646 EXPECT_EQ(Obj.Length, static_cast<uint16_t>(UINT16_MAX));
649 TEST_F(MsgPackReader, TestReadArray32Zero) {
650 Buffer.assign("\xdd\x00\x00\x00\x00", 5);
651 Reader MPReader(Buffer);
652 auto ContinueOrErr = MPReader.read(Obj);
653 EXPECT_TRUE(static_cast<bool>(ContinueOrErr));
654 EXPECT_TRUE(*ContinueOrErr);
655 EXPECT_EQ(Obj.Kind, Type::Array);
656 EXPECT_EQ(Obj.Length, 0u);
659 TEST_F(MsgPackReader, TestReadArray32One) {
660 Buffer.assign("\xdd\x00\x00\x00\x01", 5);
661 Reader MPReader(Buffer);
662 auto ContinueOrErr = MPReader.read(Obj);
663 EXPECT_TRUE(static_cast<bool>(ContinueOrErr));
664 EXPECT_TRUE(*ContinueOrErr);
665 EXPECT_EQ(Obj.Kind, Type::Array);
666 EXPECT_EQ(Obj.Length, 1u);
669 TEST_F(MsgPackReader, TestReadArray32Max) {
670 Buffer = "\xdd\xff\xff\xff\xff";
671 Reader MPReader(Buffer);
672 auto ContinueOrErr = MPReader.read(Obj);
673 EXPECT_TRUE(static_cast<bool>(ContinueOrErr));
674 EXPECT_TRUE(*ContinueOrErr);
675 EXPECT_EQ(Obj.Kind, Type::Array);
676 EXPECT_EQ(Obj.Length, static_cast<uint32_t>(UINT32_MAX));
679 TEST_F(MsgPackReader, TestReadFixMapZero) {
680 Buffer = "\x80";
681 Reader MPReader(Buffer);
682 auto ContinueOrErr = MPReader.read(Obj);
683 EXPECT_TRUE(static_cast<bool>(ContinueOrErr));
684 EXPECT_TRUE(*ContinueOrErr);
685 EXPECT_EQ(Obj.Kind, Type::Map);
686 EXPECT_EQ(Obj.Length, 0u);
689 TEST_F(MsgPackReader, TestReadFixMapOne) {
690 Buffer = "\x81";
691 Reader MPReader(Buffer);
692 auto ContinueOrErr = MPReader.read(Obj);
693 EXPECT_TRUE(static_cast<bool>(ContinueOrErr));
694 EXPECT_TRUE(*ContinueOrErr);
695 EXPECT_EQ(Obj.Kind, Type::Map);
696 EXPECT_EQ(Obj.Length, 1u);
699 TEST_F(MsgPackReader, TestReadFixMapMax) {
700 Buffer = "\x8f";
701 Reader MPReader(Buffer);
702 auto ContinueOrErr = MPReader.read(Obj);
703 EXPECT_TRUE(static_cast<bool>(ContinueOrErr));
704 EXPECT_TRUE(*ContinueOrErr);
705 EXPECT_EQ(Obj.Kind, Type::Map);
706 // FixMap format's size is a 4 bit unsigned integer, so max is 1111 = 15
707 EXPECT_EQ(Obj.Length, 15u);
710 TEST_F(MsgPackReader, TestReadMap16Zero) {
711 Buffer.assign("\xde\x00\x00", 3);
712 Reader MPReader(Buffer);
713 auto ContinueOrErr = MPReader.read(Obj);
714 EXPECT_TRUE(static_cast<bool>(ContinueOrErr));
715 EXPECT_TRUE(*ContinueOrErr);
716 EXPECT_EQ(Obj.Kind, Type::Map);
717 EXPECT_EQ(Obj.Length, 0u);
720 TEST_F(MsgPackReader, TestReadMap16One) {
721 Buffer.assign("\xde\x00\x01", 3);
722 Reader MPReader(Buffer);
723 auto ContinueOrErr = MPReader.read(Obj);
724 EXPECT_TRUE(static_cast<bool>(ContinueOrErr));
725 EXPECT_TRUE(*ContinueOrErr);
726 EXPECT_EQ(Obj.Kind, Type::Map);
727 EXPECT_EQ(Obj.Length, 1u);
730 TEST_F(MsgPackReader, TestReadMap16Max) {
731 Buffer = "\xde\xff\xff";
732 Reader MPReader(Buffer);
733 auto ContinueOrErr = MPReader.read(Obj);
734 EXPECT_TRUE(static_cast<bool>(ContinueOrErr));
735 EXPECT_TRUE(*ContinueOrErr);
736 EXPECT_EQ(Obj.Kind, Type::Map);
737 EXPECT_EQ(Obj.Length, static_cast<uint16_t>(UINT16_MAX));
740 TEST_F(MsgPackReader, TestReadMap32Zero) {
741 Buffer.assign("\xdf\x00\x00\x00\x00", 5);
742 Reader MPReader(Buffer);
743 auto ContinueOrErr = MPReader.read(Obj);
744 EXPECT_TRUE(static_cast<bool>(ContinueOrErr));
745 EXPECT_TRUE(*ContinueOrErr);
746 EXPECT_EQ(Obj.Kind, Type::Map);
747 EXPECT_EQ(Obj.Length, 0u);
750 TEST_F(MsgPackReader, TestReadMap32One) {
751 Buffer.assign("\xdf\x00\x00\x00\x01", 5);
752 Reader MPReader(Buffer);
753 auto ContinueOrErr = MPReader.read(Obj);
754 EXPECT_TRUE(static_cast<bool>(ContinueOrErr));
755 EXPECT_TRUE(*ContinueOrErr);
756 EXPECT_EQ(Obj.Kind, Type::Map);
757 EXPECT_EQ(Obj.Length, 1u);
760 TEST_F(MsgPackReader, TestReadMap32Max) {
761 Buffer = "\xdf\xff\xff\xff\xff";
762 Reader MPReader(Buffer);
763 auto ContinueOrErr = MPReader.read(Obj);
764 EXPECT_TRUE(static_cast<bool>(ContinueOrErr));
765 EXPECT_TRUE(*ContinueOrErr);
766 EXPECT_EQ(Obj.Kind, Type::Map);
767 EXPECT_EQ(Obj.Length, static_cast<uint32_t>(UINT32_MAX));
770 // FixExt formats are only available for these specific lengths: 1, 2, 4, 8, 16
772 TEST_F(MsgPackReader, TestReadFixExt1) {
773 std::string Result(1, 'a');
774 Buffer = std::string("\xd4\x01") + Result;
775 Reader MPReader(Buffer);
776 auto ContinueOrErr = MPReader.read(Obj);
777 EXPECT_TRUE(static_cast<bool>(ContinueOrErr));
778 EXPECT_TRUE(*ContinueOrErr);
779 EXPECT_EQ(Obj.Kind, Type::Extension);
780 EXPECT_EQ(Obj.Extension.Type, 0x01);
781 EXPECT_EQ(Obj.Extension.Bytes, Result);
784 TEST_F(MsgPackReader, TestReadFixExt2) {
785 std::string Result(2, 'a');
786 Buffer = std::string("\xd5\x01") + Result;
787 Reader MPReader(Buffer);
788 auto ContinueOrErr = MPReader.read(Obj);
789 EXPECT_TRUE(static_cast<bool>(ContinueOrErr));
790 EXPECT_TRUE(*ContinueOrErr);
791 EXPECT_EQ(Obj.Kind, Type::Extension);
792 EXPECT_EQ(Obj.Extension.Type, 0x01);
793 EXPECT_EQ(Obj.Extension.Bytes, Result);
796 TEST_F(MsgPackReader, TestReadFixExt4) {
797 std::string Result(4, 'a');
798 Buffer = std::string("\xd6\x01") + Result;
799 Reader MPReader(Buffer);
800 auto ContinueOrErr = MPReader.read(Obj);
801 EXPECT_TRUE(static_cast<bool>(ContinueOrErr));
802 EXPECT_TRUE(*ContinueOrErr);
803 EXPECT_EQ(Obj.Kind, Type::Extension);
804 EXPECT_EQ(Obj.Extension.Type, 0x01);
805 EXPECT_EQ(Obj.Extension.Bytes, Result);
808 TEST_F(MsgPackReader, TestReadFixExt8) {
809 std::string Result(8, 'a');
810 Buffer = std::string("\xd7\x01") + Result;
811 Reader MPReader(Buffer);
812 auto ContinueOrErr = MPReader.read(Obj);
813 EXPECT_TRUE(static_cast<bool>(ContinueOrErr));
814 EXPECT_TRUE(*ContinueOrErr);
815 EXPECT_EQ(Obj.Kind, Type::Extension);
816 EXPECT_EQ(Obj.Extension.Type, 0x01);
817 EXPECT_EQ(Obj.Extension.Bytes, Result);
820 TEST_F(MsgPackReader, TestReadFixExt16) {
821 std::string Result(16, 'a');
822 Buffer = std::string("\xd8\x01") + Result;
823 Reader MPReader(Buffer);
824 auto ContinueOrErr = MPReader.read(Obj);
825 EXPECT_TRUE(static_cast<bool>(ContinueOrErr));
826 EXPECT_TRUE(*ContinueOrErr);
827 EXPECT_EQ(Obj.Kind, Type::Extension);
828 EXPECT_EQ(Obj.Extension.Type, 0x01);
829 EXPECT_EQ(Obj.Extension.Bytes, Result);
832 TEST_F(MsgPackReader, TestReadExt8Min) {
833 // There are fix variants for sizes 1 and 2
834 Buffer.assign("\xc7\x00\x01", 3);
835 Reader MPReader(Buffer);
836 auto ContinueOrErr = MPReader.read(Obj);
837 EXPECT_TRUE(static_cast<bool>(ContinueOrErr));
838 EXPECT_TRUE(*ContinueOrErr);
839 EXPECT_EQ(Obj.Kind, Type::Extension);
840 EXPECT_EQ(Obj.Extension.Type, 0x01);
841 EXPECT_EQ(Obj.Extension.Bytes, StringRef());
844 TEST_F(MsgPackReader, TestReadExt8Max) {
845 std::string Result(UINT8_MAX, 'a');
846 Buffer = std::string("\xc7\xff\x01", 3) + Result;
847 Reader MPReader(Buffer);
848 auto ContinueOrErr = MPReader.read(Obj);
849 EXPECT_TRUE(static_cast<bool>(ContinueOrErr));
850 EXPECT_TRUE(*ContinueOrErr);
851 EXPECT_EQ(Obj.Kind, Type::Extension);
852 EXPECT_EQ(Obj.Extension.Type, 0x01);
853 EXPECT_EQ(Obj.Extension.Bytes, Result);
856 TEST_F(MsgPackReader, TestReadExt16Min) {
857 std::string Result(static_cast<uint16_t>(UINT8_MAX) + 1, 'a');
858 Buffer = std::string("\xc8\x01\x00\x01", 4) + Result;
859 Reader MPReader(Buffer);
860 auto ContinueOrErr = MPReader.read(Obj);
861 EXPECT_TRUE(static_cast<bool>(ContinueOrErr));
862 EXPECT_TRUE(*ContinueOrErr);
863 EXPECT_EQ(Obj.Kind, Type::Extension);
864 EXPECT_EQ(Obj.Extension.Type, 0x01);
865 EXPECT_EQ(Obj.Extension.Bytes, Result);
868 TEST_F(MsgPackReader, TestReadExt16Max) {
869 std::string Result(UINT16_MAX, 'a');
870 Buffer = std::string("\xc8\xff\xff\x01") + Result;
871 Reader MPReader(Buffer);
872 auto ContinueOrErr = MPReader.read(Obj);
873 EXPECT_TRUE(static_cast<bool>(ContinueOrErr));
874 EXPECT_TRUE(*ContinueOrErr);
875 EXPECT_EQ(Obj.Kind, Type::Extension);
876 EXPECT_EQ(Obj.Extension.Type, 0x01);
877 EXPECT_EQ(Obj.Extension.Bytes, Result);
880 TEST_F(MsgPackReader, TestReadExt32Min) {
881 std::string Result(static_cast<uint32_t>(UINT16_MAX) + 1, 'a');
882 Buffer = std::string("\xc9\x00\x01\x00\x00\x01", 6) + Result;
883 Reader MPReader(Buffer);
884 auto ContinueOrErr = MPReader.read(Obj);
885 EXPECT_TRUE(static_cast<bool>(ContinueOrErr));
886 EXPECT_TRUE(*ContinueOrErr);
887 EXPECT_EQ(Obj.Kind, Type::Extension);
888 EXPECT_EQ(Obj.Extension.Type, 0x01);
889 EXPECT_EQ(Obj.Extension.Bytes, Result);