charset/tests: add more str[n]casecmp_m() tests to demonstrate the bug
[Samba.git] / lib / util / charset / tests / charset.c
blob918bf57fb99f5b086f60eb1f52f56bc47af877db
1 /*
2 Unix SMB/CIFS implementation.
3 test suite for the charcnv functions
5 Copyright (C) Jelmer Vernooij 2007
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.
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.
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/>.
21 #include "includes.h"
22 #include "torture/torture.h"
24 struct torture_suite *torture_local_charset(TALLOC_CTX *mem_ctx);
26 static bool test_toupper_m(struct torture_context *tctx)
28 torture_assert_int_equal(tctx, toupper_m('c'), 'C', "c");
29 torture_assert_int_equal(tctx, toupper_m('Z'), 'Z', "z");
30 torture_assert_int_equal(tctx, toupper_m(0xFFFF4565), 0xFFFF4565, "0xFFFF4565");
31 return true;
34 static bool test_tolower_m(struct torture_context *tctx)
36 torture_assert_int_equal(tctx, tolower_m('C'), 'c', "c");
37 torture_assert_int_equal(tctx, tolower_m('z'), 'z', "z");
38 torture_assert_int_equal(tctx, tolower_m(0xFFFF4565), 0xFFFF4565, "0xFFFF4565");
39 return true;
42 static bool test_codepoint_cmpi(struct torture_context *tctx)
44 torture_assert_int_equal(tctx, codepoint_cmpi('a', 'a'), 0, "same char");
45 torture_assert_int_equal(tctx, codepoint_cmpi('A', 'a'), 0, "upcase version");
46 torture_assert_int_equal(tctx, codepoint_cmpi('b', 'a'), 1, "right diff");
47 torture_assert_int_equal(tctx, codepoint_cmpi('a', 'b'), -1, "right diff");
48 return true;
51 static bool test_strcasecmp_m(struct torture_context *tctx)
53 /* file.{accented e} in iso8859-1 */
54 const char file_iso8859_1[7] = { 0x66, 0x69, 0x6c, 0x65, 0x2d, 0xe9, 0 };
55 /* file.{accented e} in utf8 */
56 const char file_utf8[8] = { 0x66, 0x69, 0x6c, 0x65, 0x2d, 0xc3, 0xa9, 0 };
57 torture_assert_int_equal(tctx, strcasecmp_m("foo", "bar"), 4, "different strings both lower");
58 torture_assert_int_equal(tctx, strcasecmp_m("foo", "Bar"), 4, "different strings lower/upper");
59 torture_assert_int_equal(tctx, strcasecmp_m("Foo", "bar"), 4, "different strings upper/lower");
60 torture_assert_int_equal(tctx, strcasecmp_m("AFoo", "_bar"), 2, "different strings upper/lower");
61 torture_assert_int_equal(tctx, strcasecmp_m("foo", "foo"), 0, "same case strings");
62 torture_assert_int_equal(tctx, strcasecmp_m("foo", "Foo"), 0, "different case strings");
63 torture_assert_int_equal(tctx, strcasecmp_m(NULL, "Foo"), -1, "one NULL");
64 torture_assert_int_equal(tctx, strcasecmp_m("foo", NULL), 1, "other NULL");
65 torture_assert_int_equal(tctx, strcasecmp_m(NULL, NULL), 0, "both NULL");
66 torture_assert_int_equal(tctx, strcasecmp_m(file_iso8859_1, file_utf8), 38,
67 "file.{accented e} should differ");
68 return true;
72 static bool test_strequal_m(struct torture_context *tctx)
74 torture_assert(tctx, !strequal_m("foo", "bar"), "different strings");
75 torture_assert(tctx, strequal_m("foo", "foo"), "same case strings");
76 torture_assert(tctx, strequal_m("foo", "Foo"), "different case strings");
77 torture_assert(tctx, !strequal_m(NULL, "Foo"), "one NULL");
78 torture_assert(tctx, !strequal_m("foo", NULL), "other NULL");
79 torture_assert(tctx, strequal_m(NULL, NULL), "both NULL");
80 return true;
83 static bool test_strcsequal(struct torture_context *tctx)
85 torture_assert(tctx, !strcsequal("foo", "bar"), "different strings");
86 torture_assert(tctx, strcsequal("foo", "foo"), "same case strings");
87 torture_assert(tctx, !strcsequal("foo", "Foo"), "different case strings");
88 torture_assert(tctx, !strcsequal(NULL, "Foo"), "one NULL");
89 torture_assert(tctx, !strcsequal("foo", NULL), "other NULL");
90 torture_assert(tctx, strcsequal(NULL, NULL), "both NULL");
91 return true;
94 static bool test_string_replace_m(struct torture_context *tctx)
96 char data[6] = "bla";
97 string_replace_m(data, 'b', 'c');
98 torture_assert_str_equal(tctx, data, "cla", "first char replaced");
99 memcpy(data, "bab", 4);
100 string_replace_m(data, 'b', 'c');
101 torture_assert_str_equal(tctx, data, "cac", "other chars replaced");
102 memcpy(data, "bba", 4);
103 string_replace_m(data, 'b', 'c');
104 torture_assert_str_equal(tctx, data, "cca", "other chars replaced");
105 memcpy(data, "blala", 6);
106 string_replace_m(data, 'o', 'c');
107 torture_assert_str_equal(tctx, data, "blala", "no chars replaced");
108 string_replace_m(NULL, 'b', 'c');
109 return true;
112 static bool test_strncasecmp_m(struct torture_context *tctx)
114 /* file.{accented e} in iso8859-1 */
115 const char file_iso8859_1[7] = { 0x66, 0x69, 0x6c, 0x65, 0x2d, 0xe9, 0 };
116 /* file.{accented e} in utf8 */
117 const char file_utf8[8] = { 0x66, 0x69, 0x6c, 0x65, 0x2d, 0xc3, 0xa9, 0 };
118 torture_assert_int_equal(tctx, strncasecmp_m("foo", "bar", 3), 4, "different strings both lower");
119 torture_assert_int_equal(tctx, strncasecmp_m("foo", "Bar", 3), 4, "different strings lower/upper");
120 torture_assert_int_equal(tctx, strncasecmp_m("Foo", "bar", 3), 4, "different strings upper/lower");
121 torture_assert_int_equal(tctx, strncasecmp_m("AFoo", "_bar", 4), 2, "different strings upper/lower");
122 torture_assert_int_equal(tctx, strncasecmp_m("foo", "foo", 3), 0, "same case strings");
123 torture_assert_int_equal(tctx, strncasecmp_m("foo", "Foo", 3), 0, "different case strings");
124 torture_assert_int_equal(tctx, strncasecmp_m("fool", "Foo", 3),0, "different case strings");
125 torture_assert_int_equal(tctx, strncasecmp_m("fool", "Fool", 40), 0, "over size");
126 torture_assert_int_equal(tctx, strncasecmp_m("BLA", "Fool", 0),0, "empty");
127 torture_assert_int_equal(tctx, strncasecmp_m(NULL, "Foo", 3), -1, "one NULL");
128 torture_assert_int_equal(tctx, strncasecmp_m("foo", NULL, 3), 1, "other NULL");
129 torture_assert_int_equal(tctx, strncasecmp_m(NULL, NULL, 3), 0, "both NULL");
130 torture_assert_int_equal(tctx, strncasecmp_m(file_iso8859_1, file_utf8, 6), 38,
131 "file.{accented e} should differ");
132 return true;
135 static bool test_next_token_null(struct torture_context *tctx)
137 char buf[20];
138 torture_assert(tctx, !next_token(NULL, buf, " ", 20), "null ptr works");
139 return true;
142 static bool test_next_token(struct torture_context *tctx)
144 const char *teststr = "foo bar bla";
145 char buf[20];
146 torture_assert(tctx, next_token(&teststr, buf, " ", 20), "finding token works");
147 torture_assert_str_equal(tctx, buf, "foo", "token matches");
148 torture_assert_str_equal(tctx, teststr, "bar bla", "ptr modified correctly");
150 torture_assert(tctx, next_token(&teststr, buf, " ", 20), "finding token works");
151 torture_assert_str_equal(tctx, buf, "bar", "token matches");
152 torture_assert_str_equal(tctx, teststr, "bla", "ptr modified correctly");
154 torture_assert(tctx, next_token(&teststr, buf, " ", 20), "finding token works");
155 torture_assert_str_equal(tctx, buf, "bla", "token matches");
156 torture_assert_str_equal(tctx, teststr, "", "ptr modified correctly");
158 torture_assert(tctx, !next_token(&teststr, buf, " ", 20), "finding token doesn't work");
159 return true;
162 static bool test_next_token_implicit_sep(struct torture_context *tctx)
164 const char *teststr = "foo\tbar\n bla";
165 char buf[20];
166 torture_assert(tctx, next_token(&teststr, buf, NULL, 20), "finding token works");
167 torture_assert_str_equal(tctx, buf, "foo", "token matches");
168 torture_assert_str_equal(tctx, teststr, "bar\n bla", "ptr modified correctly");
170 torture_assert(tctx, next_token(&teststr, buf, NULL, 20), "finding token works");
171 torture_assert_str_equal(tctx, buf, "bar", "token matches");
172 torture_assert_str_equal(tctx, teststr, " bla", "ptr modified correctly");
174 torture_assert(tctx, next_token(&teststr, buf, NULL, 20), "finding token works");
175 torture_assert_str_equal(tctx, buf, "bla", "token matches");
176 torture_assert_str_equal(tctx, teststr, "", "ptr modified correctly");
178 torture_assert(tctx, !next_token(&teststr, buf, NULL, 20), "finding token doesn't work");
179 return true;
182 static bool test_next_token_seps(struct torture_context *tctx)
184 const char *teststr = ",foo bla";
185 char buf[20];
186 torture_assert(tctx, next_token(&teststr, buf, ",", 20), "finding token works");
187 torture_assert_str_equal(tctx, buf, "foo bla", "token matches");
188 torture_assert_str_equal(tctx, teststr, "", "ptr modified correctly");
190 torture_assert(tctx, !next_token(&teststr, buf, ",", 20), "finding token doesn't work");
191 return true;
194 static bool test_next_token_quotes(struct torture_context *tctx)
196 const char *teststr = "\"foo bar\" bla";
197 char buf[20];
198 torture_assert(tctx, next_token(&teststr, buf, " ", 20), "finding token works");
199 torture_assert_str_equal(tctx, buf, "foo bar", "token matches");
200 torture_assert_str_equal(tctx, teststr, "bla", "ptr modified correctly");
202 torture_assert(tctx, next_token(&teststr, buf, " ", 20), "finding token works");
203 torture_assert_str_equal(tctx, buf, "bla", "token matches");
204 torture_assert_str_equal(tctx, teststr, "", "ptr modified correctly");
206 torture_assert(tctx, !next_token(&teststr, buf, " ", 20), "finding token doesn't work");
207 return true;
210 static bool test_next_token_quote_wrong(struct torture_context *tctx)
212 const char *teststr = "\"foo bar bla";
213 char buf[20];
214 torture_assert(tctx, next_token(&teststr, buf, " ", 20), "finding token works");
215 torture_assert_str_equal(tctx, buf, "foo bar bla", "token matches");
216 torture_assert_str_equal(tctx, teststr, "", "ptr modified correctly");
218 torture_assert(tctx, !next_token(&teststr, buf, " ", 20), "finding token doesn't work");
219 return true;
222 static bool test_strlen_m(struct torture_context *tctx)
224 torture_assert_int_equal(tctx, strlen_m("foo"), 3, "simple len");
225 torture_assert_int_equal(tctx, strlen_m("foo\x83l"), 6, "extended len");
226 torture_assert_int_equal(tctx, strlen_m(""), 0, "empty");
227 torture_assert_int_equal(tctx, strlen_m(NULL), 0, "NULL");
228 return true;
231 static bool test_strlen_m_term(struct torture_context *tctx)
233 torture_assert_int_equal(tctx, strlen_m_term("foo"), 4, "simple len");
234 torture_assert_int_equal(tctx, strlen_m_term("foo\x83l"), 7, "extended len");
235 torture_assert_int_equal(tctx, strlen_m_term(""), 1, "empty");
236 torture_assert_int_equal(tctx, strlen_m_term(NULL), 0, "NULL");
237 return true;
240 static bool test_strlen_m_term_null(struct torture_context *tctx)
242 torture_assert_int_equal(tctx, strlen_m_term_null("foo"), 4, "simple len");
243 torture_assert_int_equal(tctx, strlen_m_term_null("foo\x83l"), 7, "extended len");
244 torture_assert_int_equal(tctx, strlen_m_term_null(""), 0, "empty");
245 torture_assert_int_equal(tctx, strlen_m_term_null(NULL), 0, "NULL");
246 return true;
249 static bool test_strhaslower(struct torture_context *tctx)
251 torture_assert(tctx, strhaslower("a"), "one low char");
252 torture_assert(tctx, strhaslower("aB"), "one low, one up char");
253 torture_assert(tctx, !strhaslower("B"), "one up char");
254 torture_assert(tctx, !strhaslower(""), "empty string");
255 torture_assert(tctx, !strhaslower("3"), "one digit");
256 return true;
259 static bool test_strhasupper(struct torture_context *tctx)
261 torture_assert(tctx, strhasupper("B"), "one up char");
262 torture_assert(tctx, strhasupper("aB"), "one low, one up char");
263 torture_assert(tctx, !strhasupper("a"), "one low char");
264 torture_assert(tctx, !strhasupper(""), "empty string");
265 torture_assert(tctx, !strhasupper("3"), "one digit");
266 return true;
269 static bool test_count_chars_m(struct torture_context *tctx)
271 torture_assert_int_equal(tctx, count_chars_m("foo", 'o'), 2, "simple");
272 torture_assert_int_equal(tctx, count_chars_m("", 'o'), 0, "empty");
273 torture_assert_int_equal(tctx, count_chars_m("bla", 'o'), 0, "none");
274 torture_assert_int_equal(tctx, count_chars_m("bla", '\0'), 0, "null");
275 return true;
278 struct torture_suite *torture_local_charset(TALLOC_CTX *mem_ctx)
280 struct torture_suite *suite = torture_suite_create(mem_ctx, "charset");
282 torture_suite_add_simple_test(suite, "toupper_m", test_toupper_m);
283 torture_suite_add_simple_test(suite, "tolower_m", test_tolower_m);
284 torture_suite_add_simple_test(suite, "codepoint_cmpi", test_codepoint_cmpi);
285 torture_suite_add_simple_test(suite, "strcasecmp_m", test_strcasecmp_m);
286 torture_suite_add_simple_test(suite, "strequal_m", test_strequal_m);
287 torture_suite_add_simple_test(suite, "strcsequal", test_strcsequal);
288 torture_suite_add_simple_test(suite, "string_replace_m", test_string_replace_m);
289 torture_suite_add_simple_test(suite, "strncasecmp_m", test_strncasecmp_m);
290 torture_suite_add_simple_test(suite, "next_token", test_next_token);
291 torture_suite_add_simple_test(suite, "next_token_null", test_next_token_null);
292 torture_suite_add_simple_test(suite, "next_token_implicit_sep", test_next_token_implicit_sep);
293 torture_suite_add_simple_test(suite, "next_token_quotes", test_next_token_quotes);
294 torture_suite_add_simple_test(suite, "next_token_seps", test_next_token_seps);
295 torture_suite_add_simple_test(suite, "next_token_quote_wrong", test_next_token_quote_wrong);
296 torture_suite_add_simple_test(suite, "strlen_m", test_strlen_m);
297 torture_suite_add_simple_test(suite, "strlen_m_term", test_strlen_m_term);
298 torture_suite_add_simple_test(suite, "strlen_m_term_null", test_strlen_m_term_null);
299 torture_suite_add_simple_test(suite, "strhaslower", test_strhaslower);
300 torture_suite_add_simple_test(suite, "strhasupper", test_strhasupper);
301 torture_suite_add_simple_test(suite, "count_chars_m", test_count_chars_m);
303 return suite;