| 1 |
/* |
| 2 |
This file is part of libmicrohttpd |
| 3 |
Copyright (C) 2017-2021 Karlson2k (Evgeny Grin) |
| 4 |
|
| 5 |
This test tool is free software; you can redistribute it and/or |
| 6 |
modify it under the terms of the GNU General Public License as |
| 7 |
published by the Free Software Foundation; either version 2, or |
| 8 |
(at your option) any later version. |
| 9 |
|
| 10 |
This test tool is distributed in the hope that it will be useful, |
| 11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
Lesser General Public License for more details. |
| 14 |
|
| 15 |
You should have received a copy of the GNU Lesser General Public |
| 16 |
License along with this library; if not, write to the Free Software |
| 17 |
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 |
*/ |
| 19 |
|
| 20 |
/** |
| 21 |
* @file microhttpd/test_str_token.c |
| 22 |
* @brief Unit tests for MHD_str_remove_tokens_caseless_() function |
| 23 |
* @author Karlson2k (Evgeny Grin) |
| 24 |
*/ |
| 25 |
|
| 26 |
#include "mhd_options.h" |
| 27 |
#include <string.h> |
| 28 |
#include <stdio.h> |
| 29 |
#include "mhd_str.h" |
| 30 |
#include "mhd_assert.h" |
| 31 |
|
| 32 |
|
| 33 |
static int |
| 34 |
expect_result_n (const char *str, size_t str_len, |
| 35 |
const char *tokens, size_t tokens_len, |
| 36 |
const char *expected, size_t expected_len, |
| 37 |
const bool expected_removed) |
| 38 |
{ |
| 39 |
char buf_in[1024]; |
| 40 |
char buf_tokens[256]; |
| 41 |
bool res; |
| 42 |
size_t result_len; |
| 43 |
|
| 44 |
mhd_assert (sizeof(buf_in) > str_len + 2); |
| 45 |
mhd_assert (sizeof(buf_tokens) > tokens_len + 2); |
| 46 |
|
| 47 |
memset (buf_tokens, '#', sizeof(buf_tokens)); |
| 48 |
memcpy (buf_tokens, tokens, tokens_len); /* Copy without zero-termination */ |
| 49 |
memset (buf_in, '$', sizeof(buf_in)); |
| 50 |
memcpy (buf_in, str, str_len); /* Copy without zero-termination */ |
| 51 |
|
| 52 |
result_len = str_len; |
| 53 |
|
| 54 |
res = MHD_str_remove_tokens_caseless_ (buf_in, &result_len, |
| 55 |
buf_tokens, tokens_len); |
| 56 |
|
| 57 |
if ( (expected_removed != res) || |
| 58 |
(expected_len != result_len) || |
| 59 |
((0 != result_len) && (0 != memcmp (expected, buf_in, result_len))) || |
| 60 |
('$' != buf_in[str_len])) |
| 61 |
{ |
| 62 |
fprintf (stderr, |
| 63 |
"MHD_str_remove_tokens_caseless_() FAILED:\n" |
| 64 |
"\tRESULT: " |
| 65 |
"\tMHD_str_remove_token_caseless_(\"%s\"->\"%.*s\", &(%lu->%lu)," |
| 66 |
" \"%.*s\", %lu) returned %s\n", |
| 67 |
str, |
| 68 |
(int) result_len, buf_in, |
| 69 |
(unsigned long) str_len, (unsigned long) result_len, |
| 70 |
(int) tokens_len, buf_tokens, (unsigned long) tokens_len, |
| 71 |
res ? "true" : "false"); |
| 72 |
fprintf (stderr, |
| 73 |
"\tEXPECTED: " |
| 74 |
"\tMHD_str_remove_token_caseless_(\"%s\"->\"%s\", &(%lu->%lu)," |
| 75 |
" \"%.*s\", %lu) returned %s\n", |
| 76 |
str, |
| 77 |
expected, |
| 78 |
(unsigned long) str_len, (unsigned long) expected_len, |
| 79 |
(int) tokens_len, buf_tokens, (unsigned long) tokens_len, |
| 80 |
expected_removed ? "true" : "false"); |
| 81 |
return 1; |
| 82 |
} |
| 83 |
return 0; |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
#define expect_result(s,t,e,found) \ |
| 88 |
expect_result_n ((s),MHD_STATICSTR_LEN_ (s), \ |
| 89 |
(t),MHD_STATICSTR_LEN_ (t), \ |
| 90 |
(e),MHD_STATICSTR_LEN_ (e), found) |
| 91 |
|
| 92 |
static int |
| 93 |
check_result (void) |
| 94 |
{ |
| 95 |
int errcount = 0; |
| 96 |
errcount += expect_result ("string", "string", "", true); |
| 97 |
errcount += expect_result ("String", "string", "", true); |
| 98 |
errcount += expect_result ("string", "String", "", true); |
| 99 |
errcount += expect_result ("strinG", "String", "", true); |
| 100 |
errcount += expect_result ("strinG", "String\t", "", true); |
| 101 |
errcount += expect_result ("strinG", "\tString", "", true); |
| 102 |
errcount += expect_result ("tOkEn", " \t toKEN ", "", true); |
| 103 |
errcount += expect_result ("not-token, tOkEn", "token", "not-token", |
| 104 |
true); |
| 105 |
errcount += expect_result ("not-token1, tOkEn1, token", "token1", |
| 106 |
"not-token1, token", |
| 107 |
true); |
| 108 |
errcount += expect_result ("token, tOkEn1", "token1", "token", |
| 109 |
true); |
| 110 |
errcount += expect_result ("not-token, tOkEn", " \t toKEN", "not-token", |
| 111 |
true); |
| 112 |
errcount += expect_result ("not-token, tOkEn, more-token", "toKEN\t", |
| 113 |
"not-token, more-token", true); |
| 114 |
errcount += expect_result ("not-token, tOkEn, more-token", "\t toKEN,,,,,", |
| 115 |
"not-token, more-token", true); |
| 116 |
errcount += expect_result ("a, b, c, d", ",,,,,a", "b, c, d", true); |
| 117 |
errcount += expect_result ("a, b, c, d", "a,,,,,,", "b, c, d", true); |
| 118 |
errcount += expect_result ("a, b, c, d", ",,,,a,,,,,,", "b, c, d", true); |
| 119 |
errcount += expect_result ("a, b, c, d", "\t \t,,,,a,, , ,,,\t", |
| 120 |
"b, c, d", true); |
| 121 |
errcount += expect_result ("a, b, c, d", "b, c, d", "a", true); |
| 122 |
errcount += expect_result ("a, b, c, d", "a, b, c, d", "", true); |
| 123 |
errcount += expect_result ("a, b, c, d", "d, c, b, a", "", true); |
| 124 |
errcount += expect_result ("a, b, c, d", "b, d, a, c", "", true); |
| 125 |
errcount += expect_result ("a, b, c, d, e", "b, d, a, c", "e", true); |
| 126 |
errcount += expect_result ("e, a, b, c, d", "b, d, a, c", "e", true); |
| 127 |
errcount += expect_result ("e, a, b, c, d, e", "b, d, a, c", "e, e", true); |
| 128 |
errcount += expect_result ("a, b, c, d", "b,c,d", "a", true); |
| 129 |
errcount += expect_result ("a, b, c, d", "a,b,c,d", "", true); |
| 130 |
errcount += expect_result ("a, b, c, d", "d,c,b,a", "", true); |
| 131 |
errcount += expect_result ("a, b, c, d", "b,d,a,c", "", true); |
| 132 |
errcount += expect_result ("a, b, c, d, e", "b,d,a,c", "e", true); |
| 133 |
errcount += expect_result ("e, a, b, c, d", "b,d,a,c", "e", true); |
| 134 |
errcount += expect_result ("e, a, b, c, d, e", "b,d,a,c", "e, e", true); |
| 135 |
errcount += expect_result ("a, b, c, d", "d,,,,,,,,,c,b,a", "", true); |
| 136 |
errcount += expect_result ("a, b, c, d", "b,d,a,c,,,,,,,,,,", "", true); |
| 137 |
errcount += expect_result ("a, b, c, d, e", ",,,,\t,,,,b,d,a,c,\t", "e", |
| 138 |
true); |
| 139 |
errcount += expect_result ("e, a, b, c, d", "b,d,a,c", "e", true); |
| 140 |
errcount += expect_result ("token, a, b, c, d", "token", "a, b, c, d", true); |
| 141 |
errcount += expect_result ("token1, a, b, c, d", "token1", "a, b, c, d", |
| 142 |
true); |
| 143 |
errcount += expect_result ("token12, a, b, c, d", "token12", "a, b, c, d", |
| 144 |
true); |
| 145 |
errcount += expect_result ("token123, a, b, c, d", "token123", "a, b, c, d", |
| 146 |
true); |
| 147 |
errcount += expect_result ("token1234, a, b, c, d", "token1234", "a, b, c, d", |
| 148 |
true); |
| 149 |
errcount += expect_result ("token12345, a, b, c, d", "token12345", |
| 150 |
"a, b, c, d", true); |
| 151 |
errcount += expect_result ("token123456, a, b, c, d", "token123456", |
| 152 |
"a, b, c, d", true); |
| 153 |
errcount += expect_result ("token1234567, a, b, c, d", "token1234567", |
| 154 |
"a, b, c, d", true); |
| 155 |
errcount += expect_result ("token12345678, a, b, c, d", "token12345678", |
| 156 |
"a, b, c, d", true); |
| 157 |
|
| 158 |
errcount += expect_result ("", "a", "", false); |
| 159 |
errcount += expect_result ("", "", "", false); |
| 160 |
errcount += expect_result ("a, b, c, d", "bb, dd, aa, cc", "a, b, c, d", |
| 161 |
false); |
| 162 |
errcount += expect_result ("a, b, c, d, e", "bb, dd, aa, cc", "a, b, c, d, e", |
| 163 |
false); |
| 164 |
errcount += expect_result ("e, a, b, c, d", "bb, dd, aa, cc", "e, a, b, c, d", |
| 165 |
false); |
| 166 |
errcount += expect_result ("e, a, b, c, d, e", "bb, dd, aa, cc", |
| 167 |
"e, a, b, c, d, e", false); |
| 168 |
errcount += expect_result ("aa, bb, cc, dd", "b, d, a, c", "aa, bb, cc, dd", |
| 169 |
false); |
| 170 |
errcount += expect_result ("aa, bb, cc, dd, ee", "b, d, a, c", |
| 171 |
"aa, bb, cc, dd, ee", false); |
| 172 |
errcount += expect_result ("ee, aa, bb, cc, dd", "b, d, a, c", |
| 173 |
"ee, aa, bb, cc, dd", false); |
| 174 |
errcount += expect_result ("ee, aa, bb, cc, dd, ee", "b, d, a, c", |
| 175 |
"ee, aa, bb, cc, dd, ee", false); |
| 176 |
|
| 177 |
errcount += expect_result ("TESt", ",,,,,,test,,,,", "", true); |
| 178 |
errcount += expect_result ("TESt", ",,,,,\t,test,,,,", "", true); |
| 179 |
errcount += expect_result ("TESt", ",,,,,,test, ,,,", "", true); |
| 180 |
errcount += expect_result ("TESt", ",,,,,, test,,,,", "", true); |
| 181 |
errcount += expect_result ("TESt", ",,,,,, test-not,test,,", "", |
| 182 |
true); |
| 183 |
errcount += expect_result ("TESt", ",,,,,, test-not,,test,,", "", |
| 184 |
true); |
| 185 |
errcount += expect_result ("TESt", ",,,,,, test-not ,test,,", "", |
| 186 |
true); |
| 187 |
errcount += expect_result ("TESt", ",,,,,, test", "", true); |
| 188 |
errcount += expect_result ("TESt", ",,,,,, test ", "", true); |
| 189 |
errcount += expect_result ("TESt", "no-test,,,,,, test ", "", |
| 190 |
true); |
| 191 |
|
| 192 |
errcount += expect_result ("the-token, a, the-token, b, the-token, " \ |
| 193 |
"the-token, c, the-token", "the-token", "a, b, c", |
| 194 |
true); |
| 195 |
errcount += expect_result ("aa, the-token, bb, the-token, cc, the-token, " \ |
| 196 |
"the-token, dd, the-token", "the-token", |
| 197 |
"aa, bb, cc, dd", true); |
| 198 |
errcount += expect_result ("the-token, a, the-token, b, the-token, " \ |
| 199 |
"the-token, c, the-token, e", "the-token", |
| 200 |
"a, b, c, e", true); |
| 201 |
errcount += expect_result ("aa, the-token, bb, the-token, cc, the-token, " \ |
| 202 |
"the-token, dd, the-token, ee", "the-token", |
| 203 |
"aa, bb, cc, dd, ee", true); |
| 204 |
errcount += expect_result ("the-token, the-token, the-token, " \ |
| 205 |
"the-token, the-token", "the-token", "", true); |
| 206 |
errcount += expect_result ("the-token, a, the-token, the-token, b, " \ |
| 207 |
"the-token, c, the-token, a", "c,a,b", |
| 208 |
"the-token, the-token, the-token, the-token, the-token", |
| 209 |
true); |
| 210 |
errcount += expect_result ("the-token, xx, the-token, the-token, zz, " \ |
| 211 |
"the-token, yy, the-token, ww", "ww,zz,yy", |
| 212 |
"the-token, xx, the-token, the-token, the-token, the-token", |
| 213 |
true); |
| 214 |
errcount += expect_result ("the-token, a, the-token, the-token, b, " \ |
| 215 |
"the-token, c, the-token, a", " c,\t a,b,,,", |
| 216 |
"the-token, the-token, the-token, the-token, the-token", |
| 217 |
true); |
| 218 |
errcount += expect_result ("the-token, xx, the-token, the-token, zz, " \ |
| 219 |
"the-token, yy, the-token, ww", |
| 220 |
",,,,ww,\t zz, yy", |
| 221 |
"the-token, xx, the-token, the-token, the-token, the-token", |
| 222 |
true); |
| 223 |
errcount += expect_result ("the-token, a, the-token, the-token, b, " \ |
| 224 |
"the-token, c, the-token, a", ",,,,c,\t a,b", |
| 225 |
"the-token, the-token, the-token, the-token, the-token", |
| 226 |
true); |
| 227 |
errcount += expect_result ("the-token, xx, the-token, the-token, zz, " \ |
| 228 |
"the-token, yy, the-token, ww", " ww,\t zz,yy,,,,", |
| 229 |
"the-token, xx, the-token, the-token, the-token, the-token", |
| 230 |
true); |
| 231 |
errcount += expect_result ("close, 2", "close", |
| 232 |
"2", true); |
| 233 |
errcount += expect_result ("close, 22", "close", |
| 234 |
"22", true); |
| 235 |
errcount += expect_result ("close, nothing", "close", |
| 236 |
"nothing", true); |
| 237 |
errcount += expect_result ("close, 2", "2", |
| 238 |
"close", true); |
| 239 |
errcount += expect_result ("close", "close", |
| 240 |
"", true); |
| 241 |
errcount += expect_result ("close, nothing", "close, token", |
| 242 |
"nothing", true); |
| 243 |
errcount += expect_result ("close, nothing", "nothing, token", |
| 244 |
"close", true); |
| 245 |
errcount += expect_result ("close, 2", "close, 10, 12, 22, nothing", |
| 246 |
"2", true); |
| 247 |
|
| 248 |
errcount += expect_result ("strin", "string", "strin", false); |
| 249 |
errcount += expect_result ("Stringer", "string", "Stringer", false); |
| 250 |
errcount += expect_result ("sstring", "String", "sstring", false); |
| 251 |
errcount += expect_result ("string", "Strin", "string", false); |
| 252 |
errcount += expect_result ("String", "\t(-strinG", "String", false); |
| 253 |
errcount += expect_result ("String", ")strinG\t ", "String", false); |
| 254 |
errcount += expect_result ("not-token, tOkEner", "toKEN", |
| 255 |
"not-token, tOkEner", false); |
| 256 |
errcount += expect_result ("not-token, tOkEns, more-token", "toKEN", |
| 257 |
"not-token, tOkEns, more-token", false); |
| 258 |
errcount += expect_result ("tests, quest", "TESt", "tests, quest", |
| 259 |
false); |
| 260 |
errcount += expect_result ("testы", "TESt", "testы", false); |
| 261 |
errcount += expect_result ("test-not, хtest", "TESt", |
| 262 |
"test-not, хtest", false); |
| 263 |
errcount += expect_result ("testing, test not, test2", "TESt", |
| 264 |
"testing, test not, test2", false); |
| 265 |
errcount += expect_result ("", ",,,,,,,,,,,,,,,,,,,the-token", "", false); |
| 266 |
errcount += expect_result ("a1, b1, c1, d1, e1, f1, g1", "", |
| 267 |
"a1, b1, c1, d1, e1, f1, g1", false); |
| 268 |
|
| 269 |
return errcount; |
| 270 |
} |
| 271 |
|
| 272 |
|
| 273 |
int |
| 274 |
main (int argc, char *argv[]) |
| 275 |
{ |
| 276 |
int errcount = 0; |
| 277 |
(void) argc; (void) argv; /* Unused. Silent compiler warning. */ |
| 278 |
errcount += check_result (); |
| 279 |
if (0 == errcount) |
| 280 |
printf ("All tests were passed without errors.\n"); |
| 281 |
return errcount == 0 ? 0 : 1; |
| 282 |
} |