Skip to content

Commit 0f54a26

Browse files
Copilotshibu-kv
andauthored
RDKB-63834: Add unit tests for empty config file detection and fprintf failure paths in persistence.c (#285)
* Initial plan * Add unit tests for empty config file detection and fprintf failure in persistence.c Co-authored-by: shibu-kv <89052442+shibu-kv@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: shibu-kv <89052442+shibu-kv@users.noreply.github.com>
1 parent df113a5 commit 0f54a26

File tree

3 files changed

+128
-16
lines changed

3 files changed

+128
-16
lines changed

.gitignore

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Build system generated files
2+
Makefile
3+
Makefile.in
4+
aclocal.m4
5+
autom4te.cache/
6+
compile
7+
config.guess
8+
config.log
9+
config.status
10+
config.sub
11+
configure
12+
configure~
13+
depcomp
14+
install-sh
15+
libtool
16+
ltmain.sh
17+
missing
18+
m4/
19+
20+
# Build artifacts
21+
*.o
22+
*.lo
23+
*.la
24+
*.a
25+
*.so
26+
*.bin
27+
*.deps/
28+
*.dirstamp
29+
.deps/
30+
.libs/
31+
32+
# Source subdirectory build files
33+
source/**/Makefile
34+
source/**/Makefile.in
35+
source/**/*.deps/
36+
source/**/.libs/

source/test/mocks/FileioMock.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,22 @@ extern "C" int fprintf(FILE* stream, const char* format, ...) {
473473
va_end(args);
474474
return result;
475475
}
476+
477+
/* Intercept the FORTIFY_SOURCE-hardened variant of fprintf */
478+
extern "C" int __fprintf_chk(FILE* stream, int /*flag*/, const char* format, ...) {
479+
va_list args;
480+
va_start(args, format);
481+
int result = -1;
482+
483+
if (g_fileIOMock) {
484+
result = g_fileIOMock->fprintf(stream, format, args);
485+
}
486+
else {
487+
result = vfprintf(stream, format, args);
488+
}
489+
va_end(args);
490+
return result;
491+
}
476492
/*
477493
extern "C" CURLcode curl_easy_setopt(CURL *curl, CURLoption option, ...)
478494
{

source/test/utils/UtilsTest.cpp

Lines changed: 76 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ using namespace std;
5050
using ::testing::_;
5151
using ::testing::Return;
5252
using ::testing::StrEq;
53+
using ::testing::Invoke;
5354

5455
//Testing t2MtlsUtils
5556
TEST(GET_CERTS, MTLS_UTILS_NULL)
@@ -770,6 +771,49 @@ TEST_F(utilsTestFixture, FETCHLOCALCONFIGS_FUNC4)
770771
Vector_Destroy(configlist, free);
771772
}
772773

774+
TEST_F(utilsTestFixture, FETCHLOCALCONFIGS_EMPTY_FILE)
775+
{
776+
const char* path = "/opt/.t2persistentFolder/";
777+
DIR *dir = (DIR*)0xffffffff;
778+
Vector* configlist = NULL;
779+
Vector_Create(&configlist);
780+
struct dirent *entry = NULL;
781+
entry = (struct dirent *)malloc(sizeof(struct dirent));
782+
entry->d_type = DT_REG;
783+
std::strncpy(entry->d_name, "TestProfile.json", sizeof(entry->d_name) - 1);
784+
entry->d_name[sizeof(entry->d_name) - 1] = '\0';
785+
EXPECT_CALL(*g_fileIOMock, opendir(_))
786+
.Times(1)
787+
.WillOnce(Return(dir));
788+
EXPECT_CALL(*g_fileIOMock, readdir(_))
789+
.Times(2)
790+
.WillOnce(Return(entry))
791+
.WillOnce(Return((struct dirent *)NULL));
792+
EXPECT_CALL(*g_fileIOMock, open(_,_))
793+
.Times(1)
794+
.WillOnce(Return(1));
795+
EXPECT_CALL(*g_fileIOMock, fstat(_,_))
796+
.Times(1)
797+
.WillOnce(Invoke([](int /*fd*/, struct stat* buf) -> int {
798+
memset(buf, 0, sizeof(struct stat));
799+
buf->st_size = 0;
800+
return 0;
801+
}));
802+
EXPECT_CALL(*g_fileIOMock, close(_))
803+
.Times(1)
804+
.WillOnce(Return(0));
805+
EXPECT_CALL(*g_systemMock, unlink(_))
806+
.Times(1)
807+
.WillOnce(Return(0));
808+
EXPECT_CALL(*g_fileIOMock, closedir(_))
809+
.Times(1)
810+
.WillOnce(Return(0));
811+
ASSERT_EQ(T2ERROR_SUCCESS, fetchLocalConfigs(path, configlist));
812+
EXPECT_EQ(0, (int)Vector_Size(configlist));
813+
Vector_Destroy(configlist, free);
814+
free(entry);
815+
}
816+
773817
#if defined (MTLS_FROM_ENV)
774818
TEST(GetMtlsCertsTest, ReturnsDynamicCertsWhenEnvSet) {
775819
setenv("XPKI", "1", 1);
@@ -915,28 +959,44 @@ TEST_F(utilsTestFixture, SAVECONFITOFILE1)
915959
.WillOnce(Return(mockfp));
916960
ASSERT_EQ(T2ERROR_FAILURE, saveConfigToFile(filepath, profilename, config));
917961
}
918-
/*
919-
TEST_F(utilsTestFixture, SAVECONFITOFILE2)
962+
TEST_F(utilsTestFixture, SAVECONFITOFILE_FPRINTF_FAILURE)
920963
{
921-
const char* filepath = "/nvram/.t2reportprofiles";
922-
const char* profilename = "Profile_1";
923-
const char* config = "This is a config string";
924-
FILE* mockfp = (FILE *)0xffffffff;
925-
964+
const char* filepath = "/nvram/.t2reportprofiles/";
965+
const char* profilename = "Profile_1";
966+
const char* config = "This is a config string";
967+
FILE* mockfp = (FILE *)0xffffffff;
926968
EXPECT_CALL(*g_fileIOMock, fopen(_,_))
927-
.Times(1)
928-
.WillOnce(Return(mockfp));
969+
.Times(1)
970+
.WillOnce(Return(mockfp));
929971
EXPECT_CALL(*g_fileIOMock, fprintf(_,_,_))
930-
.Times(1)
931-
.WillOnce(Return(-1));
972+
.Times(1)
973+
.WillOnce(Return(-1));
932974
EXPECT_CALL(*g_fileIOMock, fclose(_))
933-
.Times(1)
934-
.WillOnce(Return(0));
935-
936-
EXPECT_EQ(saveConfigToFile(filepath, profilename, config), T2ERROR_SUCCESS);
975+
.Times(1)
976+
.WillOnce(Return(0));
977+
EXPECT_CALL(*g_systemMock, unlink(_))
978+
.Times(1)
979+
.WillOnce(Return(0));
980+
ASSERT_EQ(T2ERROR_FAILURE, saveConfigToFile(filepath, profilename, config));
981+
}
937982

983+
TEST_F(utilsTestFixture, SAVECONFITOFILE_SUCCESS)
984+
{
985+
const char* filepath = "/nvram/.t2reportprofiles/";
986+
const char* profilename = "Profile_1";
987+
const char* config = "This is a config string";
988+
FILE* mockfp = (FILE *)0xffffffff;
989+
EXPECT_CALL(*g_fileIOMock, fopen(_,_))
990+
.Times(1)
991+
.WillOnce(Return(mockfp));
992+
EXPECT_CALL(*g_fileIOMock, fprintf(_,_,_))
993+
.Times(1)
994+
.WillOnce(Return((int)strlen(config)));
995+
EXPECT_CALL(*g_fileIOMock, fclose(_))
996+
.Times(1)
997+
.WillOnce(Return(0));
998+
ASSERT_EQ(T2ERROR_SUCCESS, saveConfigToFile(filepath, profilename, config));
938999
}
939-
*/
9401000
TEST_F(utilsTestFixture, getDevicePropertyData)
9411001
{
9421002

0 commit comments

Comments
 (0)