libauthretry.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2020, 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. ***************************************************************************/
  22. /* argv1 = URL
  23. * argv2 = main auth type
  24. * argv3 = second auth type
  25. */
  26. #include "test.h"
  27. #include "memdebug.h"
  28. static CURLcode send_request(CURL *curl, const char *url, int seq,
  29. long auth_scheme, const char *userpwd)
  30. {
  31. CURLcode res;
  32. size_t len = strlen(url) + 4 + 1;
  33. char *full_url = malloc(len);
  34. if(!full_url) {
  35. fprintf(stderr, "Not enough memory for full url\n");
  36. return CURLE_OUT_OF_MEMORY;
  37. }
  38. msnprintf(full_url, len, "%s%04d", url, seq);
  39. fprintf(stderr, "Sending new request %d to %s with credential %s "
  40. "(auth %ld)\n", seq, full_url, userpwd, auth_scheme);
  41. test_setopt(curl, CURLOPT_URL, full_url);
  42. test_setopt(curl, CURLOPT_VERBOSE, 1L);
  43. test_setopt(curl, CURLOPT_HEADER, 1L);
  44. test_setopt(curl, CURLOPT_HTTPGET, 1L);
  45. test_setopt(curl, CURLOPT_USERPWD, userpwd);
  46. test_setopt(curl, CURLOPT_HTTPAUTH, auth_scheme);
  47. res = curl_easy_perform(curl);
  48. test_cleanup:
  49. free(full_url);
  50. return res;
  51. }
  52. static CURLcode send_wrong_password(CURL *curl, const char *url, int seq,
  53. long auth_scheme)
  54. {
  55. return send_request(curl, url, seq, auth_scheme, "testuser:wrongpass");
  56. }
  57. static CURLcode send_right_password(CURL *curl, const char *url, int seq,
  58. long auth_scheme)
  59. {
  60. return send_request(curl, url, seq, auth_scheme, "testuser:testpass");
  61. }
  62. static long parse_auth_name(const char *arg)
  63. {
  64. if(!arg)
  65. return CURLAUTH_NONE;
  66. if(curl_strequal(arg, "basic"))
  67. return CURLAUTH_BASIC;
  68. if(curl_strequal(arg, "digest"))
  69. return CURLAUTH_DIGEST;
  70. if(curl_strequal(arg, "ntlm"))
  71. return CURLAUTH_NTLM;
  72. return CURLAUTH_NONE;
  73. }
  74. int test(char *url)
  75. {
  76. CURLcode res;
  77. CURL *curl = NULL;
  78. long main_auth_scheme = parse_auth_name(libtest_arg2);
  79. long fallback_auth_scheme = parse_auth_name(libtest_arg3);
  80. if(main_auth_scheme == CURLAUTH_NONE ||
  81. fallback_auth_scheme == CURLAUTH_NONE) {
  82. fprintf(stderr, "auth schemes not found on commandline\n");
  83. return TEST_ERR_MAJOR_BAD;
  84. }
  85. if(curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
  86. fprintf(stderr, "curl_global_init() failed\n");
  87. return TEST_ERR_MAJOR_BAD;
  88. }
  89. /* Send wrong password, then right password */
  90. curl = curl_easy_init();
  91. if(!curl) {
  92. fprintf(stderr, "curl_easy_init() failed\n");
  93. curl_global_cleanup();
  94. return TEST_ERR_MAJOR_BAD;
  95. }
  96. res = send_wrong_password(curl, url, 100, main_auth_scheme);
  97. if(res != CURLE_OK)
  98. goto test_cleanup;
  99. res = send_right_password(curl, url, 200, fallback_auth_scheme);
  100. if(res != CURLE_OK)
  101. goto test_cleanup;
  102. curl_easy_cleanup(curl);
  103. /* Send wrong password twice, then right password */
  104. curl = curl_easy_init();
  105. if(!curl) {
  106. fprintf(stderr, "curl_easy_init() failed\n");
  107. curl_global_cleanup();
  108. return TEST_ERR_MAJOR_BAD;
  109. }
  110. res = send_wrong_password(curl, url, 300, main_auth_scheme);
  111. if(res != CURLE_OK)
  112. goto test_cleanup;
  113. res = send_wrong_password(curl, url, 400, fallback_auth_scheme);
  114. if(res != CURLE_OK)
  115. goto test_cleanup;
  116. res = send_right_password(curl, url, 500, fallback_auth_scheme);
  117. if(res != CURLE_OK)
  118. goto test_cleanup;
  119. test_cleanup:
  120. curl_easy_cleanup(curl);
  121. curl_global_cleanup();
  122. return (int)res;
  123. }