From: Patrick Oppenlander <patrick.oppenlan...@gmail.com>

---
 test/unit/leapdb.c | 118 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 118 insertions(+)
 create mode 100644 test/unit/leapdb.c

diff --git a/test/unit/leapdb.c b/test/unit/leapdb.c
new file mode 100644
index 0000000..b1b7035
--- /dev/null
+++ b/test/unit/leapdb.c
@@ -0,0 +1,118 @@
+/*
+ **********************************************************************
+ * Copyright (C) Patrick Oppenlander 2023
+ *
+ * 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 <leapdb.c>
+#include "test.h"
+
+struct test_vector {
+  time_t when;
+  int tai_offset;
+  NTP_Leap leap;
+} tests[] = {
+  /* from leap-seconds.list */
+  {2272060800, 10, LEAP_Normal}, // 1 Jan 1972
+  {2287785600, 11, LEAP_InsertSecond}, // 1 Jul 1972
+  {2303683200, 12, LEAP_InsertSecond}, // 1 Jan 1973
+  {2335219200, 13, LEAP_InsertSecond}, // 1 Jan 1974
+  {2366755200, 14, LEAP_InsertSecond}, // 1 Jan 1975
+  {2398291200, 15, LEAP_InsertSecond}, // 1 Jan 1976
+  {2429913600, 16, LEAP_InsertSecond}, // 1 Jan 1977
+  {2461449600, 17, LEAP_InsertSecond}, // 1 Jan 1978
+  {2492985600, 18, LEAP_InsertSecond}, // 1 Jan 1979
+  {2524521600, 19, LEAP_InsertSecond}, // 1 Jan 1980
+  {2571782400, 20, LEAP_InsertSecond}, // 1 Jul 1981
+  {2603318400, 21, LEAP_InsertSecond}, // 1 Jul 1982
+  {2634854400, 22, LEAP_InsertSecond}, // 1 Jul 1983
+  {2698012800, 23, LEAP_InsertSecond}, // 1 Jul 1985
+  {2776982400, 24, LEAP_InsertSecond}, // 1 Jan 1988
+  {2840140800, 25, LEAP_InsertSecond}, // 1 Jan 1990
+  {2871676800, 26, LEAP_InsertSecond}, // 1 Jan 1991
+  {2918937600, 27, LEAP_InsertSecond}, // 1 Jul 1992
+  {2950473600, 28, LEAP_InsertSecond}, // 1 Jul 1993
+  {2982009600, 29, LEAP_InsertSecond}, // 1 Jul 1994
+  {3029443200, 30, LEAP_InsertSecond}, // 1 Jan 1996
+  {3076704000, 31, LEAP_InsertSecond}, // 1 Jul 1997
+  {3124137600, 32, LEAP_InsertSecond}, // 1 Jan 1999
+  {3345062400, 33, LEAP_InsertSecond}, // 1 Jan 2006
+  {3439756800, 34, LEAP_InsertSecond}, // 1 Jan 2009
+  {3550089600, 35, LEAP_InsertSecond}, // 1 Jul 2012
+  {3644697600, 36, LEAP_InsertSecond}, // 1 Jul 2015
+  {3692217600, 37, LEAP_InsertSecond}, // 1 Jan 2017
+};
+
+static void
+test_leap_source(GetLeapFn fn)
+{
+  /* make sure check_leap_source works */
+  TEST_CHECK(check_leap_source(fn));
+
+  /* test every leap second to date */
+  int prev_tai_offset = 10;
+  for (int i = 0; i < sizeof tests / sizeof tests[0]; ++i) {
+    struct test_vector *t = tests + i;
+
+    NTP_Leap leap;
+    int tai_offset = -1;
+
+    /* one second before leap second */
+    leap = fn(t->when - LEAP_SECONDS_LIST_OFFSET - 1, &tai_offset);
+    TEST_CHECK(leap == t->leap);
+    TEST_CHECK(tai_offset = prev_tai_offset);
+
+    /* exactly on leap second */
+    leap = fn(t->when - LEAP_SECONDS_LIST_OFFSET, &tai_offset);
+    TEST_CHECK(leap == LEAP_Normal);
+    TEST_CHECK(tai_offset == t->tai_offset);
+
+    /* one second after leap second */
+    leap = fn(t->when - LEAP_SECONDS_LIST_OFFSET + 1, &tai_offset);
+    TEST_CHECK(leap == LEAP_Normal);
+    TEST_CHECK(tai_offset == t->tai_offset);
+
+    prev_tai_offset = t->tai_offset;
+  }
+}
+
+void
+test_unit(void)
+{
+  char conf[][100] = {
+    "leapsectz right/UTC",
+    "leapseclist /usr/share/zoneinfo/leap-seconds.list"
+  };
+
+  CNF_Initialise(0, 0);
+  for (int i = 0; i < sizeof conf / sizeof conf[0]; i++)
+    CNF_ParseLine(NULL, i + 1, conf[i]);
+  LDB_Initialise();
+
+  DEBUG_LOG("testing get_tz_leap");
+  test_leap_source(get_tz_leap);
+
+  DEBUG_LOG("testing get_db_leap");
+  test_leap_source(get_db_leap);
+
+  /* this exercises the twice-per-day logic */
+  DEBUG_LOG("testing LDB_GetLeap");
+  test_leap_source(LDB_GetLeap);
+
+  LDB_Finalise();
+  CNF_Finalise();
+}
-- 
2.43.0


-- 
To unsubscribe email chrony-dev-requ...@chrony.tuxfamily.org with "unsubscribe" 
in the subject.
For help email chrony-dev-requ...@chrony.tuxfamily.org with "help" in the 
subject.
Trouble?  Email listmas...@chrony.tuxfamily.org.

Reply via email to