libmicrohttpd2

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

unit_str_token_remove.c (12418B)


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