test_str_token_remove.c (12412B)
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_remove.c 22 * @brief Unit tests for mhd_str_remove_token_caseless() function 23 * @author Karlson2k (Evgeny Grin) 24 */ 25 26 #include "mhd_sys_options.h" 27 #include <string.h> 28 #include <stdio.h> 29 #include "../mhd2/mhd_str.h" 30 #include "../mhd2/mhd_str.c" 31 #include "mhd_assert.h" 32 33 34 #ifndef MHD_STATICSTR_LEN_ 35 /** 36 * Determine length of static string / macro strings at compile time. 37 */ 38 #define MHD_STATICSTR_LEN_(macro) (sizeof(macro) / sizeof(char) - 1) 39 #endif /* ! MHD_STATICSTR_LEN_ */ 40 41 42 static int 43 expect_result_n (const char *str, size_t str_len, 44 const char *token, size_t token_len, 45 const char *expected, size_t expected_len, 46 const bool expected_removed) 47 { 48 char buf_in[1024]; 49 char buf_token[256]; 50 char buf_out[1024]; 51 size_t buf_len; 52 53 mhd_assert (sizeof(buf_in) > str_len + 2); 54 mhd_assert (sizeof(buf_token) > token_len + 2); 55 mhd_assert (sizeof(buf_out) > expected_len + 2); 56 57 memset (buf_in, '#', sizeof(buf_in)); 58 memset (buf_token, '#', sizeof(buf_token)); 59 memcpy (buf_in, str, str_len); /* Copy without zero-termination */ 60 memcpy (buf_token, token, token_len); /* Copy without zero-termination */ 61 62 for (buf_len = 0; buf_len <= expected_len + 3; ++buf_len) 63 { 64 bool res; 65 ssize_t result_len; 66 memset (buf_out, '$', sizeof(buf_out)); 67 68 result_len = (ssize_t) buf_len; 69 mhd_assert (0 <= result_len); 70 71 res = mhd_str_remove_token_caseless (buf_in, str_len, buf_token, token_len, 72 buf_out, &result_len); 73 if (buf_len < expected_len) 74 { /* The result should not fit into the buffer */ 75 if (res || (0 <= result_len)) 76 { 77 fprintf (stderr, 78 "mhd_str_remove_token_caseless() FAILED:\n" 79 "\tmhd_str_remove_token_caseless(\"%.*s\", %lu," 80 " \"%.*s\", %lu, buf, &(%ld->%ld)) returned %s\n", 81 (int) str_len + 2, buf_in, (unsigned long) str_len, 82 (int) token_len + 2, buf_token, (unsigned long) token_len, 83 (long) buf_len, (long) result_len, res ? "true" : "false"); 84 return 1; 85 } 86 } 87 else 88 { /* The result should fit into the buffer */ 89 if ( (expected_removed != res) || 90 (result_len < 0) || 91 (expected_len != (size_t) result_len) || 92 ((0 != result_len) && (0 != memcmp (expected, buf_out, 93 (size_t) result_len))) || 94 ('$' != buf_out[result_len])) 95 { 96 fprintf (stderr, 97 "mhd_str_remove_token_caseless() FAILED:\n" 98 "\tmhd_str_remove_token_caseless(\"%.*s\", %lu," 99 " \"%.*s\", %lu, \"%.*s\", &(%ld->%ld)) returned %s\n", 100 (int) str_len + 2, buf_in, (unsigned long) str_len, 101 (int) token_len + 2, buf_token, (unsigned long) token_len, 102 (int) expected_len + 2, buf_out, 103 (long) buf_len, (long) result_len, 104 res ? "true" : "false"); 105 return 1; 106 } 107 } 108 } 109 return 0; 110 } 111 112 113 #define expect_result(s,t,e,found) \ 114 expect_result_n ((s),MHD_STATICSTR_LEN_ (s), \ 115 (t),MHD_STATICSTR_LEN_ (t), \ 116 (e),MHD_STATICSTR_LEN_ (e), found) 117 118 static int 119 check_result (void) 120 { 121 int errcount = 0; 122 errcount += expect_result ("string", "string", "", true); 123 errcount += expect_result ("String", "string", "", true); 124 errcount += expect_result ("string", "String", "", true); 125 errcount += expect_result ("strinG", "String", "", true); 126 errcount += expect_result ("\t strinG", "String", "", true); 127 errcount += expect_result ("strinG\t ", "String", "", true); 128 errcount += expect_result (" \t tOkEn ", "toKEN", "", true); 129 errcount += expect_result ("not token\t, tOkEn ", "toKEN", "not token", 130 true); 131 errcount += expect_result ("not token,\t tOkEn, more token", "toKEN", 132 "not token, more token", true); 133 errcount += expect_result ("not token,\t tOkEn\t, more token", "toKEN", 134 "not token, more token", true); 135 errcount += expect_result (",,,,,,test,,,,", "TESt", "", true); 136 errcount += expect_result (",,,,,\t,test,,,,", "TESt", "", true); 137 errcount += expect_result (",,,,,,test, ,,,", "TESt", "", true); 138 errcount += expect_result (",,,,,, test,,,,", "TESt", "", true); 139 errcount += expect_result (",,,,,, test not,test,,", "TESt", "test not", 140 true); 141 errcount += expect_result (",,,,,, test not,,test,,", "TESt", "test not", 142 true); 143 errcount += expect_result (",,,,,, test not ,test,,", "TESt", "test not", 144 true); 145 errcount += expect_result (",,,,,, test", "TESt", "", true); 146 errcount += expect_result (",,,,,, test ", "TESt", "", true); 147 errcount += expect_result ("no test,,,,,, test ", "TESt", "no test", 148 true); 149 errcount += expect_result ("the-token,, the-token , the-token" \ 150 ",the-token ,the-token", "the-token", "", true); 151 errcount += expect_result (" the-token,, the-token , the-token," \ 152 "the-token ,the-token ", "the-token", "", true); 153 errcount += expect_result (" the-token ,, the-token , the-token," \ 154 "the-token , the-token ", "the-token", "", true); 155 errcount += expect_result ("the-token,a, the-token , the-token,b," \ 156 "the-token , c,the-token", "the-token", "a, b, c", 157 true); 158 errcount += expect_result (" the-token, a, the-token , the-token, b," \ 159 "the-token ,c ,the-token ", "the-token", 160 "a, b, c", true); 161 errcount += expect_result (" the-token , a , the-token , the-token, b ," \ 162 "the-token , c , the-token ", "the-token", 163 "a, b, c",true); 164 errcount += expect_result ("the-token,aa, the-token , the-token,bb," \ 165 "the-token , cc,the-token", "the-token", 166 "aa, bb, cc", true); 167 errcount += expect_result (" the-token, aa, the-token , the-token, bb," \ 168 "the-token ,cc ,the-token ", "the-token", 169 "aa, bb, cc", true); 170 errcount += expect_result (" the-token , aa , the-token , the-token, bb ," \ 171 "the-token , cc , the-token ", "the-token", 172 "aa, bb, cc", true); 173 174 errcount += expect_result ("strin", "string", "strin", false); 175 errcount += expect_result ("Stringer", "string", "Stringer", false); 176 errcount += expect_result ("sstring", "String", "sstring", false); 177 errcount += expect_result ("string", "Strin", "string", false); 178 errcount += expect_result ("\t( strinG", "String", "( strinG", false); 179 errcount += expect_result (")strinG\t ", "String", ")strinG", false); 180 errcount += expect_result (" \t tOkEn t ", "toKEN", "tOkEn t", false); 181 errcount += expect_result ("not token\t, tOkEner ", "toKEN", 182 "not token, tOkEner", false); 183 errcount += expect_result ("not token,\t tOkEns, more token", "toKEN", 184 "not token, tOkEns, more token", false); 185 errcount += expect_result ("not token,\t tOkEns\t, more token", "toKEN", 186 "not token, tOkEns, more token", false); 187 errcount += expect_result (",,,,,,testing,,,,", "TESt", "testing", false); 188 errcount += expect_result (",,,,,\t,test,,,,", "TESting", "test", false); 189 errcount += expect_result ("tests,,,,,,quest, ,,,", "TESt", "tests, quest", 190 false); 191 errcount += expect_result (",,,,,, testы,,,,", "TESt", "testы", false); 192 errcount += expect_result (",,,,,, test not,хtest,,", "TESt", 193 "test not, хtest", false); 194 errcount += expect_result ("testing,,,,,, test not,,test2,,", "TESt", 195 "testing, test not, test2", false); 196 errcount += expect_result (",testi,,,,, test not ,test,,", "TESting", 197 "testi, test not, test", false); 198 errcount += expect_result (",,,,,,2 test", "TESt", "2 test", false); 199 errcount += expect_result (",,,,,,test test ", "test", "test test", 200 false); 201 errcount += expect_result ("no test,,,,,,test test", "test", 202 "no test, test test", false); 203 errcount += expect_result (",,,,,,,,,,,,,,,,,,,", "the-token", "", false); 204 errcount += expect_result (",a,b,c,d,e,f,g,,,,,,,,,,,,", "the-token", 205 "a, b, c, d, e, f, g", false); 206 errcount += expect_result (",,,,,,,,,,,,,,,,,,,", "", "", false); 207 errcount += expect_result (",a,b,c,d,e,f,g,,,,,,,,,,,,", "", 208 "a, b, c, d, e, f, g", false); 209 errcount += expect_result ("a,b,c,d,e,f,g", "", "a, b, c, d, e, f, g", 210 false); 211 errcount += expect_result ("a1,b1,c1,d1,e1,f1,g1", "", 212 "a1, b1, c1, d1, e1, f1, g1", false); 213 214 errcount += expect_result (",a,b,c,d,e,f,g,,,,,,,,,,,,the-token", 215 "the-token", "a, b, c, d, e, f, g", true); 216 errcount += expect_result (",a,b,c,d,e,f,g,,,,,,,,,,,,the-token,", 217 "the-token", "a, b, c, d, e, f, g", true); 218 errcount += expect_result (",a,b,c,d,e,f,g,,,,,,,,,,,,the-token,x", 219 "the-token", "a, b, c, d, e, f, g, x", true); 220 errcount += expect_result (",a,b,c,d,e,f,g,,,,,,,,,,,,the-token x", 221 "the-token", "a, b, c, d, e, f, g, the-token x", 222 false); 223 errcount += expect_result (",a,b,c,d,e,f,g,,,,,,,,,,,,the-token x,", 224 "the-token", "a, b, c, d, e, f, g, the-token x", 225 false); 226 errcount += expect_result (",a,b,c,d,e,f,g,,,,,,,,,,,,the-token x,x", 227 "the-token", "a, b, c, d, e, f, g," \ 228 " the-token x, x", false); 229 errcount += expect_result ("the-token,a,b,c,d,e,f,g,,,,,,,,,,,,the-token", 230 "the-token", "a, b, c, d, e, f, g", true); 231 errcount += expect_result ("the-token ,a,b,c,d,e,f,g,,,,,,,,,,,,the-token,", 232 "the-token", "a, b, c, d, e, f, g", true); 233 errcount += expect_result ("the-token,a,b,c,d,e,f,g,,,,,,,,,,,,the-token,x", 234 "the-token", "a, b, c, d, e, f, g, x", true); 235 errcount += expect_result ("the-token x,a,b,c,d,e,f,g,,,,,,,,,,,," \ 236 "the-token x", "the-token", 237 "the-token x, a, b, c, d, e, f, g, the-token x", 238 false); 239 errcount += expect_result ("the-token x,a,b,c,d,e,f,g,,,,,,,,,,,," \ 240 "the-token x,", "the-token", 241 "the-token x, a, b, c, d, e, f, g, the-token x", 242 false); 243 errcount += expect_result ("the-token x,a,b,c,d,e,f,g,,,,,,,,,,,," \ 244 "the-token x,x", "the-token", 245 "the-token x, a, b, c, d, e, f, g, " \ 246 "the-token x, x", false); 247 248 return errcount; 249 } 250 251 252 int 253 main (int argc, char *argv[]) 254 { 255 int errcount = 0; 256 (void) argc; (void) argv; /* Unused. Silent compiler warning. */ 257 errcount += check_result (); 258 return errcount == 0 ? 0 : 1; 259 }