1010std::string expected_filename;
1111int expect_fopen_count = 0 ;
1212int fopen_count = 0 ;
13+ FILE *fopen_return = MOCK_FILE_PTR;
1314
1415FILE *mock_fopen (const char *filename, const char *mode) {
1516 fopen_count++;
1617 EXPECT_STREQ (filename, expected_filename.c_str ());
1718 EXPECT_STREQ (mode, " a" );
18- return MOCK_FILE_PTR ;
19+ return fopen_return ;
1920}
2021
2122const std::string MOCK_FN_NAME = " MOCK_FUNCTION_NAME" ;
@@ -110,11 +111,18 @@ const char *env_variable = "";
110111#ifndef UMF_VERSION
111112#define UMF_VERSION " test version"
112113#endif
114+ // This trick to not work on windows
115+ #ifndef _WIN32
116+ #define abort ()
117+ #endif
113118#include " utils/utils_log.c"
114119#undef utils_env_var
115120#undef fopen
116121#undef fputs
117122#undef fflush
123+ #ifndef _WIN32
124+ #undef abort
125+ #endif
118126}
119127using umf_test::test;
120128
@@ -139,6 +147,7 @@ void helper_checkConfig(utils_log_config_t *expected, utils_log_config_t *is) {
139147TEST_F (test, parseEnv_errors) {
140148 expected_message = " " ;
141149 loggerConfig = {0 , 0 , LOG_ERROR, LOG_ERROR, NULL };
150+ fopen_return = MOCK_FILE_PTR;
142151
143152 expect_fput_count = 0 ;
144153 expected_stream = stderr;
@@ -164,6 +173,7 @@ TEST_F(test, parseEnv_errors) {
164173TEST_F (test, parseEnv) {
165174 utils_log_config_t b = loggerConfig;
166175 expected_message = " " ;
176+ fopen_return = MOCK_FILE_PTR;
167177
168178 std::vector<std::pair<std::string, int >> logLevels = {
169179 {" level:debug" , LOG_DEBUG},
@@ -250,6 +260,20 @@ TEST_F(test, parseEnv) {
250260 }
251261 }
252262}
263+ TEST_F (test, fopen_fail) {
264+ expected_stream = stderr;
265+ expect_fopen_count = 1 ;
266+ loggerConfig = {0 , 0 , LOG_ERROR, LOG_ERROR, NULL };
267+ utils_log_config_t b = loggerConfig = {0 , 0 , LOG_ERROR, LOG_ERROR, NULL };
268+
269+ expected_filename = " filepath" ;
270+ expected_message = " [ERROR UMF] utils_log_init: Cannot open output file "
271+ " filepath - logging disabled: \n " ;
272+
273+ fopen_return = NULL ;
274+ helper_log_init (" output:file,filepath" );
275+ helper_checkConfig (&b, &loggerConfig);
276+ }
253277
254278template <typename ... Args> void helper_test_log (Args... args) {
255279 fput_count = 0 ;
@@ -279,6 +303,7 @@ static std::string helper_log_str(int l) {
279303
280304TEST_F (test, log_levels) {
281305 expected_stream = stderr;
306+ fopen_return = MOCK_FILE_PTR;
282307 for (int i = LOG_DEBUG; i <= LOG_ERROR; i++) {
283308 for (int j = LOG_DEBUG; j <= LOG_ERROR; j++) {
284309 loggerConfig = {0 , 0 , (utils_log_level_t )i, LOG_DEBUG, stderr};
@@ -302,6 +327,7 @@ TEST_F(test, log_outputs) {
302327 std::vector<FILE *> outs = {stdout, stderr, MOCK_FILE_PTR};
303328 expect_fput_count = 1 ;
304329 expect_fflush_count = 1 ;
330+ fopen_return = MOCK_FILE_PTR;
305331 expected_message = " [DEBUG UMF] " + MOCK_FN_NAME + " : example log\n " ;
306332 for (auto o : outs) {
307333 loggerConfig = {0 , 0 , LOG_DEBUG, LOG_DEBUG, o};
@@ -313,6 +339,7 @@ TEST_F(test, log_outputs) {
313339TEST_F (test, flush_levels) {
314340 expected_stream = stderr;
315341 expect_fput_count = 1 ;
342+ fopen_return = MOCK_FILE_PTR;
316343 for (int i = LOG_DEBUG; i <= LOG_ERROR; i++) {
317344 for (int j = LOG_DEBUG; j <= LOG_ERROR; j++) {
318345 loggerConfig = {0 , 0 , LOG_DEBUG, (utils_log_level_t )i, stderr};
@@ -332,6 +359,7 @@ TEST_F(test, flush_levels) {
332359TEST_F (test, long_log) {
333360 expect_fput_count = 1 ;
334361 expect_fflush_count = 1 ;
362+ fopen_return = MOCK_FILE_PTR;
335363 loggerConfig = {0 , 0 , LOG_DEBUG, LOG_DEBUG, stderr};
336364 expected_message = " [DEBUG UMF] " + MOCK_FN_NAME + " : " +
337365 std::string (8189 - MOCK_FN_NAME.size (), ' x' ) + " \n " ;
@@ -347,6 +375,7 @@ TEST_F(test, long_log) {
347375TEST_F (test, timestamp_log) {
348376 expect_fput_count = 1 ;
349377 expect_fflush_count = 1 ;
378+ fopen_return = MOCK_FILE_PTR;
350379 loggerConfig = {1 , 0 , LOG_DEBUG, LOG_DEBUG, stderr};
351380 // TODO: for now we do not check output message,
352381 // as it requires more sophisticated message validation (a.k.a regrex)
@@ -357,6 +386,7 @@ TEST_F(test, timestamp_log) {
357386TEST_F (test, pid_log) {
358387 expect_fput_count = 1 ;
359388 expect_fflush_count = 1 ;
389+ fopen_return = MOCK_FILE_PTR;
360390 loggerConfig = {0 , 1 , LOG_DEBUG, LOG_DEBUG, stderr};
361391 // TODO: for now we do not check output message,
362392 // as it requires more sophisticated message validation (a.k.a regrex)
@@ -369,6 +399,7 @@ TEST_F(test, log_fatal) {
369399 expected_stream = stderr;
370400 expect_fput_count = 1 ;
371401 expect_fflush_count = 1 ;
402+ fopen_return = MOCK_FILE_PTR;
372403
373404 expected_message = " [FATAL UMF] " + MOCK_FN_NAME + " : example log\n " ;
374405 strerror_ret_static = 0 ;
@@ -379,6 +410,7 @@ TEST_F(test, log_macros) {
379410 expected_stream = stderr;
380411 expect_fput_count = 1 ;
381412 expect_fflush_count = 1 ;
413+ fopen_return = MOCK_FILE_PTR;
382414 loggerConfig = {0 , 0 , LOG_DEBUG, LOG_DEBUG, stderr};
383415
384416 expected_message = " [DEBUG UMF] TestBody: example log\n " ;
@@ -428,6 +460,7 @@ template <typename... Args> void helper_test_plog(Args... args) {
428460TEST_F (test, plog_basic) {
429461 loggerConfig = {0 , 0 , LOG_DEBUG, LOG_DEBUG, stderr};
430462 expected_stream = stderr;
463+ fopen_return = MOCK_FILE_PTR;
431464 errno = 1 ;
432465 strerr = " test error" ;
433466 expect_fput_count = 1 ;
@@ -448,6 +481,7 @@ TEST_F(test, plog_invalid) {
448481 strerr = " test error" ;
449482 expect_fput_count = 1 ;
450483 expect_fflush_count = 1 ;
484+ fopen_return = MOCK_FILE_PTR;
451485
452486 expected_message =
453487 " [DEBUG UMF] " + MOCK_FN_NAME + " : example log: unknown error\n " ;
@@ -465,6 +499,7 @@ TEST_F(test, plog_long_message) {
465499 strerror_ret_static = 0 ;
466500 strerr = " test error" ;
467501 errno = 1 ;
502+ fopen_return = MOCK_FILE_PTR;
468503
469504 expected_message = " [DEBUG UMF] " + MOCK_FN_NAME + " : " +
470505 std::string (8178 - MOCK_FN_NAME.length (), ' x' ) +
@@ -487,6 +522,7 @@ TEST_F(test, plog_long_error) {
487522 std::string tmp = std::string (2000 , ' x' );
488523 strerr = tmp.c_str ();
489524 errno = 1 ;
525+ fopen_return = MOCK_FILE_PTR;
490526#ifdef WIN32
491527 /* On windows limit is shorter, and there is no truncated detection*/
492528 expected_message = " [DEBUG UMF] " + MOCK_FN_NAME +
@@ -508,6 +544,7 @@ TEST_F(test, log_pmacros) {
508544 loggerConfig = {0 , 0 , LOG_DEBUG, LOG_DEBUG, stderr};
509545 errno = 1 ;
510546 strerr = " test error" ;
547+ fopen_return = MOCK_FILE_PTR;
511548
512549 expected_message = " [DEBUG UMF] TestBody: example log: test error\n " ;
513550 fput_count = 0 ;
@@ -544,3 +581,23 @@ TEST_F(test, log_pmacros) {
544581 EXPECT_EQ (fput_count, expect_fput_count);
545582 EXPECT_EQ (fflush_count, expect_fflush_count);
546583}
584+
585+ // this test do not work on windows as we cannot mock abort on windows
586+ #ifndef _WIN32
587+ TEST_F (test, invalid_log_level) {
588+ fopen_return = MOCK_FILE_PTR;
589+ loggerConfig = {0 , 0 , LOG_DEBUG, LOG_DEBUG, stderr};
590+ #ifndef NDEBUG
591+ expect_fput_count = 2 ;
592+ expect_fflush_count = 2 ;
593+ #else
594+ expect_fput_count = 1 ;
595+ expect_fflush_count = 1 ;
596+ #endif
597+ // TODO: we do not check output message, as there will be two separate messages
598+ // which is not supported by this simple test framework.
599+ expected_message = " " ;
600+ helper_test_log ((utils_log_level_t )10 , MOCK_FN_NAME.c_str (), " %s" ,
601+ " example log" );
602+ }
603+ #endif
0 commit comments