Mi: mi backend + mi for command version
[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 <src/common/utils.h>
30
31 /* For error.h */
32 int lttng_opt_quiet = 1;
33 int lttng_opt_verbose = 3;
34 int lttng_opt_mi;
35
36 struct valid_test_input {
37 char *input;
38 char *relative_part;
39 char *absolute_part;
40 };
41
42 struct tree_symlink {
43 char *orig;
44 char *dest;
45 };
46
47 struct symlink_test_input {
48 char *input;
49 char *expected_result;
50 };
51
52 /* Valid test cases */
53 static struct valid_test_input valid_tests_inputs[] = {
54 { "/a/b/c/d/e", "", "/a/b/c/d/e" },
55 { "./a/b/c/d/e", ".", "/a/b/c/d/e" },
56 { "../a/b/c/d/../e", "..", "/a/b/c/e" },
57 { ".././a/b/c/d/./e", "..", "/a/b/c/d/e" },
58 { "../../a/b/c/d/e", "../..", "/a/b/c/d/e" },
59 { "./a/b/../c/d/../e", ".", "/a/c/e" },
60 { "../a/b/../../c/./d/./e", "..", "/c/d/e" },
61 { "../../a/b/../c/d/../../e", "../..", "/a/e" },
62 { "./a/b/c/d/../../../../e", ".", "/e" },
63 { ".././a/b/c/d/./e", "..", "/a/b/c/d/e" },
64 { "a/", ".", "/a/" },
65 { "a", ".", "/a" },
66 { "../../", "../..", "/" },
67 { "../..", "../..", "" },
68 { "../", "..", "/" },
69 { "..", "..", "" },
70 { "./", ".", "/" },
71 { ".", ".", "" },
72 { "/../a/b/c/d/e", "", "/a/b/c/d/e" },
73 { "/a/b/c/d/../../../../../e", "", "/e" },
74 { "/..", "", "/" },
75 { "/a/..", "", "/" },
76 };
77 char **valid_tests_expected_results;
78 static const int num_valid_tests =
79 sizeof(valid_tests_inputs) / sizeof(valid_tests_inputs[0]);
80
81 /* Symlinks test cases */
82 char tree_origin[] = "/tmp/test_utils_expand_path.XXXXXX";
83
84 static const char * const tree_dirs[] = {
85 "a",
86 "a/b",
87 "a/b/c",
88 "a/e",
89 };
90 static const int num_tree_dirs =
91 sizeof(tree_dirs) / sizeof(tree_dirs[0]);
92
93 static struct tree_symlink tree_symlinks[] = {
94 { "a/d", "b/c/" },
95 { "a/g", "d/" },
96 { "a/b/f", "../e/" },
97 { "a/b/h", "../g/" },
98 { "a/b/k", "c/g/" },
99 { "a/b/c/g", "../../../" },
100 };
101 static const int num_tree_symlinks =
102 sizeof(tree_symlinks) / sizeof(tree_symlinks[0]);
103
104 static struct symlink_test_input symlink_tests_inputs[] = {
105 { "a/g/../l/.", "a/b/l" },
106 { "a/g/../l/./", "a/b/l/" },
107 { "a/g/../l/..", "a/b" },
108 { "a/g/../l/../", "a/b/" },
109 { "a/b/h/g/", "" },
110 };
111 static const int num_symlink_tests =
112 sizeof(symlink_tests_inputs) / sizeof(symlink_tests_inputs[0]);
113
114 /* Invalid test cases */
115 static char *invalid_tests_inputs[] = {
116 NULL,
117 };
118 static const int num_invalid_tests =
119 sizeof(invalid_tests_inputs) / sizeof(invalid_tests_inputs[0]);
120
121 #define ERRSIZE 100
122 char errmsg[ERRSIZE];
123 static void printerr(char *msg)
124 {
125 fprintf(stderr, "test_utils_expand_path: error: %s\n", msg);
126 }
127
128 int prepare_valid_results()
129 {
130 int i;
131 char *relative, *cur_path, *prev_path, *pprev_path, *empty;
132
133 /* Prepare the relative paths */
134 cur_path = realpath(".", NULL);
135 prev_path = realpath("..", NULL);
136 pprev_path = realpath("../..", NULL);
137 empty = strdup("");
138
139 /* allocate memory for the expected results */
140 valid_tests_expected_results = malloc(sizeof(char *) * num_valid_tests);
141 for (i = 0; i < num_valid_tests; i++) {
142 valid_tests_expected_results[i] = malloc(PATH_MAX);
143 if (valid_tests_expected_results[i] == NULL) {
144 printerr("malloc expected results");
145 free(empty);
146 return 1;
147 }
148
149 if (strcmp(valid_tests_inputs[i].relative_part, ".") == 0) {
150 relative = cur_path;
151 } else if (strcmp(valid_tests_inputs[i].relative_part, "..") == 0) {
152 relative = prev_path;
153 } else if (strcmp(valid_tests_inputs[i].relative_part, "../..") == 0) {
154 relative = pprev_path;
155 } else {
156 relative = empty;
157 }
158
159 snprintf(valid_tests_expected_results[i], PATH_MAX,
160 "%s%s", relative, valid_tests_inputs[i].absolute_part);
161 }
162
163 free(cur_path);
164 free(prev_path);
165 free(pprev_path);
166 free(empty);
167
168 return 0;
169 }
170
171 int free_valid_results()
172 {
173 int i;
174
175 for (i = 0; i < num_valid_tests; i++) {
176 free(valid_tests_expected_results[i]);
177 }
178
179 free(valid_tests_expected_results);
180
181 return 0;
182 }
183
184 int prepare_symlink_tree()
185 {
186 int i;
187 char tmppath[PATH_MAX];
188
189 /* Create the temporary directory */
190 if (mkdtemp(tree_origin) == NULL) {
191 printerr("mkdtemp");
192 goto error;
193 }
194
195 /* Create the directories of the test tree */
196 for (i = 0; i < num_tree_dirs; i++) {
197 snprintf(tmppath, PATH_MAX, "%s/%s", tree_origin, tree_dirs[i]);
198
199 if (mkdir(tmppath, 0755) != 0) {
200 snprintf(errmsg, ERRSIZE, "mkdir %s", tmppath);
201 printerr(errmsg);
202 goto error;
203 }
204 }
205
206 /* Create the symlinks of the test tree */
207 for (i = 0; i < num_tree_symlinks; i++) {
208 snprintf(tmppath, PATH_MAX, "%s/%s",
209 tree_origin, tree_symlinks[i].orig);
210
211 if (symlink(tree_symlinks[i].dest, tmppath) != 0) {
212 snprintf(errmsg, ERRSIZE, "symlink %s to %s",
213 tmppath, tree_symlinks[i].dest);
214 printerr(errmsg);
215 goto error;
216 }
217 }
218
219 return 0;
220
221 error:
222 return 1;
223 }
224
225 int free_symlink_tree()
226 {
227 int i;
228 char tmppath[PATH_MAX];
229
230 /* Remove the symlinks from the test tree */
231 for (i = num_tree_symlinks - 1; i > -1; i--) {
232 snprintf(tmppath, PATH_MAX, "%s/%s",
233 tree_origin, tree_symlinks[i].orig);
234
235 if (unlink(tmppath) != 0) {
236 snprintf(errmsg, ERRSIZE, "unlink %s", tmppath);
237 printerr(errmsg);
238 goto error;
239 }
240 }
241
242 /* Remove the directories from the test tree */
243 for (i = num_tree_dirs - 1; i > -1; i--) {
244 snprintf(tmppath, PATH_MAX, "%s/%s", tree_origin, tree_dirs[i]);
245
246 if (rmdir(tmppath) != 0) {
247 snprintf(errmsg, ERRSIZE, "rmdir %s", tmppath);
248 printerr(errmsg);
249 goto error;
250 }
251 }
252
253 /* Remove the temporary directory */
254 if (rmdir(tree_origin) != 0) {
255 snprintf(errmsg, ERRSIZE, "rmdir %s", tree_origin);
256 printerr(errmsg);
257 goto error;
258 }
259
260 return 0;
261
262 error:
263 return 1;
264 }
265
266 static void test_utils_expand_path(void)
267 {
268 char *result;
269 char name[100], tmppath[PATH_MAX];
270 int i;
271
272 /* Test valid cases */
273 for (i = 0; i < num_valid_tests; i++) {
274 sprintf(name, "valid test case: %s", valid_tests_inputs[i].input);
275
276 result = utils_expand_path(valid_tests_inputs[i].input);
277 ok(result != NULL &&
278 strcmp(result, valid_tests_expected_results[i]) == 0, name);
279
280 free(result);
281 }
282
283 /* Test symlink tree cases */
284 int treelen = strlen(tree_origin) + 1;
285 for (i = 0; i < num_symlink_tests; i++) {
286 sprintf(name, "symlink tree test case: [tmppath/]%s",
287 symlink_tests_inputs[i].input);
288
289 snprintf(tmppath, PATH_MAX, "%s/%s",
290 tree_origin, symlink_tests_inputs[i].input);
291 result = utils_expand_path(tmppath);
292 ok(result != NULL && strcmp(result + treelen,
293 symlink_tests_inputs[i].expected_result) == 0, name);
294
295 free(result);
296 }
297
298 /* Test invalid cases */
299 for (i = 0; i < num_invalid_tests; i++) {
300 sprintf(name, "invalid test case: %s", invalid_tests_inputs[i]);
301
302 result = utils_expand_path(invalid_tests_inputs[i]);
303 if (result != NULL) {
304 free(result);
305 }
306 ok(result == NULL, name);
307 }
308 }
309
310 int main(int argc, char **argv)
311 {
312 if (prepare_symlink_tree() != 0) {
313 goto error_mkdir;
314 }
315
316 if (prepare_valid_results() != 0) {
317 goto error_malloc;
318 }
319
320 plan_tests(num_valid_tests + num_invalid_tests + num_symlink_tests);
321
322 diag("utils_expand_path tests");
323
324 test_utils_expand_path();
325
326 free_valid_results();
327 free_symlink_tree();
328
329 return exit_status();
330
331 error_malloc:
332 free_valid_results();
333
334 error_mkdir:
335 free_symlink_tree();
336
337 return 1;
338 }
This page took 0.036546 seconds and 5 git commands to generate.