]> git.droids-corp.org - protos/libecoli.git/commitdiff
testcase for logs
authorOlivier Matz <zer0@droids-corp.org>
Mon, 12 Mar 2018 21:42:39 +0000 (22:42 +0100)
committerOlivier Matz <zer0@droids-corp.org>
Mon, 12 Mar 2018 21:42:39 +0000 (22:42 +0100)
lib/ecoli_log.c

index 152ba8628f5541f4fa3542025e12efb3b8b7225b..3f1525a79aba362e61509d50202e05b8d383b589 100644 (file)
@@ -7,10 +7,14 @@
 #include <stdlib.h>
 #include <string.h>
 #include <errno.h>
+#include <syslog.h>
 
 #include <ecoli_malloc.h>
 #include <ecoli_string.h>
 #include <ecoli_log.h>
+#include <ecoli_test.h>
+
+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);