diff options
author | Evgeny Grin (Karlson2k) <k2k@narod.ru> | 2022-06-07 19:30:19 +0300 |
---|---|---|
committer | Evgeny Grin (Karlson2k) <k2k@narod.ru> | 2022-06-08 16:52:50 +0300 |
commit | c8b7b8fed96c513c82b2da9037206daccf3eb492 (patch) | |
tree | 05a39b3cbc5c5ad0168ce7ba8857b8db846ff9bd /src/testcurl | |
parent | 929e046215d1323e5e4c5fed91c9c4a8b37e07c9 (diff) | |
download | libmicrohttpd-c8b7b8fed96c513c82b2da9037206daccf3eb492.tar.gz libmicrohttpd-c8b7b8fed96c513c82b2da9037206daccf3eb492.zip |
test_basicauth: added two additional tests for the new API
Diffstat (limited to 'src/testcurl')
-rw-r--r-- | src/testcurl/.gitignore | 2 | ||||
-rw-r--r-- | src/testcurl/Makefile.am | 9 | ||||
-rw-r--r-- | src/testcurl/test_basicauth.c | 156 |
3 files changed, 130 insertions, 37 deletions
diff --git a/src/testcurl/.gitignore b/src/testcurl/.gitignore index cf6a4bc3..f3ffee14 100644 --- a/src/testcurl/.gitignore +++ b/src/testcurl/.gitignore | |||
@@ -153,3 +153,5 @@ core | |||
153 | /test_basicauth | 153 | /test_basicauth |
154 | /test_parse_cookies_invalid | 154 | /test_parse_cookies_invalid |
155 | /test_basicauth_preauth | 155 | /test_basicauth_preauth |
156 | /test_basicauth_oldapi | ||
157 | /test_basicauth_preauth_oldapi \ No newline at end of file | ||
diff --git a/src/testcurl/Makefile.am b/src/testcurl/Makefile.am index aeeabb6b..3bf81e7d 100644 --- a/src/testcurl/Makefile.am +++ b/src/testcurl/Makefile.am | |||
@@ -153,7 +153,8 @@ endif | |||
153 | 153 | ||
154 | if ENABLE_BAUTH | 154 | if ENABLE_BAUTH |
155 | check_PROGRAMS += \ | 155 | check_PROGRAMS += \ |
156 | test_basicauth test_basicauth_preauth | 156 | test_basicauth test_basicauth_preauth \ |
157 | test_basicauth_oldapi test_basicauth_preauth_oldapi | ||
157 | endif | 158 | endif |
158 | 159 | ||
159 | if HAVE_POSTPROCESSOR | 160 | if HAVE_POSTPROCESSOR |
@@ -239,6 +240,12 @@ test_basicauth_SOURCES = \ | |||
239 | test_basicauth_preauth_SOURCES = \ | 240 | test_basicauth_preauth_SOURCES = \ |
240 | test_basicauth.c | 241 | test_basicauth.c |
241 | 242 | ||
243 | test_basicauth_oldapi_SOURCES = \ | ||
244 | test_basicauth.c | ||
245 | |||
246 | test_basicauth_preauth_oldapi_SOURCES = \ | ||
247 | test_basicauth.c | ||
248 | |||
242 | test_digestauth_SOURCES = \ | 249 | test_digestauth_SOURCES = \ |
243 | test_digestauth.c | 250 | test_digestauth.c |
244 | test_digestauth_LDADD = \ | 251 | test_digestauth_LDADD = \ |
diff --git a/src/testcurl/test_basicauth.c b/src/testcurl/test_basicauth.c index c7f1df84..e95047dd 100644 --- a/src/testcurl/test_basicauth.c +++ b/src/testcurl/test_basicauth.c | |||
@@ -251,6 +251,7 @@ struct CBC | |||
251 | 251 | ||
252 | static int verbose; | 252 | static int verbose; |
253 | static int preauth; | 253 | static int preauth; |
254 | static int oldapi; | ||
254 | 255 | ||
255 | static size_t | 256 | static size_t |
256 | copyBuffer (void *ptr, | 257 | copyBuffer (void *ptr, |
@@ -279,8 +280,6 @@ ahc_echo (void *cls, | |||
279 | void **req_cls) | 280 | void **req_cls) |
280 | { | 281 | { |
281 | struct MHD_Response *response; | 282 | struct MHD_Response *response; |
282 | char *username; | ||
283 | char *password; | ||
284 | enum MHD_Result ret; | 283 | enum MHD_Result ret; |
285 | static int already_called_marker; | 284 | static int already_called_marker; |
286 | (void) cls; (void) url; /* Unused. Silent compiler warning. */ | 285 | (void) cls; (void) url; /* Unused. Silent compiler warning. */ |
@@ -298,46 +297,129 @@ ahc_echo (void *cls, | |||
298 | mhdErrorExitDesc ("Unexpected HTTP method"); | 297 | mhdErrorExitDesc ("Unexpected HTTP method"); |
299 | 298 | ||
300 | /* require: USERNAME with password PASSWORD */ | 299 | /* require: USERNAME with password PASSWORD */ |
301 | password = NULL; | 300 | if (! oldapi) |
302 | username = MHD_basic_auth_get_username_password (connection, | ||
303 | &password); | ||
304 | if (NULL != username) | ||
305 | { | 301 | { |
306 | if (0 != strcmp (username, USERNAME)) | 302 | struct MHD_BasicAuthInfo *creds; |
303 | |||
304 | creds = MHD_basic_auth_get_username_password3 (connection); | ||
305 | if (NULL != creds) | ||
306 | { | ||
307 | if (NULL == creds->username) | ||
308 | mhdErrorExitDesc ("'username' is NULL"); | ||
309 | else if (MHD_STATICSTR_LEN_ (USERNAME) != creds->username_len) | ||
310 | { | ||
311 | fprintf (stderr, "'username_len' does not match.\n" | ||
312 | "Expected: %u\tRecieved: %u. ", | ||
313 | (unsigned) MHD_STATICSTR_LEN_ (USERNAME), | ||
314 | (unsigned) creds->username_len); | ||
315 | mhdErrorExitDesc ("Wrong 'username_len'"); | ||
316 | } | ||
317 | else if (0 != memcmp (creds->username, USERNAME, creds->username_len)) | ||
318 | { | ||
319 | fprintf (stderr, "'username' does not match.\n" | ||
320 | "Expected: '%s'\tRecieved: '%.*s'. ", | ||
321 | USERNAME, | ||
322 | (int) creds->username_len, | ||
323 | creds->username); | ||
324 | mhdErrorExitDesc ("Wrong 'username'"); | ||
325 | } | ||
326 | else if (0 != creds->username[creds->username_len]) | ||
327 | mhdErrorExitDesc ("'username' is not zero-terminated"); | ||
328 | else if (NULL == creds->password) | ||
329 | mhdErrorExitDesc ("'password' is NULL"); | ||
330 | else if (MHD_STATICSTR_LEN_ (PASSWORD) != creds->password_len) | ||
331 | { | ||
332 | fprintf (stderr, "'password_len' does not match.\n" | ||
333 | "Expected: %u\tRecieved: %u. ", | ||
334 | (unsigned) MHD_STATICSTR_LEN_ (PASSWORD), | ||
335 | (unsigned) creds->password_len); | ||
336 | mhdErrorExitDesc ("Wrong 'password_len'"); | ||
337 | } | ||
338 | else if (0 != memcmp (creds->password, PASSWORD, creds->password_len)) | ||
339 | { | ||
340 | fprintf (stderr, "'password' does not match.\n" | ||
341 | "Expected: '%s'\tRecieved: '%.*s'. ", | ||
342 | PASSWORD, | ||
343 | (int) creds->password_len, | ||
344 | creds->password); | ||
345 | mhdErrorExitDesc ("Wrong 'username'"); | ||
346 | } | ||
347 | else if (0 != creds->password[creds->password_len]) | ||
348 | mhdErrorExitDesc ("'password' is not zero-terminated"); | ||
349 | |||
350 | MHD_free (creds); | ||
351 | |||
352 | response = | ||
353 | MHD_create_response_from_buffer_static (MHD_STATICSTR_LEN_ (PAGE), | ||
354 | (const void *) PAGE); | ||
355 | if (NULL == response) | ||
356 | mhdErrorExitDesc ("Response creation failed"); | ||
357 | ret = MHD_queue_response (connection, MHD_HTTP_OK, response); | ||
358 | if (MHD_YES != ret) | ||
359 | mhdErrorExitDesc ("'MHD_queue_response()' failed"); | ||
360 | } | ||
361 | else | ||
307 | { | 362 | { |
308 | fprintf (stderr, "'username' does not match.\n" | 363 | response = |
309 | "Expected: '%s'\tRecieved: '%s'. ", USERNAME, username); | 364 | MHD_create_response_from_buffer_static (MHD_STATICSTR_LEN_ (DENIED), |
310 | mhdErrorExitDesc ("Wrong 'username'"); | 365 | (const void *) DENIED); |
366 | if (NULL == response) | ||
367 | mhdErrorExitDesc ("Response creation failed"); | ||
368 | ret = MHD_queue_basic_auth_fail_response3 (connection, REALM, 1, | ||
369 | response); | ||
370 | if (MHD_YES != ret) | ||
371 | mhdErrorExitDesc ("'MHD_queue_basic_auth_fail_response3()' failed"); | ||
311 | } | 372 | } |
312 | if (NULL == password) | ||
313 | mhdErrorExitDesc ("The password pointer is NULL"); | ||
314 | if (0 != strcmp (password, PASSWORD)) | ||
315 | fprintf (stderr, "'password' does not match.\n" | ||
316 | "Expected: '%s'\tRecieved: '%s'. ", PASSWORD, password); | ||
317 | response = | ||
318 | MHD_create_response_from_buffer_static (MHD_STATICSTR_LEN_ (PAGE), | ||
319 | (const void *) PAGE); | ||
320 | if (NULL == response) | ||
321 | mhdErrorExitDesc ("Response creation failed"); | ||
322 | ret = MHD_queue_response (connection, MHD_HTTP_OK, response); | ||
323 | if (MHD_YES != ret) | ||
324 | mhdErrorExitDesc ("'MHD_queue_response()' failed"); | ||
325 | } | 373 | } |
326 | else | 374 | else |
327 | { | 375 | { |
376 | char *username; | ||
377 | char *password; | ||
378 | |||
379 | password = NULL; | ||
380 | username = MHD_basic_auth_get_username_password (connection, | ||
381 | &password); | ||
382 | if (NULL != username) | ||
383 | { | ||
384 | if (0 != strcmp (username, USERNAME)) | ||
385 | { | ||
386 | fprintf (stderr, "'username' does not match.\n" | ||
387 | "Expected: '%s'\tRecieved: '%s'. ", USERNAME, username); | ||
388 | mhdErrorExitDesc ("Wrong 'username'"); | ||
389 | } | ||
390 | if (NULL == password) | ||
391 | mhdErrorExitDesc ("The password pointer is NULL"); | ||
392 | if (0 != strcmp (password, PASSWORD)) | ||
393 | fprintf (stderr, "'password' does not match.\n" | ||
394 | "Expected: '%s'\tRecieved: '%s'. ", PASSWORD, password); | ||
395 | response = | ||
396 | MHD_create_response_from_buffer_static (MHD_STATICSTR_LEN_ (PAGE), | ||
397 | (const void *) PAGE); | ||
398 | if (NULL == response) | ||
399 | mhdErrorExitDesc ("Response creation failed"); | ||
400 | ret = MHD_queue_response (connection, MHD_HTTP_OK, response); | ||
401 | if (MHD_YES != ret) | ||
402 | mhdErrorExitDesc ("'MHD_queue_response()' failed"); | ||
403 | } | ||
404 | else | ||
405 | { | ||
406 | if (NULL != password) | ||
407 | mhdErrorExitDesc ("The password pointer is NOT NULL"); | ||
408 | response = | ||
409 | MHD_create_response_from_buffer_static (MHD_STATICSTR_LEN_ (DENIED), | ||
410 | (const void *) DENIED); | ||
411 | if (NULL == response) | ||
412 | mhdErrorExitDesc ("Response creation failed"); | ||
413 | ret = MHD_queue_basic_auth_fail_response (connection, REALM, response); | ||
414 | if (MHD_YES != ret) | ||
415 | mhdErrorExitDesc ("'MHD_queue_basic_auth_fail_response()' failed"); | ||
416 | } | ||
417 | if (NULL != username) | ||
418 | MHD_free (username); | ||
328 | if (NULL != password) | 419 | if (NULL != password) |
329 | mhdErrorExitDesc ("The password pointer is NOT NULL"); | 420 | MHD_free (password); |
330 | response = | ||
331 | MHD_create_response_from_buffer_static (MHD_STATICSTR_LEN_ (DENIED), | ||
332 | (const void *) DENIED); | ||
333 | ret = MHD_queue_basic_auth_fail_response (connection, REALM, | ||
334 | response); | ||
335 | } | 421 | } |
336 | 422 | ||
337 | if (NULL != username) | ||
338 | MHD_free (username); | ||
339 | if (NULL != password) | ||
340 | MHD_free (password); | ||
341 | MHD_destroy_response (response); | 423 | MHD_destroy_response (response); |
342 | return ret; | 424 | return ret; |
343 | } | 425 | } |
@@ -376,6 +458,7 @@ setupCURL (void *cbc, int port, char *errbuf) | |||
376 | ((long) TIMEOUTS_VAL))) || | 458 | ((long) TIMEOUTS_VAL))) || |
377 | (CURLE_OK != curl_easy_setopt (c, CURLOPT_HTTP_VERSION, | 459 | (CURLE_OK != curl_easy_setopt (c, CURLOPT_HTTP_VERSION, |
378 | CURL_HTTP_VERSION_1_1)) || | 460 | CURL_HTTP_VERSION_1_1)) || |
461 | /* (CURLE_OK != curl_easy_setopt (c, CURLOPT_VERBOSE, 1L)) || */ | ||
379 | (CURLE_OK != curl_easy_setopt (c, CURLOPT_FAILONERROR, 1L)) || | 462 | (CURLE_OK != curl_easy_setopt (c, CURLOPT_FAILONERROR, 1L)) || |
380 | #if CURL_AT_LEAST_VERSION (7, 19, 4) | 463 | #if CURL_AT_LEAST_VERSION (7, 19, 4) |
381 | (CURLE_OK != curl_easy_setopt (c, CURLOPT_PROTOCOLS, CURLPROTO_HTTP)) || | 464 | (CURLE_OK != curl_easy_setopt (c, CURLOPT_PROTOCOLS, CURLPROTO_HTTP)) || |
@@ -386,10 +469,10 @@ setupCURL (void *cbc, int port, char *errbuf) | |||
386 | (CURLE_OK != curl_easy_setopt (c, CURLOPT_PORT, ((long) port))) || | 469 | (CURLE_OK != curl_easy_setopt (c, CURLOPT_PORT, ((long) port))) || |
387 | (CURLE_OK != curl_easy_setopt (c, CURLOPT_URL, url))) | 470 | (CURLE_OK != curl_easy_setopt (c, CURLOPT_URL, url))) |
388 | libcurlErrorExitDesc ("curl_easy_setopt() failed"); | 471 | libcurlErrorExitDesc ("curl_easy_setopt() failed"); |
389 | #if CURL_AT_LEAST_VERSION(7,21,3) | 472 | #if CURL_AT_LEAST_VERSION (7,21,3) |
390 | if ((CURLE_OK != curl_easy_setopt (c, CURLOPT_HTTPAUTH, | 473 | if ((CURLE_OK != curl_easy_setopt (c, CURLOPT_HTTPAUTH, |
391 | CURLAUTH_BASIC | | 474 | CURLAUTH_BASIC |
392 | (preauth ? 0 : CURLAUTH_ONLY))) || | 475 | | (preauth ? 0 : CURLAUTH_ONLY))) || |
393 | (CURLE_OK != curl_easy_setopt (c, CURLOPT_USERPWD, | 476 | (CURLE_OK != curl_easy_setopt (c, CURLOPT_USERPWD, |
394 | USERNAME ":" PASSWORD))) | 477 | USERNAME ":" PASSWORD))) |
395 | libcurlErrorExitDesc ("curl_easy_setopt() authorization options failed"); | 478 | libcurlErrorExitDesc ("curl_easy_setopt() authorization options failed"); |
@@ -646,6 +729,7 @@ main (int argc, char *const *argv) | |||
646 | #endif | 729 | #endif |
647 | #endif | 730 | #endif |
648 | #endif /* MHD_HTTPS_REQUIRE_GRYPT */ | 731 | #endif /* MHD_HTTPS_REQUIRE_GRYPT */ |
732 | oldapi = has_in_name (argv[0], "_oldapi"); | ||
649 | if (0 != curl_global_init (CURL_GLOBAL_WIN32)) | 733 | if (0 != curl_global_init (CURL_GLOBAL_WIN32)) |
650 | return 2; | 734 | return 2; |
651 | errorCount += testBasicAuth (); | 735 | errorCount += testBasicAuth (); |