Skip to content

Commit

Permalink
Allow building host tests with code coverage (#33141)
Browse files Browse the repository at this point in the history
* Fix access for == operator defined for AccessControl

* Fix nested if warning

* Fix access to ==operator and fix mismatched types

* Allow building host tests with code coverage

* Ignore virtual-device-app build directory

* Fix initialization of CHIPoBLEStackMgr with google test

* Fix find -exec syntax

* Exclude tests from the coverage report

* Revert "Ignore virtual-device-app build directory"

This reverts commit 2215235.
  • Loading branch information
arkq authored Apr 25, 2024
1 parent d342d9b commit a28965b
Show file tree
Hide file tree
Showing 10 changed files with 30 additions and 60 deletions.
2 changes: 1 addition & 1 deletion scripts/build/build/target.py
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ def Create(self, name: str, runner, repository_path: str, output_prefix: str,
builder.target = self
builder.identifier = name
builder.output_dir = os.path.join(output_prefix, name)
builder.chip_dir = repository_path
builder.chip_dir = os.path.abspath(repository_path)
builder.options = builder_options

return builder
2 changes: 1 addition & 1 deletion scripts/build/build/targets.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ def BuildHostTarget():
target.AppendModifier("ossfuzz", fuzzing_type=HostFuzzingType.OSS_FUZZ).OnlyIfRe(
"-clang").ExceptIfRe('-libfuzzer')
target.AppendModifier('coverage', use_coverage=True).OnlyIfRe(
'-(chip-tool|all-clusters)')
'-(chip-tool|all-clusters|tests)')
target.AppendModifier('dmalloc', use_dmalloc=True)
target.AppendModifier('clang', use_clang=True)
target.AppendModifier('test', extra_tests=True)
Expand Down
6 changes: 4 additions & 2 deletions scripts/build/builders/host.py
Original file line number Diff line number Diff line change
Expand Up @@ -512,19 +512,21 @@ def generate(self):
def PreBuildCommand(self):
if self.app == HostApp.TESTS and self.use_coverage:
self._Execute(['ninja', '-C', self.output_dir, 'default'], title="Build-only")
self._Execute(['find', os.path.join(self.output_dir, 'obj/src/'), '-depth',
'-name', 'tests', '-exec', 'rm -rf {} \\;'], title="Cleanup unit tests")
self._Execute(['lcov', '--initial', '--capture', '--directory', os.path.join(self.output_dir, 'obj'),
'--exclude', os.path.join(self.chip_dir, '**/tests/*'),
'--exclude', os.path.join(self.chip_dir, 'zzz_generated/*'),
'--exclude', os.path.join(self.chip_dir, 'third_party/*'),
'--exclude', os.path.join(self.chip_dir, 'out/*'),
'--exclude', '/usr/include/*',
'--output-file', os.path.join(self.coverage_dir, 'lcov_base.info')], title="Initial coverage baseline")

def PostBuildCommand(self):
if self.app == HostApp.TESTS and self.use_coverage:
self._Execute(['lcov', '--capture', '--directory', os.path.join(self.output_dir, 'obj'),
'--exclude', os.path.join(self.chip_dir, '**/tests/*'),
'--exclude', os.path.join(self.chip_dir, 'zzz_generated/*'),
'--exclude', os.path.join(self.chip_dir, 'third_party/*'),
'--exclude', os.path.join(self.chip_dir, 'out/*'),
'--exclude', '/usr/include/*',
'--output-file', os.path.join(self.coverage_dir, 'lcov_test.info')], title="Update coverage")
self._Execute(['lcov', '--add-tracefile', os.path.join(self.coverage_dir, 'lcov_base.info'),
Expand Down
6 changes: 4 additions & 2 deletions src/access/tests/TestAccessControl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@

#include <gtest/gtest.h>

namespace {
namespace chip {
namespace Access {

using namespace chip;
using namespace chip::Access;
Expand Down Expand Up @@ -2189,4 +2190,5 @@ TEST_F(TestAccessControl, TestUpdateEntry)
}
}

} // namespace
} // namespace Access
} // namespace chip
3 changes: 1 addition & 2 deletions src/lib/core/tests/TestTLV.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -859,8 +859,7 @@ void WriteEncoding3(TLVWriter & writer)
TLVWriter writer1;

err = writer.OpenContainer(ProfileTag(TestProfile_1, 1), kTLVType_Structure, writer1);
if (err != CHIP_NO_ERROR)
EXPECT_EQ(err, CHIP_NO_ERROR);
EXPECT_EQ(err, CHIP_NO_ERROR);

err = writer1.PutBoolean(ProfileTag(TestProfile_2, 2), false);
EXPECT_EQ(err, CHIP_NO_ERROR);
Expand Down
1 change: 0 additions & 1 deletion src/platform/tests/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ if (chip_device_platform != "none" && chip_device_platform != "fake") {
#
sources = [
"TestCHIPoBLEStackMgr.cpp",
"TestCHIPoBLEStackMgr.h",
"TestCHIPoBLEStackMgrDriver.cpp",
]
tests = [ "TestCHIPoBLEStackMgr" ]
Expand Down
30 changes: 0 additions & 30 deletions src/platform/tests/TestCHIPoBLEStackMgr.h

This file was deleted.

5 changes: 1 addition & 4 deletions src/platform/tests/TestCHIPoBLEStackMgrDriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,16 @@
* limitations under the License.
*/

#include "TestCHIPoBLEStackMgr.h"
#include <gtest/gtest.h>
#include <platform/CHIPDeviceConfig.h>
#include <stdlib.h>

int main(int argc, char * argv[])
{
#if CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE
testing::InitGoogleTest(&argc, argv);
if (argc == 2 && atoi(argv[1]) == 1)
{
testing::InitGoogleTest(nullptr, nullptr);
return RUN_ALL_TESTS();
}
return 0;
#endif // CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE
}
5 changes: 3 additions & 2 deletions src/system/SystemPacketBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,8 @@ class DLL_EXPORT PacketBufferHandle
#endif
}

bool operator==(const PacketBufferHandle & aOther) const { return mBuffer == aOther.mBuffer; }

protected:
#if CHIP_SYSTEM_CONFIG_USE_LWIP
// For use via LwIPPacketBufferView only.
Expand Down Expand Up @@ -684,8 +686,7 @@ class DLL_EXPORT PacketBufferHandle
}

