@@ -62,8 +62,6 @@ class CommonUtilsTest : public ::testing::Test
6262 }
6363};
6464
65- #if (0) // ///////////////////////////////////////////////////////////
66-
6765TEST_F (CommonUtilsTest, LoadStringFromFileInvalidArgument)
6866{
6967 EXPECT_STREQ (nullptr , LoadStringFromFile (nullptr , false , nullptr ));
@@ -3159,6 +3157,64 @@ TEST_F(CommonUtilsTest, GetOptionFromBuffer)
31593157 EXPECT_EQ (88 , GetIntegerOptionFromBuffer (" #This is a TestSetting test configuration for TestSetting\n #TestSetting=100\n TestSetting=88" , " TestSetting" , ' =' , ' #' , 10 , nullptr ));
31603158}
31613159
3160+ TEST_F (CommonUtilsTest, ReadEndOfFile)
3161+ {
3162+ const char * testFileContents = " Line 1\n Line 2\n Line 3\n Testline 4\n " ;
3163+ char * contents = NULL ;
3164+ EXPECT_TRUE (SavePayloadToFile (m_path, testFileContents, strlen (testFileContents), nullptr ));
3165+
3166+ EXPECT_STREQ (" \n " , contents = ReadEndOfFile (m_path, 1 , nullptr ));
3167+ FREE_MEMORY (contents);
3168+
3169+ EXPECT_STREQ (" 4\n " , contents = ReadEndOfFile (m_path, 2 , nullptr ));
3170+ FREE_MEMORY (contents);
3171+
3172+ EXPECT_STREQ (" 4\n " , contents = ReadEndOfFile (m_path, 3 , nullptr ));
3173+ FREE_MEMORY (contents);
3174+
3175+ EXPECT_STREQ (" Testline 4\n " , contents = ReadEndOfFile (m_path, strlen (" Testline 4\n " ), nullptr ));
3176+ FREE_MEMORY (contents);
3177+
3178+ EXPECT_STREQ (" Line 2\n Line 3\n Testline 4\n " , contents = ReadEndOfFile (m_path, strlen (" Line 2\n Line 3\n Testline 4\n " ), nullptr ));
3179+ FREE_MEMORY (contents);
3180+
3181+ EXPECT_TRUE (Cleanup (m_path));
3182+ }
3183+
3184+ TEST_F (CommonUtilsTest, CrashHandler)
3185+ {
3186+ OsConfigLogHandle log = nullptr ;
3187+ EXPECT_NE (nullptr , log = OpenLog (m_path, nullptr ));
3188+
3189+ OsConfigLogInfo (log, " Installing the crash handler in the parent process" );
3190+ InstallCrashHandler (m_path);
3191+
3192+ OsConfigLogInfo (log, " Forking the child process that will crash" );
3193+ pid_t pid = fork ();
3194+ EXPECT_NE (-1 , pid);
3195+ if (0 == pid)
3196+ {
3197+ // Cause a genuine SIGSEGV via NULL dereference exercises the full signal delivery and handler path
3198+ OsConfigLogInfo (log, " Forcing the crash" );
3199+ volatile int * null_ptr = NULL ;
3200+ *null_ptr = 0 ;
3201+ _exit (0 ); // never reached
3202+ }
3203+ waitpid (pid, NULL , 0 );
3204+ OsConfigLogInfo (log, " Done!" );
3205+
3206+ // Verify the crash handler [ERROR] lines appear in the handler log
3207+ char * contents = NULL ;
3208+ char * result = NULL ;
3209+ EXPECT_NE (nullptr , contents = LoadStringFromFile (m_path, false , nullptr ));
3210+ printf (" %s" , contents);
3211+ EXPECT_NE (nullptr , result = strstr (contents, " [ERROR] Crash due to segmentation fault (SIGSEGV)" ));
3212+ EXPECT_NE (nullptr , result = strstr (contents, " [ERROR] Stack trace:" ));
3213+
3214+ CloseLog (&log);
3215+ EXPECT_TRUE (Cleanup (m_path));
3216+ }
3217+
31623218TEST_F (CommonUtilsTest, LoggingOptions)
31633219{
31643220 const char * emergency = " EMERGENCY" ;
@@ -3231,64 +3287,4 @@ TEST_F(CommonUtilsTest, LoggingOptions)
32313287 EXPECT_FALSE (IsConsoleLoggingEnabled ());
32323288
32333289 EXPECT_FALSE (IsDaemon ());
3234- }
3235-
3236- #endif // #ifdef(0) /////////////////////////////////////////////////////////////
3237-
3238- TEST_F (CommonUtilsTest, ReadEndOfFile)
3239- {
3240- const char * testFileContents = " Line 1\n Line 2\n Line 3\n Testline 4\n " ;
3241- char * contents = NULL ;
3242- EXPECT_TRUE (SavePayloadToFile (m_path, testFileContents, strlen (testFileContents), nullptr ));
3243-
3244- EXPECT_STREQ (" \n " , contents = ReadEndOfFile (m_path, 1 , nullptr ));
3245- FREE_MEMORY (contents);
3246-
3247- EXPECT_STREQ (" 4\n " , contents = ReadEndOfFile (m_path, 2 , nullptr ));
3248- FREE_MEMORY (contents);
3249-
3250- EXPECT_STREQ (" 4\n " , contents = ReadEndOfFile (m_path, 3 , nullptr ));
3251- FREE_MEMORY (contents);
3252-
3253- EXPECT_STREQ (" Testline 4\n " , contents = ReadEndOfFile (m_path, strlen (" Testline 4\n " ), nullptr ));
3254- FREE_MEMORY (contents);
3255-
3256- EXPECT_STREQ (" Line 2\n Line 3\n Testline 4\n " , contents = ReadEndOfFile (m_path, strlen (" Line 2\n Line 3\n Testline 4\n " ), nullptr ));
3257- FREE_MEMORY (contents);
3258-
3259- EXPECT_TRUE (Cleanup (m_path));
3260- }
3261-
3262- TEST_F (CommonUtilsTest, CrashHandler)
3263- {
3264- OsConfigLogHandle log = nullptr ;
3265- EXPECT_NE (nullptr , log = OpenLog (m_path, nullptr ));
3266-
3267- OsConfigLogInfo (log, " Installing the crash handler in the parent process" );
3268- InstallCrashHandler (m_path);
3269-
3270- OsConfigLogInfo (log, " Forking the child process that will crash" );
3271- pid_t pid = fork ();
3272- EXPECT_NE (-1 , pid);
3273- if (0 == pid)
3274- {
3275- // Cause a genuine SIGSEGV via NULL dereference exercises the full signal delivery and handler path
3276- OsConfigLogInfo (log, " Forcing the crash" );
3277- volatile int * null_ptr = NULL ;
3278- *null_ptr = 0 ;
3279- _exit (0 ); // never reached
3280- }
3281- waitpid (pid, NULL , 0 );
3282- OsConfigLogInfo (log, " Done!" );
3283-
3284- // Verify the crash handler [ERROR] lines appear in the handler log
3285- char * contents = NULL ;
3286- char * result = NULL ;
3287- EXPECT_NE (nullptr , contents = LoadStringFromFile (m_path, false , nullptr ));
3288- printf (" %s" , contents);
3289- EXPECT_NE (nullptr , result = strstr (contents, " [ERROR] Crash due to segmentation fault (SIGSEGV)" ));
3290- EXPECT_NE (nullptr , result = strstr (contents, " [ERROR] Stack trace:" ));
3291-
3292- CloseLog (&log);
3293- EXPECT_TRUE (Cleanup (m_path));
3294- }
3290+ }
0 commit comments