s4: tests: Added local.charset test for Bug 10716 - smbd constantly crashes when...
[gd/samba-autobuild/.git] / lib / util / charset / tests / charset.c
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 struct torture_suite *torture_local_charset(TALLOC_CTX *mem_ctx);
25
26 static bool test_toupper_m(struct torture_context *tctx)
27 {
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;
32 }
33
34 static bool test_tolower_m(struct torture_context *tctx)
35 {
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;
40 }
41
42 static bool test_codepoint_cmpi(struct torture_context *tctx)
43 {
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;
49 }
50
51 static bool test_strcasecmp_m(struct torture_context *tctx)
52 {
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(tctx, strcasecmp_m("foo", "bar") != 0, "different strings");
58         torture_assert(tctx, strcasecmp_m("foo", "foo") == 0, "same case strings");
59         torture_assert(tctx, strcasecmp_m("foo", "Foo") == 0, "different case strings");
60         torture_assert(tctx, strcasecmp_m(NULL, "Foo") != 0, "one NULL");
61         torture_assert(tctx, strcasecmp_m("foo", NULL) != 0, "other NULL");
62         torture_assert(tctx, strcasecmp_m(NULL, NULL) == 0, "both NULL");
63         torture_assert(tctx, strcasecmp_m(file_iso8859_1, file_utf8) != 0,
64                 "file.{accented e} should differ");
65         return true;
66 }
67
68
69 static bool test_strequal_m(struct torture_context *tctx)
70 {
71         torture_assert(tctx, !strequal_m("foo", "bar"), "different strings");
72         torture_assert(tctx, strequal_m("foo", "foo"), "same case strings");
73         torture_assert(tctx, strequal_m("foo", "Foo"), "different case strings");
74         torture_assert(tctx, !strequal_m(NULL, "Foo"), "one NULL");
75         torture_assert(tctx, !strequal_m("foo", NULL), "other NULL");
76         torture_assert(tctx, strequal_m(NULL, NULL), "both NULL");
77         return true;
78 }
79
80 static bool test_strcsequal(struct torture_context *tctx)
81 {
82         torture_assert(tctx, !strcsequal("foo", "bar"), "different strings");
83         torture_assert(tctx, strcsequal("foo", "foo"), "same case strings");
84         torture_assert(tctx, !strcsequal("foo", "Foo"), "different case strings");
85         torture_assert(tctx, !strcsequal(NULL, "Foo"), "one NULL");
86         torture_assert(tctx, !strcsequal("foo", NULL), "other NULL");
87         torture_assert(tctx, strcsequal(NULL, NULL), "both NULL");
88         return true;
89 }
90
91 static bool test_string_replace_m(struct torture_context *tctx)
92 {
93         char data[6] = "bla";
94         string_replace_m(data, 'b', 'c');
95         torture_assert_str_equal(tctx, data, "cla", "first char replaced");
96         memcpy(data, "bab", 4);
97         string_replace_m(data, 'b', 'c');
98         torture_assert_str_equal(tctx, data, "cac", "other chars replaced");
99         memcpy(data, "bba", 4);
100         string_replace_m(data, 'b', 'c');
101         torture_assert_str_equal(tctx, data, "cca", "other chars replaced");
102         memcpy(data, "blala", 6);
103         string_replace_m(data, 'o', 'c');
104         torture_assert_str_equal(tctx, data, "blala", "no chars replaced");
105         string_replace_m(NULL, 'b', 'c');
106         return true;
107 }
108
109 static bool test_strncasecmp_m(struct torture_context *tctx)
110 {
111         /* file.{accented e} in iso8859-1 */
112         const char file_iso8859_1[7] = { 0x66, 0x69, 0x6c, 0x65, 0x2d, 0xe9, 0 };
113         /* file.{accented e} in utf8 */
114         const char file_utf8[8] =      { 0x66, 0x69, 0x6c, 0x65, 0x2d, 0xc3, 0xa9, 0 };
115         torture_assert(tctx, strncasecmp_m("foo", "bar", 3) != 0, "different strings");
116         torture_assert(tctx, strncasecmp_m("foo", "foo", 3) == 0, "same case strings");
117         torture_assert(tctx, strncasecmp_m("foo", "Foo", 3) == 0, "different case strings");
118         torture_assert(tctx, strncasecmp_m("fool", "Foo", 3) == 0, "different case strings");
119         torture_assert(tctx, strncasecmp_m("fool", "Fool", 40) == 0, "over size");
120         torture_assert(tctx, strncasecmp_m("BLA", "Fool", 0) == 0, "empty");
121         torture_assert(tctx, strncasecmp_m(NULL, "Foo", 3) != 0, "one NULL");
122         torture_assert(tctx, strncasecmp_m("foo", NULL, 3) != 0, "other NULL");
123         torture_assert(tctx, strncasecmp_m(NULL, NULL, 3) == 0, "both NULL");
124         torture_assert(tctx, strncasecmp_m(file_iso8859_1, file_utf8, 6) != 0,
125                 "file.{accented e} should differ");
126         return true;
127 }
128
129 static bool test_next_token_null(struct torture_context *tctx)
130 {
131         char buf[20];
132         torture_assert(tctx, !next_token(NULL, buf, " ", 20), "null ptr works");
133         return true;
134 }
135
136 static bool test_next_token(struct torture_context *tctx)
137 {
138         const char *teststr = "foo bar bla";
139         char buf[20];
140         torture_assert(tctx, next_token(&teststr, buf, " ", 20), "finding token works");
141         torture_assert_str_equal(tctx, buf, "foo", "token matches");
142         torture_assert_str_equal(tctx, teststr, "bar bla", "ptr modified correctly");
143
144         torture_assert(tctx, next_token(&teststr, buf, " ", 20), "finding token works");
145         torture_assert_str_equal(tctx, buf, "bar", "token matches");
146         torture_assert_str_equal(tctx, teststr, "bla", "ptr modified correctly");
147
148         torture_assert(tctx, next_token(&teststr, buf, " ", 20), "finding token works");
149         torture_assert_str_equal(tctx, buf, "bla", "token matches");
150         torture_assert_str_equal(tctx, teststr, "", "ptr modified correctly");
151
152         torture_assert(tctx, !next_token(&teststr, buf, " ", 20), "finding token doesn't work");
153         return true;
154 }
155
156 static bool test_next_token_implicit_sep(struct torture_context *tctx)
157 {
158         const char *teststr = "foo\tbar\n bla";
159         char buf[20];
160         torture_assert(tctx, next_token(&teststr, buf, NULL, 20), "finding token works");
161         torture_assert_str_equal(tctx, buf, "foo", "token matches");
162         torture_assert_str_equal(tctx, teststr, "bar\n bla", "ptr modified correctly");
163
164         torture_assert(tctx, next_token(&teststr, buf, NULL, 20), "finding token works");
165         torture_assert_str_equal(tctx, buf, "bar", "token matches");
166         torture_assert_str_equal(tctx, teststr, " bla", "ptr modified correctly");
167
168         torture_assert(tctx, next_token(&teststr, buf, NULL, 20), "finding token works");
169         torture_assert_str_equal(tctx, buf, "bla", "token matches");
170         torture_assert_str_equal(tctx, teststr, "", "ptr modified correctly");
171
172         torture_assert(tctx, !next_token(&teststr, buf, NULL, 20), "finding token doesn't work");
173         return true;
174 }
175
176 static bool test_next_token_seps(struct torture_context *tctx)
177 {
178         const char *teststr = ",foo bla";
179         char buf[20];
180         torture_assert(tctx, next_token(&teststr, buf, ",", 20), "finding token works");
181         torture_assert_str_equal(tctx, buf, "foo bla", "token matches");
182         torture_assert_str_equal(tctx, teststr, "", "ptr modified correctly");
183
184         torture_assert(tctx, !next_token(&teststr, buf, ",", 20), "finding token doesn't work");
185         return true;
186 }
187
188 static bool test_next_token_quotes(struct torture_context *tctx)
189 {
190         const char *teststr = "\"foo bar\" bla";
191         char buf[20];
192         torture_assert(tctx, next_token(&teststr, buf, " ", 20), "finding token works");
193         torture_assert_str_equal(tctx, buf, "foo bar", "token matches");
194         torture_assert_str_equal(tctx, teststr, "bla", "ptr modified correctly");
195
196         torture_assert(tctx, next_token(&teststr, buf, " ", 20), "finding token works");
197         torture_assert_str_equal(tctx, buf, "bla", "token matches");
198         torture_assert_str_equal(tctx, teststr, "", "ptr modified correctly");
199
200         torture_assert(tctx, !next_token(&teststr, buf, " ", 20), "finding token doesn't work");
201         return true;
202 }
203
204 static bool test_next_token_quote_wrong(struct torture_context *tctx)
205 {
206         const char *teststr = "\"foo bar bla";
207         char buf[20];
208         torture_assert(tctx, next_token(&teststr, buf, " ", 20), "finding token works");
209         torture_assert_str_equal(tctx, buf, "foo bar bla", "token matches");
210         torture_assert_str_equal(tctx, teststr, "", "ptr modified correctly");
211
212         torture_assert(tctx, !next_token(&teststr, buf, " ", 20), "finding token doesn't work");
213         return true;
214 }
215
216 static bool test_strlen_m(struct torture_context *tctx)
217 {
218         torture_assert_int_equal(tctx, strlen_m("foo"), 3, "simple len");
219         torture_assert_int_equal(tctx, strlen_m("foo\x83l"), 6, "extended len");
220         torture_assert_int_equal(tctx, strlen_m(NULL), 0, "NULL");
221         return true;
222 }
223
224 static bool test_strlen_m_term(struct torture_context *tctx)
225 {
226         torture_assert_int_equal(tctx, strlen_m_term("foo"), 4, "simple len");
227         torture_assert_int_equal(tctx, strlen_m_term("foo\x83l"), 7, "extended len");
228         torture_assert_int_equal(tctx, strlen_m(NULL), 0, "NULL");
229         return true;
230 }
231
232 static bool test_strhaslower(struct torture_context *tctx)
233 {
234         torture_assert(tctx, strhaslower("a"), "one low char");
235         torture_assert(tctx, strhaslower("aB"), "one low, one up char");
236         torture_assert(tctx, !strhaslower("B"), "one up char");
237         torture_assert(tctx, !strhaslower(""), "empty string");
238         torture_assert(tctx, !strhaslower("3"), "one digit");
239         return true;
240 }
241
242 static bool test_strhasupper(struct torture_context *tctx)
243 {
244         torture_assert(tctx, strhasupper("B"), "one up char");
245         torture_assert(tctx, strhasupper("aB"), "one low, one up char");
246         torture_assert(tctx, !strhasupper("a"), "one low char");
247         torture_assert(tctx, !strhasupper(""), "empty string");
248         torture_assert(tctx, !strhasupper("3"), "one digit");
249         return true;
250 }
251
252 static bool test_count_chars_m(struct torture_context *tctx)
253 {
254         torture_assert_int_equal(tctx, count_chars_m("foo", 'o'), 2, "simple");
255         torture_assert_int_equal(tctx, count_chars_m("", 'o'), 0, "empty");
256         torture_assert_int_equal(tctx, count_chars_m("bla", 'o'), 0, "none");
257         torture_assert_int_equal(tctx, count_chars_m("bla", '\0'), 0, "null");
258         return true;
259 }
260
261 struct torture_suite *torture_local_charset(TALLOC_CTX *mem_ctx)
262 {
263         struct torture_suite *suite = torture_suite_create(mem_ctx, "charset");
264
265         torture_suite_add_simple_test(suite, "toupper_m", test_toupper_m);
266         torture_suite_add_simple_test(suite, "tolower_m", test_tolower_m);
267         torture_suite_add_simple_test(suite, "codepoint_cmpi", test_codepoint_cmpi);
268         torture_suite_add_simple_test(suite, "strcasecmp_m", test_strcasecmp_m);
269         torture_suite_add_simple_test(suite, "strequal_m", test_strequal_m);
270         torture_suite_add_simple_test(suite, "strcsequal", test_strcsequal);
271         torture_suite_add_simple_test(suite, "string_replace_m", test_string_replace_m);
272         torture_suite_add_simple_test(suite, "strncasecmp_m", test_strncasecmp_m);
273         torture_suite_add_simple_test(suite, "next_token", test_next_token);
274         torture_suite_add_simple_test(suite, "next_token_null", test_next_token_null);
275         torture_suite_add_simple_test(suite, "next_token_implicit_sep", test_next_token_implicit_sep);
276         torture_suite_add_simple_test(suite, "next_token_quotes", test_next_token_quotes);
277         torture_suite_add_simple_test(suite, "next_token_seps", test_next_token_seps);
278         torture_suite_add_simple_test(suite, "next_token_quote_wrong", test_next_token_quote_wrong);
279         torture_suite_add_simple_test(suite, "strlen_m", test_strlen_m);
280         torture_suite_add_simple_test(suite, "strlen_m_term", test_strlen_m_term);
281         torture_suite_add_simple_test(suite, "strhaslower", test_strhaslower);
282         torture_suite_add_simple_test(suite, "strhasupper", test_strhasupper);
283         torture_suite_add_simple_test(suite, "count_chars_m", test_count_chars_m);
284
285         return suite;
286 }