00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include "test.h"
00028 #include "memdebug.h"
00029
00030 static CURLcode send_request(CURL *curl, const char *url, int seq,
00031 long auth_scheme, const char *userpwd)
00032 {
00033 CURLcode res;
00034 size_t len = strlen(url) + 4 + 1;
00035 char *full_url = malloc(len);
00036 if(!full_url) {
00037 fprintf(stderr, "Not enough memory for full url\n");
00038 return CURLE_OUT_OF_MEMORY;
00039 }
00040
00041 snprintf(full_url, len, "%s%04d", url, seq);
00042 fprintf(stderr, "Sending new request %d to %s with credential %s "
00043 "(auth %ld)\n", seq, full_url, userpwd, auth_scheme);
00044 test_setopt(curl, CURLOPT_URL, full_url);
00045 test_setopt(curl, CURLOPT_VERBOSE, 1L);
00046 test_setopt(curl, CURLOPT_HEADER, 1L);
00047 test_setopt(curl, CURLOPT_HTTPGET, 1L);
00048 test_setopt(curl, CURLOPT_USERPWD, userpwd);
00049 test_setopt(curl, CURLOPT_HTTPAUTH, auth_scheme);
00050
00051 res = curl_easy_perform(curl);
00052
00053 test_cleanup:
00054 free(full_url);
00055 return res;
00056 }
00057
00058 static CURLcode send_wrong_password(CURL *curl, const char *url, int seq,
00059 long auth_scheme)
00060 {
00061 return send_request(curl, url, seq, auth_scheme, "testuser:wrongpass");
00062 }
00063
00064 static CURLcode send_right_password(CURL *curl, const char *url, int seq,
00065 long auth_scheme)
00066 {
00067 return send_request(curl, url, seq, auth_scheme, "testuser:testpass");
00068 }
00069
00070 static long parse_auth_name(const char *arg)
00071 {
00072 if(!arg)
00073 return CURLAUTH_NONE;
00074 if(curl_strequal(arg, "basic"))
00075 return CURLAUTH_BASIC;
00076 if(curl_strequal(arg, "digest"))
00077 return CURLAUTH_DIGEST;
00078 if(curl_strequal(arg, "ntlm"))
00079 return CURLAUTH_NTLM;
00080 return CURLAUTH_NONE;
00081 }
00082
00083 int test(char *url)
00084 {
00085 CURLcode res;
00086 CURL *curl = NULL;
00087
00088 long main_auth_scheme = parse_auth_name(libtest_arg2);
00089 long fallback_auth_scheme = parse_auth_name(libtest_arg3);
00090
00091 if(main_auth_scheme == CURLAUTH_NONE ||
00092 fallback_auth_scheme == CURLAUTH_NONE) {
00093 fprintf(stderr, "auth schemes not found on commandline\n");
00094 return TEST_ERR_MAJOR_BAD;
00095 }
00096
00097 if(curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
00098 fprintf(stderr, "curl_global_init() failed\n");
00099 return TEST_ERR_MAJOR_BAD;
00100 }
00101
00102
00103
00104 curl = curl_easy_init();
00105 if(!curl) {
00106 fprintf(stderr, "curl_easy_init() failed\n");
00107 curl_global_cleanup();
00108 return TEST_ERR_MAJOR_BAD;
00109 }
00110
00111 res = send_wrong_password(curl, url, 100, main_auth_scheme);
00112 if(res != CURLE_OK)
00113 goto test_cleanup;
00114 curl_easy_reset(curl);
00115
00116 res = send_right_password(curl, url, 200, fallback_auth_scheme);
00117 if(res != CURLE_OK)
00118 goto test_cleanup;
00119 curl_easy_reset(curl);
00120
00121 curl_easy_cleanup(curl);
00122
00123
00124 curl = curl_easy_init();
00125 if(!curl) {
00126 fprintf(stderr, "curl_easy_init() failed\n");
00127 curl_global_cleanup();
00128 return TEST_ERR_MAJOR_BAD;
00129 }
00130
00131 res = send_wrong_password(curl, url, 300, main_auth_scheme);
00132 if(res != CURLE_OK)
00133 goto test_cleanup;
00134 curl_easy_reset(curl);
00135
00136 res = send_wrong_password(curl, url, 400, fallback_auth_scheme);
00137 if(res != CURLE_OK)
00138 goto test_cleanup;
00139 curl_easy_reset(curl);
00140
00141 res = send_right_password(curl, url, 500, fallback_auth_scheme);
00142 if(res != CURLE_OK)
00143 goto test_cleanup;
00144 curl_easy_reset(curl);
00145
00146 test_cleanup:
00147
00148 curl_easy_cleanup(curl);
00149 curl_global_cleanup();
00150
00151 return (int)res;
00152 }
00153