libmicrohttpd2

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

unit_str_token.c (5023B)


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