]> git.ipfire.org Git - thirdparty/chrony.git/commitdiff
test: fix sources unit test
authorMiroslav Lichvar <mlichvar@redhat.com>
Thu, 4 Jun 2020 09:13:38 +0000 (11:13 +0200)
committerMiroslav Lichvar <mlichvar@redhat.com>
Thu, 4 Jun 2020 12:50:17 +0000 (14:50 +0200)
Use different source addresses, fix a debug message and a memory leak.

test/unit/sources.c

index 606fcabcbdd95c9dbff437f9f3923f44b6a9da35..fdc70c01f5e8ddbe327884b7b725232fabd433f0 100644 (file)
 #include "test.h"
 
 static SRC_Instance
-create_source(SRC_Type type, int authenticated, int sel_options)
+create_source(SRC_Type type, IPAddr *addr, int authenticated, int sel_options)
 {
-  static IPAddr addr;
+  TST_GetRandomAddress(addr, IPADDR_UNSPEC, -1);
 
-  TST_GetRandomAddress(&addr, IPADDR_UNSPEC, -1);
-
-  return SRC_CreateNewInstance(UTI_IPToRefid(&addr), type, authenticated, sel_options,
-                               type == SRC_NTP ? &addr : NULL,
+  return SRC_CreateNewInstance(UTI_IPToRefid(addr), type, authenticated, sel_options,
+                               type == SRC_NTP ? addr : NULL,
                                SRC_DEFAULT_MINSAMPLES, SRC_DEFAULT_MAXSAMPLES, 0.0, 1.0);
 }
 
@@ -38,6 +36,7 @@ test_unit(void)
 {
   SRC_AuthSelectMode sel_mode;
   SRC_Instance srcs[16];
+  IPAddr addrs[16];
   RPT_SourceReport report;
   NTP_Sample sample;
   int i, j, k, l, n1, n2, n3, n4, samples, sel_options;
@@ -63,7 +62,7 @@ test_unit(void)
                                     SRC_SELECT_TRUST | SRC_SELECT_REQUIRE);
 
       DEBUG_LOG("added source %d options %d", j, sel_options);
-      srcs[j] = create_source(SRC_NTP, 0, sel_options);
+      srcs[j] = create_source(SRC_NTP, &addrs[j], 0, sel_options);
       SRC_UpdateReachability(srcs[j], 1);
 
       samples = (i + j) % 5 + 3;
@@ -127,7 +126,7 @@ test_unit(void)
           }
         }
 
-        DEBUG_LOG("sources %d passed %d trusted %d/%d required %d/%d", j, passed,
+        DEBUG_LOG("sources %d passed %d trusted %d/%d required %d/%d", j + 1, passed,
                   trusted_passed, trusted, required_passed, required);
 
         TEST_CHECK(!trusted || !passed || (passed_lo >= trusted_lo && passed_hi <= trusted_hi));
@@ -178,13 +177,13 @@ test_unit(void)
     assert(n1 + n2 + n3 < sizeof (srcs) / sizeof (srcs[0]));
 
     for (j = 0; j < n1; j++)
-      srcs[j] = create_source(SRC_REFCLOCK, random() % 2, sel_options);
+      srcs[j] = create_source(SRC_REFCLOCK, &addrs[j], random() % 2, sel_options);
     for (; j < n1 + n2; j++)
-      srcs[j] = create_source(SRC_NTP, 1, sel_options);
+      srcs[j] = create_source(SRC_NTP, &addrs[j], 1, sel_options);
     for (; j < n1 + n2 + n3; j++)
-      srcs[j] = create_source(SRC_NTP, 0, sel_options);
+      srcs[j] = create_source(SRC_NTP, &addrs[j], 0, sel_options);
     for (; j < n1 + n2 + n3 + n4; j++)
-      srcs[j] = create_source(random() % 2 ? SRC_REFCLOCK : SRC_NTP,
+      srcs[j] = create_source(random() % 2 ? SRC_REFCLOCK : SRC_NTP, &addrs[j],
                               random() % 2, sel_options | SRC_SELECT_NOSELECT);
 
     switch (sel_mode) {
@@ -218,7 +217,7 @@ test_unit(void)
     for (j = n1 + n2 + n3; j < n1 + n2 + n3 + n4; j++)
       TEST_CHECK(srcs[j]->sel_options == (sel_options | SRC_SELECT_NOSELECT));
 
-    for (j = n1 + n2 + n3 - 1; j >= 0; j--) {
+    for (j = n1 + n2 + n3 + n4 - 1; j >= 0; j--) {
       if (j < n1 + n2)
         TEST_CHECK(srcs[j]->sel_options == sel_options);
       SRC_DestroyInstance(srcs[j]);