Skip to content

Commit fe1bf4b

Browse files
new test cases for reportprofiles
1 parent 4f58a73 commit fe1bf4b

12 files changed

Lines changed: 243 additions & 17 deletions

File tree

.github/workflows/code-coverage.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
name: Code Coverage
22

33
on:
4+
push:
5+
branches: [topic/*]
46
pull_request:
57
branches: [ main]
68

source/bulkdata/profile.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@
4545

4646
#define MAX_LEN 256
4747

48+
#ifdef GTEST_ENABLE
49+
#define sendReportOverHTTP __wrap_sendReportOverHTTP
50+
#define sendCachedReportsOverHTTP __wrap_sendCachedReportsOverHTTP
51+
#endif
52+
4853
static bool initialized = false;
4954
static Vector *profileList;
5055
static pthread_mutex_t plMutex;

source/bulkdata/profilexconf.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -986,4 +986,8 @@ T2ERROR ProfileXConf_storeMarkerEvent(T2Event *eventInfo)
986986
T2Debug("%s --out\n", __FUNCTION__);
987987
return T2ERROR_SUCCESS;
988988
}
989-
989+
#ifdef GTEST_ENABLE
990+
void test_set_reportThreadExits(bool value) {
991+
reportThreadExits = value;
992+
}
993+
#endif

source/bulkdata/reportprofiles.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1514,3 +1514,21 @@ bool isMtlsEnabled(void)
15141514
#endif
15151515
#endif
15161516
}
1517+
1518+
#ifdef GTEST_ENABLE
1519+
typedef void* (*reportOnDemandFunc)(void*);
1520+
reportOnDemandFunc reportOnDemandFuncCallback(void)
1521+
{
1522+
return reportOnDemand;
1523+
}
1524+
typedef void (*freeProfilesHashMapFunc)(void *);
1525+
freeProfilesHashMapFunc freeProfilesHashMapFuncCallback(void)
1526+
{
1527+
return freeProfilesHashMap;
1528+
}
1529+
typedef void (*freeReportProfileHashMapFunc)(void *);
1530+
freeReportProfileHashMapFunc freeReportProfileHashMapFuncCallback(void)
1531+
{
1532+
return freeReportProfileHashMap;
1533+
}
1534+
#endif

source/ccspinterface/rbusInterface.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1954,9 +1954,11 @@ bool rbusCheckMethodExists(const char* rbusMethodName)
19541954
return true ;
19551955
}
19561956
#ifdef GTEST_ENABLE
1957+
#ifdef DCMAGENT
19571958
typedef void (*rbusReloadConfFunc)(rbusHandle_t, rbusEvent_t const *, rbusEventSubscription_t *);
19581959
rbusReloadConfFunc rbusReloadConfFuncCallback(void)
19591960
{
19601961
return rbusReloadConf;
19611962
}
19621963
#endif
1964+
#endif

source/test/bulkdata/Makefile.am

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,19 +53,23 @@ profilexconf_gtest_bin_LDFLAGS += -Wl,--wrap=sendReportOverHTTP -Wl,--wrap=sendC
5353

5454
profilexconf_gtest_bin_SOURCES = = gtest_main.cpp profilexconfTest.cpp profilexconfMock.cpp SchedulerMock.cpp ../mocks/SystemMock.cpp ../mocks/FileioMock.cpp ../mocks/rdklogMock.cpp ../mocks/rbusMock.cpp ../../bulkdata/profilexconf.c ../../utils/vector.c ../../utils/t2log_wrapper.c ../../utils/t2common.c ../../utils/t2collection.c ../../utils/persistence.c ../../bulkdata/t2markers.c ../../reportgen/reportgen.c ../../dcautil/legacyutils.c
5555

56+
profile_gtest_bin_CFLAGS = -DGTEST_ENABLE
57+
5658
profile_gtest_bin_CPPFLAGS = -I$(PKG_CONFIG_SYSROOT_DIR)/usr/include/gtest -I$(PKG_CONFIG_SYSROOT_DIR)/usr/include/glib-2.0 -I$(PKG_CONFIG_SYSROOT_DIR)/usr/lib/x86_64-linux-gnu/glib-2.0/include -I$(PKG_CONFIG_SYSROOT_DIR)/usr/local/lib -I$(PKG_CONFIG_SYSROOT_DIR)$(includedir)/gtest -I${top_srcdir}/gtest/include -I${top_srcdir}/source/include -I${top_srcdir}/source -I${top_srcdir}/source/test/mocks -I${top_srcdir}/source/test/rbus -I${top_srcdir}/source/test/rdk_logger -I${top_srcdir}/include -I${top_srcdir}/source/utils -I${top_srcdir}/source/privacycontrol -I${PKG_CONFIG_SYSROOT_DIR}$(includedir)/rbus -I${top_srcdir}/source/dcautil -I${top_srcdir}/source/ccspinterface -I${top_srcdir}/source/reportgen -I${top_srcdir}/source/xconf-client -I${top_srcdir}/source/protocol/http -I${top_srcdir}/source/protocol/rbusMethod -I${top_srcdir}/source/t2parser -I${top_srcdir}/source/bulkdata -I${top_srcdir}/source/scheduler -I${top_srcdir}/source/ccspinterface -I${PKG_CONFIG_SYSROOT_DIR}$(includedir) -I${PKG_CONFIG_SYSROOT_DIR}/usr/src/googletest/googlemock/include -I${RDK_PROJECT_ROOT_PATH}/$(GLIB_CFLAGS) -I${PKG_CONFIG_SYSROOT_DIR}$(includedir)/glib-2.0 -I${PKG_CONFIG_SYSROOT_DIR}$(libdir)/glib-2.0/include
5759

58-
profile_gtest_bin_SOURCES = gtest_main.cpp ../mocks/SystemMock.cpp ../mocks/FileioMock.cpp ../mocks/rdklogMock.cpp ../mocks/rbusMock.cpp ../mocks/rdkconfigMock.cpp ../mocks/VectorMock.cpp SchedulerMock.cpp ../../bulkdata/profile.c profileTest.cpp ../../utils/persistence.c ../../utils/t2common.c ../../utils/t2collection.c ../../utils/t2MtlsUtils.c ../../utils/t2log_wrapper.c ../../dcautil/dcautil.c ../../dcautil/dca.c ../../dcautil/legacyutils.c ../../dcautil/dcaproc.c ../../xconf-client/xconfclient.c ../../protocol/rbusMethod/rbusmethodinterface.c ../../privacycontrol/rdkservices_privacyutils.c ../../reportgen/reportgen.c ../../bulkdata/t2eventreceiver.c ../../bulkdata/t2markers.c ../../t2parser/t2parser.c ../../bulkdata/datamodel.c ../../t2parser/t2parserxconf.c ../../bulkdata/reportprofiles.c ../../bulkdata/profilexconf.c ../../ccspinterface/rbusInterface.c ../../ccspinterface/busInterface.c ../../protocol/http/curlinterface.c ../../protocol/http/multicurlinterface.c
60+
profile_gtest_bin_SOURCES = gtest_main.cpp ../mocks/SystemMock.cpp ../mocks/FileioMock.cpp ../mocks/rdklogMock.cpp ../mocks/rbusMock.cpp ../mocks/rdkconfigMock.cpp ../mocks/VectorMock.cpp SchedulerMock.cpp profileMock.cpp ../../bulkdata/profile.c profileTest.cpp ../../utils/persistence.c ../../utils/t2common.c ../../utils/t2collection.c ../../utils/t2MtlsUtils.c ../../utils/t2log_wrapper.c ../../dcautil/dcautil.c ../../dcautil/dca.c ../../dcautil/legacyutils.c ../../dcautil/dcaproc.c ../../xconf-client/xconfclient.c ../../protocol/rbusMethod/rbusmethodinterface.c ../../privacycontrol/rdkservices_privacyutils.c ../../reportgen/reportgen.c ../../bulkdata/t2eventreceiver.c ../../bulkdata/t2markers.c ../../t2parser/t2parser.c ../../bulkdata/datamodel.c ../../t2parser/t2parserxconf.c ../../bulkdata/reportprofiles.c ../../bulkdata/profilexconf.c ../../ccspinterface/rbusInterface.c ../../ccspinterface/busInterface.c ../../protocol/http/curlinterface.c ../../protocol/http/multicurlinterface.c
5961

6062
profile_gtest_bin_LDFLAGS = -L/usr/src/googletest/googletest/lib/.libs -lgcov -L/src/googletest/googlemock/lib -L/usr/src/googletest/googlemock/lib/.libs -L/usr/include/glib-2.0 -lgmock -lcjson -lcurl -lmsgpackc -lgtest -lgtest_main -lglib-2.0
6163

62-
# Test binary for reportprofiles.c
64+
profile_gtest_bin_LDFLAGS += -Wl,--wrap=sendReportOverHTTP -Wl,--wrap=sendCachedReportsOverHTTP
65+
66+
reportprofiles_gtest_bin_CFLAGS = -DGTEST_ENABLE
6367

6468
reportprofiles_gtest_bin_CPPFLAGS = -I$(PKG_CONFIG_SYSROOT_DIR)/usr/include/gtest -I$(PKG_CONFIG_SYSROOT_DIR)/usr/include/glib-2.0 -I$(PKG_CONFIG_SYSROOT_DIR)/usr/lib/x86_64-linux-gnu/glib-2.0/include -I$(PKG_CONFIG_SYSROOT_DIR)/usr/local/lib -I$(PKG_CONFIG_SYSROOT_DIR)$(includedir)/gtest -I${top_srcdir}/gtest/include -I${top_srcdir}/source/include -I${top_srcdir}/source -I${top_srcdir}/source/test/mocks -I${top_srcdir}/source/test/rbus -I${top_srcdir}/source/test/rdk_logger -I${top_srcdir}/include -I${top_srcdir}/source/utils -I${top_srcdir}/source/privacycontrol -I${PKG_CONFIG_SYSROOT_DIR}$(includedir)/rbus -I${top_srcdir}/source/dcautil -I${top_srcdir}/source/ccspinterface -I${top_srcdir}/source/reportgen -I${top_srcdir}/source/xconf-client -I${top_srcdir}/source/protocol/http -I${top_srcdir}/source/protocol/rbusMethod -I${top_srcdir}/source/t2parser -I${top_srcdir}/source/bulkdata -I${top_srcdir}/source/scheduler -I${top_srcdir}/source/ccspinterface -I${PKG_CONFIG_SYSROOT_DIR}$(includedir) -I${PKG_CONFIG_SYSROOT_DIR}/usr/src/googletest/googlemock/include -I${RDK_PROJECT_ROOT_PATH}/$(GLIB_CFLAGS) -I${PKG_CONFIG_SYSROOT_DIR}$(includedir)/glib-2.0 -I${PKG_CONFIG_SYSROOT_DIR}$(libdir)/glib-2.0/include
6569

6670
reportprofiles_gtest_bin_SOURCES = gtest_main.cpp ../mocks/SystemMock.cpp ../mocks/FileioMock.cpp ../mocks/rdklogMock.cpp ../mocks/rbusMock.cpp ../mocks/rdkconfigMock.cpp ../mocks/VectorMock.cpp SchedulerMock.cpp reportprofileMock.cpp ../../bulkdata/reportprofiles.c reportprofilesTest.cpp ../../utils/persistence.c ../../utils/t2common.c ../../utils/t2collection.c ../../utils/t2MtlsUtils.c ../../utils/t2log_wrapper.c ../../dcautil/dcautil.c ../../dcautil/dca.c ../../dcautil/legacyutils.c ../../dcautil/dcaproc.c ../../xconf-client/xconfclient.c ../../protocol/rbusMethod/rbusmethodinterface.c ../../privacycontrol/rdkservices_privacyutils.c ../../reportgen/reportgen.c ../../bulkdata/t2eventreceiver.c ../../bulkdata/t2markers.c ../../t2parser/t2parser.c ../../bulkdata/datamodel.c ../../t2parser/t2parserxconf.c ../../bulkdata/profile.c ../../bulkdata/profilexconf.c ../../ccspinterface/rbusInterface.c ../../ccspinterface/busInterface.c ../../protocol/http/curlinterface.c ../../protocol/http/multicurlinterface.c
6771

6872
reportprofiles_gtest_bin_LDFLAGS = -L/usr/src/googletest/googletest/lib/.libs -L/usr/src/googletest/googlemock/lib/.libs -lgmock -lgtest -lpthread -lcjson -lmsgpackc -lglib-2.0 -lrt -lcurl
6973

70-
reportprofiles_gtest_bin_LDFLAGS += -Wl,--wrap=isRbusEnabled
74+
reportprofiles_gtest_bin_LDFLAGS += -Wl,--wrap=isRbusEnabled -Wl,--wrap=sendReportOverHTTP -Wl,--wrap=sendCachedReportsOverHTTP
7175

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright 2020 Comcast Cable Communications Management, LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* SPDX-License-Identifier: Apache-2.0
17+
*/
18+
#include <stdbool.h>
19+
#include <cjson/cJSON.h>
20+
#include "vector.h"
21+
#include "msgpack.h"
22+
#include "test/bulkdata/profileMock.h"
23+
24+
25+
//protocol mock functions
26+
extern "C" T2ERROR __wrap_sendReportOverHTTP(char *httpUrl, char *payload)
27+
{
28+
if(!g_profileMock)
29+
{
30+
return T2ERROR_FAILURE;
31+
}
32+
// No mock function needed, just a stub
33+
return g_profileMock->sendReportOverHTTP(httpUrl, payload);
34+
}
35+
36+
extern "C" T2ERROR __wrap_sendCachedReportsOverHTTP(char *httpUrl, Vector *reportList)
37+
{
38+
if(!g_profileMock)
39+
{
40+
return T2ERROR_FAILURE;
41+
}
42+
// No mock function needed, just a stub
43+
return g_profileMock->sendCachedReportsOverHTTP(httpUrl, reportList);
44+
}
45+

source/test/bulkdata/profileMock.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2020 Comcast Cable Communications Management, LLC
3+
** Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*
15+
* SPDX-License-Identifier: Apache-2.0
16+
*/
17+
//#ifndef SOURCE_TEST_MOCKS_SYSTEMMOCK_H_
18+
//#define SOURCE_TEST_MOCKS_SYSTEMMOCK_H_
19+
20+
#include <gtest/gtest.h>
21+
#include <gmock/gmock.h>
22+
23+
#include "telemetry2_0.h"
24+
25+
26+
class profileMock
27+
{
28+
public:
29+
30+
MOCK_METHOD(T2ERROR, sendReportOverHTTP, (char *httpUrl, char *payload), ());
31+
MOCK_METHOD(T2ERROR, sendCachedReportsOverHTTP, (char *httpUrl, Vector *reportList), ());
32+
};
33+
34+
extern profileMock *g_profileMock;
35+

source/test/bulkdata/profileTest.cpp

Lines changed: 86 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -859,6 +859,68 @@ TEST_F(ProfileTest, deleteAllReportProfiles) {
859859
EXPECT_EQ(deleteAllReportProfiles(), T2ERROR_SUCCESS);
860860
}
861861

862+
#ifdef GTEST_ENABLE
863+
extern "C" {
864+
typedef void* (*reportOnDemandFunc)(void*);
865+
reportOnDemandFunc reportOnDemandFuncCallback(void);
866+
typedef void (*freeProfilesHashMapFunc)(void *);
867+
freeProfilesHashMapFunc freeProfilesHashMapFuncCallback(void);
868+
typedef void (*freeReportProfileHashMapFunc)(void *);
869+
freeReportProfileHashMapFunc freeReportProfileHashMapFuncCallback(void);
870+
typedef void (*__msgpack_free_blobFunc)(void*);
871+
__msgpack_free_blobFunc __msgpack_free_blobFuncCallback(void);
872+
}
873+
874+
TEST(ReportProfilesCallbacks, FreeProfilesHashMap) {
875+
auto cb = freeProfilesHashMapFuncCallback();
876+
ASSERT_NE(cb, nullptr);
877+
878+
// Test with an actual element
879+
hash_element_t* item = (hash_element_t*) std::malloc(sizeof(hash_element_t));
880+
item->key = (char*) std::malloc(12);
881+
std::strcpy(item->key, "testkey");
882+
item->data = std::malloc(8);
883+
cb(item);
884+
885+
// Test with nullptr
886+
cb(nullptr);
887+
}
888+
TEST_F(ProfileTest, reportOnDemandTest)
889+
{
890+
reportOnDemandFunc func = reportOnDemandFuncCallback();
891+
ASSERT_NE(func,nullptr);
892+
func((void*)"ABORT");
893+
func((void*)"FOO");
894+
func(nullptr);
895+
}
896+
897+
TEST(ReportProfilesCallbacks, FreeReportProfileHashMap) {
898+
auto cb = freeReportProfileHashMapFuncCallback();
899+
ASSERT_NE(cb, nullptr);
900+
901+
// Make an item with ReportProfile-like .data
902+
hash_element_t* item = (hash_element_t*) std::malloc(sizeof(hash_element_t));
903+
item->key = (char*) std::malloc(12);
904+
std::strcpy(item->key, "profkey");
905+
struct ReportProfile {
906+
char* hash;
907+
char* config;
908+
void* hash_map_pad; // just to align with how your system might fill it, can be omitted
909+
};
910+
ReportProfile* rp = (ReportProfile*) std::malloc(sizeof(ReportProfile));
911+
rp->hash = (char*) std::malloc(6);
912+
std::strcpy(rp->hash, "hashV");
913+
rp->config = (char*) std::malloc(8);
914+
std::strcpy(rp->config, "cfgVal");
915+
item->data = rp;
916+
917+
cb(item);
918+
919+
// Safe to call with nullptr
920+
cb(nullptr);
921+
SUCCEED();
922+
}
923+
#endif
862924
#endif
863925

864926
#if 1
@@ -909,11 +971,21 @@ TEST_F(ProfileTest, SetAndIsSet) {
909971
profile->cachedReportList = nullptr;
910972
profile->protocol = strdup("HTTP");
911973
profile->encodingType = strdup("JSON");
912-
profile->t2HTTPDest = nullptr;
913-
profile->grepSeekProfile = nullptr;
974+
profile->jsonReportObj = nullptr; // types now match
975+
profile->checkPreviousSeek = true;
976+
977+
profile->t2HTTPDest = (T2HTTP *)malloc(sizeof(T2HTTP));
978+
profile->t2HTTPDest->URL = strdup("https://mock1xconf:50051/dataLakeMockXconf");
979+
profile->isUpdated = true;
980+
GrepSeekProfile *gsProfile = (GrepSeekProfile *)malloc(sizeof(GrepSeekProfile));
981+
if (gsProfile)
982+
{
983+
gsProfile->logFileSeekMap = hash_map_create();
984+
gsProfile->execCounter = 0;
985+
}
986+
profile->grepSeekProfile = gsProfile;
987+
//profile->grepSeekProfile = nullptr;
914988
profile->reportInProgress = false;
915-
profile->isUpdated = false;
916-
917989
EXPECT_CALL(*g_vectorMock, Vector_Size(_))
918990
.Times(::testing::AtMost(3))
919991
.WillRepeatedly(Return(0)); // Return 1 to indicate one profile in the list
@@ -926,26 +998,29 @@ TEST_F(ProfileTest, SetAndIsSet) {
926998
EXPECT_CALL(*g_vectorMock, Vector_PushBack(_, _))
927999
.Times(::testing::AtMost(3))
9281000
.WillRepeatedly(Return(T2ERROR_SUCCESS));
929-
1001+
9301002
// Scheduler mock expectations - ProfileXConf_set calls registerProfileWithScheduler
9311003
EXPECT_CALL(*g_schedulerMock, registerProfileWithScheduler(_, _, _, _, _, _, _, _))
9321004
.Times(::testing::AtMost(1))
9331005
.WillRepeatedly(Return(T2ERROR_SUCCESS));
934-
1006+
9351007
EXPECT_EQ(ProfileXConf_set(profile), T2ERROR_SUCCESS);
9361008
EXPECT_TRUE(ProfileXConf_isSet());
9371009

1010+
9381011
// Get name
9391012
char* name = ProfileXconf_getName();
9401013
ASSERT_NE(name, nullptr);
9411014
EXPECT_STREQ(name, "TestProfile");
9421015
free(name);
9431016

944-
// Clean up - ProfileXConf_uninit calls unregisterProfileFromScheduler
945-
EXPECT_CALL(*g_schedulerMock, unregisterProfileFromScheduler(_))
946-
.Times(::testing::AtMost(1))
947-
.WillRepeatedly(Return(T2ERROR_SUCCESS));
948-
1017+
test_set_reportThreadExits(true);
1018+
generateDcaReport(false,true);
1019+
EXPECT_CALL(*g_schedulerMock, SendInterruptToTimeoutThread(_))
1020+
.Times(::testing::AtMost(1));
1021+
1022+
ReportProfiles_Interrupt();
1023+
9491024
EXPECT_EQ(ProfileXConf_uninit(), T2ERROR_SUCCESS);
9501025
}
9511026

source/test/bulkdata/reportprofileMock.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,21 @@ extern "C" bool __wrap_isRbusEnabled()
3434
return g_reportprofileMock->isRbusEnabled();
3535
}
3636

37+
extern "C" {
38+
T2ERROR __wrap_sendReportOverHTTP(char *httpUrl, char *payload)
39+
{
40+
if (g_reportprofileMock) {
41+
return g_reportprofileMock->sendReportOverHTTP(httpUrl, payload);
42+
}
43+
return T2ERROR_FAILURE;
44+
}
45+
46+
T2ERROR __wrap_sendCachedReportsOverHTTP(char *httpUrl, Vector *reportList)
47+
{
48+
if (g_reportprofileMock) {
49+
return g_reportprofileMock->sendCachedReportsOverHTTP(httpUrl, reportList);
50+
}
51+
return T2ERROR_FAILURE;
52+
}
53+
}
54+

0 commit comments

Comments
 (0)