Allow building host tests with code coverage (#33141)

* 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 2215235dbef9890fa3252ccd200c1aff87f951b8.
diff --git a/scripts/build/build/target.py b/scripts/build/build/target.py
index 8411ff9..7f17161 100644
--- a/scripts/build/build/target.py
+++ b/scripts/build/build/target.py
@@ -406,7 +406,7 @@
         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
diff --git a/scripts/build/build/targets.py b/scripts/build/build/targets.py
index 3f5fc57..9eb2ebf 100755
--- a/scripts/build/build/targets.py
+++ b/scripts/build/build/targets.py
@@ -177,7 +177,7 @@
     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)
diff --git a/scripts/build/builders/host.py b/scripts/build/builders/host.py
index fcb609c..c1c81db 100644
--- a/scripts/build/builders/host.py
+++ b/scripts/build/builders/host.py
@@ -512,19 +512,21 @@
     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'),
diff --git a/src/access/tests/TestAccessControl.cpp b/src/access/tests/TestAccessControl.cpp
index 2d25a7c..16f3028 100644
--- a/src/access/tests/TestAccessControl.cpp
+++ b/src/access/tests/TestAccessControl.cpp
@@ -23,7 +23,8 @@
 
 #include <gtest/gtest.h>
 
-namespace {
+namespace chip {
+namespace Access {
 
 using namespace chip;
 using namespace chip::Access;
@@ -2189,4 +2190,5 @@
     }
 }
 
-} // namespace
+} // namespace Access
+} // namespace chip
diff --git a/src/lib/core/tests/TestTLV.cpp b/src/lib/core/tests/TestTLV.cpp
index 4b391c4..19b4c77 100644
--- a/src/lib/core/tests/TestTLV.cpp
+++ b/src/lib/core/tests/TestTLV.cpp
@@ -859,8 +859,7 @@
         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);
diff --git a/src/platform/tests/BUILD.gn b/src/platform/tests/BUILD.gn
index 11e8bd5..bf4c2ee 100644
--- a/src/platform/tests/BUILD.gn
+++ b/src/platform/tests/BUILD.gn
@@ -78,7 +78,6 @@
       #
       sources = [
         "TestCHIPoBLEStackMgr.cpp",
-        "TestCHIPoBLEStackMgr.h",
         "TestCHIPoBLEStackMgrDriver.cpp",
       ]
       tests = [ "TestCHIPoBLEStackMgr" ]
diff --git a/src/platform/tests/TestCHIPoBLEStackMgr.h b/src/platform/tests/TestCHIPoBLEStackMgr.h
deleted file mode 100644
index b0f4f59..0000000
--- a/src/platform/tests/TestCHIPoBLEStackMgr.h
+++ /dev/null
@@ -1,30 +0,0 @@
-/*
- *
- *    Copyright (c) 2020 Project CHIP Authors
- *
- *    Licensed under the Apache License, Version 2.0 (the "License");
- *    you may not use this file except in compliance with the License.
- *    You may obtain a copy of the License at
- *
- *        http://www.apache.org/licenses/LICENSE-2.0
- *
- *    Unless required by applicable law or agreed to in writing, software
- *    distributed under the License is distributed on an "AS IS" BASIS,
- *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- *    See the License for the specific language governing permissions and
- *    limitations under the License.
- */
-
-/**
- *    @file
- *      This file declares test entry point for CHIP over BLE code unit tests in Linux
- *
- */
-
-#pragma once
-
-#include <platform/CHIPDeviceConfig.h>
-
-#if CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE
-int TestCHIPoBLEStackManager();
-#endif // CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE
diff --git a/src/platform/tests/TestCHIPoBLEStackMgrDriver.cpp b/src/platform/tests/TestCHIPoBLEStackMgrDriver.cpp
index 2635ebe..38e3f23 100644
--- a/src/platform/tests/TestCHIPoBLEStackMgrDriver.cpp
+++ b/src/platform/tests/TestCHIPoBLEStackMgrDriver.cpp
@@ -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
 }
diff --git a/src/system/SystemPacketBuffer.h b/src/system/SystemPacketBuffer.h
index 513f0b4..9287437 100644
--- a/src/system/SystemPacketBuffer.h
+++ b/src/system/SystemPacketBuffer.h
@@ -657,6 +657,8 @@
 #endif
     }
 
+    bool operator==(const PacketBufferHandle & aOther) const { return mBuffer == aOther.mBuffer; }
+
 protected:
 #if CHIP_SYSTEM_CONFIG_USE_LWIP
     // For use via LwIPPacketBufferView only.
@@ -684,8 +686,7 @@
     }
 
     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();
diff --git a/src/system/tests/TestSystemPacketBuffer.cpp b/src/system/tests/TestSystemPacketBuffer.cpp
index 5b3f709..5ee708f 100644
--- a/src/system/tests/TestSystemPacketBuffer.cpp
+++ b/src/system/tests/TestSystemPacketBuffer.cpp
@@ -469,13 +469,13 @@
                     {
                         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
@@ -487,7 +487,7 @@
                     {
                         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) -
@@ -497,7 +497,7 @@
                     {
                         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)));
@@ -632,9 +632,9 @@
                 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
@@ -642,9 +642,9 @@
                 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;
@@ -703,7 +703,7 @@
             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);
         }
     }
@@ -786,7 +786,7 @@
                     {
                         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);
                     }
@@ -796,13 +796,13 @@
                         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;
                         }
                     }
@@ -1263,7 +1263,7 @@
                     // 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);
                 }