Debian Codegraph @

madrisan-nagios-plugins-linux/nagios-plugins-linux/tests/tslibfiles_filecount.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 <fcntl.h>
24 #include <stdio.h>
25 #include <sys/stat.h>
26 #include <unistd.h>
27
28 #include "testutils.h"
29 #include "xasprintf.h"
30
31 # define NPL_TESTING
32 # include "../lib/files.c"
33 # undef NPL_TESTING
34
35 typedef struct test_data
36 {
37 char *basedir;
38 unsigned int flags;
39 int64_t age;
40 int64_t size;
41 char *pattern;
42 int64_t expect_value;
43 } test_data;
44
45 typedef struct environment
46 {
47 char *name;
48 char *target;
49 unsigned int flags;
50 } test_environment;
51
52 static test_environment const env[] = {
53 {(char *) "1", NULL, S_IFREG },
54 {(char *) "2", NULL, S_IFREG },
55 {(char *) "3", NULL, S_IFREG },
56 {(char *) "4", NULL, S_IFREG },
57 {(char *) "5", NULL, S_IFREG },
58 {(char *) "6", NULL, S_IFREG },
59 {(char *) "link1", "1", S_IFLNK},
60 {(char *) ".hiddenfile", NULL, S_IFREG },
61 {(char *) "a", NULL, S_IFDIR},
62 {(char *) "a/.hiddendir", NULL, S_IFDIR},
63 {(char *) "a/1", NULL, S_IFREG },
64 {(char *) "a/2", NULL, S_IFREG },
65 {(char *) "a/3", NULL, S_IFREG },
66 {(char *) "a/b", NULL, S_IFDIR},
67 {(char *) "a/link2", "2", S_IFLNK},
68 {(char *) "a/b/1", NULL, S_IFREG},
69 {(char *) "a/b/2", NULL, S_IFREG},
70 {(char *) "a/b/3", NULL, S_IFREG},
71 {(char *) "a/b/4", NULL, S_IFREG},
72 {(char *) "a/b/link3", "../3", S_IFLNK},
73 {NULL, 0}
74 };
75
76 static int
77 test_files_filecount (const void *tdata)
78 {
79 const struct test_data *data = tdata;
80 struct files_types *filecount = NULL;
81 int ret = 0;
82
83 ret = files_filecount (data->basedir, data->flags, data->age, data->size,
84 data->pattern, &filecount);
85 if (ret < 0)
86 return EXIT_AM_HARDFAIL;
87
88 TEST_ASSERT_EQUAL_NUMERIC (filecount->total, data->expect_value);
89
90 free (filecount);
91 return ret;
92 }
93
94 static int
95 test_create_tree (char **basedir)
96 {
97 char template[] = "/tmp/tslibfiles_filecount.XXXXXX";
98 test_environment e = env[0];
99 int i = 0;
100
101 *basedir = mkdtemp (template);
102 if (NULL == basedir)
103 {
104 perror ("mkdtemp failed in test_create_tree ()");
105 return EXIT_AM_HARDFAIL;
106 }
107 printf ("\tenvironment: %s\n", *basedir);
108
109 while (e.name)
110 {
111 char *path = xasprintf ("%s/%s", *basedir, e.name);
112 switch (e.flags)
113 {
114 default:
115 perror ("reached default in test_create_tree ()");
116 return EXIT_AM_HARDFAIL;
117 case S_IFDIR:
118 printf ("\t- directory: %s\n", e.name);
119 if (mkdir (path, S_IRWXU) < 0)
120 {
121 perror ("- mkdir failed in test_create_tree ()");
122 return EXIT_AM_HARDFAIL;
123 }
124 break;
125 case S_IFLNK:
126 printf ("\t- symlink : %s\n", e.name);
127 if (symlink (e.target, path) == -1)
128 {
129 perror ("symlink failed in test_create_tree ()");
130 return EXIT_AM_HARDFAIL;
131 }
132 break;
133 case S_IFREG:
134 printf ("\t- reg file : %s\n", e.name);
135 if ((open (path, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR)) == -1)
136 {
137 perror ("open failed in test_create_tree ()");
138 return EXIT_AM_HARDFAIL;
139 }
140 break;
141 }
142
143 free (path);
144 e = env[++i];
145 }
146
147 return 0;
148 }
149
150 static int
151 mymain (void)
152 {
153 int ret = 0;
154 char *basedir;
155
156 # define DO_TEST(TEST, BASEDIR, FLAGS, AGE, SIZE, PATTERN, EXPECT_VALUE) \
157 do \
158 { \
159 test_data data = { \
160 .basedir = BASEDIR, \
161 .flags = FLAGS, \
162 .age = AGE, \
163 .size = SIZE, \
164 .pattern = PATTERN, \
165 .expect_value = EXPECT_VALUE, \
166 }; \
167 if (test_run("check function files_filecount (" TEST ")", \
168 test_files_filecount, (&data)) < 0) \
169 ret = -1; \
170 } \
171 while (0)
172
173 /* test the function files_filecount() */
174
175 ret = test_create_tree (&basedir);
176 if (ret != 0)
177 return ret;
178
179 DO_TEST ("default",
180 basedir,
181 FILES_DEFAULT,
182 0, 0, NULL, 8);
183 DO_TEST ("default + hidden",
184 basedir,
185 FILES_DEFAULT | FILES_INCLUDE_HIDDEN,
186 0, 0, NULL, 9);
187 DO_TEST ("default - symlinks",
188 basedir,
189 FILES_DEFAULT | FILES_IGNORE_SYMLINKS,
190 0, 0, NULL, 7);
191 DO_TEST ("regular only",
192 basedir,
193 FILES_REGULAR_ONLY,
194 0, 0, NULL, 6);
195 DO_TEST ("regular + hidden",
196 basedir,
197 FILES_REGULAR_ONLY | FILES_INCLUDE_HIDDEN,
198 0, 0, NULL, 7);
199 DO_TEST ("regular + symlinks",
200 basedir,
201 FILES_REGULAR_ONLY | FILES_IGNORE_SYMLINKS,
202 0, 0, NULL, 6);
203 DO_TEST ("recursive",
204 basedir,
205 FILES_RECURSIVE,
206 0, 0, NULL, 18);
207 DO_TEST ("recursive + regular",
208 basedir,
209 FILES_RECURSIVE | FILES_REGULAR_ONLY,
210 0, 0, NULL, 13);
211 DO_TEST ("recursive + hidden",
212 basedir,
213 FILES_RECURSIVE | FILES_INCLUDE_HIDDEN,
214 0, 0, NULL, 20);
215 DO_TEST ("recursive - symlinks",
216 basedir,
217 FILES_RECURSIVE | FILES_IGNORE_SYMLINKS,
218 0, 0, NULL, 15);
219 DO_TEST ("recursive + hidden - symlinks",
220 basedir,
221 FILES_RECURSIVE | FILES_INCLUDE_HIDDEN | FILES_IGNORE_SYMLINKS,
222 0, 0, NULL, 17);
223
224 return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
225 }
226
227 TEST_MAIN (mymain)