libmicrohttpd

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

test_str_tokens_remove.c (14063B)


      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 microhttpd/test_str_token.c
     22  * @brief  Unit tests for MHD_str_remove_tokens_caseless_() function
     23  * @author Karlson2k (Evgeny Grin)
     24  */
     25 
     26 #include "mhd_options.h"
     27 #include <string.h>
     28 #include <stdio.h>
     29 #include "mhd_str.h"
     30 #include "mhd_assert.h"
     31 
     32 /* Turn any MHD_PANIC() or failing mhd_assert() reached from this
     33    test into a marked, classifiable test error (TESTING.md, P5). */
     34 #include "mhd_panic_tripwire.h"
     35 
     36 
     37 static int
     38 expect_result_n (const char *str, size_t str_len,
     39                  const char *tokens, size_t tokens_len,
     40                  const char *expected, size_t expected_len,
     41                  const bool expected_removed)
     42 {
     43   char buf_in[1024];
     44   char buf_tokens[256];
     45   bool res;
     46   size_t result_len;
     47 
     48   mhd_assert (sizeof(buf_in) > str_len + 2);
     49   mhd_assert (sizeof(buf_tokens) > tokens_len + 2);
     50 
     51   memset (buf_tokens, '#', sizeof(buf_tokens));
     52   memcpy (buf_tokens, tokens, tokens_len); /* Copy without zero-termination */
     53   memset (buf_in, '$', sizeof(buf_in));
     54   memcpy (buf_in, str, str_len); /* Copy without zero-termination */
     55 
     56   result_len = str_len;
     57 
     58   res = MHD_str_remove_tokens_caseless_ (buf_in, &result_len,
     59                                          buf_tokens, tokens_len);
     60 
     61   if ( (expected_removed != res) ||
     62        (expected_len != result_len) ||
     63        ((0 != result_len) && (0 != memcmp (expected, buf_in, result_len))) ||
     64        ('$' != buf_in[str_len]))
     65   {
     66     fprintf (stderr,
     67              "MHD_str_remove_tokens_caseless_() FAILED:\n"
     68              "\tRESULT: "
     69              "\tMHD_str_remove_token_caseless_(\"%s\"->\"%.*s\", &(%lu->%lu),"
     70              " \"%.*s\", %lu) returned %s\n",
     71              str,
     72              (int) result_len, buf_in,
     73              (unsigned long) str_len, (unsigned long) result_len,
     74              (int) tokens_len, buf_tokens, (unsigned long) tokens_len,
     75              res ? "true" : "false");
     76     fprintf (stderr,
     77              "\tEXPECTED: "
     78              "\tMHD_str_remove_token_caseless_(\"%s\"->\"%s\", &(%lu->%lu),"
     79              " \"%.*s\", %lu) returned %s\n",
     80              str,
     81              expected,
     82              (unsigned long) str_len, (unsigned long) expected_len,
     83              (int) tokens_len, buf_tokens, (unsigned long) tokens_len,
     84              expected_removed ? "true" : "false");
     85     return 1;
     86   }
     87   return 0;
     88 }
     89 
     90 
     91 #define expect_result(s,t,e,found) \
     92   expect_result_n ((s),MHD_STATICSTR_LEN_ (s), \
     93                    (t),MHD_STATICSTR_LEN_ (t), \
     94                    (e),MHD_STATICSTR_LEN_ (e), found)
     95 
     96 static int
     97 check_result (void)
     98 {
     99   int errcount = 0;
    100   errcount += expect_result ("string", "string", "", true);
    101   errcount += expect_result ("String", "string", "", true);
    102   errcount += expect_result ("string", "String", "", true);
    103   errcount += expect_result ("strinG", "String", "", true);
    104   errcount += expect_result ("strinG", "String\t", "", true);
    105   errcount += expect_result ("strinG", "\tString", "", true);
    106   errcount += expect_result ("tOkEn", " \t toKEN  ", "", true);
    107   errcount += expect_result ("not-token, tOkEn", "token", "not-token",
    108                              true);
    109   errcount += expect_result ("not-token1, tOkEn1, token", "token1",
    110                              "not-token1, token",
    111                              true);
    112   errcount += expect_result ("token, tOkEn1", "token1", "token",
    113                              true);
    114   errcount += expect_result ("not-token, tOkEn", " \t toKEN", "not-token",
    115                              true);
    116   errcount += expect_result ("not-token, tOkEn, more-token", "toKEN\t",
    117                              "not-token, more-token", true);
    118   errcount += expect_result ("not-token, tOkEn, more-token", "\t  toKEN,,,,,",
    119                              "not-token, more-token", true);
    120   errcount += expect_result ("a, b, c, d", ",,,,,a", "b, c, d", true);
    121   errcount += expect_result ("a, b, c, d", "a,,,,,,", "b, c, d", true);
    122   errcount += expect_result ("a, b, c, d", ",,,,a,,,,,,", "b, c, d", true);
    123   errcount += expect_result ("a, b, c, d", "\t \t,,,,a,,   ,   ,,,\t",
    124                              "b, c, d", true);
    125   errcount += expect_result ("a, b, c, d", "b, c, d", "a", true);
    126   errcount += expect_result ("a, b, c, d", "a, b, c, d", "", true);
    127   errcount += expect_result ("a, b, c, d", "d, c, b, a", "", true);
    128   errcount += expect_result ("a, b, c, d", "b, d, a, c", "", true);
    129   errcount += expect_result ("a, b, c, d, e", "b, d, a, c", "e", true);
    130   errcount += expect_result ("e, a, b, c, d", "b, d, a, c", "e", true);
    131   errcount += expect_result ("e, a, b, c, d, e", "b, d, a, c", "e, e", true);
    132   errcount += expect_result ("a, b, c, d", "b,c,d", "a", true);
    133   errcount += expect_result ("a, b, c, d", "a,b,c,d", "", true);
    134   errcount += expect_result ("a, b, c, d", "d,c,b,a", "", true);
    135   errcount += expect_result ("a, b, c, d", "b,d,a,c", "", true);
    136   errcount += expect_result ("a, b, c, d, e", "b,d,a,c", "e", true);
    137   errcount += expect_result ("e, a, b, c, d", "b,d,a,c", "e", true);
    138   errcount += expect_result ("e, a, b, c, d, e", "b,d,a,c", "e, e", true);
    139   errcount += expect_result ("a, b, c, d", "d,,,,,,,,,c,b,a", "", true);
    140   errcount += expect_result ("a, b, c, d", "b,d,a,c,,,,,,,,,,", "", true);
    141   errcount += expect_result ("a, b, c, d, e", ",,,,\t,,,,b,d,a,c,\t", "e",
    142                              true);
    143   errcount += expect_result ("e, a, b, c, d", "b,d,a,c", "e", true);
    144   errcount += expect_result ("token, a, b, c, d", "token", "a, b, c, d", true);
    145   errcount += expect_result ("token1, a, b, c, d", "token1", "a, b, c, d",
    146                              true);
    147   errcount += expect_result ("token12, a, b, c, d", "token12", "a, b, c, d",
    148                              true);
    149   errcount += expect_result ("token123, a, b, c, d", "token123", "a, b, c, d",
    150                              true);
    151   errcount += expect_result ("token1234, a, b, c, d", "token1234", "a, b, c, d",
    152                              true);
    153   errcount += expect_result ("token12345, a, b, c, d", "token12345",
    154                              "a, b, c, d", true);
    155   errcount += expect_result ("token123456, a, b, c, d", "token123456",
    156                              "a, b, c, d", true);
    157   errcount += expect_result ("token1234567, a, b, c, d", "token1234567",
    158                              "a, b, c, d", true);
    159   errcount += expect_result ("token12345678, a, b, c, d", "token12345678",
    160                              "a, b, c, d", true);
    161 
    162   errcount += expect_result ("", "a", "", false);
    163   errcount += expect_result ("", "", "", false);
    164   errcount += expect_result ("a, b, c, d", "bb, dd, aa, cc", "a, b, c, d",
    165                              false);
    166   errcount += expect_result ("a, b, c, d, e", "bb, dd, aa, cc", "a, b, c, d, e",
    167                              false);
    168   errcount += expect_result ("e, a, b, c, d", "bb, dd, aa, cc", "e, a, b, c, d",
    169                              false);
    170   errcount += expect_result ("e, a, b, c, d, e", "bb, dd, aa, cc",
    171                              "e, a, b, c, d, e", false);
    172   errcount += expect_result ("aa, bb, cc, dd", "b, d, a, c", "aa, bb, cc, dd",
    173                              false);
    174   errcount += expect_result ("aa, bb, cc, dd, ee", "b, d, a, c",
    175                              "aa, bb, cc, dd, ee", false);
    176   errcount += expect_result ("ee, aa, bb, cc, dd", "b, d, a, c",
    177                              "ee, aa, bb, cc, dd", false);
    178   errcount += expect_result ("ee, aa, bb, cc, dd, ee", "b, d, a, c",
    179                              "ee, aa, bb, cc, dd, ee", false);
    180 
    181   errcount += expect_result ("TESt", ",,,,,,test,,,,", "", true);
    182   errcount += expect_result ("TESt", ",,,,,\t,test,,,,", "", true);
    183   errcount += expect_result ("TESt", ",,,,,,test, ,,,", "", true);
    184   errcount += expect_result ("TESt", ",,,,,, test,,,,", "", true);
    185   errcount += expect_result ("TESt", ",,,,,, test-not,test,,", "",
    186                              true);
    187   errcount += expect_result ("TESt", ",,,,,, test-not,,test,,", "",
    188                              true);
    189   errcount += expect_result ("TESt", ",,,,,, test-not ,test,,", "",
    190                              true);
    191   errcount += expect_result ("TESt", ",,,,,, test", "", true);
    192   errcount += expect_result ("TESt", ",,,,,, test      ", "", true);
    193   errcount += expect_result ("TESt", "no-test,,,,,, test      ", "",
    194                              true);
    195 
    196   errcount += expect_result ("the-token, a, the-token, b, the-token, " \
    197                              "the-token, c, the-token", "the-token", "a, b, c",
    198                              true);
    199   errcount += expect_result ("aa, the-token, bb, the-token, cc, the-token, " \
    200                              "the-token, dd, the-token", "the-token",
    201                              "aa, bb, cc, dd", true);
    202   errcount += expect_result ("the-token, a, the-token, b, the-token, " \
    203                              "the-token, c, the-token, e", "the-token",
    204                              "a, b, c, e", true);
    205   errcount += expect_result ("aa, the-token, bb, the-token, cc, the-token, " \
    206                              "the-token, dd, the-token, ee", "the-token",
    207                              "aa, bb, cc, dd, ee", true);
    208   errcount += expect_result ("the-token, the-token, the-token, " \
    209                              "the-token, the-token", "the-token", "", true);
    210   errcount += expect_result ("the-token, a, the-token, the-token, b, " \
    211                              "the-token, c, the-token, a", "c,a,b",
    212                              "the-token, the-token, the-token, the-token, the-token",
    213                              true);
    214   errcount += expect_result ("the-token, xx, the-token, the-token, zz, " \
    215                              "the-token, yy, the-token, ww", "ww,zz,yy",
    216                              "the-token, xx, the-token, the-token, the-token, the-token",
    217                              true);
    218   errcount += expect_result ("the-token, a, the-token, the-token, b, " \
    219                              "the-token, c, the-token, a", " c,\t a,b,,,",
    220                              "the-token, the-token, the-token, the-token, the-token",
    221                              true);
    222   errcount += expect_result ("the-token, xx, the-token, the-token, zz, " \
    223                              "the-token, yy, the-token, ww",
    224                              ",,,,ww,\t zz,  yy",
    225                              "the-token, xx, the-token, the-token, the-token, the-token",
    226                              true);
    227   errcount += expect_result ("the-token, a, the-token, the-token, b, " \
    228                              "the-token, c, the-token, a", ",,,,c,\t a,b",
    229                              "the-token, the-token, the-token, the-token, the-token",
    230                              true);
    231   errcount += expect_result ("the-token, xx, the-token, the-token, zz, " \
    232                              "the-token, yy, the-token, ww", " ww,\t zz,yy,,,,",
    233                              "the-token, xx, the-token, the-token, the-token, the-token",
    234                              true);
    235   errcount += expect_result ("close, 2", "close",
    236                              "2", true);
    237   errcount += expect_result ("close, 22", "close",
    238                              "22", true);
    239   errcount += expect_result ("close, nothing", "close",
    240                              "nothing", true);
    241   errcount += expect_result ("close, 2", "2",
    242                              "close", true);
    243   errcount += expect_result ("close", "close",
    244                              "", true);
    245   errcount += expect_result ("close, nothing", "close, token",
    246                              "nothing", true);
    247   errcount += expect_result ("close, nothing", "nothing, token",
    248                              "close", true);
    249   errcount += expect_result ("close, 2", "close, 10, 12, 22, nothing",
    250                              "2", true);
    251 
    252   errcount += expect_result ("strin", "string", "strin", false);
    253   errcount += expect_result ("Stringer", "string", "Stringer", false);
    254   errcount += expect_result ("sstring", "String", "sstring", false);
    255   errcount += expect_result ("string", "Strin", "string", false);
    256   errcount += expect_result ("String", "\t(-strinG", "String", false);
    257   errcount += expect_result ("String", ")strinG\t ", "String", false);
    258   errcount += expect_result ("not-token, tOkEner", "toKEN",
    259                              "not-token, tOkEner", false);
    260   errcount += expect_result ("not-token, tOkEns, more-token", "toKEN",
    261                              "not-token, tOkEns, more-token", false);
    262   errcount += expect_result ("tests, quest", "TESt", "tests, quest",
    263                              false);
    264   errcount += expect_result ("testы", "TESt", "testы", false);
    265   errcount += expect_result ("test-not, хtest", "TESt",
    266                              "test-not, хtest", false);
    267   errcount += expect_result ("testing, test not, test2", "TESt",
    268                              "testing, test not, test2", false);
    269   errcount += expect_result ("", ",,,,,,,,,,,,,,,,,,,the-token", "", false);
    270   errcount += expect_result ("a1, b1, c1, d1, e1, f1, g1", "",
    271                              "a1, b1, c1, d1, e1, f1, g1", false);
    272 
    273   return errcount;
    274 }
    275 
    276 
    277 int
    278 main (int argc, char *argv[])
    279 {
    280   int errcount = 0;
    281   (void) argc; (void) argv; /* Unused. Silent compiler warning. */
    282   errcount += check_result ();
    283   if (0 == errcount)
    284     printf ("All tests were passed without errors.\n");
    285   return errcount == 0 ? 0 : 1;
    286 }