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