Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test for nested buffer verifier #7252

Merged
merged 2 commits into from
Apr 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 1 addition & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ if(MSVC)
/WX
/wd4512 # C4512: assignment operator could not be generated
/wd4316 # C4316: object allocated on the heap may not be aligned

/wd4456 # C4456: hides previous local declaration
$<$<CXX_COMPILER_ID:CLANG>:
/D_CRT_SECURE_NO_WARNINGS
>
Expand All @@ -359,7 +359,6 @@ else()
-Wold-style-cast
-Wimplicit-fallthrough
-Wextra-semi
-Werror=shadow
-fsigned-char
-Wnon-virtual-dtor

Expand Down
72 changes: 65 additions & 7 deletions tests/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ flatbuffers::DetachedBuffer CreateFlatBufferTest(std::string &buffer) {
#endif

// Make sure the template deduces an initializer as std::vector<std::string>
builder.CreateVectorOfStrings({"hello", "world"});
builder.CreateVectorOfStrings({ "hello", "world" });

// Create many vectors of strings
std::vector<std::string> manyNames;
Expand Down Expand Up @@ -227,7 +227,7 @@ flatbuffers::DetachedBuffer CreateFlatBufferTest(std::string &buffer) {

FinishMonsterBuffer(builder, mloc);

// clang-format off
// clang-format off
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unrelated changes?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I formatted the whole file, and these were wrong.

I really wish clang-format comments could be a macro instead of comment.

#ifdef FLATBUFFERS_TEST_VERBOSE
// print byte data for debugging:
auto p = builder.GetBufferPointer();
Expand All @@ -253,7 +253,7 @@ void AccessFlatBufferTest(const uint8_t *flatbuf, size_t length,
verifier.SetFlexReuseTracker(&flex_reuse_tracker);
TEST_EQ(VerifyMonsterBuffer(verifier), true);

// clang-format off
// clang-format off
#ifdef FLATBUFFERS_TRACK_VERIFIER_BUFFER_SIZE
std::vector<uint8_t> test_buff;
test_buff.resize(length * 2);
Expand Down Expand Up @@ -634,7 +634,7 @@ void SizePrefixedTest() {
}

void TriviallyCopyableTest() {
// clang-format off
// clang-format off
#if __GNUG__ && __GNUC__ < 5
TEST_EQ(__has_trivial_copy(Vec3), true);
#else
Expand Down Expand Up @@ -1615,7 +1615,7 @@ void FuzzTest2() {
}
};

// clang-format off
// clang-format off
#define AddToSchemaAndInstances(schema_add, instance_add) \
RndDef::Add(definitions, schema, instances_per_definition, \
schema_add, instance_add, definition)
Expand Down Expand Up @@ -1769,7 +1769,7 @@ void FuzzTest2() {
TEST_NOTNULL(nullptr); //-V501 (this comment supresses CWE-570 warning)
}

// clang-format off
// clang-format off
#ifdef FLATBUFFERS_TEST_VERBOSE
TEST_OUTPUT_LINE("%dk schema tested with %dk of json\n",
static_cast<int>(schema.length() / 1024),
Expand Down Expand Up @@ -3161,7 +3161,7 @@ void FlexBuffersTest() {
});
slb.Finish();

// clang-format off
// clang-format off
#ifdef FLATBUFFERS_TEST_VERBOSE
for (size_t i = 0; i < slb.GetBuffer().size(); i++)
printf("%d ", slb.GetBuffer().data()[i]);
Expand Down Expand Up @@ -4179,6 +4179,63 @@ void FieldIdentifierTest() {
#endif
}

void NestedVerifierTest() {
// Create a nested monster.
flatbuffers::FlatBufferBuilder nested_builder;
FinishMonsterBuffer(
nested_builder,
CreateMonster(nested_builder, nullptr, 0, 0,
nested_builder.CreateString("NestedMonster")));

// Verify the nested monster
flatbuffers::Verifier verifier(nested_builder.GetBufferPointer(),
nested_builder.GetSize());
TEST_EQ(true, VerifyMonsterBuffer(verifier));

{
// Create the outer monster.
flatbuffers::FlatBufferBuilder builder;

// Add the nested monster as a vector of bytes.
auto nested_monster_bytes = builder.CreateVector(
nested_builder.GetBufferPointer(), nested_builder.GetSize());

auto name = builder.CreateString("OuterMonster");

MonsterBuilder mon_builder(builder);
mon_builder.add_name(name);
mon_builder.add_testnestedflatbuffer(nested_monster_bytes);
FinishMonsterBuffer(builder, mon_builder.Finish());

// Verify the root monster, which includes verifing the nested monster
flatbuffers::Verifier verifier(builder.GetBufferPointer(),
builder.GetSize());
TEST_EQ(true, VerifyMonsterBuffer(verifier));
}

{
// Create the outer monster.
flatbuffers::FlatBufferBuilder builder;

// Purposely invalidate the nested flatbuffer setting its length to 1, an
// invalid length.
uint8_t invalid_nested_buffer[1];
auto nested_monster_bytes = builder.CreateVector(invalid_nested_buffer, 1);

auto name = builder.CreateString("OuterMonster");

MonsterBuilder mon_builder(builder);
mon_builder.add_name(name);
mon_builder.add_testnestedflatbuffer(nested_monster_bytes);
FinishMonsterBuffer(builder, mon_builder.Finish());

// Verify the root monster fails, since the included nested monster fails.
flatbuffers::Verifier verifier(builder.GetBufferPointer(),
builder.GetSize());
TEST_EQ(false, VerifyMonsterBuffer(verifier));
}
}

void ParseIncorrectMonsterJsonTest() {
std::string schemafile;
TEST_EQ(flatbuffers::LoadFile((test_data_path + "monster_test.bfbs").c_str(),
Expand Down Expand Up @@ -4364,6 +4421,7 @@ int FlatBufferTests() {
FixedLengthArraySpanTest();
StructUnionTest();
WarningsAsErrorsTest();
NestedVerifierTest();
return 0;
}

Expand Down