]> Savannah Git Hosting - gnulib.git/commitdiff
boot-time: Add tests.
authorBruno Haible <bruno@clisp.org>
Thu, 10 Aug 2023 11:08:48 +0000 (13:08 +0200)
committerBruno Haible <bruno@clisp.org>
Thu, 10 Aug 2023 11:08:48 +0000 (13:08 +0200)
* tests/test-boot-time.c: New file.
* modules/boot-time-tests: New file.

ChangeLog
modules/boot-time-tests [new file with mode: 0644]
tests/test-boot-time.c [new file with mode: 0644]

index cb3c9377974c7b443057ffe9062b3e499dec47f6..c2a67df01f743d5e7c36dca41df960244390bc1f 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,9 @@
 2023-08-10  Bruno Haible  <bruno@clisp.org>
 
+       boot-time: Add tests.
+       * tests/test-boot-time.c: New file.
+       * modules/boot-time-tests: New file.
+
        boot-time: New module.
        * lib/boot-time.h: New file.
        * lib/boot-time.c: New file.
diff --git a/modules/boot-time-tests b/modules/boot-time-tests
new file mode 100644 (file)
index 0000000..0ba9d46
--- /dev/null
@@ -0,0 +1,11 @@
+Files:
+tests/test-boot-time.c
+
+Depends-on:
+
+configure.ac:
+
+Makefile.am:
+TESTS += test-boot-time
+check_PROGRAMS += test-boot-time
+test_boot_time_LDADD = $(LDADD) $(READUTMP_LIB)
diff --git a/tests/test-boot-time.c b/tests/test-boot-time.c
new file mode 100644 (file)
index 0000000..2915140
--- /dev/null
@@ -0,0 +1,50 @@
+/* Test of getting the boot time.
+   Copyright (C) 2023 Free Software Foundation, Inc.
+
+   This program is free software: you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation, either version 3 of the License, or
+   (at your option) any later version.
+
+   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, see <https://www.gnu.org/licenses/>.  */
+
+/* Written by Bruno Haible <bruno@clisp.org>, 2023.  */
+
+#include <config.h>
+
+#include "boot-time.h"
+
+#include <stddef.h>
+#include <stdio.h>
+#include <time.h>
+
+#include "macros.h"
+
+int
+main (int argc, char *argv[])
+{
+  struct timespec boot_time;
+  ASSERT (get_boot_time (&boot_time) == 0);
+
+  time_t tim = boot_time.tv_sec;
+  struct tm *gmt = gmtime (&tim);
+  ASSERT (gmt != NULL);
+  char timbuf[100];
+  ASSERT (strftime (timbuf, sizeof (timbuf), "%Y-%m-%d %H:%M:%S", gmt) > 0);
+
+  printf ("Boot time (UTC): %s.%09ld\n", timbuf, (long) boot_time.tv_nsec);
+
+  /* If the boot time is more than 5 years in the past or more than a week
+     in the future, the value must be wrong.  */
+  time_t now = time (NULL);
+  ASSERT (tim >= now - 157680000);
+  ASSERT (tim <= now + 604800);
+
+  return 0;
+}