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