From b17ffaa68ae6a899b1d31e3a59834fcd514260a7 Mon Sep 17 00:00:00 2001 From: Jaeden Amero Date: Wed, 21 Jul 2021 17:29:09 +0100 Subject: [PATCH 1/8] cmake: Remove trailing whitespace Remove trailing whitespace from lorawantimer's CMakeLists.txt. --- .../UNITTESTS/features/lorawan/lorawantimer/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/connectivity/lorawan/tests/UNITTESTS/features/lorawan/lorawantimer/CMakeLists.txt b/connectivity/lorawan/tests/UNITTESTS/features/lorawan/lorawantimer/CMakeLists.txt index dea9f687954..b0bd2252bb8 100644 --- a/connectivity/lorawan/tests/UNITTESTS/features/lorawan/lorawantimer/CMakeLists.txt +++ b/connectivity/lorawan/tests/UNITTESTS/features/lorawan/lorawantimer/CMakeLists.txt @@ -7,7 +7,7 @@ add_executable(${TEST_NAME}) target_compile_definitions(${TEST_NAME} PRIVATE - NDEBUG=1 + NDEBUG=1 MBED_CONF_LORA_TX_MAX_SIZE=255 ) From a1e61b61c2251e8ea42a008b7e3cc6f37a6605d6 Mon Sep 17 00:00:00 2001 From: Jaeden Amero Date: Thu, 22 Jul 2021 14:11:08 +0100 Subject: [PATCH 2/8] equeue: Add missing header guards The header equeue_stub.h was missing header guards to enforce single inclusion. Add some header guards. --- events/tests/UNITTESTS/doubles/equeue_stub.h | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/events/tests/UNITTESTS/doubles/equeue_stub.h b/events/tests/UNITTESTS/doubles/equeue_stub.h index 648d920058a..fcdddf9e53d 100644 --- a/events/tests/UNITTESTS/doubles/equeue_stub.h +++ b/events/tests/UNITTESTS/doubles/equeue_stub.h @@ -15,6 +15,9 @@ * limitations under the License. */ +#ifndef __EQUEUE_STUB_H__ +#define __EQUEUE_STUB_H__ + #include "stdint.h" #include "stdbool.h" @@ -24,3 +27,5 @@ typedef struct { } equeue_stub_def; extern equeue_stub_def equeue_stub; + +#endif From 4480abeaf9e8762b3bd57585796791298b92d182 Mon Sep 17 00:00:00 2001 From: Jaeden Amero Date: Thu, 22 Jul 2021 11:51:28 +0100 Subject: [PATCH 3/8] cmake: Avoid linking to gcov Use the `--coverage` option instead of manually linking to gcov, as some host platforms (like macOS, FreeBSD) don't have gcov by default and use an llvm equivalent instead. --- .../tests/UNITTESTS/doubles/fakes/CMakeLists.txt | 6 +++++- events/tests/UNITTESTS/doubles/fakes/CMakeLists.txt | 6 +++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/connectivity/FEATURE_BLE/tests/UNITTESTS/doubles/fakes/CMakeLists.txt b/connectivity/FEATURE_BLE/tests/UNITTESTS/doubles/fakes/CMakeLists.txt index 75ae98d64af..aabae2f3f36 100644 --- a/connectivity/FEATURE_BLE/tests/UNITTESTS/doubles/fakes/CMakeLists.txt +++ b/connectivity/FEATURE_BLE/tests/UNITTESTS/doubles/fakes/CMakeLists.txt @@ -33,10 +33,14 @@ target_sources(mbed-fakes-ble ${mbed-os_SOURCE_DIR}/connectivity/FEATURE_BLE/tests/UNITTESTS/doubles/fakes/SecurityManagerImpl_mock.h ) +target_link_options(mbed-fakes-ble + PRIVATE + --coverage +) + target_link_libraries(mbed-fakes-ble PRIVATE mbed-headers-base mbed-headers-platform mbed-headers-events - gcov ) diff --git a/events/tests/UNITTESTS/doubles/fakes/CMakeLists.txt b/events/tests/UNITTESTS/doubles/fakes/CMakeLists.txt index 1baacb11247..65bf2ced5dc 100644 --- a/events/tests/UNITTESTS/doubles/fakes/CMakeLists.txt +++ b/events/tests/UNITTESTS/doubles/fakes/CMakeLists.txt @@ -13,8 +13,12 @@ target_include_directories(mbed-fakes-event-queue . ) +target_link_options(mbed-fakes-event-queue + PRIVATE + --coverage +) + target_link_libraries(mbed-fakes-event-queue PRIVATE mbed-headers - gcov ) From 162ae220b18ff32ee631202567ca85eb69823372 Mon Sep 17 00:00:00 2001 From: Jaeden Amero Date: Thu, 22 Jul 2021 14:00:14 +0100 Subject: [PATCH 4/8] equeue: Enable use of stub from C++ Add extern "C" to the equeue_stub declaration to avoid an error when a C file implements the equeue_stub symbol (as we do in equeue_stub.c). Undefined symbols for architecture x86_64: "_equeue_stub", referenced from: Test_LoRaWANTimer_start_Test::TestBody() in Test_LoRaWANTimer.cpp.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation) --- events/tests/UNITTESTS/doubles/equeue_stub.h | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/events/tests/UNITTESTS/doubles/equeue_stub.h b/events/tests/UNITTESTS/doubles/equeue_stub.h index fcdddf9e53d..168b3931980 100644 --- a/events/tests/UNITTESTS/doubles/equeue_stub.h +++ b/events/tests/UNITTESTS/doubles/equeue_stub.h @@ -21,6 +21,10 @@ #include "stdint.h" #include "stdbool.h" +#ifdef __cplusplus +extern "C" { +#endif + typedef struct { void *void_ptr; bool call_cb_immediately; @@ -28,4 +32,8 @@ typedef struct { extern equeue_stub_def equeue_stub; +#ifdef __cplusplus +} +#endif + #endif From 07aefbc6bcef88ab38d6462c9ff3b8f0935a9a2a Mon Sep 17 00:00:00 2001 From: Robert Walton Date: Mon, 26 Jul 2021 23:28:12 +0100 Subject: [PATCH 5/8] unittests: Test_LoRaWANTimer.start: Fix test failure We weren't initialising the mocks correctly, so an MBED_ASSERT failed in `LoRaWANTimer::start` because the "timer_id" was not set. --- .../features/lorawan/lorawantimer/Test_LoRaWANTimer.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/connectivity/lorawan/tests/UNITTESTS/features/lorawan/lorawantimer/Test_LoRaWANTimer.cpp b/connectivity/lorawan/tests/UNITTESTS/features/lorawan/lorawantimer/Test_LoRaWANTimer.cpp index 9fb10a39854..fd0ab985c86 100644 --- a/connectivity/lorawan/tests/UNITTESTS/features/lorawan/lorawantimer/Test_LoRaWANTimer.cpp +++ b/connectivity/lorawan/tests/UNITTESTS/features/lorawan/lorawantimer/Test_LoRaWANTimer.cpp @@ -71,9 +71,12 @@ TEST_F(Test_LoRaWANTimer, init) TEST_F(Test_LoRaWANTimer, start) { - equeue_stub.void_ptr = NULL; + struct equeue_event ptr; + equeue_stub.void_ptr = &ptr; timer_event_t ev; memset(&ev, 0, sizeof(ev)); + object->init(ev, my_callback); + equeue_stub.call_cb_immediately = true; object->start(ev, 10); } From 0a944f0ac499f206f0c53a9b9fadb36937192477 Mon Sep 17 00:00:00 2001 From: Jaeden Amero Date: Thu, 22 Jul 2021 17:54:54 +0100 Subject: [PATCH 6/8] test: lorawanstack: Use valid message type We previously made up an invalid message type, which was roughly `MCPS_MULTICAST | 0x40`. This causes an MBED_ASSERT failure in the handle_rx function, as it uses an assert to validate the message type passed to it (in convert_to_msg_flag()). Use the valid message type MCPS_MULTICAST instead in the handle_rx unit test. --- .../features/lorawan/lorawanstack/Test_LoRaWANStack.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/connectivity/lorawan/tests/UNITTESTS/features/lorawan/lorawanstack/Test_LoRaWANStack.cpp b/connectivity/lorawan/tests/UNITTESTS/features/lorawan/lorawanstack/Test_LoRaWANStack.cpp index 13aca435fb1..4903dd75b58 100644 --- a/connectivity/lorawan/tests/UNITTESTS/features/lorawan/lorawanstack/Test_LoRaWANStack.cpp +++ b/connectivity/lorawan/tests/UNITTESTS/features/lorawan/lorawanstack/Test_LoRaWANStack.cpp @@ -554,7 +554,7 @@ TEST_F(Test_LoRaWANStack, handle_rx) } ind.buffer = ind_buf; ind.buffer_size = 50; - ind.type = mcps_type_t(66); + ind.type = MCPS_MULTICAST; radio._ev->rx_done(NULL, 0, 0, 0); EXPECT_TRUE(50 == object->handle_rx(data, 50, port, flags, false)); EXPECT_EQ(10, data[10]); From f0d7e2406676133d6138ed17aebbbcb0c0e0f1c9 Mon Sep 17 00:00:00 2001 From: Jaeden Amero Date: Thu, 22 Jul 2021 17:20:54 +0100 Subject: [PATCH 7/8] test: CircularBuffer: Don't push zero elements The CircularBuffer doesn't allow pushing zero elements; you must push at least one. Update the CircularBuffer unit test to avoid invalid use of the CircularBuffer. --- platform/tests/UNITTESTS/CircularBuffer/test_CircularBuffer.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/platform/tests/UNITTESTS/CircularBuffer/test_CircularBuffer.cpp b/platform/tests/UNITTESTS/CircularBuffer/test_CircularBuffer.cpp index 9d45a5404af..26f0296480b 100644 --- a/platform/tests/UNITTESTS/CircularBuffer/test_CircularBuffer.cpp +++ b/platform/tests/UNITTESTS/CircularBuffer/test_CircularBuffer.cpp @@ -69,7 +69,7 @@ TEST_F(TestCircularBuffer, push_pop_multiple) const int test_numbers[TEST_BUFFER_SIZE] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; /* this will check pushing across the buffer end */ - for (int i = 0; i < TEST_BUFFER_SIZE; i++) { + for (int i = 1; i < TEST_BUFFER_SIZE; i++) { int test_numbers_popped[TEST_BUFFER_SIZE] = { 0 }; buf->push(test_numbers, i); EXPECT_EQ(buf->size(), i); From 51b81e0629344ca9169efcabbc1edf9fc1ee4d76 Mon Sep 17 00:00:00 2001 From: Jaeden Amero Date: Wed, 21 Jul 2021 16:26:46 +0100 Subject: [PATCH 8/8] test: Make MBED_ASSERT failures fail Make MBED_ASSERT failures fail the unit test case. Without this, we might not notice the assertion failure as it wouldn't be bubbled up to googletest. --- platform/tests/UNITTESTS/doubles/CMakeLists.txt | 1 + platform/tests/UNITTESTS/doubles/mbed_assert_stub.cpp | 7 +++++++ 2 files changed, 8 insertions(+) diff --git a/platform/tests/UNITTESTS/doubles/CMakeLists.txt b/platform/tests/UNITTESTS/doubles/CMakeLists.txt index ac94aabebad..31710967ba3 100644 --- a/platform/tests/UNITTESTS/doubles/CMakeLists.txt +++ b/platform/tests/UNITTESTS/doubles/CMakeLists.txt @@ -35,4 +35,5 @@ target_link_libraries(mbed-stubs-platform mbed-headers-base mbed-headers-hal mbed-headers-platform + gmock_main ) diff --git a/platform/tests/UNITTESTS/doubles/mbed_assert_stub.cpp b/platform/tests/UNITTESTS/doubles/mbed_assert_stub.cpp index 875ef8792d0..baea5f221cd 100644 --- a/platform/tests/UNITTESTS/doubles/mbed_assert_stub.cpp +++ b/platform/tests/UNITTESTS/doubles/mbed_assert_stub.cpp @@ -16,6 +16,7 @@ */ #include "platform/mbed_assert.h" +#include "gtest/gtest.h" #include #include @@ -27,4 +28,10 @@ extern "C" void mbed_assert_internal(const char *expr, const char *file, int lin if (mbed_assert_throw_errors) { throw 1; } + + /* Ensure we fail the unit test if the Mbed assertion fails. Without this, + * we might not notice the assertion failure as it wouldn't be bubbled up + * to googletest. Note that this is after the above throw, as some tests + * check that an exception is thrown (i.e. negative tests). */ + FAIL(); }