libmicrohttpd

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

test_auth_parse.c (76980B)


      1 /*
      2   This file is part of libmicrohttpd
      3   Copyright (C) 2022-2023 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_auth_parse.c
     22  * @brief  Unit tests for request's 'Authorization" headers parsing
     23  * @author Karlson2k (Evgeny Grin)
     24  */
     25 
     26 #include "mhd_options.h"
     27 #include <string.h>
     28 #include <stdio.h>
     29 #include <errno.h>
     30 #include "gen_auth.h"
     31 #ifdef BAUTH_SUPPORT
     32 #include "basicauth.h"
     33 #endif /* BAUTH_SUPPORT */
     34 #ifdef DAUTH_SUPPORT
     35 #include "digestauth.h"
     36 #endif /* DAUTH_SUPPORT */
     37 #include "mhd_assert.h"
     38 #include "internal.h"
     39 #include "connection.h"
     40 
     41 
     42 #ifndef MHD_STATICSTR_LEN_
     43 /**
     44  * Determine length of static string / macro strings at compile time.
     45  */
     46 #define MHD_STATICSTR_LEN_(macro) (sizeof(macro) / sizeof(char) - 1)
     47 #endif /* ! MHD_STATICSTR_LEN_ */
     48 
     49 
     50 #if defined(HAVE___FUNC__)
     51 #define externalErrorExit(ignore) \
     52     _externalErrorExit_func(NULL, __func__, __LINE__)
     53 #define externalErrorExitDesc(errDesc) \
     54     _externalErrorExit_func(errDesc, __func__, __LINE__)
     55 #define mhdErrorExit(ignore) \
     56     _mhdErrorExit_func(NULL, __func__, __LINE__)
     57 #define mhdErrorExitDesc(errDesc) \
     58     _mhdErrorExit_func(errDesc, __func__, __LINE__)
     59 #elif defined(HAVE___FUNCTION__)
     60 #define externalErrorExit(ignore) \
     61     _externalErrorExit_func(NULL, __FUNCTION__, __LINE__)
     62 #define externalErrorExitDesc(errDesc) \
     63     _externalErrorExit_func(errDesc, __FUNCTION__, __LINE__)
     64 #define mhdErrorExit(ignore) \
     65     _mhdErrorExit_func(NULL, __FUNCTION__, __LINE__)
     66 #define mhdErrorExitDesc(errDesc) \
     67     _mhdErrorExit_func(errDesc, __FUNCTION__, __LINE__)
     68 #else
     69 #define externalErrorExit(ignore) _externalErrorExit_func(NULL, NULL, __LINE__)
     70 #define externalErrorExitDesc(errDesc) \
     71   _externalErrorExit_func(errDesc, NULL, __LINE__)
     72 #define mhdErrorExit(ignore) _mhdErrorExit_func(NULL, NULL, __LINE__)
     73 #define mhdErrorExitDesc(errDesc) _mhdErrorExit_func(errDesc, NULL, __LINE__)
     74 #endif
     75 
     76 
     77 _MHD_NORETURN static void
     78 _externalErrorExit_func (const char *errDesc, const char *funcName, int lineNum)
     79 {
     80   if ((NULL != errDesc) && (0 != errDesc[0]))
     81     fprintf (stderr, "%s", errDesc);
     82   else
     83     fprintf (stderr, "System or external library call failed");
     84   if ((NULL != funcName) && (0 != funcName[0]))
     85     fprintf (stderr, " in %s", funcName);
     86   if (0 < lineNum)
     87     fprintf (stderr, " at line %d", lineNum);
     88 
     89   fprintf (stderr, ".\nLast errno value: %d (%s)\n", (int) errno,
     90            strerror (errno));
     91 #ifdef MHD_WINSOCK_SOCKETS
     92   fprintf (stderr, "WSAGetLastError() value: %d\n", (int) WSAGetLastError ());
     93 #endif /* MHD_WINSOCK_SOCKETS */
     94   fflush (stderr);
     95   exit (99);
     96 }
     97 
     98 
     99 _MHD_NORETURN static void
    100 _mhdErrorExit_func (const char *errDesc, const char *funcName, int lineNum)
    101 {
    102   if ((NULL != errDesc) && (0 != errDesc[0]))
    103     fprintf (stderr, "%s", errDesc);
    104   else
    105     fprintf (stderr, "MHD unexpected error");
    106   if ((NULL != funcName) && (0 != funcName[0]))
    107     fprintf (stderr, " in %s", funcName);
    108   if (0 < lineNum)
    109     fprintf (stderr, " at line %d", lineNum);
    110 
    111   fprintf (stderr, ".\nLast errno value: %d (%s)\n", (int) errno,
    112            strerror (errno));
    113 
    114   fflush (stderr);
    115   exit (8);
    116 }
    117 
    118 
    119 /* Declarations for local replacements of MHD functions */
    120 /* None, headers are included */
    121 
    122 /* Local replacements implementations */
    123 
    124 void *
    125 MHD_connection_alloc_memory_ (struct MHD_Connection *connection,
    126                               size_t size)
    127 {
    128   void *ret;
    129   if (NULL == connection)
    130     mhdErrorExitDesc ("'connection' parameter is NULL");
    131   /* Use 'read_buffer' just as a flag */
    132   if (NULL != connection->read_buffer)
    133   {
    134     /* Use 'write_buffer' just as a flag */
    135     if (NULL != connection->write_buffer)
    136       mhdErrorExitDesc ("Unexpected third memory allocation, " \
    137                         "while previous allocations was not freed");
    138   }
    139   /* Just use simple "malloc()" here */
    140   ret = malloc (size);
    141   if (NULL == ret)
    142     externalErrorExit ();
    143 
    144   /* Track up to two allocations */
    145   if (NULL == connection->read_buffer)
    146     connection->read_buffer = (char *) ret;
    147   else
    148     connection->write_buffer = (char *) ret;
    149   return ret;
    150 }
    151 
    152 
    153 /**
    154  * Static variable to avoid additional malloc()/free() pairs
    155  */
    156 static struct MHD_Connection conn;
    157 
    158 void
    159 MHD_DLOG (const struct MHD_Daemon *daemon,
    160           const char *format,
    161           ...)
    162 {
    163   (void) daemon;
    164   if (! conn.rq.client_aware)
    165   {
    166     fprintf (stderr, "Unexpected call of 'MHD_LOG(), format is '%s'.\n",
    167              format);
    168     fprintf (stderr, "'Authorization' header value: '%s'.\n",
    169              (NULL == conn.rq.headers_received) ?
    170              "NULL" : (conn.rq.headers_received->value));
    171     mhdErrorExit ();
    172   }
    173   conn.rq.client_aware = false; /* Clear the flag */
    174   return;
    175 }
    176 
    177 
    178 /**
    179  * Static variable to avoid additional malloc()/free() pairs
    180  */
    181 static struct MHD_HTTP_Req_Header req_header;
    182 
    183 static void
    184 test_global_init (void)
    185 {
    186   memset (&conn, 0, sizeof(conn));
    187   memset (&req_header, 0, sizeof(req_header));
    188 }
    189 
    190 
    191 /**
    192  * Add "Authorization" client test header.
    193  *
    194  * @param hdr the pointer to the headr value, must be valid until end of
    195  *            checking of this header
    196  * @param hdr_len the length of the @a hdr
    197  * @note The function is NOT thread-safe
    198  */
    199 static void
    200 add_AuthHeader (const char *hdr, size_t hdr_len)
    201 {
    202   if ((NULL != conn.rq.headers_received) ||
    203       (NULL != conn.rq.headers_received_tail))
    204     externalErrorExitDesc ("Connection's test headers are not empty already");
    205   if (NULL != hdr)
    206   {
    207     /* Skip initial whitespaces, emulate MHD's headers processing */
    208     while (' ' == hdr[0] || '\t' == hdr[0])
    209     {
    210       hdr++;
    211       hdr_len--;
    212     }
    213     req_header.header = MHD_HTTP_HEADER_AUTHORIZATION; /* Static string */
    214     req_header.header_size = MHD_STATICSTR_LEN_ (MHD_HTTP_HEADER_AUTHORIZATION);
    215     req_header.value = hdr;
    216     req_header.value_size = hdr_len;
    217     req_header.kind = MHD_HEADER_KIND;
    218     req_header.prev = NULL;
    219     req_header.next = NULL;
    220     conn.rq.headers_received = &req_header;
    221     conn.rq.headers_received_tail = &req_header;
    222   }
    223   else
    224   {
    225     conn.rq.headers_received = NULL;
    226     conn.rq.headers_received_tail = NULL;
    227   }
    228   conn.state = MHD_CONNECTION_FULL_REQ_RECEIVED; /* Should be a typical value */
    229 }
    230 
    231 
    232 #ifdef BAUTH_SUPPORT
    233 /**
    234  * Parse previously added Basic Authorization client header and return
    235  * result of the parsing.
    236  *
    237  * Function performs basic checking of the parsing result
    238  * @return result of header parsing
    239  * @note The function is NOT thread-safe
    240  */
    241 static const struct MHD_RqBAuth *
    242 get_BAuthRqParams (void)
    243 {
    244   const struct MHD_RqBAuth *res1;
    245   const struct MHD_RqBAuth *res2;
    246   /* Store pointer in some member unused in this test */
    247   res1 = MHD_get_rq_bauth_params_ (&conn);
    248   if (! conn.rq.bauth_tried)
    249     mhdErrorExitDesc ("'rq.bauth_tried' is not set");
    250   res2 = MHD_get_rq_bauth_params_ (&conn);
    251   if (res1 != res2)
    252     mhdErrorExitDesc ("MHD_get_rq_bauth_params_() returned another pointer " \
    253                       "when called for the second time");
    254   return res2;
    255 }
    256 
    257 
    258 #endif /* BAUTH_SUPPORT */
    259 
    260 #ifdef DAUTH_SUPPORT
    261 /**
    262  * Parse previously added Digest Authorization client header and return
    263  * result of the parsing.
    264  *
    265  * Function performs basic checking of the parsing result
    266  * @return result of header parsing
    267  * @note The function is NOT thread-safe
    268  */
    269 static const struct MHD_RqDAuth *
    270 get_DAuthRqParams (void)
    271 {
    272   const struct MHD_RqDAuth *res1;
    273   const struct MHD_RqDAuth *res2;
    274   /* Store pointer in some member unused in this test */
    275   res1 = MHD_get_rq_dauth_params_ (&conn);
    276   if (! conn.rq.dauth_tried)
    277     mhdErrorExitDesc ("'rq.dauth_tried' is not set");
    278   res2 = MHD_get_rq_dauth_params_ (&conn);
    279   if (res1 != res2)
    280     mhdErrorExitDesc ("MHD_get_rq_bauth_params_() returned another pointer " \
    281                       "when called for the second time");
    282   return res2;
    283 }
    284 
    285 
    286 #endif /* DAUTH_SUPPORT */
    287 
    288 
    289 static void
    290 clean_AuthHeaders (void)
    291 {
    292   conn.state = MHD_CONNECTION_INIT;
    293   free (conn.read_buffer);
    294   free (conn.write_buffer);
    295 
    296 #ifdef BAUTH_SUPPORT
    297   conn.rq.bauth_tried = false;
    298 #endif /* BAUTH_SUPPORT */
    299 #ifdef DAUTH_SUPPORT
    300   conn.rq.dauth_tried = false;
    301 #endif /* BAUTH_SUPPORT */
    302 
    303 #ifdef BAUTH_SUPPORT
    304   if ((NULL != conn.rq.bauth) &&
    305       (conn.read_buffer != (const char *) conn.rq.bauth) &&
    306       (conn.write_buffer != (const char *) conn.rq.bauth))
    307     externalErrorExitDesc ("Memory allocation is not tracked as it should be");
    308   conn.rq.bauth = NULL;
    309 #endif /* BAUTH_SUPPORT */
    310 #ifdef DAUTH_SUPPORT
    311   if ((NULL != conn.rq.dauth) &&
    312       (conn.read_buffer != (const char *) conn.rq.dauth) &&
    313       (conn.write_buffer != (const char *) conn.rq.dauth))
    314     externalErrorExitDesc ("Memory allocation is not tracked as it should be");
    315   conn.rq.dauth = NULL;
    316 #endif /* BAUTH_SUPPORT */
    317 
    318   conn.rq.headers_received = NULL;
    319   conn.rq.headers_received_tail = NULL;
    320 
    321   conn.read_buffer = NULL;
    322   conn.write_buffer = NULL;
    323   conn.rq.client_aware = false;
    324 }
    325 
    326 
    327 enum MHD_TestAuthType
    328 {
    329   MHD_TEST_AUTHTYPE_NONE,
    330   MHD_TEST_AUTHTYPE_BASIC,
    331   MHD_TEST_AUTHTYPE_DIGEST,
    332 };
    333 
    334 
    335 /* return zero if succeed, non-zero otherwise */
    336 static unsigned int
    337 expect_result_type_n (const char *hdr, size_t hdr_len,
    338                       const enum MHD_TestAuthType expected_type,
    339                       int expect_log,
    340                       unsigned int line_num)
    341 {
    342   unsigned int ret;
    343 
    344   ret = 0;
    345   add_AuthHeader (hdr, hdr_len);
    346   if (expect_log)
    347     conn.rq.client_aware = true; /* Use like a flag */
    348   else
    349     conn.rq.client_aware = false;
    350 #ifdef BAUTH_SUPPORT
    351   if (MHD_TEST_AUTHTYPE_BASIC == expected_type)
    352   {
    353     if (NULL == get_BAuthRqParams ())
    354     {
    355       fprintf (stderr,
    356                "'Authorization' header parsing FAILED:\n"
    357                "Basic Authorization was not found, while it should be.\n");
    358       ret++;
    359     }
    360   }
    361   else
    362 #endif /* BAUTH_SUPPORT */
    363 #ifdef DAUTH_SUPPORT
    364   if (MHD_TEST_AUTHTYPE_DIGEST == expected_type)
    365   {
    366     if (NULL == get_DAuthRqParams ())
    367     {
    368       fprintf (stderr,
    369                "'Authorization' header parsing FAILED:\n"
    370                "Digest Authorization was not found, while it should be.\n");
    371       ret++;
    372     }
    373   }
    374   else
    375 #endif /* BAUTH_SUPPORT */
    376   {
    377 #ifdef BAUTH_SUPPORT
    378     if (NULL != get_BAuthRqParams ())
    379     {
    380       fprintf (stderr,
    381                "'Authorization' header parsing FAILED:\n"
    382                "Found Basic Authorization, while it should not be.\n");
    383       ret++;
    384     }
    385 #endif /* BAUTH_SUPPORT */
    386 #ifdef DAUTH_SUPPORT
    387     if (NULL != get_DAuthRqParams ())
    388     {
    389       fprintf (stderr,
    390                "'Authorization' header parsing FAILED:\n"
    391                "Found Digest Authorization, while it should not be.\n");
    392       ret++;
    393     }
    394 #endif /* DAUTH_SUPPORT */
    395   }
    396 #if defined(BAUTH_SUPPORT) && defined(DAUTH_SUPPORT)
    397   if (conn.rq.client_aware)
    398   {
    399     fprintf (stderr,
    400              "'Authorization' header parsing ERROR:\n"
    401              "Log function must be called, but it was not.\n");
    402     ret++;
    403   }
    404 #endif /* BAUTH_SUPPORT && DAUTH_SUPPORT */
    405 
    406   if (ret)
    407   {
    408     if (NULL == hdr)
    409       fprintf (stderr,
    410                "Input: Absence of 'Authorization' header.\n");
    411     else if (0 == hdr_len)
    412       fprintf (stderr,
    413                "Input: empty 'Authorization' header.\n");
    414     else
    415       fprintf (stderr,
    416                "Input Header: '%.*s'\n", (int) hdr_len, hdr);
    417     fprintf (stderr,
    418              "The check is at line: %u\n\n", line_num);
    419     ret = 1;
    420   }
    421   clean_AuthHeaders ();
    422 
    423   return ret;
    424 }
    425 
    426 
    427 #define expect_result_type(h,t,l) \
    428     expect_result_type_n(h,MHD_STATICSTR_LEN_(h),t,l,__LINE__)
    429 
    430 
    431 static unsigned int
    432 check_type (void)
    433 {
    434   unsigned int r = 0; /**< The number of errors */
    435 
    436   r += expect_result_type_n (NULL, 0, MHD_TEST_AUTHTYPE_NONE, 0, __LINE__);
    437 
    438   r += expect_result_type ("", MHD_TEST_AUTHTYPE_NONE, 0);
    439   r += expect_result_type (" ", MHD_TEST_AUTHTYPE_NONE, 0);
    440   r += expect_result_type ("    ", MHD_TEST_AUTHTYPE_NONE, 0);
    441   r += expect_result_type ("\t", MHD_TEST_AUTHTYPE_NONE, 0);
    442   r += expect_result_type (" \t", MHD_TEST_AUTHTYPE_NONE, 0);
    443   r += expect_result_type ("\t ", MHD_TEST_AUTHTYPE_NONE, 0);
    444   r += expect_result_type ("\t \t", MHD_TEST_AUTHTYPE_NONE, 0);
    445   r += expect_result_type (" \t ", MHD_TEST_AUTHTYPE_NONE, 0);
    446   r += expect_result_type (" \t \t", MHD_TEST_AUTHTYPE_NONE, 0);
    447   r += expect_result_type ("\t \t ", MHD_TEST_AUTHTYPE_NONE, 0);
    448 
    449   r += expect_result_type ("Basic", MHD_TEST_AUTHTYPE_BASIC, 0);
    450   r += expect_result_type (" Basic", MHD_TEST_AUTHTYPE_BASIC, 0);
    451   r += expect_result_type ("\tBasic", MHD_TEST_AUTHTYPE_BASIC, 0);
    452   r += expect_result_type ("\t Basic", MHD_TEST_AUTHTYPE_BASIC, 0);
    453   r += expect_result_type (" \tBasic", MHD_TEST_AUTHTYPE_BASIC, 0);
    454   r += expect_result_type ("    Basic", MHD_TEST_AUTHTYPE_BASIC, 0);
    455   r += expect_result_type ("\t\t\tBasic", MHD_TEST_AUTHTYPE_BASIC, 0);
    456   r += expect_result_type ("\t\t  \tBasic", MHD_TEST_AUTHTYPE_BASIC, 0);
    457   r += expect_result_type ("\t\t  \t Basic", MHD_TEST_AUTHTYPE_BASIC, 0);
    458   r += expect_result_type ("Basic ", MHD_TEST_AUTHTYPE_BASIC, 0);
    459   r += expect_result_type ("Basic \t", MHD_TEST_AUTHTYPE_BASIC, 0);
    460   r += expect_result_type ("Basic \t ", MHD_TEST_AUTHTYPE_BASIC, 0);
    461   r += expect_result_type ("Basic 123", MHD_TEST_AUTHTYPE_BASIC, 0);
    462   r += expect_result_type ("Basic \t123", MHD_TEST_AUTHTYPE_BASIC, 0);
    463   r += expect_result_type ("Basic  abc ", MHD_TEST_AUTHTYPE_BASIC, 0);
    464   r += expect_result_type ("bAsIC", MHD_TEST_AUTHTYPE_BASIC, 0);
    465   r += expect_result_type (" bAsIC", MHD_TEST_AUTHTYPE_BASIC, 0);
    466   r += expect_result_type ("\tbAsIC", MHD_TEST_AUTHTYPE_BASIC, 0);
    467   r += expect_result_type ("\t bAsIC", MHD_TEST_AUTHTYPE_BASIC, 0);
    468   r += expect_result_type (" \tbAsIC", MHD_TEST_AUTHTYPE_BASIC, 0);
    469   r += expect_result_type ("    bAsIC", MHD_TEST_AUTHTYPE_BASIC, 0);
    470   r += expect_result_type ("\t\t\tbAsIC", MHD_TEST_AUTHTYPE_BASIC, 0);
    471   r += expect_result_type ("\t\t  \tbAsIC", MHD_TEST_AUTHTYPE_BASIC, 0);
    472   r += expect_result_type ("\t\t  \t bAsIC", MHD_TEST_AUTHTYPE_BASIC, 0);
    473   r += expect_result_type ("bAsIC ", MHD_TEST_AUTHTYPE_BASIC, 0);
    474   r += expect_result_type ("bAsIC \t", MHD_TEST_AUTHTYPE_BASIC, 0);
    475   r += expect_result_type ("bAsIC \t ", MHD_TEST_AUTHTYPE_BASIC, 0);
    476   r += expect_result_type ("bAsIC 123", MHD_TEST_AUTHTYPE_BASIC, 0);
    477   r += expect_result_type ("bAsIC \t123", MHD_TEST_AUTHTYPE_BASIC, 0);
    478   r += expect_result_type ("bAsIC  abc ", MHD_TEST_AUTHTYPE_BASIC, 0);
    479   r += expect_result_type ("basic", MHD_TEST_AUTHTYPE_BASIC, 0);
    480   r += expect_result_type (" basic", MHD_TEST_AUTHTYPE_BASIC, 0);
    481   r += expect_result_type ("\tbasic", MHD_TEST_AUTHTYPE_BASIC, 0);
    482   r += expect_result_type ("\t basic", MHD_TEST_AUTHTYPE_BASIC, 0);
    483   r += expect_result_type (" \tbasic", MHD_TEST_AUTHTYPE_BASIC, 0);
    484   r += expect_result_type ("    basic", MHD_TEST_AUTHTYPE_BASIC, 0);
    485   r += expect_result_type ("\t\t\tbasic", MHD_TEST_AUTHTYPE_BASIC, 0);
    486   r += expect_result_type ("\t\t  \tbasic", MHD_TEST_AUTHTYPE_BASIC, 0);
    487   r += expect_result_type ("\t\t  \t basic", MHD_TEST_AUTHTYPE_BASIC, 0);
    488   r += expect_result_type ("basic ", MHD_TEST_AUTHTYPE_BASIC, 0);
    489   r += expect_result_type ("basic \t", MHD_TEST_AUTHTYPE_BASIC, 0);
    490   r += expect_result_type ("basic \t ", MHD_TEST_AUTHTYPE_BASIC, 0);
    491   r += expect_result_type ("basic 123", MHD_TEST_AUTHTYPE_BASIC, 0);
    492   r += expect_result_type ("basic \t123", MHD_TEST_AUTHTYPE_BASIC, 0);
    493   r += expect_result_type ("basic  abc ", MHD_TEST_AUTHTYPE_BASIC, 0);
    494   r += expect_result_type ("BASIC", MHD_TEST_AUTHTYPE_BASIC, 0);
    495   r += expect_result_type (" BASIC", MHD_TEST_AUTHTYPE_BASIC, 0);
    496   r += expect_result_type ("\tBASIC", MHD_TEST_AUTHTYPE_BASIC, 0);
    497   r += expect_result_type ("\t BASIC", MHD_TEST_AUTHTYPE_BASIC, 0);
    498   r += expect_result_type (" \tBASIC", MHD_TEST_AUTHTYPE_BASIC, 0);
    499   r += expect_result_type ("    BASIC", MHD_TEST_AUTHTYPE_BASIC, 0);
    500   r += expect_result_type ("\t\t\tBASIC", MHD_TEST_AUTHTYPE_BASIC, 0);
    501   r += expect_result_type ("\t\t  \tBASIC", MHD_TEST_AUTHTYPE_BASIC, 0);
    502   r += expect_result_type ("\t\t  \t BASIC", MHD_TEST_AUTHTYPE_BASIC, 0);
    503   r += expect_result_type ("BASIC ", MHD_TEST_AUTHTYPE_BASIC, 0);
    504   r += expect_result_type ("BASIC \t", MHD_TEST_AUTHTYPE_BASIC, 0);
    505   r += expect_result_type ("BASIC \t ", MHD_TEST_AUTHTYPE_BASIC, 0);
    506   r += expect_result_type ("BASIC 123", MHD_TEST_AUTHTYPE_BASIC, 0);
    507   r += expect_result_type ("BASIC \t123", MHD_TEST_AUTHTYPE_BASIC, 0);
    508   r += expect_result_type ("BASIC  abc ", MHD_TEST_AUTHTYPE_BASIC, 0);
    509   /* Only single token is allowed for 'Basic' Authorization */
    510   r += expect_result_type ("Basic a b", MHD_TEST_AUTHTYPE_NONE, 1);
    511   r += expect_result_type ("Basic a\tb", MHD_TEST_AUTHTYPE_NONE, 1);
    512   r += expect_result_type ("Basic a\tb", MHD_TEST_AUTHTYPE_NONE, 1);
    513   r += expect_result_type ("Basic abc1 b", MHD_TEST_AUTHTYPE_NONE, 1);
    514   r += expect_result_type ("Basic c abc1", MHD_TEST_AUTHTYPE_NONE, 1);
    515   r += expect_result_type ("Basic c abc1 ", MHD_TEST_AUTHTYPE_NONE, 1);
    516   r += expect_result_type ("Basic c abc1\t", MHD_TEST_AUTHTYPE_NONE, 1);
    517   r += expect_result_type ("Basic c\tabc1\t", MHD_TEST_AUTHTYPE_NONE, 1);
    518   r += expect_result_type ("Basic c abc1 b", MHD_TEST_AUTHTYPE_NONE, 1);
    519   r += expect_result_type ("Basic zyx, b", MHD_TEST_AUTHTYPE_NONE, 1);
    520   r += expect_result_type ("Basic zyx,b", MHD_TEST_AUTHTYPE_NONE, 1);
    521   r += expect_result_type ("Basic zyx ,b", MHD_TEST_AUTHTYPE_NONE, 1);
    522   r += expect_result_type ("Basic zyx;b", MHD_TEST_AUTHTYPE_NONE, 1);
    523   r += expect_result_type ("Basic zyx; b", MHD_TEST_AUTHTYPE_NONE, 1);
    524 
    525   r += expect_result_type ("Basic2", MHD_TEST_AUTHTYPE_NONE, 0);
    526   r += expect_result_type (" Basic2", MHD_TEST_AUTHTYPE_NONE, 0);
    527   r += expect_result_type (" Basic2 ", MHD_TEST_AUTHTYPE_NONE, 0);
    528   r += expect_result_type ("\tBasic2", MHD_TEST_AUTHTYPE_NONE, 0);
    529   r += expect_result_type ("\t Basic2", MHD_TEST_AUTHTYPE_NONE, 0);
    530   r += expect_result_type (" \tBasic2", MHD_TEST_AUTHTYPE_NONE, 0);
    531   r += expect_result_type ("    Basic2", MHD_TEST_AUTHTYPE_NONE, 0);
    532   r += expect_result_type ("\t\t\tBasic2", MHD_TEST_AUTHTYPE_NONE, 0);
    533   r += expect_result_type ("\t\t  \tBasic2", MHD_TEST_AUTHTYPE_NONE, 0);
    534   r += expect_result_type ("\t\t  \t Basic2", MHD_TEST_AUTHTYPE_NONE, 0);
    535   r += expect_result_type ("Basic2 ", MHD_TEST_AUTHTYPE_NONE, 0);
    536   r += expect_result_type ("Basic2 \t", MHD_TEST_AUTHTYPE_NONE, 0);
    537   r += expect_result_type ("Basic2 \t ", MHD_TEST_AUTHTYPE_NONE, 0);
    538   r += expect_result_type ("Basic2 123", MHD_TEST_AUTHTYPE_NONE, 0);
    539   r += expect_result_type ("Basic2 \t123", MHD_TEST_AUTHTYPE_NONE, 0);
    540   r += expect_result_type ("Basic2  abc ", MHD_TEST_AUTHTYPE_NONE, 0);
    541   r += expect_result_type ("BasicBasic", MHD_TEST_AUTHTYPE_NONE, 0);
    542   r += expect_result_type (" BasicBasic", MHD_TEST_AUTHTYPE_NONE, 0);
    543   r += expect_result_type ("\tBasicBasic", MHD_TEST_AUTHTYPE_NONE, 0);
    544   r += expect_result_type ("\t BasicBasic", MHD_TEST_AUTHTYPE_NONE, 0);
    545   r += expect_result_type (" \tBasicBasic", MHD_TEST_AUTHTYPE_NONE, 0);
    546   r += expect_result_type ("BasicBasic ", MHD_TEST_AUTHTYPE_NONE, 0);
    547   r += expect_result_type ("BasicBasic \t", MHD_TEST_AUTHTYPE_NONE, 0);
    548   r += expect_result_type ("BasicBasic \t\t", MHD_TEST_AUTHTYPE_NONE, 0);
    549   r += expect_result_type ("BasicDigest", MHD_TEST_AUTHTYPE_NONE, 0);
    550   r += expect_result_type (" BasicDigest", MHD_TEST_AUTHTYPE_NONE, 0);
    551   r += expect_result_type ("BasicDigest ", MHD_TEST_AUTHTYPE_NONE, 0);
    552   r += expect_result_type ("Basic\0", MHD_TEST_AUTHTYPE_NONE, 0);
    553   r += expect_result_type ("\0" "Basic", MHD_TEST_AUTHTYPE_NONE, 0);
    554 
    555   r += expect_result_type ("Digest", MHD_TEST_AUTHTYPE_DIGEST, 0);
    556   r += expect_result_type (" Digest", MHD_TEST_AUTHTYPE_DIGEST, 0);
    557   r += expect_result_type ("\tDigest", MHD_TEST_AUTHTYPE_DIGEST, 0);
    558   r += expect_result_type ("\t Digest", MHD_TEST_AUTHTYPE_DIGEST, 0);
    559   r += expect_result_type (" \tDigest", MHD_TEST_AUTHTYPE_DIGEST, 0);
    560   r += expect_result_type ("    Digest", MHD_TEST_AUTHTYPE_DIGEST, 0);
    561   r += expect_result_type ("\t\t\tDigest", MHD_TEST_AUTHTYPE_DIGEST, 0);
    562   r += expect_result_type ("\t\t  \tDigest", MHD_TEST_AUTHTYPE_DIGEST, 0);
    563   r += expect_result_type ("\t\t  \t Digest", MHD_TEST_AUTHTYPE_DIGEST, 0);
    564   r += expect_result_type ("Digest ", MHD_TEST_AUTHTYPE_DIGEST, 0);
    565   r += expect_result_type ("Digest \t", MHD_TEST_AUTHTYPE_DIGEST, 0);
    566   r += expect_result_type ("Digest \t ", MHD_TEST_AUTHTYPE_DIGEST, 0);
    567   r += expect_result_type ("\tDigest ", MHD_TEST_AUTHTYPE_DIGEST, 0);
    568   r += expect_result_type ("  Digest \t", MHD_TEST_AUTHTYPE_DIGEST, 0);
    569   r += expect_result_type ("\t \tDigest \t ", MHD_TEST_AUTHTYPE_DIGEST, 0);
    570 
    571   r += expect_result_type ("digEST", MHD_TEST_AUTHTYPE_DIGEST, 0);
    572   r += expect_result_type (" digEST", MHD_TEST_AUTHTYPE_DIGEST, 0);
    573   r += expect_result_type ("\tdigEST", MHD_TEST_AUTHTYPE_DIGEST, 0);
    574   r += expect_result_type ("\t digEST", MHD_TEST_AUTHTYPE_DIGEST, 0);
    575   r += expect_result_type (" \tdigEST", MHD_TEST_AUTHTYPE_DIGEST, 0);
    576   r += expect_result_type ("    digEST", MHD_TEST_AUTHTYPE_DIGEST, 0);
    577   r += expect_result_type ("\t\t\tdigEST", MHD_TEST_AUTHTYPE_DIGEST, 0);
    578   r += expect_result_type ("\t\t  \tdigEST", MHD_TEST_AUTHTYPE_DIGEST, 0);
    579   r += expect_result_type ("\t\t  \t digEST", MHD_TEST_AUTHTYPE_DIGEST, 0);
    580   r += expect_result_type ("digEST ", MHD_TEST_AUTHTYPE_DIGEST, 0);
    581   r += expect_result_type ("digEST \t", MHD_TEST_AUTHTYPE_DIGEST, 0);
    582   r += expect_result_type ("digEST \t ", MHD_TEST_AUTHTYPE_DIGEST, 0);
    583   r += expect_result_type ("\tdigEST ", MHD_TEST_AUTHTYPE_DIGEST, 0);
    584   r += expect_result_type ("  digEST \t", MHD_TEST_AUTHTYPE_DIGEST, 0);
    585   r += expect_result_type ("\t \tdigEST \t ", MHD_TEST_AUTHTYPE_DIGEST, 0);
    586   r += expect_result_type ("digest", MHD_TEST_AUTHTYPE_DIGEST, 0);
    587   r += expect_result_type (" digest", MHD_TEST_AUTHTYPE_DIGEST, 0);
    588   r += expect_result_type ("\tdigest", MHD_TEST_AUTHTYPE_DIGEST, 0);
    589   r += expect_result_type ("\t digest", MHD_TEST_AUTHTYPE_DIGEST, 0);
    590   r += expect_result_type (" \tdigest", MHD_TEST_AUTHTYPE_DIGEST, 0);
    591   r += expect_result_type ("    digest", MHD_TEST_AUTHTYPE_DIGEST, 0);
    592   r += expect_result_type ("\t\t\tdigest", MHD_TEST_AUTHTYPE_DIGEST, 0);
    593   r += expect_result_type ("\t\t  \tdigest", MHD_TEST_AUTHTYPE_DIGEST, 0);
    594   r += expect_result_type ("\t\t  \t digest", MHD_TEST_AUTHTYPE_DIGEST, 0);
    595   r += expect_result_type ("digest ", MHD_TEST_AUTHTYPE_DIGEST, 0);
    596   r += expect_result_type ("digest \t", MHD_TEST_AUTHTYPE_DIGEST, 0);
    597   r += expect_result_type ("digest \t ", MHD_TEST_AUTHTYPE_DIGEST, 0);
    598   r += expect_result_type ("\tdigest ", MHD_TEST_AUTHTYPE_DIGEST, 0);
    599   r += expect_result_type ("  digest \t", MHD_TEST_AUTHTYPE_DIGEST, 0);
    600   r += expect_result_type ("\t \tdigest \t ", MHD_TEST_AUTHTYPE_DIGEST, 0);
    601   r += expect_result_type ("DIGEST", MHD_TEST_AUTHTYPE_DIGEST, 0);
    602   r += expect_result_type (" DIGEST", MHD_TEST_AUTHTYPE_DIGEST, 0);
    603   r += expect_result_type ("\tDIGEST", MHD_TEST_AUTHTYPE_DIGEST, 0);
    604   r += expect_result_type ("\t DIGEST", MHD_TEST_AUTHTYPE_DIGEST, 0);
    605   r += expect_result_type (" \tDIGEST", MHD_TEST_AUTHTYPE_DIGEST, 0);
    606   r += expect_result_type ("    DIGEST", MHD_TEST_AUTHTYPE_DIGEST, 0);
    607   r += expect_result_type ("\t\t\tDIGEST", MHD_TEST_AUTHTYPE_DIGEST, 0);
    608   r += expect_result_type ("\t\t  \tDIGEST", MHD_TEST_AUTHTYPE_DIGEST, 0);
    609   r += expect_result_type ("\t\t  \t DIGEST", MHD_TEST_AUTHTYPE_DIGEST, 0);
    610   r += expect_result_type ("DIGEST ", MHD_TEST_AUTHTYPE_DIGEST, 0);
    611   r += expect_result_type ("DIGEST \t", MHD_TEST_AUTHTYPE_DIGEST, 0);
    612   r += expect_result_type ("DIGEST \t ", MHD_TEST_AUTHTYPE_DIGEST, 0);
    613   r += expect_result_type ("\tDIGEST ", MHD_TEST_AUTHTYPE_DIGEST, 0);
    614   r += expect_result_type ("  DIGEST \t", MHD_TEST_AUTHTYPE_DIGEST, 0);
    615   r += expect_result_type ("\t \tDIGEST \t ", MHD_TEST_AUTHTYPE_DIGEST, 0);
    616   r += expect_result_type ("Digest ,", MHD_TEST_AUTHTYPE_DIGEST, 0);
    617   r += expect_result_type ("Digest ,\t", MHD_TEST_AUTHTYPE_DIGEST, 0);
    618   r += expect_result_type ("Digest ,  ", MHD_TEST_AUTHTYPE_DIGEST, 0);
    619   r += expect_result_type ("Digest   ,  ", MHD_TEST_AUTHTYPE_DIGEST, 0);
    620   r += expect_result_type ("Digest ,\t, ,\t, ,\t, ,", \
    621                            MHD_TEST_AUTHTYPE_DIGEST, 0);
    622   r += expect_result_type ("Digest ,\t,\t,\t,\t,\t,\t,", \
    623                            MHD_TEST_AUTHTYPE_DIGEST, 0);
    624   r += expect_result_type ("Digest a=b", MHD_TEST_AUTHTYPE_DIGEST, 0);
    625   r += expect_result_type ("Digest a=\"b\"", MHD_TEST_AUTHTYPE_DIGEST, 0);
    626   r += expect_result_type ("Digest nc=1", MHD_TEST_AUTHTYPE_DIGEST, 0);
    627   r += expect_result_type ("Digest nc=\"1\"", MHD_TEST_AUTHTYPE_DIGEST, 0);
    628   r += expect_result_type ("Digest a=b ", MHD_TEST_AUTHTYPE_DIGEST, 0);
    629   r += expect_result_type ("Digest a=\"b\" ", MHD_TEST_AUTHTYPE_DIGEST, 0);
    630   r += expect_result_type ("Digest nc=1 ", MHD_TEST_AUTHTYPE_DIGEST, 0);
    631   r += expect_result_type ("Digest nc=\"1\" ", MHD_TEST_AUTHTYPE_DIGEST, 0);
    632   r += expect_result_type ("Digest a = b", MHD_TEST_AUTHTYPE_DIGEST, 0);
    633   r += expect_result_type ("Digest a\t=\t\"b\"", \
    634                            MHD_TEST_AUTHTYPE_DIGEST, 0);
    635   r += expect_result_type ("Digest nc =1", MHD_TEST_AUTHTYPE_DIGEST, 0);
    636   r += expect_result_type ("Digest nc= \"1\"", MHD_TEST_AUTHTYPE_DIGEST, 0);
    637   r += expect_result_type ("Digest a=\tb ", MHD_TEST_AUTHTYPE_DIGEST, 0);
    638   r += expect_result_type ("Digest a = \"b\" ", MHD_TEST_AUTHTYPE_DIGEST, 0);
    639   r += expect_result_type ("Digest nc\t\t\t= 1 ", \
    640                            MHD_TEST_AUTHTYPE_DIGEST, 0);
    641   r += expect_result_type ("Digest nc   =\t\t\t\"1\" ", \
    642                            MHD_TEST_AUTHTYPE_DIGEST, 0);
    643   r += expect_result_type ("Digest nc =1,,,,", MHD_TEST_AUTHTYPE_DIGEST, 0);
    644   r += expect_result_type ("Digest nc =1  ,,,,", \
    645                            MHD_TEST_AUTHTYPE_DIGEST, 0);
    646   r += expect_result_type ("Digest ,,,,nc= \"1 \"", \
    647                            MHD_TEST_AUTHTYPE_DIGEST, 0);
    648   r += expect_result_type ("Digest ,,,,  nc= \" 1\"", \
    649                            MHD_TEST_AUTHTYPE_DIGEST, 0);
    650   r += expect_result_type ("Digest ,,,, nc= \"1\",,,,", \
    651                            MHD_TEST_AUTHTYPE_DIGEST, 0);
    652   r += expect_result_type ("Digest ,,,, nc= \"1\"  ,,,,", \
    653                            MHD_TEST_AUTHTYPE_DIGEST, 0);
    654   r += expect_result_type ("Digest ,,,, nc= \"1\"  ,,,,", \
    655                            MHD_TEST_AUTHTYPE_DIGEST, 0);
    656   r += expect_result_type ("Digest ,,,, nc= \"1\"  ,,,,", \
    657                            MHD_TEST_AUTHTYPE_DIGEST, 0);
    658   r += expect_result_type ("Digest ,,,, nc= \"1\"  ,,,,,", \
    659                            MHD_TEST_AUTHTYPE_DIGEST, 0);
    660 
    661   r += expect_result_type ("Digest nc", MHD_TEST_AUTHTYPE_NONE, 1);
    662   r += expect_result_type ("Digest   nc", MHD_TEST_AUTHTYPE_NONE, 1);
    663   r += expect_result_type ("Digest nc  ", MHD_TEST_AUTHTYPE_NONE, 1);
    664   r += expect_result_type ("Digest nc  ,", MHD_TEST_AUTHTYPE_NONE, 1);
    665   r += expect_result_type ("Digest nc  , ", MHD_TEST_AUTHTYPE_NONE, 1);
    666   r += expect_result_type ("Digest \tnc\t  ", MHD_TEST_AUTHTYPE_NONE, 1);
    667   r += expect_result_type ("Digest \tnc\t  ", MHD_TEST_AUTHTYPE_NONE, 1);
    668   r += expect_result_type ("Digest nc,", MHD_TEST_AUTHTYPE_NONE, 1);
    669   r += expect_result_type ("Digest nc,uri", MHD_TEST_AUTHTYPE_NONE, 1);
    670   r += expect_result_type ("Digest nc=1,uri", MHD_TEST_AUTHTYPE_NONE, 1);
    671   r += expect_result_type ("Digest nc=1,uri   ", \
    672                            MHD_TEST_AUTHTYPE_NONE, 1);
    673   r += expect_result_type ("Digest nc=1,uri,", MHD_TEST_AUTHTYPE_NONE, 1);
    674   r += expect_result_type ("Digest nc=1, uri,", \
    675                            MHD_TEST_AUTHTYPE_NONE, 1);
    676   r += expect_result_type ("Digest nc=1,uri   ,", \
    677                            MHD_TEST_AUTHTYPE_NONE, 1);
    678   r += expect_result_type ("Digest nc=1,uri   , ", \
    679                            MHD_TEST_AUTHTYPE_NONE, 1);
    680   /* Binary zero */
    681   r += expect_result_type ("Digest nc=1\0", MHD_TEST_AUTHTYPE_NONE, 1);
    682   r += expect_result_type ("Digest nc=1\0" " ", \
    683                            MHD_TEST_AUTHTYPE_NONE, 1);
    684   r += expect_result_type ("Digest nc=1\t\0", MHD_TEST_AUTHTYPE_NONE, 1);
    685   r += expect_result_type ("Digest nc=\0" "1", MHD_TEST_AUTHTYPE_NONE, 1);
    686   /* Semicolon */
    687   r += expect_result_type ("Digest nc=1;", MHD_TEST_AUTHTYPE_NONE, 1);
    688   r += expect_result_type ("Digest nc=1; ", MHD_TEST_AUTHTYPE_NONE, 1);
    689   r += expect_result_type ("Digest nc=;1", MHD_TEST_AUTHTYPE_NONE, 1);
    690   r += expect_result_type ("Digest nc;=1", MHD_TEST_AUTHTYPE_NONE, 1);
    691   /* The equal sign alone */
    692   r += expect_result_type ("Digest =", MHD_TEST_AUTHTYPE_NONE, 1);
    693   r += expect_result_type ("Digest   =", MHD_TEST_AUTHTYPE_NONE, 1);
    694   r += expect_result_type ("Digest   =  ", MHD_TEST_AUTHTYPE_NONE, 1);
    695   r += expect_result_type ("Digest ,=", MHD_TEST_AUTHTYPE_NONE, 1);
    696   r += expect_result_type ("Digest , =", MHD_TEST_AUTHTYPE_NONE, 1);
    697   r += expect_result_type ("Digest ,= ", MHD_TEST_AUTHTYPE_NONE, 1);
    698   r += expect_result_type ("Digest , = ", MHD_TEST_AUTHTYPE_NONE, 1);
    699   r += expect_result_type ("Digest nc=1,=", MHD_TEST_AUTHTYPE_NONE, 1);
    700   r += expect_result_type ("Digest nc=1, =", MHD_TEST_AUTHTYPE_NONE, 1);
    701   r += expect_result_type ("Digest foo=bar,=", MHD_TEST_AUTHTYPE_NONE, 1);
    702   r += expect_result_type ("Digest foo=bar, =", \
    703                            MHD_TEST_AUTHTYPE_NONE, 1);
    704   /* Unclosed quotation */
    705   r += expect_result_type ("Digest nc=\"", MHD_TEST_AUTHTYPE_NONE, 1);
    706   r += expect_result_type ("Digest nc=\"abc", MHD_TEST_AUTHTYPE_NONE, 1);
    707   r += expect_result_type ("Digest nc=\"   ", MHD_TEST_AUTHTYPE_NONE, 1);
    708   r += expect_result_type ("Digest nc=\"abc   ", \
    709                            MHD_TEST_AUTHTYPE_NONE, 1);
    710   r += expect_result_type ("Digest nc=\"   abc", \
    711                            MHD_TEST_AUTHTYPE_NONE, 1);
    712   r += expect_result_type ("Digest nc=\"   abc", \
    713                            MHD_TEST_AUTHTYPE_NONE, 1);
    714   r += expect_result_type ("Digest nc=\"\\", MHD_TEST_AUTHTYPE_NONE, 1);
    715   r += expect_result_type ("Digest nc=\"\\\"", MHD_TEST_AUTHTYPE_NONE, 1);
    716   r += expect_result_type ("Digest nc=\"  \\\"", \
    717                            MHD_TEST_AUTHTYPE_NONE, 1);
    718   r += expect_result_type ("Digest nc=\"\\\"  ", \
    719                            MHD_TEST_AUTHTYPE_NONE, 1);
    720   r += expect_result_type ("Digest nc=\"  \\\"  ", \
    721                            MHD_TEST_AUTHTYPE_NONE, 1);
    722   r += expect_result_type ("Digest nc=\"\\\"\\\"\\\"\\\"", \
    723                            MHD_TEST_AUTHTYPE_NONE, 1);
    724   r += expect_result_type ("Digest nc= \"", MHD_TEST_AUTHTYPE_NONE, 1);
    725   r += expect_result_type ("Digest nc= \"abc", MHD_TEST_AUTHTYPE_NONE, 1);
    726   r += expect_result_type ("Digest nc= \"   ", MHD_TEST_AUTHTYPE_NONE, 1);
    727   r += expect_result_type ("Digest nc= \"abc   ", \
    728                            MHD_TEST_AUTHTYPE_NONE, 1);
    729   r += expect_result_type ("Digest nc= \"   abc", \
    730                            MHD_TEST_AUTHTYPE_NONE, 1);
    731   r += expect_result_type ("Digest nc= \"   abc", \
    732                            MHD_TEST_AUTHTYPE_NONE, 1);
    733   r += expect_result_type ("Digest nc= \"\\", \
    734                            MHD_TEST_AUTHTYPE_NONE, 1);
    735   r += expect_result_type ("Digest nc= \"\\\"", \
    736                            MHD_TEST_AUTHTYPE_NONE, 1);
    737   r += expect_result_type ("Digest nc= \"  \\\"", \
    738                            MHD_TEST_AUTHTYPE_NONE, 1);
    739   r += expect_result_type ("Digest nc= \"\\\"  ", \
    740                            MHD_TEST_AUTHTYPE_NONE, 1);
    741   r += expect_result_type ("Digest nc= \"  \\\"  ", \
    742                            MHD_TEST_AUTHTYPE_NONE, 1);
    743   r += expect_result_type ("Digest nc= \"\\\"\\\"\\\"\\\"", \
    744                            MHD_TEST_AUTHTYPE_NONE, 1);
    745   r += expect_result_type ("Digest foo=\"", MHD_TEST_AUTHTYPE_NONE, 1);
    746   r += expect_result_type ("Digest foo=\"bar", MHD_TEST_AUTHTYPE_NONE, 1);
    747   r += expect_result_type ("Digest foo=\"   ", MHD_TEST_AUTHTYPE_NONE, 1);
    748   r += expect_result_type ("Digest foo=\"bar   ", \
    749                            MHD_TEST_AUTHTYPE_NONE, 1);
    750   r += expect_result_type ("Digest foo=\"   bar", \
    751                            MHD_TEST_AUTHTYPE_NONE, 1);
    752   r += expect_result_type ("Digest foo=\"   bar", \
    753                            MHD_TEST_AUTHTYPE_NONE, 1);
    754   r += expect_result_type ("Digest foo= \"   bar", \
    755                            MHD_TEST_AUTHTYPE_NONE, 1);
    756   r += expect_result_type ("Digest foo=\",   bar", \
    757                            MHD_TEST_AUTHTYPE_NONE, 1);
    758   r += expect_result_type ("Digest foo=\"   bar,", \
    759                            MHD_TEST_AUTHTYPE_NONE, 1);
    760   r += expect_result_type ("Digest foo=\"\\\"", \
    761                            MHD_TEST_AUTHTYPE_NONE, 1);
    762   r += expect_result_type ("Digest foo=\"  \\\"", \
    763                            MHD_TEST_AUTHTYPE_NONE, 1);
    764   r += expect_result_type ("Digest foo=\"\\\"  ", \
    765                            MHD_TEST_AUTHTYPE_NONE, 1);
    766   r += expect_result_type ("Digest foo=\"  \\\"  ", \
    767                            MHD_TEST_AUTHTYPE_NONE, 1);
    768   r += expect_result_type ("Digest foo=\"\\\"\\\"\\\"\\\"", \
    769                            MHD_TEST_AUTHTYPE_NONE, 1);
    770   /* Full set of parameters with semicolon inside */
    771   r += expect_result_type ("Digest username=\"test@example.com\", " \
    772                            "realm=\"users@example.com\", nonce=\"32141232413abcde\", " \
    773                            "uri=\"/example\", qop=auth, nc=00000001; cnonce=\"0a4f113b\", " \
    774                            "response=\"6629fae49393a05397450978507c4ef1\", " \
    775                            "opaque=\"sadfljk32sdaf\"", \
    776                            MHD_TEST_AUTHTYPE_NONE, 1);
    777   r += expect_result_type ("Digest username=\"test@example.com\", " \
    778                            "realm=\"users@example.com\", nonce=\"32141232413abcde\", " \
    779                            "uri=\"/example\", qop=auth, nc=00000001;cnonce=\"0a4f113b\", " \
    780                            "response=\"6629fae49393a05397450978507c4ef1\", " \
    781                            "opaque=\"sadfljk32sdaf\"", \
    782                            MHD_TEST_AUTHTYPE_NONE, 1);
    783   r += expect_result_type ("Digest username;=\"test@example.com\", " \
    784                            "realm=\"users@example.com\", nonce=\"32141232413abcde\", " \
    785                            "uri=\"/example\", qop=auth, nc=00000001, cnonce=\"0a4f113b\", " \
    786                            "response=\"6629fae49393a05397450978507c4ef1\", " \
    787                            "opaque=\"sadfljk32sdaf\"", \
    788                            MHD_TEST_AUTHTYPE_NONE, 1);
    789 
    790   r += expect_result_type ("Digest2", MHD_TEST_AUTHTYPE_NONE, 0);
    791   r += expect_result_type ("2Digest", MHD_TEST_AUTHTYPE_NONE, 0);
    792   r += expect_result_type ("Digest" "a", MHD_TEST_AUTHTYPE_NONE, 0);
    793   r += expect_result_type ("a" "Digest", MHD_TEST_AUTHTYPE_NONE, 0);
    794   r += expect_result_type (" Digest2", MHD_TEST_AUTHTYPE_NONE, 0);
    795   r += expect_result_type (" 2Digest", MHD_TEST_AUTHTYPE_NONE, 0);
    796   r += expect_result_type (" Digest" "a", MHD_TEST_AUTHTYPE_NONE, 0);
    797   r += expect_result_type (" a" "Digest", MHD_TEST_AUTHTYPE_NONE, 0);
    798   r += expect_result_type ("Digest2 ", MHD_TEST_AUTHTYPE_NONE, 0);
    799   r += expect_result_type ("2Digest ", MHD_TEST_AUTHTYPE_NONE, 0);
    800   r += expect_result_type ("Digest" "a", MHD_TEST_AUTHTYPE_NONE, 0);
    801   r += expect_result_type ("a" "Digest ", MHD_TEST_AUTHTYPE_NONE, 0);
    802   r += expect_result_type ("DigestBasic", MHD_TEST_AUTHTYPE_NONE, 0);
    803   r += expect_result_type ("DigestBasic ", MHD_TEST_AUTHTYPE_NONE, 0);
    804   r += expect_result_type (" DigestBasic", MHD_TEST_AUTHTYPE_NONE, 0);
    805   r += expect_result_type ("DigestBasic" "a", MHD_TEST_AUTHTYPE_NONE, 0);
    806   r += expect_result_type ("Digest" "\0", MHD_TEST_AUTHTYPE_NONE, 0);
    807   r += expect_result_type ("\0" "Digest", MHD_TEST_AUTHTYPE_NONE, 0);
    808   return r;
    809 }
    810 
    811 
    812 #ifdef BAUTH_SUPPORT
    813 
    814 /* return zero if succeed, 1 otherwise */
    815 static unsigned int
    816 expect_basic_n (const char *hdr, size_t hdr_len,
    817                 const char *tkn, size_t tkn_len,
    818                 unsigned int line_num)
    819 {
    820   const struct MHD_RqBAuth *h;
    821   unsigned int ret;
    822 
    823   mhd_assert (NULL != hdr);
    824   mhd_assert (0 != hdr_len);
    825 
    826   add_AuthHeader (hdr, hdr_len);
    827   h = get_BAuthRqParams ();
    828   if (NULL == h)
    829     mhdErrorExitDesc ("'MHD_get_rq_bauth_params_()' returned NULL");
    830   ret = 1;
    831   if (tkn_len != h->token68.len)
    832     fprintf (stderr,
    833              "'Authorization' header parsing FAILED:\n"
    834              "Wrong token length:\tRESULT[%u]: %.*s\tEXPECTED[%u]: %.*s\n",
    835              (unsigned) h->token68.len,
    836              (int) h->token68.len,
    837              h->token68.str ?
    838              h->token68.str : "(NULL)",
    839              (unsigned) tkn_len, (int) tkn_len, tkn ? tkn : "(NULL)");
    840   else if ( ((NULL == tkn) != (NULL == h->token68.str)) ||
    841             ((NULL != tkn) &&
    842              (0 != memcmp (tkn, h->token68.str, tkn_len))) )
    843     fprintf (stderr,
    844              "'Authorization' header parsing FAILED:\n"
    845              "Wrong token string:\tRESULT[%u]: %.*s\tEXPECTED[%u]: %.*s\n",
    846              (unsigned) h->token68.len,
    847              (int) h->token68.len,
    848              h->token68.str ?
    849              h->token68.str : "(NULL)",
    850              (unsigned) tkn_len, (int) tkn_len, tkn ? tkn : "(NULL)");
    851   else
    852     ret = 0;
    853   if (0 != ret)
    854   {
    855     fprintf (stderr,
    856              "Input Header: '%.*s'\n", (int) hdr_len, hdr);
    857     fprintf (stderr,
    858              "The check is at line: %u\n\n", line_num);
    859   }
    860   clean_AuthHeaders ();
    861 
    862   return ret;
    863 }
    864 
    865 
    866 #define expect_basic(h,t) \
    867     expect_basic_n(h,MHD_STATICSTR_LEN_(h),t,MHD_STATICSTR_LEN_(t),__LINE__)
    868 
    869 static unsigned int
    870 check_basic (void)
    871 {
    872   unsigned int r = 0; /**< The number of errors */
    873 
    874   r += expect_basic ("Basic a", "a");
    875   r += expect_basic ("Basic    a", "a");
    876   r += expect_basic ("Basic \ta", "a");
    877   r += expect_basic ("Basic \ta\t", "a");
    878   r += expect_basic ("Basic \ta ", "a");
    879   r += expect_basic ("Basic  a ", "a");
    880   r += expect_basic ("Basic \t a\t ", "a");
    881   r += expect_basic ("Basic \t abc\t ", "abc");
    882   r += expect_basic ("Basic 2143sdfa4325sdfgfdab354354314SDSDFc", \
    883                      "2143sdfa4325sdfgfdab354354314SDSDFc");
    884   r += expect_basic ("Basic 2143sdfa4325sdfgfdab354354314SDSDFc  ", \
    885                      "2143sdfa4325sdfgfdab354354314SDSDFc");
    886   r += expect_basic ("Basic   2143sdfa4325sdfgfdab354354314SDSDFc", \
    887                      "2143sdfa4325sdfgfdab354354314SDSDFc");
    888   r += expect_basic ("Basic   2143sdfa4325sdfgfdab354354314SDSDFc  ", \
    889                      "2143sdfa4325sdfgfdab354354314SDSDFc");
    890   r += expect_basic ("  Basic 2143sdfa4325sdfgfdab354354314SDSDFc", \
    891                      "2143sdfa4325sdfgfdab354354314SDSDFc");
    892   r += expect_basic ("  Basic  2143sdfa4325sdfgfdab354354314SDSDFc", \
    893                      "2143sdfa4325sdfgfdab354354314SDSDFc");
    894   r += expect_basic ("  Basic 2143sdfa4325sdfgfdab354354314SDSDFc ", \
    895                      "2143sdfa4325sdfgfdab354354314SDSDFc");
    896   r += expect_basic ("  Basic  2143sdfa4325sdfgfdab354354314SDSDFc ", \
    897                      "2143sdfa4325sdfgfdab354354314SDSDFc");
    898   r += expect_basic ("  Basic  2143sdfa4325sdfgfdab354354314SDSDFc  ", \
    899                      "2143sdfa4325sdfgfdab354354314SDSDFc");
    900   r += expect_basic ("Basic -A.1-z~9+/=====", "-A.1-z~9+/=====");
    901   r += expect_basic ("  Basic   -A.1-z~9+/===== ", "-A.1-z~9+/=====");
    902 
    903   r += expect_basic_n ("Basic", MHD_STATICSTR_LEN_ ("Basic"), NULL, 0,__LINE__);
    904   r += expect_basic_n ("   Basic", MHD_STATICSTR_LEN_ ("   Basic"), NULL, 0,
    905                        __LINE__);
    906   r += expect_basic_n ("Basic   ", MHD_STATICSTR_LEN_ ("Basic   "), NULL, 0,
    907                        __LINE__);
    908   r += expect_basic_n ("Basic \t\t", MHD_STATICSTR_LEN_ ("Basic \t\t"), NULL, 0,
    909                        __LINE__);
    910 
    911   return r;
    912 }
    913 
    914 
    915 #endif /* BAUTH_SUPPORT */
    916 
    917 
    918 #ifdef DAUTH_SUPPORT
    919 
    920 /* return zero if succeed, 1 otherwise */
    921 static unsigned int
    922 cmp_dauth_param (const char *pname, const struct MHD_RqDAuthParam *param,
    923                  const char *expected_value)
    924 {
    925   unsigned int ret;
    926   size_t expected_len;
    927   bool expected_quoted;
    928   mhd_assert (NULL != param);
    929   mhd_assert (NULL != pname);
    930   ret = 0;
    931 
    932   if (NULL == expected_value)
    933   {
    934     expected_len = 0;
    935     expected_quoted = false;
    936     if (NULL != param->value.str)
    937       ret = 1;
    938     else if (param->value.len != expected_len)
    939       ret = 1;
    940     else if (param->quoted != expected_quoted)
    941       ret = 1;
    942   }
    943   else
    944   {
    945     expected_len = strlen (expected_value);
    946     expected_quoted = (NULL != memchr (expected_value, '\\', expected_len));
    947     if (NULL == param->value.str)
    948       ret = 1;
    949     else if (param->value.len != expected_len)
    950       ret = 1;
    951     else if (param->quoted != expected_quoted)
    952       ret = 1;
    953     else if (0 != memcmp (param->value.str, expected_value, expected_len))
    954       ret = 1;
    955   }
    956   if (0 != ret)
    957   {
    958     fprintf (stderr, "Parameter '%s' parsed incorrectly:\n", pname);
    959     fprintf (stderr, "\tRESULT  :\tvalue.str: %.*s",
    960              (int) (param->value.str ? param->value.len : 6),
    961              param->value.str ? param->value.str : "(NULL)");
    962     fprintf (stderr, "\tvalue.len: %u",
    963              (unsigned) param->value.len);
    964     fprintf (stderr, "\tquoted: %s\n",
    965              (unsigned) param->quoted ? "true" : "false");
    966     fprintf (stderr, "\tEXPECTED:\tvalue.str: %.*s",
    967              (int) (expected_value ? expected_len : 6),
    968              expected_value ? expected_value : "(NULL)");
    969     fprintf (stderr, "\tvalue.len: %u",
    970              (unsigned) expected_len);
    971     fprintf (stderr, "\tquoted: %s\n",
    972              (unsigned) expected_quoted ? "true" : "false");
    973   }
    974   return ret;
    975 }
    976 
    977 
    978 /* return zero if succeed, 1 otherwise */
    979 static unsigned int
    980 expect_digest_n (const char *hdr, size_t hdr_len,
    981                  const char *nonce,
    982                  enum MHD_DigestAuthAlgo3 algo3,
    983                  const char *response,
    984                  const char *username,
    985                  const char *username_ext,
    986                  const char *realm,
    987                  const char *uri,
    988                  const char *qop_raw,
    989                  enum MHD_DigestAuthQOP qop,
    990                  const char *cnonce,
    991                  const char *nc,
    992                  int userhash,
    993                  unsigned int line_num)
    994 {
    995   const struct MHD_RqDAuth *h;
    996   unsigned int ret;
    997 
    998   mhd_assert (NULL != hdr);
    999   mhd_assert (0 != hdr_len);
   1000 
   1001   add_AuthHeader (hdr, hdr_len);
   1002 
   1003   h = get_DAuthRqParams ();
   1004   if (NULL == h)
   1005     mhdErrorExitDesc ("'MHD_get_rq_dauth_params_()' returned NULL");
   1006   ret = 0;
   1007 
   1008   ret += cmp_dauth_param ("nonce", &h->nonce, nonce);
   1009   if (h->algo3 != algo3)
   1010   {
   1011     ret += 1;
   1012     fprintf (stderr, "Parameter 'algorithm' detected incorrectly:\n");
   1013     fprintf (stderr, "\tRESULT  :\t%u\n",
   1014              (unsigned) h->algo3);
   1015     fprintf (stderr, "\tEXPECTED:\t%u\n",
   1016              (unsigned) algo3);
   1017   }
   1018   ret += cmp_dauth_param ("response", &h->response, response);
   1019   ret += cmp_dauth_param ("username", &h->username, username);
   1020   ret += cmp_dauth_param ("username_ext", &h->username_ext,
   1021                           username_ext);
   1022   ret += cmp_dauth_param ("realm", &h->realm, realm);
   1023   ret += cmp_dauth_param ("uri", &h->uri, uri);
   1024   ret += cmp_dauth_param ("qop", &h->qop_raw, qop_raw);
   1025   if (h->qop != qop)
   1026   {
   1027     ret += 1;
   1028     fprintf (stderr, "Parameter 'qop' detected incorrectly:\n");
   1029     fprintf (stderr, "\tRESULT  :\t%u\n",
   1030              (unsigned) h->qop);
   1031     fprintf (stderr, "\tEXPECTED:\t%u\n",
   1032              (unsigned) qop);
   1033   }
   1034   ret += cmp_dauth_param ("cnonce", &h->cnonce, cnonce);
   1035   ret += cmp_dauth_param ("nc", &h->nc, nc);
   1036   if (h->userhash != ! (! userhash))
   1037   {
   1038     ret += 1;
   1039     fprintf (stderr, "Parameter 'userhash' parsed incorrectly:\n");
   1040     fprintf (stderr, "\tRESULT  :\t%s\n",
   1041              h->userhash ? "true" : "false");
   1042     fprintf (stderr, "\tEXPECTED:\t%s\n",
   1043              userhash ? "true" : "false");
   1044   }
   1045   if (0 != ret)
   1046   {
   1047     fprintf (stderr,
   1048              "Input Header: '%.*s'\n", (int) hdr_len, hdr);
   1049     fprintf (stderr,
   1050              "The check is at line: %u\n\n", line_num);
   1051   }
   1052   clean_AuthHeaders ();
   1053 
   1054   return ret;
   1055 }
   1056 
   1057 
   1058 #define expect_digest(h,no,a,rs,un,ux,rm,ur,qr,qe,c,nc,uh) \
   1059     expect_digest_n(h,MHD_STATICSTR_LEN_(h),\
   1060                     no,a,rs,un,ux,rm,ur,qr,qe,c,nc,uh,__LINE__)
   1061 
   1062 static unsigned int
   1063 check_digest (void)
   1064 {
   1065   unsigned int r = 0; /**< The number of errors */
   1066 
   1067   r += expect_digest ("Digest", NULL, MHD_DIGEST_AUTH_ALGO3_MD5, \
   1068                       NULL, NULL, NULL, NULL, NULL, \
   1069                       NULL, MHD_DIGEST_AUTH_QOP_NONE, NULL, NULL, 0);
   1070   r += expect_digest ("Digest nc=1", NULL, MHD_DIGEST_AUTH_ALGO3_MD5, \
   1071                       NULL, NULL, NULL, NULL, NULL, NULL, \
   1072                       MHD_DIGEST_AUTH_QOP_NONE, NULL, "1", 0);
   1073   r += expect_digest ("Digest nc=\"1\"", NULL, MHD_DIGEST_AUTH_ALGO3_MD5, \
   1074                       NULL, NULL, NULL, NULL, NULL, \
   1075                       NULL, MHD_DIGEST_AUTH_QOP_NONE, NULL, "1", 0);
   1076   r += expect_digest ("Digest nc=\"1\"   ", NULL, MHD_DIGEST_AUTH_ALGO3_MD5, \
   1077                       NULL, NULL, NULL, NULL, NULL, \
   1078                       NULL, MHD_DIGEST_AUTH_QOP_NONE, NULL, "1", 0);
   1079   r += expect_digest ("Digest ,nc=\"1\"   ", NULL, MHD_DIGEST_AUTH_ALGO3_MD5, \
   1080                       NULL, NULL, NULL, NULL, NULL, \
   1081                       NULL, MHD_DIGEST_AUTH_QOP_NONE, NULL, "1", 0);
   1082   r += expect_digest ("Digest nc=\"1\",   ", NULL, MHD_DIGEST_AUTH_ALGO3_MD5, \
   1083                       NULL, NULL, NULL, NULL, NULL, \
   1084                       NULL, MHD_DIGEST_AUTH_QOP_NONE, NULL, "1", 0);
   1085   r += expect_digest ("Digest nc=\"1\" ,   ", NULL, MHD_DIGEST_AUTH_ALGO3_MD5, \
   1086                       NULL, NULL, NULL, NULL, NULL, \
   1087                       NULL, MHD_DIGEST_AUTH_QOP_NONE, NULL, "1", 0);
   1088   r += expect_digest ("Digest nc=1,   ", NULL, MHD_DIGEST_AUTH_ALGO3_MD5, \
   1089                       NULL, NULL, NULL, NULL, NULL, \
   1090                       NULL, MHD_DIGEST_AUTH_QOP_NONE, NULL, "1", 0);
   1091   r += expect_digest ("Digest nc=1 ,   ", NULL, MHD_DIGEST_AUTH_ALGO3_MD5, \
   1092                       NULL, NULL, NULL, NULL, NULL, \
   1093                       NULL, MHD_DIGEST_AUTH_QOP_NONE, NULL, "1", 0);
   1094   r += expect_digest ("Digest ,,,nc=1,   ", NULL, MHD_DIGEST_AUTH_ALGO3_MD5, \
   1095                       NULL, NULL, NULL, NULL, NULL, \
   1096                       NULL, MHD_DIGEST_AUTH_QOP_NONE, NULL, "1", 0);
   1097   r += expect_digest ("Digest ,,,nc=1 ,   ", NULL, MHD_DIGEST_AUTH_ALGO3_MD5, \
   1098                       NULL, NULL, NULL, NULL, NULL, \
   1099                       NULL, MHD_DIGEST_AUTH_QOP_NONE, NULL, "1", 0);
   1100   r += expect_digest ("Digest ,,,nc=\"1 \",   ", NULL, \
   1101                       MHD_DIGEST_AUTH_ALGO3_MD5, \
   1102                       NULL, NULL, NULL, NULL, NULL, \
   1103                       NULL, MHD_DIGEST_AUTH_QOP_NONE, NULL, "1 ", 0);
   1104   r += expect_digest ("Digest nc=\"1 \"", NULL, MHD_DIGEST_AUTH_ALGO3_MD5, \
   1105                       NULL, NULL, NULL, NULL, NULL, \
   1106                       NULL, MHD_DIGEST_AUTH_QOP_NONE, NULL, "1 ", 0);
   1107   r += expect_digest ("Digest nc=\"1 \" ,", NULL, MHD_DIGEST_AUTH_ALGO3_MD5, \
   1108                       NULL, NULL, NULL, NULL, NULL, \
   1109                       NULL, MHD_DIGEST_AUTH_QOP_NONE, NULL, "1 ", 0);
   1110   r += expect_digest ("Digest nc=\"1 \", ", NULL, MHD_DIGEST_AUTH_ALGO3_MD5, \
   1111                       NULL, NULL, NULL, NULL, NULL, \
   1112                       NULL, MHD_DIGEST_AUTH_QOP_NONE, NULL, "1 ", 0);
   1113   r += expect_digest ("Digest nc=\"1;\", ", NULL, MHD_DIGEST_AUTH_ALGO3_MD5, \
   1114                       NULL, NULL, NULL, NULL, NULL, \
   1115                       NULL, MHD_DIGEST_AUTH_QOP_NONE, NULL, "1;", 0);
   1116   r += expect_digest ("Digest nc=\"1\\;\", ", NULL, MHD_DIGEST_AUTH_ALGO3_MD5, \
   1117                       NULL, NULL, NULL, NULL, NULL, \
   1118                       NULL, MHD_DIGEST_AUTH_QOP_NONE, NULL, "1\\;", 0);
   1119 
   1120   r += expect_digest ("Digest userhash=false", NULL, \
   1121                       MHD_DIGEST_AUTH_ALGO3_MD5, \
   1122                       NULL, NULL, NULL, NULL, NULL, \
   1123                       NULL, MHD_DIGEST_AUTH_QOP_NONE, NULL, NULL, 0);
   1124   r += expect_digest ("Digest userhash=\"false\"", NULL, \
   1125                       MHD_DIGEST_AUTH_ALGO3_MD5, \
   1126                       NULL, NULL, NULL, NULL, NULL, \
   1127                       NULL, MHD_DIGEST_AUTH_QOP_NONE, NULL, NULL, 0);
   1128   r += expect_digest ("Digest userhash=foo", NULL, \
   1129                       MHD_DIGEST_AUTH_ALGO3_MD5, \
   1130                       NULL, NULL, NULL, NULL, NULL, \
   1131                       NULL, MHD_DIGEST_AUTH_QOP_NONE, NULL, NULL, 0);
   1132   r += expect_digest ("Digest userhash=true", NULL, \
   1133                       MHD_DIGEST_AUTH_ALGO3_MD5, \
   1134                       NULL, NULL, NULL, NULL, NULL, \
   1135                       NULL, MHD_DIGEST_AUTH_QOP_NONE, NULL, NULL, 1);
   1136   r += expect_digest ("Digest userhash=\"true\"", NULL, \
   1137                       MHD_DIGEST_AUTH_ALGO3_MD5, \
   1138                       NULL, NULL, NULL, NULL, NULL, \
   1139                       NULL, MHD_DIGEST_AUTH_QOP_NONE, NULL, NULL, 1);
   1140   r += expect_digest ("Digest userhash=\"\\t\\r\\u\\e\"", NULL, \
   1141                       MHD_DIGEST_AUTH_ALGO3_MD5, \
   1142                       NULL, NULL, NULL, NULL, NULL, \
   1143                       NULL, MHD_DIGEST_AUTH_QOP_NONE, NULL, NULL, 1);
   1144   r += expect_digest ("Digest userhash=TRUE", NULL, \
   1145                       MHD_DIGEST_AUTH_ALGO3_MD5, \
   1146                       NULL, NULL, NULL, NULL, NULL, \
   1147                       NULL, MHD_DIGEST_AUTH_QOP_NONE, NULL, NULL, 1);
   1148   r += expect_digest ("Digest userhash=True", NULL, \
   1149                       MHD_DIGEST_AUTH_ALGO3_MD5, \
   1150                       NULL, NULL, NULL, NULL, NULL, \
   1151                       NULL, MHD_DIGEST_AUTH_QOP_NONE, NULL, NULL, 1);
   1152   r += expect_digest ("Digest userhash = true", NULL, \
   1153                       MHD_DIGEST_AUTH_ALGO3_MD5, \
   1154                       NULL, NULL, NULL,  NULL, NULL, \
   1155                       NULL, MHD_DIGEST_AUTH_QOP_NONE, NULL, NULL, 1);
   1156   r += expect_digest ("Digest userhash=True2", NULL, \
   1157                       MHD_DIGEST_AUTH_ALGO3_MD5, \
   1158                       NULL, NULL, NULL, NULL, NULL, \
   1159                       NULL, MHD_DIGEST_AUTH_QOP_NONE, NULL, NULL, 0);
   1160   r += expect_digest ("Digest userhash=\" true\"", NULL, \
   1161                       MHD_DIGEST_AUTH_ALGO3_MD5, \
   1162                       NULL, NULL, NULL,  NULL, NULL, \
   1163                       NULL, MHD_DIGEST_AUTH_QOP_NONE, NULL, NULL, 0);
   1164 
   1165   r += expect_digest ("Digest algorithm=MD5", NULL, \
   1166                       MHD_DIGEST_AUTH_ALGO3_MD5, \
   1167                       NULL, NULL, NULL, NULL, NULL, \
   1168                       NULL, MHD_DIGEST_AUTH_QOP_NONE, NULL, NULL, 0);
   1169   r += expect_digest ("Digest algorithm=md5", NULL, \
   1170                       MHD_DIGEST_AUTH_ALGO3_MD5, \
   1171                       NULL, NULL, NULL, NULL, NULL, \
   1172                       NULL, MHD_DIGEST_AUTH_QOP_NONE, NULL, NULL, 0);
   1173   r += expect_digest ("Digest algorithm=Md5", NULL, \
   1174                       MHD_DIGEST_AUTH_ALGO3_MD5, \
   1175                       NULL, NULL, NULL, NULL, NULL, \
   1176                       NULL, MHD_DIGEST_AUTH_QOP_NONE, NULL, NULL, 0);
   1177   r += expect_digest ("Digest algorithm=mD5", NULL, \
   1178                       MHD_DIGEST_AUTH_ALGO3_MD5, \
   1179                       NULL, NULL, NULL, NULL, NULL, \
   1180                       NULL, MHD_DIGEST_AUTH_QOP_NONE, NULL, NULL, 0);
   1181   r += expect_digest ("Digest algorithm=\"MD5\"", NULL, \
   1182                       MHD_DIGEST_AUTH_ALGO3_MD5, \
   1183                       NULL, NULL, NULL, NULL, NULL, \
   1184                       NULL, MHD_DIGEST_AUTH_QOP_NONE, NULL, NULL, 0);
   1185   r += expect_digest ("Digest algorithm=\"\\M\\D\\5\"", NULL, \
   1186                       MHD_DIGEST_AUTH_ALGO3_MD5, \
   1187                       NULL, NULL, NULL, NULL, NULL, \
   1188                       NULL, MHD_DIGEST_AUTH_QOP_NONE, NULL, NULL, 0);
   1189   r += expect_digest ("Digest algorithm=\"\\m\\d\\5\"", NULL, \
   1190                       MHD_DIGEST_AUTH_ALGO3_MD5, \
   1191                       NULL, NULL, NULL, NULL, NULL, \
   1192                       NULL, MHD_DIGEST_AUTH_QOP_NONE, NULL, NULL, 0);
   1193   r += expect_digest ("Digest algorithm=SHA-256", NULL, \
   1194                       MHD_DIGEST_AUTH_ALGO3_SHA256, \
   1195                       NULL, NULL, NULL, NULL, NULL, \
   1196                       NULL, MHD_DIGEST_AUTH_QOP_NONE, NULL, NULL, 0);
   1197   r += expect_digest ("Digest algorithm=sha-256", NULL, \
   1198                       MHD_DIGEST_AUTH_ALGO3_SHA256, \
   1199                       NULL, NULL, NULL, NULL, NULL, \
   1200                       NULL, MHD_DIGEST_AUTH_QOP_NONE, NULL, NULL, 0);
   1201   r += expect_digest ("Digest algorithm=Sha-256", NULL, \
   1202                       MHD_DIGEST_AUTH_ALGO3_SHA256, \
   1203                       NULL, NULL, NULL, NULL, NULL, \
   1204                       NULL, MHD_DIGEST_AUTH_QOP_NONE, NULL, NULL, 0);
   1205   r += expect_digest ("Digest algorithm=\"SHA-256\"", NULL, \
   1206                       MHD_DIGEST_AUTH_ALGO3_SHA256, \
   1207                       NULL, NULL, NULL, NULL, NULL, \
   1208                       NULL, MHD_DIGEST_AUTH_QOP_NONE, NULL, NULL, 0);
   1209   r += expect_digest ("Digest algorithm=\"SHA\\-25\\6\"", NULL, \
   1210                       MHD_DIGEST_AUTH_ALGO3_SHA256, \
   1211                       NULL, NULL, NULL, NULL, NULL, \
   1212                       NULL, MHD_DIGEST_AUTH_QOP_NONE, NULL, NULL, 0);
   1213   r += expect_digest ("Digest algorithm=\"shA-256\"", NULL, \
   1214                       MHD_DIGEST_AUTH_ALGO3_SHA256, \
   1215                       NULL, NULL, NULL, NULL, NULL, \
   1216                       NULL, MHD_DIGEST_AUTH_QOP_NONE, NULL, NULL, 0);
   1217   r += expect_digest ("Digest algorithm=MD5-sess", NULL, \
   1218                       MHD_DIGEST_AUTH_ALGO3_MD5_SESSION, \
   1219                       NULL, NULL, NULL, NULL, NULL, \
   1220                       NULL, MHD_DIGEST_AUTH_QOP_NONE, NULL, NULL, 0);
   1221   r += expect_digest ("Digest algorithm=MD5-SESS", NULL, \
   1222                       MHD_DIGEST_AUTH_ALGO3_MD5_SESSION, \
   1223                       NULL, NULL, NULL, NULL, NULL, \
   1224                       NULL, MHD_DIGEST_AUTH_QOP_NONE, NULL, NULL, 0);
   1225   r += expect_digest ("Digest algorithm=md5-Sess", NULL, \
   1226                       MHD_DIGEST_AUTH_ALGO3_MD5_SESSION, \
   1227                       NULL, NULL, NULL, NULL, NULL, \
   1228                       NULL, MHD_DIGEST_AUTH_QOP_NONE, NULL, NULL, 0);
   1229   r += expect_digest ("Digest algorithm=SHA-256-seSS", NULL, \
   1230                       MHD_DIGEST_AUTH_ALGO3_SHA256_SESSION, \
   1231                       NULL, NULL, NULL, NULL, NULL, \
   1232                       NULL, MHD_DIGEST_AUTH_QOP_NONE, NULL, NULL, 0);
   1233   r += expect_digest ("Digest algorithm=SHA-512-256", NULL, \
   1234                       MHD_DIGEST_AUTH_ALGO3_SHA512_256, \
   1235                       NULL, NULL, NULL, NULL, NULL, \
   1236                       NULL, MHD_DIGEST_AUTH_QOP_NONE, NULL, NULL, 0);
   1237   r += expect_digest ("Digest algorithm=SHA-512-256-sess", NULL, \
   1238                       MHD_DIGEST_AUTH_ALGO3_SHA512_256_SESSION, \
   1239                       NULL, NULL, NULL, NULL, NULL, \
   1240                       NULL, MHD_DIGEST_AUTH_QOP_NONE, NULL, NULL, 0);
   1241   r += expect_digest ("Digest algorithm=MD5-2", NULL, \
   1242                       MHD_DIGEST_AUTH_ALGO3_INVALID, \
   1243                       NULL, NULL, NULL, NULL, NULL, \
   1244                       NULL, MHD_DIGEST_AUTH_QOP_NONE, NULL, NULL, 0);
   1245   r += expect_digest ("Digest algorithm=MD5-sess2", NULL, \
   1246                       MHD_DIGEST_AUTH_ALGO3_INVALID, \
   1247                       NULL, NULL, NULL, NULL, NULL, \
   1248                       NULL, MHD_DIGEST_AUTH_QOP_NONE, NULL, NULL, 0);
   1249   r += expect_digest ("Digest algorithm=SHA-256-512", NULL, \
   1250                       MHD_DIGEST_AUTH_ALGO3_INVALID, \
   1251                       NULL, NULL, NULL, NULL, NULL, \
   1252                       NULL, MHD_DIGEST_AUTH_QOP_NONE, NULL, NULL, 0);
   1253   r += expect_digest ("Digest algorithm=", NULL, \
   1254                       MHD_DIGEST_AUTH_ALGO3_INVALID, \
   1255                       NULL, NULL, NULL, NULL, NULL, \
   1256                       NULL, MHD_DIGEST_AUTH_QOP_NONE, NULL, NULL, 0);
   1257 
   1258   r += expect_digest ("Digest qop=auth", NULL, \
   1259                       MHD_DIGEST_AUTH_ALGO3_MD5, \
   1260                       NULL, NULL, NULL, NULL, NULL, \
   1261                       "auth", MHD_DIGEST_AUTH_QOP_AUTH, NULL, NULL, 0);
   1262   r += expect_digest ("Digest qop=\"auth\"", NULL, \
   1263                       MHD_DIGEST_AUTH_ALGO3_MD5, \
   1264                       NULL, NULL, NULL, NULL, NULL, \
   1265                       "auth", MHD_DIGEST_AUTH_QOP_AUTH, NULL, NULL, 0);
   1266   r += expect_digest ("Digest qop=Auth", NULL, \
   1267                       MHD_DIGEST_AUTH_ALGO3_MD5, \
   1268                       NULL, NULL, NULL, NULL, NULL, \
   1269                       "Auth", MHD_DIGEST_AUTH_QOP_AUTH, NULL, NULL, 0);
   1270   r += expect_digest ("Digest qop=AUTH", NULL, \
   1271                       MHD_DIGEST_AUTH_ALGO3_MD5, \
   1272                       NULL, NULL, NULL, NULL, NULL, \
   1273                       "AUTH", MHD_DIGEST_AUTH_QOP_AUTH, NULL, NULL, 0);
   1274   r += expect_digest ("Digest qop=\"\\A\\ut\\H\"", NULL, \
   1275                       MHD_DIGEST_AUTH_ALGO3_MD5, \
   1276                       NULL, NULL, NULL, NULL, NULL, \
   1277                       "\\A\\ut\\H", MHD_DIGEST_AUTH_QOP_AUTH, NULL, NULL, 0);
   1278   r += expect_digest ("Digest qop=\"auth \"", NULL, \
   1279                       MHD_DIGEST_AUTH_ALGO3_MD5, \
   1280                       NULL, NULL, NULL, NULL, NULL, \
   1281                       "auth ", MHD_DIGEST_AUTH_QOP_INVALID, NULL, NULL, 0);
   1282   r += expect_digest ("Digest qop=auth-int", NULL, \
   1283                       MHD_DIGEST_AUTH_ALGO3_MD5, \
   1284                       NULL, NULL, NULL, NULL, NULL, \
   1285                       "auth-int", MHD_DIGEST_AUTH_QOP_AUTH_INT, NULL, NULL, 0);
   1286   r += expect_digest ("Digest qop=\"auth-int\"", NULL, \
   1287                       MHD_DIGEST_AUTH_ALGO3_MD5, \
   1288                       NULL, NULL, NULL, NULL, NULL, \
   1289                       "auth-int", MHD_DIGEST_AUTH_QOP_AUTH_INT, NULL, NULL, 0);
   1290   r += expect_digest ("Digest qop=\"auTh-iNt\"", NULL, \
   1291                       MHD_DIGEST_AUTH_ALGO3_MD5, \
   1292                       NULL, NULL, NULL, NULL, NULL, \
   1293                       "auTh-iNt", MHD_DIGEST_AUTH_QOP_AUTH_INT, NULL, NULL, 0);
   1294   r += expect_digest ("Digest qop=\"auTh-iNt2\"", NULL, \
   1295                       MHD_DIGEST_AUTH_ALGO3_MD5, \
   1296                       NULL, NULL, NULL, NULL, NULL, \
   1297                       "auTh-iNt2", MHD_DIGEST_AUTH_QOP_INVALID, NULL, NULL, 0);
   1298 
   1299   r += expect_digest ("Digest username=\"test@example.com\", " \
   1300                       "realm=\"users@example.com\", " \
   1301                       "nonce=\"32141232413abcde\", " \
   1302                       "uri=\"/example\", qop=auth, nc=00000001, " \
   1303                       "cnonce=\"0a4f113b\", " \
   1304                       "response=\"6629fae49393a05397450978507c4ef1\", " \
   1305                       "opaque=\"sadfljk32sdaf\"", "32141232413abcde", \
   1306                       MHD_DIGEST_AUTH_ALGO3_MD5, \
   1307                       "6629fae49393a05397450978507c4ef1", "test@example.com", \
   1308                       NULL, "users@example.com", "/example", \
   1309                       "auth", MHD_DIGEST_AUTH_QOP_AUTH, \
   1310                       "0a4f113b", "00000001", 0);
   1311   r += expect_digest ("Digest username=\"test@example.com\", " \
   1312                       "realm=\"users@example.com\", algorithm=SHA-256, " \
   1313                       "nonce=\"32141232413abcde\", " \
   1314                       "username*=UTF-8''%c2%a3%20and%20%e2%82%ac%20rates, " \
   1315                       "uri=\"/example\", qop=auth, nc=00000001, " \
   1316                       "cnonce=\"0a4f113b\", " \
   1317                       "response=\"6629fae49393a05397450978507c4ef1\", " \
   1318                       "opaque=\"sadfljk32sdaf\"", "32141232413abcde", \
   1319                       MHD_DIGEST_AUTH_ALGO3_SHA256, \
   1320                       "6629fae49393a05397450978507c4ef1", "test@example.com", \
   1321                       "UTF-8''%c2%a3%20and%20%e2%82%ac%20rates", \
   1322                       "users@example.com", "/example", \
   1323                       "auth", MHD_DIGEST_AUTH_QOP_AUTH, "0a4f113b", \
   1324                       "00000001", 0);
   1325   r += expect_digest ("Digest username=test@example.com, " \
   1326                       "realm=users@example.com, algorithm=\"SHA-256-sess\", " \
   1327                       "nonce=32141232413abcde, " \
   1328                       "username*=UTF-8''%c2%a3%20and%20%e2%82%ac%20rates, " \
   1329                       "uri=/example, qop=\"auth\", nc=\"00000001\", " \
   1330                       "cnonce=0a4f113b, " \
   1331                       "response=6629fae49393a05397450978507c4ef1, " \
   1332                       "opaque=sadfljk32sdaf", "32141232413abcde", \
   1333                       MHD_DIGEST_AUTH_ALGO3_SHA256_SESSION, \
   1334                       "6629fae49393a05397450978507c4ef1", "test@example.com", \
   1335                       "UTF-8''%c2%a3%20and%20%e2%82%ac%20rates", \
   1336                       "users@example.com", "/example", \
   1337                       "auth", MHD_DIGEST_AUTH_QOP_AUTH, "0a4f113b", \
   1338                       "00000001", 0);
   1339   r += expect_digest ("Digest username = \"test@example.com\", " \
   1340                       "realm\t=\t\"users@example.com\", algorithm\t= SHA-256, " \
   1341                       "nonce\t= \"32141232413abcde\", " \
   1342                       "username*\t=\tUTF-8''%c2%a3%20and%20%e2%82%ac%20rates, " \
   1343                       "uri = \"/example\", qop = auth, nc\t=\t00000001, " \
   1344                       "cnonce\t\t\t=   \"0a4f113b\", " \
   1345                       "response  =\"6629fae49393a05397450978507c4ef1\", " \
   1346                       "opaque=\t\t\"sadfljk32sdaf\"", "32141232413abcde", \
   1347                       MHD_DIGEST_AUTH_ALGO3_SHA256, \
   1348                       "6629fae49393a05397450978507c4ef1", "test@example.com", \
   1349                       "UTF-8''%c2%a3%20and%20%e2%82%ac%20rates", \
   1350                       "users@example.com", "/example", \
   1351                       "auth", MHD_DIGEST_AUTH_QOP_AUTH, "0a4f113b", \
   1352                       "00000001", 0);
   1353   r += expect_digest ("Digest username=\"test@example.com\"," \
   1354                       "realm=\"users@example.com\",algorithm=SHA-512-256," \
   1355                       "nonce=\"32141232413abcde\"," \
   1356                       "username*=UTF-8''%c2%a3%20and%20%e2%82%ac%20rates," \
   1357                       "uri=\"/example\",qop=auth,nc=00000001," \
   1358                       "cnonce=\"0a4f113b\"," \
   1359                       "response=\"6629fae49393a05397450978507c4ef1\"," \
   1360                       "opaque=\"sadfljk32sdaf\"", "32141232413abcde", \
   1361                       MHD_DIGEST_AUTH_ALGO3_SHA512_256, \
   1362                       "6629fae49393a05397450978507c4ef1", "test@example.com", \
   1363                       "UTF-8''%c2%a3%20and%20%e2%82%ac%20rates", \
   1364                       "users@example.com", "/example", \
   1365                       "auth", MHD_DIGEST_AUTH_QOP_AUTH, "0a4f113b", \
   1366                       "00000001", 0);
   1367   r += expect_digest ("Digest username=\"test@example.com\"," \
   1368                       "realm=\"users@example.com\",algorithm=SHA-256," \
   1369                       "nonce=\"32141232413abcde\",asdf=asdffdsaf," \
   1370                       "username*=UTF-8''%c2%a3%20and%20%e2%82%ac%20rates," \
   1371                       "uri=\"/example\",qop=auth,nc=00000001,cnonce=\"0a4f113b\"," \
   1372                       "response=\"6629fae49393a05397450978507c4ef1\"," \
   1373                       "opaque=\"sadfljk32sdaf\"", "32141232413abcde", \
   1374                       MHD_DIGEST_AUTH_ALGO3_SHA256, \
   1375                       "6629fae49393a05397450978507c4ef1", "test@example.com", \
   1376                       "UTF-8''%c2%a3%20and%20%e2%82%ac%20rates", \
   1377                       "users@example.com", "/example", \
   1378                       "auth", MHD_DIGEST_AUTH_QOP_AUTH, "0a4f113b", \
   1379                       "00000001", 0);
   1380   r += expect_digest ("Digest abc=zyx, username=\"test@example.com\", " \
   1381                       "realm=\"users@example.com\", algorithm=SHA-256, " \
   1382                       "nonce=\"32141232413abcde\", " \
   1383                       "username*=UTF-8''%c2%a3%20and%20%e2%82%ac%20rates, " \
   1384                       "uri=\"/example\", qop=auth, nc=00000001, " \
   1385                       "cnonce=\"0a4f113b\", " \
   1386                       "response=\"6629fae49393a05397450978507c4ef1\", " \
   1387                       "opaque=\"sadfljk32sdaf\"", "32141232413abcde", \
   1388                       MHD_DIGEST_AUTH_ALGO3_SHA256, \
   1389                       "6629fae49393a05397450978507c4ef1", "test@example.com", \
   1390                       "UTF-8''%c2%a3%20and%20%e2%82%ac%20rates", \
   1391                       "users@example.com", "/example", \
   1392                       "auth", MHD_DIGEST_AUTH_QOP_AUTH, "0a4f113b", \
   1393                       "00000001", 0);
   1394   r += expect_digest ("Digest abc=zyx,,,,,,,username=\"test@example.com\", " \
   1395                       "realm=\"users@example.com\", algorithm=SHA-256, " \
   1396                       "nonce=\"32141232413abcde\", " \
   1397                       "username*=UTF-8''%c2%a3%20and%20%e2%82%ac%20rates, " \
   1398                       "uri=\"/example\", qop=auth, nc=00000001, " \
   1399                       "cnonce=\"0a4f113b\", " \
   1400                       "response=\"6629fae49393a05397450978507c4ef1\", " \
   1401                       "opaque=\"sadfljk32sdaf\"", "32141232413abcde",
   1402                       MHD_DIGEST_AUTH_ALGO3_SHA256, \
   1403                       "6629fae49393a05397450978507c4ef1", "test@example.com", \
   1404                       "UTF-8''%c2%a3%20and%20%e2%82%ac%20rates", \
   1405                       "users@example.com", "/example", \
   1406                       "auth", MHD_DIGEST_AUTH_QOP_AUTH, "0a4f113b", \
   1407                       "00000001", 0);
   1408   r += expect_digest ("Digest abc=zyx,,,,,,,username=\"test@example.com\", " \
   1409                       "realm=\"users@example.com\", algorithm=SHA-256, " \
   1410                       "nonce=\"32141232413abcde\", " \
   1411                       "username*=UTF-8''%c2%a3%20and%20%e2%82%ac%20rates, " \
   1412                       "uri=\"/example\", qop=auth, nc=00000001, "
   1413                       "cnonce=\"0a4f113b\", " \
   1414                       "response=\"6629fae49393a05397450978507c4ef1\", " \
   1415                       "opaque=\"sadfljk32sdaf\",,,,,", "32141232413abcde", \
   1416                       MHD_DIGEST_AUTH_ALGO3_SHA256, \
   1417                       "6629fae49393a05397450978507c4ef1", "test@example.com", \
   1418                       "UTF-8''%c2%a3%20and%20%e2%82%ac%20rates", \
   1419                       "users@example.com", "/example", \
   1420                       "auth", MHD_DIGEST_AUTH_QOP_AUTH, "0a4f113b", \
   1421                       "00000001", 0);
   1422   r += expect_digest ("Digest abc=zyx,,,,,,,username=\"test@example.com\", " \
   1423                       "realm=\"users@example.com\", algorithm=SHA-256, " \
   1424                       "nonce=\"32141232413abcde\", " \
   1425                       "username*=UTF-8''%c2%a3%20and%20%e2%82%ac%20rates, " \
   1426                       "uri=\"/example\", qop=auth, nc=00000001, " \
   1427                       "cnonce=\"0a4f113b\", " \
   1428                       "response=\"6629fae49393a05397450978507c4ef1\", " \
   1429                       "opaque=\"sadfljk32sdaf\",foo=bar", "32141232413abcde", \
   1430                       MHD_DIGEST_AUTH_ALGO3_SHA256, \
   1431                       "6629fae49393a05397450978507c4ef1", "test@example.com", \
   1432                       "UTF-8''%c2%a3%20and%20%e2%82%ac%20rates", \
   1433                       "users@example.com", "/example", \
   1434                       "auth", MHD_DIGEST_AUTH_QOP_AUTH, "0a4f113b", \
   1435                       "00000001", 0);
   1436   r += expect_digest ("Digest abc=\"zyx\", username=\"test@example.com\", " \
   1437                       "realm=\"users@example.com\", algorithm=SHA-256, " \
   1438                       "nonce=\"32141232413abcde\", " \
   1439                       "username*=UTF-8''%c2%a3%20and%20%e2%82%ac%20rates, " \
   1440                       "uri=\"/example\", qop=auth, nc=00000001, "
   1441                       "cnonce=\"0a4f113b\", " \
   1442                       "response=\"6629fae49393a05397450978507c4ef1\", " \
   1443                       "opaque=\"sadfljk32sdaf\",foo=bar", "32141232413abcde", \
   1444                       MHD_DIGEST_AUTH_ALGO3_SHA256, \
   1445                       "6629fae49393a05397450978507c4ef1", "test@example.com", \
   1446                       "UTF-8''%c2%a3%20and%20%e2%82%ac%20rates", \
   1447                       "users@example.com", "/example", \
   1448                       "auth", MHD_DIGEST_AUTH_QOP_AUTH, "0a4f113b", \
   1449                       "00000001", 0);
   1450   r += expect_digest ("Digest abc=\"zyx, abc\", " \
   1451                       "username=\"test@example.com\", " \
   1452                       "realm=\"users@example.com\", algorithm=SHA-256, " \
   1453                       "nonce=\"32141232413abcde\", " \
   1454                       "username*=UTF-8''%c2%a3%20and%20%e2%82%ac%20rates, " \
   1455                       "uri=\"/example\", qop=auth, nc=00000001, "
   1456                       "cnonce=\"0a4f113b\", " \
   1457                       "response=\"6629fae49393a05397450978507c4ef1\", " \
   1458                       "opaque=\"sadfljk32sdaf\",foo=bar", "32141232413abcde", \
   1459                       MHD_DIGEST_AUTH_ALGO3_SHA256, \
   1460                       "6629fae49393a05397450978507c4ef1", "test@example.com", \
   1461                       "UTF-8''%c2%a3%20and%20%e2%82%ac%20rates", \
   1462                       "users@example.com", "/example", \
   1463                       "auth", MHD_DIGEST_AUTH_QOP_AUTH, "0a4f113b", \
   1464                       "00000001", 0);
   1465   r += expect_digest ("Digest abc=\"zyx, abc=cde\", " \
   1466                       "username=\"test@example.com\", " \
   1467                       "realm=\"users@example.com\", algorithm=SHA-256, " \
   1468                       "nonce=\"32141232413abcde\", " \
   1469                       "username*=UTF-8''%c2%a3%20and%20%e2%82%ac%20rates, " \
   1470                       "uri=\"/example\", qop=auth, nc=00000001, " \
   1471                       "cnonce=\"0a4f113b\", " \
   1472                       "response=\"6629fae49393a05397450978507c4ef1\", " \
   1473                       "opaque=\"sadfljk32sdaf\",foo=bar", "32141232413abcde", \
   1474                       MHD_DIGEST_AUTH_ALGO3_SHA256, \
   1475                       "6629fae49393a05397450978507c4ef1", "test@example.com", \
   1476                       "UTF-8''%c2%a3%20and%20%e2%82%ac%20rates", \
   1477                       "users@example.com", "/example", \
   1478                       "auth", MHD_DIGEST_AUTH_QOP_AUTH, "0a4f113b", \
   1479                       "00000001", 0);
   1480   r += expect_digest ("Digest abc=\"zyx, abc=cde\", " \
   1481                       "username=\"test@example.com\", " \
   1482                       "realm=\"users@example.com\", algorithm=SHA-256, " \
   1483                       "nonce=\"32141232413abcde\", " \
   1484                       "username*=UTF-8''%c2%a3%20and%20%e2%82%ac%20rates, " \
   1485                       "uri=\"/example\", qop=auth, nc=00000001, " \
   1486                       "cnonce=\"0a4f113b\", " \
   1487                       "response=\"6629fae49393a05397450978507c4ef1\", " \
   1488                       "opaque=\"sadfljk32sdaf\", foo=\"bar1, bar2\"", \
   1489                       "32141232413abcde", \
   1490                       MHD_DIGEST_AUTH_ALGO3_SHA256, \
   1491                       "6629fae49393a05397450978507c4ef1", "test@example.com", \
   1492                       "UTF-8''%c2%a3%20and%20%e2%82%ac%20rates", \
   1493                       "users@example.com", "/example", \
   1494                       "auth", MHD_DIGEST_AUTH_QOP_AUTH, "0a4f113b", \
   1495                       "00000001", 0);
   1496   r += expect_digest ("Digest abc=\"zyx, \\\\\"abc=cde\\\\\"\", " \
   1497                       "username=\"test@example.com\", " \
   1498                       "realm=\"users@example.com\", algorithm=SHA-256, " \
   1499                       "nonce=\"32141232413abcde\", " \
   1500                       "username*=UTF-8''%c2%a3%20and%20%e2%82%ac%20rates, " \
   1501                       "uri=\"/example\", qop=auth, nc=00000001, cnonce=\"0a4f113b\", " \
   1502                       "response=\"6629fae49393a05397450978507c4ef1\", " \
   1503                       "opaque=\"sadfljk32sdaf\", foo=\"bar1, bar2\"", \
   1504                       "32141232413abcde",
   1505                       MHD_DIGEST_AUTH_ALGO3_SHA256, \
   1506                       "6629fae49393a05397450978507c4ef1", "test@example.com", \
   1507                       "UTF-8''%c2%a3%20and%20%e2%82%ac%20rates", \
   1508                       "users@example.com", "/example", \
   1509                       "auth", MHD_DIGEST_AUTH_QOP_AUTH, "0a4f113b", \
   1510                       "00000001", 0);
   1511   r += expect_digest ("Digest abc=\"zyx, \\\\\"abc=cde\\\\\"\", " \
   1512                       "username=\"test@example.com\", " \
   1513                       "realm=\"users@example.com\", algorithm=SHA-256, " \
   1514                       "nonce=\"32141232413abcde\", " \
   1515                       "username*=UTF-8''%c2%a3%20and%20%e2%82%ac%20rates, " \
   1516                       "uri=\"/example\", qop=auth, nc=00000001, "
   1517                       "cnonce=\"0a4f113b\", " \
   1518                       "response=\"6629fae49393a05397450978507c4ef1\", " \
   1519                       "opaque=\"sadfljk32sdaf\", foo=\",nc=02\"",
   1520                       "32141232413abcde", \
   1521                       MHD_DIGEST_AUTH_ALGO3_SHA256, \
   1522                       "6629fae49393a05397450978507c4ef1", "test@example.com", \
   1523                       "UTF-8''%c2%a3%20and%20%e2%82%ac%20rates", \
   1524                       "users@example.com", "/example", \
   1525                       "auth", MHD_DIGEST_AUTH_QOP_AUTH, "0a4f113b", \
   1526                       "00000001", 0);
   1527 
   1528   return r;
   1529 }
   1530 
   1531 
   1532 #endif /* DAUTH_SUPPORT */
   1533 
   1534 #define TEST_AUTH_STR "dXNlcjpwYXNz"
   1535 
   1536 static unsigned int
   1537 check_two_auths (void)
   1538 {
   1539   unsigned int ret;
   1540   static struct MHD_HTTP_Req_Header h1;
   1541   static struct MHD_HTTP_Req_Header h2;
   1542   static struct MHD_HTTP_Req_Header h3;
   1543 #ifdef BAUTH_SUPPORT
   1544   const struct MHD_RqBAuth *bauth;
   1545 #endif /* BAUTH_SUPPORT */
   1546 #ifdef DAUTH_SUPPORT
   1547   const struct MHD_RqDAuth *dauth;
   1548 #endif /* DAUTH_SUPPORT */
   1549 
   1550   if ((NULL != conn.rq.headers_received) ||
   1551       (NULL != conn.rq.headers_received_tail))
   1552     externalErrorExitDesc ("Connection's test headers are not empty already");
   1553 
   1554   /* Init and use both Basic and Digest Auth headers */
   1555   memset (&h1, 0, sizeof(h1));
   1556   memset (&h2, 0, sizeof(h2));
   1557   memset (&h3, 0, sizeof(h3));
   1558 
   1559   h1.kind = MHD_HEADER_KIND;
   1560   h1.header = MHD_HTTP_HEADER_HOST; /* Just some random header */
   1561   h1.header_size = MHD_STATICSTR_LEN_ (MHD_HTTP_HEADER_HOST);
   1562   h1.value = "localhost";
   1563   h1.value_size = strlen (h1.value);
   1564 
   1565   h2.kind = MHD_HEADER_KIND;
   1566   h2.header = MHD_HTTP_HEADER_AUTHORIZATION;
   1567   h2.header_size = MHD_STATICSTR_LEN_ (MHD_HTTP_HEADER_AUTHORIZATION);
   1568   h2.value = "Basic " TEST_AUTH_STR;
   1569   h2.value_size = strlen (h2.value);
   1570 
   1571   h3.kind = MHD_HEADER_KIND;
   1572   h3.header = MHD_HTTP_HEADER_AUTHORIZATION;
   1573   h3.header_size = MHD_STATICSTR_LEN_ (MHD_HTTP_HEADER_AUTHORIZATION);
   1574   h3.value = "Digest cnonce=" TEST_AUTH_STR;
   1575   h3.value_size = strlen (h3.value);
   1576 
   1577   conn.rq.headers_received = &h1;
   1578   h1.next = &h2;
   1579   h2.prev = &h1;
   1580   h2.next = &h3;
   1581   h3.prev = &h2;
   1582   conn.rq.headers_received_tail = &h3;
   1583 
   1584   conn.state = MHD_CONNECTION_FULL_REQ_RECEIVED; /* Should be a typical value */
   1585 
   1586   ret = 0;
   1587 #ifdef BAUTH_SUPPORT
   1588   bauth = get_BAuthRqParams ();
   1589 #endif /* BAUTH_SUPPORT */
   1590 #ifdef DAUTH_SUPPORT
   1591   dauth = get_DAuthRqParams ();
   1592 #endif /* DAUTH_SUPPORT */
   1593 #ifdef BAUTH_SUPPORT
   1594   if (NULL == bauth)
   1595   {
   1596     fprintf (stderr, "No Basic Authorization header detected. Line: %u\n",
   1597              (unsigned int) __LINE__);
   1598     ret++;
   1599   }
   1600   else if ((MHD_STATICSTR_LEN_ (TEST_AUTH_STR) != bauth->token68.len) ||
   1601            (0 != memcmp (bauth->token68.str, TEST_AUTH_STR,
   1602                          bauth->token68.len)))
   1603   {
   1604     fprintf (stderr, "Basic Authorization token does not match. Line: %u\n",
   1605              (unsigned int) __LINE__);
   1606     ret++;
   1607   }
   1608 #endif /* BAUTH_SUPPORT */
   1609 #ifdef DAUTH_SUPPORT
   1610   if (NULL == dauth)
   1611   {
   1612     fprintf (stderr, "No Digest Authorization header detected. Line: %u\n",
   1613              (unsigned int) __LINE__);
   1614     ret++;
   1615   }
   1616   else if ((MHD_STATICSTR_LEN_ (TEST_AUTH_STR) != dauth->cnonce.value.len) ||
   1617            (0 != memcmp (dauth->cnonce.value.str, TEST_AUTH_STR,
   1618                          dauth->cnonce.value.len)))
   1619   {
   1620     fprintf (stderr, "Digest Authorization 'cnonce' does not match. Line: %u\n",
   1621              (unsigned int) __LINE__);
   1622     ret++;
   1623   }
   1624 #endif /* DAUTH_SUPPORT */
   1625 
   1626   /* Cleanup */
   1627   conn.rq.headers_received = NULL;
   1628   conn.rq.headers_received_tail = NULL;
   1629   conn.state = MHD_CONNECTION_INIT;
   1630 
   1631   return ret;
   1632 }
   1633 
   1634 
   1635 int
   1636 main (int argc, char *argv[])
   1637 {
   1638   unsigned int errcount = 0;
   1639   (void) argc; (void) argv; /* Unused. Silent compiler warning. */
   1640   test_global_init ();
   1641 
   1642   errcount += check_type ();
   1643 #ifdef BAUTH_SUPPORT
   1644   errcount += check_basic ();
   1645 #endif /* BAUTH_SUPPORT */
   1646 #ifdef DAUTH_SUPPORT
   1647   errcount += check_digest ();
   1648 #endif /* DAUTH_SUPPORT */
   1649   errcount += check_two_auths ();
   1650   if (0 == errcount)
   1651     printf ("All tests were passed without errors.\n");
   1652   return errcount == 0 ? 0 : 1;
   1653 }