Skip to content

Commit e4a079f

Browse files
RDK-60805: Adding L1 unit test cases for reportprofiles
Reason for change: Adding L1 unit test cases for reportprofiles Test Procedure: Tested and verified Risks: Medium Priority: P1 Signed-off-by: Rose Mary Benny <RoseMary_Benny@comcast.com>
1 parent e1191b8 commit e4a079f

6 files changed

Lines changed: 212 additions & 55 deletions

File tree

source/bulkdata/reportprofiles.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -907,6 +907,7 @@ void ReportProfiles_ProcessReportProfilesBlob(cJSON *profiles_root, bool rprofil
907907
{
908908
getMarkerCompRbusSub(false);
909909
}
910+
910911
// Populate profile hash map for current configuration
911912
for( profileIndex = 0; profileIndex < profiles_count; profileIndex++ )
912913
{
@@ -976,7 +977,6 @@ void ReportProfiles_ProcessReportProfilesBlob(cJSON *profiles_root, bool rprofil
976977
T2Error("Failed to remove previous report profile from the disk\n");
977978
}
978979
}
979-
980980
if(isRbusEnabled())
981981
{
982982
unregisterDEforCompEventList();
@@ -1064,7 +1064,6 @@ void ReportProfiles_ProcessReportProfilesBlob(cJSON *profiles_root, bool rprofil
10641064
}
10651065
}
10661066
}
1067-
10681067
if (rm_flag)
10691068
{
10701069
removeProfileFromDisk(DirPath, MSGPACK_REPORTPROFILES_PERSISTENT_FILE);

source/test/bulkdata/Makefile.am

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ AUTOMAKE_OPTIONS = subdir-objects
2727

2828
ACLOCAL_AMFLAGS = -I m4
2929

30-
bin_PROGRAMS = profile_gtest.bin datamodel_gtest.bin t2markers_gtest.bin profilexconf_gtest.bin
30+
bin_PROGRAMS = profile_gtest.bin datamodel_gtest.bin t2markers_gtest.bin profilexconf_gtest.bin reportprofiles_gtest.bin
3131

3232
datamodel_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
3333

@@ -59,3 +59,13 @@ profile_gtest_bin_SOURCES = gtest_main.cpp ../mocks/SystemMock.cpp ../mocks/File
5959

6060
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
6161

62+
# Test binary for reportprofiles.c
63+
64+
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
65+
66+
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
67+
68+
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
69+
70+
reportprofiles_gtest_bin_LDFLAGS += -Wl,--wrap=isRbusEnabled
71+

source/test/bulkdata/profileTest.cpp

Lines changed: 41 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,28 @@ TEST_F(ProfileTest, UninitProfileList_Success) {
225225
EXPECT_EQ(uninitProfileList(), T2ERROR_SUCCESS);
226226
}
227227

228+
TEST_F(ProfileTest, ReportProfiles_uninit) {
229+
EXPECT_CALL(*g_vectorMock, Vector_Create(_))
230+
.Times(::testing::AtMost(3))
231+
.WillRepeatedly(Return(T2ERROR_SUCCESS));
232+
EXPECT_CALL(*g_vectorMock, Vector_PushBack(_, _))
233+
.Times(::testing::AtMost(1))
234+
.WillRepeatedly(Return(T2ERROR_SUCCESS));
235+
EXPECT_CALL(*g_vectorMock, Vector_Size(_))
236+
.Times(::testing::AtMost(3))
237+
.WillRepeatedly(Return(0));
238+
EXPECT_CALL(*g_vectorMock, Vector_At(_, _))
239+
.Times(::testing::AtMost(2))
240+
.WillRepeatedly(Return(nullptr));
241+
EXPECT_CALL(*g_schedulerMock, uninitScheduler())
242+
.Times(::testing::AtMost(1));
243+
EXPECT_CALL(*g_schedulerMock, unregisterProfileFromScheduler(_))
244+
.Times(::testing::AtMost(5))
245+
.WillRepeatedly(Return(T2ERROR_SUCCESS));
246+
247+
EXPECT_EQ(ReportProfiles_uninit(), T2ERROR_FAILURE);
248+
}
249+
228250
// Test getProfileCount
229251
TEST_F(ProfileTest, GetProfileCount_NotInitialized) {
230252
EXPECT_CALL(*g_vectorMock, Vector_Size(_))
@@ -759,6 +781,14 @@ TEST_F(ProfileTest, ReportProfiles_deleteProfileXConf) {
759781
}
760782
#endif
761783

784+
TEST_F(ProfileTest, ReportProfiles_deleteProfileXConf_EmptyList) {
785+
ProfileXConf profile;
786+
EXPECT_CALL(*g_vectorMock, Vector_Size(_))
787+
.Times(::testing::AtMost(1))
788+
.WillRepeatedly(Return(0)); // Return 1 to indicate only one profile (no duplicates)
789+
EXPECT_EQ(ReportProfiles_deleteProfileXConf(&profile), T2ERROR_SUCCESS);
790+
}
791+
762792
TEST_F(ProfileTest, ReportProfiles_deleteProfile) {
763793
EXPECT_CALL(*g_vectorMock, Vector_Size(_))
764794
.Times(::testing::AtMost(2))
@@ -810,7 +840,16 @@ TEST_F(ProfileTest, RemovePreRPfromDisk) {
810840
EXPECT_EQ(RemovePreRPfromDisk("/tmp", &dummy), T2ERROR_SUCCESS);
811841
}
812842

813-
#if 0
843+
TEST_F(ProfileTest, RemovePreRPfromDisk_FailsIfDirNull) {
844+
hash_map_t dummy;
845+
// Mock opendir to return NULL to simulate failure
846+
EXPECT_CALL(*g_fileIOMock, opendir(_))
847+
.Times(1)
848+
.WillOnce(Return(nullptr));
849+
// readdir and closedir should NOT be called in this branch
850+
EXPECT_EQ(RemovePreRPfromDisk("/tmp", &dummy), T2ERROR_FAILURE);
851+
}
852+
814853
TEST_F(ProfileTest, deleteAllReportProfiles) {
815854
EXPECT_CALL(*g_vectorMock, Vector_Size(_))
816855
.Times(::testing::AtMost(1))
@@ -819,58 +858,7 @@ TEST_F(ProfileTest, deleteAllReportProfiles) {
819858
.WillRepeatedly(Return(T2ERROR_SUCCESS));
820859
EXPECT_EQ(deleteAllReportProfiles(), T2ERROR_SUCCESS);
821860
}
822-
#endif
823-
824-
#if 0
825-
TEST_F(ProfileTest, isMtlsEnabled) {
826-
char status[8] = "true";
827-
EXPECT_CALL(*g_rbusMock, rbus_get(_,_,_))
828-
.Times(::testing::AtMost(2))
829-
.WillRepeatedly(Return(RBUS_ERROR_SUCCESS));
830-
EXPECT_CALL(*g_rbusMock, rbusValue_GetType(_))
831-
.Times(::testing::AtMost(2))
832-
.WillRepeatedly(Return(RBUS_BOOLEAN));
833-
EXPECT_CALL(*g_rbusMock, rbusValue_GetBoolean(_))
834-
.Times(::testing::AtMost(2))
835-
.WillRepeatedly(Return(RBUS_ERROR_SUCCESS));
836-
EXPECT_CALL(*g_rbusMock, rbusValue_Release(_))
837-
.Times(::testing::AtMost(2))
838-
.WillRepeatedly(Return());
839-
EXPECT_CALL(*g_rbusMock, rbusValue_ToString(_,_,_))
840-
.Times(::testing::AtMost(1))
841-
.WillRepeatedly(Return(status));
842-
EXPECT_TRUE(isMtlsEnabled());
843-
}
844-
#endif
845861

846-
#if 0
847-
TEST_F(ProfileTest, ReportProfiles_uninit) {
848-
EXPECT_CALL(*g_vectorMock, Vector_Create(_))
849-
.Times(::testing::AtMost(3)) // 1 for local test configlist, 1 for global profileList, 1 for configList in loadReportProfilesFromDisk
850-
.WillRepeatedly(Return(T2ERROR_SUCCESS));
851-
EXPECT_CALL(*g_vectorMock, Vector_PushBack(_, _))
852-
.Times(::testing::AtMost(1))
853-
.WillRepeatedly(Return(T2ERROR_SUCCESS));
854-
EXPECT_CALL(*g_vectorMock, Vector_Size(_))
855-
.Times(::testing::AtMost(3)) // May be called multiple times - in deleteAllProfiles, etc.
856-
.WillRepeatedly(Return(0)); // Return 0 to indicate no profiles (avoid unregister calls)
857-
EXPECT_CALL(*g_vectorMock, Vector_At(_, _))
858-
.Times(::testing::AtMost(2)) // May be called if profiles exist
859-
.WillRepeatedly(Return(nullptr));
860-
861-
// Scheduler mock expectations - uninitScheduler is definitely called
862-
EXPECT_CALL(*g_schedulerMock, uninitScheduler())
863-
.Times(::testing::AtMost(1));
864-
865-
// unregisterProfileFromScheduler may be called for each profile during deleteAllProfiles
866-
// Using AtMost to handle cases where profiles exist
867-
EXPECT_CALL(*g_schedulerMock, unregisterProfileFromScheduler(_))
868-
.Times(::testing::AtMost(5)) // Allow up to 5 calls in case profiles exist
869-
.WillRepeatedly(Return(T2ERROR_SUCCESS));
870-
871-
EXPECT_EQ(ReportProfiles_uninit(), T2ERROR_SUCCESS);
872-
}
873-
#endif
874862
#endif
875863

876864
#if 1
@@ -1097,6 +1085,7 @@ TEST_F(ProfileTest, DeleteProfile) {
10971085
profile->isUpdated = false;
10981086

10991087
ProfileXConf_set(profile);
1088+
EXPECT_EQ(ReportProfiles_setProfileXConf(profile),T2ERROR_FAILURE);
11001089
EXPECT_EQ(ProfileXConf_delete(profile), T2ERROR_FAILURE);
11011090
ProfileXConf_uninit();
11021091
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include <stdbool.h>
2+
#include <cjson/cJSON.h>
3+
#include "test/bulkdata/reportprofileMock.h"
4+
5+
6+
7+
// Mock Method
8+
9+
extern "C" bool __wrap_isRbusEnabled()
10+
{
11+
if (!g_reportprofileMock)
12+
{
13+
return false;
14+
}
15+
return g_reportprofileMock->isRbusEnabled();
16+
}
17+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#pragma once
2+
3+
#include <gtest/gtest.h>
4+
#include <gmock/gmock.h>
5+
6+
7+
#include "telemetry2_0.h"
8+
9+
class reportprofileMock
10+
{
11+
public:
12+
13+
MOCK_METHOD(bool, isRbusEnabled, (), ());
14+
};
15+
16+
extern reportprofileMock *g_reportprofileMock;
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
#include <gtest/gtest.h>
2+
#include <gmock/gmock.h>
3+
4+
extern "C" {
5+
#include "reportprofiles.h"
6+
#include "profile.h"
7+
#include "t2eventreceiver.h"
8+
#include "t2collection.h"
9+
#include "t2log_wrapper.h"
10+
#include "msgpack.h"
11+
}
12+
13+
#include "test/mocks/SystemMock.h"
14+
#include "test/mocks/FileioMock.h"
15+
#include "test/mocks/rdklogMock.h"
16+
#include "test/mocks/rbusMock.h"
17+
#include "test/mocks/rdkconfigMock.h"
18+
#include "test/mocks/VectorMock.h"
19+
#include "test/bulkdata/SchedulerMock.h"
20+
#include "reportprofileMock.h"
21+
22+
using namespace std;
23+
using ::testing::_;
24+
using ::testing::Return;
25+
using ::testing::StrEq;
26+
using ::testing::Invoke;
27+
// Create global mocks if you need them just like t2markersTest.cpp
28+
FileMock *g_fileIOMock = NULL;
29+
SystemMock * g_systemMock = NULL;
30+
rdklogMock *m_rdklogMock = NULL;
31+
rbusMock *g_rbusMock = NULL;
32+
rdkconfigMock *g_rdkconfigMock = nullptr;
33+
extern VectorMock *g_vectorMock;
34+
extern SchedulerMock *g_schedulerMock;
35+
reportprofileMock* g_reportprofileMock = nullptr;
36+
// Test fixture for reportprofiles
37+
class reportprofilesTestFixture : public ::testing::Test {
38+
protected:
39+
void SetUp() override {
40+
g_reportprofileMock = new reportprofileMock();
41+
g_systemMock = new SystemMock();
42+
g_fileIOMock = new FileMock();
43+
m_rdklogMock = new rdklogMock();
44+
g_rbusMock = new rbusMock();
45+
g_vectorMock = new VectorMock();
46+
}
47+
void TearDown() override {
48+
delete g_reportprofileMock;
49+
delete g_systemMock;
50+
delete g_fileIOMock;
51+
delete m_rdklogMock;
52+
delete g_rbusMock;
53+
delete g_vectorMock;
54+
g_reportprofileMock = nullptr;
55+
g_systemMock = nullptr;
56+
g_fileIOMock = nullptr;
57+
m_rdklogMock = nullptr;
58+
g_rbusMock = nullptr;
59+
g_vectorMock = nullptr;
60+
}
61+
};
62+
63+
64+
TEST_F(reportprofilesTestFixture, ProcessMsgPackBlob_InvalidFormat) {
65+
struct __msgpack__ msg;
66+
msg.msgpack_blob = nullptr;
67+
msg.msgpack_blob_size = 0;
68+
int ret = __ReportProfiles_ProcessReportProfilesMsgPackBlob(&msg, false);
69+
EXPECT_EQ(ret, T2ERROR_INVALID_ARGS);
70+
}
71+
72+
TEST_F(reportprofilesTestFixture, ProcessReportProfilesBlob_EmptyProfile_T2_TEMP_RP) {
73+
cJSON *root = cJSON_CreateObject();
74+
cJSON *profiles = cJSON_CreateArray();
75+
cJSON_AddItemToObject(root, "profiles", profiles);
76+
ReportProfiles_ProcessReportProfilesBlob(root, T2_TEMP_RP);
77+
cJSON_Delete(root);
78+
}
79+
80+
TEST_F(reportprofilesTestFixture, ProcessReportProfilesBlob_EmptyProfile_Normal) {
81+
cJSON *root = cJSON_CreateObject();
82+
cJSON *profiles = cJSON_CreateArray();
83+
cJSON_AddItemToObject(root, "profiles", profiles);
84+
EXPECT_CALL(*g_vectorMock, Vector_Size(_)).WillRepeatedly(Return(0));
85+
EXPECT_CALL(*g_vectorMock, Vector_Destroy(_, _)).WillRepeatedly(Return(T2ERROR_SUCCESS));
86+
ReportProfiles_ProcessReportProfilesBlob(root, T2_RP); // normal, triggers deleteAllReportProfiles
87+
cJSON_Delete(root);
88+
}
89+
90+
TEST_F(reportprofilesTestFixture, ProcessReportProfilesBlob_AddNewProfile) {
91+
// New profile, triggers add logic and saveConfigToFile
92+
cJSON *root = cJSON_CreateObject();
93+
cJSON *profiles = cJSON_CreateArray();
94+
cJSON_AddItemToObject(root, "profiles", profiles);
95+
cJSON *profile = cJSON_CreateObject();
96+
cJSON_AddStringToObject(profile, "name", "newprofile");
97+
cJSON_AddStringToObject(profile, "hash", "newhash");
98+
cJSON *value = cJSON_CreateObject();
99+
cJSON_AddStringToObject(value, "param", "value");
100+
cJSON_AddItemToObject(profile, "value", value);
101+
cJSON_AddItemToArray(profiles, profile);
102+
103+
EXPECT_CALL(*g_reportprofileMock, isRbusEnabled())
104+
.WillRepeatedly(Return(false));
105+
// Expect add and saveConfigToFile, can stub if needed
106+
EXPECT_CALL(*g_vectorMock, Vector_Size(_)).WillRepeatedly(Return(0));
107+
EXPECT_CALL(*g_vectorMock, Vector_Destroy(_, _)).WillRepeatedly(Return(T2ERROR_SUCCESS));
108+
109+
DIR *dir = reinterpret_cast<DIR*>(1);
110+
111+
EXPECT_CALL(*g_fileIOMock, opendir(_))
112+
.Times(::testing::AtMost(1))
113+
.WillRepeatedly(Return(dir));
114+
EXPECT_CALL(*g_fileIOMock, readdir(_))
115+
.Times(::testing::AtMost(1))
116+
.WillRepeatedly(Return((struct dirent *)NULL));
117+
EXPECT_CALL(*g_fileIOMock, closedir(_))
118+
.Times(::testing::AtMost(1))
119+
.WillRepeatedly(Return(0));
120+
ReportProfiles_ProcessReportProfilesBlob(root, T2_RP);
121+
cJSON_Delete(root);
122+
}
123+
TEST_F(reportprofilesTestFixture, ProcessReportProfilesMsgPackBlob_NullBlob) {
124+
ReportProfiles_ProcessReportProfilesMsgPackBlob(NULL,0);
125+
}
126+

0 commit comments

Comments
 (0)