libmicrohttpd2

HTTP server C library (MHD 2.x, alpha)
Log | Files | Refs | README | LICENSE

unit_str_token.c (4999B)


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