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,13 @@ const char *env_variable = "";
110111#ifndef UMF_VERSION
111112#define UMF_VERSION " test version"
112113#endif
114+ #define abort ()
113115#include " utils/utils_log.c"
114116#undef utils_env_var
115117#undef fopen
116118#undef fputs
117119#undef fflush
120+ #undef abort
118121}
119122using umf_test::test;
120123
@@ -139,6 +142,7 @@ void helper_checkConfig(utils_log_config_t *expected, utils_log_config_t *is) {
139142TEST_F (test, parseEnv_errors) {
140143 expected_message = " " ;
141144 loggerConfig = {0 , 0 , LOG_ERROR, LOG_ERROR, NULL };
145+ fopen_return = MOCK_FILE_PTR;
142146
143147 expect_fput_count = 0 ;
144148 expected_stream = stderr;
@@ -164,6 +168,7 @@ TEST_F(test, parseEnv_errors) {
164168TEST_F (test, parseEnv) {
165169 utils_log_config_t b = loggerConfig;
166170 expected_message = " " ;
171+ fopen_return = MOCK_FILE_PTR;
167172
168173 std::vector<std::pair<std::string, int >> logLevels = {
169174 {" level:debug" , LOG_DEBUG},
@@ -250,6 +255,20 @@ TEST_F(test, parseEnv) {
250255 }
251256 }
252257}
258+ TEST_F (test, fopen_fail) {
259+ expected_stream = stderr;
260+ expect_fopen_count = 1 ;
261+ loggerConfig = {0 , 0 , LOG_ERROR, LOG_ERROR, NULL };
262+ utils_log_config_t b = loggerConfig = {0 , 0 , LOG_ERROR, LOG_ERROR, NULL };
263+
264+ expected_filename = " filepath" ;
265+ expected_message = " [ERROR UMF] utils_log_init: Cannot open output file "
266+ " filepath - logging disabled: \n " ;
267+
268+ fopen_return = NULL ;
269+ helper_log_init (" output:file,filepath" );
270+ helper_checkConfig (&b, &loggerConfig);
271+ }
253272
254273template <typename ... Args> void helper_test_log (Args... args) {
255274 fput_count = 0 ;
@@ -279,6 +298,7 @@ static std::string helper_log_str(int l) {
279298
280299TEST_F (test, log_levels) {
281300 expected_stream = stderr;
301+ fopen_return = MOCK_FILE_PTR;
282302 for (int i = LOG_DEBUG; i <= LOG_ERROR; i++) {
283303 for (int j = LOG_DEBUG; j <= LOG_ERROR; j++) {
284304 loggerConfig = {0 , 0 , (utils_log_level_t )i, LOG_DEBUG, stderr};
@@ -302,6 +322,7 @@ TEST_F(test, log_outputs) {
302322 std::vector<FILE *> outs = {stdout, stderr, MOCK_FILE_PTR};
303323 expect_fput_count = 1 ;
304324 expect_fflush_count = 1 ;
325+ fopen_return = MOCK_FILE_PTR;
305326 expected_message = " [DEBUG UMF] " + MOCK_FN_NAME + " : example log\n " ;
306327 for (auto o : outs) {
307328 loggerConfig = {0 , 0 , LOG_DEBUG, LOG_DEBUG, o};
@@ -313,6 +334,7 @@ TEST_F(test, log_outputs) {
313334TEST_F (test, flush_levels) {
314335 expected_stream = stderr;
315336 expect_fput_count = 1 ;
337+ fopen_return = MOCK_FILE_PTR;
316338 for (int i = LOG_DEBUG; i <= LOG_ERROR; i++) {
317339 for (int j = LOG_DEBUG; j <= LOG_ERROR; j++) {
318340 loggerConfig = {0 , 0 , LOG_DEBUG, (utils_log_level_t )i, stderr};
@@ -332,6 +354,7 @@ TEST_F(test, flush_levels) {
332354TEST_F (test, long_log) {
333355 expect_fput_count = 1 ;
334356 expect_fflush_count = 1 ;
357+ fopen_return = MOCK_FILE_PTR;
335358 loggerConfig = {0 , 0 , LOG_DEBUG, LOG_DEBUG, stderr};
336359 expected_message = " [DEBUG UMF] " + MOCK_FN_NAME + " : " +
337360 std::string (8189 - MOCK_FN_NAME.size (), ' x' ) + " \n " ;
@@ -347,6 +370,7 @@ TEST_F(test, long_log) {
347370TEST_F (test, timestamp_log) {
348371 expect_fput_count = 1 ;
349372 expect_fflush_count = 1 ;
373+ fopen_return = MOCK_FILE_PTR;
350374 loggerConfig = {1 , 0 , LOG_DEBUG, LOG_DEBUG, stderr};
351375 // TODO: for now we do not check output message,
352376 // as it requires more sophisticated message validation (a.k.a regrex)
@@ -357,6 +381,7 @@ TEST_F(test, timestamp_log) {
357381TEST_F (test, pid_log) {
358382 expect_fput_count = 1 ;
359383 expect_fflush_count = 1 ;
384+ fopen_return = MOCK_FILE_PTR;
360385 loggerConfig = {0 , 1 , LOG_DEBUG, LOG_DEBUG, stderr};
361386 // TODO: for now we do not check output message,
362387 // as it requires more sophisticated message validation (a.k.a regrex)
@@ -369,6 +394,7 @@ TEST_F(test, log_fatal) {
369394 expected_stream = stderr;
370395 expect_fput_count = 1 ;
371396 expect_fflush_count = 1 ;
397+ fopen_return = MOCK_FILE_PTR;
372398
373399 expected_message = " [FATAL UMF] " + MOCK_FN_NAME + " : example log\n " ;
374400 strerror_ret_static = 0 ;
@@ -379,6 +405,7 @@ TEST_F(test, log_macros) {
379405 expected_stream = stderr;
380406 expect_fput_count = 1 ;
381407 expect_fflush_count = 1 ;
408+ fopen_return = MOCK_FILE_PTR;
382409 loggerConfig = {0 , 0 , LOG_DEBUG, LOG_DEBUG, stderr};
383410
384411 expected_message = " [DEBUG UMF] TestBody: example log\n " ;
@@ -428,6 +455,7 @@ template <typename... Args> void helper_test_plog(Args... args) {
428455TEST_F (test, plog_basic) {
429456 loggerConfig = {0 , 0 , LOG_DEBUG, LOG_DEBUG, stderr};
430457 expected_stream = stderr;
458+ fopen_return = MOCK_FILE_PTR;
431459 errno = 1 ;
432460 strerr = " test error" ;
433461 expect_fput_count = 1 ;
@@ -448,6 +476,7 @@ TEST_F(test, plog_invalid) {
448476 strerr = " test error" ;
449477 expect_fput_count = 1 ;
450478 expect_fflush_count = 1 ;
479+ fopen_return = MOCK_FILE_PTR;
451480
452481 expected_message =
453482 " [DEBUG UMF] " + MOCK_FN_NAME + " : example log: unknown error\n " ;
@@ -465,6 +494,7 @@ TEST_F(test, plog_long_message) {
465494 strerror_ret_static = 0 ;
466495 strerr = " test error" ;
467496 errno = 1 ;
497+ fopen_return = MOCK_FILE_PTR;
468498
469499 expected_message = " [DEBUG UMF] " + MOCK_FN_NAME + " : " +
470500 std::string (8178 - MOCK_FN_NAME.length (), ' x' ) +
@@ -487,6 +517,7 @@ TEST_F(test, plog_long_error) {
487517 std::string tmp = std::string (2000 , ' x' );
488518 strerr = tmp.c_str ();
489519 errno = 1 ;
520+ fopen_return = MOCK_FILE_PTR;
490521#ifdef WIN32
491522 /* On windows limit is shorter, and there is no truncated detection*/
492523 expected_message = " [DEBUG UMF] " + MOCK_FN_NAME +
@@ -508,6 +539,7 @@ TEST_F(test, log_pmacros) {
508539 loggerConfig = {0 , 0 , LOG_DEBUG, LOG_DEBUG, stderr};
509540 errno = 1 ;
510541 strerr = " test error" ;
542+ fopen_return = MOCK_FILE_PTR;
511543
512544 expected_message = " [DEBUG UMF] TestBody: example log: test error\n " ;
513545 fput_count = 0 ;
@@ -544,3 +576,20 @@ TEST_F(test, log_pmacros) {
544576 EXPECT_EQ (fput_count, expect_fput_count);
545577 EXPECT_EQ (fflush_count, expect_fflush_count);
546578}
579+
580+ TEST_F (test, invalid_log_level) {
581+ fopen_return = MOCK_FILE_PTR;
582+ loggerConfig = {0 , 0 , LOG_DEBUG, LOG_DEBUG, stderr};
583+ #ifndef NDEBUG
584+ expect_fput_count = 2 ;
585+ expect_fflush_count = 2 ;
586+ #else
587+ expect_fput_count = 1 ;
588+ expect_fflush_count = 1 ;
589+ #endif
590+ // TODO: we do not check output message, as there will be two separate messages
591+ // which is not supported by this simple test framework.
592+ expected_message = " " ;
593+ helper_test_log ((utils_log_level_t )10 , MOCK_FN_NAME.c_str (), " %s" ,
594+ " example log" );
595+ }
0 commit comments