diff options
Diffstat (limited to 'src/daemon/postprocessor_test.c')
-rw-r--r-- | src/daemon/postprocessor_test.c | 159 |
1 files changed, 80 insertions, 79 deletions
diff --git a/src/daemon/postprocessor_test.c b/src/daemon/postprocessor_test.c index a931b300..6641fe1e 100644 --- a/src/daemon/postprocessor_test.c +++ b/src/daemon/postprocessor_test.c | |||
@@ -40,10 +40,10 @@ | |||
40 | * Each series of checks should be terminated by | 40 | * Each series of checks should be terminated by |
41 | * five NULL-entries. | 41 | * five NULL-entries. |
42 | */ | 42 | */ |
43 | const char * want[] = { | 43 | const char *want[] = { |
44 | #define URL_DATA "abc=def&x=5" | 44 | #define URL_DATA "abc=def&x=5" |
45 | #define URL_START 0 | 45 | #define URL_START 0 |
46 | "abc", NULL, NULL, NULL, "def", | 46 | "abc", NULL, NULL, NULL, "def", |
47 | "x", NULL, NULL, NULL, "5", | 47 | "x", NULL, NULL, NULL, "5", |
48 | #define URL_END (URL_START + 10) | 48 | #define URL_END (URL_START + 10) |
49 | NULL, NULL, NULL, NULL, NULL, | 49 | NULL, NULL, NULL, NULL, NULL, |
@@ -63,81 +63,80 @@ const char * want[] = { | |||
63 | }; | 63 | }; |
64 | 64 | ||
65 | static int | 65 | static int |
66 | mismatch(const char * a, const char * b) { | 66 | mismatch (const char *a, const char *b) |
67 | if (a == b) | 67 | { |
68 | if (a == b) | ||
68 | return 0; | 69 | return 0; |
69 | if ( (a == NULL) || | 70 | if ((a == NULL) || (b == NULL)) |
70 | (b == NULL) ) | ||
71 | return 1; | 71 | return 1; |
72 | return 0 != strcmp(a, b); | 72 | return 0 != strcmp (a, b); |
73 | } | 73 | } |
74 | 74 | ||
75 | static int | 75 | static int |
76 | value_checker(void * cls, | 76 | value_checker (void *cls, |
77 | enum MHD_ValueKind kind, | 77 | enum MHD_ValueKind kind, |
78 | const char * key, | 78 | const char *key, |
79 | const char * filename, | 79 | const char *filename, |
80 | const char * content_type, | 80 | const char *content_type, |
81 | const char * transfer_encoding, | 81 | const char *transfer_encoding, |
82 | const char * data, | 82 | const char *data, size_t off, size_t size) |
83 | size_t off, | 83 | { |
84 | size_t size) { | 84 | int *want_off = cls; |
85 | int * want_off = cls; | ||
86 | int idx = *want_off; | 85 | int idx = *want_off; |
87 | 86 | ||
88 | #if 0 | 87 | #if 0 |
89 | fprintf(stderr, | 88 | fprintf (stderr, |
90 | "VC: `%s' `%s' `%s' `%s' `%.*s'\n", | 89 | "VC: `%s' `%s' `%s' `%s' `%.*s'\n", |
91 | key, filename, content_type, transfer_encoding, size, data); | 90 | key, filename, content_type, transfer_encoding, size, data); |
92 | #endif | 91 | #endif |
93 | if (size == 0) | 92 | if (size == 0) |
94 | return MHD_YES; | 93 | return MHD_YES; |
95 | if ( (idx < 0) || | 94 | if ((idx < 0) || |
96 | (want[idx] == NULL) || | 95 | (want[idx] == NULL) || |
97 | (0 != strcmp(key, want[idx])) || | 96 | (0 != strcmp (key, want[idx])) || |
98 | (mismatch(filename, want[idx+1])) || | 97 | (mismatch (filename, want[idx + 1])) || |
99 | (mismatch(content_type, want[idx+2])) || | 98 | (mismatch (content_type, want[idx + 2])) || |
100 | (mismatch(transfer_encoding, want[idx+3])) || | 99 | (mismatch (transfer_encoding, want[idx + 3])) || |
101 | (0 != memcmp(data, &want[idx+4][off], size)) ) | 100 | (0 != memcmp (data, &want[idx + 4][off], size))) |
102 | { | 101 | { |
103 | *want_off = -1; | 102 | *want_off = -1; |
104 | return MHD_NO; | 103 | return MHD_NO; |
105 | } | 104 | } |
106 | if (off + size == strlen(want[idx+4])) | 105 | if (off + size == strlen (want[idx + 4])) |
107 | *want_off = idx + 5; | 106 | *want_off = idx + 5; |
108 | return MHD_YES; | 107 | return MHD_YES; |
109 | 108 | ||
110 | } | 109 | } |
111 | 110 | ||
112 | 111 | ||
113 | static int | 112 | static int |
114 | test_urlencoding() { | 113 | test_urlencoding () |
114 | { | ||
115 | struct MHD_Connection connection; | 115 | struct MHD_Connection connection; |
116 | struct MHD_HTTP_Header header; | 116 | struct MHD_HTTP_Header header; |
117 | struct MHD_PostProcessor * pp; | 117 | struct MHD_PostProcessor *pp; |
118 | unsigned int want_off = URL_START; | 118 | unsigned int want_off = URL_START; |
119 | int i; | 119 | int i; |
120 | int delta; | 120 | int delta; |
121 | size_t size; | 121 | size_t size; |
122 | 122 | ||
123 | memset(&connection, 0, sizeof(struct MHD_Connection)); | 123 | memset (&connection, 0, sizeof (struct MHD_Connection)); |
124 | memset(&header, 0, sizeof(struct MHD_HTTP_Header)); | 124 | memset (&header, 0, sizeof (struct MHD_HTTP_Header)); |
125 | connection.headers_received = &header; | 125 | connection.headers_received = &header; |
126 | header.header = MHD_HTTP_HEADER_CONTENT_TYPE; | 126 | header.header = MHD_HTTP_HEADER_CONTENT_TYPE; |
127 | header.value = MHD_HTTP_POST_ENCODING_FORM_URLENCODED; | 127 | header.value = MHD_HTTP_POST_ENCODING_FORM_URLENCODED; |
128 | header.kind = MHD_HEADER_KIND; | 128 | header.kind = MHD_HEADER_KIND; |
129 | pp = MHD_create_post_processor(&connection, | 129 | pp = MHD_create_post_processor (&connection, |
130 | 1024, | 130 | 1024, &value_checker, &want_off); |
131 | &value_checker, | ||
132 | &want_off); | ||
133 | i = 0; | 131 | i = 0; |
134 | size = strlen(URL_DATA); | 132 | size = strlen (URL_DATA); |
135 | while (i < size) { | 133 | while (i < size) |
136 | delta = 1 + random() % (size - i); | 134 | { |
137 | MHD_post_process(pp, &URL_DATA[i], delta); | 135 | delta = 1 + random () % (size - i); |
138 | i += delta; | 136 | MHD_post_process (pp, &URL_DATA[i], delta); |
139 | } | 137 | i += delta; |
140 | MHD_destroy_post_processor(pp); | 138 | } |
139 | MHD_destroy_post_processor (pp); | ||
141 | if (want_off != URL_END) | 140 | if (want_off != URL_END) |
142 | return 1; | 141 | return 1; |
143 | return 0; | 142 | return 0; |
@@ -145,33 +144,34 @@ test_urlencoding() { | |||
145 | 144 | ||
146 | 145 | ||
147 | static int | 146 | static int |
148 | test_multipart() { | 147 | test_multipart () |
148 | { | ||
149 | struct MHD_Connection connection; | 149 | struct MHD_Connection connection; |
150 | struct MHD_HTTP_Header header; | 150 | struct MHD_HTTP_Header header; |
151 | struct MHD_PostProcessor * pp; | 151 | struct MHD_PostProcessor *pp; |
152 | unsigned int want_off = FORM_START; | 152 | unsigned int want_off = FORM_START; |
153 | int i; | 153 | int i; |
154 | int delta; | 154 | int delta; |
155 | size_t size; | 155 | size_t size; |
156 | 156 | ||
157 | memset(&connection, 0, sizeof(struct MHD_Connection)); | 157 | memset (&connection, 0, sizeof (struct MHD_Connection)); |
158 | memset(&header, 0, sizeof(struct MHD_HTTP_Header)); | 158 | memset (&header, 0, sizeof (struct MHD_HTTP_Header)); |
159 | connection.headers_received = &header; | 159 | connection.headers_received = &header; |
160 | header.header = MHD_HTTP_HEADER_CONTENT_TYPE; | 160 | header.header = MHD_HTTP_HEADER_CONTENT_TYPE; |
161 | header.value = MHD_HTTP_POST_ENCODING_MULTIPART_FORMDATA ", boundary=AaB03x"; | 161 | header.value = |
162 | MHD_HTTP_POST_ENCODING_MULTIPART_FORMDATA ", boundary=AaB03x"; | ||
162 | header.kind = MHD_HEADER_KIND; | 163 | header.kind = MHD_HEADER_KIND; |
163 | pp = MHD_create_post_processor(&connection, | 164 | pp = MHD_create_post_processor (&connection, |
164 | 1024, | 165 | 1024, &value_checker, &want_off); |
165 | &value_checker, | ||
166 | &want_off); | ||
167 | i = 0; | 166 | i = 0; |
168 | size = strlen(FORM_DATA); | 167 | size = strlen (FORM_DATA); |
169 | while (i < size) { | 168 | while (i < size) |
170 | delta = 1 + random() % (size - i); | 169 | { |
171 | MHD_post_process(pp, &FORM_DATA[i], delta); | 170 | delta = 1 + random () % (size - i); |
172 | i += delta; | 171 | MHD_post_process (pp, &FORM_DATA[i], delta); |
173 | } | 172 | i += delta; |
174 | MHD_destroy_post_processor(pp); | 173 | } |
174 | MHD_destroy_post_processor (pp); | ||
175 | if (want_off != FORM_END) | 175 | if (want_off != FORM_END) |
176 | return 2; | 176 | return 2; |
177 | return 0; | 177 | return 0; |
@@ -179,33 +179,34 @@ test_multipart() { | |||
179 | 179 | ||
180 | 180 | ||
181 | static int | 181 | static int |
182 | test_nested_multipart() { | 182 | test_nested_multipart () |
183 | { | ||
183 | struct MHD_Connection connection; | 184 | struct MHD_Connection connection; |
184 | struct MHD_HTTP_Header header; | 185 | struct MHD_HTTP_Header header; |
185 | struct MHD_PostProcessor * pp; | 186 | struct MHD_PostProcessor *pp; |
186 | unsigned int want_off = FORM_NESTED_START; | 187 | unsigned int want_off = FORM_NESTED_START; |
187 | int i; | 188 | int i; |
188 | int delta; | 189 | int delta; |
189 | size_t size; | 190 | size_t size; |
190 | 191 | ||
191 | memset(&connection, 0, sizeof(struct MHD_Connection)); | 192 | memset (&connection, 0, sizeof (struct MHD_Connection)); |
192 | memset(&header, 0, sizeof(struct MHD_HTTP_Header)); | 193 | memset (&header, 0, sizeof (struct MHD_HTTP_Header)); |
193 | connection.headers_received = &header; | 194 | connection.headers_received = &header; |
194 | header.header = MHD_HTTP_HEADER_CONTENT_TYPE; | 195 | header.header = MHD_HTTP_HEADER_CONTENT_TYPE; |
195 | header.value = MHD_HTTP_POST_ENCODING_MULTIPART_FORMDATA ", boundary=AaB03x"; | 196 | header.value = |
197 | MHD_HTTP_POST_ENCODING_MULTIPART_FORMDATA ", boundary=AaB03x"; | ||
196 | header.kind = MHD_HEADER_KIND; | 198 | header.kind = MHD_HEADER_KIND; |
197 | pp = MHD_create_post_processor(&connection, | 199 | pp = MHD_create_post_processor (&connection, |
198 | 1024, | 200 | 1024, &value_checker, &want_off); |
199 | &value_checker, | ||
200 | &want_off); | ||
201 | i = 0; | 201 | i = 0; |
202 | size = strlen(FORM_NESTED_DATA); | 202 | size = strlen (FORM_NESTED_DATA); |
203 | while (i < size) { | 203 | while (i < size) |
204 | delta = 1 + random() % (size - i); | 204 | { |
205 | MHD_post_process(pp, &FORM_NESTED_DATA[i], delta); | 205 | delta = 1 + random () % (size - i); |
206 | i += delta; | 206 | MHD_post_process (pp, &FORM_NESTED_DATA[i], delta); |
207 | } | 207 | i += delta; |
208 | MHD_destroy_post_processor(pp); | 208 | } |
209 | MHD_destroy_post_processor (pp); | ||
209 | if (want_off != FORM_NESTED_END) | 210 | if (want_off != FORM_NESTED_END) |
210 | return 4; | 211 | return 4; |
211 | return 0; | 212 | return 0; |
@@ -216,9 +217,9 @@ main (int argc, char *const *argv) | |||
216 | { | 217 | { |
217 | unsigned int errorCount = 0; | 218 | unsigned int errorCount = 0; |
218 | 219 | ||
219 | errorCount += test_urlencoding(); | 220 | errorCount += test_urlencoding (); |
220 | errorCount += test_multipart(); | 221 | errorCount += test_multipart (); |
221 | errorCount += test_nested_multipart(); | 222 | errorCount += test_nested_multipart (); |
222 | if (errorCount != 0) | 223 | if (errorCount != 0) |
223 | fprintf (stderr, "Error (code: %u)\n", errorCount); | 224 | fprintf (stderr, "Error (code: %u)\n", errorCount); |
224 | return errorCount != 0; /* 0 == pass */ | 225 | return errorCount != 0; /* 0 == pass */ |