summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Makefile.am2
-rw-r--r--TODO.org10
-rw-r--r--configure.ac1
-rw-r--r--lib/Makefile.am2
-rw-r--r--t/Makefile.am5
-rw-r--r--t/test_cee_format.c135
6 files changed, 151 insertions, 4 deletions
diff --git a/Makefile.am b/Makefile.am
index a9f5bf7..edeecee 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -1,4 +1,4 @@
-SUBDIRS = lib
+SUBDIRS = lib t
ACLOCAL_AMFLAGS = -I m4 --install
EXTRA_DIST = LICENSE README.rst
diff --git a/TODO.org b/TODO.org
index cb06fc3..29e40f4 100644
--- a/TODO.org
+++ b/TODO.org
@@ -28,12 +28,18 @@ writing a new syslog(3) man page.
*** MAYBE Perhaps export cee_legacy_syslog(), and write a man page for that?
-* TODO Test suite
+* DONE Test suite
+CLOSED: [2012-03-20 Tue 12:29]
+- CLOSING NOTE [2012-03-20 Tue 12:29] \\
+ Test suite is in place.
The test suite cannot test the syslog calls themselves, as that would
be way too hacky to do properly. So it will test cee_format() instead,
which is used by the syslog calls anyway.
-** TODO Decide how to test the library
+** DONE Decide how to test the library
+CLOSED: [2012-03-20 Tue 12:29]
+- CLOSING NOTE [2012-03-20 Tue 12:29] \\
+ It's decided, I'll do just that.
I believe the best way to test would be to re-parse the JSON, extract
the fields, and verify they're of the expected value and type.
diff --git a/configure.ac b/configure.ac
index 6f3cb9c..de3c8d8 100644
--- a/configure.ac
+++ b/configure.ac
@@ -58,4 +58,5 @@ AC_OUTPUT(
Makefile
lib/Makefile
lib/libcee-syslog.pc
+ t/Makefile
)
diff --git a/lib/Makefile.am b/lib/Makefile.am
index 0ced726..99ccf47 100644
--- a/lib/Makefile.am
+++ b/lib/Makefile.am
@@ -3,7 +3,7 @@ LCS_REVISION = 0
LCS_AGE = 0
lib_LTLIBRARIES = libcee-syslog.la
-libcee_syslog_la_LIBADD = @JSON_LIBS@
+libcee_syslog_la_LIBADD = @JSON_LIBS@ -ldl
libcee_syslog_la_CFLAGS = @JSON_CFLAGS@
libcee_syslog_la_SOURCES = \
diff --git a/t/Makefile.am b/t/Makefile.am
new file mode 100644
index 0000000..b0066c8
--- /dev/null
+++ b/t/Makefile.am
@@ -0,0 +1,5 @@
+TESTS = test_cee_format
+check_PROGRAMS = ${TESTS}
+
+AM_CFLAGS = -I$(top_srcdir)/lib @JSON_CFLAGS@
+LDADD = $(top_builddir)/lib/libcee-syslog.la @JSON_LIBS@
diff --git a/t/test_cee_format.c b/t/test_cee_format.c
new file mode 100644
index 0000000..d7f886e
--- /dev/null
+++ b/t/test_cee_format.c
@@ -0,0 +1,135 @@
+#include "cee-syslog.h"
+#include <json.h>
+#include <assert.h>
+#include <string.h>
+#include <stdlib.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;
+
+ openlog ("cee-syslog/test_simple", 0, LOG_LOCAL0);
+
+ msg = cee_format (LOG_DEBUG, "hello, I'm %s!", __FUNCTION__, NULL);
+ jo = parse_msg (msg);
+ free (msg);
+
+ verify_value (jo, "msg", "hello, I'm test_simple!");
+ verify_value (jo, "facility", "local0");
+ verify_value (jo, "priority", "debug");
+ verify_value (jo, "program", "cee-syslog/test_simple");
+ verify_value_exists (jo, "pid");
+ verify_value_exists (jo, "uid");
+ verify_value_exists (jo, "gid");
+
+ json_object_put (jo);
+
+ closelog ();
+}
+
+static void
+test_no_discover (void)
+{
+ char *msg;
+ struct json_object *jo;
+
+ openlog ("cee-syslog/test_no_discover", LOG_CEE_NODISCOVER, LOG_LOCAL0);
+
+ msg = cee_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");
+
+ json_object_put (jo);
+
+ closelog ();
+}
+
+static void
+test_additional_fields (void)
+{
+ char *msg;
+ struct json_object *jo;
+
+ openlog ("cee-syslog/test_additional_fields", 0, LOG_LOCAL0);
+
+ msg = cee_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 ();
+}
+
+int
+main (void)
+{
+ test_simple ();
+ test_no_discover ();
+ test_additional_fields ();
+
+ return 0;
+}