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