| 1 |
/* |
| 2 |
This file is part of libmicrohttpd |
| 3 |
Copyright (C) 2008 Christian Grothoff |
| 4 |
|
| 5 |
libmicrohttpd is free software; you can redistribute it and/or modify |
| 6 |
it under the terms of the GNU General Public License as published |
| 7 |
by the Free Software Foundation; either version 2, or (at your |
| 8 |
option) any later version. |
| 9 |
|
| 10 |
libmicrohttpd is distributed in the hope that it will be useful, but |
| 11 |
WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
General Public License for more details. |
| 14 |
|
| 15 |
You should have received a copy of the GNU General Public License |
| 16 |
along with libmicrohttpd; see the file COPYING. If not, write to the |
| 17 |
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 18 |
Boston, MA 02110-1301, USA. |
| 19 |
*/ |
| 20 |
|
| 21 |
/** |
| 22 |
* @file test_postprocessor_large.c |
| 23 |
* @brief Testcase with very large input for postprocessor |
| 24 |
* @author Christian Grothoff |
| 25 |
*/ |
| 26 |
|
| 27 |
#include "platform.h" |
| 28 |
#include "microhttpd.h" |
| 29 |
#include "internal.h" |
| 30 |
#include "mhd_compat.h" |
| 31 |
|
| 32 |
#ifndef WINDOWS |
| 33 |
#include <unistd.h> |
| 34 |
#endif |
| 35 |
|
| 36 |
static enum MHD_Result |
| 37 |
value_checker (void *cls, |
| 38 |
enum MHD_ValueKind kind, |
| 39 |
const char *key, |
| 40 |
const char *filename, |
| 41 |
const char *content_type, |
| 42 |
const char *transfer_encoding, |
| 43 |
const char *data, uint64_t off, size_t size) |
| 44 |
{ |
| 45 |
size_t *pos = (size_t *) cls; |
| 46 |
(void) kind; (void) key; (void) filename; (void) content_type; /* Unused. Silent compiler warning. */ |
| 47 |
(void) transfer_encoding; (void) data; (void) off; /* Unused. Silent compiler warning. */ |
| 48 |
#if 0 |
| 49 |
fprintf (stderr, |
| 50 |
"VC: %" PRIu64 " %u `%s' `%s' `%s' `%s' `%.*s'\n", |
| 51 |
off, size, |
| 52 |
key, filename, content_type, transfer_encoding, size, data); |
| 53 |
#endif |
| 54 |
if (size == 0) |
| 55 |
return MHD_YES; |
| 56 |
*pos += size; |
| 57 |
return MHD_YES; |
| 58 |
|
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
static unsigned int |
| 63 |
test_simple_large (void) |
| 64 |
{ |
| 65 |
struct MHD_Connection connection; |
| 66 |
struct MHD_HTTP_Req_Header header; |
| 67 |
struct MHD_PostProcessor *pp; |
| 68 |
size_t i; |
| 69 |
size_t delta; |
| 70 |
size_t size; |
| 71 |
char data[102400]; |
| 72 |
size_t pos; |
| 73 |
|
| 74 |
pos = 0; |
| 75 |
memset (data, 'A', sizeof (data)); |
| 76 |
memcpy (data, "key=", 4); |
| 77 |
data[sizeof (data) - 1] = '\0'; |
| 78 |
memset (&connection, 0, sizeof (struct MHD_Connection)); |
| 79 |
memset (&header, 0, sizeof (struct MHD_HTTP_Res_Header)); |
| 80 |
connection.rq.headers_received = &header; |
| 81 |
header.header = MHD_HTTP_HEADER_CONTENT_TYPE; |
| 82 |
header.value = MHD_HTTP_POST_ENCODING_FORM_URLENCODED; |
| 83 |
header.header_size = strlen (header.header); |
| 84 |
header.value_size = strlen (header.value); |
| 85 |
header.kind = MHD_HEADER_KIND; |
| 86 |
pp = MHD_create_post_processor (&connection, 1024, &value_checker, &pos); |
| 87 |
i = 0; |
| 88 |
size = strlen (data); |
| 89 |
while (i < size) |
| 90 |
{ |
| 91 |
delta = 1 + ((size_t) MHD_random_ ()) % (size - i); |
| 92 |
if (MHD_YES != |
| 93 |
MHD_post_process (pp, |
| 94 |
&data[i], |
| 95 |
delta)) |
| 96 |
{ |
| 97 |
fprintf (stderr, |
| 98 |
"MHD_post_process() failed!\n"); |
| 99 |
MHD_destroy_post_processor (pp); |
| 100 |
return 1; |
| 101 |
} |
| 102 |
i += delta; |
| 103 |
} |
| 104 |
MHD_destroy_post_processor (pp); |
| 105 |
if (pos != sizeof (data) - 5) /* minus 0-termination and 'key=' */ |
| 106 |
return 1; |
| 107 |
return 0; |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
int |
| 112 |
main (int argc, char *const *argv) |
| 113 |
{ |
| 114 |
unsigned int errorCount = 0; |
| 115 |
(void) argc; (void) argv; /* Unused. Silent compiler warning. */ |
| 116 |
|
| 117 |
errorCount += test_simple_large (); |
| 118 |
if (errorCount != 0) |
| 119 |
fprintf (stderr, "Error (code: %u)\n", errorCount); |
| 120 |
return errorCount != 0; /* 0 == pass */ |
| 121 |
} |