tests: add negative unit tests for logctl 80/198880/1 accepted/tizen/unified/20190201.061322 submit/tizen/20190131.021840 submit/tizen/20190131.080207
authorMichal Bloch <m.bloch@samsung.com>
Wed, 30 Jan 2019 15:04:55 +0000 (16:04 +0100)
committerMichal Bloch <m.bloch@samsung.com>
Wed, 30 Jan 2019 15:28:23 +0000 (16:28 +0100)
Completes logctl coverage.

Change-Id: I3f2ab69d071361ca7f0573e86b2a2636db4f6e2c
Signed-off-by: Michal Bloch <m.bloch@samsung.com>
Makefile.am
src/tests/logctl.c

index 576c813..a0e01a6 100644 (file)
@@ -239,7 +239,7 @@ src_tests_queued_entry_LDFLAGS = $(AM_LDFLAGS) -Wl,--wrap=clock_gettime
 
 src_tests_logctl_SOURCES = src/tests/logctl.c src/logctl/logctl.c src/shared/parsers.c src/shared/logconfig.c src/libdlog/loglimiter.c src/shared/logcommon.c
 src_tests_logctl_CFLAGS = $(check_CFLAGS)
-src_tests_logctl_LDFLAGS = $(AM_LDFLAGS)
+src_tests_logctl_LDFLAGS = $(AM_LDFLAGS) -Wl,--wrap=calloc,--wrap=asprintf,--wrap=fopen,--wrap=fdopen,--wrap=mkstemp,--wrap=fchmod,--wrap=rename
 
 src_tests_config_SOURCES = src/tests/config.c src/shared/logconfig.c src/shared/logcommon.c src/shared/parsers.c
 src_tests_config_CFLAGS = $(check_CFLAGS)
index a5631d4..a57a82c 100644 (file)
@@ -6,6 +6,61 @@
 #include <logcommon.h>
 #include <logctl.h>
 
+static bool fail_calloc = false;
+extern void *__real_calloc(size_t nmemb, size_t size);
+void *__wrap_calloc(size_t nmemb, size_t size)
+{
+       return fail_calloc ? NULL : __real_calloc(nmemb, size);
+}
+
+static bool fail_asprintf = false;
+int __wrap_asprintf(char **strp, const char *fmt, ...)
+{
+       if (fail_asprintf)
+               return -1;
+
+       va_list va;
+       va_start(va, fmt);
+       const int ret = vasprintf(strp, fmt, va);
+       va_end(va);
+       return ret;
+}
+
+static bool fail_fopen;
+FILE *__real_fopen(const char *path, const char *mode);
+FILE *__wrap_fopen(const char *path, const char *mode)
+{
+       return fail_fopen ? NULL : __real_fopen(path, mode);
+}
+
+static bool fail_fdopen;
+FILE *__real_fdopen(int fd, const char *mode);
+FILE *__wrap_fdopen(int fd, const char *mode)
+{
+       return fail_fdopen ? NULL : __real_fdopen(fd, mode);
+}
+
+static bool fail_mkstemp;
+int __real_mkstemp(char *template);
+int __wrap_mkstemp(char *template)
+{
+       return fail_mkstemp ? -1 : __real_mkstemp(template);
+}
+
+static bool fail_fchmod;
+int __real_fchmod(const char *path, mode_t mode);
+int __wrap_fchmod(const char *path, mode_t mode)
+{
+       return fail_fchmod ? -1 : __real_fchmod(path, mode);
+}
+
+static bool fail_rename;
+int __real_rename(const char *oldpath, const char *newpath);
+int __wrap_rename(const char *oldpath, const char *newpath)
+{
+       return fail_rename ? -1 : __real_rename(oldpath, newpath);
+}
+
 static const char *const TMPFILE_PATH = "src/tests/logctl_testfile";
 
 void _prepare_file(const char *const *lines, size_t lines_cnt)
@@ -76,10 +131,25 @@ void test_copy_file_with_exceptions()
                kv.values[i] = kv_pairs[i].value;
        }
 
-       const int ret = copy_file_with_exceptions(TMPFILE_PATH, &kv);
-       assert(ret == EXIT_SUCCESS);
-
+       assert(EXIT_SUCCESS == copy_file_with_exceptions(TMPFILE_PATH, &kv));
        COMPARE_FILE(lines_out);
+
+#define TEST_FAIL(callname) \
+       fail_##callname = true; \
+       assert(EXIT_FAILURE == copy_file_with_exceptions(TMPFILE_PATH, &kv)); \
+       fail_##callname = false;
+
+       assert(EXIT_SUCCESS == copy_file_with_exceptions(TMPFILE_PATH, &kv));
+       TEST_FAIL(fopen);
+       TEST_FAIL(asprintf);
+       TEST_FAIL(mkstemp);
+       TEST_FAIL(fdopen);
+       TEST_FAIL(fchmod);
+       TEST_FAIL(calloc);
+       TEST_FAIL(rename);
+       assert(EXIT_SUCCESS == copy_file_with_exceptions(TMPFILE_PATH, &kv));
+
+#undef TEST_FAIL
 }
 
 void test_handle_clear()
@@ -143,6 +213,12 @@ void test_handle_set()
        pp.value = NULL;
        assert(EXIT_SUCCESS == handle_set(&pp, TMPFILE_PATH, NULL));
        COMPARE_FILE(lines_clear);
+
+       assert(EXIT_SUCCESS == handle_set(&pp, TMPFILE_PATH, NULL));
+       fail_asprintf = true;
+       assert(EXIT_FAILURE == handle_set(&pp, TMPFILE_PATH, NULL));
+       fail_asprintf = false;
+       assert(EXIT_SUCCESS == handle_set(&pp, TMPFILE_PATH, NULL));
 }
 
 void test_handle_plog()
@@ -199,6 +275,16 @@ void test_handle_plog()
        pp.plog_buffers_bitset = 0;
        assert(EXIT_SUCCESS == handle_plog(&pp, TMPFILE_PATH, NULL));
        COMPARE_FILE(lines_all);
+
+       fail_calloc = true;
+       assert(EXIT_FAILURE == handle_plog(&pp, TMPFILE_PATH, NULL));
+       fail_calloc = false;
+
+       fail_asprintf = true;
+       assert(EXIT_FAILURE == handle_plog(&pp, TMPFILE_PATH, NULL));
+       fail_asprintf = false;
+
+       assert(EXIT_SUCCESS == handle_plog(&pp, TMPFILE_PATH, NULL));
 }
 
 struct expected_opts {