libmicrohttpd

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

commit c8b7b8fed96c513c82b2da9037206daccf3eb492
parent 929e046215d1323e5e4c5fed91c9c4a8b37e07c9
Author: Evgeny Grin (Karlson2k) <k2k@narod.ru>
Date:   Tue,  7 Jun 2022 19:30:19 +0300

test_basicauth: added two additional tests for the new API

Diffstat:
Msrc/testcurl/.gitignore | 3+++
Msrc/testcurl/Makefile.am | 9++++++++-
Msrc/testcurl/test_basicauth.c | 156+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------------------
3 files changed, 131 insertions(+), 37 deletions(-)

diff --git a/src/testcurl/.gitignore b/src/testcurl/.gitignore @@ -153,3 +153,5 @@ core /test_basicauth /test_parse_cookies_invalid /test_basicauth_preauth +/test_basicauth_oldapi +/test_basicauth_preauth_oldapi +\ No newline at end of file diff --git a/src/testcurl/Makefile.am b/src/testcurl/Makefile.am @@ -153,7 +153,8 @@ endif if ENABLE_BAUTH check_PROGRAMS += \ - test_basicauth test_basicauth_preauth + test_basicauth test_basicauth_preauth \ + test_basicauth_oldapi test_basicauth_preauth_oldapi endif if HAVE_POSTPROCESSOR @@ -239,6 +240,12 @@ test_basicauth_SOURCES = \ test_basicauth_preauth_SOURCES = \ test_basicauth.c +test_basicauth_oldapi_SOURCES = \ + test_basicauth.c + +test_basicauth_preauth_oldapi_SOURCES = \ + test_basicauth.c + test_digestauth_SOURCES = \ test_digestauth.c test_digestauth_LDADD = \ diff --git a/src/testcurl/test_basicauth.c b/src/testcurl/test_basicauth.c @@ -251,6 +251,7 @@ struct CBC static int verbose; static int preauth; +static int oldapi; static size_t copyBuffer (void *ptr, @@ -279,8 +280,6 @@ ahc_echo (void *cls, void **req_cls) { struct MHD_Response *response; - char *username; - char *password; enum MHD_Result ret; static int already_called_marker; (void) cls; (void) url; /* Unused. Silent compiler warning. */ @@ -298,46 +297,129 @@ ahc_echo (void *cls, mhdErrorExitDesc ("Unexpected HTTP method"); /* require: USERNAME with password PASSWORD */ - password = NULL; - username = MHD_basic_auth_get_username_password (connection, - &password); - if (NULL != username) + if (! oldapi) { - if (0 != strcmp (username, USERNAME)) + struct MHD_BasicAuthInfo *creds; + + creds = MHD_basic_auth_get_username_password3 (connection); + if (NULL != creds) + { + if (NULL == creds->username) + mhdErrorExitDesc ("'username' is NULL"); + else if (MHD_STATICSTR_LEN_ (USERNAME) != creds->username_len) + { + fprintf (stderr, "'username_len' does not match.\n" + "Expected: %u\tRecieved: %u. ", + (unsigned) MHD_STATICSTR_LEN_ (USERNAME), + (unsigned) creds->username_len); + mhdErrorExitDesc ("Wrong 'username_len'"); + } + else if (0 != memcmp (creds->username, USERNAME, creds->username_len)) + { + fprintf (stderr, "'username' does not match.\n" + "Expected: '%s'\tRecieved: '%.*s'. ", + USERNAME, + (int) creds->username_len, + creds->username); + mhdErrorExitDesc ("Wrong 'username'"); + } + else if (0 != creds->username[creds->username_len]) + mhdErrorExitDesc ("'username' is not zero-terminated"); + else if (NULL == creds->password) + mhdErrorExitDesc ("'password' is NULL"); + else if (MHD_STATICSTR_LEN_ (PASSWORD) != creds->password_len) + { + fprintf (stderr, "'password_len' does not match.\n" + "Expected: %u\tRecieved: %u. ", + (unsigned) MHD_STATICSTR_LEN_ (PASSWORD), + (unsigned) creds->password_len); + mhdErrorExitDesc ("Wrong 'password_len'"); + } + else if (0 != memcmp (creds->password, PASSWORD, creds->password_len)) + { + fprintf (stderr, "'password' does not match.\n" + "Expected: '%s'\tRecieved: '%.*s'. ", + PASSWORD, + (int) creds->password_len, + creds->password); + mhdErrorExitDesc ("Wrong 'username'"); + } + else if (0 != creds->password[creds->password_len]) + mhdErrorExitDesc ("'password' is not zero-terminated"); + + MHD_free (creds); + + response = + MHD_create_response_from_buffer_static (MHD_STATICSTR_LEN_ (PAGE), + (const void *) PAGE); + if (NULL == response) + mhdErrorExitDesc ("Response creation failed"); + ret = MHD_queue_response (connection, MHD_HTTP_OK, response); + if (MHD_YES != ret) + mhdErrorExitDesc ("'MHD_queue_response()' failed"); + } + else { - fprintf (stderr, "'username' does not match.\n" - "Expected: '%s'\tRecieved: '%s'. ", USERNAME, username); - mhdErrorExitDesc ("Wrong 'username'"); + response = + MHD_create_response_from_buffer_static (MHD_STATICSTR_LEN_ (DENIED), + (const void *) DENIED); + if (NULL == response) + mhdErrorExitDesc ("Response creation failed"); + ret = MHD_queue_basic_auth_fail_response3 (connection, REALM, 1, + response); + if (MHD_YES != ret) + mhdErrorExitDesc ("'MHD_queue_basic_auth_fail_response3()' failed"); } - if (NULL == password) - mhdErrorExitDesc ("The password pointer is NULL"); - if (0 != strcmp (password, PASSWORD)) - fprintf (stderr, "'password' does not match.\n" - "Expected: '%s'\tRecieved: '%s'. ", PASSWORD, password); - response = - MHD_create_response_from_buffer_static (MHD_STATICSTR_LEN_ (PAGE), - (const void *) PAGE); - if (NULL == response) - mhdErrorExitDesc ("Response creation failed"); - ret = MHD_queue_response (connection, MHD_HTTP_OK, response); - if (MHD_YES != ret) - mhdErrorExitDesc ("'MHD_queue_response()' failed"); } else { + char *username; + char *password; + + password = NULL; + username = MHD_basic_auth_get_username_password (connection, + &password); + if (NULL != username) + { + if (0 != strcmp (username, USERNAME)) + { + fprintf (stderr, "'username' does not match.\n" + "Expected: '%s'\tRecieved: '%s'. ", USERNAME, username); + mhdErrorExitDesc ("Wrong 'username'"); + } + if (NULL == password) + mhdErrorExitDesc ("The password pointer is NULL"); + if (0 != strcmp (password, PASSWORD)) + fprintf (stderr, "'password' does not match.\n" + "Expected: '%s'\tRecieved: '%s'. ", PASSWORD, password); + response = + MHD_create_response_from_buffer_static (MHD_STATICSTR_LEN_ (PAGE), + (const void *) PAGE); + if (NULL == response) + mhdErrorExitDesc ("Response creation failed"); + ret = MHD_queue_response (connection, MHD_HTTP_OK, response); + if (MHD_YES != ret) + mhdErrorExitDesc ("'MHD_queue_response()' failed"); + } + else + { + if (NULL != password) + mhdErrorExitDesc ("The password pointer is NOT NULL"); + response = + MHD_create_response_from_buffer_static (MHD_STATICSTR_LEN_ (DENIED), + (const void *) DENIED); + if (NULL == response) + mhdErrorExitDesc ("Response creation failed"); + ret = MHD_queue_basic_auth_fail_response (connection, REALM, response); + if (MHD_YES != ret) + mhdErrorExitDesc ("'MHD_queue_basic_auth_fail_response()' failed"); + } + if (NULL != username) + MHD_free (username); if (NULL != password) - mhdErrorExitDesc ("The password pointer is NOT NULL"); - response = - MHD_create_response_from_buffer_static (MHD_STATICSTR_LEN_ (DENIED), - (const void *) DENIED); - ret = MHD_queue_basic_auth_fail_response (connection, REALM, - response); + MHD_free (password); } - if (NULL != username) - MHD_free (username); - if (NULL != password) - MHD_free (password); MHD_destroy_response (response); return ret; } @@ -376,6 +458,7 @@ setupCURL (void *cbc, int port, char *errbuf) ((long) TIMEOUTS_VAL))) || (CURLE_OK != curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1)) || + /* (CURLE_OK != curl_easy_setopt (c, CURLOPT_VERBOSE, 1L)) || */ (CURLE_OK != curl_easy_setopt (c, CURLOPT_FAILONERROR, 1L)) || #if CURL_AT_LEAST_VERSION (7, 19, 4) (CURLE_OK != curl_easy_setopt (c, CURLOPT_PROTOCOLS, CURLPROTO_HTTP)) || @@ -386,10 +469,10 @@ setupCURL (void *cbc, int port, char *errbuf) (CURLE_OK != curl_easy_setopt (c, CURLOPT_PORT, ((long) port))) || (CURLE_OK != curl_easy_setopt (c, CURLOPT_URL, url))) libcurlErrorExitDesc ("curl_easy_setopt() failed"); -#if CURL_AT_LEAST_VERSION(7,21,3) +#if CURL_AT_LEAST_VERSION (7,21,3) if ((CURLE_OK != curl_easy_setopt (c, CURLOPT_HTTPAUTH, - CURLAUTH_BASIC | - (preauth ? 0 : CURLAUTH_ONLY))) || + CURLAUTH_BASIC + | (preauth ? 0 : CURLAUTH_ONLY))) || (CURLE_OK != curl_easy_setopt (c, CURLOPT_USERPWD, USERNAME ":" PASSWORD))) libcurlErrorExitDesc ("curl_easy_setopt() authorization options failed"); @@ -646,6 +729,7 @@ main (int argc, char *const *argv) #endif #endif #endif /* MHD_HTTPS_REQUIRE_GRYPT */ + oldapi = has_in_name (argv[0], "_oldapi"); if (0 != curl_global_init (CURL_GLOBAL_WIN32)) return 2; errorCount += testBasicAuth ();