]> git.ipfire.org Git - thirdparty/chrony.git/commitdiff
test: add support for unit testing
authorMiroslav Lichvar <mlichvar@redhat.com>
Fri, 5 Feb 2016 08:52:46 +0000 (09:52 +0100)
committerMiroslav Lichvar <mlichvar@redhat.com>
Fri, 5 Feb 2016 14:20:40 +0000 (15:20 +0100)
Makefile.in
configure
test/unit/Makefile.in [new file with mode: 0644]
test/unit/test.c [new file with mode: 0644]
test/unit/test.h [new file with mode: 0644]

index 6ac0c9e6fcb91516c601d4b7e04d1d970e82d9a2..c87f67f353db398212c24a57112a6523593f8152 100644 (file)
@@ -120,6 +120,7 @@ install: chronyd chronyc
        $(CC) $(CFLAGS) $(CPPFLAGS) -S $<
 
 check : chronyd chronyc
+       $(MAKE) -C test/unit check
        cd test/simulation && ./run
 
 install-docs : docs
index 8608c8dc37a7b73407dc0cbb23581236dc653c6b..144e30daa459f07d1d541b62585f617191530ee5 100755 (executable)
--- a/configure
+++ b/configure
@@ -848,7 +848,7 @@ fi
 
 add_def CHRONY_VERSION "\"${CHRONY_VERSION}\""
 
