diff options
Diffstat (limited to 'src/testcurl/test_digestauth_sha256.c')
-rw-r--r-- | src/testcurl/test_digestauth_sha256.c | 159 |
1 files changed, 81 insertions, 78 deletions
diff --git a/src/testcurl/test_digestauth_sha256.c b/src/testcurl/test_digestauth_sha256.c index c3430283..3482d14d 100644 --- a/src/testcurl/test_digestauth_sha256.c +++ b/src/testcurl/test_digestauth_sha256.c | |||
@@ -45,9 +45,11 @@ | |||
45 | #include <wincrypt.h> | 45 | #include <wincrypt.h> |
46 | #endif | 46 | #endif |
47 | 47 | ||
48 | #define PAGE "<html><head><title>libmicrohttpd demo</title></head><body>Access granted</body></html>" | 48 | #define PAGE \ |
49 | "<html><head><title>libmicrohttpd demo</title></head><body>Access granted</body></html>" | ||
49 | 50 | ||
50 | #define DENIED "<html><head><title>libmicrohttpd demo</title></head><body>Access denied</body></html>" | 51 | #define DENIED \ |
52 | "<html><head><title>libmicrohttpd demo</title></head><body>Access denied</body></html>" | ||
51 | 53 | ||
52 | #define MY_OPAQUE "11733b200778ce33060f31c9af70a870ba96ddd4" | 54 | #define MY_OPAQUE "11733b200778ce33060f31c9af70a870ba96ddd4" |
53 | 55 | ||
@@ -90,26 +92,26 @@ ahc_echo (void *cls, | |||
90 | const char *password = "testpass"; | 92 | const char *password = "testpass"; |
91 | const char *realm = "test@example.com"; | 93 | const char *realm = "test@example.com"; |
92 | int ret; | 94 | int ret; |
93 | (void)cls;(void)url; /* Unused. Silent compiler warning. */ | 95 | (void) cls; (void) url; /* Unused. Silent compiler warning. */ |
94 | (void)method;(void)version;(void)upload_data; /* Unused. Silent compiler warning. */ | 96 | (void) method; (void) version; (void) upload_data; /* Unused. Silent compiler warning. */ |
95 | (void)upload_data_size;(void)unused; /* Unused. Silent compiler warning. */ | 97 | (void) upload_data_size; (void) unused; /* Unused. Silent compiler warning. */ |
96 | 98 | ||
97 | username = MHD_digest_auth_get_username (connection); | 99 | username = MHD_digest_auth_get_username (connection); |
98 | if ( (username == NULL) || | 100 | if ( (username == NULL) || |
99 | (0 != strcmp (username, "testuser")) ) | 101 | (0 != strcmp (username, "testuser")) ) |
100 | { | 102 | { |
101 | response = MHD_create_response_from_buffer (strlen (DENIED), | 103 | response = MHD_create_response_from_buffer (strlen (DENIED), |
102 | DENIED, | 104 | DENIED, |
103 | MHD_RESPMEM_PERSISTENT); | 105 | MHD_RESPMEM_PERSISTENT); |
104 | ret = MHD_queue_auth_fail_response2 (connection, | 106 | ret = MHD_queue_auth_fail_response2 (connection, |
105 | realm, | 107 | realm, |
106 | MY_OPAQUE, | 108 | MY_OPAQUE, |
107 | response, | 109 | response, |
108 | MHD_NO, | 110 | MHD_NO, |
109 | MHD_DIGEST_ALG_SHA256); | 111 | MHD_DIGEST_ALG_SHA256); |
110 | MHD_destroy_response(response); | 112 | MHD_destroy_response (response); |
111 | return ret; | 113 | return ret; |
112 | } | 114 | } |
113 | ret = MHD_digest_auth_check2 (connection, | 115 | ret = MHD_digest_auth_check2 (connection, |
114 | realm, | 116 | realm, |
115 | username, | 117 | username, |
@@ -119,22 +121,23 @@ ahc_echo (void *cls, | |||
119 | free (username); | 121 | free (username); |
120 | if ( (ret == MHD_INVALID_NONCE) || | 122 | if ( (ret == MHD_INVALID_NONCE) || |
121 | (ret == MHD_NO) ) | 123 | (ret == MHD_NO) ) |
122 | { | 124 | { |
123 | response = MHD_create_response_from_buffer (strlen (DENIED), | 125 | response = MHD_create_response_from_buffer (strlen (DENIED), |
124 | DENIED, | 126 | DENIED, |
125 | MHD_RESPMEM_PERSISTENT); | 127 | MHD_RESPMEM_PERSISTENT); |
126 | if (NULL == response) | 128 | if (NULL == response) |
127 | return MHD_NO; | 129 | return MHD_NO; |
128 | ret = MHD_queue_auth_fail_response2 (connection, | 130 | ret = MHD_queue_auth_fail_response2 (connection, |
129 | realm, | 131 | realm, |
130 | MY_OPAQUE, | 132 | MY_OPAQUE, |
131 | response, | 133 | response, |
132 | (MHD_INVALID_NONCE == ret) ? MHD_YES : MHD_NO, | 134 | (MHD_INVALID_NONCE == ret) ? MHD_YES : |
133 | MHD_DIGEST_ALG_SHA256); | 135 | MHD_NO, |
134 | MHD_destroy_response(response); | 136 | MHD_DIGEST_ALG_SHA256); |
135 | return ret; | 137 | MHD_destroy_response (response); |
136 | } | 138 | return ret; |
137 | response = MHD_create_response_from_buffer (strlen(PAGE), | 139 | } |
140 | response = MHD_create_response_from_buffer (strlen (PAGE), | ||
138 | PAGE, | 141 | PAGE, |
139 | MHD_RESPMEM_PERSISTENT); | 142 | MHD_RESPMEM_PERSISTENT); |
140 | ret = MHD_queue_response (connection, | 143 | ret = MHD_queue_response (connection, |
@@ -174,30 +177,30 @@ testDigestAuth () | |||
174 | fd = open ("/dev/urandom", | 177 | fd = open ("/dev/urandom", |
175 | O_RDONLY); | 178 | O_RDONLY); |
176 | if (-1 == fd) | 179 | if (-1 == fd) |
180 | { | ||
181 | fprintf (stderr, | ||
182 | "Failed to open `%s': %s\n", | ||
183 | "/dev/urandom", | ||
184 | strerror (errno)); | ||
185 | return 1; | ||
186 | } | ||
187 | while (off < 8) | ||
188 | { | ||
189 | len = read (fd, | ||
190 | rnd, | ||
191 | 8); | ||
192 | if (len == (size_t) -1) | ||
177 | { | 193 | { |
178 | fprintf (stderr, | 194 | fprintf (stderr, |
179 | "Failed to open `%s': %s\n", | 195 | "Failed to read `%s': %s\n", |
180 | "/dev/urandom", | 196 | "/dev/urandom", |
181 | strerror(errno)); | 197 | strerror (errno)); |
198 | (void) close (fd); | ||
182 | return 1; | 199 | return 1; |
183 | } | 200 | } |
184 | while (off < 8) | 201 | off += len; |
185 | { | 202 | } |
186 | len = read (fd, | 203 | (void) close (fd); |
187 | rnd, | ||
188 | 8); | ||
189 | if (len == (size_t)-1) | ||
190 | { | ||
191 | fprintf (stderr, | ||
192 | "Failed to read `%s': %s\n", | ||
193 | "/dev/urandom", | ||
194 | strerror(errno)); | ||
195 | (void) close(fd); | ||
196 | return 1; | ||
197 | } | ||
198 | off += len; | ||
199 | } | ||
200 | (void) close(fd); | ||
201 | #else | 204 | #else |
202 | { | 205 | { |
203 | HCRYPTPROV cc; | 206 | HCRYPTPROV cc; |
@@ -215,7 +218,7 @@ testDigestAuth () | |||
215 | GetLastError ()); | 218 | GetLastError ()); |
216 | return 1; | 219 | return 1; |
217 | } | 220 | } |
218 | b = CryptGenRandom (cc, 8, (BYTE*)rnd); | 221 | b = CryptGenRandom (cc, 8, (BYTE*) rnd); |
219 | if (b == 0) | 222 | if (b == 0) |
220 | { | 223 | { |
221 | fprintf (stderr, | 224 | fprintf (stderr, |
@@ -230,25 +233,25 @@ testDigestAuth () | |||
230 | d = MHD_start_daemon (MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG, | 233 | d = MHD_start_daemon (MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG, |
231 | port, NULL, NULL, | 234 | port, NULL, NULL, |
232 | &ahc_echo, PAGE, | 235 | &ahc_echo, PAGE, |
233 | MHD_OPTION_DIGEST_AUTH_RANDOM, sizeof (rnd), rnd, | 236 | MHD_OPTION_DIGEST_AUTH_RANDOM, sizeof (rnd), rnd, |
234 | MHD_OPTION_NONCE_NC_SIZE, 300, | 237 | MHD_OPTION_NONCE_NC_SIZE, 300, |
235 | MHD_OPTION_END); | 238 | MHD_OPTION_END); |
236 | if (d == NULL) | 239 | if (d == NULL) |
237 | return 1; | 240 | return 1; |
238 | if (0 == port) | 241 | if (0 == port) |
239 | { | 242 | { |
240 | const union MHD_DaemonInfo *dinfo; | 243 | const union MHD_DaemonInfo *dinfo; |
241 | 244 | ||
242 | dinfo = MHD_get_daemon_info (d, | 245 | dinfo = MHD_get_daemon_info (d, |
243 | MHD_DAEMON_INFO_BIND_PORT); | 246 | MHD_DAEMON_INFO_BIND_PORT); |
244 | if ( (NULL == dinfo) || | 247 | if ( (NULL == dinfo) || |
245 | (0 == dinfo->port) ) | 248 | (0 == dinfo->port) ) |
246 | { | 249 | { |
247 | MHD_stop_daemon (d); | 250 | MHD_stop_daemon (d); |
248 | return 32; | 251 | return 32; |
249 | } | ||
250 | port = (int)dinfo->port; | ||
251 | } | 252 | } |
253 | port = (int) dinfo->port; | ||
254 | } | ||
252 | snprintf (url, | 255 | snprintf (url, |
253 | sizeof (url), | 256 | sizeof (url), |
254 | "http://127.0.0.1:%d/bar%%20foo%%3Fkey%%3Dvalue", | 257 | "http://127.0.0.1:%d/bar%%20foo%%3Fkey%%3Dvalue", |
@@ -268,14 +271,14 @@ testDigestAuth () | |||
268 | crashes on my system!*/ | 271 | crashes on my system!*/ |
269 | curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1L); | 272 | curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1L); |
270 | if (CURLE_OK != (errornum = curl_easy_perform (c))) | 273 | if (CURLE_OK != (errornum = curl_easy_perform (c))) |
271 | { | 274 | { |
272 | fprintf (stderr, | 275 | fprintf (stderr, |
273 | "curl_easy_perform failed: `%s'\n", | 276 | "curl_easy_perform failed: `%s'\n", |
274 | curl_easy_strerror (errornum)); | 277 | curl_easy_strerror (errornum)); |
275 | curl_easy_cleanup (c); | 278 | curl_easy_cleanup (c); |
276 | MHD_stop_daemon (d); | 279 | MHD_stop_daemon (d); |
277 | return 2; | 280 | return 2; |
278 | } | 281 | } |
279 | curl_easy_cleanup (c); | 282 | curl_easy_cleanup (c); |
280 | MHD_stop_daemon (d); | 283 | MHD_stop_daemon (d); |
281 | if (cbc.pos != strlen (PAGE)) | 284 | if (cbc.pos != strlen (PAGE)) |
@@ -291,7 +294,7 @@ main (int argc, char *const *argv) | |||
291 | { | 294 | { |
292 | unsigned int errorCount = 0; | 295 | unsigned int errorCount = 0; |
293 | curl_version_info_data *d = curl_version_info (CURLVERSION_NOW); | 296 | curl_version_info_data *d = curl_version_info (CURLVERSION_NOW); |
294 | (void)argc; (void)argv; /* Unused. Silent compiler warning. */ | 297 | (void) argc; (void) argv; /* Unused. Silent compiler warning. */ |
295 | 298 | ||
296 | /* curl added SHA256 support in 7.57 = 7.0x39 */ | 299 | /* curl added SHA256 support in 7.57 = 7.0x39 */ |
297 | if (d->version_num < 0x073900) | 300 | if (d->version_num < 0x073900) |
@@ -304,7 +307,7 @@ main (int argc, char *const *argv) | |||
304 | #endif | 307 | #endif |
305 | #endif | 308 | #endif |
306 | #endif /* MHD_HTTPS_REQUIRE_GRYPT */ | 309 | #endif /* MHD_HTTPS_REQUIRE_GRYPT */ |
307 | if (0 != curl_global_init (CURL_GLOBAL_WIN32)) | 310 | if (0 != curl_global_init (CURL_GLOBAL_WIN32)) |
308 | return 2; | 311 | return 2; |
309 | errorCount += testDigestAuth (); | 312 | errorCount += testDigestAuth (); |
310 | if (errorCount != 0) | 313 | if (errorCount != 0) |