Re: [chrony-dev] [PATCH v3 5/5] test/unit: add leapdb test

2024-01-29 Thread Miroslav Lichvar
On Mon, Jan 29, 2024 at 04:29:00PM +0100, Miroslav Lichvar wrote:
> On Thu, Dec 07, 2023 at 01:17:16PM +1100, [email protected] wrote:
> > +void
> > +test_unit(void)
> > +{
> > +  char conf[][100] = {
> > +"leapsectz right/UTC",
> > +"leapseclist /usr/share/zoneinfo/leap-seconds.list"
> > +  };
> 
> The test should not fail on non-glibc systems and when the list file is
> missing. It should skip instead. 

For testing the list parsing, maybe it's best to include a cut-down
list with only few entries and few lines of comment (not the full 10KB
file) as leapdb.list directly in the unit test directory, similarly to
the ntp_core.keys file.

-- 
Miroslav Lichvar


-- 
To unsubscribe email [email protected] with "unsubscribe" 
in the subject.
For help email [email protected] with "help" in the 
subject.
Trouble?  Email [email protected].



Re: [chrony-dev] [PATCH v3 5/5] test/unit: add leapdb test

2024-01-29 Thread Miroslav Lichvar
On Thu, Dec 07, 2023 at 01:17:16PM +1100, [email protected] wrote:
> diff --git a/test/unit/leapdb.c b/test/unit/leapdb.c

> +struct test_vector {
> +  time_t when;
> +  int tai_offset;
> +  NTP_Leap leap;
> +} tests[] = {
> +  /* From leap-seconds.list */
> +  {2272060800, 10, LEAP_Normal}, // 1 Jan 1972

Please use the classic C comment syntax /* */.

> +void
> +test_unit(void)
> +{
> +  char conf[][100] = {
> +"leapsectz right/UTC",
> +"leapseclist /usr/share/zoneinfo/leap-seconds.list"
> +  };

The test should not fail on non-glibc systems and when the list file is
missing. It should skip instead. 

-- 
Miroslav Lichvar


-- 
To unsubscribe email [email protected] with "unsubscribe" 
in the subject.
For help email [email protected] with "help" in the 
subject.
Trouble?  Email [email protected].



[chrony-dev] [PATCH v3 5/5] test/unit: add leapdb test

2023-12-06 Thread patrick . oppenlander
From: Patrick Oppenlander 

---
 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 000..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 
+#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 [email protected] with "unsubscribe" 
in the subject.
For help email [email protected] with "help" in the 
subject.
Trouble?  Email [email protected].