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., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, 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 
31 #ifndef WINDOWS
32 #include <unistd.h>
33 #endif
34 
35 static int
value_checker(void * cls,enum MHD_ValueKind kind,const char * key,const char * filename,const char * content_type,const char * transfer_encoding,const char * data,uint64_t off,size_t size)36 value_checker (void *cls,
37                enum MHD_ValueKind kind,
38                const char *key,
39                const char *filename,
40                const char *content_type,
41                const char *transfer_encoding,
42                const char *data, uint64_t off, size_t size)
43 {
44   unsigned int *pos = cls;
45 #if 0
46   fprintf (stderr,
47            "VC: %llu %u `%s' `%s' `%s' `%s' `%.*s'\n",
48            off, size,
49            key, filename, content_type, transfer_encoding, size, data);
50 #endif
51   if (size == 0)
52     return MHD_YES;
53   *pos += size;
54   return MHD_YES;
55 
56 }
57 
58 
59 static int
test_simple_large()60 test_simple_large ()
61 {
62   struct MHD_Connection connection;
63   struct MHD_HTTP_Header header;
64   struct MHD_PostProcessor *pp;
65   int i;
66   int delta;
67   size_t size;
68   char data[102400];
69   unsigned int pos;
70 
71   pos = 0;
72   memset (data, 'A', sizeof (data));
73   memcpy (data, "key=", 4);
74   data[sizeof (data) - 1] = '\0';
75   memset (&connection, 0, sizeof (struct MHD_Connection));
76   memset (&header, 0, sizeof (struct MHD_HTTP_Header));
77   connection.headers_received = &header;
78   header.header = MHD_HTTP_HEADER_CONTENT_TYPE;
79   header.value = MHD_HTTP_POST_ENCODING_FORM_URLENCODED;
80   header.kind = MHD_HEADER_KIND;
81   pp = MHD_create_post_processor (&connection, 1024, &value_checker, &pos);
82   i = 0;
83   size = strlen (data);
84   while (i < size)
85     {
86       delta = 1 + MHD_random_ () % (size - i);
87       MHD_post_process (pp, &data[i], delta);
88       i += delta;
89     }
90   MHD_destroy_post_processor (pp);
91   if (pos != sizeof (data) - 5) /* minus 0-termination and 'key=' */
92     return 1;
93   return 0;
94 }
95 
96 int
main(int argc,char * const * argv)97 main (int argc, char *const *argv)
98 {
99   unsigned int errorCount = 0;
100 
101   errorCount += test_simple_large ();
102   if (errorCount != 0)
103     fprintf (stderr, "Error (code: %u)\n", errorCount);
104   return errorCount != 0;       /* 0 == pass */
105 }
106