From: Bruno Haible Date: Thu, 10 Aug 2023 11:08:48 +0000 (+0200) Subject: boot-time: Add tests. X-Git-Tag: v1.0~940 X-Git-Url: https://gitweb.git.savannah.gnu.org/gitweb/?a=commitdiff_plain;h=c1a4224957f8bfc6ad38224398a79dabf6e981c6;p=gnulib.git boot-time: Add tests. * tests/test-boot-time.c: New file. * modules/boot-time-tests: New file. --- diff --git a/ChangeLog b/ChangeLog index cb3c937797..c2a67df01f 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,9 @@ 2023-08-10 Bruno Haible + 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 index 0000000000..0ba9d467ca --- /dev/null +++ b/modules/boot-time-tests @@ -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 index 0000000000..291514019a --- /dev/null +++ b/tests/test-boot-time.c @@ -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 . */ + +/* Written by Bruno Haible , 2023. */ + +#include + +#include "boot-time.h" + +#include +#include +#include + +#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; +}