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-22 08:26
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_httpheader_new_null) {
45 : /* SIG11 in release build, SIG6(assert/abort) in debug build */
46 3 : forktest_only;
47 :
48 2 : HTTPHeader_new(NULL,NULL);
49 : }
50 : END_TEST
51 :
52 3 : START_TEST(libhttp_httpheader_del_null) {
53 : /* SIG11 in release build, SIG6(assert/abort) in debug build */
54 3 : forktest_only;
55 :
56 2 : HTTPHeader_del(NULL);
57 : }
58 : END_TEST
59 :
60 3 : START_TEST(libhttp_httpheader_newdel) {
61 3 : HTTPHeader_t* header=NULL;
62 :
63 3 : header = HTTPHeader_new("name","value");
64 3 : ck_assert(header);
65 3 : ck_assert(0==strcmp("name",HTTPHeader_getname(header)));
66 3 : ck_assert(0==strcmp("value",HTTPHeader_getvalue(header)));
67 3 : HTTPHeader_del(header);
68 3 : }
69 : END_TEST
70 :
71 3 : START_TEST(libhttp_httpheader_newdel_empty) {
72 3 : HTTPHeader_t* header=NULL;
73 :
74 3 : header = HTTPHeader_new("","");
75 3 : ck_assert(header);
76 3 : ck_assert(0==strcmp("",HTTPHeader_getname(header)));
77 3 : ck_assert(0==strcmp("",HTTPHeader_getvalue(header)));
78 3 : HTTPHeader_del(header);
79 3 : }
80 : END_TEST
81 :
82 3 : START_TEST(libhttp_httpheader_newdel_doublefree) {
83 3 : HTTPHeader_t* header=NULL;
84 :
85 : /* Always SIG11 (not caught by assertions) */
86 3 : forktest_only;
87 :
88 2 : header = HTTPHeader_new("","");
89 2 : ck_assert(header);
90 2 : ck_assert(0==strcmp("",HTTPHeader_getname(header)));
91 2 : ck_assert(0==strcmp("",HTTPHeader_getvalue(header)));
92 2 : HTTPHeader_del(header);
93 2 : HTTPHeader_del(header);
94 : }
95 : END_TEST
96 :
97 3 : START_TEST(libhttp_httpheader_basicauth) {
98 3 : HTTPHeader_t* header=NULL;
99 :
100 3 : header = HTTPHeader_basicauth("admin@demo.com","password");
101 3 : ck_assert(0==strcmp("Authorization",HTTPHeader_getname(header)));
102 : /* Could be dynamically created with base64.header/base64.c inclusion */
103 3 : ck_assert(0==strcmp("Basic YWRtaW5AZGVtby5jb206cGFzc3dvcmQ=",HTTPHeader_getvalue(header)));
104 3 : HTTPHeader_del(header);
105 3 : }
106 : END_TEST
107 :
108 3 : START_TEST(libhttp_newdel) {
109 : HTTP_t* uut;
110 :
111 3 : uut = HTTP_new();
112 3 : HTTP_del(uut);
113 3 : }
114 : END_TEST
115 :
116 3 : START_TEST(libhttp_newdel_doublefree) {
117 : HTTP_t* uut;
118 :
119 : /* Always SIG11 (not caught by assertions) */
120 3 : forktest_only;
121 :
122 2 : uut = HTTP_new();
123 2 : HTTP_del(uut);
124 :
125 : /* Double free should trigger SIG11 */
126 2 : HTTP_del(uut);
127 : }
128 : END_TEST
129 :
130 3 : START_TEST(libhttp_del_null) {
131 : /* SIG11 in release build, SIG6(assert/abort) in debug build */
132 3 : forktest_only;
133 :
134 2 : HTTP_del(NULL);
135 : }
136 : END_TEST
137 :
138 3 : START_TEST(libhttp_setbody_null_empty) {
139 : /* SIG11 in release build, SIG6(assert/abort) in debug build */
140 3 : forktest_only;
141 2 : HTTP_setbody(NULL,"");
142 : }
143 : END_TEST
144 :
145 3 : START_TEST(libhttp_setbody_null) {
146 : /* SIG11 in release build, SIG6(assert/abort) in debug build */
147 3 : forktest_only;
148 : HTTP_t* uut;
149 2 : uut = HTTP_new();
150 2 : HTTP_setbody(uut,NULL);
151 : }
152 : END_TEST
153 :
154 3 : START_TEST(libhttp_setbody_empty) {
155 : HTTP_t* uut;
156 3 : uut = HTTP_new();
157 3 : HTTP_setbody(uut,"");
158 3 : ck_assert(0==strcmp("",HTTP_getbody(uut)));
159 3 : HTTP_del(uut);
160 3 : }
161 : END_TEST
162 :
163 3 : START_TEST(libhttp_setbody_valid) {
164 : HTTP_t* uut;
165 3 : uut = HTTP_new();
166 3 : HTTP_setbody(uut,"abc");
167 3 : ck_assert(0==strcmp("abc",HTTP_getbody(uut)));
168 3 : HTTP_del(uut);
169 3 : }
170 : END_TEST
171 :
172 3 : START_TEST(libhttp_getbody_first) {
173 : HTTP_t* uut;
174 3 : uut = HTTP_new();
175 3 : ck_assert(0==strcmp("",HTTP_getbody(uut)));
176 3 : HTTP_del(uut);
177 3 : }
178 : END_TEST
179 :
180 3 : START_TEST(libhttp_addheader_null_null) {
181 : /* SIG11 in release build, SIG6(assert/abort) in debug build */
182 3 : forktest_only;
183 2 : HTTP_addheader(NULL,NULL);
184 : }
185 : END_TEST
186 :
187 3 : START_TEST(libhttp_addheader_null) {
188 : HTTP_t* uut;
189 :
190 : /* SIG11 in release build, SIG6(assert/abort) in debug build */
191 3 : forktest_only;
192 :
193 2 : uut = HTTP_new();
194 2 : HTTP_addheader(uut,NULL);
195 : }
196 : END_TEST
197 :
198 3 : START_TEST(libhttp_addheader_valid) {
199 : HTTP_t* uut;
200 : HTTPHeader_t* header;
201 :
202 3 : uut = HTTP_new();
203 3 : header = HTTPHeader_new("name","value");
204 3 : ck_assert(NULL!=HTTP_addheader(uut,header));
205 3 : HTTP_delheader(uut,header);
206 3 : HTTPHeader_del(header);
207 3 : }
208 : END_TEST
209 :
210 3 : START_TEST(libhttp_addheader_doubleadd) {
211 : HTTP_t* uut;
212 : HTTPHeader_t* header;
213 :
214 3 : uut = HTTP_new();
215 3 : header = HTTPHeader_new("name","value");
216 3 : ck_assert(NULL!=HTTP_addheader(uut,header));
217 3 : ck_assert(NULL==HTTP_addheader(uut,header));
218 3 : HTTP_delheader(uut,header);
219 3 : HTTPHeader_del(header);
220 3 : }
221 : END_TEST
222 :
223 3 : START_TEST(libhttp_basictest) {
224 : char* tmp;
225 : HTTP_t* uut;
226 : HTTPHeader_t* header;
227 :
228 3 : uut = HTTP_new();
229 :
230 3 : HTTP_setbody(uut,"<HTML></HTML>\r\n");
231 :
232 3 : if (NULL==(header=HTTPHeader_new("Host","localhost"))) {
233 0 : perror("libhttp_basictest HTTPHeader_new Host");
234 0 : ck_abort();
235 : }
236 3 : if (NULL==(uut=HTTP_addheader(uut,header))) {
237 0 : perror("libhttp_basictest HTTP_addheader Host");
238 0 : ck_abort();
239 : }
240 :
241 3 : if (NULL==(header=HTTPHeader_new("Host","localhost"))) {
242 0 : perror("libhttp_basictest HTTPHeader_new Host2");
243 0 : ck_abort();
244 : }
245 3 : if (NULL==(uut=HTTP_addheader(uut,header))) {
246 0 : perror("libhttp_basictest HTTP_addheader Host2");
247 0 : ck_abort();
248 : }
249 :
250 3 : if (NULL==(header=HTTPHeader_new("Content-type","text/html"))) {
251 0 : perror("libhttp_basictest HTTPHeader_new Host");
252 0 : ck_abort();
253 : }
254 3 : if (NULL==(uut=HTTP_addheader(uut,header))) {
255 0 : perror("libhttp_basictest HTTP_addheader Host");
256 0 : ck_abort();
257 : }
258 :
259 3 : tmp=HTTP_buildrequest(HTTPMETHOD_GET,"/v1/api/bdbs",HTTPVERSION_HTTP11);
260 3 : printf("%s",tmp);
261 3 : free(tmp);
262 :
263 3 : tmp = HTTP_buildheaders(uut);
264 3 : printf("%s",tmp);
265 3 : free(tmp);
266 :
267 3 : printf("%s",HTTP_getbody(uut));
268 :
269 3 : HTTP_del(uut);
270 :
271 3 : }
272 : END_TEST
273 :
274 : /************************************************************************/
275 : /* Begining of test (potentially in child) */
276 60 : void libhttp_checked_uninitialized_setup() {
277 60 : signals_catch();
278 60 : forktest_gprofdir();
279 60 : }
280 :
281 : /* End of test (potentially in child) */
282 40 : void libhttp_checked_uninitialized_teardown() {
283 40 : signals_release();
284 40 : }
285 : /************************************************************************/
286 43 : void libhttp_unchecked_common_setup() {
287 43 : forktest_init();
288 43 : }
289 3 : void libhttp_unchecked_common_teardown() {
290 3 : }
291 : /************************************************************************/
292 44 : Suite* libhttp_suite() {
293 : Suite *s;
294 : TCase *tc;
295 :
296 44 : s = suite_create("LibHTTP");
297 :
298 44 : tc = tcase_create("LibHTTPUninitialized");
299 44 : tcase_set_tags(tc,"LibHTTPTag LibHTTPTag2");
300 : /* tcase_set_timeout(tc,5); */ /* seconds */
301 44 : tcase_add_checked_fixture(tc, libhttp_checked_uninitialized_setup, libhttp_checked_uninitialized_teardown);
302 44 : tcase_add_unchecked_fixture(tc, libhttp_unchecked_common_setup, libhttp_unchecked_common_teardown);
303 44 : suite_add_tcase(s, tc);
304 44 : tcase_add_test_raise_signal(tc, libhttp_test,6);
305 44 : tcase_add_test_raise_signal(tc, libhttp_httpheader_new_null,SIG_ABRTSEGV);
306 44 : tcase_add_test_raise_signal(tc, libhttp_httpheader_del_null,SIG_ABRTSEGV);
307 44 : tcase_add_test(tc, libhttp_httpheader_newdel);
308 44 : tcase_add_test(tc, libhttp_httpheader_newdel_empty);
309 44 : tcase_add_test_raise_signal(tc, libhttp_httpheader_newdel_doublefree,11);
310 44 : tcase_add_test(tc, libhttp_httpheader_basicauth);
311 44 : tcase_add_test(tc, libhttp_newdel);
312 44 : tcase_add_test_raise_signal(tc, libhttp_newdel_doublefree,11);
313 44 : tcase_add_test_raise_signal(tc, libhttp_del_null,SIG_ABRTSEGV);
314 44 : tcase_add_test_raise_signal(tc, libhttp_setbody_null_empty,SIG_ABRTSEGV);
315 44 : tcase_add_test_raise_signal(tc, libhttp_setbody_null,SIG_ABRTSEGV);
316 44 : tcase_add_test(tc, libhttp_setbody_empty);
317 44 : tcase_add_test(tc, libhttp_setbody_valid);
318 44 : tcase_add_test(tc, libhttp_getbody_first);
319 44 : tcase_add_test_raise_signal(tc, libhttp_addheader_null_null,SIG_ABRTSEGV);
320 44 : tcase_add_test_raise_signal(tc, libhttp_addheader_null,SIG_ABRTSEGV);
321 44 : tcase_add_test(tc, libhttp_addheader_valid);
322 44 : tcase_add_test(tc, libhttp_addheader_doubleadd);
323 : /*
324 : * libhttp_firstheader
325 : * libhttp_nextheader
326 : * libhttp_findheader
327 : * libhttp_delheader
328 : */
329 44 : tcase_add_test(tc, libhttp_basictest);
330 :
331 44 : return s;
332 : }
333 : /* vim: set tw=80: */
|