Add helper sources for unit tests 08/140808/11
authorKonrad Kuchciak <k.kuchciak@samsung.com>
Wed, 26 Jul 2017 11:07:06 +0000 (13:07 +0200)
committerKonrad Kuchciak <k.kuchciak@samsung.com>
Thu, 3 Aug 2017 14:11:05 +0000 (16:11 +0200)
Change-Id: Ie9ebf2ae5bae6e33fde763792e26f416d6834f51
Signed-off-by: Konrad Kuchciak <k.kuchciak@samsung.com>
[Fix string generation]
Signed-off-by: Krzysztof Opasiak <k.opasiak@samsung.com>
Makefile.am
tests/unit/helpers.c [new file with mode: 0644]
tests/unit/helpers.h [new file with mode: 0644]

index f3e35b3ec2888be5d91f002593844dc61f8ebfdd..37ede1dc154e910a2aac03e6b2e2bffdada86b9d 100644 (file)
@@ -126,13 +126,22 @@ check_PROGRAMS = test
 
 test_LDFLAGS = -ldl
 
-test_LDADD = $(CMOCKA_LIBS)
+test_LDADD = $(CMOCKA_LIBS) $(LIBSYSTEMD_LIBS) $(AUDIT_LIBS) \
+    $(LIBEJDB_LIBS) $(JSON_C_LIBS)
 
 test_CFLAGS = -I${top_srcdir}/src/util
 
 test_SOURCES = \
     tests/unit/runner.c \
-    tests/unit/example.c
+    tests/unit/helpers.c \
+    src/core/event.c \
+    src/core/database.c \
+    src/core/service.c \
+    src/core/module.c \
+    src/core/faultd-config.c \
+    src/util/systemd_dbus.c \
+    src/util/log.c \
+    src/util/common.c
 
 TESTS = $(check_PROGRAMS)
 endif
diff --git a/tests/unit/helpers.c b/tests/unit/helpers.c
new file mode 100644 (file)
index 0000000..77dbaec
--- /dev/null
@@ -0,0 +1,84 @@
+#include "helpers.h"
+
+int faultd_object_count_children(struct faultd_object *obj)
+{
+       struct faultd_object *child;
+       int i = 0;
+
+       list_for_each_entry(child, &obj->val.children, node)
+               i++;
+
+       return i;
+}
+
+char generate_char()
+{
+       /* generate random char in range 0x20-0x7E (' ' to '~') excluding '.' */
+       char c;
+
+       while (1) {
+               c = ' ' + (rand() % 94);
+               if (c != '.')
+                       break;
+       }
+
+       return c;
+}
+
+char *generate_string(int len)
+{
+       char *str;
+
+       str = calloc(len + 1, sizeof(char));
+       if (!str)
+               return NULL;
+
+       for (int i = 0; i < len; ++i) {
+               str[i] = generate_char();
+
+       }
+       str[len] = '\0';
+
+       return str;
+}
+
+int generate_int()
+{
+       return rand();
+}
+
+time_t generate_time_t()
+{
+       return generate_int();
+}
+
+bson_oid_t generate_bson_oid_t()
+{
+       bson_oid_t oid;
+
+       for (int i = 0; i < ARRAY_SIZE(oid.ints); ++i)
+               oid.ints[i] = generate_int();
+       return oid;
+}
+
+sd_id128_t generate_uuid()
+{
+       return SD_ID128_MAKE(bb, 21, cd, 23, bd, 6c, 43, d6, 51, e4, e5, 7d, ab, 03, 09, 07);
+}
+
+faultd_oid_t generate_oid()
+{
+       faultd_oid_t oid;
+
+       oid.uuid = generate_uuid();
+       return oid;
+}
+
+struct timespec generate_timespec()
+{
+       struct timespec t;
+
+       t.tv_sec = generate_int();
+       t.tv_nsec = generate_int();
+       return t;
+}
diff --git a/tests/unit/helpers.h b/tests/unit/helpers.h
new file mode 100644 (file)
index 0000000..bfc1e6c
--- /dev/null
@@ -0,0 +1,22 @@
+#ifndef FAULTD_HELPERS_H
+#define FAULTD_HELPERS_H
+
+#include "fdtf.h"
+
+#include <time.h>
+#include <stdio.h>
+#include <systemd/sd-id128.h>
+#include <systemd/sd-bus.h>
+
+int faultd_object_count_children(struct faultd_object *obj);
+
+char generate_char();
+char *generate_string(int len);
+int generate_int();
+time_t generate_time_t();
+bson_oid_t generate_bson_oid_t();
+sd_id128_t generate_uuid();
+faultd_oid_t generate_oid();
+struct timespec generate_timespec();
+
+#endif /* FAULTD_UNIT_H */