PacketBuffer * Get() const { return mBuffer; }

bool operator==(const PacketBufferHandle & aOther) const { return mBuffer == aOther.mBuffer; }
PacketBuffer * GetNext() const { return static_cast<PacketBuffer *>(mBuffer->next); }

#if CHIP_SYSTEM_PACKETBUFFER_HAS_RIGHTSIZE
void InternalRightSize();
Expand Down
30 changes: 15 additions & 15 deletions src/system/tests/TestSystemPacketBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -469,13 +469,13 @@ TEST_F_FROM_FIXTURE(TestSystemPacketBuffer, CheckSetDataLength)
{
EXPECT_EQ(config_2.handle->len, (config_2.end_buffer - config_2.payload_ptr));
EXPECT_EQ(config_2.handle->tot_len, (config_2.end_buffer - config_2.payload_ptr));
EXPECT_EQ(config_2.handle->next, nullptr);
EXPECT_EQ(config_2.handle.GetNext(), nullptr);
}
else
{
EXPECT_EQ(config_2.handle->len, length);
EXPECT_EQ(config_2.handle->tot_len, length);
EXPECT_EQ(config_2.handle->next, nullptr);
EXPECT_EQ(config_2.handle.GetNext(), nullptr);
}
}
else
Expand All @@ -487,7 +487,7 @@ TEST_F_FROM_FIXTURE(TestSystemPacketBuffer, CheckSetDataLength)
{
EXPECT_EQ(config_2.handle->len, (config_2.end_buffer - config_2.payload_ptr));
EXPECT_EQ(config_2.handle->tot_len, (config_2.end_buffer - config_2.payload_ptr));
EXPECT_EQ(config_2.handle->next, nullptr);
EXPECT_EQ(config_2.handle.GetNext(), nullptr);

EXPECT_EQ(config_1.handle->tot_len,
(config_1.init_len + static_cast<int32_t>(config_2.end_buffer - config_2.payload_ptr) -
Expand All @@ -497,7 +497,7 @@ TEST_F_FROM_FIXTURE(TestSystemPacketBuffer, CheckSetDataLength)
{
EXPECT_EQ(config_2.handle->len, length);
EXPECT_EQ(config_2.handle->tot_len, length);
EXPECT_EQ(config_2.handle->next, nullptr);
EXPECT_EQ(config_2.handle.GetNext(), nullptr);

EXPECT_EQ(config_1.handle->tot_len,
(config_1.init_len + static_cast<int32_t>(length) - static_cast<int32_t>(config_2.init_len)));
Expand Down Expand Up @@ -632,19 +632,19 @@ TEST_F_FROM_FIXTURE(TestSystemPacketBuffer, CheckAddToEnd)
EXPECT_EQ(config_3.handle->ref, 1); // config_3.handle

EXPECT_EQ(config_1.handle->tot_len, (config_1.init_len + config_2.init_len));
EXPECT_EQ(config_1.handle->next, config_2.handle.Get());
EXPECT_EQ(config_2.handle->next, nullptr);
EXPECT_EQ(config_3.handle->next, nullptr);
EXPECT_EQ(config_1.handle.GetNext(), config_2.handle.Get());
EXPECT_EQ(config_2.handle.GetNext(), nullptr);
EXPECT_EQ(config_3.handle.GetNext(), nullptr);

config_1.handle->AddToEnd(config_3.handle.Retain());
EXPECT_EQ(config_1.handle->ref, 1); // config_1.handle
EXPECT_EQ(config_2.handle->ref, 2); // config_2.handle and config_1.handle->next
EXPECT_EQ(config_3.handle->ref, 2); // config_3.handle and config_2.handle->next

EXPECT_EQ(config_1.handle->tot_len, (config_1.init_len + config_2.init_len + config_3.init_len));
EXPECT_EQ(config_1.handle->next, config_2.handle.Get());
EXPECT_EQ(config_2.handle->next, config_3.handle.Get());
EXPECT_EQ(config_3.handle->next, nullptr);
EXPECT_EQ(config_1.handle.GetNext(), config_2.handle.Get());
EXPECT_EQ(config_2.handle.GetNext(), config_3.handle.Get());
EXPECT_EQ(config_3.handle.GetNext(), nullptr);

config_1.handle = nullptr;
config_2.handle = nullptr;
Expand Down Expand Up @@ -703,7 +703,7 @@ TEST_F_FROM_FIXTURE(TestSystemPacketBuffer, CheckPopHead)
const PacketBufferHandle popped = config_1.handle.PopHead();

EXPECT_EQ(config_1.handle, config_2.handle);
EXPECT_EQ(config_1.handle->next, nullptr);
EXPECT_EQ(config_1.handle.GetNext(), nullptr);
EXPECT_EQ(config_1.handle->tot_len, config_1.init_len);
}
}
Expand Down Expand Up @@ -786,7 +786,7 @@ TEST_F_FROM_FIXTURE(TestSystemPacketBuffer, CheckCompactHead)
{
EXPECT_EQ(config_1.handle->len, config_1.handle->MaxDataLength());
EXPECT_EQ(buffer_2->len, config_1.handle->tot_len - config_1.handle->MaxDataLength());
EXPECT_EQ(config_1.handle->next, buffer_2);
EXPECT_EQ(config_1.handle.GetNext(), buffer_2);
EXPECT_EQ(config_1.handle->ref, 2);
EXPECT_EQ(buffer_2->ref, 1);
}
Expand All @@ -796,13 +796,13 @@ TEST_F_FROM_FIXTURE(TestSystemPacketBuffer, CheckCompactHead)
if (data_length_1 >= config_1.handle->MaxDataLength() && data_length_2 == 0)
{
/* make sure the second buffer is not freed */
EXPECT_EQ(config_1.handle->next, buffer_2);
EXPECT_EQ(config_1.handle.GetNext(), buffer_2);
EXPECT_EQ(buffer_2->ref, 1);
}
else
{
/* make sure the second buffer is freed */
EXPECT_EQ(config_1.handle->next, nullptr);
EXPECT_EQ(config_1.handle.GetNext(), nullptr);
buffer_2 = nullptr;
}
}
Expand Down Expand Up @@ -1263,7 +1263,7 @@ TEST_F_FROM_FIXTURE(TestSystemPacketBuffer, CheckFree)
// Verify that head ref count is decremented.
EXPECT_EQ(config_1.handle->ref, initial_refs_1 - 1);
// Verify that chain is maintained.
EXPECT_EQ(config_1.handle->next, config_2.handle.Get());
EXPECT_EQ(config_1.handle.GetNext(), config_2.handle.Get());
// Verify that chained buffer ref count has not changed.
EXPECT_EQ(config_2.handle->ref, initial_refs_2);
}
Expand Down

0 comments on commit a28965b

Please sign in to comment.