Skip to content

Commit bc40dc2

Browse files
committed
increase logger test coverage to 100%
1 parent 9bf1316 commit bc40dc2

File tree

2 files changed

+64
-1
lines changed

2 files changed

+64
-1
lines changed

src/utils/utils_sanitizers.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@
2121
#define __SANITIZE_ADDRESS__ 1
2222
#endif
2323
#endif
24+
#if __has_feature(undefined)
25+
#ifndef __SANITIZE_UNDEFINED__
26+
#define __SANITIZE_UNDEFINED__ 1
27+
#endif
28+
#endif
2429
#endif
2530

2631
#if UMF_VG_ENABLED

test/utils/utils_log.cpp

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@
1010
std::string expected_filename;
1111
int expect_fopen_count = 0;
1212
int fopen_count = 0;
13+
FILE *fopen_return = MOCK_FILE_PTR;
1314

1415
FILE *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

2122
const 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
}
119127
using umf_test::test;
120128

@@ -139,6 +147,7 @@ void helper_checkConfig(utils_log_config_t *expected, utils_log_config_t *is) {
139147
TEST_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) {
164173
TEST_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

254278
template <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

280304
TEST_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) {
313339
TEST_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) {
332359
TEST_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) {
347375
TEST_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) {
357386
TEST_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) {
428460
TEST_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,24 @@ 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 doesn't work on windows as we cannot mock abort on windows
586+
// also it is not supported if UndefinedBehaviorSanitizer is enabled
587+
#if !defined(_WIN32) && !defined(__SANITIZE_UNDEFINED__)
588+
TEST_F(test, invalid_log_level) {
589+
fopen_return = MOCK_FILE_PTR;
590+
loggerConfig = {0, 0, LOG_DEBUG, LOG_DEBUG, stderr};
591+
#ifndef NDEBUG
592+
expect_fput_count = 2;
593+
expect_fflush_count = 2;
594+
#else
595+
expect_fput_count = 1;
596+
expect_fflush_count = 1;
597+
#endif
598+
// TODO: we do not check output message, as there will be two separate messages
599+
// which is not supported by this simple test framework.
600+
expected_message = "";
601+
helper_test_log((utils_log_level_t)10, MOCK_FN_NAME.c_str(), "%s",
602+
"example log");
603+
}
604+
#endif

0 commit comments

Comments
 (0)