summaryrefslogtreecommitdiffstats
path: root/t/test_umberlog.c
diff options
context:
space:
mode:
authorGergely Nagy <algernon@balabit.hu>2012-03-23 10:08:16 +0100
committerGergely Nagy <algernon@balabit.hu>2012-03-23 10:10:26 +0100
commit30676376a578e1ff31862abcb4780b063b0284d0 (patch)
tree35dfdf791dd017dd66782334f45e56b28a13e333 /t/test_umberlog.c
parent63866194f6fcc9adc0c5c4d873f04f8cf2217963 (diff)
downloadlibumberlog-30676376a578e1ff31862abcb4780b063b0284d0.tar.gz
libumberlog-30676376a578e1ff31862abcb4780b063b0284d0.tar.xz
libumberlog-30676376a578e1ff31862abcb4780b063b0284d0.zip
Rename the project to libumberlog
Signed-off-by: Gergely Nagy <algernon@balabit.hu>
Diffstat (limited to 't/test_umberlog.c')
-rw-r--r--t/test_umberlog.c203
1 files changed, 203 insertions, 0 deletions
diff --git a/t/test_umberlog.c b/t/test_umberlog.c
new file mode 100644
index 0000000..d1c2911
--- /dev/null
+++ b/t/test_umberlog.c
@@ -0,0 +1,203 @@
+#define _GNU_SOURCE 1
+
+#include "umberlog.h"
+#include <json.h>
+#include <assert.h>
+#include <string.h>
+#include <stdlib.h>
+#include <sys/types.h>
+#include <unistd.h>
+#include <stdio.h>
+#include <limits.h>
+
+static void
+verify_value (struct json_object *jo, const char *key,
+ const char *expected_value)
+{
+ struct json_object *o;
+ const char *value;
+
+ o = json_object_object_get (jo, key);
+
+ assert (o != NULL);
+
+ value = json_object_get_string (o);
+
+ assert (value != NULL);
+ assert (strcmp (value, expected_value) == 0);
+}
+
+static void
+verify_value_exists (struct json_object *jo, const char *key)
+{
+ struct json_object *o;
+
+ o = json_object_object_get (jo, key);
+ assert (o != NULL);
+}
+
+static void
+verify_value_missing (struct json_object *jo, const char *key)
+{
+ struct json_object *o;
+
+ o = json_object_object_get (jo, key);
+ assert (o == NULL);
+}
+
+static struct json_object *
+parse_msg (const char *msg)
+{
+ struct json_object *jo;
+
+ jo = json_tokener_parse (msg);
+ assert (jo != NULL);
+
+ return jo;
+}
+
+static void
+test_simple (void)
+{
+ char *msg;
+ struct json_object *jo;
+ char host[HOST_NAME_MAX + 1];
+
+ openlog ("umberlog/test_simple", 0, LOG_LOCAL0);
+
+ msg = ul_format (LOG_DEBUG, "hello, I'm %s!", __FUNCTION__, NULL);
+ jo = parse_msg (msg);
+ free (msg);
+
+ gethostname (host, HOST_NAME_MAX);
+
+ verify_value (jo, "msg", "hello, I'm test_simple!");
+ verify_value (jo, "facility", "local0");
+ verify_value (jo, "priority", "debug");
+ verify_value (jo, "program", "umberlog/test_simple");
+ verify_value_exists (jo, "pid");
+ verify_value_exists (jo, "uid");
+ verify_value_exists (jo, "gid");
+ verify_value_exists (jo, "timestamp");
+ verify_value (jo, "host", host);
+
+ json_object_put (jo);
+
+ closelog ();
+}
+
+static void
+test_no_discover (void)
+{
+ char *msg;
+ struct json_object *jo;
+
+ openlog ("umberlog/test_no_discover", LOG_UL_NODISCOVER, LOG_LOCAL0);
+
+ msg = ul_format (LOG_DEBUG, "hello, I'm %s!", __FUNCTION__, NULL);
+ jo = parse_msg (msg);
+ free (msg);
+
+ verify_value (jo, "msg", "hello, I'm test_no_discover!");
+ verify_value_missing (jo, "facility");
+ verify_value_missing (jo, "priority");
+ verify_value_missing (jo, "program");
+ verify_value_missing (jo, "pid");
+ verify_value_missing (jo, "uid");
+ verify_value_missing (jo, "gid");
+ verify_value_missing (jo, "host");
+ verify_value_missing (jo, "timestamp");
+
+ json_object_put (jo);
+
+ closelog ();
+}
+
+static void
+test_additional_fields (void)
+{
+ char *msg;
+ struct json_object *jo;
+
+ openlog ("umberlog/test_additional_fields", 0, LOG_LOCAL0);
+
+ msg = ul_format (LOG_DEBUG, "testing 1, 2, 3...",
+ "random_number", "%d", 42,
+ "random_string", "fourty-two",
+ NULL);
+ jo = parse_msg (msg);
+ free (msg);
+
+ verify_value (jo, "msg", "testing 1, 2, 3...");
+ verify_value (jo, "random_number", "42");
+ verify_value (jo, "random_string", "fourty-two");
+
+ json_object_put (jo);
+
+ closelog ();
+}
+
+static void
+test_discover_priority (void)
+{
+ char *msg, *pid;
+ struct json_object *jo;
+
+ openlog ("umberlog/test_discover_priority", 0, LOG_LOCAL0);
+
+ msg = ul_format (LOG_DEBUG, "testing 1, 2, 3...",
+ "pid", "%d", getpid () + 42,
+ NULL);
+ jo = parse_msg (msg);
+ free (msg);
+
+ verify_value (jo, "msg", "testing 1, 2, 3...");
+
+ if (asprintf (&pid, "%d", getpid ()) == -1)
+ abort ();
+ verify_value (jo, "pid", pid);
+ free (pid);
+
+ json_object_put (jo);
+
+ closelog ();
+}
+
+static void
+test_no_timestamp (void)
+{
+ char *msg;
+ struct json_object *jo;
+
+ openlog ("umberlog/test_no_timestamp", LOG_UL_NOTIME, LOG_LOCAL0);
+
+ msg = ul_format (LOG_DEBUG, "hello, I'm %s!", __FUNCTION__, NULL);
+ jo = parse_msg (msg);
+ free (msg);
+
+ verify_value (jo, "msg", "hello, I'm test_no_timestamp!");
+ verify_value (jo, "facility", "local0");
+ verify_value (jo, "priority", "debug");
+ verify_value (jo, "program", "umberlog/test_no_timestamp");
+ verify_value_exists (jo, "pid");
+ verify_value_exists (jo, "uid");
+ verify_value_exists (jo, "gid");
+ verify_value_missing (jo, "timestamp");
+ verify_value_exists (jo, "host");
+
+ json_object_put (jo);
+
+ closelog ();
+}
+
+int
+main (void)
+{
+ test_simple ();
+ test_no_discover ();
+ test_additional_fields ();
+ test_discover_priority ();
+ test_no_timestamp ();
+
+ return 0;
+}