RDK-60610: Adding L1 unit test cases for commonlib,reportgen#254
RDK-60610: Adding L1 unit test cases for commonlib,reportgen#254rosemarybennyy wants to merge 1 commit intodevelopfrom
Conversation
c471ddb to
60cab50
Compare
There was a problem hiding this comment.
Pull request overview
Adds additional L1 unit tests for reportgen and commonlib behavior, including error/edge branches that were previously untested.
Changes:
- Added
reportgenunit tests covering realloc-failure handling inprepareHttpUrl, additional trim/regex branches inencodeParamResultInJSON, and a positive regex-match path forapplyRegexToValuevia callback. - Added
commonlibunit tests fort2_event_*cache-enabled/disabled paths and additionalgetParamValueboolean handling. - Introduced
#ifdef GTEST_ENABLEhelper hooks intelemetry_busmessage_sender.cto expose internal state and a callback to the internal event-marker population function for testing.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| source/test/reportgen/reportgenTest.cpp | Adds new reportgen unit tests, including realloc-failure injection and additional regex/trim coverage. |
| source/test/commonlib/TelemetryBusMsgSender.cpp | Adds commonlib unit tests for event sending paths and boolean parameter retrieval, plus use of new test hooks. |
| source/commonlib/telemetry_busmessage_sender.c | Adds GTEST-only exported helpers to access internal flags and the internal marker-list function for unit tests. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
60cab50 to
f7a59fb
Compare
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| CURL* curl = (CURL*) 0xffee; | ||
| char* profile = strdup("RDK_Profile"); | ||
|
|
||
| // Prepare the expectations for Curl and our mock functions | ||
| EXPECT_CALL(*m_reportgenMock, curl_easy_init()) | ||
| .Times(1) | ||
| .WillOnce(Return(curl)); | ||
| EXPECT_CALL(*m_reportgenMock, curl_easy_escape(_,_,_)) | ||
| .Times(1) | ||
| .WillOnce(Return(profile)); | ||
| EXPECT_CALL(*m_reportgenMock, curl_free(profile)) | ||
| .Times(1); | ||
| EXPECT_CALL(*m_reportgenMock, curl_easy_cleanup(curl)) | ||
| .Times(1); |
There was a problem hiding this comment.
In prepareHttpUrl_ReallocFails, profile is allocated via strdup but never freed. Since curl_free is mocked, it won’t actually free this memory unless you add an action (e.g., invoke free) or explicitly free(profile) after the call. This keeps the test leak-free under sanitizers/valgrind.
f457bb4 to
ffc75e0
Compare
Reason for change: Adding L1 unit test cases for reportgenand commonlib Test Procedure: Tested and verified Risks: Medium Priority: P1 Signed-off-by: Rose Mary Benny <RoseMary_Benny@comcast.com>
ffc75e0 to
642bdff
Compare
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 3 out of 3 changed files in this pull request and generated 5 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| TEST_F(TelemetryBusmessageSenderTest, t2_event_d_iscachingenabled_true_1) | ||
| { | ||
| t2_init((char*)"sysinit"); | ||
|
|
||
| EXPECT_CALL(*g_systemMock, access(_,_)) | ||
| .WillRepeatedly(Return(-1)); // Accept any number of calls | ||
| EXPECT_CALL(*g_rbusMock, rbus_getUint(_, _, _)) | ||
| .Times(1) | ||
| .WillOnce([](rbusHandle_t handle, const char* name, uint32_t* value) { | ||
| *value = 1; | ||
| return RBUS_ERROR_SUCCESS; // <-- Simulate SUCCESS | ||
| }); | ||
| *test_get_isRbusEnabled_ptr() = false; | ||
| *test_get_isT2Ready_ptr() = true; | ||
| int ret = t2_event_d("marker", 13); | ||
|
|
||
| *test_get_isRbusEnabled_ptr() = true; | ||
| EXPECT_EQ(ret, T2ERROR_SUCCESS); | ||
| } |
There was a problem hiding this comment.
t2_event_d_iscachingenabled_true_1 directly calls test_get_isRbusEnabled_ptr() / test_get_isT2Ready_ptr(), but those helper declarations are wrapped in #ifdef GTEST_ENABLE while this test itself is not. If GTEST_ENABLE isn’t defined in a given test build, this will fail to compile. Please wrap this test in the same #ifdef GTEST_ENABLE block (or remove the dependency on those helpers).
| return nullptr; | ||
| } | ||
| if (!real_realloc_fptr) { | ||
| real_realloc_fptr = (void*(*)(void*, size_t))dlsym(RTLD_NEXT, "realloc"); |
There was a problem hiding this comment.
The test interposes realloc() and uses dlsym(RTLD_NEXT, "realloc"), but the result is never checked for NULL. If dlsym fails (static link, unusual loader behavior, missing symbol), real_realloc_fptr stays NULL and the next call will crash when invoked. Please add a defensive check (and a clear failure path) if dlsym returns NULL, or use a safer allocation-failure injection strategy scoped to the unit under test.
| real_realloc_fptr = (void*(*)(void*, size_t))dlsym(RTLD_NEXT, "realloc"); | |
| real_realloc_fptr = (void*(*)(void*, size_t))dlsym(RTLD_NEXT, "realloc"); | |
| if (!real_realloc_fptr) { | |
| /* Failed to resolve the real realloc; abort tests to avoid | |
| * calling through a NULL function pointer. */ | |
| abort(); | |
| } |
| TEST_F(TelemetryBusmessageSenderTest, t2_event_d_iscachingenabled_false) | ||
| { | ||
| t2_init((char*)"sysint"); | ||
| EXPECT_CALL(*g_systemMock, access(_,_)) | ||
| .WillRepeatedly(Return(-1)); | ||
| EXPECT_CALL(*g_rbusMock, rbus_getUint(_, _, _)) | ||
| .Times(1) | ||
| .WillOnce([](rbusHandle_t handle, const char* name, uint32_t* value) { | ||
| *value = 0; | ||
| return RBUS_ERROR_BUS_ERROR; | ||
| }); | ||
|
|
||
| int ret; | ||
| ret = t2_event_d("marker", 13); | ||
| EXPECT_EQ(ret, T2ERROR_SUCCESS); | ||
| } |
There was a problem hiding this comment.
This test setup makes isCachingRequired() return true (because rbus_getUint returns an error), so report_or_cache_data() will spawn a background thread via pthread_create(cacheEventToFile, ...). The fixture TearDown() calls t2_uninit() which destroys mutexes and can race with that thread, making the test flaky/UB. Please avoid triggering the caching path in unit tests (e.g., return RBUS_ERROR_SUCCESS and set t2ReadyStatus to include T2_STATE_COMPONENT_READY), or stub out pthread_create/cacheEventToFile and join/disable the thread.
| TEST_F(TelemetryBusmessageSenderTest, t2_event_f_iscachingenabled_false) | ||
| { | ||
| t2_init((char*)"sysint"); | ||
| EXPECT_CALL(*g_systemMock, access(_,_)) | ||
| .WillRepeatedly(Return(-1)); | ||
| EXPECT_CALL(*g_rbusMock, rbus_getUint(_, _, _)) | ||
| .Times(1) | ||
| .WillOnce([](rbusHandle_t handle, const char* name, uint32_t* value) { | ||
| *value = 0; | ||
| return RBUS_ERROR_BUS_ERROR; | ||
| }); | ||
| int ret; | ||
| ret = t2_event_f("marker", 123.456); | ||
| EXPECT_EQ(ret, T2ERROR_SUCCESS); | ||
| } |
There was a problem hiding this comment.
This test also forces the caching path (via rbus_getUint error), which spawns the async cacheEventToFile thread. That can race with t2_uninit() in TearDown() and cause nondeterministic failures. Consider adjusting mocks so isCachingRequired() returns false, or stub/join the caching thread for deterministic unit tests.
| TEST_F(TelemetryBusmessageSenderTest, t2_event_d_iscachingenabled_true) | ||
| { | ||
| t2_init((char*)"sysint"); | ||
|
|
||
| EXPECT_CALL(*g_systemMock, access(_,_)) | ||
| .WillRepeatedly(Return(-1)); // Accept any number of calls | ||
|
|
||
| EXPECT_CALL(*g_rbusMock, rbus_getUint(_, _, _)) | ||
| .Times(1) | ||
| .WillOnce([](rbusHandle_t handle, const char* name, uint32_t* value) { | ||
| *value = 0; | ||
| return RBUS_ERROR_SUCCESS; // <-- Simulate SUCCESS | ||
| }); | ||
|
|
||
| int ret = t2_event_d("marker", 13); | ||
| EXPECT_EQ(ret, T2ERROR_SUCCESS); | ||
| } |
There was a problem hiding this comment.
Here rbus_getUint returns success but sets *value = 0, which makes (t2ReadyStatus & T2_STATE_COMPONENT_READY) == 0 and therefore triggers caching + pthread_create(cacheEventToFile, ...). As written, this can race with t2_uninit() in TearDown() (mutex destruction while the caching thread runs). To keep the test deterministic, set *value to include T2_STATE_COMPONENT_READY (e.g., 1) and/or stub out the caching thread.
Reason for change: Adding L1 unit test cases for reportgenand commonlib
Test Procedure: Tested and verified
Risks: Medium
Priority: P1