Fix: test test_ust_data.c: check OOM
[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
ad7c9c18 31/* For error.h */
cc7f9e36
RB
32int lttng_opt_quiet = 1;
33int lttng_opt_verbose = 3;
c7e35b03 34int lttng_opt_mi;
cc7f9e36
RB
35
36struct valid_test_input {
37 char *input;
38 char *relative_part;
39 char *absolute_part;
40};
41
02bf969d
RB
42struct tree_symlink {
43 char *orig;
44 char *dest;
45};
46
47struct symlink_test_input {
48 char *input;
49 char *expected_result;
50};
51
cc7f9e36
RB
52/* Valid test cases */
53static 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 { ".", ".", "" },
ab5ff479
RB
72 { "/../a/b/c/d/e", "", "/a/b/c/d/e" },
73 { "/a/b/c/d/../../../../../e", "", "/e" },
9a506343
RB
74 { "/..", "", "/" },
75 { "/a/..", "", "/" },
cc7f9e36
RB
76};
77char **valid_tests_expected_results;
78static const int num_valid_tests =
79 sizeof(valid_tests_inputs) / sizeof(valid_tests_inputs[0]);
80
02bf969d
RB
81/* Symlinks test cases */
82char tree_origin[] = "/tmp/test_utils_expand_path.XXXXXX";
83
84static const char * const tree_dirs[] = {
85 "a",
86 "a/b",
87 "a/b/c",
88 "a/e",
89};
90static const int num_tree_dirs =
91 sizeof(tree_dirs) / sizeof(tree_dirs[0]);
92
93static 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};
101static const int num_tree_symlinks =
102 sizeof(tree_symlinks) / sizeof(tree_symlinks[0]);
103
104static 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};
111static const int num_symlink_tests =
112 sizeof(symlink_tests_inputs) / sizeof(symlink_tests_inputs[0]);
113
cc7f9e36
RB
114/* Invalid test cases */
115static char *invalid_tests_inputs[] = {
116 NULL,
cc7f9e36
RB
117};
118static const int num_invalid_tests =
119 sizeof(invalid_tests_inputs) / sizeof(invalid_tests_inputs[0]);
120
02bf969d
RB
121#define ERRSIZE 100
122char errmsg[ERRSIZE];
123static void printerr(char *msg)
124{
125 fprintf(stderr, "test_utils_expand_path: error: %s\n", msg);
126}
127
cc7f9e36
RB
128int 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) {
02bf969d 144 printerr("malloc expected results");
13b42522 145 free(empty);
cc7f9e36
RB
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
171int 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
02bf969d
RB
184int 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
221error:
222 return 1;
223}
224
225int 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
262error:
263 return 1;
264}
265
cc7f9e36
RB
266static void test_utils_expand_path(void)
267{
268 char *result;
02bf969d 269 char name[100], tmppath[PATH_MAX];
cc7f9e36
RB
270 int i;
271
272 /* Test valid cases */
273 for (i = 0; i < num_valid_tests; i++) {
cc7f9e36
RB
274 sprintf(name, "valid test case: %s", valid_tests_inputs[i].input);
275
276 result = utils_expand_path(valid_tests_inputs[i].input);
ba526563
RB
277 ok(result != NULL &&
278 strcmp(result, valid_tests_expected_results[i]) == 0, name);
cc7f9e36
RB
279
280 free(result);
281 }
282
02bf969d
RB
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
cc7f9e36
RB
298 /* Test invalid cases */
299 for (i = 0; i < num_invalid_tests; i++) {
cc7f9e36
RB
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
310int main(int argc, char **argv)
311{
02bf969d
RB
312 if (prepare_symlink_tree() != 0) {
313 goto error_mkdir;
314 }
315
cc7f9e36 316 if (prepare_valid_results() != 0) {
02bf969d 317 goto error_malloc;
cc7f9e36
RB
318 }
319
02bf969d 320 plan_tests(num_valid_tests + num_invalid_tests + num_symlink_tests);
cc7f9e36
RB
321
322 diag("utils_expand_path tests");
323
324 test_utils_expand_path();
325
326 free_valid_results();
02bf969d
RB
327 free_symlink_tree();
328
cc7f9e36 329 return exit_status();
02bf969d
RB
330
331error_malloc:
332 free_valid_results();
333
334error_mkdir:
335 free_symlink_tree();
336
337 return 1;
cc7f9e36 338}
This page took 0.037271 seconds and 4 git commands to generate.