test_str_token.c (5002B)
1 /* 2 This file is part of libmicrohttpd 3 Copyright (C) 2017 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 some mhd_str functions 23 * @author Karlson2k (Evgeny Grin) 24 */ 25 26 #include "mhd_sys_options.h" 27 #include <stdio.h> 28 #include "../mhd2/mhd_str.h" 29 #include "../mhd2/mhd_str.c" 30 31 32 #ifndef MHD_STATICSTR_LEN_ 33 /** 34 * Determine length of static string / macro strings at compile time. 35 */ 36 #define MHD_STATICSTR_LEN_(macro) (sizeof(macro) / sizeof(char) - 1) 37 #endif /* ! MHD_STATICSTR_LEN_ */ 38 39 40 static int 41 expect_found_n (const char *str, const char *token, size_t token_len) 42 { 43 if (! mhd_str_has_token_caseless (str, token, token_len)) 44 { 45 fprintf (stderr, 46 "mhd_str_has_token_caseless() FAILED:\n\tmhd_str_has_token_caseless(%s, %s, %lu) return false\n", 47 str, token, (unsigned long) token_len); 48 return 1; 49 } 50 return 0; 51 } 52 53 54 #define expect_found(s,t) expect_found_n ((s),(t),MHD_STATICSTR_LEN_ (t)) 55 56 static int 57 expect_not_found_n (const char *str, const char *token, size_t token_len) 58 { 59 if (mhd_str_has_token_caseless (str, token, token_len)) 60 { 61 fprintf (stderr, 62 "mhd_str_has_token_caseless() FAILED:\n\tmhd_str_has_token_caseless(%s, %s, %lu) return true\n", 63 str, token, (unsigned long) token_len); 64 return 1; 65 } 66 return 0; 67 } 68 69 70 #define expect_not_found(s,t) expect_not_found_n ((s),(t),MHD_STATICSTR_LEN_ ( \ 71 t)) 72 73 static int 74 check_match (void) 75 { 76 int errcount = 0; 77 errcount += expect_found ("string", "string"); 78 errcount += expect_found ("String", "string"); 79 errcount += expect_found ("string", "String"); 80 errcount += expect_found ("strinG", "String"); 81 errcount += expect_found ("\t strinG", "String"); 82 errcount += expect_found ("strinG\t ", "String"); 83 errcount += expect_found (" \t tOkEn ", "toKEN"); 84 errcount += expect_found ("not token\t, tOkEn ", "toKEN"); 85 errcount += expect_found ("not token,\t tOkEn, more token", "toKEN"); 86 errcount += expect_found ("not token,\t tOkEn\t, more token", "toKEN"); 87 errcount += expect_found (",,,,,,test,,,,", "TESt"); 88 errcount += expect_found (",,,,,\t,test,,,,", "TESt"); 89 errcount += expect_found (",,,,,,test, ,,,", "TESt"); 90 errcount += expect_found (",,,,,, test,,,,", "TESt"); 91 errcount += expect_found (",,,,,, test not,test,,", "TESt"); 92 errcount += expect_found (",,,,,, test not,,test,,", "TESt"); 93 errcount += expect_found (",,,,,, test not ,test,,", "TESt"); 94 errcount += expect_found (",,,,,, test", "TESt"); 95 errcount += expect_found (",,,,,, test ", "TESt"); 96 errcount += expect_found ("no test,,,,,, test ", "TESt"); 97 return errcount; 98 } 99 100 101 static int 102 check_not_match (void) 103 { 104 int errcount = 0; 105 errcount += expect_not_found ("strin", "string"); 106 errcount += expect_not_found ("Stringer", "string"); 107 errcount += expect_not_found ("sstring", "String"); 108 errcount += expect_not_found ("string", "Strin"); 109 errcount += expect_not_found ("\t( strinG", "String"); 110 errcount += expect_not_found (")strinG\t ", "String"); 111 errcount += expect_not_found (" \t tOkEn t ", "toKEN"); 112 errcount += expect_not_found ("not token\t, tOkEner ", "toKEN"); 113 errcount += expect_not_found ("not token,\t tOkEns, more token", "toKEN"); 114 errcount += expect_not_found ("not token,\t tOkEns\t, more token", "toKEN"); 115 errcount += expect_not_found (",,,,,,testing,,,,", "TESt"); 116 errcount += expect_not_found (",,,,,\t,test,,,,", "TESting"); 117 errcount += expect_not_found ("tests,,,,,,quest, ,,,", "TESt"); 118 errcount += expect_not_found (",,,,,, testы,,,,", "TESt"); 119 errcount += expect_not_found (",,,,,, test not,хtest,,", "TESt"); 120 errcount += expect_not_found ("testing,,,,,, test not,,test2,,", "TESt"); 121 errcount += expect_not_found (",testi,,,,, test not ,test,,", "TESting"); 122 errcount += expect_not_found (",,,,,,2 test", "TESt"); 123 errcount += expect_not_found (",,,,,,test test ", "test"); 124 errcount += expect_not_found ("no test,,,,,, test test", "test"); 125 return errcount; 126 } 127 128 129 int 130 main (int argc, char *argv[]) 131 { 132 int errcount = 0; 133 (void) argc; (void) argv; /* Unused. Silent compiler warning. */ 134 errcount += check_match (); 135 errcount += check_not_match (); 136 return errcount == 0 ? 0 : 1; 137 }