test_postprocessor_large.c (3734B)
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 /* Turn any MHD_PANIC() or failing mhd_assert() reached from this 33 test into a marked, classifiable test error (TESTING.md, P5). */ 34 #include "mhd_panic_tripwire.h" 35 36 #ifndef WINDOWS 37 #include <unistd.h> 38 #endif 39 40 static enum MHD_Result 41 value_checker (void *cls, 42 enum MHD_ValueKind kind, 43 const char *key, 44 const char *filename, 45 const char *content_type, 46 const char *transfer_encoding, 47 const char *data, uint64_t off, size_t size) 48 { 49 size_t *pos = (size_t *) cls; 50 (void) kind; (void) key; (void) filename; (void) content_type; /* Unused. Silent compiler warning. */ 51 (void) transfer_encoding; (void) data; (void) off; /* Unused. Silent compiler warning. */ 52 #if 0 53 fprintf (stderr, 54 "VC: %" PRIu64 " %u `%s' `%s' `%s' `%s' `%.*s'\n", 55 off, size, 56 key, filename, content_type, transfer_encoding, size, data); 57 #endif 58 if (size == 0) 59 return MHD_YES; 60 *pos += size; 61 return MHD_YES; 62 63 } 64 65 66 static unsigned int 67 test_simple_large (void) 68 { 69 struct MHD_Connection connection; 70 struct MHD_HTTP_Req_Header header; 71 struct MHD_PostProcessor *pp; 72 size_t i; 73 size_t delta; 74 size_t size; 75 char data[102400]; 76 size_t pos; 77 78 pos = 0; 79 memset (data, 'A', sizeof (data)); 80 memcpy (data, "key=", 4); 81 data[sizeof (data) - 1] = '\0'; 82 memset (&connection, 0, sizeof (struct MHD_Connection)); 83 memset (&header, 0, sizeof (struct MHD_HTTP_Res_Header)); 84 connection.rq.headers_received = &header; 85 header.header = MHD_HTTP_HEADER_CONTENT_TYPE; 86 header.value = MHD_HTTP_POST_ENCODING_FORM_URLENCODED; 87 header.header_size = strlen (header.header); 88 header.value_size = strlen (header.value); 89 header.kind = MHD_HEADER_KIND; 90 pp = MHD_create_post_processor (&connection, 1024, &value_checker, &pos); 91 i = 0; 92 size = strlen (data); 93 while (i < size) 94 { 95 delta = 1 + ((size_t) MHD_random_ ()) % (size - i); 96 if (MHD_YES != 97 MHD_post_process (pp, 98 &data[i], 99 delta)) 100 { 101 fprintf (stderr, 102 "MHD_post_process() failed!\n"); 103 MHD_destroy_post_processor (pp); 104 return 1; 105 } 106 i += delta; 107 } 108 MHD_destroy_post_processor (pp); 109 if (pos != sizeof (data) - 5) /* minus 0-termination and 'key=' */ 110 return 1; 111 return 0; 112 } 113 114 115 int 116 main (int argc, char *const *argv) 117 { 118 unsigned int errorCount = 0; 119 (void) argc; (void) argv; /* Unused. Silent compiler warning. */ 120 121 errorCount += test_simple_large (); 122 if (errorCount != 0) 123 fprintf (stderr, "Error (code: %u)\n", errorCount); 124 return errorCount != 0; /* 0 == pass */ 125 }