1 /*
2  * libdpkg - Debian packaging suite library routines
3  * t-string.c - test string handling
4  *
5  * Copyright © 2009-2011, 2014-2015 Guillem Jover <guillem@debian.org>
6  *
7  * This is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  */
20 
21 #include <config.h>
22 #include <compat.h>
23 
24 #include <dpkg/test.h>
25 #include <dpkg/string.h>
26 
27 #include <stdlib.h>
28 #include <string.h>
29 
30 #include <stdio.h>
31 
32 static void
test_str_is_set(void)33 test_str_is_set(void)
34 {
35 	/* Test if strings are unset. */
36 	test_pass(str_is_unset(NULL));
37 	test_pass(str_is_unset(""));
38 	test_fail(str_is_unset("aaa"));
39 
40 	/* Test if strings are set. */
41 	test_fail(str_is_set(NULL));
42 	test_fail(str_is_set(""));
43 	test_pass(str_is_set("ccc"));
44 }
45 
46 static void
test_str_match_end(void)47 test_str_match_end(void)
48 {
49 	test_pass(str_match_end("foo bar quux", "quux"));
50 	test_pass(str_match_end("foo bar quux", "bar quux"));
51 	test_pass(str_match_end("foo bar quux", "foo bar quux"));
52 	test_fail(str_match_end("foo bar quux", "foo bar quux zorg"));
53 	test_fail(str_match_end("foo bar quux", "foo bar"));
54 	test_fail(str_match_end("foo bar quux", "foo"));
55 }
56 
57 static void
test_str_fnv_hash(void)58 test_str_fnv_hash(void)
59 {
60 	test_pass(str_fnv_hash("") == 0x811c9dc5U);
61 	test_pass(str_fnv_hash("a") == 0xe40c292cUL);
62 	test_pass(str_fnv_hash("b") == 0xe70c2de5UL);
63 	test_pass(str_fnv_hash("c") == 0xe60c2c52UL);
64 	test_pass(str_fnv_hash("d") == 0xe10c2473UL);
65 	test_pass(str_fnv_hash("e") == 0xe00c22e0UL);
66 	test_pass(str_fnv_hash("f") == 0xe30c2799UL);
67 	test_pass(str_fnv_hash("fo") == 0x6222e842UL);
68 	test_pass(str_fnv_hash("foo") == 0xa9f37ed7UL);
69 	test_pass(str_fnv_hash("foob") == 0x3f5076efUL);
70 	test_pass(str_fnv_hash("fooba") == 0x39aaa18aUL);
71 	test_pass(str_fnv_hash("foobar") == 0xbf9cf968UL);
72 
73 	test_pass(str_fnv_hash("test-string") == 0xd28f6e61UL);
74 	test_pass(str_fnv_hash("Test-string") == 0x00a54b81UL);
75 	test_pass(str_fnv_hash("rest-string") == 0x1cdeebffUL);
76 	test_pass(str_fnv_hash("Rest-string") == 0x20464b9fUL);
77 }
78 
79 static void
test_str_concat(void)80 test_str_concat(void)
81 {
82 	char buf[1024], *str;
83 
84 	memset(buf, 0, sizeof(buf));
85 	str = str_concat(buf, NULL);
86 	test_pass(str == buf);
87 	test_str(buf, ==, "");
88 
89 	memset(buf, 0, sizeof(buf));
90 	str = str_concat(buf, "aaa", NULL);
91 	test_str(buf, ==, "aaa");
92 	test_pass(str == buf + 3);
93 
94 	memset(buf, 0, sizeof(buf));
95 	str = str_concat(buf, "zzzz", "yy", "xxxx", NULL);
96 	test_str(buf, ==, "zzzzyyxxxx");
97 	test_pass(str == buf + 10);
98 
99 	memset(buf, 0, sizeof(buf));
100 	str = str_concat(buf, "1234", "", "5678", NULL);
101 	test_str(buf, ==, "12345678");
102 	test_pass(str == buf + 8);
103 
104 	memset(buf, ' ', sizeof(buf));
105 	str = str_concat(buf, "eol", NULL, "bom", NULL);
106 	test_str(buf, ==, "eol");
107 	test_pass(str == buf + 3);
108 }
109 
110 static void
test_str_fmt(void)111 test_str_fmt(void)
112 {
113 	char *str;
114 
115 	str = str_fmt("%s", "abcde");
116 	test_str(str, ==, "abcde");
117 	free(str);
118 
119 	str = str_fmt("%d", 15);
120 	test_str(str, ==, "15");
121 	free(str);
122 }
123 
124 static void
test_str_escape_fmt(void)125 test_str_escape_fmt(void)
126 {
127 	char buf[1024], *q;
128 
129 	memset(buf, 'a', sizeof(buf));
130 	q = str_escape_fmt(buf, "", sizeof(buf));
131 	strcpy(q, " end");
132 	test_str(buf, ==, " end");
133 
134 	memset(buf, 'a', sizeof(buf));
135 	q = str_escape_fmt(buf, "%", sizeof(buf));
136 	strcpy(q, " end");
137 	test_str(buf, ==, "%% end");
138 
139 	memset(buf, 'a', sizeof(buf));
140 	q = str_escape_fmt(buf, "%%%", sizeof(buf));
141 	strcpy(q, " end");
142 	test_str(buf, ==, "%%%%%% end");
143 
144 	memset(buf, 'a', sizeof(buf));
145 	q = str_escape_fmt(buf, "%b%b%c%c%%", sizeof(buf));
146 	strcpy(q, " end");
147 	test_str(buf, ==, "%%b%%b%%c%%c%%%% end");
148 
149 	/* Test delimited buffer. */
150 	memset(buf, 'a', sizeof(buf));
151 	q = str_escape_fmt(buf, NULL, 0);
152 	test_mem(buf, ==, "aaaa", 4);
153 	test_pass(buf == q);
154 	test_pass(strnlen(buf, sizeof(buf)) == sizeof(buf));
155 
156 	memset(buf, 'a', sizeof(buf));
157 	q = str_escape_fmt(buf, "b", 1);
158 	test_str(q, ==, "");
159 
160 	memset(buf, 'a', sizeof(buf));
161 	q = str_escape_fmt(buf, "%%%", 5);
162 	strcpy(q, " end");
163 	test_str(buf, ==, "%%%% end");
164 
165 	memset(buf, 'a', sizeof(buf));
166 	q = str_escape_fmt(buf, "%%%", 4);
167 	strcpy(q, " end");
168 	test_str(buf, ==, "%% end");
169 }
170 
171 static void
test_str_quote_meta(void)172 test_str_quote_meta(void)
173 {
174 	char *str;
175 
176 	str = str_quote_meta("foo1 2bar");
177 	test_str(str, ==, "foo1\\ 2bar");
178 	free(str);
179 
180 	str = str_quote_meta("foo1?2bar");
181 	test_str(str, ==, "foo1\\?2bar");
182 	free(str);
183 
184 	str = str_quote_meta("foo1*2bar");
185 	test_str(str, ==, "foo1\\*2bar");
186 	free(str);
187 }
188 
189 static void
test_str_strip_quotes(void)190 test_str_strip_quotes(void)
191 {
192 	char buf[1024], *str;
193 
194 	strcpy(buf, "unquoted text");
195 	str = str_strip_quotes(buf);
196 	test_str(str, ==, "unquoted text");
197 
198 	strcpy(buf, "contained 'quoted text'");
199 	str = str_strip_quotes(buf);
200 	test_str(str, ==, "contained 'quoted text'");
201 
202 	strcpy(buf, "contained \"quoted text\"");
203 	str = str_strip_quotes(buf);
204 	test_str(str, ==, "contained \"quoted text\"");
205 
206 	strcpy(buf, "'unbalanced quotes");
207 	str = str_strip_quotes(buf);
208 	test_pass(str == NULL);
209 
210 	strcpy(buf, "\"unbalanced quotes");
211 	str = str_strip_quotes(buf);
212 	test_pass(str == NULL);
213 
214 	strcpy(buf, "'mismatched quotes\"");
215 	str = str_strip_quotes(buf);
216 	test_pass(str == NULL);
217 
218 	strcpy(buf, "\"mismatched quotes'");
219 	str = str_strip_quotes(buf);
220 	test_pass(str == NULL);
221 
222 	strcpy(buf, "'completely quoted text'");
223 	str = str_strip_quotes(buf);
224 	test_str(str, ==, "completely quoted text");
225 
226 	strcpy(buf, "\"completely quoted text\"");
227 	str = str_strip_quotes(buf);
228 	test_str(str, ==, "completely quoted text");
229 }
230 
TEST_ENTRY(test)231 TEST_ENTRY(test)
232 {
233 	test_plan(62);
234 
235 	test_str_is_set();
236 	test_str_match_end();
237 	test_str_fnv_hash();
238 	test_str_concat();
239 	test_str_fmt();
240 	test_str_escape_fmt();
241 	test_str_quote_meta();
242 	test_str_strip_quotes();
243 }
244