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 18:21
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 3 : 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 3 : forktest_only;
39 :
40 2 : 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_basictest) {
480 : char* output_str;
481 : HTTP_t* http;
482 :
483 : /* Build HTTP */
484 3 : http = HTTP_new();
485 3 : ck_assert(HTTP_addheader(http,"Host","localhost"));
486 3 : ck_assert(HTTP_addheader(http,"Host","localhost"));
487 3 : ck_assert(HTTP_addheader(http,"Content-type","text/html"));
488 3 : HTTP_setbody(http,"<HTML></HTML>\r\n");
489 :
490 : /* Generate request line */
491 3 : output_str=HTTP_buildrequest(HTTPMETHOD_GET,"/v1/api/bdbs",HTTPVERSION_HTTP11);
492 3 : printf("%s",output_str);
493 3 : free(output_str);
494 :
495 : /* Generate headers */
496 3 : output_str = HTTP_buildheaders(http);
497 3 : printf("%s",output_str);
498 3 : free(output_str);
499 :
500 : /* Generate body */
501 3 : printf("%s",HTTP_getbody(http));
502 :
503 : /* Cleanup */
504 3 : HTTP_del(http);
505 3 : }
506 : END_TEST
507 :
508 : /************************************************************************/
509 : /* Begining of test (potentially in child) */
510 108 : void libhttp_checked_uninitialized_setup() {
511 108 : signals_catch();
512 108 : forktest_gprofdir();
513 108 : }
514 :
515 : /* End of test (potentially in child) */
516 74 : void libhttp_checked_uninitialized_teardown() {
517 74 : signals_release();
518 74 : }
519 : /************************************************************************/
520 75 : void libhttp_unchecked_common_setup() {
521 75 : forktest_init();
522 75 : }
523 3 : void libhttp_unchecked_common_teardown() {
524 3 : }
525 : /************************************************************************/
526 76 : Suite* libhttp_suite() {
527 : Suite *s;
528 : TCase *tc;
529 :
530 76 : s = suite_create("LibHTTP");
531 :
532 76 : tc = tcase_create("LibHTTPUninitialized");
533 76 : tcase_set_tags(tc,"LibHTTPTag LibHTTPTag2");
534 : /* tcase_set_timeout(tc,5); */ /* seconds */
535 76 : tcase_add_checked_fixture(tc, libhttp_checked_uninitialized_setup, libhttp_checked_uninitialized_teardown);
536 76 : tcase_add_unchecked_fixture(tc, libhttp_unchecked_common_setup, libhttp_unchecked_common_teardown);
537 76 : suite_add_tcase(s, tc);
538 76 : tcase_add_test_raise_signal(tc, libhttp_test,6);
539 76 : tcase_add_test(tc, libhttp_new);
540 76 : tcase_add_test_raise_signal(tc, libhttp_del_null,SIG_ABRTSEGV);
541 76 : tcase_add_test(tc, libhttp_del_empty);
542 76 : tcase_add_test(tc, libhttp_del_populated);
543 76 : tcase_add_test_raise_signal(tc, libhttp_setbody_nullhttp,SIG_ABRTSEGV);
544 76 : tcase_add_test_raise_signal(tc, libhttp_setbody_nullbody,SIG_ABRTSEGV);
545 76 : tcase_add_test(tc, libhttp_setbody_empty);
546 76 : tcase_add_test(tc, libhttp_setbody_valid);
547 76 : tcase_add_test_raise_signal(tc, libhttp_getbody_null,SIG_ABRTSEGV);
548 76 : tcase_add_test(tc, libhttp_getbody_populated);
549 76 : tcase_add_test_raise_signal(tc, libhttp_addheader_nullhttp,SIG_ABRTSEGV);
550 76 : tcase_add_test_raise_signal(tc, libhttp_addheader_nullname,SIG_ABRTSEGV);
551 76 : tcase_add_test_raise_signal(tc, libhttp_addheader_nullvalue,SIG_ABRTSEGV);
552 76 : tcase_add_test(tc, libhttp_addheader_emptyname);
553 76 : tcase_add_test(tc, libhttp_addheader_emptyvalue);
554 76 : tcase_add_test(tc, libhttp_addheader_valid);
555 76 : tcase_add_test_raise_signal(tc, libhttp_addbasicauth_nullhttp,SIG_ABRTSEGV);
556 76 : tcase_add_test_raise_signal(tc, libhttp_addbasicauth_nulllogin,SIG_ABRTSEGV);
557 76 : tcase_add_test_raise_signal(tc, libhttp_addbasicauth_nullpass,SIG_ABRTSEGV);
558 76 : tcase_add_test(tc, libhttp_addbasicauth_emptylogin);
559 76 : tcase_add_test(tc, libhttp_addbasicauth_emptypass);
560 76 : tcase_add_test(tc, libhttp_addbasicauth_valid);
561 76 : tcase_add_test_raise_signal(tc, libhttp_setname_nullheader,SIG_ABRTSEGV);
562 76 : tcase_add_test_raise_signal(tc, libhttp_setname_nullname,SIG_ABRTSEGV);
563 76 : tcase_add_test(tc, libhttp_setname_emptyname);
564 76 : tcase_add_test(tc, libhttp_setname_valid);
565 76 : tcase_add_test_raise_signal(tc, libhttp_getname_nullheader,SIG_ABRTSEGV);
566 76 : tcase_add_test(tc, libhttp_getname_valid);
567 76 : tcase_add_test_raise_signal(tc, libhttp_setvalue_nullheader,SIG_ABRTSEGV);
568 76 : tcase_add_test_raise_signal(tc, libhttp_setvalue_nullvalue,SIG_ABRTSEGV);
569 76 : tcase_add_test(tc, libhttp_setvalue_emptyvalue);
570 76 : tcase_add_test(tc, libhttp_setvalue_valid);
571 76 : tcase_add_test_raise_signal(tc, libhttp_getvalue_nullheader,SIG_ABRTSEGV);
572 76 : tcase_add_test(tc, libhttp_getvalue_valid);
573 76 : tcase_add_test(tc, libhttp_basictest);
574 :
575 76 : return s;
576 : }
577 : /* vim: set tw=80: */
|