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