diff options
Diffstat (limited to 'src/testcurl/test_digestauth_with_arguments.c')
-rw-r--r-- | src/testcurl/test_digestauth_with_arguments.c | 164 |
1 files changed, 85 insertions, 79 deletions
diff --git a/src/testcurl/test_digestauth_with_arguments.c b/src/testcurl/test_digestauth_with_arguments.c index 3f2852c0..5bacdc76 100644 --- a/src/testcurl/test_digestauth_with_arguments.c +++ b/src/testcurl/test_digestauth_with_arguments.c | |||
@@ -43,9 +43,11 @@ | |||
43 | #include <wincrypt.h> | 43 | #include <wincrypt.h> |
44 | #endif | 44 | #endif |
45 | 45 | ||
46 | #define PAGE "<html><head><title>libmicrohttpd demo</title></head><body>Access granted</body></html>" | 46 | #define PAGE \ |
47 | "<html><head><title>libmicrohttpd demo</title></head><body>Access granted</body></html>" | ||
47 | 48 | ||
48 | #define DENIED "<html><head><title>libmicrohttpd demo</title></head><body>Access denied</body></html>" | 49 | #define DENIED \ |
50 | "<html><head><title>libmicrohttpd demo</title></head><body>Access denied</body></html>" | ||
49 | 51 | ||
50 | #define MY_OPAQUE "11733b200778ce33060f31c9af70a870ba96ddd4" | 52 | #define MY_OPAQUE "11733b200778ce33060f31c9af70a870ba96ddd4" |
51 | 53 | ||
@@ -82,48 +84,49 @@ ahc_echo (void *cls, | |||
82 | const char *password = "testpass"; | 84 | const char *password = "testpass"; |
83 | const char *realm = "test@example.com"; | 85 | const char *realm = "test@example.com"; |
84 | int ret; | 86 | int ret; |
85 | (void)cls;(void)url; /* Unused. Silent compiler warning. */ | 87 | (void) cls; (void) url; /* Unused. Silent compiler warning. */ |
86 | (void)method;(void)version;(void)upload_data; /* Unused. Silent compiler warning. */ | 88 | (void) method; (void) version; (void) upload_data; /* Unused. Silent compiler warning. */ |
87 | (void)upload_data_size;(void)unused; /* Unused. Silent compiler warning. */ | 89 | (void) upload_data_size; (void) unused; /* Unused. Silent compiler warning. */ |
88 | 90 | ||
89 | username = MHD_digest_auth_get_username(connection); | 91 | username = MHD_digest_auth_get_username (connection); |
90 | if ( (username == NULL) || | 92 | if ( (username == NULL) || |
91 | (0 != strcmp (username, "testuser")) ) | 93 | (0 != strcmp (username, "testuser")) ) |
92 | { | 94 | { |
93 | response = MHD_create_response_from_buffer(strlen (DENIED), | 95 | response = MHD_create_response_from_buffer (strlen (DENIED), |
94 | DENIED, | 96 | DENIED, |
95 | MHD_RESPMEM_PERSISTENT); | 97 | MHD_RESPMEM_PERSISTENT); |
96 | ret = MHD_queue_auth_fail_response(connection, realm, | 98 | ret = MHD_queue_auth_fail_response (connection, realm, |
97 | MY_OPAQUE, | 99 | MY_OPAQUE, |
98 | response, | 100 | response, |
99 | MHD_NO); | 101 | MHD_NO); |
100 | MHD_destroy_response(response); | 102 | MHD_destroy_response (response); |
101 | return ret; | 103 | return ret; |
102 | } | 104 | } |
103 | ret = MHD_digest_auth_check(connection, realm, | 105 | ret = MHD_digest_auth_check (connection, realm, |
104 | username, | 106 | username, |
105 | password, | 107 | password, |
106 | 300); | 108 | 300); |
107 | free(username); | 109 | free (username); |
108 | if ( (ret == MHD_INVALID_NONCE) || | 110 | if ( (ret == MHD_INVALID_NONCE) || |
109 | (ret == MHD_NO) ) | 111 | (ret == MHD_NO) ) |
110 | { | 112 | { |
111 | response = MHD_create_response_from_buffer(strlen (DENIED), | 113 | response = MHD_create_response_from_buffer (strlen (DENIED), |
112 | DENIED, | 114 | DENIED, |
113 | MHD_RESPMEM_PERSISTENT); | 115 | MHD_RESPMEM_PERSISTENT); |
114 | if (NULL == response) | 116 | if (NULL == response) |
115 | return MHD_NO; | 117 | return MHD_NO; |
116 | ret = MHD_queue_auth_fail_response(connection, realm, | 118 | ret = MHD_queue_auth_fail_response (connection, realm, |
117 | MY_OPAQUE, | 119 | MY_OPAQUE, |
118 | response, | 120 | response, |
119 | (ret == MHD_INVALID_NONCE) ? MHD_YES : MHD_NO); | 121 | (ret == MHD_INVALID_NONCE) ? MHD_YES : |
120 | MHD_destroy_response(response); | 122 | MHD_NO); |
121 | return ret; | 123 | MHD_destroy_response (response); |
122 | } | 124 | return ret; |
123 | response = MHD_create_response_from_buffer(strlen(PAGE), PAGE, | 125 | } |
124 | MHD_RESPMEM_PERSISTENT); | 126 | response = MHD_create_response_from_buffer (strlen (PAGE), PAGE, |
125 | ret = MHD_queue_response(connection, MHD_HTTP_OK, response); | 127 | MHD_RESPMEM_PERSISTENT); |
126 | MHD_destroy_response(response); | 128 | ret = MHD_queue_response (connection, MHD_HTTP_OK, response); |
129 | MHD_destroy_response (response); | ||
127 | return ret; | 130 | return ret; |
128 | } | 131 | } |
129 | 132 | ||
@@ -154,45 +157,46 @@ testDigestAuth () | |||
154 | cbc.size = 2048; | 157 | cbc.size = 2048; |
155 | cbc.pos = 0; | 158 | cbc.pos = 0; |
156 | #ifndef WINDOWS | 159 | #ifndef WINDOWS |
157 | fd = open("/dev/urandom", O_RDONLY); | 160 | fd = open ("/dev/urandom", O_RDONLY); |
158 | if (-1 == fd) | 161 | if (-1 == fd) |
159 | { | 162 | { |
160 | fprintf(stderr, "Failed to open `%s': %s\n", | 163 | fprintf (stderr, "Failed to open `%s': %s\n", |
161 | "/dev/urandom", | 164 | "/dev/urandom", |
162 | strerror(errno)); | 165 | strerror (errno)); |
163 | return 1; | 166 | return 1; |
164 | } | 167 | } |
165 | while (off < 8) | 168 | while (off < 8) |
169 | { | ||
170 | len = read (fd, rnd, 8); | ||
171 | if (len == (size_t) -1) | ||
166 | { | 172 | { |
167 | len = read(fd, rnd, 8); | 173 | fprintf (stderr, |
168 | if (len == (size_t)-1) | 174 | "Failed to read `%s': %s\n", |
169 | { | 175 | "/dev/urandom", |
170 | fprintf(stderr, | 176 | strerror (errno)); |
171 | "Failed to read `%s': %s\n", | 177 | (void) close (fd); |
172 | "/dev/urandom", | 178 | return 1; |
173 | strerror(errno)); | ||
174 | (void) close(fd); | ||
175 | return 1; | ||
176 | } | ||
177 | off += len; | ||
178 | } | 179 | } |
179 | (void) close(fd); | 180 | off += len; |
181 | } | ||
182 | (void) close (fd); | ||
180 | #else | 183 | #else |
181 | { | 184 | { |
182 | HCRYPTPROV cc; | 185 | HCRYPTPROV cc; |
183 | BOOL b; | 186 | BOOL b; |
184 | b = CryptAcquireContext (&cc, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT); | 187 | b = CryptAcquireContext (&cc, NULL, NULL, PROV_RSA_FULL, |
188 | CRYPT_VERIFYCONTEXT); | ||
185 | if (b == 0) | 189 | if (b == 0) |
186 | { | 190 | { |
187 | fprintf (stderr, "Failed to acquire crypto provider context: %lu\n", | 191 | fprintf (stderr, "Failed to acquire crypto provider context: %lu\n", |
188 | GetLastError ()); | 192 | GetLastError ()); |
189 | return 1; | 193 | return 1; |
190 | } | 194 | } |
191 | b = CryptGenRandom (cc, 8, (BYTE*)rnd); | 195 | b = CryptGenRandom (cc, 8, (BYTE*) rnd); |
192 | if (b == 0) | 196 | if (b == 0) |
193 | { | 197 | { |
194 | fprintf (stderr, "Failed to generate 8 random bytes: %lu\n", | 198 | fprintf (stderr, "Failed to generate 8 random bytes: %lu\n", |
195 | GetLastError ()); | 199 | GetLastError ()); |
196 | } | 200 | } |
197 | CryptReleaseContext (cc, 0); | 201 | CryptReleaseContext (cc, 0); |
198 | if (b == 0) | 202 | if (b == 0) |
@@ -201,19 +205,21 @@ testDigestAuth () | |||
201 | #endif | 205 | #endif |
202 | d = MHD_start_daemon (MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG, | 206 | d = MHD_start_daemon (MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG, |
203 | port, NULL, NULL, &ahc_echo, PAGE, | 207 | port, NULL, NULL, &ahc_echo, PAGE, |
204 | MHD_OPTION_DIGEST_AUTH_RANDOM, sizeof (rnd), rnd, | 208 | MHD_OPTION_DIGEST_AUTH_RANDOM, sizeof (rnd), rnd, |
205 | MHD_OPTION_NONCE_NC_SIZE, 300, | 209 | MHD_OPTION_NONCE_NC_SIZE, 300, |
206 | MHD_OPTION_END); | 210 | MHD_OPTION_END); |
207 | if (d == NULL) | 211 | if (d == NULL) |
208 | return 1; | 212 | return 1; |
209 | if (0 == port) | 213 | if (0 == port) |
214 | { | ||
215 | const union MHD_DaemonInfo *dinfo; | ||
216 | dinfo = MHD_get_daemon_info (d, MHD_DAEMON_INFO_BIND_PORT); | ||
217 | if ((NULL == dinfo) ||(0 == dinfo->port) ) | ||
210 | { | 218 | { |
211 | const union MHD_DaemonInfo *dinfo; | 219 | MHD_stop_daemon (d); return 32; |
212 | dinfo = MHD_get_daemon_info (d, MHD_DAEMON_INFO_BIND_PORT); | ||
213 | if (NULL == dinfo || 0 == dinfo->port) | ||
214 | { MHD_stop_daemon (d); return 32; } | ||
215 | port = (int)dinfo->port; | ||
216 | } | 220 | } |
221 | port = (int) dinfo->port; | ||
222 | } | ||
217 | snprintf (url, | 223 | snprintf (url, |
218 | sizeof (url), | 224 | sizeof (url), |
219 | "http://127.0.0.1:%d/bar%%20foo%%3Fkey%%3Dvalue", | 225 | "http://127.0.0.1:%d/bar%%20foo%%3Fkey%%3Dvalue", |
@@ -233,14 +239,14 @@ testDigestAuth () | |||
233 | crashes on my system!*/ | 239 | crashes on my system!*/ |
234 | curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1L); | 240 | curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1L); |
235 | if (CURLE_OK != (errornum = curl_easy_perform (c))) | 241 | if (CURLE_OK != (errornum = curl_easy_perform (c))) |
236 | { | 242 | { |
237 | fprintf (stderr, | 243 | fprintf (stderr, |
238 | "curl_easy_perform failed: `%s'\n", | 244 | "curl_easy_perform failed: `%s'\n", |
239 | curl_easy_strerror (errornum)); | 245 | curl_easy_strerror (errornum)); |
240 | curl_easy_cleanup (c); | 246 | curl_easy_cleanup (c); |
241 | MHD_stop_daemon (d); | 247 | MHD_stop_daemon (d); |
242 | return 2; | 248 | return 2; |
243 | } | 249 | } |
244 | curl_easy_cleanup (c); | 250 | curl_easy_cleanup (c); |
245 | MHD_stop_daemon (d); | 251 | MHD_stop_daemon (d); |
246 | if (cbc.pos != strlen (PAGE)) | 252 | if (cbc.pos != strlen (PAGE)) |
@@ -255,7 +261,7 @@ int | |||
255 | main (int argc, char *const *argv) | 261 | main (int argc, char *const *argv) |
256 | { | 262 | { |
257 | unsigned int errorCount = 0; | 263 | unsigned int errorCount = 0; |
258 | (void)argc; (void)argv; /* Unused. Silent compiler warning. */ | 264 | (void) argc; (void) argv; /* Unused. Silent compiler warning. */ |
259 | 265 | ||
260 | #ifdef MHD_HTTPS_REQUIRE_GRYPT | 266 | #ifdef MHD_HTTPS_REQUIRE_GRYPT |
261 | #ifdef HAVE_GCRYPT_H | 267 | #ifdef HAVE_GCRYPT_H |