libauthretry.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2022, Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. /* argv1 = URL
  25. * argv2 = main auth type
  26. * argv3 = second auth type
  27. */
  28. #include "test.h"
  29. #include "memdebug.h"
  30. static CURLcode send_request(CURL *curl, const char *url, int seq,
  31. long auth_scheme, const char *userpwd)
  32. {
  33. CURLcode res;
  34. size_t len = strlen(url) + 4 + 1;
  35. char *full_url = malloc(len);
  36. if(!full_url) {
  37. fprintf(stderr, "Not enough memory for full url\n");
  38. return CURLE_OUT_OF_MEMORY;
  39. }
  40. msnprintf(full_url, len, "%s%04d", url, seq);
  41. fprintf(stderr, "Sending new request %d to %s with credential %s "
  42. "(auth %ld)\n", seq, full_url, userpwd, auth_scheme);
  43. test_setopt(curl, CURLOPT_URL, full_url);
  44. test_setopt(curl, CURLOPT_VERBOSE, 1L);
  45. test_setopt(curl, CURLOPT_HEADER, 1L);
  46. test_setopt(curl, CURLOPT_HTTPGET, 1L);
  47. test_setopt(curl, CURLOPT_USERPWD, userpwd);
  48. test_setopt(curl, CURLOPT_HTTPAUTH, auth_scheme);
  49. res = curl_easy_perform(curl);
  50. test_cleanup:
  51. free(full_url);
  52. return res;
  53. }
  54. static CURLcode send_wrong_password(CURL *curl, const char *url, int seq,
  55. long auth_scheme)
  56. {
  57. return send_request(curl, url, seq, auth_scheme, "testuser:wrongpass");
  58. }
  59. static CURLcode send_right_password(CURL *curl, const char *url, int seq,
  60. long auth_scheme)
  61. {
  62. return send_request(curl, url, seq, auth_scheme, "testuser:testpass");
  63. }
  64. static long parse_auth_name(const char *arg)
  65. {
  66. if(!arg)
  67. return CURLAUTH_NONE;
  68. if(curl_strequal(arg, "basic"))
  69. return CURLAUTH_BASIC;
  70. if(curl_strequal(arg, "digest"))
  71. return CURLAUTH_DIGEST;
  72. if(curl_strequal(arg, "ntlm"))
  73. return CURLAUTH_NTLM;
  74. return CURLAUTH_NONE;
  75. }
  76. int test(char *url)
  77. {
  78. CURLcode res;
  79. CURL *curl = NULL;
  80. long main_auth_scheme = parse_auth_name(libtest_arg2);
  81. long fallback_auth_scheme = parse_auth_name(libtest_arg3);
  82. if(main_auth_scheme == CURLAUTH_NONE ||
  83. fallback_auth_scheme == CURLAUTH_NONE) {
  84. fprintf(stderr, "auth schemes not found on commandline\n");
  85. return TEST_ERR_MAJOR_BAD;
  86. }
  87. if(curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
  88. fprintf(stderr, "curl_global_init() failed\n");
  89. return TEST_ERR_MAJOR_BAD;
  90. }
  91. /* Send wrong password, then right password */
  92. curl = curl_easy_init();
  93. if(!curl) {
  94. fprintf(stderr, "curl_easy_init() failed\n");
  95. curl_global_cleanup();
  96. return TEST_ERR_MAJOR_BAD;
  97. }
  98. res = send_wrong_password(curl, url, 100, main_auth_scheme);
  99. if(res != CURLE_OK)
  100. goto test_cleanup;
  101. res = send_right_password(curl, url, 200, fallback_auth_scheme);
  102. if(res != CURLE_OK)
  103. goto test_cleanup;
  104. curl_easy_cleanup(curl);
  105. /* Send wrong password twice, then right password */
  106. curl = curl_easy_init();
  107. if(!curl) {
  108. fprintf(stderr, "curl_easy_init() failed\n");
  109. curl_global_cleanup();
  110. return TEST_ERR_MAJOR_BAD;
  111. }
  112. res = send_wrong_password(curl, url, 300, main_auth_scheme);
  113. if(res != CURLE_OK)
  114. goto test_cleanup;
  115. res = send_wrong_password(curl, url, 400, fallback_auth_scheme);
  116. if(res != CURLE_OK)
  117. goto test_cleanup;
  118. res = send_right_password(curl, url, 500, fallback_auth_scheme);
  119. if(res != CURLE_OK)
  120. goto test_cleanup;
  121. test_cleanup:
  122. curl_easy_cleanup(curl);
  123. curl_global_cleanup();
  124. return (int)res;
  125. }