]> git.ipfire.org Git - thirdparty/chrony.git/commitdiff
test: add ntp_sources unit test
authorMiroslav Lichvar <mlichvar@redhat.com>
Fri, 5 Feb 2016 13:46:03 +0000 (14:46 +0100)
committerMiroslav Lichvar <mlichvar@redhat.com>
Fri, 5 Feb 2016 14:20:40 +0000 (15:20 +0100)
test/unit/ntp_sources.c [new file with mode: 0644]

diff --git a/test/unit/ntp_sources.c b/test/unit/ntp_sources.c
new file mode 100644 (file)
index 0000000..c16d5ae
--- /dev/null
@@ -0,0 +1,99 @@
+/*
+ **********************************************************************
+ * 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 <ntp_sources.c>
+#include <conf.h>
+#include <ntp_io.h>
+#include "test.h"
+
+void
+test_unit(void)
+{
+  int i, j, k, slot, found;
+  uint32_t hash = 0;
+  NTP_Remote_Address addrs[256], addr;
+  SourceParameters params;
+  char conf[] = "port 0";
+
+  memset(&params, 0, sizeof (params));
+
+  CNF_Initialise(0);
+  CNF_ParseLine(NULL, 1, conf);
+
+  LCL_Initialise();
+  SCH_Initialise();
+  SRC_Initialise();
+  NIO_Initialise(IPADDR_UNSPEC);
+  NCR_Initialise();
+  NSR_Initialise();
+
+  for (i = 0; i < 6; i++) {
+    TEST_CHECK(ARR_GetSize(records) == 1);
+
+    DEBUG_LOG(0, "collision mod %u", 1U << i);
+
+    for (j = 0; j < sizeof (addrs) / sizeof (addrs[0]); j++) {
+      do {
+        get_random_address(&addrs[j].ip_addr, IPADDR_UNSPEC, -1);
+      } while (UTI_IPToHash(&addrs[j].ip_addr) % (1U << i) != hash % (1U << i));
+
+      addrs[j].port = random() % 1024;
+
+      if (!j)
+        hash = UTI_IPToHash(&addrs[j].ip_addr);
+
+      DEBUG_LOG(0, "adding source %s hash %"PRIu32, UTI_IPToString(&addrs[j].ip_addr),
+                UTI_IPToHash(&addrs[j].ip_addr) % (1U << i));
+
+      NSR_AddSource(&addrs[j], random() % 2 ? NTP_SERVER : NTP_PEER, &params);
+
+      for (k = 0; k < j; k++) {
+        addr = addrs[k];
+        find_slot(&addr, &slot, &found);
+        TEST_CHECK(found == 2);
+        TEST_CHECK(!UTI_CompareIPs(&get_record(slot)->remote_addr->ip_addr,
+                                   &addr.ip_addr, NULL));
+        addr.port++;
+        find_slot(&addr, &slot, &found);
+        TEST_CHECK(found == 1);
+        TEST_CHECK(!UTI_CompareIPs(&get_record(slot)->remote_addr->ip_addr,
+                                   &addr.ip_addr, NULL));
+      }
+    }
+
+    for (j = 0; j < sizeof (addrs) / sizeof (addrs[0]); j++) {
+      DEBUG_LOG(0, "removing source %s", UTI_IPToString(&addrs[j].ip_addr));
+      NSR_RemoveSource(&addrs[j]);
+
+      for (k = 0; k < sizeof (addrs) / sizeof (addrs[0]); k++) {
+        find_slot(&addrs[k], &slot, &found);
+        TEST_CHECK(found == (k <= j ? 0 : 2));
+      }
+    }
+  }
+
+  NSR_Finalise();
+  NCR_Finalise();
+  NIO_Finalise();
+  SRC_Finalise();
+  SCH_Finalise();
+  LCL_Finalise();
+  CNF_Finalise();
+}