Skip to content

Commit 2105a79

Browse files
RDK-60533: L1 unit test cases - protocol/rbusMethod and http (#241)
* RDK-60519: L1 unit test cases - dcautil,ccspinterface,protocol/rbusMethod Reason for change: Adding L1 unit test cases for dcautil and ccspinterface, protocol/rbsuMethod Test Procedure: Tested and verified Risks: Medium Priority: P1 Signed-off-by: Rose Mary Benny <RoseMary_Benny@comcast.com> * resolved copilot review comments * RDK-60533: L1 unit test cases - protocol/rbusMethod and http Reason for change: Adding L1 unit test cases for protocol/rbsuMethod and http Test Procedure: Tested and verified Risks: Medium Priority: P1 Signed-off-by: Rose Mary Benny <RoseMary_Benny@comcast.com> --------- Signed-off-by: Rose Mary Benny <RoseMary_Benny@comcast.com> Co-authored-by: Shibu Kakkoth Vayalambron <shibu.kakkoth@gmail.com>
1 parent f566d39 commit 2105a79

File tree

3 files changed

+127
-0
lines changed

3 files changed

+127
-0
lines changed

source/protocol/http/curlinterface.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,15 @@ typedef size_t (*WriteToFileFunc)(void *, size_t, size_t, void *);
639639
typedef T2ERROR (*SetHeaderFunc)(CURL *, const char *, struct curl_slist **, childResponse *);
640640
typedef T2ERROR (*SetMtlsHeadersFunc)(CURL *, const char *, const char *, childResponse *);
641641
typedef T2ERROR (*SetPayloadFunc)(CURL *, const char *, childResponse *);
642+
pthread_mutex_t* getCurlFileMutex(void)
643+
{
644+
return &curlFileMutex;
645+
}
646+
typedef void (*sendOverHTTPInitFunc)();
647+
sendOverHTTPInitFunc sendOverHTTPInitFuncCallback()
648+
{
649+
return sendOverHTTPInit;
650+
}
642651
WriteToFileFunc getWriteToFileCallback()
643652
{
644653
return writeToFile;

source/protocol/rbusMethod/rbusmethodinterface.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,3 +175,16 @@ T2ERROR sendCachedReportsOverRBUSMethod(char *methodName, Vector* inputParams, V
175175
T2Debug("%s --out\n", __FUNCTION__);
176176
return T2ERROR_SUCCESS;
177177
}
178+
179+
#ifdef GTEST_ENABLE
180+
pthread_mutex_t* getRbusMethodMutex(void)
181+
{
182+
return &rbusMethodMutex;
183+
}
184+
185+
typedef void (*asyncMethodHandlerFunc)(rbusHandle_t, char const*, rbusError_t, rbusObject_t);
186+
asyncMethodHandlerFunc asyncMethodHandlerFuncCallback(void)
187+
{
188+
return asyncMethodHandler;
189+
}
190+
#endif

source/test/protocol/ProtocolTest.cpp

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,29 @@ TEST_F(protocolTestFixture, CURLINTERFACE_STATIC_SetHeader_setopt_failure)
626626
EXPECT_EQ(resp.curlSetopCode, CURLE_FAILED_INIT);
627627
}
628628

629+
TEST_F(protocolTestFixture, CURLINTERFACE_STATIC_SetHeader_FirstSetopt_failure_lineNumber)
630+
{
631+
SetHeaderFunc setHeaderCb = getSetHeaderCallback();
632+
ASSERT_NE(setHeaderCb, nullptr);
633+
634+
CURL *curl = (CURL*)0x1;
635+
const char *destURL = "http://localhost";
636+
struct curl_slist *headerList = nullptr;
637+
childResponse resp;
638+
memset(&resp, 0, sizeof(resp));
639+
640+
// The very first curl_easy_setopt_mock call returns CURLE_FAILED_INIT.
641+
EXPECT_CALL(*g_fileIOMock, curl_easy_setopt_mock(_,_,_))
642+
.Times(1)
643+
.WillOnce(Return(CURLE_FAILED_INIT));
644+
645+
T2ERROR code = setHeaderCb(curl, destURL, &headerList, &resp);
646+
EXPECT_EQ(code, T2ERROR_FAILURE);
647+
EXPECT_EQ(resp.curlSetopCode, CURLE_FAILED_INIT);
648+
// Assert that lineNumber field gets set. (Should match the __LINE__ where the macro is expanded; we test it's nonzero.)
649+
EXPECT_NE(resp.lineNumber, 0);
650+
}
651+
629652
TEST_F(protocolTestFixture, CURLINTERFACE_STATIC_SetMtlsHeaders_setopt_failure)
630653
{
631654
SetMtlsHeadersFunc cb = getSetMtlsHeadersCallback();
@@ -764,6 +787,40 @@ TEST_F(protocolTestFixture, CURLINTERFACE_STATIC_SetHeader_HTTPHEADER_failure)
764787
EXPECT_EQ(resp.curlSetopCode, CURLE_COULDNT_CONNECT);
765788
}
766789

790+
TEST_F(protocolTestFixture, CURLINTERFACE_STATIC_SetHeader_HTTPHEADER_failure_block)
791+
{
792+
SetHeaderFunc setHeaderCb = getSetHeaderCallback();
793+
ASSERT_NE(setHeaderCb, nullptr);
794+
795+
CURL *curl = (CURL*)0x1;
796+
const char *destURL = "http://localhost";
797+
struct curl_slist *headerList = nullptr;
798+
childResponse resp;
799+
memset(&resp, 0, sizeof(resp));
800+
801+
// Simulate curl_slist_append returns
802+
EXPECT_CALL(*g_fileIOMock, curl_slist_append(_, _))
803+
.Times(2)
804+
.WillRepeatedly(Return((struct curl_slist*)0x1));
805+
806+
// Mock all prior curl_easy_setopt calls to return OK, only HTTPHEADER fails
807+
::testing::Sequence s;
808+
EXPECT_CALL(*g_fileIOMock, curl_easy_setopt_mock(_, ::testing::Ne(CURLOPT_HTTPHEADER), _))
809+
.Times(::testing::AtLeast(1))
810+
.InSequence(s)
811+
.WillRepeatedly(Return(CURLE_OK));
812+
EXPECT_CALL(*g_fileIOMock, curl_easy_setopt_mock(_, CURLOPT_HTTPHEADER, _))
813+
.InSequence(s)
814+
.WillOnce(Return(CURLE_COULDNT_CONNECT)); // Simulate failure at HTTPHEADER
815+
816+
T2ERROR result = setHeaderCb(curl, destURL, &headerList, &resp);
817+
818+
EXPECT_EQ(result, T2ERROR_FAILURE);
819+
EXPECT_EQ(resp.curlSetopCode, CURLE_COULDNT_CONNECT);
820+
// Should be set to a non-zero line number corresponding to line 166 in your source
821+
EXPECT_NE(resp.lineNumber, 0);
822+
}
823+
767824
TEST_F(protocolTestFixture, CURLINTERFACE_STATIC_SetHeader_WRITEFUNCTION_failure)
768825
{
769826
SetHeaderFunc setHeaderCb = getSetHeaderCallback();
@@ -958,3 +1015,51 @@ TEST_F(protocolTestFixture, CURLINTERFACE_STATIC_SetHeader_SUCCESS)
9581015
EXPECT_NE(headerList, nullptr);
9591016
}
9601017
#endif
1018+
1019+
#ifdef GTEST_ENABLE
1020+
extern "C" {
1021+
pthread_mutex_t* getRbusMethodMutex(void);
1022+
pthread_mutex_t* getCurlFileMutex(void);
1023+
typedef void (*sendOverHTTPInitFunc)();
1024+
sendOverHTTPInitFunc sendOverHTTPInitFuncCallback();
1025+
1026+
typedef void (*asyncMethodHandlerFunc)(rbusHandle_t,char const*,rbusError_t,rbusObject_t);
1027+
asyncMethodHandlerFunc asyncMethodHandlerFuncCallback(void);
1028+
}
1029+
1030+
TEST(CurlInterface_Static, SendOverHTTPInit_CoversMutexInit)
1031+
{
1032+
// Get function pointer to static sendOverHTTPInit
1033+
auto fn = sendOverHTTPInitFuncCallback();
1034+
ASSERT_NE(fn, nullptr);
1035+
fn();
1036+
// Check that the mutex is initialized (trylock succeeds or returns expected error if already locked)
1037+
pthread_mutex_t *pmutex = getCurlFileMutex();
1038+
int trylock_result = pthread_mutex_trylock(pmutex);
1039+
// Accept either success (0) or busy (EBUSY) as initialized.
1040+
EXPECT_TRUE(trylock_result == 0 || trylock_result == EBUSY);
1041+
1042+
if (trylock_result == 0) { // If locked, unlock for cleanup
1043+
pthread_mutex_unlock(pmutex);
1044+
}
1045+
1046+
pthread_mutex_destroy(pmutex);
1047+
}
1048+
TEST(AsyncMethodHandlerFunc, CoversAllBranches)
1049+
{
1050+
// Access and initialize the mutex defined in the C module
1051+
pthread_mutex_t* pmutex = getRbusMethodMutex();
1052+
pthread_mutex_init(pmutex, NULL);
1053+
1054+
// Get function pointer for static asyncMethodHandler
1055+
auto fn = asyncMethodHandlerFuncCallback();
1056+
ASSERT_NE(fn, nullptr);
1057+
1058+
// Success branch: sets isRbusMethod = true and unlocks
1059+
fn(NULL, "TestMethodSuccess", RBUS_ERROR_SUCCESS, NULL);
1060+
// Error branch: sets isRbusMethod = false and unlocks
1061+
fn(NULL, "TestMethodFail", RBUS_ERROR_BUS_ERROR, NULL);
1062+
1063+
pthread_mutex_destroy(pmutex);
1064+
}
1065+
#endif

0 commit comments

Comments
 (0)