Add Unit test to poll compatibility layer
[lttng-tools.git] / tests / unit / test_utils_expand_path.c
1 /*
2 * Copyright (C) - 2013 Raphaƫl Beamonte <raphael.beamonte@gmail.com>
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by as
6 * published by the Free Software Foundation; only version 2 of the License.
7 *
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
12 *
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 51
15 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 */
17
18 #include <assert.h>
19 #include <string.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <limits.h>
23
24 #include <sys/stat.h>
25 #include <sys/types.h>
26
27 #include <tap/tap.h>
28
29 #include <common/utils.h>
30 #include <common/common.h>
31
32 /* For error.h */
33 int lttng_opt_quiet = 1;
34 int lttng_opt_verbose = 3;
35 int lttng_opt_mi;
36
37 struct valid_test_input {
38 char *input;
39 char *relative_part;
40 char *absolute_part;
41 };
42
43 struct tree_symlink {
44 char *orig;
45 char *dest;
46 };
47
48 struct symlink_test_input {
49 char *input;
50 char *expected_result;
51 };
52
53 /* Valid test cases */
54 static struct valid_test_input valid_tests_inputs[] = {
55 { "/a/b/c/d/e", "", "/a/b/c/d/e" },
56 { "/a//b//c/d/e", "", "/a/b/c/d/e" },
57 { "./a/b/c/d/e", ".", "/a/b/c/d/e" },
58 { "../a/b/c/d/../e", "..", "/a/b/c/e" },
59 { ".././a/b/c/d/./e", "..", "/a/b/c/d/e" },
60 { "../../a/b/c/d/e", "../..", "/a/b/c/d/e" },
61 { "./a/b/../c/d/../e", ".", "/a/c/e" },
62 { "../a/b/../../c/./d/./e", "..", "/c/d/e" },
63 { "../../a/b/../c/d/../../e", "../..", "/a/e" },
64 { "./a/b/c/d/../../../../e", ".", "/e" },
65 { ".././a/b/c/d/./e", "..", "/a/b/c/d/e" },
66 { "a/", ".", "/a/" },
67 { "a", ".", "/a" },
68 { "../../", "../..", "/" },
69 { "../..", "../..", "" },
70 { "../", "..", "/" },
71 { "..", "..", "" },
72 { "./", ".", "/" },
73 { ".", ".", "" },
74 { "/../a/b/c/d/e", "", "/a/b/c/d/e" },
75 { "/a/b/c/d/../../../../../e", "", "/e" },
76 { "/..", "", "/" },
77 { "/a/..", "", "/" },
78 };
79 char **valid_tests_expected_results;
80 static const int num_valid_tests =
81 sizeof(valid_tests_inputs) / sizeof(valid_tests_inputs[0]);
82
83 /* Symlinks test cases */
84 char tree_origin[] = "/tmp/test_utils_expand_path.XXXXXX";
85
86 static const char * const tree_dirs[] = {
87 "a",
88 "a/b",
89 "a/b/c",
90 "a/e",
91 };
92 static const int num_tree_dirs =
93 sizeof(tree_dirs) / sizeof(tree_dirs[0]);
94
95 static struct tree_symlink tree_symlinks[] = {
96 { "a/d", "b/c/" },
97 { "a/g", "d/" },
98 { "a/b/f", "../e/" },
99 { "a/b/h", "../g/" },
100 { "a/b/k", "c/g/" },
101 { "a/b/c/g", "../../../" },
102 };
103 static const int num_tree_symlinks =
104 sizeof(tree_symlinks) / sizeof(tree_symlinks[0]);
105
106 static struct symlink_test_input symlink_tests_inputs[] = {
107 { "a/g/../l/.", "a/b/l" },
108 { "a/g/../l/./", "a/b/l/" },
109 { "a/g/../l/..", "a/b" },
110 { "a/g/../l/../", "a/b/" },
111 { "a/b/h/g/", "" },
112 };
113 static const int num_symlink_tests =
114 sizeof(symlink_tests_inputs) / sizeof(symlink_tests_inputs[0]);
115
116 /* Invalid test cases */
117 static char *invalid_tests_inputs[] = {
118 NULL,
119 };
120 static const int num_invalid_tests =
121 sizeof(invalid_tests_inputs) / sizeof(invalid_tests_inputs[0]);
122
123 #define ERRSIZE 100
124 char errmsg[ERRSIZE];
125 static void printerr(char *msg)
126 {
127 fprintf(stderr, "test_utils_expand_path: error: %s\n", msg);
128 }
129
130 int prepare_valid_results(void)
131 {
132 int i;
133 char *relative, *cur_path = NULL, *prev_path = NULL,
134 *pprev_path = NULL, *empty = NULL;
135 int ret = 0;
136
137 /* Prepare the relative paths */
138 cur_path = realpath(".", NULL);
139 prev_path = realpath("..", NULL);
140 pprev_path = realpath("../..", NULL);
141 empty = strdup("");
142 if (!cur_path || !prev_path || !pprev_path || !empty) {
143 printerr("strdup out of memory");
144 ret = -1;
145 goto end;
146 }
147
148 /* allocate memory for the expected results */
149 valid_tests_expected_results = zmalloc(sizeof(char *) * num_valid_tests);
150 if (!valid_tests_expected_results) {
151 printerr("out of memory");
152 ret = -1;
153 goto end;
154 }
155 for (i = 0; i < num_valid_tests; i++) {
156 valid_tests_expected_results[i] = malloc(PATH_MAX);
157 if (valid_tests_expected_results[i] == NULL) {
158 printerr("malloc expected results");
159 ret = -1;
160 goto end;
161 }
162
163 if (strcmp(valid_tests_inputs[i].relative_part, ".") == 0) {
164 relative = cur_path;
165 } else if (strcmp(valid_tests_inputs[i].relative_part, "..") == 0) {
166 relative = prev_path;
167 } else if (strcmp(valid_tests_inputs[i].relative_part, "../..") == 0) {
168 relative = pprev_path;
169 } else {
170 relative = empty;
171 }
172
173 snprintf(valid_tests_expected_results[i], PATH_MAX,
174 "%s%s", relative, valid_tests_inputs[i].absolute_part);
175 }
176
177 end:
178 free(cur_path);
179 free(prev_path);
180 free(pprev_path);
181 free(empty);
182
183 return ret;
184 }
185
186 int free_valid_results(void)
187 {
188 int i;
189
190 for (i = 0; i < num_valid_tests; i++) {
191 free(valid_tests_expected_results[i]);
192 }
193
194 free(valid_tests_expected_results);
195
196 return 0;
197 }
198
199 int prepare_symlink_tree(void)
200 {
201 int i;
202 char tmppath[PATH_MAX];
203
204 /* Create the temporary directory */
205 if (mkdtemp(tree_origin) == NULL) {
206 printerr("mkdtemp");
207 goto error;
208 }
209
210 /* Create the directories of the test tree */
211 for (i = 0; i < num_tree_dirs; i++) {
212 snprintf(tmppath, PATH_MAX, "%s/%s", tree_origin, tree_dirs[i]);
213
214 if (mkdir(tmppath, 0755) != 0) {
215 snprintf(errmsg, ERRSIZE, "mkdir %s", tmppath);
216 printerr(errmsg);
217 goto error;
218 }
219 }
220
221 /* Create the symlinks of the test tree */
222 for (i = 0; i < num_tree_symlinks; i++) {
223 snprintf(tmppath, PATH_MAX, "%s/%s",
224 tree_origin, tree_symlinks[i].orig);
225
226 if (symlink(tree_symlinks[i].dest, tmppath) != 0) {
227 snprintf(errmsg, ERRSIZE, "symlink %s to %s",
228 tmppath, tree_symlinks[i].dest);
229 printerr(errmsg);
230 goto error;
231 }
232 }
233
234 return 0;
235
236 error:
237 return 1;
238 }
239
240 int free_symlink_tree(void)
241 {
242 int i;
243 char tmppath[PATH_MAX];
244
245 /* Remove the symlinks from the test tree */
246 for (i = num_tree_symlinks - 1; i > -1; i--) {
247 snprintf(tmppath, PATH_MAX, "%s/%s",
248 tree_origin, tree_symlinks[i].orig);
249
250 if (unlink(tmppath) != 0) {
251 snprintf(errmsg, ERRSIZE, "unlink %s", tmppath);
252 printerr(errmsg);
253 goto error;
254 }
255 }
256
257 /* Remove the directories from the test tree */
258 for (i = num_tree_dirs - 1; i > -1; i--) {
259 snprintf(tmppath, PATH_MAX, "%s/%s", tree_origin, tree_dirs[i]);
260
261 if (rmdir(tmppath) != 0) {
262 snprintf(errmsg, ERRSIZE, "rmdir %s", tmppath);
263 printerr(errmsg);
264 goto error;
265 }
266 }
267
268 /* Remove the temporary directory */
269 if (rmdir(tree_origin) != 0) {
270 snprintf(errmsg, ERRSIZE, "rmdir %s", tree_origin);
271 printerr(errmsg);
272 goto error;
273 }
274
275 return 0;
276
277 error:
278 return 1;
279 }
280
281 static void test_utils_expand_path(void)
282 {
283 char *result;
284 char name[100], tmppath[PATH_MAX], real_tree_origin[PATH_MAX];
285 int i, treelen;
286
287 /* Test valid cases */
288 for (i = 0; i < num_valid_tests; i++) {
289 sprintf(name, "valid test case: %s", valid_tests_inputs[i].input);
290
291 result = utils_expand_path(valid_tests_inputs[i].input);
292 ok(result != NULL &&
293 strcmp(result, valid_tests_expected_results[i]) == 0, name);
294
295 free(result);
296 }
297
298 /*
299 * Get the realpath for the tree_origin since it can itself be a
300 * symlink.
301 */
302 result = realpath(tree_origin, real_tree_origin);
303 if (!result) {
304 fail("realpath failed.");
305 return;
306 }
307
308 /* Test symlink tree cases */
309 treelen = strlen(real_tree_origin) + 1;
310 for (i = 0; i < num_symlink_tests; i++) {
311 sprintf(name, "symlink tree test case: [tmppath/]%s",
312 symlink_tests_inputs[i].input);
313
314 snprintf(tmppath, PATH_MAX, "%s/%s",
315 real_tree_origin, symlink_tests_inputs[i].input);
316 result = utils_expand_path(tmppath);
317 ok(result != NULL && strcmp(result + treelen,
318 symlink_tests_inputs[i].expected_result) == 0, name);
319
320 free(result);
321 }
322
323 /* Test invalid cases */
324 for (i = 0; i < num_invalid_tests; i++) {
325 const char *test_input = invalid_tests_inputs[i];
326
327 sprintf(name, "invalid test case: %s", test_input ?
328 test_input : "NULL");
329
330 result = utils_expand_path(test_input);
331 if (result != NULL) {
332 free(result);
333 }
334 ok(result == NULL, name);
335 }
336 }
337
338 int main(int argc, char **argv)
339 {
340 if (prepare_symlink_tree() != 0) {
341 goto error_mkdir;
342 }
343
344 if (prepare_valid_results() != 0) {
345 goto error_malloc;
346 }
347
348 plan_tests(num_valid_tests + num_invalid_tests + num_symlink_tests);
349
350 diag("utils_expand_path tests");
351
352 test_utils_expand_path();
353
354 free_valid_results();
355 free_symlink_tree();
356
357 return exit_status();
358
359 error_malloc:
360 free_valid_results();
361
362 error_mkdir:
363 free_symlink_tree();
364
365 return 1;
366 }
This page took 0.05332 seconds and 4 git commands to generate.