Debian Codegraph @

madrisan-nagios-plugins-linux/nagios-plugins-linux/tests/tsuptime.c

raw symbols SCIP
1 // SPDX-License-Identifier: GPL-3.0-or-later
2 /*
3 * License: GPLv3+
4 * Copyright (c) 2017 Davide Madrisan <[email protected]>
5 *
6 * Unit test for check_uptime.c
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 *
21 */
22
23 #include "testutils.h"
24
25 /* silence the compiler's warning 'function defined but not used' */
26 static _Noreturn void print_version (void) __attribute__((unused));
27 static _Noreturn void usage (FILE * out) __attribute__((unused));
28 static double uptime_sysinfo () __attribute__((unused));
29 #if HAVE_CLOCK_GETTIME_MONOTONIC
30 static double uptime_clock_monotonic () __attribute__((unused));
31 #endif
32
33 #define NPL_TESTING
34 # include "../plugins/check_uptime.c"
35 #undef NPL_TESTING
36
37 struct test_data
38 {
39 double secs;
40 char *output;
41 };
42
43 static int
44 test_uptime_output (const void *tdata)
45 {
46 int ret = 0;
47 const struct test_data *data = tdata;
48
49 TEST_ASSERT_EQUAL_STRING (sprint_uptime (data->secs), data->output);
50 return ret;
51 }
52
53 #define ONE_MINUTE 60
54 #define ONE_HOUR (60*ONE_MINUTE)
55 #define ONE_DAY (24*ONE_HOUR)
56 #define ONE_MONTH (30*ONE_DAY)
57
58 static int
59 mymain (void)
60 {
61 int ret = 0;
62 struct test_data data;
63
64 #define DO_TEST(SECS, UPTIME_STR) \
65 do \
66 { \
67 data.secs = SECS; \
68 data.output = UPTIME_STR; \
69 if (test_run ("check uptime output of "#SECS" secs", \
70 test_uptime_output, &data) < 0) \
71 ret = -1; \
72 } \
73 while (0)
74
75 DO_TEST (59, "0 min");
76 DO_TEST (3*ONE_HOUR+2*ONE_MINUTE, "3 hours 2 min");
77 DO_TEST (1*ONE_DAY+1*ONE_HOUR+25*ONE_MINUTE, "1 day 1 hour 25 min");
78 DO_TEST (9*ONE_MONTH+4*ONE_HOUR+10*ONE_MINUTE, "270 days 4 hours 10 min");
79
80 return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
81 }
82
83 #undef ONE_MONTH
84 #undef ONE_DAY
85 #undef ONE_HOUR
86 #undef ONE_MINUTE
87
88 TEST_MAIN (mymain);