-
Notifications
You must be signed in to change notification settings - Fork 271
/
test-path-util.c
159 lines (134 loc) · 4.23 KB
/
test-path-util.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/* Copyright (c) 2016 Dovecot authors, see the included COPYING file */
#include <unistd.h>
#include <stdlib.h>
#include <libgen.h>
#include "test-lib.h"
#include "path-util.h"
static char tmpdir[64];
static const char *cwd;
static const char *link1;
static const char *link2;
static const char *link3;
static void test_local_path() {
const char *expected = t_strconcat(cwd, "/README.md", NULL);
const char *npath = NULL, *error = NULL;
test_assert(t_normpath_to("README.md", cwd, &npath, &error) == 0);
test_assert_strcmp(npath, expected);
}
static void test_absolute_path_no_change(void) {
const char *npath = NULL, *error = NULL;
test_assert(t_normpath_to("/", "/", &npath, &error) == 0);
test_assert_strcmp(npath, "/");
test_assert(t_normpath_to(cwd, cwd, &npath, &error) == 0);
test_assert_strcmp(npath, cwd);
}
static int path_height(const char* p) {
int n;
for (n = 0; *p != '\0'; ++p)
n += *p == '/';
return n;
}
static void test_travel_to_root(void) {
int l = path_height(cwd);
const char *npath = cwd;
for (npath = cwd; l--;) {
const char *error;
test_assert_idx(t_normpath_to("../", npath, &npath, &error) == 0, l);
}
test_assert_strcmp(npath, "/");
}
static void test_extra_slashes(void) {
const char *npath = NULL, *error = NULL;
test_assert(t_normpath_to(".", cwd, &npath, &error) == 0);
test_assert_strcmp(npath, cwd);
test_assert(t_normpath_to("./", cwd, &npath, &error) == 0);
test_assert_strcmp(npath, cwd);
test_assert(t_normpath_to(".////", cwd, &npath, &error) == 0);
test_assert_strcmp(npath, cwd);
}
static void test_nonexistent_path(void) {
const char *npath = NULL, *error = NULL;
const char *expected = t_strconcat(cwd, "/nonexistent", NULL);
test_assert(t_normpath_to("nonexistent", cwd, &npath, &error) == 0);
test_assert_strcmp(npath, expected);
test_assert(t_realpath_to("nonexistent", cwd, &npath, &error) == -1);
test_assert(error != NULL);
}
static void test_relative_dotdot() {
const char *rel_path = t_strconcat("../", basename(tmpdir), NULL);
const char *npath = NULL, *error = NULL;
test_assert(t_normpath_to(rel_path, tmpdir, &npath, &error) == 0);
test_assert_strcmp(npath, tmpdir);
test_assert(t_normpath_to("..", tmpdir, &npath, &error) == 0);
test_assert_strcmp(npath, "/tmp");
test_assert(t_normpath_to("../", tmpdir, &npath, &error) == 0);
test_assert_strcmp(npath, "/tmp");
test_assert(t_normpath_to("../.", tmpdir, &npath, &error) == 0);
test_assert_strcmp(npath, "/tmp");
}
static void test_link1() {
const char *npath = NULL, *error = NULL;
test_assert(t_realpath_to(link1, "/", &npath, &error) == 0);
test_assert_strcmp(npath, tmpdir);
}
static void test_link_loop() {
const char *npath = NULL, *error = NULL;
errno = 0;
test_assert(t_realpath_to(link2, "/", &npath, &error) == -1);
test_assert(errno == ELOOP);
test_assert(error != NULL);
}
static void test_abspath_vs_normpath() {
const char *abs = t_abspath_to("../../bin", "/usr/lib/");
test_assert_strcmp(abs, "/usr/lib//../../bin");
const char *norm = NULL, *error = NULL;
test_assert(t_normpath_to("../../bin", "/usr///lib/", &norm, &error) == 0);
test_assert_strcmp(norm, "/bin");
}
static void test_init(void) {
const char *error;
test_assert(t_get_working_dir(&cwd, &error) == 0);
strcpy(tmpdir, "/tmp/tmpdir.XXXXXX");
if (mkdtemp(tmpdir) == NULL) {
i_fatal("mkdtemp: %m");
}
link1 = t_strconcat(tmpdir, "/link1", NULL);
if (symlink(tmpdir, link1) < 0) {
i_fatal("symlink: %m");
}
/* link2 and link3 point to each other to create a loop */
link2 = t_strconcat(tmpdir, "/link2", NULL);
link3 = t_strconcat(tmpdir, "/link3", NULL);
if (symlink(link3, link2) < 0) {
i_fatal("symlink: %m");
}
if (symlink(link2, link3) < 0) {
i_fatal("symlink: %m");
}
}
static void test_deinit(void) {
if (unlink(link1) < 0) {
i_fatal("unlink: %m");
} if (unlink(link2) < 0) {
i_fatal("unlink: %m");
} if (unlink(link3) < 0) {
i_fatal("unlink: %m");
} if (rmdir(tmpdir) < 0) {
i_fatal("rmdir: %m");
}
}
void test_path_util(void) {
test_begin("test_path_util");
test_init();
test_local_path();
test_absolute_path_no_change();
test_travel_to_root();
test_extra_slashes();
test_nonexistent_path();
test_relative_dotdot();
test_link1();
test_link_loop();
test_abspath_vs_normpath();
test_deinit();
test_end();
}