[chrony-dev] [PATCH v3 5/5] test/unit: add leapdb test |
[ Thread Index |
Date Index
| More chrony.tuxfamily.org/chrony-dev Archives
]
- To: chrony-dev@xxxxxxxxxxxxxxxxxxxx
- Subject: [chrony-dev] [PATCH v3 5/5] test/unit: add leapdb test
- From: patrick.oppenlander@xxxxxxxxx
- Date: Thu, 7 Dec 2023 13:17:16 +1100
- Cc: Patrick Oppenlander <patrick.oppenlander@xxxxxxxxx>
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1701915457; x=1702520257; darn=chrony.tuxfamily.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=eZnBMPoiIMFHLDSAqYqNCvYDePWPmtix3+JLJyEQDMc=; b=dDZqPz6iRRVH1wUJGyDhR2j/eyCvOcT8m0Fh+bVrsS0oZGJQuPz1tEcNJSq6wtIUK5 lfX9T6Rd3cClP88hTk/z+Qu3vOzYwqeXMO6HqiqWEJdQrQFlc1oaB3psSs3Mh7nSqSPY vOjtJ3TpvroNByCZDFT/+CVngiE+PJK6rdt1MUoajxuLa+aiiPLQPJElZHcfy4fECnZD p05icqvdoNaTbddZMEjrMDMvvll9p1pXoGw7zu52mex+Mf+D2Ma0ZF1L5WtmC3dbvtWY KzUX5DuXOCr6YfRVo+idsdtsAsZ0KbOOxs+Vve/ZSjmRPCkQ08mJAjOHqsKRDSNTs3uv 6LtQ==
From: Patrick Oppenlander <patrick.oppenlander@xxxxxxxxx>
---
test/unit/leapdb.c | 117 +++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 117 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..ac5d86d
--- /dev/null
+++ b/test/unit/leapdb.c
@@ -0,0 +1,117 @@
+/*
+ **********************************************************************
+ * 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(NTP_Leap (*fn)(time_t when, int *tai_offset))
+{
+ 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_SEC_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_SEC_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_SEC_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_leap_sec_list_leap");
+ test_leap_source(get_leap_sec_list_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-request@xxxxxxxxxxxxxxxxxxxx with "unsubscribe" in the subject.
For help email chrony-dev-request@xxxxxxxxxxxxxxxxxxxx with "help" in the subject.
Trouble? Email listmaster@xxxxxxxxxxxxxxxxxxxx.