Add Unit test to poll compatibility layer
[lttng-tools.git] / tests / unit / test_utils_expand_path.c
CommitLineData
cc7f9e36
RB
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
02bf969d
RB
24#include <sys/stat.h>
25#include <sys/types.h>
26
cc7f9e36
RB
27#include <tap/tap.h>
28
343af227 29#include <common/utils.h>
8098bf0a
MD
30#include <common/common.h>
31
ad7c9c18 32/* For error.h */
cc7f9e36
RB
33int lttng_opt_quiet = 1;
34int lttng_opt_verbose = 3;
c7e35b03 35int lttng_opt_mi;
cc7f9e36
RB
36
37struct valid_test_input {
38 char *input;
39 char *relative_part;
40 char *absolute_part;
41};
42
02bf969d
RB
43struct tree_symlink {
44 char *orig;
45 char *dest;
46};
47
48struct symlink_test_input {
49 char *input;
50 char *expected_result;
51};
52
cc7f9e36
RB
53/* Valid test cases */
54static struct valid_test_input valid_tests_inputs[] = {
55 { "/a/b/c/d/e", "", "/a/b/c/d/e" },
4b223a67 56 { "/a//b//c/d/e", "", "/a/b/c/d/e" },
cc7f9e36
RB
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 { ".", ".", "" },
ab5ff479
RB
74 { "/../a/b/c/d/e", "", "/a/b/c/d/e" },
75 { "/a/b/c/d/../../../../../e", "", "/e" },
9a506343
RB
76 { "/..", "", "/" },
77 { "/a/..", "", "/" },
cc7f9e36
RB
78};
79char **valid_tests_expected_results;
80static const int num_valid_tests =
81 sizeof(valid_tests_inputs) / sizeof(valid_tests_inputs[0]);
82
02bf969d
RB
83/* Symlinks test cases */
84char tree_origin[] = "/tmp/test_utils_expand_path.XXXXXX";
85
86static const char * const tree_dirs[] = {
87 "a",
88 "a/b",
89 "a/b/c",
90 "a/e",
91};
92static const int num_tree_dirs =
93 sizeof(tree_dirs) / sizeof(tree_dirs[0]);
94
95static 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};
103static const int num_tree_symlinks =
104 sizeof(tree_symlinks) / sizeof(tree_symlinks[0]);
105
106static 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};
113static const int num_symlink_tests =
114 sizeof(symlink_tests_inputs) / sizeof(symlink_tests_inputs[0]);
115
cc7f9e36
RB
116/* Invalid test cases */
117static char *invalid_tests_inputs[] = {
118 NULL,
cc7f9e36
RB
119};
120static const int num_invalid_tests =
121 sizeof(invalid_tests_inputs) / sizeof(invalid_tests_inputs[0]);
122
02bf969d
RB
123#define ERRSIZE 100
124char errmsg[ERRSIZE];
125static void printerr(char *msg)
126{
127 fprintf(stderr, "test_utils_expand_path: error: %s\n", msg);
128}
129
1661351d 130int prepare_valid_results(void)
cc7f9e36
RB
131{
132 int i;
8098bf0a
MD
133 char *relative, *cur_path = NULL, *prev_path = NULL,
134 *pprev_path = NULL, *empty = NULL;
135 int ret = 0;
cc7f9e36
RB
136
137 /* Prepare the relative paths */
138 cur_path = realpath(".", NULL);
139 prev_path = realpath("..", NULL);
140 pprev_path = realpath("../..", NULL);
141 empty = strdup("");
8098bf0a
MD
142 if (!cur_path || !prev_path || !pprev_path || !empty) {
143 printerr("strdup out of memory");
144 ret = -1;
145 goto end;
146 }
cc7f9e36
RB
147
148 /* allocate memory for the expected results */
8098bf0a
MD
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 }
cc7f9e36
RB
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) {
02bf969d 158 printerr("malloc expected results");
8098bf0a
MD
159 ret = -1;
160 goto end;
cc7f9e36
RB
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
8098bf0a 177end:
cc7f9e36
RB
178 free(cur_path);
179 free(prev_path);
180 free(pprev_path);
181 free(empty);
182
8098bf0a 183 return ret;
cc7f9e36
RB
184}
185
1661351d 186int free_valid_results(void)
cc7f9e36
RB
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
1661351d 199int prepare_symlink_tree(void)
02bf969d
RB
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
236error:
237 return 1;
238}
239
1661351d 240int free_symlink_tree(void)
02bf969d
RB
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
277error:
278 return 1;
279}
280
cc7f9e36
RB
281static void test_utils_expand_path(void)
282{
283 char *result;
f66e964a
JR
284 char name[100], tmppath[PATH_MAX], real_tree_origin[PATH_MAX];
285 int i, treelen;
cc7f9e36
RB
286
287 /* Test valid cases */
288 for (i = 0; i < num_valid_tests; i++) {
cc7f9e36
RB
289 sprintf(name, "valid test case: %s", valid_tests_inputs[i].input);
290
291 result = utils_expand_path(valid_tests_inputs[i].input);
ba526563
RB
292 ok(result != NULL &&
293 strcmp(result, valid_tests_expected_results[i]) == 0, name);
cc7f9e36
RB
294
295 free(result);
296 }
297
f66e964a
JR
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
02bf969d 308 /* Test symlink tree cases */
f66e964a 309 treelen = strlen(real_tree_origin) + 1;
02bf969d
RB
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",
f66e964a 315 real_tree_origin, symlink_tests_inputs[i].input);
02bf969d
RB
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
cc7f9e36
RB
323 /* Test invalid cases */
324 for (i = 0; i < num_invalid_tests; i++) {
8082d471 325 const char *test_input = invalid_tests_inputs[i];
cc7f9e36 326
8082d471
JG
327 sprintf(name, "invalid test case: %s", test_input ?
328 test_input : "NULL");
329
330 result = utils_expand_path(test_input);
cc7f9e36
RB
331 if (result != NULL) {
332 free(result);
333 }
334 ok(result == NULL, name);
335 }
336}
337
338int main(int argc, char **argv)
339{
02bf969d
RB
340 if (prepare_symlink_tree() != 0) {
341 goto error_mkdir;
342 }
343
cc7f9e36 344 if (prepare_valid_results() != 0) {
02bf969d 345 goto error_malloc;
cc7f9e36
RB
346 }
347
02bf969d 348 plan_tests(num_valid_tests + num_invalid_tests + num_symlink_tests);
cc7f9e36
RB
349
350 diag("utils_expand_path tests");
351
352 test_utils_expand_path();
353
354 free_valid_results();
02bf969d
RB
355 free_symlink_tree();
356
cc7f9e36 357 return exit_status();
02bf969d
RB
358
359error_malloc:
360 free_valid_results();
361
362error_mkdir:
363 free_symlink_tree();
364
365 return 1;
cc7f9e36 366}
This page took 0.077967 seconds and 4 git commands to generate.