diff options
Diffstat (limited to 'src/testcurl/test_digestauth.c')
-rw-r--r-- | src/testcurl/test_digestauth.c | 157 |
1 files changed, 80 insertions, 77 deletions
diff --git a/src/testcurl/test_digestauth.c b/src/testcurl/test_digestauth.c index b0a39bf7..4de006b7 100644 --- a/src/testcurl/test_digestauth.c +++ b/src/testcurl/test_digestauth.c | |||
@@ -44,9 +44,11 @@ | |||
44 | #include <wincrypt.h> | 44 | #include <wincrypt.h> |
45 | #endif | 45 | #endif |
46 | 46 | ||
47 | #define PAGE "<html><head><title>libmicrohttpd demo</title></head><body>Access granted</body></html>" | 47 | #define PAGE \ |
48 | "<html><head><title>libmicrohttpd demo</title></head><body>Access granted</body></html>" | ||
48 | 49 | ||
49 | #define DENIED "<html><head><title>libmicrohttpd demo</title></head><body>Access denied</body></html>" | 50 | #define DENIED \ |
51 | "<html><head><title>libmicrohttpd demo</title></head><body>Access denied</body></html>" | ||
50 | 52 | ||
51 | #define MY_OPAQUE "11733b200778ce33060f31c9af70a870ba96ddd4" | 53 | #define MY_OPAQUE "11733b200778ce33060f31c9af70a870ba96ddd4" |
52 | 54 | ||
@@ -89,48 +91,49 @@ ahc_echo (void *cls, | |||
89 | const char *password = "testpass"; | 91 | const char *password = "testpass"; |
90 | const char *realm = "test@example.com"; | 92 | const char *realm = "test@example.com"; |
91 | int ret; | 93 | int ret; |
92 | (void)cls;(void)url; /* Unused. Silent compiler warning. */ | 94 | (void) cls; (void) url; /* Unused. Silent compiler warning. */ |
93 | (void)method;(void)version;(void)upload_data; /* Unused. Silent compiler warning. */ | 95 | (void) method; (void) version; (void) upload_data; /* Unused. Silent compiler warning. */ |
94 | (void)upload_data_size;(void)unused; /* Unused. Silent compiler warning. */ | 96 | (void) upload_data_size; (void) unused; /* Unused. Silent compiler warning. */ |
95 | 97 | ||
96 | username = MHD_digest_auth_get_username (connection); | 98 | username = MHD_digest_auth_get_username (connection); |
97 | if ( (username == NULL) || | 99 | if ( (username == NULL) || |
98 | (0 != strcmp (username, "testuser")) ) | 100 | (0 != strcmp (username, "testuser")) ) |
99 | { | 101 | { |
100 | response = MHD_create_response_from_buffer (strlen (DENIED), | 102 | response = MHD_create_response_from_buffer (strlen (DENIED), |
101 | DENIED, | 103 | DENIED, |
102 | MHD_RESPMEM_PERSISTENT); | 104 | MHD_RESPMEM_PERSISTENT); |
103 | ret = MHD_queue_auth_fail_response (connection, | 105 | ret = MHD_queue_auth_fail_response (connection, |
104 | realm, | 106 | realm, |
105 | MY_OPAQUE, | 107 | MY_OPAQUE, |
106 | response, | 108 | response, |
107 | MHD_NO); | 109 | MHD_NO); |
108 | MHD_destroy_response(response); | 110 | MHD_destroy_response (response); |
109 | return ret; | 111 | return ret; |
110 | } | 112 | } |
111 | ret = MHD_digest_auth_check (connection, | 113 | ret = MHD_digest_auth_check (connection, |
112 | realm, | 114 | realm, |
113 | username, | 115 | username, |
114 | password, | 116 | password, |
115 | 300); | 117 | 300); |
116 | free (username); | 118 | free (username); |
117 | if ( (ret == MHD_INVALID_NONCE) || | 119 | if ( (ret == MHD_INVALID_NONCE) || |
118 | (ret == MHD_NO) ) | 120 | (ret == MHD_NO) ) |
119 | { | 121 | { |
120 | response = MHD_create_response_from_buffer (strlen (DENIED), | 122 | response = MHD_create_response_from_buffer (strlen (DENIED), |
121 | DENIED, | 123 | DENIED, |
122 | MHD_RESPMEM_PERSISTENT); | 124 | MHD_RESPMEM_PERSISTENT); |
123 | if (NULL == response) | 125 | if (NULL == response) |
124 | return MHD_NO; | 126 | return MHD_NO; |
125 | ret = MHD_queue_auth_fail_response (connection, | 127 | ret = MHD_queue_auth_fail_response (connection, |
126 | realm, | 128 | realm, |
127 | MY_OPAQUE, | 129 | MY_OPAQUE, |
128 | response, | 130 | response, |
129 | (MHD_INVALID_NONCE == ret) ? MHD_YES : MHD_NO); | 131 | (MHD_INVALID_NONCE == ret) ? MHD_YES : |
130 | MHD_destroy_response(response); | 132 | MHD_NO); |
131 | return ret; | 133 | MHD_destroy_response (response); |
132 | } | 134 | return ret; |
133 | response = MHD_create_response_from_buffer (strlen(PAGE), | 135 | } |
136 | response = MHD_create_response_from_buffer (strlen (PAGE), | ||
134 | PAGE, | 137 | PAGE, |
135 | MHD_RESPMEM_PERSISTENT); | 138 | MHD_RESPMEM_PERSISTENT); |
136 | ret = MHD_queue_response (connection, | 139 | ret = MHD_queue_response (connection, |
@@ -170,30 +173,30 @@ testDigestAuth () | |||
170 | fd = open ("/dev/urandom", | 173 | fd = open ("/dev/urandom", |
171 | O_RDONLY); | 174 | O_RDONLY); |
172 | if (-1 == fd) | 175 | if (-1 == fd) |
176 | { | ||
177 | fprintf (stderr, | ||
178 | "Failed to open `%s': %s\n", | ||
179 | "/dev/urandom", | ||
180 | strerror (errno)); | ||
181 | return 1; | ||
182 | } | ||
183 | while (off < 8) | ||
184 | { | ||
185 | len = read (fd, | ||
186 | rnd, | ||
187 | 8); | ||
188 | if (len == (size_t) -1) | ||
173 | { | 189 | { |
174 | fprintf (stderr, | 190 | fprintf (stderr, |
175 | "Failed to open `%s': %s\n", | 191 | "Failed to read `%s': %s\n", |
176 | "/dev/urandom", | 192 | "/dev/urandom", |
177 | strerror(errno)); | 193 | strerror (errno)); |
194 | (void) close (fd); | ||
178 | return 1; | 195 | return 1; |
179 | } | 196 | } |
180 | while (off < 8) | 197 | off += len; |
181 | { | 198 | } |
182 | len = read (fd, | 199 | (void) close (fd); |
183 | rnd, | ||
184 | 8); | ||
185 | if (len == (size_t)-1) | ||
186 | { | ||
187 | fprintf (stderr, | ||
188 | "Failed to read `%s': %s\n", | ||
189 | "/dev/urandom", | ||
190 | strerror(errno)); | ||
191 | (void) close(fd); | ||
192 | return 1; | ||
193 | } | ||
194 | off += len; | ||
195 | } | ||
196 | (void) close(fd); | ||
197 | #else | 200 | #else |
198 | { | 201 | { |
199 | HCRYPTPROV cc; | 202 | HCRYPTPROV cc; |
@@ -211,7 +214,7 @@ testDigestAuth () | |||
211 | GetLastError ()); | 214 | GetLastError ()); |
212 | return 1; | 215 | return 1; |
213 | } | 216 | } |
214 | b = CryptGenRandom (cc, 8, (BYTE*)rnd); | 217 | b = CryptGenRandom (cc, 8, (BYTE*) rnd); |
215 | if (b == 0) | 218 | if (b == 0) |
216 | { | 219 | { |
217 | fprintf (stderr, | 220 | fprintf (stderr, |
@@ -226,25 +229,25 @@ testDigestAuth () | |||
226 | d = MHD_start_daemon (MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG, | 229 | d = MHD_start_daemon (MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG, |
227 | port, NULL, NULL, | 230 | port, NULL, NULL, |
228 | &ahc_echo, PAGE, | 231 | &ahc_echo, PAGE, |
229 | MHD_OPTION_DIGEST_AUTH_RANDOM, sizeof (rnd), rnd, | 232 | MHD_OPTION_DIGEST_AUTH_RANDOM, sizeof (rnd), rnd, |
230 | MHD_OPTION_NONCE_NC_SIZE, 300, | 233 | MHD_OPTION_NONCE_NC_SIZE, 300, |
231 | MHD_OPTION_END); | 234 | MHD_OPTION_END); |
232 | if (d == NULL) | 235 | if (d == NULL) |
233 | return 1; | 236 | return 1; |
234 | if (0 == port) | 237 | if (0 == port) |
235 | { | 238 | { |
236 | const union MHD_DaemonInfo *dinfo; | 239 | const union MHD_DaemonInfo *dinfo; |
237 | 240 | ||
238 | dinfo = MHD_get_daemon_info (d, | 241 | dinfo = MHD_get_daemon_info (d, |
239 | MHD_DAEMON_INFO_BIND_PORT); | 242 | MHD_DAEMON_INFO_BIND_PORT); |
240 | if ( (NULL == dinfo) || | 243 | if ( (NULL == dinfo) || |
241 | (0 == dinfo->port) ) | 244 | (0 == dinfo->port) ) |
242 | { | 245 | { |
243 | MHD_stop_daemon (d); | 246 | MHD_stop_daemon (d); |
244 | return 32; | 247 | return 32; |
245 | } | ||
246 | port = (int)dinfo->port; | ||
247 | } | 248 | } |
249 | port = (int) dinfo->port; | ||
250 | } | ||
248 | snprintf (url, | 251 | snprintf (url, |
249 | sizeof (url), | 252 | sizeof (url), |
250 | "http://127.0.0.1:%d/bar%%20foo%%3Fkey%%3Dvalue", | 253 | "http://127.0.0.1:%d/bar%%20foo%%3Fkey%%3Dvalue", |
@@ -264,14 +267,14 @@ testDigestAuth () | |||
264 | crashes on my system!*/ | 267 | crashes on my system!*/ |
265 | curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1L); | 268 | curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1L); |
266 | if (CURLE_OK != (errornum = curl_easy_perform (c))) | 269 | if (CURLE_OK != (errornum = curl_easy_perform (c))) |
267 | { | 270 | { |
268 | fprintf (stderr, | 271 | fprintf (stderr, |
269 | "curl_easy_perform failed: `%s'\n", | 272 | "curl_easy_perform failed: `%s'\n", |
270 | curl_easy_strerror (errornum)); | 273 | curl_easy_strerror (errornum)); |
271 | curl_easy_cleanup (c); | 274 | curl_easy_cleanup (c); |
272 | MHD_stop_daemon (d); | 275 | MHD_stop_daemon (d); |
273 | return 2; | 276 | return 2; |
274 | } | 277 | } |
275 | curl_easy_cleanup (c); | 278 | curl_easy_cleanup (c); |
276 | MHD_stop_daemon (d); | 279 | MHD_stop_daemon (d); |
277 | if (cbc.pos != strlen (PAGE)) | 280 | if (cbc.pos != strlen (PAGE)) |
@@ -286,7 +289,7 @@ int | |||
286 | main (int argc, char *const *argv) | 289 | main (int argc, char *const *argv) |
287 | { | 290 | { |
288 | unsigned int errorCount = 0; | 291 | unsigned int errorCount = 0; |
289 | (void)argc; (void)argv; /* Unused. Silent compiler warning. */ | 292 | (void) argc; (void) argv; /* Unused. Silent compiler warning. */ |
290 | 293 | ||
291 | #ifdef MHD_HTTPS_REQUIRE_GRYPT | 294 | #ifdef MHD_HTTPS_REQUIRE_GRYPT |
292 | #ifdef HAVE_GCRYPT_H | 295 | #ifdef HAVE_GCRYPT_H |
@@ -296,7 +299,7 @@ main (int argc, char *const *argv) | |||
296 | #endif | 299 | #endif |
297 | #endif | 300 | #endif |
298 | #endif /* MHD_HTTPS_REQUIRE_GRYPT */ | 301 | #endif /* MHD_HTTPS_REQUIRE_GRYPT */ |
299 | if (0 != curl_global_init (CURL_GLOBAL_WIN32)) | 302 | if (0 != curl_global_init (CURL_GLOBAL_WIN32)) |
300 | return 2; | 303 | return 2; |
301 | errorCount += testDigestAuth (); | 304 | errorCount += testDigestAuth (); |
302 | if (errorCount != 0) | 305 | if (errorCount != 0) |