Debian Codegraph @

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

raw symbols SCIP
1 // SPDX-License-Identifier: GPL-3.0-or-later
2 /*
3 * License: GPLv3+
4 * Copyright (c) 2016,2017 Davide Madrisan <[email protected]>
5 *
6 * Unit test for check_cswch.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 "system.h"
24 #include "testutils.h"
25
26 /* silence the compiler's warning 'function defined but not used' */
27 static _Noreturn void print_version (void) __attribute__((unused));
28 static _Noreturn void usage (FILE * out) __attribute__((unused));
29 static unsigned long long get_ctxtdelta (unsigned int, unsigned int, bool)
30 __attribute__((unused));
31
32 #define NPL_TESTING
33 # include "../plugins/check_cswch.c"
34 #undef NPL_TESTING
35
36 static bool
37 test_cswch_proc_stat_exists ()
38 {
39 FILE *fp;
40
41 if ((fp = fopen (get_path_proc_stat (), "r")) == NULL)
42 return false;
43
44 fclose (fp);
45 return true;
46 }
47
48 static int
49 test_cswch_proc_parsing ()
50 {
51 int ret = 0;
52 const char *env_variable = "NPL_TEST_PATH_PROCSTAT";
53
54 ret = setenv (env_variable, NPL_TEST_PATH_PROCSTAT, 1);
55 if (ret < 0)
56 return EXIT_AM_HARDFAIL;
57
58 /* next function will parse the line "ctxt 13817032" */
59 if (cpu_stats_get_cswch () != 13817032)
60 ret = -1;
61
62 unsetenv (env_variable);
63 return ret;
64 }
65
66 static int
67 mymain (void)
68 {
69 int ret = 0;
70
71 if (!test_cswch_proc_stat_exists ())
72 return EXIT_AM_SKIP;
73
74 #define DO_TEST(MSG, FUNC, DATA) \
75 do { if (test_run (MSG, FUNC, DATA) < 0) ret = -1; } while (0)
76
77 DO_TEST ("check for parsing errors in cpu_stats_get_cswch()",
78 test_cswch_proc_parsing, NULL);
79
80 return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
81 }
82
83 TEST_MAIN (mymain)