libmicrohttpd2

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

test_str_tokens_remove.c (14167B)


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