Debian Codegraph @

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

raw symbols SCIP
1 // SPDX-License-Identifier: GPL-3.0-or-later
2 /*
3 * License: GPLv3+
4 * Copyright (c) 2022 Davide Madrisan <[email protected]>
5 *
6 * Unit test for lib/files.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 # define NPL_TESTING
26 # include "../lib/files.c"
27 # undef NPL_TESTING
28
29 typedef struct test_data
30 {
31 int64_t age;
32 time_t filemtime;
33 time_t now;
34 bool expect_value;
35 } test_data;
36
37 static int
38 test_files_check_age (const void *tdata)
39 {
40 const struct test_data *data = tdata;
41 int ret = 0;
42
43 bool check = files_check_age (data->age, data->now, data->filemtime);
44 TEST_ASSERT_EQUAL_NUMERIC (check, data->expect_value);
45 return ret;
46 }
47
48 static int
49 mymain (void)
50 {
51 int ret = 0;
52
53 # define DO_TEST(AGE, NOW, FILEMTIME, EXPECT_VALUE) \
54 do \
55 { \
56 test_data data = { \
57 .age = AGE, \
58 .filemtime = FILEMTIME, \
59 .now = NOW, \
60 .expect_value = EXPECT_VALUE, \
61 }; \
62 if (test_run("check function files_check_age", \
63 test_files_check_age, (&data)) < 0) \
64 ret = -1; \
65 } \
66 while (0)
67
68 /* test the function files_check_age() */
69
70 int64_t age = 3600 * 8; /* 8 hours ago */
71 time_t now;
72
73 now = time (NULL);
74
75 DO_TEST (0, now, 0, true);
76
77 DO_TEST (+age, now, now - (age + 1), true);
78 DO_TEST (+age, now, now - (age - 1), false);
79 DO_TEST (-age, now, now - (age - 1), true);
80 DO_TEST (-age, now, now - (age + 1), false);
81
82 return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
83 }
84
85 TEST_MAIN (mymain)