@@ -50,6 +50,7 @@ using namespace std;
5050using ::testing::_;
5151using ::testing::Return;
5252using ::testing::StrEq;
53+ using ::testing::Invoke;
5354
5455// Testing t2MtlsUtils
5556TEST (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)
774818TEST (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- */
9401000TEST_F (utilsTestFixture, getDevicePropertyData)
9411001{
9421002
0 commit comments