diff options
Diffstat (limited to 'src/microhttpd/test_auth_parse.c')
-rw-r--r-- | src/microhttpd/test_auth_parse.c | 135 |
1 files changed, 126 insertions, 9 deletions
diff --git a/src/microhttpd/test_auth_parse.c b/src/microhttpd/test_auth_parse.c index baa93702..b9507f97 100644 --- a/src/microhttpd/test_auth_parse.c +++ b/src/microhttpd/test_auth_parse.c | |||
@@ -127,15 +127,24 @@ MHD_connection_alloc_memory_ (struct MHD_Connection *connection, | |||
127 | void *ret; | 127 | void *ret; |
128 | if (NULL == connection) | 128 | if (NULL == connection) |
129 | mhdErrorExitDesc ("'connection' parameter is NULL"); | 129 | mhdErrorExitDesc ("'connection' parameter is NULL"); |
130 | /* Use 'socket_context' just as a flag */ | 130 | /* Use 'read_buffer' just as a flag */ |
131 | if (NULL != connection->socket_context) | 131 | if (NULL != connection->read_buffer) |
132 | mhdErrorExitDesc ("Unexpected memory allocation, " \ | 132 | { |
133 | "while previous allocation was not freed"); | 133 | /* Use 'write_buffer' just as a flag */ |
134 | if (NULL != connection->write_buffer) | ||
135 | mhdErrorExitDesc ("Unexpected third memory allocation, " \ | ||
136 | "while previous allocations was not freed"); | ||
137 | } | ||
134 | /* Just use simple "malloc()" here */ | 138 | /* Just use simple "malloc()" here */ |
135 | ret = malloc (size); | 139 | ret = malloc (size); |
136 | if (NULL == ret) | 140 | if (NULL == ret) |
137 | externalErrorExit (); | 141 | externalErrorExit (); |
138 | connection->socket_context = ret; | 142 | |
143 | /* Track up to two allocations */ | ||
144 | if (NULL == connection->read_buffer) | ||
145 | connection->read_buffer = (char *) ret; | ||
146 | else | ||
147 | connection->write_buffer = (char *) ret; | ||
139 | return ret; | 148 | return ret; |
140 | } | 149 | } |
141 | 150 | ||
@@ -280,7 +289,8 @@ static void | |||
280 | clean_AuthHeaders (void) | 289 | clean_AuthHeaders (void) |
281 | { | 290 | { |
282 | conn.state = MHD_CONNECTION_INIT; | 291 | conn.state = MHD_CONNECTION_INIT; |
283 | free (conn.socket_context); | 292 | free (conn.read_buffer); |
293 | free (conn.write_buffer); | ||
284 | 294 | ||
285 | #ifdef BAUTH_SUPPORT | 295 | #ifdef BAUTH_SUPPORT |
286 | conn.rq.bauth_tried = false; | 296 | conn.rq.bauth_tried = false; |
@@ -290,12 +300,16 @@ clean_AuthHeaders (void) | |||
290 | #endif /* BAUTH_SUPPORT */ | 300 | #endif /* BAUTH_SUPPORT */ |
291 | 301 | ||
292 | #ifdef BAUTH_SUPPORT | 302 | #ifdef BAUTH_SUPPORT |
293 | if ((NULL != conn.rq.bauth) && (conn.socket_context != conn.rq.bauth)) | 303 | if ((NULL != conn.rq.bauth) && |
304 | (conn.read_buffer != (const char *) conn.rq.bauth) && | ||
305 | (conn.write_buffer != (const char *) conn.rq.bauth)) | ||
294 | externalErrorExitDesc ("Memory allocation is not tracked as it should be"); | 306 | externalErrorExitDesc ("Memory allocation is not tracked as it should be"); |
295 | conn.rq.bauth = NULL; | 307 | conn.rq.bauth = NULL; |
296 | #endif /* BAUTH_SUPPORT */ | 308 | #endif /* BAUTH_SUPPORT */ |
297 | #ifdef DAUTH_SUPPORT | 309 | #ifdef DAUTH_SUPPORT |
298 | if ((NULL != conn.rq.dauth) && (conn.socket_context != conn.rq.dauth)) | 310 | if ((NULL != conn.rq.dauth) && |
311 | (conn.read_buffer != (const char *) conn.rq.dauth) && | ||
312 | (conn.write_buffer != (const char *) conn.rq.dauth)) | ||
299 | externalErrorExitDesc ("Memory allocation is not tracked as it should be"); | 313 | externalErrorExitDesc ("Memory allocation is not tracked as it should be"); |
300 | conn.rq.dauth = NULL; | 314 | conn.rq.dauth = NULL; |
301 | #endif /* BAUTH_SUPPORT */ | 315 | #endif /* BAUTH_SUPPORT */ |
@@ -303,7 +317,8 @@ clean_AuthHeaders (void) | |||
303 | conn.rq.headers_received = NULL; | 317 | conn.rq.headers_received = NULL; |
304 | conn.rq.headers_received_tail = NULL; | 318 | conn.rq.headers_received_tail = NULL; |
305 | 319 | ||
306 | conn.socket_context = NULL; | 320 | conn.read_buffer = NULL; |
321 | conn.write_buffer = NULL; | ||
307 | conn.rq.last = NULL; | 322 | conn.rq.last = NULL; |
308 | } | 323 | } |
309 | 324 | ||
@@ -1282,6 +1297,107 @@ check_digest (void) | |||
1282 | 1297 | ||
1283 | #endif /* DAUTH_SUPPORT */ | 1298 | #endif /* DAUTH_SUPPORT */ |
1284 | 1299 | ||
1300 | #define TEST_AUTH_STR "dXNlcjpwYXNz" | ||
1301 | |||
1302 | static unsigned int | ||
1303 | check_two_auths (void) | ||
1304 | { | ||
1305 | unsigned int ret; | ||
1306 | static struct MHD_HTTP_Req_Header h1; | ||
1307 | static struct MHD_HTTP_Req_Header h2; | ||
1308 | static struct MHD_HTTP_Req_Header h3; | ||
1309 | #ifdef BAUTH_SUPPORT | ||
1310 | const struct MHD_RqBAuth *bauth; | ||
1311 | #endif /* BAUTH_SUPPORT */ | ||
1312 | #ifdef DAUTH_SUPPORT | ||
1313 | const struct MHD_RqDAuth *dauth; | ||
1314 | #endif /* DAUTH_SUPPORT */ | ||
1315 | |||
1316 | if ((NULL != conn.rq.headers_received) || | ||
1317 | (NULL != conn.rq.headers_received_tail)) | ||
1318 | externalErrorExitDesc ("Connection's test headers are not empty already"); | ||
1319 | |||
1320 | /* Init and use both Basic and Digest Auth headers */ | ||
1321 | memset (&h1, 0, sizeof(h1)); | ||
1322 | memset (&h2, 0, sizeof(h2)); | ||
1323 | memset (&h3, 0, sizeof(h3)); | ||
1324 | |||
1325 | h1.kind = MHD_HEADER_KIND; | ||
1326 | h1.header = MHD_HTTP_HEADER_HOST; /* Just some random header */ | ||
1327 | h1.header_size = MHD_STATICSTR_LEN_ (MHD_HTTP_HEADER_HOST); | ||
1328 | h1.value = "localhost"; | ||
1329 | h1.value_size = strlen (h1.value); | ||
1330 | |||
1331 | h2.kind = MHD_HEADER_KIND; | ||
1332 | h2.header = MHD_HTTP_HEADER_AUTHORIZATION; | ||
1333 | h2.header_size = MHD_STATICSTR_LEN_ (MHD_HTTP_HEADER_AUTHORIZATION); | ||
1334 | h2.value = "Basic " TEST_AUTH_STR; | ||
1335 | h2.value_size = strlen (h2.value); | ||
1336 | |||
1337 | h3.kind = MHD_HEADER_KIND; | ||
1338 | h3.header = MHD_HTTP_HEADER_AUTHORIZATION; | ||
1339 | h3.header_size = MHD_STATICSTR_LEN_ (MHD_HTTP_HEADER_AUTHORIZATION); | ||
1340 | h3.value = "Digest cnonce=" TEST_AUTH_STR; | ||
1341 | h3.value_size = strlen (h3.value); | ||
1342 | |||
1343 | conn.rq.headers_received = &h1; | ||
1344 | h1.next = &h2; | ||
1345 | h2.prev = &h1; | ||
1346 | h2.next = &h3; | ||
1347 | h3.prev = &h2; | ||
1348 | conn.rq.headers_received_tail = &h3; | ||
1349 | |||
1350 | conn.state = MHD_CONNECTION_FULL_REQ_RECEIVED; /* Should be a typical value */ | ||
1351 | |||
1352 | ret = 0; | ||
1353 | #ifdef BAUTH_SUPPORT | ||
1354 | bauth = get_BAuthRqParams (); | ||
1355 | #endif /* BAUTH_SUPPORT */ | ||
1356 | #ifdef DAUTH_SUPPORT | ||
1357 | dauth = get_DAuthRqParams (); | ||
1358 | #endif /* DAUTH_SUPPORT */ | ||
1359 | #ifdef BAUTH_SUPPORT | ||
1360 | if (NULL == bauth) | ||
1361 | { | ||
1362 | fprintf (stderr, "No Basic Authorization header detected. Line: %u\n", | ||
1363 | (unsigned int) __LINE__); | ||
1364 | ret++; | ||
1365 | } | ||
1366 | else if ((MHD_STATICSTR_LEN_ (TEST_AUTH_STR) != bauth->token68.len) || | ||
1367 | (0 != memcmp (bauth->token68.str, TEST_AUTH_STR, | ||
1368 | bauth->token68.len))) | ||
1369 | { | ||
1370 | fprintf (stderr, "Basic Authorization token does not match. Line: %u\n", | ||
1371 | (unsigned int) __LINE__); | ||
1372 | ret++; | ||
1373 | } | ||
1374 | #endif /* BAUTH_SUPPORT */ | ||
1375 | #ifdef DAUTH_SUPPORT | ||
1376 | if (NULL == dauth) | ||
1377 | { | ||
1378 | fprintf (stderr, "No Digest Authorization header detected. Line: %u\n", | ||
1379 | (unsigned int) __LINE__); | ||
1380 | ret++; | ||
1381 | } | ||
1382 | else if ((MHD_STATICSTR_LEN_ (TEST_AUTH_STR) != dauth->cnonce.value.len) || | ||
1383 | (0 != memcmp (dauth->cnonce.value.str, TEST_AUTH_STR, | ||
1384 | dauth->cnonce.value.len))) | ||
1385 | { | ||
1386 | fprintf (stderr, "Digest Authorization 'cnonce' does not match. Line: %u\n", | ||
1387 | (unsigned int) __LINE__); | ||
1388 | ret++; | ||
1389 | } | ||
1390 | #endif /* DAUTH_SUPPORT */ | ||
1391 | |||
1392 | /* Cleanup */ | ||
1393 | conn.rq.headers_received = NULL; | ||
1394 | conn.rq.headers_received_tail = NULL; | ||
1395 | conn.state = MHD_CONNECTION_INIT; | ||
1396 | |||
1397 | return ret; | ||
1398 | } | ||
1399 | |||
1400 | |||
1285 | int | 1401 | int |
1286 | main (int argc, char *argv[]) | 1402 | main (int argc, char *argv[]) |
1287 | { | 1403 | { |
@@ -1296,6 +1412,7 @@ main (int argc, char *argv[]) | |||
1296 | #ifdef DAUTH_SUPPORT | 1412 | #ifdef DAUTH_SUPPORT |
1297 | errcount += check_digest (); | 1413 | errcount += check_digest (); |
1298 | #endif /* DAUTH_SUPPORT */ | 1414 | #endif /* DAUTH_SUPPORT */ |
1415 | errcount += check_two_auths (); | ||
1299 | if (0 == errcount) | 1416 | if (0 == errcount) |
1300 | printf ("All tests were passed without errors.\n"); | 1417 | printf ("All tests were passed without errors.\n"); |
1301 | return errcount == 0 ? 0 : 1; | 1418 | return errcount == 0 ? 0 : 1; |