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

2024-02-11 Thread Miroslav Lichvar
On Mon, Feb 12, 2024 at 08:20:46AM +1100, Patrick Oppenlander wrote:
> Thanks for resolving those remaining issues. Looks like I should have
> been running a few more tests beyond "make check". How do you run the
> static analyser?

There is test/compilation/002-scanbuild in the repository to run
the clang static analyzer in few different chrony configurations.

I also run a Coverity scan we have access to at Red Hat before each
release. I think they provide a free service for open source projects,
but I'm not sure how that works.

-- 
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 v5 5/5] test/unit: add leapdb test

2024-02-11 Thread Patrick Oppenlander
On Fri, Feb 9, 2024 at 2:28 AM Miroslav Lichvar  wrote:
>
> On Thu, Feb 08, 2024 at 02:36:29PM +1100, [email protected] wrote:
> > +} tests[] = {
> > +  /* leapdb.list is a cut down version of leap-seconds.list */
> > +  {3439756800, 34, LEAP_InsertSecond, 0}, /* 1 Jan 2009 */
> > +  {3550089600, 35, LEAP_InsertSecond, 0}, /* 1 Jul 2012 */
> > +  {3644697600, 36, LEAP_InsertSecond, 0}, /* 1 Jul 2015 */
> > +  {3692217600, 37, LEAP_InsertSecond, 0}, /* 1 Jan 2017 */
> > +  {34711891200, 36, LEAP_DeleteSecond, 1}, /* 1 Jan 3000 fake in 
> > leapdb.list */
>
> This value is too large to fit in 32-bit time_t. I changed it to 1 Jan
> 2020.

Ah, I didn't think of that as all of the platforms I work with are
64-bit time_t and have been for a few years now.

> There was also an issue I found after I updated the other tests. The
> refclock tai option checked only for leapsectz. And there was a new
> issue reported by static analyzer about uninitialized value, probably
> due to not being able to follow the new function pointers.

Thanks for resolving those remaining issues. Looks like I should have
been running a few more tests beyond "make check". How do you run the
static analyser?

Thanks for getting it merged,

Patrick

> Thanks,
>
> --
> 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].
>

--
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 v5 5/5] test/unit: add leapdb test

2024-02-08 Thread Miroslav Lichvar
On Thu, Feb 08, 2024 at 02:36:29PM +1100, [email protected] wrote:
> +} tests[] = {
> +  /* leapdb.list is a cut down version of leap-seconds.list */
> +  {3439756800, 34, LEAP_InsertSecond, 0}, /* 1 Jan 2009 */
> +  {3550089600, 35, LEAP_InsertSecond, 0}, /* 1 Jul 2012 */
> +  {3644697600, 36, LEAP_InsertSecond, 0}, /* 1 Jul 2015 */
> +  {3692217600, 37, LEAP_InsertSecond, 0}, /* 1 Jan 2017 */
> +  {34711891200, 36, LEAP_DeleteSecond, 1}, /* 1 Jan 3000 fake in leapdb.list 
> */

This value is too large to fit in 32-bit time_t. I changed it to 1 Jan
2020.

There was also an issue I found after I updated the other tests. The
refclock tai option checked only for leapsectz. And there was a new
issue reported by static analyzer about uninitialized value, probably
due to not being able to follow the new function pointers.

Thanks,

-- 
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 v5 5/5] test/unit: add leapdb test

2024-02-07 Thread patrick . oppenlander
From: Patrick Oppenlander 

---
 test/unit/leapdb.c| 104 ++
 test/unit/leapdb.list |  22 +
 2 files changed, 126 insertions(+)
 create mode 100644 test/unit/leapdb.c
 create mode 100644 test/unit/leapdb.list

diff --git a/test/unit/leapdb.c b/test/unit/leapdb.c
new file mode 100644
index 000..b41278a
--- /dev/null
+++ b/test/unit/leapdb.c
@@ -0,0 +1,104 @@
+/*
+ **
+ * 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;
+  int fake;
+} tests[] = {
+  /* leapdb.list is a cut down version of leap-seconds.list */
+  {3439756800, 34, LEAP_InsertSecond, 0}, /* 1 Jan 2009 */
+  {3550089600, 35, LEAP_InsertSecond, 0}, /* 1 Jul 2012 */
+  {3644697600, 36, LEAP_InsertSecond, 0}, /* 1 Jul 2015 */
+  {3692217600, 37, LEAP_InsertSecond, 0}, /* 1 Jan 2017 */
+  {34711891200, 36, LEAP_DeleteSecond, 1}, /* 1 Jan 3000 fake in leapdb.list */
+};
+
+static void
+test_leap_source(NTP_Leap (*fn)(time_t when, int *tai_offset),
+ int skip_fakes)
+{
+  int prev_tai_offset = 34;
+  for (int i = 0; i < sizeof tests / sizeof tests[0]; ++i) {
+struct test_vector *t = tests + i;
+
+NTP_Leap leap;
+int tai_offset = -1;
+
+/* Our unit test leapdb.list contains a fake entry removing a leap second.
+ * Skip this when testing with the right/UTC timezone using mktime(). */
+if (skip_fakes && t->fake)
+  continue;
+
+/* 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 leapdb.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();
+
+  if (check_leap_source(get_tz_leap)) {
+DEBUG_LOG("testing get_tz_leap");
+test_leap_source(get_tz_leap, 1);
+  } else {
+DEBUG_LOG("Skipping get_tz_leap test. Either the right/UTC timezone is "
+ "missing, or mktime() doesn't support leap seconds.");
+  }
+
+  DEBUG_LOG("testing get_list_leap");
+  TEST_CHECK(check_leap_source(get_list_leap));
+  test_leap_source(get_list_leap, 0);
+
+  /* This exercises the twice-per-day logic */
+  DEBUG_LOG("testing LDB_GetLeap");
+  test_leap_source(LDB_GetLeap, 1);
+
+  LDB_Finalise();
+  CNF_Finalise();
+}
diff --git a/test/unit/leapdb.list b/test/unit/leapdb.list
new file mode 100644
index 000..8fd2ae9
--- /dev/null
+++ b/test/unit/leapdb.list
@@ -0,0 +1,22 @@
+#
+# Cut down version of leap-seconds.list for unit test.
+#
+# Blank lines need to be ignored, so include a few for testing.
+# Whitespace errors on non-blank lines below are copied from the original file.
+#
+   
+#  Leap second data update time
+#$  3676924800
+#
+#  File update time
+#@ 3928521600
+ 
+3439756800 34  # 1 Jan 2009
+3550089600 35  # 1 Jul 2012
+3644697600 36  # 1 Jul 2015
+3692217600 37  # 1 Jan 2017
+3471189120036  # 1 Jan 3000 (fake entry to test negative leap second)
+
+#  FIPS 180-1 hash
+#  NOTE! this value has not been recomputed for this unit test file.
+#h 16edd0f0 3666784f 37db6bdd e74ced87 59af48f1
-- 
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].