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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
|
/*
* libdpkg - Debian packaging suite library routines
* t-path.c - test path handling code
*
* Copyright © 2009-2012 Guillem Jover <guillem@debian.org>
*
* This is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <config.h>
#include <compat.h>
#include <ctype.h>
#include <stdbool.h>
#include <stdlib.h>
#include <dpkg/test.h>
#include <dpkg/path.h>
/* Use the test_trim_eq_ref macro to avoid leaking the string and to get
* meaningful line numbers from assert. */
#define test_trim_eq_ref(p, ref) \
do { \
char *t = test_alloc(strdup((p))); \
path_trim_slash_slashdot(t); \
test_str(t, ==, (ref)); \
free(t); \
} while (0)
static void
test_path_trim(void)
{
test_trim_eq_ref("/a", "/a");
test_trim_eq_ref("./././.", ".");
test_trim_eq_ref("./././", ".");
test_trim_eq_ref("./.", ".");
test_trim_eq_ref("./", ".");
test_trim_eq_ref("/./././.", "/");
test_trim_eq_ref("/./", "/");
test_trim_eq_ref("/.", "/");
test_trim_eq_ref("/", "/");
test_trim_eq_ref("", "");
test_trim_eq_ref("/./../.", "/./..");
test_trim_eq_ref("/foo/bar/./", "/foo/bar");
test_trim_eq_ref("./foo/bar/./", "./foo/bar");
test_trim_eq_ref("/./foo/bar/./", "/./foo/bar");
}
static void
test_path_skip(void)
{
test_str(path_skip_slash_dotslash("./././."), ==, ".");
test_str(path_skip_slash_dotslash("./././"), ==, "");
test_str(path_skip_slash_dotslash("./."), ==, ".");
test_str(path_skip_slash_dotslash("./"), ==, "");
test_str(path_skip_slash_dotslash("/./././."), ==, ".");
test_str(path_skip_slash_dotslash("/./"), ==, "");
test_str(path_skip_slash_dotslash("/."), ==, ".");
test_str(path_skip_slash_dotslash("/"), ==, "");
test_str(path_skip_slash_dotslash("/./../."), ==, "../.");
test_str(path_skip_slash_dotslash("/foo/bar/./"), ==, "foo/bar/./");
test_str(path_skip_slash_dotslash("./foo/bar/./"), ==, "foo/bar/./");
test_str(path_skip_slash_dotslash("/./foo/bar/./"), ==, "foo/bar/./");
}
static void
test_path_basename(void)
{
test_str(path_basename("./."), ==, ".");
test_str(path_basename("./"), ==, "");
test_str(path_basename("/."), ==, ".");
test_str(path_basename("/"), ==, "");
test_str(path_basename("/foo"), ==, "foo");
test_str(path_basename("/foo/bar"), ==, "bar");
test_str(path_basename("/foo/bar/"), ==, "");
}
static void
test_path_temp(void)
{
char *template;
template = path_make_temp_template("test");
test_pass(strstr(template, "test") != NULL);
test_pass(strstr(template, "XXXXXX") != NULL);
free(template);
}
static bool
string_is_ascii(const char *str)
{
while (*str) {
if (!isascii(*str))
return false;
str++;
}
return true;
}
static void
test_path_quote(void)
{
const char src_7_bit[] = "string with 7-bit chars only";
const char src_7_bit_trim[] = "string with 7-bit chars";
const char src_8_bit[] = "text w/ 8-bit chars: \\ \370 \300 \342 end";
const char src_8_bit_end[] = "text \370";
const char src_bs_end[] = "text \\";
char *dst;
size_t len;
/* Test 0 length. */
dst = NULL;
path_quote_filename(dst, src_7_bit, 0);
/* Test no quoting. */
len = strlen(src_7_bit) + 1;
dst = test_alloc(malloc(len));
path_quote_filename(dst, src_7_bit, len);
test_str(dst, ==, src_7_bit);
free(dst);
/* Test no quoting with limit. */
len = strlen(src_7_bit_trim) + 1;
dst = test_alloc(malloc(len));
path_quote_filename(dst, src_7_bit, len);
test_str(dst, ==, src_7_bit_trim);
free(dst);
/* Test normal quoting. */
len = strlen(src_8_bit) * 2 + 1;
dst = test_alloc(malloc(len));
path_quote_filename(dst, src_8_bit, len);
test_pass(strstr(dst, "end") != NULL);
test_pass(string_is_ascii(dst));
free(dst);
/* Test normal quoting with limit. */
len = strlen(src_8_bit_end) + 1 + 2;
dst = test_alloc(malloc(len));
path_quote_filename(dst, src_8_bit_end, len);
test_str(dst, ==, "text ");
free(dst);
/* Test backslash quoting with limit. */
len = strlen(src_bs_end) + 1;
dst = test_alloc(malloc(len));
path_quote_filename(dst, src_bs_end, len);
test_str(dst, ==, "text ");
free(dst);
}
TEST_ENTRY(test)
{
test_plan(41);
test_path_trim();
test_path_skip();
test_path_basename();
test_path_temp();
test_path_quote();
}
|