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