libmicrohttpd

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

test_digestauth_emu_ext.c (28924B)


      1 /*
      2      This file is part of libmicrohttpd
      3      Copyright (C) 2010 Christian Grothoff
      4      Copyright (C) 2016-2022 Evgeny Grin (Karlson2k)
      5 
      6      libmicrohttpd is free software; you can redistribute it and/or modify
      7      it under the terms of the GNU General Public License as published
      8      by the Free Software Foundation; either version 2, or (at your
      9      option) any later version.
     10 
     11      libmicrohttpd is distributed in the hope that it will be useful, but
     12      WITHOUT ANY WARRANTY; without even the implied warranty of
     13      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     14      General Public License for more details.
     15 
     16      You should have received a copy of the GNU General Public License
     17      along with libmicrohttpd; see the file COPYING.  If not, write to the
     18      Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
     19      Boston, MA 02110-1301, USA.
     20 */
     21 
     22 /**
     23  * @file test_digest_emu_ext.c
     24  * @brief  Testcase for MHD Digest Authorisation client's header parsing
     25  * @author Karlson2k (Evgeny Grin)
     26  *
     27  * libcurl does not support extended notation for username, so this test
     28  * "emulates" client request will all valid fields except nonce, cnonce and
     29  * response (however syntactically these fields valid as well).
     30  */
     31 
     32 #include "MHD_config.h"
     33 #include "platform.h"
     34 #include <curl/curl.h>
     35 #include <microhttpd.h>
     36 #include <stdlib.h>
     37 #include <string.h>
     38 #include <time.h>
     39 #include <errno.h>
     40 
     41 #ifndef WINDOWS
     42 #include <sys/socket.h>
     43 #include <unistd.h>
     44 #else
     45 #include <wincrypt.h>
     46 #endif
     47 
     48 #include "mhd_has_param.h"
     49 #include "mhd_has_in_name.h"
     50 
     51 /* Turn any MHD_PANIC() or failing mhd_assert() reached from this
     52    test into a marked, classifiable test error (TESTING.md, P5). */
     53 #include "mhd_panic_tripwire.h"
     54 
     55 #ifndef MHD_STATICSTR_LEN_
     56 /**
     57  * Determine length of static string / macro strings at compile time.
     58  */
     59 #define MHD_STATICSTR_LEN_(macro) (sizeof(macro) / sizeof(char) - 1)
     60 #endif /* ! MHD_STATICSTR_LEN_ */
     61 
     62 #ifndef CURL_VERSION_BITS
     63 #define CURL_VERSION_BITS(x,y,z) ((x) << 16 | (y) << 8 | (z))
     64 #endif /* ! CURL_VERSION_BITS */
     65 #ifndef CURL_AT_LEAST_VERSION
     66 #define CURL_AT_LEAST_VERSION(x,y,z) \
     67   (LIBCURL_VERSION_NUM >= CURL_VERSION_BITS (x, y, z))
     68 #endif /* ! CURL_AT_LEAST_VERSION */
     69 
     70 #ifndef _MHD_INSTRMACRO
     71 /* Quoted macro parameter */
     72 #define _MHD_INSTRMACRO(a) #a
     73 #endif /* ! _MHD_INSTRMACRO */
     74 #ifndef _MHD_STRMACRO
     75 /* Quoted expanded macro parameter */
     76 #define _MHD_STRMACRO(a) _MHD_INSTRMACRO (a)
     77 #endif /* ! _MHD_STRMACRO */
     78 
     79 #if defined(HAVE___FUNC__)
     80 #define externalErrorExit(ignore) \
     81   _externalErrorExit_func (NULL, __func__, __LINE__)
     82 #define externalErrorExitDesc(errDesc) \
     83   _externalErrorExit_func (errDesc, __func__, __LINE__)
     84 #define libcurlErrorExit(ignore) \
     85   _libcurlErrorExit_func (NULL, __func__, __LINE__)
     86 #define libcurlErrorExitDesc(errDesc) \
     87   _libcurlErrorExit_func (errDesc, __func__, __LINE__)
     88 #define mhdErrorExit(ignore) \
     89   _mhdErrorExit_func (NULL, __func__, __LINE__)
     90 #define mhdErrorExitDesc(errDesc) \
     91   _mhdErrorExit_func (errDesc, __func__, __LINE__)
     92 #define checkCURLE_OK(libcurlcall) \
     93   _checkCURLE_OK_func ((libcurlcall), _MHD_STRMACRO (libcurlcall), \
     94                        __func__, __LINE__)
     95 #elif defined(HAVE___FUNCTION__)
     96 #define externalErrorExit(ignore) \
     97   _externalErrorExit_func (NULL, __FUNCTION__, __LINE__)
     98 #define externalErrorExitDesc(errDesc) \
     99   _externalErrorExit_func (errDesc, __FUNCTION__, __LINE__)
    100 #define libcurlErrorExit(ignore) \
    101   _libcurlErrorExit_func (NULL, __FUNCTION__, __LINE__)
    102 #define libcurlErrorExitDesc(errDesc) \
    103   _libcurlErrorExit_func (errDesc, __FUNCTION__, __LINE__)
    104 #define mhdErrorExit(ignore) \
    105   _mhdErrorExit_func (NULL, __FUNCTION__, __LINE__)
    106 #define mhdErrorExitDesc(errDesc) \
    107   _mhdErrorExit_func (errDesc, __FUNCTION__, __LINE__)
    108 #define checkCURLE_OK(libcurlcall) \
    109   _checkCURLE_OK_func ((libcurlcall), _MHD_STRMACRO (libcurlcall), \
    110                        __FUNCTION__, __LINE__)
    111 #else
    112 #define externalErrorExit(ignore) _externalErrorExit_func (NULL, NULL, __LINE__)
    113 #define externalErrorExitDesc(errDesc) \
    114   _externalErrorExit_func (errDesc, NULL, __LINE__)
    115 #define libcurlErrorExit(ignore) _libcurlErrorExit_func (NULL, NULL, __LINE__)
    116 #define libcurlErrorExitDesc(errDesc) \
    117   _libcurlErrorExit_func (errDesc, NULL, __LINE__)
    118 #define mhdErrorExit(ignore) _mhdErrorExit_func (NULL, NULL, __LINE__)
    119 #define mhdErrorExitDesc(errDesc) _mhdErrorExit_func (errDesc, NULL, __LINE__)
    120 #define checkCURLE_OK(libcurlcall) \
    121   _checkCURLE_OK_func ((libcurlcall), _MHD_STRMACRO (libcurlcall), NULL, \
    122                        __LINE__)
    123 #endif
    124 
    125 
    126 _MHD_NORETURN static void
    127 _externalErrorExit_func (const char *errDesc, const char *funcName, int lineNum)
    128 {
    129   fflush (stdout);
    130   if ((NULL != errDesc) && (0 != errDesc[0]))
    131     fprintf (stderr, "%s", errDesc);
    132   else
    133     fprintf (stderr, "System or external library call failed");
    134   if ((NULL != funcName) && (0 != funcName[0]))
    135     fprintf (stderr, " in %s", funcName);
    136   if (0 < lineNum)
    137     fprintf (stderr, " at line %d", lineNum);
    138 
    139   fprintf (stderr, ".\nLast errno value: %d (%s)\n", (int) errno,
    140            strerror (errno));
    141 #ifdef MHD_WINSOCK_SOCKETS
    142   fprintf (stderr, "WSAGetLastError() value: %d\n", (int) WSAGetLastError ());
    143 #endif /* MHD_WINSOCK_SOCKETS */
    144   fflush (stderr);
    145   exit (99);
    146 }
    147 
    148 
    149 /* Not actually used in this test */
    150 static char libcurl_errbuf[CURL_ERROR_SIZE] = "";
    151 
    152 _MHD_NORETURN static void
    153 _libcurlErrorExit_func (const char *errDesc, const char *funcName, int lineNum)
    154 {
    155   fflush (stdout);
    156   if ((NULL != errDesc) && (0 != errDesc[0]))
    157     fprintf (stderr, "%s", errDesc);
    158   else
    159     fprintf (stderr, "CURL library call failed");
    160   if ((NULL != funcName) && (0 != funcName[0]))
    161     fprintf (stderr, " in %s", funcName);
    162   if (0 < lineNum)
    163     fprintf (stderr, " at line %d", lineNum);
    164 
    165   fprintf (stderr, ".\nLast errno value: %d (%s)\n", (int) errno,
    166            strerror (errno));
    167 #ifdef MHD_WINSOCK_SOCKETS
    168   fprintf (stderr, "WSAGetLastError() value: %d\n", (int) WSAGetLastError ());
    169 #endif /* MHD_WINSOCK_SOCKETS */
    170   if (0 != libcurl_errbuf[0])
    171     fprintf (stderr, "Last libcurl error description: %s\n", libcurl_errbuf);
    172 
    173   fflush (stderr);
    174   exit (99);
    175 }
    176 
    177 
    178 _MHD_NORETURN static void
    179 _mhdErrorExit_func (const char *errDesc, const char *funcName, int lineNum)
    180 {
    181   fflush (stdout);
    182   if ((NULL != errDesc) && (0 != errDesc[0]))
    183     fprintf (stderr, "%s", errDesc);
    184   else
    185     fprintf (stderr, "MHD unexpected error");
    186   if ((NULL != funcName) && (0 != funcName[0]))
    187     fprintf (stderr, " in %s", funcName);
    188   if (0 < lineNum)
    189     fprintf (stderr, " at line %d", lineNum);
    190 
    191   fprintf (stderr, ".\nLast errno value: %d (%s)\n", (int) errno,
    192            strerror (errno));
    193 #ifdef MHD_WINSOCK_SOCKETS
    194   fprintf (stderr, "WSAGetLastError() value: %d\n", (int) WSAGetLastError ());
    195 #endif /* MHD_WINSOCK_SOCKETS */
    196 
    197   fflush (stderr);
    198   exit (8);
    199 }
    200 
    201 
    202 #if 0
    203 /* Function unused in this test */
    204 static void
    205 _checkCURLE_OK_func (CURLcode code, const char *curlFunc,
    206                      const char *funcName, int lineNum)
    207 {
    208   if (CURLE_OK == code)
    209     return;
    210 
    211   fflush (stdout);
    212   if ((NULL != curlFunc) && (0 != curlFunc[0]))
    213     fprintf (stderr, "'%s' resulted in '%s'", curlFunc,
    214              curl_easy_strerror (code));
    215   else
    216     fprintf (stderr, "libcurl function call resulted in '%s'",
    217              curl_easy_strerror (code));
    218   if ((NULL != funcName) && (0 != funcName[0]))
    219     fprintf (stderr, " in %s", funcName);
    220   if (0 < lineNum)
    221     fprintf (stderr, " at line %d", lineNum);
    222 
    223   fprintf (stderr, ".\nLast errno value: %d (%s)\n", (int) errno,
    224            strerror (errno));
    225   if (0 != libcurl_errbuf[0])
    226     fprintf (stderr, "Last libcurl error description: %s\n", libcurl_errbuf);
    227 
    228   fflush (stderr);
    229   exit (9);
    230 }
    231 
    232 
    233 #endif
    234 
    235 
    236 /* Could be increased to facilitate debugging */
    237 #define TIMEOUTS_VAL 10
    238 
    239 #define MHD_URI_BASE_PATH "/bar%20foo?key=value"
    240 
    241 #define REALM "TestRealm"
    242 /* "titkos szuperügynök" in UTF-8 */
    243 #define USERNAME "titkos szuper" "\xC3\xBC" "gyn" "\xC3\xB6" "k"
    244 /* percent-encoded username */
    245 #define USERNAME_PCTENC "titkos%20szuper%C3%BCgyn%C3%B6k"
    246 #define PASSWORD_VALUE "fake pass"
    247 #define OPAQUE_VALUE "opaque-content"
    248 #define NONCE_EMU "badbadbadbadbadbadbadbadbadbadbadbadbadbadba"
    249 #define CNONCE_EMU "utututututututututututututututututututututs="
    250 #define RESPONSE_EMU "badbadbadbadbadbadbadbadbadbadba"
    251 
    252 
    253 #define PAGE \
    254   "<html><head><title>libmicrohttpd demo page</title>" \
    255   "</head><body>Access granted</body></html>"
    256 
    257 #define DENIED \
    258   "<html><head><title>libmicrohttpd - Access denied</title>" \
    259   "</head><body>Access denied</body></html>"
    260 
    261 struct CBC
    262 {
    263   char *buf;
    264   size_t pos;
    265   size_t size;
    266 };
    267 
    268 /* Global parameters */
    269 static int verbose;
    270 static int oldapi;
    271 
    272 /* Static helper variables */
    273 static struct curl_slist *curl_headers;
    274 
    275 static void
    276 test_global_init (void)
    277 {
    278   libcurl_errbuf[0] = 0;
    279 
    280   if (0 != curl_global_init (CURL_GLOBAL_WIN32))
    281     externalErrorExit ();
    282 
    283   curl_headers = NULL;
    284   curl_headers =
    285     curl_slist_append (curl_headers,
    286                        "Authorization: "
    287                        "Digest username*=UTF-8''" USERNAME_PCTENC ", "
    288                        "realm=\"" REALM "\", "
    289                        "nonce=\"" NONCE_EMU "\", "
    290                        "uri=\"" MHD_URI_BASE_PATH "\", "
    291                        "cnonce=\"" CNONCE_EMU "\", "
    292                        "nc=00000001, "
    293                        "qop=auth, "
    294                        "response=\"" RESPONSE_EMU "\", "
    295                        "opaque=\"" OPAQUE_VALUE "\", "
    296                        "algorithm=MD5");
    297   if (NULL == curl_headers)
    298     externalErrorExit ();
    299 }
    300 
    301 
    302 static void
    303 test_global_cleanup (void)
    304 {
    305   curl_slist_free_all (curl_headers);
    306   curl_headers = NULL;
    307   curl_global_cleanup ();
    308 }
    309 
    310 
    311 static size_t
    312 copyBuffer (void *ptr,
    313             size_t size,
    314             size_t nmemb,
    315             void *ctx)
    316 {
    317   struct CBC *cbc = ctx;
    318 
    319   if (cbc->pos + size * nmemb > cbc->size)
    320     mhdErrorExitDesc ("Wrong too large data");       /* overflow */
    321   memcpy (&cbc->buf[cbc->pos], ptr, size * nmemb);
    322   cbc->pos += size * nmemb;
    323   return size * nmemb;
    324 }
    325 
    326 
    327 static enum MHD_Result
    328 ahc_echo (void *cls,
    329           struct MHD_Connection *connection,
    330           const char *url,
    331           const char *method,
    332           const char *version,
    333           const char *upload_data,
    334           size_t *upload_data_size,
    335           void **req_cls)
    336 {
    337   struct MHD_Response *response;
    338   enum MHD_Result ret;
    339   static int already_called_marker;
    340   (void) cls; (void) url;         /* Unused. Silent compiler warning. */
    341   (void) method; (void) version; (void) upload_data; /* Unused. Silent compiler warning. */
    342   (void) upload_data_size;        /* Unused. Silent compiler warning. */
    343 
    344   if (&already_called_marker != *req_cls)
    345   { /* Called for the first time, request not fully read yet */
    346     *req_cls = &already_called_marker;
    347     /* Wait for complete request */
    348     return MHD_YES;
    349   }
    350 
    351   if (0 != strcmp (method, MHD_HTTP_METHOD_GET))
    352     mhdErrorExitDesc ("Unexpected HTTP method");
    353 
    354   if (! oldapi)
    355   {
    356     struct MHD_DigestAuthUsernameInfo *creds;
    357     struct MHD_DigestAuthInfo *dinfo;
    358     enum MHD_DigestAuthResult check_res;
    359 
    360     creds = MHD_digest_auth_get_username3 (connection);
    361     if (NULL == creds)
    362       mhdErrorExitDesc ("MHD_digest_auth_get_username3() returned NULL");
    363     else if (MHD_DIGEST_AUTH_UNAME_TYPE_EXTENDED != creds->uname_type)
    364     {
    365       fprintf (stderr, "Unexpected 'uname_type'.\n"
    366                "Expected: %d\tRecieved: %d. ",
    367                (int) MHD_DIGEST_AUTH_UNAME_TYPE_EXTENDED,
    368                (int) creds->uname_type);
    369       mhdErrorExitDesc ("Wrong 'uname_type'");
    370     }
    371     else if (NULL == creds->username)
    372       mhdErrorExitDesc ("'username' is NULL");
    373     else if (creds->username_len != MHD_STATICSTR_LEN_ (USERNAME))
    374     {
    375       fprintf (stderr, "'username_len' does not match.\n"
    376                "Expected: %u\tRecieved: %u. ",
    377                (unsigned) MHD_STATICSTR_LEN_ (USERNAME),
    378                (unsigned) creds->username_len);
    379       mhdErrorExitDesc ("Wrong 'username_len'");
    380     }
    381     else if (0 != memcmp (creds->username, USERNAME, creds->username_len))
    382     {
    383       fprintf (stderr, "'username' does not match.\n"
    384                "Expected: '%s'\tRecieved: '%.*s'. ",
    385                USERNAME,
    386                (int) creds->username_len,
    387                creds->username);
    388       mhdErrorExitDesc ("Wrong 'username'");
    389     }
    390     else if (NULL != creds->userhash_hex)
    391       mhdErrorExitDesc ("'userhash_hex' is NOT NULL");
    392     else if (0 != creds->userhash_hex_len)
    393       mhdErrorExitDesc ("'userhash_hex' is NOT zero");
    394     else if (NULL != creds->userhash_bin)
    395       mhdErrorExitDesc ("'userhash_bin' is NOT NULL");
    396 
    397     dinfo = MHD_digest_auth_get_request_info3 (connection);
    398     if (NULL == dinfo)
    399       mhdErrorExitDesc ("MHD_digest_auth_get_username3() returned NULL");
    400     else if (MHD_DIGEST_AUTH_UNAME_TYPE_EXTENDED != dinfo->uname_type)
    401     {
    402       fprintf (stderr, "Unexpected 'uname_type'.\n"
    403                "Expected: %d\tRecieved: %d. ",
    404                (int) MHD_DIGEST_AUTH_UNAME_TYPE_EXTENDED,
    405                (int) creds->uname_type);
    406       mhdErrorExitDesc ("Wrong 'uname_type'");
    407     }
    408     else if (NULL == dinfo->username)
    409       mhdErrorExitDesc ("'username' is NULL");
    410     else if (dinfo->username_len != MHD_STATICSTR_LEN_ (USERNAME))
    411     {
    412       fprintf (stderr, "'username_len' does not match.\n"
    413                "Expected: %u\tRecieved: %u. ",
    414                (unsigned) MHD_STATICSTR_LEN_ (USERNAME),
    415                (unsigned) dinfo->username_len);
    416       mhdErrorExitDesc ("Wrong 'username_len'");
    417     }
    418     else if (0 != memcmp (dinfo->username, USERNAME, dinfo->username_len))
    419     {
    420       fprintf (stderr, "'username' does not match.\n"
    421                "Expected: '%s'\tRecieved: '%.*s'. ",
    422                USERNAME,
    423                (int) dinfo->username_len,
    424                dinfo->username);
    425       mhdErrorExitDesc ("Wrong 'username'");
    426     }
    427     else if (NULL != dinfo->userhash_hex)
    428       mhdErrorExitDesc ("'userhash_hex' is NOT NULL");
    429     else if (0 != dinfo->userhash_hex_len)
    430       mhdErrorExitDesc ("'userhash_hex' is NOT zero");
    431     else if (NULL != dinfo->userhash_bin)
    432       mhdErrorExitDesc ("'userhash_bin' is NOT NULL");
    433     else if (MHD_DIGEST_AUTH_ALGO3_MD5 != dinfo->algo3)
    434     {
    435       fprintf (stderr, "Unexpected 'algo'.\n"
    436                "Expected: %d\tRecieved: %d. ",
    437                (int) MHD_DIGEST_AUTH_ALGO3_MD5,
    438                (int) dinfo->algo3);
    439       mhdErrorExitDesc ("Wrong 'algo'");
    440     }
    441     else if (MHD_STATICSTR_LEN_ (CNONCE_EMU) != dinfo->cnonce_len)
    442     {
    443       fprintf (stderr, "Unexpected 'cnonce_len'.\n"
    444                "Expected: %d\tRecieved: %ld. ",
    445                (int) MHD_STATICSTR_LEN_ (CNONCE_EMU),
    446                (long) dinfo->cnonce_len);
    447       mhdErrorExitDesc ("Wrong 'cnonce_len'");
    448     }
    449     else if (NULL == dinfo->opaque)
    450       mhdErrorExitDesc ("'opaque' is NULL");
    451     else if (dinfo->opaque_len != MHD_STATICSTR_LEN_ (OPAQUE_VALUE))
    452     {
    453       fprintf (stderr, "'opaque_len' does not match.\n"
    454                "Expected: %u\tRecieved: %u. ",
    455                (unsigned) MHD_STATICSTR_LEN_ (OPAQUE_VALUE),
    456                (unsigned) dinfo->opaque_len);
    457       mhdErrorExitDesc ("Wrong 'opaque_len'");
    458     }
    459     else if (0 != memcmp (dinfo->opaque, OPAQUE_VALUE, dinfo->opaque_len))
    460     {
    461       fprintf (stderr, "'opaque' does not match.\n"
    462                "Expected: '%s'\tRecieved: '%.*s'. ",
    463                OPAQUE_VALUE,
    464                (int) dinfo->opaque_len,
    465                dinfo->opaque);
    466       mhdErrorExitDesc ("Wrong 'opaque'");
    467     }
    468     else if (MHD_DIGEST_AUTH_QOP_AUTH != dinfo->qop)
    469     {
    470       fprintf (stderr, "Unexpected 'qop'.\n"
    471                "Expected: %d\tRecieved: %d. ",
    472                (int) MHD_DIGEST_AUTH_QOP_AUTH,
    473                (int) dinfo->qop);
    474       mhdErrorExitDesc ("Wrong 'qop'");
    475     }
    476     else if (NULL == dinfo->realm)
    477       mhdErrorExitDesc ("'realm' is NULL");
    478     else if (dinfo->realm_len != MHD_STATICSTR_LEN_ (REALM))
    479     {
    480       fprintf (stderr, "'realm_len' does not match.\n"
    481                "Expected: %u\tRecieved: %u. ",
    482                (unsigned) MHD_STATICSTR_LEN_ (REALM),
    483                (unsigned) dinfo->realm_len);
    484       mhdErrorExitDesc ("Wrong 'realm_len'");
    485     }
    486     else if (0 != memcmp (dinfo->realm, REALM, dinfo->realm_len))
    487     {
    488       fprintf (stderr, "'realm' does not match.\n"
    489                "Expected: '%s'\tRecieved: '%.*s'. ",
    490                OPAQUE_VALUE,
    491                (int) dinfo->realm_len,
    492                dinfo->realm);
    493       mhdErrorExitDesc ("Wrong 'realm'");
    494     }
    495     MHD_free (creds);
    496     MHD_free (dinfo);
    497 
    498     check_res = MHD_digest_auth_check3 (connection, REALM, USERNAME,
    499                                         PASSWORD_VALUE,
    500                                         50 * TIMEOUTS_VAL,
    501                                         0, MHD_DIGEST_AUTH_MULT_QOP_AUTH,
    502                                         MHD_DIGEST_AUTH_MULT_ALGO3_MD5);
    503 
    504     switch (check_res)
    505     {
    506     /* Valid results */
    507     case MHD_DAUTH_NONCE_STALE:
    508       if (verbose)
    509         printf ("Got valid auth check result: MHD_DAUTH_NONCE_STALE.\n");
    510       break;
    511     case MHD_DAUTH_NONCE_WRONG:
    512       if (verbose)
    513         printf ("Got valid auth check result: MHD_DAUTH_NONCE_WRONG.\n");
    514       break;
    515 
    516     /* Invalid results */
    517     case MHD_DAUTH_OK:
    518       mhdErrorExitDesc ("'MHD_digest_auth_check3()' succeed, " \
    519                         "but it should not");
    520       break;
    521     case MHD_DAUTH_ERROR:
    522       externalErrorExitDesc ("General error returned " \
    523                              "by 'MHD_digest_auth_check3()'");
    524       break;
    525     case MHD_DAUTH_WRONG_USERNAME:
    526       mhdErrorExitDesc ("MHD_digest_auth_check3()' returned " \
    527                         "MHD_DAUTH_WRONG_USERNAME");
    528       break;
    529     case MHD_DAUTH_RESPONSE_WRONG:
    530       mhdErrorExitDesc ("MHD_digest_auth_check3()' returned " \
    531                         "MHD_DAUTH_RESPONSE_WRONG");
    532       break;
    533     case MHD_DAUTH_WRONG_HEADER:
    534     case MHD_DAUTH_WRONG_REALM:
    535     case MHD_DAUTH_WRONG_URI:
    536     case MHD_DAUTH_WRONG_QOP:
    537     case MHD_DAUTH_WRONG_ALGO:
    538     case MHD_DAUTH_TOO_LARGE:
    539     case MHD_DAUTH_NONCE_OTHER_COND:
    540       fprintf (stderr, "'MHD_digest_auth_check3()' returned "
    541                "unexpected result: %d. ",
    542                check_res);
    543       mhdErrorExitDesc ("Wrong returned code");
    544       break;
    545     default:
    546       fprintf (stderr, "'MHD_digest_auth_check3()' returned "
    547                "impossible result code: %d. ",
    548                check_res);
    549       mhdErrorExitDesc ("Impossible returned code");
    550     }
    551 
    552     response =
    553       MHD_create_response_from_buffer_static (MHD_STATICSTR_LEN_ (DENIED),
    554                                               (const void *) DENIED);
    555     if (NULL == response)
    556       mhdErrorExitDesc ("Response creation failed");
    557     ret = MHD_queue_auth_fail_response2 (connection, REALM, OPAQUE_VALUE,
    558                                          response, 0, MHD_DIGEST_ALG_MD5);
    559     if (MHD_YES != ret)
    560       mhdErrorExitDesc ("'MHD_queue_auth_fail_response2()' failed");
    561   }
    562   else
    563   {
    564     char *username;
    565     int check_res;
    566 
    567     username = MHD_digest_auth_get_username (connection);
    568     if (NULL == username)
    569       mhdErrorExitDesc ("'MHD_digest_auth_get_username()' returned NULL");
    570     else if (0 != strcmp (username, USERNAME))
    571     {
    572       fprintf (stderr, "'username' does not match.\n"
    573                "Expected: '%s'\tRecieved: '%s'. ",
    574                USERNAME,
    575                username);
    576       mhdErrorExitDesc ("Wrong 'username'");
    577     }
    578     MHD_free (username);
    579 
    580     check_res = MHD_digest_auth_check (connection, REALM, USERNAME,
    581                                        PASSWORD_VALUE,
    582                                        300);
    583 
    584     if (MHD_INVALID_NONCE != check_res)
    585     {
    586       fprintf (stderr, "'MHD_digest_auth_check()' returned unexpected"
    587                " result: %d. ", check_res);
    588       mhdErrorExitDesc ("Wrong 'MHD_digest_auth_check()' result");
    589     }
    590     response =
    591       MHD_create_response_from_buffer_static (MHD_STATICSTR_LEN_ (DENIED),
    592                                               (const void *) DENIED);
    593     if (NULL == response)
    594       mhdErrorExitDesc ("Response creation failed");
    595 
    596     ret = MHD_queue_auth_fail_response (connection, REALM, OPAQUE_VALUE,
    597                                         response, 0);
    598     if (MHD_YES != ret)
    599       mhdErrorExitDesc ("'MHD_queue_auth_fail_response()' failed");
    600   }
    601 
    602   MHD_destroy_response (response);
    603   return ret;
    604 }
    605 
    606 
    607 static CURL *
    608 setupCURL (void *cbc, uint16_t port)
    609 {
    610   CURL *c;
    611   char url[512];
    612 
    613   if (1)
    614   {
    615     int res;
    616     /* A workaround for some old libcurl versions, which ignore the specified
    617      * port by CURLOPT_PORT when authorisation is used. */
    618     res = snprintf (url, (sizeof(url) / sizeof(url[0])),
    619                     "http://127.0.0.1:%u%s",
    620                     (unsigned int) port, MHD_URI_BASE_PATH);
    621     if ((0 >= res) || ((sizeof(url) / sizeof(url[0])) <= (size_t) res))
    622       externalErrorExitDesc ("Cannot form request URL");
    623   }
    624 
    625   c = curl_easy_init ();
    626   if (NULL == c)
    627     libcurlErrorExitDesc ("curl_easy_init() failed");
    628 
    629   if ((CURLE_OK != curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1L)) ||
    630       (CURLE_OK != curl_easy_setopt (c, CURLOPT_PROXY, "")) ||
    631       (CURLE_OK != curl_easy_setopt (c, CURLOPT_WRITEFUNCTION,
    632                                      &copyBuffer)) ||
    633       (CURLE_OK != curl_easy_setopt (c, CURLOPT_WRITEDATA, cbc)) ||
    634       (CURLE_OK != curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT,
    635                                      ((long) TIMEOUTS_VAL))) ||
    636       (CURLE_OK != curl_easy_setopt (c, CURLOPT_HTTP_VERSION,
    637                                      CURL_HTTP_VERSION_1_1)) ||
    638       (CURLE_OK != curl_easy_setopt (c, CURLOPT_TIMEOUT,
    639                                      ((long) TIMEOUTS_VAL))) ||
    640       (CURLE_OK != curl_easy_setopt (c, CURLOPT_ERRORBUFFER,
    641                                      libcurl_errbuf)) ||
    642       /* (CURLE_OK != curl_easy_setopt (c, CURLOPT_VERBOSE, 1L)) || */
    643       (CURLE_OK != curl_easy_setopt (c, CURLOPT_FAILONERROR, 0L)) ||
    644       (CURLE_OK != curl_easy_setopt (c, CURLOPT_HTTPHEADER, curl_headers)) ||
    645 #if CURL_AT_LEAST_VERSION (7, 85, 0)
    646       (CURLE_OK != curl_easy_setopt (c, CURLOPT_PROTOCOLS_STR, "http")) ||
    647 #elif CURL_AT_LEAST_VERSION (7, 19, 4)
    648       (CURLE_OK != curl_easy_setopt (c, CURLOPT_PROTOCOLS, CURLPROTO_HTTP)) ||
    649 #endif /* CURL_AT_LEAST_VERSION (7, 19, 4) */
    650 #if CURL_AT_LEAST_VERSION (7, 45, 0)
    651       (CURLE_OK != curl_easy_setopt (c, CURLOPT_DEFAULT_PROTOCOL, "http")) ||
    652 #endif /* CURL_AT_LEAST_VERSION (7, 45, 0) */
    653       (CURLE_OK != curl_easy_setopt (c, CURLOPT_PORT, ((long) port))) ||
    654       (CURLE_OK != curl_easy_setopt (c, CURLOPT_URL, url)))
    655     libcurlErrorExitDesc ("curl_easy_setopt() failed");
    656   return c;
    657 }
    658 
    659 
    660 static CURLcode
    661 performQueryExternal (struct MHD_Daemon *d, CURL *c)
    662 {
    663   CURLM *multi;
    664   time_t start;
    665   struct timeval tv;
    666   CURLcode ret;
    667 
    668   ret = CURLE_FAILED_INIT; /* will be replaced with real result */
    669   multi = NULL;
    670   multi = curl_multi_init ();
    671   if (multi == NULL)
    672     libcurlErrorExitDesc ("curl_multi_init() failed");
    673   if (CURLM_OK != curl_multi_add_handle (multi, c))
    674     libcurlErrorExitDesc ("curl_multi_add_handle() failed");
    675 
    676   start = time (NULL);
    677   while (time (NULL) - start <= TIMEOUTS_VAL)
    678   {
    679     fd_set rs;
    680     fd_set ws;
    681     fd_set es;
    682     MHD_socket maxMhdSk;
    683     int maxCurlSk;
    684     int running;
    685 
    686     maxMhdSk = MHD_INVALID_SOCKET;
    687     maxCurlSk = -1;
    688     FD_ZERO (&rs);
    689     FD_ZERO (&ws);
    690     FD_ZERO (&es);
    691     if (NULL != multi)
    692     {
    693       curl_multi_perform (multi, &running);
    694       if (0 == running)
    695       {
    696         struct CURLMsg *msg;
    697         int msgLeft;
    698         int totalMsgs = 0;
    699         do
    700         {
    701           msg = curl_multi_info_read (multi, &msgLeft);
    702           if (NULL == msg)
    703             libcurlErrorExitDesc ("curl_multi_info_read() failed");
    704           totalMsgs++;
    705           if (CURLMSG_DONE == msg->msg)
    706             ret = msg->data.result;
    707         } while (msgLeft > 0);
    708         if (1 != totalMsgs)
    709         {
    710           fprintf (stderr,
    711                    "curl_multi_info_read returned wrong "
    712                    "number of results (%d).\n",
    713                    totalMsgs);
    714           externalErrorExit ();
    715         }
    716         curl_multi_remove_handle (multi, c);
    717         curl_multi_cleanup (multi);
    718         multi = NULL;
    719       }
    720       else
    721       {
    722         if (CURLM_OK != curl_multi_fdset (multi, &rs, &ws, &es, &maxCurlSk))
    723           libcurlErrorExitDesc ("curl_multi_fdset() failed");
    724       }
    725     }
    726     if (NULL == multi)
    727     { /* libcurl has finished, check whether MHD still needs to perform cleanup */
    728       if (0 != MHD_get_timeout64s (d))
    729         break; /* MHD finished as well */
    730     }
    731     if (MHD_YES != MHD_get_fdset (d, &rs, &ws, &es, &maxMhdSk))
    732       mhdErrorExitDesc ("MHD_get_fdset() failed");
    733     tv.tv_sec = 0;
    734     tv.tv_usec = 200000;
    735 #ifdef MHD_POSIX_SOCKETS
    736     if (maxMhdSk > maxCurlSk)
    737       maxCurlSk = maxMhdSk;
    738 #endif /* MHD_POSIX_SOCKETS */
    739     if (-1 == select (maxCurlSk + 1, &rs, &ws, &es, &tv))
    740     {
    741 #ifdef MHD_POSIX_SOCKETS
    742       if (EINTR != errno)
    743         externalErrorExitDesc ("Unexpected select() error");
    744 #else
    745       if ((WSAEINVAL != WSAGetLastError ()) ||
    746           (0 != rs.fd_count) || (0 != ws.fd_count) || (0 != es.fd_count) )
    747         externalErrorExitDesc ("Unexpected select() error");
    748       Sleep (200);
    749 #endif
    750     }
    751     if (MHD_YES != MHD_run_from_select (d, &rs, &ws, &es))
    752       mhdErrorExitDesc ("MHD_run_from_select() failed");
    753   }
    754 
    755   return ret;
    756 }
    757 
    758 
    759 /**
    760  * Check request result
    761  * @param curl_code the CURL easy return code
    762  * @param pcbc the pointer struct CBC
    763  * @return non-zero if success, zero if failed
    764  */
    765 static unsigned int
    766 check_result (CURLcode curl_code, CURL *c, struct CBC *pcbc)
    767 {
    768   long code;
    769   if (CURLE_OK != curl_easy_getinfo (c, CURLINFO_RESPONSE_CODE, &code))
    770     libcurlErrorExit ();
    771 
    772   if (401 != code)
    773   {
    774     fprintf (stderr, "Request returned wrong code: %ld.\n",
    775              code);
    776     return 0;
    777   }
    778 
    779   if (CURLE_OK != curl_code)
    780   {
    781     fflush (stdout);
    782     if (0 != libcurl_errbuf[0])
    783       fprintf (stderr, "Request failed. "
    784                "libcurl error: '%s'.\n"
    785                "libcurl error description: '%s'.\n",
    786                curl_easy_strerror (curl_code),
    787                libcurl_errbuf);
    788     else
    789       fprintf (stderr, "Request failed. "
    790                "libcurl error: '%s'.\n",
    791                curl_easy_strerror (curl_code));
    792     fflush (stderr);
    793     return 0;
    794   }
    795 
    796   if (pcbc->pos != strlen (DENIED))
    797   {
    798     fprintf (stderr, "Got %u bytes ('%.*s'), expected %u bytes. ",
    799              (unsigned) pcbc->pos, (int) pcbc->pos, pcbc->buf,
    800              (unsigned) strlen (DENIED));
    801     mhdErrorExitDesc ("Wrong returned data length");
    802   }
    803   if (0 != memcmp (DENIED, pcbc->buf, pcbc->pos))
    804   {
    805     fprintf (stderr, "Got invalid response '%.*s'. ",
    806              (int) pcbc->pos, pcbc->buf);
    807     mhdErrorExitDesc ("Wrong returned data");
    808   }
    809   return 1;
    810 }
    811 
    812 
    813 static unsigned int
    814 testDigestAuthEmu (void)
    815 {
    816   struct MHD_Daemon *d;
    817   uint16_t port;
    818   struct CBC cbc;
    819   char buf[2048];
    820   CURL *c;
    821   int failed = 0;
    822 
    823   if (MHD_NO != MHD_is_feature_supported (MHD_FEATURE_AUTODETECT_BIND_PORT))
    824     port = 0;
    825   else
    826     port = 4210;
    827 
    828   d = MHD_start_daemon (MHD_USE_ERROR_LOG | MHD_USE_NO_THREAD_SAFETY,
    829                         port, NULL, NULL,
    830                         &ahc_echo, NULL,
    831                         MHD_OPTION_APP_FD_SETSIZE, (int) FD_SETSIZE,
    832                         MHD_OPTION_END);
    833   if (d == NULL)
    834     return 1;
    835   if (0 == port)
    836   {
    837     const union MHD_DaemonInfo *dinfo;
    838 
    839     dinfo = MHD_get_daemon_info (d,
    840                                  MHD_DAEMON_INFO_BIND_PORT);
    841     if ( (NULL == dinfo) ||
    842          (0 == dinfo->port) )
    843       mhdErrorExitDesc ("MHD_get_daemon_info() failed");
    844     port = (uint16_t) dinfo->port;
    845   }
    846 
    847   /* First request */
    848   cbc.buf = buf;
    849   cbc.size = sizeof (buf);
    850   cbc.pos = 0;
    851   memset (cbc.buf, 0, cbc.size);
    852   c = setupCURL (&cbc, port);
    853   if (check_result (performQueryExternal (d, c), c, &cbc))
    854   {
    855     if (verbose)
    856       printf ("Got expected response.\n");
    857   }
    858   else
    859   {
    860     fprintf (stderr, "Request FAILED.\n");
    861     failed = 1;
    862   }
    863   curl_easy_cleanup (c);
    864 
    865   MHD_stop_daemon (d);
    866   return failed ? 1 : 0;
    867 }
    868 
    869 
    870 int
    871 main (int argc, char *const *argv)
    872 {
    873   unsigned int errorCount = 0;
    874   (void) argc; (void) argv; /* Unused. Silent compiler warning. */
    875 
    876   verbose = ! (has_param (argc, argv, "-q") ||
    877                has_param (argc, argv, "--quiet") ||
    878                has_param (argc, argv, "-s") ||
    879                has_param (argc, argv, "--silent"));
    880   oldapi = has_in_name (argv[0], "_oldapi");
    881   test_global_init ();
    882 
    883   errorCount += testDigestAuthEmu ();
    884   if (errorCount != 0)
    885     fprintf (stderr, "Error (code: %u)\n", errorCount);
    886   test_global_cleanup ();
    887   return (0 == errorCount) ? 0 : 1;       /* 0 == pass */
    888 }