libmicrohttpd

HTTP/1.x server C library (MHD 1.x, stable)
Log | Files | Refs | Submodules | README | LICENSE

test_str_token.c (4924B)


      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_options.h"
     27 #include <stdio.h>
     28 #include "mhd_str.h"
     29 
     30 /* Turn any MHD_PANIC() or failing mhd_assert() reached from this
     31    test into a marked, classifiable test error (TESTING.md, P5). */
     32 #include "mhd_panic_tripwire.h"
     33 
     34 
     35 static int
     36 expect_found_n (const char *str, const char *token, size_t token_len)
     37 {
     38   if (! MHD_str_has_token_caseless_ (str, token, token_len))
     39   {
     40     fprintf (stderr,
     41              "MHD_str_has_token_caseless_() FAILED:\n\tMHD_str_has_token_caseless_(%s, %s, %lu) return false\n",
     42              str, token, (unsigned long) token_len);
     43     return 1;
     44   }
     45   return 0;
     46 }
     47 
     48 
     49 #define expect_found(s,t) expect_found_n ((s),(t),MHD_STATICSTR_LEN_ (t))
     50 
     51 static int
     52 expect_not_found_n (const char *str, const char *token, size_t token_len)
     53 {
     54   if (MHD_str_has_token_caseless_ (str, token, token_len))
     55   {
     56     fprintf (stderr,
     57              "MHD_str_has_token_caseless_() FAILED:\n\tMHD_str_has_token_caseless_(%s, %s, %lu) return true\n",
     58              str, token, (unsigned long) token_len);
     59     return 1;
     60   }
     61   return 0;
     62 }
     63 
     64 
     65 #define expect_not_found(s,t) expect_not_found_n ((s),(t),MHD_STATICSTR_LEN_ ( \
     66                                                     t))
     67 
     68 static int
     69 check_match (void)
     70 {
     71   int errcount = 0;
     72   errcount += expect_found ("string", "string");
     73   errcount += expect_found ("String", "string");
     74   errcount += expect_found ("string", "String");
     75   errcount += expect_found ("strinG", "String");
     76   errcount += expect_found ("\t strinG", "String");
     77   errcount += expect_found ("strinG\t ", "String");
     78   errcount += expect_found (" \t tOkEn  ", "toKEN");
     79   errcount += expect_found ("not token\t,  tOkEn  ", "toKEN");
     80   errcount += expect_found ("not token,\t  tOkEn, more token", "toKEN");
     81   errcount += expect_found ("not token,\t  tOkEn\t, more token", "toKEN");
     82   errcount += expect_found (",,,,,,test,,,,", "TESt");
     83   errcount += expect_found (",,,,,\t,test,,,,", "TESt");
     84   errcount += expect_found (",,,,,,test, ,,,", "TESt");
     85   errcount += expect_found (",,,,,, test,,,,", "TESt");
     86   errcount += expect_found (",,,,,, test not,test,,", "TESt");
     87   errcount += expect_found (",,,,,, test not,,test,,", "TESt");
     88   errcount += expect_found (",,,,,, test not ,test,,", "TESt");
     89   errcount += expect_found (",,,,,, test", "TESt");
     90   errcount += expect_found (",,,,,, test      ", "TESt");
     91   errcount += expect_found ("no test,,,,,, test      ", "TESt");
     92   return errcount;
     93 }
     94 
     95 
     96 static int
     97 check_not_match (void)
     98 {
     99   int errcount = 0;
    100   errcount += expect_not_found ("strin", "string");
    101   errcount += expect_not_found ("Stringer", "string");
    102   errcount += expect_not_found ("sstring", "String");
    103   errcount += expect_not_found ("string", "Strin");
    104   errcount += expect_not_found ("\t( strinG", "String");
    105   errcount += expect_not_found (")strinG\t ", "String");
    106   errcount += expect_not_found (" \t tOkEn t ", "toKEN");
    107   errcount += expect_not_found ("not token\t,  tOkEner  ", "toKEN");
    108   errcount += expect_not_found ("not token,\t  tOkEns, more token", "toKEN");
    109   errcount += expect_not_found ("not token,\t  tOkEns\t, more token", "toKEN");
    110   errcount += expect_not_found (",,,,,,testing,,,,", "TESt");
    111   errcount += expect_not_found (",,,,,\t,test,,,,", "TESting");
    112   errcount += expect_not_found ("tests,,,,,,quest, ,,,", "TESt");
    113   errcount += expect_not_found (",,,,,, testы,,,,", "TESt");
    114   errcount += expect_not_found (",,,,,, test not,хtest,,", "TESt");
    115   errcount += expect_not_found ("testing,,,,,, test not,,test2,,", "TESt");
    116   errcount += expect_not_found (",testi,,,,, test not ,test,,", "TESting");
    117   errcount += expect_not_found (",,,,,,2 test", "TESt");
    118   errcount += expect_not_found (",,,,,,test test      ", "test");
    119   errcount += expect_not_found ("no test,,,,,, test      test", "test");
    120   return errcount;
    121 }
    122 
    123 
    124 int
    125 main (int argc, char *argv[])
    126 {
    127   int errcount = 0;
    128   (void) argc; (void) argv; /* Unused. Silent compiler warning. */
    129   errcount += check_match ();
    130   errcount += check_not_match ();
    131   return errcount == 0 ? 0 : 1;
    132 }