Line data Source code
1 : /**
2 : * @file suite_libhttp.c
3 : * @brief Check testsuite for libhttp functions
4 : * @author François Cerbelle (Fanfan), francois@cerbelle.net
5 : *
6 : * @internal
7 : * Created: 20/03/2022
8 : * Revision: none
9 : * Last modified: 2024-11-24 23:15
10 : * Compiler: gcc
11 : * Organization: Cerbelle.net
12 : * Copyright: Copyright (c) 2024, François Cerbelle
13 : *
14 : * This source code is released for free distribution under the terms of the
15 : * GNU General Public License as published by the Free Software Foundation.
16 : */
17 :
18 : #ifdef HAVE_CONFIG_H
19 : #include "config.h"
20 : #endif
21 :
22 : #include <check.h>
23 : #include <stdlib.h>
24 : #include "checktools.inc"
25 :
26 : #include "libhttp.h"
27 : #include <stdio.h> /* perror */
28 :
29 : /** Dummy test to trigger fixtures
30 : *
31 : * This test is defined and added to all testcases in order to execute at
32 : * least one test test in each testcase of the testsuite, trigger the
33 : * fixtures and have a decent coverage report.
34 : */
35 9 : START_TEST(libhttp_test) {
36 : /* This test is supposed to trigger a SIGABRT(6) and will crash the
37 : * whole testsuite if not caught or not in a child process */
38 9 : forktest_only;
39 :
40 6 : abort();
41 : }
42 : END_TEST
43 :
44 3 : START_TEST(libhttp_new) {
45 : HTTP_t* http;
46 : char* body;
47 : HTTPHeader_t* header;
48 :
49 3 : http = HTTP_new();
50 3 : ck_assert(http);
51 :
52 3 : body = HTTP_getbody(http);
53 3 : ck_assert(body);
54 3 : ck_assert(strcmp(body,"")==0);
55 :
56 3 : header = HTTP_firstheader(http);
57 3 : ck_assert(header==NULL);
58 :
59 3 : HTTP_del(http);
60 3 : }
61 : END_TEST
62 :
63 3 : START_TEST(libhttp_del_null) {
64 : /* SIG6 in debug builds, SIG11 in release builds */
65 3 : forktest_only;
66 :
67 2 : HTTP_del(NULL);
68 : }
69 : END_TEST
70 :
71 3 : START_TEST(libhttp_del_empty) {
72 : HTTP_t* http;
73 :
74 3 : http = HTTP_new();
75 3 : ck_assert(http);
76 3 : HTTP_del(http);
77 3 : }
78 : END_TEST
79 :
80 3 : START_TEST(libhttp_del_populated) {
81 : HTTP_t* http;
82 : HTTPHeader_t* header;
83 :
84 3 : http = HTTP_new();
85 3 : ck_assert(http);
86 :
87 3 : header = HTTP_addheader(http,"name1","value1");
88 3 : ck_assert(header);
89 :
90 3 : header = HTTP_addheader(http,"name2","value2");
91 3 : ck_assert(header);
92 :
93 3 : http = HTTP_setbody(http,"<HTML></HTML>");
94 3 : ck_assert(http);
95 :
96 3 : HTTP_del(http);
97 3 : }
98 : END_TEST
99 :
100 3 : START_TEST(libhttp_setbody_nullhttp) {
101 : /* SIG6 in debug builds, SIG11 in release builds */
102 3 : forktest_only;
103 :
104 2 : HTTP_setbody(NULL,"");
105 : }
106 : END_TEST
107 :
108 3 : START_TEST(libhttp_setbody_nullbody) {
109 : HTTP_t* http;
110 :
111 : /* SIG6 in debug builds, SIG11 in release builds */
112 3 : forktest_only;
113 :
114 2 : http = HTTP_new();
115 2 : ck_assert(http);
116 :
117 2 : HTTP_setbody(http,NULL);
118 : }
119 : END_TEST
120 :
121 3 : START_TEST(libhttp_setbody_empty) {
122 : HTTP_t* http;
123 : char* body;
124 :
125 3 : http = HTTP_new();
126 3 : ck_assert(http);
127 :
128 3 : http = HTTP_setbody(http,"");
129 3 : ck_assert(http);
130 3 : body = HTTP_getbody(http);
131 3 : ck_assert(strcmp(body,"")==0);
132 :
133 3 : HTTP_del(http);
134 3 : }
135 : END_TEST
136 :
137 3 : START_TEST(libhttp_setbody_valid) {
138 : HTTP_t* http;
139 : char* body;
140 :
141 3 : http = HTTP_new();
142 3 : ck_assert(http);
143 :
144 3 : http = HTTP_setbody(http,"<HTML></HTML>");
145 3 : ck_assert(http);
146 3 : body = HTTP_getbody(http);
147 3 : ck_assert(strcmp(body,"<HTML></HTML>")==0);
148 :
149 3 : HTTP_del(http);
150 3 : }
151 : END_TEST
152 :
153 3 : START_TEST(libhttp_getbody_null) {
154 : /* SIG6 in debug builds, SIG11 in release builds */
155 3 : forktest_only;
156 :
157 2 : HTTP_getbody(NULL);
158 : }
159 : END_TEST
160 :
161 3 : START_TEST(libhttp_getbody_populated) {
162 : HTTP_t* http;
163 : char* body;
164 :
165 3 : http = HTTP_new();
166 3 : ck_assert(http);
167 :
168 3 : body = HTTP_getbody(http);
169 3 : ck_assert(body);
170 3 : ck_assert(strcmp(body,"")==0);
171 :
172 3 : http = HTTP_setbody(http,"<HTML></HTML>");
173 3 : ck_assert(http);
174 :
175 3 : body = HTTP_getbody(http);
176 3 : ck_assert(body);
177 3 : ck_assert(strcmp(body,"<HTML></HTML>")==0);
178 :
179 3 : HTTP_del(http);
180 3 : }
181 : END_TEST
182 :
183 3 : START_TEST(libhttp_addheader_nullhttp) {
184 : /* SIG6 in debug builds, SIG11 in release builds */
185 3 : forktest_only;
186 2 : HTTP_addheader(NULL,"name","value");
187 : }
188 : END_TEST
189 :
190 3 : START_TEST(libhttp_addheader_nullname) {
191 : HTTP_t* http;
192 :
193 : /* SIG6 in debug builds, SIG11 in release builds */
194 3 : forktest_only;
195 :
196 2 : http=HTTP_new();
197 2 : HTTP_addheader(http,NULL,"value");
198 : }
199 : END_TEST
200 :
201 3 : START_TEST(libhttp_addheader_nullvalue) {
202 : HTTP_t* http;
203 :
204 : /* SIG6 in debug builds, SIG11 in release builds */
205 3 : forktest_only;
206 :
207 2 : http=HTTP_new();
208 2 : HTTP_addheader(http,"name",NULL);
209 : }
210 : END_TEST
211 :
212 3 : START_TEST(libhttp_addheader_emptyname) {
213 : HTTP_t* http;
214 : HTTPHeader_t *header;
215 :
216 3 : http=HTTP_new();
217 3 : header = HTTP_addheader(http,"","value");
218 3 : ck_assert(NULL==header);
219 3 : HTTP_del(http);
220 3 : }
221 : END_TEST
222 :
223 3 : START_TEST(libhttp_addheader_emptyvalue) {
224 : HTTP_t* http;
225 : HTTPHeader_t *header;
226 :
227 3 : http=HTTP_new();
228 3 : header = HTTP_addheader(http,"name","");
229 3 : ck_assert(NULL!=header);
230 3 : ck_assert(0==strcmp("name",HTTPHeader_getname(header)));
231 3 : ck_assert(0==strcmp("",HTTPHeader_getvalue(header)));
232 3 : HTTP_del(http);
233 3 : }
234 : END_TEST
235 :
236 3 : START_TEST(libhttp_addheader_valid) {
237 : HTTP_t* http;
238 : HTTPHeader_t *header;
239 :
240 3 : http=HTTP_new();
241 3 : header = HTTP_addheader(http,"name","value");
242 3 : ck_assert(NULL!=header);
243 3 : ck_assert(0==strcmp("name",HTTPHeader_getname(header)));
244 3 : ck_assert(0==strcmp("value",HTTPHeader_getvalue(header)));
245 3 : HTTP_del(http);
246 3 : }
247 : END_TEST
248 :
249 3 : START_TEST(libhttp_addbasicauth_nullhttp) {
250 : /* SIG6 in debug builds, SIG11 in release builds */
251 3 : forktest_only;
252 2 : HTTP_addbasicauth(NULL,"login","pass");
253 : }
254 : END_TEST
255 :
256 3 : START_TEST(libhttp_addbasicauth_nulllogin) {
257 : HTTP_t* http;
258 :
259 : /* SIG6 in debug builds, SIG11 in release builds */
260 3 : forktest_only;
261 :
262 2 : http=HTTP_new();
263 2 : HTTP_addbasicauth(http,NULL,"pass");
264 : }
265 : END_TEST
266 :
267 3 : START_TEST(libhttp_addbasicauth_nullpass) {
268 : HTTP_t* http;
269 :
270 : /* SIG6 in debug builds, SIG11 in release builds */
271 3 : forktest_only;
272 :
273 2 : http=HTTP_new();
274 2 : HTTP_addbasicauth(http,"login",NULL);
275 : }
276 : END_TEST
277 :
278 3 : START_TEST(libhttp_addbasicauth_emptylogin) {
279 : HTTP_t* http;
280 : HTTPHeader_t *header;
281 :
282 3 : http=HTTP_new();
283 3 : header = HTTP_addbasicauth(http,"","pass");
284 3 : ck_assert(NULL!=header);
285 3 : ck_assert(0==strcmp("Authorization",HTTPHeader_getname(header)));
286 3 : ck_assert(0==strcmp("Basic OnBhc3M=",HTTPHeader_getvalue(header)));
287 3 : HTTP_del(http);
288 3 : }
289 : END_TEST
290 :
291 3 : START_TEST(libhttp_addbasicauth_emptypass) {
292 : HTTP_t* http;
293 : HTTPHeader_t *header;
294 :
295 3 : http=HTTP_new();
296 3 : header = HTTP_addbasicauth(http,"login","");
297 3 : ck_assert(NULL!=header);
298 3 : ck_assert(0==strcmp("Authorization",HTTPHeader_getname(header)));
299 3 : ck_assert(0==strcmp("Basic bG9naW46",HTTPHeader_getvalue(header)));
300 3 : HTTP_del(http);
301 3 : }
302 : END_TEST
303 :
304 3 : START_TEST(libhttp_addbasicauth_valid) {
305 : HTTP_t* http;
306 : HTTPHeader_t *header;
307 :
308 3 : http=HTTP_new();
309 3 : header = HTTP_addbasicauth(http,"login","pass");
310 3 : ck_assert(NULL!=header);
311 3 : ck_assert(0==strcmp("Authorization",HTTPHeader_getname(header)));
312 3 : ck_assert(0==strcmp("Basic bG9naW46cGFzcw==",HTTPHeader_getvalue(header)));
313 3 : HTTP_del(http);
314 3 : }
315 : END_TEST
316 :
317 3 : START_TEST(libhttp_setname_nullheader) {
318 : /* SIG6 in debug builds, SIG11 in release builds */
319 3 : forktest_only;
320 2 : HTTPHeader_setname(NULL,"name");
321 : }
322 : END_TEST
323 :
324 3 : START_TEST(libhttp_setname_nullname) {
325 : HTTP_t* http;
326 : HTTPHeader_t *header;
327 :
328 : /* SIG6 in debug builds, SIG11 in release builds */
329 3 : forktest_only;
330 :
331 2 : http=HTTP_new();
332 2 : header = HTTP_addheader(http,"name","value");
333 2 : ck_assert(NULL!=header);
334 2 : ck_assert(0==strcmp("name",HTTPHeader_getname(header)));
335 2 : ck_assert(0==strcmp("value",HTTPHeader_getvalue(header)));
336 :
337 2 : header = HTTPHeader_setname(header,NULL);
338 : }
339 : END_TEST
340 :
341 3 : START_TEST(libhttp_setname_emptyname) {
342 : HTTP_t* http;
343 : HTTPHeader_t *header;
344 :
345 3 : http=HTTP_new();
346 3 : header = HTTP_addheader(http,"name","value");
347 3 : ck_assert(NULL!=header);
348 3 : ck_assert(0==strcmp("name",HTTPHeader_getname(header)));
349 3 : ck_assert(0==strcmp("value",HTTPHeader_getvalue(header)));
350 :
351 3 : header = HTTPHeader_setname(header,"");
352 3 : ck_assert(NULL==header);
353 3 : header = HTTP_firstheader(http);
354 3 : ck_assert(NULL!=header);
355 3 : ck_assert(0==strcmp("name",HTTPHeader_getname(header)));
356 3 : ck_assert(0==strcmp("value",HTTPHeader_getvalue(header)));
357 3 : HTTP_del(http);
358 3 : }
359 : END_TEST
360 :
361 3 : START_TEST(libhttp_setname_valid) {
362 : HTTP_t* http;
363 : HTTPHeader_t *header;
364 :
365 3 : http=HTTP_new();
366 3 : header = HTTP_addheader(http,"name","value");
367 3 : ck_assert(NULL!=header);
368 3 : ck_assert(0==strcmp("name",HTTPHeader_getname(header)));
369 3 : ck_assert(0==strcmp("value",HTTPHeader_getvalue(header)));
370 :
371 3 : header = HTTPHeader_setname(header,"newname");
372 3 : ck_assert(NULL!=header);
373 3 : ck_assert(0==strcmp("newname",HTTPHeader_getname(header)));
374 3 : ck_assert(0==strcmp("value",HTTPHeader_getvalue(header)));
375 3 : HTTP_del(http);
376 3 : }
377 : END_TEST
378 :
379 3 : START_TEST(libhttp_getname_nullheader) {
380 : /* SIG6 in debug builds, SIG11 in release builds */
381 3 : forktest_only;
382 2 : HTTPHeader_getname(NULL);
383 : }
384 : END_TEST
385 :
386 3 : START_TEST(libhttp_getname_valid) {
387 : HTTP_t* http;
388 : HTTPHeader_t *header;
389 :
390 3 : http=HTTP_new();
391 3 : header = HTTP_addheader(http,"name","value");
392 3 : ck_assert(NULL!=header);
393 3 : ck_assert(0==strcmp("name",HTTPHeader_getname(header)));
394 3 : ck_assert(0==strcmp("value",HTTPHeader_getvalue(header)));
395 3 : HTTP_del(http);
396 3 : }
397 : END_TEST
398 :
399 3 : START_TEST(libhttp_setvalue_nullheader) {
400 : /* SIG6 in debug builds, SIG11 in release builds */
401 3 : forktest_only;
402 2 : HTTPHeader_setvalue(NULL,"value");
403 : }
404 : END_TEST
405 :
406 3 : START_TEST(libhttp_setvalue_nullvalue) {
407 : HTTP_t* http;
408 : HTTPHeader_t *header;
409 :
410 : /* SIG6 in debug builds, SIG11 in release builds */
411 3 : forktest_only;
412 :
413 2 : http=HTTP_new();
414 2 : header = HTTP_addheader(http,"name","value");
415 2 : ck_assert(NULL!=header);
416 2 : ck_assert(0==strcmp("name",HTTPHeader_getname(header)));
417 2 : ck_assert(0==strcmp("value",HTTPHeader_getvalue(header)));
418 :
419 2 : header = HTTPHeader_setvalue(header,NULL);
420 : }
421 : END_TEST
422 :
423 3 : START_TEST(libhttp_setvalue_emptyvalue) {
424 : HTTP_t* http;
425 : HTTPHeader_t *header;
426 :
427 3 : http=HTTP_new();
428 3 : header = HTTP_addheader(http,"name","value");
429 3 : ck_assert(NULL!=header);
430 3 : ck_assert(0==strcmp("name",HTTPHeader_getname(header)));
431 3 : ck_assert(0==strcmp("value",HTTPHeader_getvalue(header)));
432 :
433 3 : header = HTTPHeader_setvalue(header,"");
434 3 : ck_assert(NULL!=header);
435 3 : ck_assert(0==strcmp("name",HTTPHeader_getname(header)));
436 3 : ck_assert(0==strcmp("",HTTPHeader_getvalue(header)));
437 3 : HTTP_del(http);
438 3 : }
439 : END_TEST
440 :
441 3 : START_TEST(libhttp_setvalue_valid) {
442 : HTTP_t* http;
443 : HTTPHeader_t *header;
444 :
445 3 : http=HTTP_new();
446 3 : header = HTTP_addheader(http,"name","value");
447 3 : ck_assert(NULL!=header);
448 3 : ck_assert(0==strcmp("name",HTTPHeader_getname(header)));
449 3 : ck_assert(0==strcmp("value",HTTPHeader_getvalue(header)));
450 :
451 3 : header = HTTPHeader_setvalue(header,"newvalue");
452 3 : ck_assert(NULL!=header);
453 3 : ck_assert(0==strcmp("name",HTTPHeader_getname(header)));
454 3 : ck_assert(0==strcmp("newvalue",HTTPHeader_getvalue(header)));
455 3 : HTTP_del(http);
456 3 : }
457 : END_TEST
458 :
459 3 : START_TEST(libhttp_getvalue_nullheader) {
460 : /* SIG6 in debug builds, SIG11 in release builds */
461 3 : forktest_only;
462 2 : HTTPHeader_getvalue(NULL);
463 : }
464 : END_TEST
465 :
466 3 : START_TEST(libhttp_getvalue_valid) {
467 : HTTP_t* http;
468 : HTTPHeader_t *header;
469 :
470 3 : http=HTTP_new();
471 3 : header = HTTP_addheader(http,"name","value");
472 3 : ck_assert(NULL!=header);
473 3 : ck_assert(0==strcmp("name",HTTPHeader_getname(header)));
474 3 : ck_assert(0==strcmp("value",HTTPHeader_getvalue(header)));
475 3 : HTTP_del(http);
476 3 : }
477 : END_TEST
478 :
479 3 : START_TEST(libhttp_firstheader_nullhttp) {
480 : /* SIG6 in debug builds, SIG11 in release builds */
481 3 : forktest_only;
482 2 : HTTP_firstheader(NULL);
483 : }
484 : END_TEST
485 :
486 3 : START_TEST(libhttp_firstheader_empty) {
487 : HTTP_t* http;
488 : HTTPHeader_t *header;
489 :
490 3 : http=HTTP_new();
491 3 : header = HTTP_firstheader(http);
492 3 : ck_assert(NULL==header);
493 3 : HTTP_del(http);
494 3 : }
495 : END_TEST
496 :
497 3 : START_TEST(libhttp_firstheader_single) {
498 : HTTP_t* http;
499 : HTTPHeader_t *header;
500 :
501 3 : http=HTTP_new();
502 3 : header = HTTP_addheader(http,"name","value");
503 3 : ck_assert(NULL!=header);
504 3 : ck_assert(0==strcmp("name",HTTPHeader_getname(header)));
505 3 : ck_assert(0==strcmp("value",HTTPHeader_getvalue(header)));
506 :
507 3 : header = HTTP_firstheader(http);
508 3 : ck_assert(NULL!=header);
509 3 : ck_assert(0==strcmp("name",HTTPHeader_getname(header)));
510 3 : ck_assert(0==strcmp("value",HTTPHeader_getvalue(header)));
511 3 : HTTP_del(http);
512 3 : }
513 : END_TEST
514 :
515 3 : START_TEST(libhttp_firstheader_multi) {
516 : HTTP_t* http;
517 : HTTPHeader_t *header;
518 :
519 3 : http=HTTP_new();
520 3 : header = HTTP_addheader(http,"name1","value1");
521 3 : ck_assert(NULL!=header);
522 3 : ck_assert(0==strcmp("name1",HTTPHeader_getname(header)));
523 3 : ck_assert(0==strcmp("value1",HTTPHeader_getvalue(header)));
524 3 : header = HTTP_addheader(http,"name2","value2");
525 3 : ck_assert(NULL!=header);
526 3 : ck_assert(0==strcmp("name2",HTTPHeader_getname(header)));
527 3 : ck_assert(0==strcmp("value2",HTTPHeader_getvalue(header)));
528 :
529 3 : header = HTTP_firstheader(http);
530 3 : ck_assert(NULL!=header);
531 3 : ck_assert(0==strcmp("name1",HTTPHeader_getname(header)));
532 3 : ck_assert(0==strcmp("value1",HTTPHeader_getvalue(header)));
533 3 : HTTP_del(http);
534 3 : }
535 : END_TEST
536 :
537 3 : START_TEST(libhttp_nextheader_nullheader) {
538 : /* SIG6 in debug builds, SIG11 in release builds */
539 3 : forktest_only;
540 2 : HTTP_nextheader(NULL);
541 : }
542 : END_TEST
543 :
544 3 : START_TEST(libhttp_nextheader_empty) {
545 : HTTP_t* http;
546 : HTTPHeader_t *header;
547 :
548 : /* SIG6 in debug builds, SIG11 in release builds */
549 3 : forktest_only;
550 :
551 2 : http=HTTP_new();
552 2 : header = HTTP_firstheader(http);
553 2 : header = HTTP_nextheader(header);
554 : }
555 : END_TEST
556 :
557 3 : START_TEST(libhttp_nextheader_single) {
558 : HTTP_t* http;
559 : HTTPHeader_t *header;
560 :
561 3 : http=HTTP_new();
562 3 : header = HTTP_addheader(http,"name","value");
563 3 : ck_assert(NULL!=header);
564 3 : ck_assert(0==strcmp("name",HTTPHeader_getname(header)));
565 3 : ck_assert(0==strcmp("value",HTTPHeader_getvalue(header)));
566 :
567 3 : header = HTTP_firstheader(http);
568 3 : header = HTTP_nextheader(header);
569 3 : ck_assert(NULL==header);
570 :
571 3 : HTTP_del(http);
572 3 : }
573 : END_TEST
574 :
575 3 : START_TEST(libhttp_nextheader_multi) {
576 : HTTP_t* http;
577 : HTTPHeader_t *header;
578 :
579 3 : http=HTTP_new();
580 3 : header = HTTP_addheader(http,"name1","value1");
581 3 : ck_assert(NULL!=header);
582 3 : ck_assert(0==strcmp("name1",HTTPHeader_getname(header)));
583 3 : ck_assert(0==strcmp("value1",HTTPHeader_getvalue(header)));
584 3 : header = HTTP_addheader(http,"name2","value2");
585 3 : ck_assert(NULL!=header);
586 3 : ck_assert(0==strcmp("name2",HTTPHeader_getname(header)));
587 3 : ck_assert(0==strcmp("value2",HTTPHeader_getvalue(header)));
588 3 : header = HTTP_addheader(http,"name3","value3");
589 3 : ck_assert(NULL!=header);
590 3 : ck_assert(0==strcmp("name3",HTTPHeader_getname(header)));
591 3 : ck_assert(0==strcmp("value3",HTTPHeader_getvalue(header)));
592 :
593 3 : header = HTTP_firstheader(http);
594 3 : ck_assert(NULL!=header);
595 3 : ck_assert(0==strcmp("name1",HTTPHeader_getname(header)));
596 3 : ck_assert(0==strcmp("value1",HTTPHeader_getvalue(header)));
597 3 : header = HTTP_nextheader(header);
598 3 : ck_assert(NULL!=header);
599 3 : ck_assert(0==strcmp("name2",HTTPHeader_getname(header)));
600 3 : ck_assert(0==strcmp("value2",HTTPHeader_getvalue(header)));
601 3 : header = HTTP_nextheader(header);
602 3 : ck_assert(NULL!=header);
603 3 : ck_assert(0==strcmp("name3",HTTPHeader_getname(header)));
604 3 : ck_assert(0==strcmp("value3",HTTPHeader_getvalue(header)));
605 3 : header = HTTP_nextheader(header);
606 3 : ck_assert(NULL==header);
607 :
608 3 : HTTP_del(http);
609 3 : }
610 : END_TEST
611 :
612 3 : START_TEST(libhttp_findheader_nullheader) {
613 : /* SIG6 in debug builds, SIG11 in release builds */
614 3 : forktest_only;
615 2 : HTTP_findheader(NULL,"");
616 : }
617 : END_TEST
618 :
619 3 : START_TEST(libhttp_findheader_nullname) {
620 : HTTP_t* http;
621 : HTTPHeader_t *header;
622 :
623 : /* SIG6 in debug builds, SIG11 in release builds */
624 3 : forktest_only;
625 :
626 2 : http=HTTP_new();
627 2 : header = HTTP_addheader(http,"name1","value1");
628 2 : header = HTTP_addheader(http,"name2","value2");
629 2 : header = HTTP_addheader(http,"name3","value3");
630 2 : header = HTTP_firstheader(http);
631 2 : HTTP_findheader(header,NULL);
632 : }
633 : END_TEST
634 :
635 3 : START_TEST(libhttp_findheader_emptyname) {
636 : HTTP_t* http;
637 : HTTPHeader_t *header;
638 :
639 3 : http=HTTP_new();
640 3 : header = HTTP_addheader(http,"name1","value1");
641 3 : header = HTTP_addheader(http,"name2","value2");
642 3 : header = HTTP_addheader(http,"name3","value3");
643 3 : header = HTTP_firstheader(http);
644 3 : header = HTTP_findheader(header,"");
645 : /* Search accepted despite empty header name forbidden, will return NULL
646 : * because there will always be a miss */
647 3 : ck_assert(NULL==header);
648 :
649 3 : HTTP_del(http);
650 3 : }
651 : END_TEST
652 :
653 3 : START_TEST(libhttp_findheader_missingname) {
654 : HTTP_t* http;
655 : HTTPHeader_t *header;
656 :
657 3 : http=HTTP_new();
658 3 : header = HTTP_addheader(http,"name1","value1");
659 3 : header = HTTP_addheader(http,"name2","value2");
660 3 : header = HTTP_addheader(http,"name3","value3");
661 3 : header = HTTP_firstheader(http);
662 3 : header = HTTP_findheader(header,"name4");
663 3 : ck_assert(NULL==header);
664 :
665 3 : HTTP_del(http);
666 3 : }
667 : END_TEST
668 :
669 3 : START_TEST(libhttp_findheader_firstname) {
670 : HTTP_t* http;
671 : HTTPHeader_t *header;
672 :
673 3 : http=HTTP_new();
674 3 : header = HTTP_addheader(http,"name1","value1");
675 3 : header = HTTP_addheader(http,"name2","value2");
676 3 : header = HTTP_addheader(http,"name3","value3");
677 3 : header = HTTP_firstheader(http);
678 3 : header = HTTP_findheader(header,"name1");
679 3 : ck_assert(NULL!=header);
680 3 : ck_assert(0==strcmp("name1",HTTPHeader_getname(header)));
681 3 : ck_assert(0==strcmp("value1",HTTPHeader_getvalue(header)));
682 :
683 3 : HTTP_del(http);
684 3 : }
685 : END_TEST
686 :
687 3 : START_TEST(libhttp_findheader_middlename) {
688 : HTTP_t* http;
689 : HTTPHeader_t *header;
690 :
691 3 : http=HTTP_new();
692 3 : header = HTTP_addheader(http,"name1","value1");
693 3 : header = HTTP_addheader(http,"name2","value2");
694 3 : header = HTTP_addheader(http,"name3","value3");
695 3 : header = HTTP_firstheader(http);
696 3 : header = HTTP_findheader(header,"name2");
697 3 : ck_assert(NULL!=header);
698 3 : ck_assert(0==strcmp("name2",HTTPHeader_getname(header)));
699 3 : ck_assert(0==strcmp("value2",HTTPHeader_getvalue(header)));
700 :
701 3 : HTTP_del(http);
702 3 : }
703 : END_TEST
704 :
705 3 : START_TEST(libhttp_findheader_lastname) {
706 : HTTP_t* http;
707 : HTTPHeader_t *header;
708 :
709 3 : http=HTTP_new();
710 3 : header = HTTP_addheader(http,"name1","value1");
711 3 : header = HTTP_addheader(http,"name2","value2");
712 3 : header = HTTP_addheader(http,"name3","value3");
713 3 : header = HTTP_firstheader(http);
714 3 : header = HTTP_findheader(header,"name3");
715 3 : ck_assert(NULL!=header);
716 3 : ck_assert(0==strcmp("name3",HTTPHeader_getname(header)));
717 3 : ck_assert(0==strcmp("value3",HTTPHeader_getvalue(header)));
718 :
719 3 : HTTP_del(http);
720 3 : }
721 : END_TEST
722 :
723 3 : START_TEST(libhttp_findheader_secondmiddle) {
724 : HTTP_t* http;
725 : HTTPHeader_t *header;
726 :
727 3 : http=HTTP_new();
728 3 : header = HTTP_addheader(http,"name1","value1");
729 3 : header = HTTP_addheader(http,"name2","value2");
730 3 : header = HTTP_addheader(http,"name1","value4");
731 3 : header = HTTP_addheader(http,"name3","value3");
732 3 : header = HTTP_firstheader(http);
733 3 : header = HTTP_findheader(header,"name1");
734 3 : ck_assert(NULL!=header);
735 3 : ck_assert(0==strcmp("name1",HTTPHeader_getname(header)));
736 3 : ck_assert(0==strcmp("value1",HTTPHeader_getvalue(header)));
737 3 : header = HTTP_nextheader(header);
738 3 : header = HTTP_findheader(header,"name1");
739 3 : ck_assert(NULL!=header);
740 3 : ck_assert(0==strcmp("name1",HTTPHeader_getname(header)));
741 3 : ck_assert(0==strcmp("value4",HTTPHeader_getvalue(header)));
742 :
743 3 : HTTP_del(http);
744 3 : }
745 : END_TEST
746 :
747 3 : START_TEST(libhttp_findheader_secondlast) {
748 : HTTP_t* http;
749 : HTTPHeader_t *header;
750 :
751 3 : http=HTTP_new();
752 3 : header = HTTP_addheader(http,"name1","value1");
753 3 : header = HTTP_addheader(http,"name2","value2");
754 3 : header = HTTP_addheader(http,"name1","value3");
755 3 : header = HTTP_firstheader(http);
756 3 : header = HTTP_findheader(header,"name1");
757 3 : ck_assert(NULL!=header);
758 3 : ck_assert(0==strcmp("name1",HTTPHeader_getname(header)));
759 3 : ck_assert(0==strcmp("value1",HTTPHeader_getvalue(header)));
760 3 : header = HTTP_nextheader(header);
761 3 : header = HTTP_findheader(header,"name1");
762 3 : ck_assert(NULL!=header);
763 3 : ck_assert(0==strcmp("name1",HTTPHeader_getname(header)));
764 3 : ck_assert(0==strcmp("value3",HTTPHeader_getvalue(header)));
765 :
766 3 : HTTP_del(http);
767 3 : }
768 : END_TEST
769 :
770 3 : START_TEST(libhttp_remheader_nullhttp) {
771 : HTTP_t* http;
772 : HTTPHeader_t *header;
773 :
774 : /* SIG6 in debug builds, SIG11 in release builds */
775 3 : forktest_only;
776 :
777 2 : http=HTTP_new();
778 2 : header = HTTP_addheader(http,"name1","value1");
779 2 : HTTP_remheader(NULL,header);
780 : }
781 : END_TEST
782 :
783 3 : START_TEST(libhttp_remheader_nullheader) {
784 : HTTP_t* http;
785 :
786 : /* SIG6 in debug builds, SIG11 in release builds */
787 3 : forktest_only;
788 :
789 2 : http=HTTP_new();
790 2 : HTTP_remheader(http,NULL);
791 : }
792 : END_TEST
793 :
794 3 : START_TEST(libhttp_remheader_missingheader) {
795 : HTTP_t* http;
796 : HTTP_t* http2;
797 : HTTPHeader_t *header;
798 :
799 3 : http=HTTP_new();
800 3 : header = HTTP_addheader(http,"name1","value1");
801 3 : header = HTTP_addheader(http,"name2","value2");
802 3 : header = HTTP_addheader(http,"name3","value3");
803 :
804 3 : http2=HTTP_new();
805 3 : header = HTTP_addheader(http2,"name4","value4");
806 3 : header = HTTP_firstheader(http2);
807 :
808 3 : header = HTTP_firstheader(http2);
809 : #if 0
810 : /** @bug Header deleted if not found in this HTTP, but it obviously
811 : * belongs to another HTTP, will be freed, but not removed from his
812 : * header list SIGSEGV11 to expect at some point */
813 : ck_assert(NULL==HTTP_remheader(http,header));
814 : #endif
815 :
816 3 : header = HTTP_firstheader(http);
817 3 : ck_assert(NULL!=header);
818 3 : ck_assert(0==strcmp("name1",HTTPHeader_getname(header)));
819 3 : ck_assert(0==strcmp("value1",HTTPHeader_getvalue(header)));
820 3 : header = HTTP_nextheader(header);
821 3 : ck_assert(NULL!=header);
822 3 : ck_assert(0==strcmp("name2",HTTPHeader_getname(header)));
823 3 : ck_assert(0==strcmp("value2",HTTPHeader_getvalue(header)));
824 3 : header = HTTP_nextheader(header);
825 3 : ck_assert(NULL!=header);
826 3 : ck_assert(0==strcmp("name3",HTTPHeader_getname(header)));
827 3 : ck_assert(0==strcmp("value3",HTTPHeader_getvalue(header)));
828 3 : header = HTTP_nextheader(header);
829 3 : ck_assert(NULL==header);
830 3 : header = HTTP_firstheader(http2);
831 3 : ck_assert(NULL!=header);
832 3 : ck_assert(0==strcmp("name4",HTTPHeader_getname(header)));
833 3 : ck_assert(0==strcmp("value4",HTTPHeader_getvalue(header)));
834 3 : header = HTTP_nextheader(header);
835 3 : ck_assert(NULL==header);
836 :
837 3 : HTTP_del(http);
838 3 : }
839 : END_TEST
840 :
841 3 : START_TEST(libhttp_remheader_firstheader) {
842 : HTTP_t* http;
843 : HTTPHeader_t *header;
844 :
845 3 : http=HTTP_new();
846 3 : header = HTTP_addheader(http,"name1","value1");
847 3 : header = HTTP_addheader(http,"name2","value2");
848 3 : header = HTTP_addheader(http,"name3","value3");
849 :
850 3 : header = HTTP_firstheader(http);
851 3 : ck_assert(http==HTTP_remheader(http,header));
852 :
853 3 : header = HTTP_firstheader(http);
854 3 : ck_assert(NULL!=header);
855 3 : ck_assert(0==strcmp("name2",HTTPHeader_getname(header)));
856 3 : ck_assert(0==strcmp("value2",HTTPHeader_getvalue(header)));
857 3 : header = HTTP_nextheader(header);
858 3 : ck_assert(NULL!=header);
859 3 : ck_assert(0==strcmp("name3",HTTPHeader_getname(header)));
860 3 : ck_assert(0==strcmp("value3",HTTPHeader_getvalue(header)));
861 3 : header = HTTP_nextheader(header);
862 3 : ck_assert(NULL==header);
863 :
864 3 : HTTP_del(http);
865 3 : }
866 : END_TEST
867 :
868 3 : START_TEST(libhttp_remheader_middleheader) {
869 : HTTP_t* http;
870 : HTTPHeader_t *header;
871 :
872 3 : http=HTTP_new();
873 3 : header = HTTP_addheader(http,"name1","value1");
874 3 : header = HTTP_addheader(http,"name2","value2");
875 3 : header = HTTP_addheader(http,"name3","value3");
876 :
877 3 : header = HTTP_findheader(HTTP_firstheader(http),"name2");
878 3 : ck_assert(http==HTTP_remheader(http,header));
879 :
880 3 : header = HTTP_firstheader(http);
881 3 : ck_assert(NULL!=header);
882 3 : ck_assert(0==strcmp("name1",HTTPHeader_getname(header)));
883 3 : ck_assert(0==strcmp("value1",HTTPHeader_getvalue(header)));
884 3 : header = HTTP_nextheader(header);
885 3 : ck_assert(NULL!=header);
886 3 : ck_assert(0==strcmp("name3",HTTPHeader_getname(header)));
887 3 : ck_assert(0==strcmp("value3",HTTPHeader_getvalue(header)));
888 3 : header = HTTP_nextheader(header);
889 3 : ck_assert(NULL==header);
890 :
891 3 : HTTP_del(http);
892 3 : }
893 : END_TEST
894 :
895 3 : START_TEST(libhttp_remheader_lastheader) {
896 : HTTP_t* http;
897 : HTTPHeader_t *header;
898 :
899 3 : http=HTTP_new();
900 3 : header = HTTP_addheader(http,"name1","value1");
901 3 : header = HTTP_addheader(http,"name2","value2");
902 3 : header = HTTP_addheader(http,"name3","value3");
903 :
904 3 : header = HTTP_findheader(HTTP_firstheader(http),"name3");
905 3 : ck_assert(http==HTTP_remheader(http,header));
906 :
907 3 : header = HTTP_firstheader(http);
908 3 : ck_assert(NULL!=header);
909 3 : ck_assert(0==strcmp("name1",HTTPHeader_getname(header)));
910 3 : ck_assert(0==strcmp("value1",HTTPHeader_getvalue(header)));
911 3 : header = HTTP_nextheader(header);
912 3 : ck_assert(NULL!=header);
913 3 : ck_assert(0==strcmp("name2",HTTPHeader_getname(header)));
914 3 : ck_assert(0==strcmp("value2",HTTPHeader_getvalue(header)));
915 3 : header = HTTP_nextheader(header);
916 3 : ck_assert(NULL==header);
917 :
918 3 : HTTP_del(http);
919 3 : }
920 : END_TEST
921 :
922 3 : START_TEST(libhttp_remheader_onlyheader) {
923 : HTTP_t* http;
924 : HTTPHeader_t *header;
925 :
926 3 : http=HTTP_new();
927 3 : header = HTTP_addheader(http,"name1","value1");
928 :
929 3 : header = HTTP_firstheader(http);
930 3 : ck_assert(http==HTTP_remheader(http,header));
931 :
932 3 : header = HTTP_firstheader(http);
933 3 : ck_assert(NULL==header);
934 :
935 3 : HTTP_del(http);
936 3 : }
937 : END_TEST
938 :
939 :
940 :
941 :
942 :
943 :
944 3 : START_TEST(libhttp_basictest) {
945 : char* output_str;
946 : HTTP_t* http;
947 :
948 : /* Build HTTP */
949 3 : http = HTTP_new();
950 3 : ck_assert(HTTP_addheader(http,"Host","localhost"));
951 3 : ck_assert(HTTP_addheader(http,"Host","localhost"));
952 3 : ck_assert(HTTP_addheader(http,"Content-type","text/html"));
953 3 : HTTP_setbody(http,"<HTML></HTML>\r\n");
954 :
955 : /* Generate request line */
956 3 : output_str=HTTP_buildrequest(HTTPMETHOD_GET,"/v1/api/bdbs",HTTPVERSION_HTTP11);
957 3 : printf("%s",output_str);
958 3 : free(output_str);
959 :
960 : /* Generate headers */
961 3 : output_str = HTTP_buildheaders(http);
962 3 : printf("%s",output_str);
963 3 : free(output_str);
964 :
965 : /* Generate body */
966 3 : printf("%s",HTTP_getbody(http));
967 :
968 : /* Cleanup */
969 3 : HTTP_del(http);
970 3 : }
971 : END_TEST
972 :
973 : /************************************************************************/
974 : /* Begining of test (potentially in child) */
975 186 : void libhttp_checked_common_setup() {
976 186 : signals_catch();
977 186 : forktest_gprofdir();
978 186 : }
979 :
980 : /* End of test (potentially in child) */
981 134 : void libhttp_checked_common_teardown() {
982 134 : signals_release();
983 134 : }
984 : /************************************************************************/
985 139 : void libhttp_unchecked_common_setup() {
986 139 : forktest_init();
987 139 : }
988 12 : void libhttp_unchecked_initialized_setup() {
989 12 : libhttp_unchecked_common_setup();
990 : /** @todo http = HTTP_new(); */
991 12 : }
992 5 : void libhttp_unchecked_populated_setup() {
993 5 : libhttp_unchecked_initialized_setup();
994 : /** @todo HTTP_addheader(); HTTP_setbody(); */
995 5 : }
996 15 : void libhttp_unchecked_common_teardown() {
997 15 : }
998 3 : void libhttp_unchecked_populated_teardown() {
999 3 : libhttp_unchecked_common_teardown();
1000 : /** @todo HTTP_del(http); */
1001 3 : }
1002 : /************************************************************************/
1003 128 : Suite* libhttp_suite() {
1004 : Suite *s;
1005 : TCase *tc;
1006 :
1007 128 : s = suite_create("LibHTTP");
1008 :
1009 128 : tc = tcase_create("LibHTTPUninitialized");
1010 128 : tcase_set_tags(tc,"LibHTTPTag LibHTTPTag2");
1011 : /* tcase_set_timeout(tc,5); */ /* seconds */
1012 128 : tcase_add_checked_fixture(tc, libhttp_checked_common_setup, libhttp_checked_common_teardown);
1013 128 : tcase_add_unchecked_fixture(tc, libhttp_unchecked_common_setup, libhttp_unchecked_common_teardown);
1014 128 : suite_add_tcase(s, tc);
1015 128 : tcase_add_test_raise_signal(tc, libhttp_test,6);
1016 128 : tcase_add_test(tc, libhttp_new);
1017 128 : tcase_add_test_raise_signal(tc, libhttp_del_null,SIG_ABRTSEGV);
1018 128 : tcase_add_test(tc, libhttp_del_empty);
1019 128 : tcase_add_test(tc, libhttp_del_populated);
1020 128 : tcase_add_test_raise_signal(tc, libhttp_setbody_nullhttp,SIG_ABRTSEGV);
1021 128 : tcase_add_test_raise_signal(tc, libhttp_setbody_nullbody,SIG_ABRTSEGV);
1022 128 : tcase_add_test(tc, libhttp_setbody_empty);
1023 128 : tcase_add_test(tc, libhttp_setbody_valid);
1024 128 : tcase_add_test_raise_signal(tc, libhttp_getbody_null,SIG_ABRTSEGV);
1025 128 : tcase_add_test(tc, libhttp_getbody_populated);
1026 128 : tcase_add_test_raise_signal(tc, libhttp_addheader_nullhttp,SIG_ABRTSEGV);
1027 128 : tcase_add_test_raise_signal(tc, libhttp_addheader_nullname,SIG_ABRTSEGV);
1028 128 : tcase_add_test_raise_signal(tc, libhttp_addheader_nullvalue,SIG_ABRTSEGV);
1029 128 : tcase_add_test(tc, libhttp_addheader_emptyname);
1030 128 : tcase_add_test(tc, libhttp_addheader_emptyvalue);
1031 128 : tcase_add_test(tc, libhttp_addheader_valid);
1032 128 : tcase_add_test_raise_signal(tc, libhttp_addbasicauth_nullhttp,SIG_ABRTSEGV);
1033 128 : tcase_add_test_raise_signal(tc, libhttp_addbasicauth_nulllogin,SIG_ABRTSEGV);
1034 128 : tcase_add_test_raise_signal(tc, libhttp_addbasicauth_nullpass,SIG_ABRTSEGV);
1035 128 : tcase_add_test(tc, libhttp_addbasicauth_emptylogin);
1036 128 : tcase_add_test(tc, libhttp_addbasicauth_emptypass);
1037 128 : tcase_add_test(tc, libhttp_addbasicauth_valid);
1038 128 : tcase_add_test_raise_signal(tc, libhttp_setname_nullheader,SIG_ABRTSEGV);
1039 128 : tcase_add_test_raise_signal(tc, libhttp_setname_nullname,SIG_ABRTSEGV);
1040 128 : tcase_add_test(tc, libhttp_setname_emptyname);
1041 128 : tcase_add_test(tc, libhttp_setname_valid);
1042 128 : tcase_add_test_raise_signal(tc, libhttp_getname_nullheader,SIG_ABRTSEGV);
1043 128 : tcase_add_test(tc, libhttp_getname_valid);
1044 128 : tcase_add_test_raise_signal(tc, libhttp_setvalue_nullheader,SIG_ABRTSEGV);
1045 128 : tcase_add_test_raise_signal(tc, libhttp_setvalue_nullvalue,SIG_ABRTSEGV);
1046 128 : tcase_add_test(tc, libhttp_setvalue_emptyvalue);
1047 128 : tcase_add_test(tc, libhttp_setvalue_valid);
1048 128 : tcase_add_test_raise_signal(tc, libhttp_getvalue_nullheader,SIG_ABRTSEGV);
1049 128 : tcase_add_test(tc, libhttp_getvalue_valid);
1050 128 : tcase_add_test_raise_signal(tc, libhttp_firstheader_nullhttp,SIG_ABRTSEGV);
1051 128 : tcase_add_test(tc, libhttp_firstheader_empty);
1052 128 : tcase_add_test(tc, libhttp_firstheader_single);
1053 128 : tcase_add_test(tc, libhttp_firstheader_multi);
1054 128 : tcase_add_test_raise_signal(tc, libhttp_nextheader_nullheader,SIG_ABRTSEGV);
1055 128 : tcase_add_test_raise_signal(tc, libhttp_nextheader_empty,SIG_ABRTSEGV);
1056 128 : tcase_add_test(tc, libhttp_nextheader_single);
1057 128 : tcase_add_test(tc, libhttp_nextheader_multi);
1058 128 : tcase_add_test_raise_signal(tc, libhttp_findheader_nullheader,SIG_ABRTSEGV);
1059 128 : tcase_add_test_raise_signal(tc, libhttp_findheader_nullname,SIG_ABRTSEGV);
1060 128 : tcase_add_test(tc, libhttp_findheader_emptyname);
1061 128 : tcase_add_test(tc, libhttp_findheader_missingname);
1062 128 : tcase_add_test(tc, libhttp_findheader_firstname);
1063 128 : tcase_add_test(tc, libhttp_findheader_middlename);
1064 128 : tcase_add_test(tc, libhttp_findheader_lastname);
1065 128 : tcase_add_test(tc, libhttp_findheader_secondmiddle);
1066 128 : tcase_add_test(tc, libhttp_findheader_secondlast);
1067 128 : tcase_add_test_raise_signal(tc, libhttp_remheader_nullhttp,SIG_ABRTSEGV);
1068 128 : tcase_add_test_raise_signal(tc, libhttp_remheader_nullheader,SIG_ABRTSEGV);
1069 128 : tcase_add_test(tc, libhttp_remheader_missingheader);
1070 128 : tcase_add_test(tc, libhttp_remheader_firstheader);
1071 128 : tcase_add_test(tc, libhttp_remheader_middleheader);
1072 128 : tcase_add_test(tc, libhttp_remheader_lastheader);
1073 128 : tcase_add_test(tc, libhttp_remheader_onlyheader);
1074 128 : tcase_add_test(tc, libhttp_basictest);
1075 :
1076 128 : tc = tcase_create("LibHTTPInitialized");
1077 128 : tcase_set_tags(tc,"LibHTTPTag LibHTTPTag2");
1078 : /* tcase_set_timeout(tc,5); */ /* seconds */
1079 128 : tcase_add_checked_fixture(tc, libhttp_checked_common_setup, libhttp_checked_common_teardown);
1080 128 : tcase_add_unchecked_fixture(tc, libhttp_unchecked_initialized_setup, libhttp_unchecked_common_teardown);
1081 128 : suite_add_tcase(s, tc);
1082 128 : tcase_add_test_raise_signal(tc, libhttp_test,6);
1083 :
1084 128 : tc = tcase_create("LibHTTPPopulated");
1085 128 : tcase_set_tags(tc,"LibHTTPTag LibHTTPTag2");
1086 : /* tcase_set_timeout(tc,5); */ /* seconds */
1087 128 : tcase_add_checked_fixture(tc, libhttp_checked_common_setup, libhttp_checked_common_teardown);
1088 128 : tcase_add_unchecked_fixture(tc, libhttp_unchecked_populated_setup, libhttp_unchecked_populated_teardown);
1089 128 : suite_add_tcase(s, tc);
1090 128 : tcase_add_test_raise_signal(tc, libhttp_test,6);
1091 :
1092 : #if 0
1093 : /** @todo OOM unit tests */
1094 : tc = tcase_create("LibHTTPOOM");
1095 : tcase_set_tags(tc,"LibHTTPTag LibHTTPTag2");
1096 : /* tcase_set_timeout(tc,5); */ /* seconds */
1097 : tcase_add_checked_fixture(tc, libhttp_checked_common_setup, libhttp_checked_common_teardown);
1098 : tcase_add_unchecked_fixture(tc, libhttp_unchecked_common_setup, libhttp_unchecked_common_teardown);
1099 : suite_add_tcase(s, tc);
1100 : tcase_add_test_raise_signal(tc, libhttp_test,6);
1101 : #endif
1102 :
1103 128 : return s;
1104 : }
1105 : /* vim: set tw=80: */
|