-for f in Makefile chrony.conf.5 chrony.texi chronyc.1 chronyd.8
+for f in Makefile test/unit/Makefile chrony.conf.5 chrony.texi chronyc.1 chronyd.8
 do
   echo Creating $f
   sed -e "s%@EXTRA_OBJECTS@%${EXTRA_OBJECTS}%;\
diff --git a/test/unit/Makefile.in b/test/unit/Makefile.in
new file mode 100644 (file)
index 0000000..5fd925c
--- /dev/null
@@ -0,0 +1,42 @@
+TEST_WRAPPER =
+CHRONY_SRCDIR = ../..
+
+CC = @CC@
+CFLAGS = @CFLAGS@
+CPPFLAGS = -I$(CHRONY_SRCDIR) @CPPFLAGS@
+LDFLAGS = @LDFLAGS@ @LIBS@ @EXTRA_LIBS@
+
+SHARED_OBJS = test.o
+
+TEST_OBJS := $(sort $(patsubst %.c,%.o,$(wildcard *.c)))
+TESTS := $(patsubst %.o,%.test,$(filter-out $(SHARED_OBJS),$(TEST_OBJS)))
+
+FILTER_OBJS = %/main.o %/client.o %/getdate.o
+CHRONY_OBJS := $(filter-out $(FILTER_OBJS),$(wildcard $(CHRONY_SRCDIR)/*.o))
+
+all: $(TESTS)
+
+%.test: %.o $(SHARED_OBJS)
+       $(CC) $(CFLAGS) -o $@ $^ $(CHRONY_OBJS:%/$*.o=) $(LDFLAGS)
+
+%.o: %.c
+       $(CC) $(CPPFLAGS) $(CFLAGS) -c $<
+
+check: $(TESTS)
+       @ret=0; \
+       for t in $^; do \
+         $(TEST_WRAPPER) ./$$t || ret=1; \
+       done; \
+       exit $$ret
+
+clean:
+       rm -f *.o $(TESTS)
+       rm -rf .deps
+
+.deps:
+       @mkdir .deps
+
+.deps/%.d: %.c | .deps
+       @$(CC) -MM $(CPPFLAGS) -MT '$(<:%.c=%.o) $@' $< -o $@
+
+-include $(TEST_OBJS:%.o=.deps/%.d)
diff --git a/test/unit/test.c b/test/unit/test.c
new file mode 100644 (file)
index 0000000..1a7bc4a
--- /dev/null
@@ -0,0 +1,119 @@
+/*
+ **********************************************************************
+ * Copyright (C) Miroslav Lichvar  2016
+ * 
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of version 2 of the GNU General Public License as
+ * published by the Free Software Foundation.
+ * 
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ * 
+ **********************************************************************
+ */
+
+#include <config.h>
+#include <sysincl.h>
+#include <logging.h>
+
+#include "test.h"
+
+void
+test_fail(int line)
+{
+  printf("FAIL (on line %d)\n", line);
+  exit(1);
+}
+
+int
+main(int argc, char **argv)
+{
+  char *test_name, *s;
+  int i, seed = 0;
+
+  test_name = argv[0];
+  s = strrchr(test_name, '.');
+  if (s)
+    *s = '\0';
+  s = strrchr(test_name, '/');
+  if (s)
+    test_name = s + 1;
+
+  for (i = 1; i < argc; i++) {
+    if (!strcmp(argv[i], "-d")) {
+      LOG_SetDebugLevel(2);
+    } else if (!strcmp(argv[i], "-s") && i + 1 < argc) {
+      seed = atoi(argv[++i]);
+    } else {
+      fprintf(stderr, "Unknown option\n");
+      exit(1);
+    }
+  }
+
+  srandom(seed ? seed : time(NULL));
+
+  printf("Testing %-30s ", test_name);
+  fflush(stdout);
+
+  test_unit();
+
+  printf("PASS\n");
+
+  return 0;
+}
+
+void
+get_random_address(IPAddr *ip, int family, int bits)
+{
+  if (family != IPADDR_INET4 && family != IPADDR_INET6)
+    family = random() % 2 ? IPADDR_INET4 : IPADDR_INET6;
+
+  ip->family = family;
+
+  if (family == IPADDR_INET4) {
+    if (bits < 0)
+      bits = 32;
+    assert(bits <= 32);
+
+    if (bits > 16)
+      ip->addr.in4 = (uint32_t)random() % (1U << (bits - 16)) << 16 |
+                     (uint32_t)random() % (1U << 16);
+    else
+      ip->addr.in4 = (uint32_t)random() % (1U << bits);
+  } else {
+    int i, b;
+
+    if (bits < 0)
+      bits = 128;
+    assert(bits <= 128);
+
+    for (i = 0, b = 120; i < 16; i++, b -= 8) {
+      if (b >= bits) {
+        ip->addr.in6[i] = 0;
+      } else {
+        ip->addr.in6[i] = random() % (1U << MIN(bits - b, 8));
+      }
+    }
+  }
+}
+
+void
+swap_address_bit(IPAddr *ip, unsigned int b)
+{
+  if (ip->family == IPADDR_INET4) {
+    assert(b < 32);
+    ip->addr.in4 ^= 1U << (31 - b);
+  } else if (ip->family == IPADDR_INET6) {
+    assert(b < 128);
+    ip->addr.in6[b / 8] ^= 1U << (7 - b % 8);
+  } else {
+    assert(0);
+  }
+}
+
diff --git a/test/unit/test.h b/test/unit/test.h
new file mode 100644 (file)
index 0000000..d1e83cb
--- /dev/null
@@ -0,0 +1,41 @@
+/*
+ **********************************************************************
+ * Copyright (C) Miroslav Lichvar  2016
+ * 
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of version 2 of the GNU General Public License as
+ * published by the Free Software Foundation.
+ * 
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ * 
+ **********************************************************************
+ */
+
+#ifndef GOT_TEST_H
+#define GOT_TEST_H
+
+#include <addressing.h>
+
+extern void test_unit(void);
+
+#define TEST_CHECK(expr) \
+  do { \
+    if (!(expr)) { \
+      test_fail(__LINE__); \
+      exit(1); \
+    } \
+  } while (0)
+
+extern void test_fail(int line);
+
+extern void get_random_address(IPAddr *ip, int family, int bits);
+extern void swap_address_bit(IPAddr *ip, unsigned int b);
+
+#endif