From: Olivier Matz Date: Mon, 12 Mar 2018 21:42:39 +0000 (+0100) Subject: testcase for logs X-Git-Url: http://git.droids-corp.org/?a=commitdiff_plain;h=38e5638bb3b4e2e8a409184d4f07977594a5e773;p=protos%2Flibecoli.git testcase for logs --- diff --git a/lib/ecoli_log.c b/lib/ecoli_log.c index 152ba86..3f1525a 100644 --- a/lib/ecoli_log.c +++ b/lib/ecoli_log.c @@ -7,10 +7,14 @@ #include #include #include +#include #include #include #include +#include + +EC_LOG_TYPE_REGISTER(log); static ec_log_t ec_log_fct = ec_log_default_cb; static void *ec_log_opaque; @@ -147,3 +151,80 @@ int ec_log(int type, enum ec_log_level level, const char *format, ...) return ret; } + +/* LCOV_EXCL_START */ +static int +log_cb(int type, enum ec_log_level level, void *opaque, const char *str) +{ + (void)type; + (void)level; + (void)str; + *(int *)opaque = 1; + + return 0; +} + +static int ec_log_testcase(void) +{ + ec_log_t prev_log_cb; + void *prev_opaque; + const char *logname; + int testres = 0; + int check_cb = 0; + int logtype; + int level; + int ret; + + prev_log_cb = ec_log_fct; + prev_opaque = ec_log_opaque; + + ret = ec_log_fct_register(log_cb, &check_cb); + testres |= EC_TEST_CHECK(ret == 0, + "cannot register log function\n"); + EC_LOG(LOG_ERR, "test\n"); + testres |= EC_TEST_CHECK(check_cb == 1, + "log callback was not invoked\n"); + logtype = ec_log_lookup("dsdedesdes"); + testres |= EC_TEST_CHECK(logtype == -1, + "lookup invalid name should return -1"); + logtype = ec_log_lookup("log"); + logname = ec_log_name(logtype); + testres |= EC_TEST_CHECK(logname != NULL && + !strcmp(logname, "log"), + "cannot get log name\n"); + logname = ec_log_name(-1); + testres |= EC_TEST_CHECK(logname != NULL && + !strcmp(logname, "unknown"), + "cannot get invalid log name\n"); + logname = ec_log_name(34324); + testres |= EC_TEST_CHECK(logname != NULL && + !strcmp(logname, "unknown"), + "cannot get invalid log name\n"); + level = ec_log_level_get(); + ret = ec_log_level_set(2); + testres |= EC_TEST_CHECK(ret == 0 && ec_log_level_get() == 2, + "cannot set log level\n"); + ret = ec_log_level_set(10); + testres |= EC_TEST_CHECK(ret != 0, + "should not be able to set log level\n"); + + ret = ec_log_fct_register(NULL, NULL); + ec_log_level_set(LOG_DEBUG); + EC_LOG(LOG_DEBUG, "test log\n"); + ec_log_level_set(LOG_INFO); + EC_LOG(LOG_DEBUG, "test log (not displayed)\n"); + ec_log_level_set(level); + + ec_log_fct = prev_log_cb; + ec_log_opaque = prev_opaque; + + return testres; +} +/* LCOV_EXCL_STOP */ + +static struct ec_test ec_log_test = { + .name = "log", + .test = ec_log_testcase, +}; + +EC_TEST_REGISTER(ec_log_test);