Skip to content

Commit 134beee

Browse files
committed
Merge pull request #379 from whoshuu/increase-unpack-coverage
Add coverage for all free unpack codepaths
2 parents ea991d5 + d8dd778 commit 134beee

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed

test/pack_unpack.cpp

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,3 +391,77 @@ TEST(unpack, parse_error)
391391
}
392392
EXPECT_TRUE(thrown);
393393
}
394+
395+
TEST(unpack, returned_parse_error)
396+
{
397+
msgpack::sbuffer sbuf;
398+
399+
char c = '\xc1';
400+
sbuf.write(&c, 1);
401+
402+
bool thrown = false;
403+
try {
404+
msgpack::unpack(sbuf.data(), sbuf.size());
405+
EXPECT_TRUE(false);
406+
}
407+
catch (msgpack::parse_error const&) {
408+
thrown = true;
409+
}
410+
EXPECT_TRUE(thrown);
411+
}
412+
413+
TEST(unpack, zone_parse_error)
414+
{
415+
msgpack::sbuffer sbuf;
416+
417+
char c = '\xc1';
418+
sbuf.write(&c, 1);
419+
420+
bool thrown = false;
421+
msgpack::zone z;
422+
try {
423+
msgpack::unpack(z, sbuf.data(), sbuf.size());
424+
EXPECT_TRUE(false);
425+
}
426+
catch (msgpack::parse_error const&) {
427+
thrown = true;
428+
}
429+
EXPECT_TRUE(thrown);
430+
}
431+
432+
TEST(unpack, extra_bytes)
433+
{
434+
msgpack::sbuffer sbuf;
435+
msgpack::pack(sbuf, 1);
436+
437+
msgpack::unpacked msg = msgpack::unpack(sbuf.data(), sbuf.size() + 1);
438+
EXPECT_EQ(1, msg.get().as<int>());
439+
}
440+
441+
TEST(unpack, zone_extra_bytes)
442+
{
443+
msgpack::sbuffer sbuf;
444+
msgpack::pack(sbuf, 1);
445+
446+
msgpack::zone z;
447+
msgpack::object obj = msgpack::unpack(z, sbuf.data(), sbuf.size() + 1);
448+
EXPECT_EQ(1, obj.as<int>());
449+
}
450+
451+
TEST(unpack, int_off_larger_than_length)
452+
{
453+
msgpack::sbuffer sbuf;
454+
msgpack::pack(sbuf, 1);
455+
456+
std::size_t off = 2;
457+
458+
bool thrown = false;
459+
try {
460+
msgpack::unpacked msg = msgpack::unpack(sbuf.data(), sbuf.size(), off);
461+
}
462+
catch (msgpack::insufficient_bytes const&) {
463+
thrown = true;
464+
}
465+
EXPECT_TRUE(thrown);
466+
EXPECT_EQ(off, 2u);
467+
}

0 commit comments

Comments
 (0)