Debian Codegraph @

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

raw symbols SCIP
1 // SPDX-License-Identifier: GPL-3.0-or-later
2 /*
3 * License: GPLv3+
4 * Copyright (c) 2016,2018 Davide Madrisan <[email protected]>
5 *
6 * Basic test utilities for the Nagios Plugin Linux.
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 * This software is based on the source code of the tool "vmstat".
22 */
23
24 #ifndef _GNU_SOURCE
25 # define _GNU_SOURCE /* activate extra prototypes for glibc */
26 #endif
27
28 #include <errno.h>
29 #include <stdarg.h>
30 #include <stdio.h>
31 #include <sys/stat.h>
32 #include <unistd.h>
33
34 #include "common.h"
35 #include "progname.h"
36 #include "system.h"
37 #include "testutils.h"
38 #include "xalloc.h"
39
40 static size_t test_counter;
41
42 /* Check that a file is regular and has executable bits.
43 * If false is returned, errno is valid. */
44 static bool
45 test_file_is_executable (const char *file)
46 {
47 struct stat sb;
48
49 if (stat (file, &sb) < 0)
50 return false;
51
52 if (S_ISREG (sb.st_mode) && (sb.st_mode & 0111) != 0)
53 return true;
54
55 errno = S_ISDIR (sb.st_mode) ? EISDIR : EACCES;
56 return false;
57 }
58
59 static int
60 test_use_terminal_colors (void)
61 {
62 return isatty (STDIN_FILENO);
63 }
64
65 char *
66 test_fstringify (const char * filename)
67 {
68 char * buffer = NULL;
69 long size;
70 size_t chars;
71 FILE * stream = fopen (filename, "r");
72
73 if (NULL == stream)
74 return NULL;
75
76 fseek (stream, 0, SEEK_END);
77 if ((size = ftell (stream)) < 0)
78 {
79 fclose (stream);
80 return NULL;
81 }
82
83 rewind (stream);
84
85 buffer = xmalloc ((size_t)size + 1);
86 chars = fread (buffer, 1, (size_t)size, stream);
87
88 if (ferror (stream) || (chars < (size_t)size))
89 {
90 free (buffer);
91 buffer = NULL;
92 }
93
94 fclose (stream);
95 return buffer;
96 }
97
98 int
99 test_main (int argc, char **argv, int (*func) (void), ...)
100 {
101 const char *lib;
102 va_list ap;
103 int ret;
104
105 va_start (ap, func);
106 while ((lib = va_arg(ap, const char *)))
107 TEST_PRELOAD (lib);
108 va_end(ap);
109
110 program_name = argv[0];
111 ret = (func) ();
112
113 return ret;
114 }
115
116 int
117 test_run (const char *title, int (*body) (const void *data), const void *data)
118 {
119 int ret = body (data);
120 test_counter++;
121
122 fprintf (stderr, "%2zu) %-65s ... ", test_counter, title);
123
124 if (test_use_terminal_colors ())
125 fprintf (stderr, "%s\n",
126 (ret == 0) ? "\e[32mOK\e[0m" : /* green */
127 (ret == EXIT_AM_SKIP) ? "\e[34m\e[1mSKIP\e[0m" : /* bold blue */
128 (ret == EXIT_AM_HARDFAIL) ? /* bold red */
129 "\e[31m\e[1mHARDFAIL\e[0m" : "\e[31m\e[1mFAILED\e[0m");
130 else
131 fprintf (stderr, "%s\n",
132 (ret == 0) ? "OK" :
133 (ret == EXIT_AM_SKIP) ? "SKIP" :
134 (ret == EXIT_AM_HARDFAIL) ? "HARDFAIL" : "FAILED");
135
136 return ret;
137 }