Debian Codegraph @

lib/util/charset/tests/charset.c

raw
1 /*
2 Unix SMB/CIFS implementation.
3 test suite for the charcnv functions
4
5 Copyright (C) Jelmer Vernooij 2007
6
7 This program 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 3 of the License, or
10 (at your option) any later version.
11
12 This program 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 <http://www.gnu.org/licenses/>.
19 */
20
21 #include "includes.h"
22 #include "torture/torture.h"
23
24 #undef strcasecmp
25 #undef strncasecmp
26
27 struct torture_suite *torture_local_charset(TALLOC_CTX *mem_ctx);
28
29 static bool test_toupper_m(struct torture_context *tctx)
30 {
31 torture_assert_int_equal(tctx, toupper_m('c'), 'C', "c");
32 torture_assert_int_equal(tctx, toupper_m('Z'), 'Z', "z");
33 torture_assert_int_equal(tctx, toupper_m(0xFFFF4565), 0xFFFF4565, "0xFFFF4565");
34 return true;
35 }
36
37 static bool test_tolower_m(struct torture_context *tctx)
38 {
39 torture_assert_int_equal(tctx, tolower_m('C'), 'c', "c");
40 torture_assert_int_equal(tctx, tolower_m('z'), 'z', "z");
41 torture_assert_int_equal(tctx, tolower_m(0xFFFF4565), 0xFFFF4565, "0xFFFF4565");
42 return true;
43 }
44
45 static bool test_codepoint_cmpi(struct torture_context *tctx)
46 {
47 torture_assert_int_equal(tctx, codepoint_cmpi('a', 'a'), 0, "same char");
48 torture_assert_int_equal(tctx, codepoint_cmpi('A', 'a'), 0, "upcase version");
49 torture_assert_int_equal(tctx, codepoint_cmpi('b', 'a'), 1, "right diff");
50 torture_assert_int_equal(tctx, codepoint_cmpi('b', 'A'), 1, "left greater, mixed case");
51 torture_assert_int_equal(tctx, codepoint_cmpi('C', 'a'), 1, "left greater, mixed case");
52 torture_assert_int_equal(tctx, codepoint_cmpi('a', 'b'), -1, "right greater");
53 torture_assert_int_equal(tctx, codepoint_cmpi('A', 'B'), -1, "right greater, upper case");
54 torture_assert_int_equal(tctx, codepoint_cmpi(0xc5, 0xc5), 0,
55 "Latin Capital Letter A with Ring Above");
56 torture_assert_int_equal(tctx, codepoint_cmpi(0xc5, 0xe5), 0,
57 "Latin both Letter A with Ring Above, lower right");
58 torture_assert_int_equal(tctx, codepoint_cmpi(0xe5, 0xde), -1,
59 "å < Þ");
60 return true;
61 }
62
63 static bool test_strcasecmp(struct torture_context *tctx)
64 {
65 torture_assert_int_equal(tctx, strcasecmp("foo", "bar"), 4, "different strings both lower");
66 torture_assert_int_equal(tctx, strcasecmp("foo", "Bar"), 4, "different strings lower/upper");
67 torture_assert_int_equal(tctx, strcasecmp("Foo", "bar"), 4, "different strings upper/lower");
68 torture_assert_int_equal(tctx, strcasecmp("AFoo", "_bar"), 2, "different strings upper/lower");
69 torture_assert_int_equal(tctx, strcasecmp("foo", "foo"), 0, "same case strings");
70 torture_assert_int_equal(tctx, strcasecmp("foo", "Foo"), 0, "different case strings");
71
72 /*
73 * Note that strcasecmp() doesn't allow NULL arguments
74 */
75 return true;
76 }
77
78 static bool test_strcasecmp_m(struct torture_context *tctx)
79 {
80 /* file.{accented e} in iso8859-1 */
81 const char file_iso8859_1[7] = { 0x66, 0x69, 0x6c, 0x65, 0x2d, 0xe9, 0 };
82 /* file.{accented e} in utf8 */
83 const char file_utf8[8] = { 0x66, 0x69, 0x6c, 0x65, 0x2d, 0xc3, 0xa9, 0 };
84 torture_assert_int_greater(tctx, strcasecmp_m("foo", "bar"), 0, "different strings both lower");
85 torture_assert_int_less(tctx, strcasecmp_m("bar", "foo"), 0, "different strings both lower");
86 torture_assert_int_greater(tctx, strcasecmp_m("foo", "Bar"), 0, "different strings lower/upper");
87 torture_assert_int_greater(tctx, strcasecmp_m("Foo", "bar"), 0, "different strings upper/lower");
88 torture_assert_int_greater(tctx, strcasecmp_m("AFoo", "_bar"), 0, "different strings upper/lower");
89 torture_assert_int_equal(tctx, strcasecmp_m("foo", "foo"), 0, "same case strings");
90 torture_assert_int_equal(tctx, strcasecmp_m("foo", "Foo"), 0, "different case strings");
91 torture_assert_int_greater(tctx, strcasecmp_m("food", "Foo"), 0, "strings differ towards the end");
92 torture_assert_int_less(tctx, strcasecmp_m("food", "Fool"), 0, "strings differ towards the end");
93 torture_assert_int_less(tctx, strcasecmp_m(NULL, "Foo"), 0, "one NULL");
94 torture_assert_int_greater(tctx, strcasecmp_m("foo", NULL), 0, "other NULL");
95 torture_assert_int_equal(tctx, strcasecmp_m(NULL, NULL), 0, "both NULL");
96 torture_assert_int_greater(tctx, strcasecmp_m(file_iso8859_1, file_utf8), 0,
97 "file.{accented e} should differ");
98 return true;
99 }
100
101
102 static bool test_strequal_m(struct torture_context *tctx)
103 {
104 torture_assert(tctx, !strequal_m("foo", "bar"), "different strings");
105 torture_assert(tctx, strequal_m("foo", "foo"), "same case strings");
106 torture_assert(tctx, strequal_m("foo", "Foo"), "different case strings");
107 torture_assert(tctx, !strequal_m(NULL, "Foo"), "one NULL");
108 torture_assert(tctx, !strequal_m("foo", NULL), "other NULL");
109 torture_assert(tctx, strequal_m(NULL, NULL), "both NULL");
110 return true;
111 }
112
113 static bool test_strcsequal(struct torture_context *tctx)
114 {
115 torture_assert(tctx, !strcsequal("foo", "bar"), "different strings");
116 torture_assert(tctx, strcsequal("foo", "foo"), "same case strings");
117 torture_assert(tctx, !strcsequal("foo", "Foo"), "different case strings");
118 torture_assert(tctx, !strcsequal(NULL, "Foo"), "one NULL");
119 torture_assert(tctx, !strcsequal("foo", NULL), "other NULL");
120 torture_assert(tctx, strcsequal(NULL, NULL), "both NULL");
121 return true;
122 }
123
124 static bool test_string_replace_m(struct torture_context *tctx)
125 {
126 char data[6] = "bla";
127 string_replace_m(data, 'b', 'c');
128 torture_assert_str_equal(tctx, data, "cla", "first char replaced");
129 memcpy(data, "bab", 4);
130 string_replace_m(data, 'b', 'c');
131 torture_assert_str_equal(tctx, data, "cac", "other chars replaced");
132 memcpy(data, "bba", 4);
133 string_replace_m(data, 'b', 'c');
134 torture_assert_str_equal(tctx, data, "cca", "other chars replaced");
135 memcpy(data, "blala", 6);
136 string_replace_m(data, 'o', 'c');
137 torture_assert_str_equal(tctx, data, "blala", "no chars replaced");
138 string_replace_m(NULL, 'b', 'c');
139 return true;
140 }
141
142 static bool test_strncasecmp(struct torture_context *tctx)
143 {
144 torture_assert_int_equal(tctx, strncasecmp("foo", "bar", 3), 4, "different strings both lower");
145 torture_assert_int_equal(tctx, strncasecmp("foo", "Bar", 3), 4, "different strings lower/upper");
146 torture_assert_int_equal(tctx, strncasecmp("Foo", "bar", 3), 4, "different strings upper/lower");
147 torture_assert_int_equal(tctx, strncasecmp("AFoo", "_bar", 4), 2, "different strings upper/lower");
148 torture_assert_int_equal(tctx, strncasecmp("foo", "foo", 3), 0, "same case strings");
149 torture_assert_int_equal(tctx, strncasecmp("foo", "Foo", 3), 0, "different case strings");
150 torture_assert_int_equal(tctx, strncasecmp("fool", "Foo", 3),0, "different case strings");
151 torture_assert_int_equal(tctx, strncasecmp("fool", "Fool", 40), 0, "over size");
152 torture_assert_int_equal(tctx, strncasecmp("BLA", "Fool", 0),0, "empty");
153
154 /*
155 * Note that strncasecmp() doesn't allow NULL arguments
156 */
157 return true;
158 }
159
160 static bool test_strncasecmp_m(struct torture_context *tctx)
161 {
162 /* file.{accented e} in iso8859-1 */
163 const char file_iso8859_1[7] = { 0x66, 0x69, 0x6c, 0x65, 0x2d, 0xe9, 0 };
164 /* file.{accented e} in utf8 */
165 const char file_utf8[8] = { 0x66, 0x69, 0x6c, 0x65, 0x2d, 0xc3, 0xa9, 0 };
166 torture_assert_int_greater(tctx, strncasecmp_m("foo", "bar", 3), 0, "different strings both lower");
167 torture_assert_int_greater(tctx, strncasecmp_m("foo", "Bar", 3), 0, "different strings lower/upper");
168 torture_assert_int_greater(tctx, strncasecmp_m("Foo", "bar", 3), 0, "different strings upper/lower");
169 torture_assert_int_greater(tctx, strncasecmp_m("AFoo", "_bar", 4), 0, "different strings upper/lower");
170 torture_assert_int_equal(tctx, strncasecmp_m("foo", "foo", 3), 0, "same case strings");
171 torture_assert_int_equal(tctx, strncasecmp_m("foo", "Foo", 3), 0, "different case strings");
172 torture_assert_int_equal(tctx, strncasecmp_m("fool", "Foo", 3),0, "different case strings");
173 torture_assert_int_equal(tctx, strncasecmp_m("fool", "Fool", 40), 0, "over size");
174 torture_assert_int_equal(tctx, strncasecmp_m("BLA", "Fool", 0),0, "empty");
175 torture_assert_int_less(tctx, strncasecmp_m(NULL, "Foo", 3), 0, "one NULL");
176 torture_assert_int_greater(tctx, strncasecmp_m("foo", NULL, 3), 0, "other NULL");
177 torture_assert_int_equal(tctx, strncasecmp_m(NULL, NULL, 3), 0, "both NULL");
178 torture_assert_int_greater(tctx, strncasecmp_m(file_iso8859_1, file_utf8, 6), 0,
179 "file.{accented e} should differ");
180 return true;
181 }
182
183 static bool test_next_token_null(struct torture_context *tctx)
184 {
185 char *buf = NULL;
186 torture_assert(tctx,
187 !next_token_talloc(tctx, NULL, &buf, " "),
188 "null ptr works");
189 return true;
190 }
191
192 static bool test_next_token(struct torture_context *tctx)
193 {
194 const char *teststr = "foo bar bla";
195 char *buf = NULL;
196 torture_assert(tctx,
197 next_token_talloc(tctx, &teststr, &buf, " "),
198 "finding token works");
199 torture_assert_str_equal(tctx, buf, "foo", "token matches");
200 torture_assert_str_equal(tctx, teststr, "bar bla", "ptr modified correctly");
201 TALLOC_FREE(buf);
202
203 torture_assert(tctx,
204 next_token_talloc(tctx, &teststr, &buf, " "),
205 "finding token works");
206 torture_assert_str_equal(tctx, buf, "bar", "token matches");
207 torture_assert_str_equal(tctx, teststr, "bla", "ptr modified correctly");
208 TALLOC_FREE(buf);
209
210 torture_assert(tctx,
211 next_token_talloc(tctx, &teststr, &buf, " "),
212 "finding token works");
213 torture_assert_str_equal(tctx, buf, "bla", "token matches");
214 torture_assert_str_equal(tctx, teststr, "", "ptr modified correctly");
215 TALLOC_FREE(buf);
216
217 torture_assert(tctx,
218 !next_token_talloc(tctx, &teststr, &buf, " "),
219 "finding token doesn't work");
220 return true;
221 }
222
223 static bool test_next_token_implicit_sep(struct torture_context *tctx)
224 {
225 const char *teststr = "foo\tbar\n bla";
226 char *buf = NULL;
227
228 torture_assert(tctx,
229 next_token_talloc(tctx, &teststr, &buf, NULL),
230 "finding token works");
231 torture_assert_str_equal(tctx, buf, "foo", "token matches");
232 torture_assert_str_equal(tctx, teststr, "bar\n bla", "ptr modified correctly");
233 TALLOC_FREE(buf);
234
235 torture_assert(tctx,
236 next_token_talloc(tctx, &teststr, &buf, NULL),
237 "finding token works");
238 torture_assert_str_equal(tctx, buf, "bar", "token matches");
239 torture_assert_str_equal(tctx, teststr, " bla", "ptr modified correctly");
240 TALLOC_FREE(buf);
241
242 torture_assert(tctx,
243 next_token_talloc(tctx, &teststr, &buf, NULL),
244 "finding token works");
245 torture_assert_str_equal(tctx, buf, "bla", "token matches");
246 torture_assert_str_equal(tctx, teststr, "", "ptr modified correctly");
247 TALLOC_FREE(buf);
248
249 torture_assert(tctx,
250 !next_token_talloc(tctx, &teststr, &buf, NULL),
251 "finding token doesn't work");
252 return true;
253 }
254
255 static bool test_next_token_seps(struct torture_context *tctx)
256 {
257 const char *teststr = ",foo bla";
258 char *buf = NULL;
259 torture_assert(tctx,
260 next_token_talloc(tctx, &teststr, &buf, ","),
261 "finding token works");
262 torture_assert_str_equal(tctx, buf, "foo bla", "token matches");
263 torture_assert_str_equal(tctx, teststr, "", "ptr modified correctly");
264 TALLOC_FREE(buf);
265
266 torture_assert(tctx,
267 !next_token_talloc(tctx, &teststr, &buf, ","),
268 "finding token doesn't work");
269 return true;
270 }
271
272 static bool test_next_token_quotes(struct torture_context *tctx)
273 {
274 const char *teststr = "\"foo bar\" bla";
275 char *buf = NULL;
276 torture_assert(tctx,
277 next_token_talloc(tctx, &teststr, &buf, " "),
278 "finding token works");
279 torture_assert_str_equal(tctx, buf, "foo bar", "token matches");
280 torture_assert_str_equal(tctx, teststr, "bla", "ptr modified correctly");
281 TALLOC_FREE(buf);
282
283 torture_assert(tctx,
284 next_token_talloc(tctx, &teststr, &buf, " "),
285 "finding token works");
286 torture_assert_str_equal(tctx, buf, "bla", "token matches");
287 torture_assert_str_equal(tctx, teststr, "", "ptr modified correctly");
288 TALLOC_FREE(buf);
289
290 torture_assert(tctx,
291 !next_token_talloc(tctx, &teststr, &buf, " "),
292 "finding token doesn't work");
293 return true;
294 }
295
296 static bool test_next_token_quote_wrong(struct torture_context *tctx)
297 {
298 const char *teststr = "\"foo bar bla";
299 char *buf = NULL;
300 torture_assert(tctx,
301 next_token_talloc(tctx, &teststr, &buf, " "),
302 "finding token works");
303 torture_assert_str_equal(tctx, buf, "foo bar bla", "token matches");
304 torture_assert_str_equal(tctx, teststr, "", "ptr modified correctly");
305
306 torture_assert(tctx,
307 !next_token_talloc(tctx, &teststr, &buf, " "),
308 "finding token doesn't work");
309 return true;
310 }
311
312 static bool test_strlen_m(struct torture_context *tctx)
313 {
314 torture_assert_int_equal(tctx, strlen_m("foo"), 3, "simple len");
315 torture_assert_int_equal(tctx, strlen_m("foo\x83l"), 6, "extended len");
316 torture_assert_int_equal(tctx, strlen_m(""), 0, "empty");
317 torture_assert_int_equal(tctx, strlen_m(NULL), 0, "NULL");
318 return true;
319 }
320
321 static bool test_strlen_m_term(struct torture_context *tctx)
322 {
323 torture_assert_int_equal(tctx, strlen_m_term("foo"), 4, "simple len");
324 torture_assert_int_equal(tctx, strlen_m_term("foo\x83l"), 7, "extended len");
325 torture_assert_int_equal(tctx, strlen_m_term(""), 1, "empty");
326 torture_assert_int_equal(tctx, strlen_m_term(NULL), 0, "NULL");
327 return true;
328 }
329
330 static bool test_strlen_m_term_null(struct torture_context *tctx)
331 {
332 torture_assert_int_equal(tctx, strlen_m_term_null("foo"), 4, "simple len");
333 torture_assert_int_equal(tctx, strlen_m_term_null("foo\x83l"), 7, "extended len");
334 torture_assert_int_equal(tctx, strlen_m_term_null(""), 0, "empty");
335 torture_assert_int_equal(tctx, strlen_m_term_null(NULL), 0, "NULL");
336 return true;
337 }
338
339 static bool test_strhaslower(struct torture_context *tctx)
340 {
341 torture_assert(tctx, strhaslower("a"), "one low char");
342 torture_assert(tctx, strhaslower("aB"), "one low, one up char");
343 torture_assert(tctx, !strhaslower("B"), "one up char");
344 torture_assert(tctx, !strhaslower(""), "empty string");
345 torture_assert(tctx, !strhaslower("3"), "one digit");
346 return true;
347 }
348
349 static bool test_strhasupper(struct torture_context *tctx)
350 {
351 torture_assert(tctx, strhasupper("B"), "one up char");
352 torture_assert(tctx, strhasupper("aB"), "one low, one up char");
353 torture_assert(tctx, !strhasupper("a"), "one low char");
354 torture_assert(tctx, !strhasupper(""), "empty string");
355 torture_assert(tctx, !strhasupper("3"), "one digit");
356 return true;
357 }
358
359 static bool test_count_chars_m(struct torture_context *tctx)
360 {
361 torture_assert_int_equal(tctx, count_chars_m("foo", 'o'), 2, "simple");
362 torture_assert_int_equal(tctx, count_chars_m("", 'o'), 0, "empty");
363 torture_assert_int_equal(tctx, count_chars_m("bla", 'o'), 0, "none");
364 torture_assert_int_equal(tctx, count_chars_m("bla", '\0'), 0, "null");
365 return true;
366 }
367
368 struct torture_suite *torture_local_charset(TALLOC_CTX *mem_ctx)
369 {
370 struct torture_suite *suite = torture_suite_create(mem_ctx, "charset");
371
372 torture_suite_add_simple_test(suite, "toupper_m", test_toupper_m);
373 torture_suite_add_simple_test(suite, "tolower_m", test_tolower_m);
374 torture_suite_add_simple_test(suite, "codepoint_cmpi", test_codepoint_cmpi);
375 torture_suite_add_simple_test(suite, "strcasecmp", test_strcasecmp);
376 torture_suite_add_simple_test(suite, "strcasecmp_m", test_strcasecmp_m);
377 torture_suite_add_simple_test(suite, "strequal_m", test_strequal_m);
378 torture_suite_add_simple_test(suite, "strcsequal", test_strcsequal);
379 torture_suite_add_simple_test(suite, "string_replace_m", test_string_replace_m);
380 torture_suite_add_simple_test(suite, "strncasecmp", test_strncasecmp);
381 torture_suite_add_simple_test(suite, "strncasecmp_m", test_strncasecmp_m);
382 torture_suite_add_simple_test(suite, "next_token", test_next_token);
383 torture_suite_add_simple_test(suite, "next_token_null", test_next_token_null);
384 torture_suite_add_simple_test(suite, "next_token_implicit_sep", test_next_token_implicit_sep);
385 torture_suite_add_simple_test(suite, "next_token_quotes", test_next_token_quotes);
386 torture_suite_add_simple_test(suite, "next_token_seps", test_next_token_seps);
387 torture_suite_add_simple_test(suite, "next_token_quote_wrong", test_next_token_quote_wrong);
388 torture_suite_add_simple_test(suite, "strlen_m", test_strlen_m);
389 torture_suite_add_simple_test(suite, "strlen_m_term", test_strlen_m_term);
390 torture_suite_add_simple_test(suite, "strlen_m_term_null", test_strlen_m_term_null);
391 torture_suite_add_simple_test(suite, "strhaslower", test_strhaslower);
392 torture_suite_add_simple_test(suite, "strhasupper", test_strhasupper);
393 torture_suite_add_simple_test(suite, "count_chars_m", test_count_chars_m);
394
395 return suite;
396 }