#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)
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()
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()
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 {