Line data Source code
1 : /** @file suite_libhttp.c
2 : * @brief Check testsuite for libhttp functions
3 : * @author François Cerbelle (Fanfan), francois@cerbelle.net
4 : *
5 : * @internal
6 : * Created: 20/03/2022
7 : * Revision: none
8 : * Last modified: 2024-11-20 22:52
9 : * Compiler: gcc
10 : * Organization: Cerbelle.net
11 : * Copyright: Copyright (c) 2024, François Cerbelle
12 : *
13 : * This source code is released for free distribution under the terms of the
14 : * GNU General Public License as published by the Free Software Foundation.
15 : */
16 :
17 : #ifdef HAVE_CONFIG_H
18 : #include "config.h"
19 : #endif
20 :
21 : #include <check.h>
22 : #include <stdlib.h>
23 : #include "checktools.inc"
24 :
25 : #include "libhttp.h"
26 : #include <stdio.h> /* perror */
27 :
28 : /** Dummy test to trigger fixtures
29 : *
30 : * This test is defined and added to all testcases in order to execute at
31 : * least one test test in each testcase of the testsuite, trigger the
32 : * fixtures and have a decent coverage report.
33 : */
34 3 : START_TEST(libhttp_test) {
35 : /* This test is supposed to trigger a SIGABRT(6) and will crash the
36 : * whole testsuite if not caught or not in a child process */
37 3 : forktest_only;
38 :
39 2 : abort();
40 : }
41 : END_TEST
42 :
43 3 : START_TEST(libhttp_httpheader_new_null) {
44 : /* SIG11 in release build, SIG6(assert/abort) in debug build */
45 3 : forktest_only;
46 :
47 2 : HTTPHeader_new(NULL,NULL);
48 : }
49 : END_TEST
50 :
51 3 : START_TEST(libhttp_httpheader_del_null) {
52 : /* SIG11 in release build, SIG6(assert/abort) in debug build */
53 3 : forktest_only;
54 :
55 2 : HTTPHeader_del(NULL);
56 : }
57 : END_TEST
58 :
59 3 : START_TEST(libhttp_httpheader_newdel) {
60 3 : HTTPHeader_t* header=NULL;
61 :
62 3 : header = HTTPHeader_new("name","value");
63 3 : ck_assert(header);
64 3 : ck_assert(0==strcmp("name",HTTPHeader_getname(header)));
65 3 : ck_assert(0==strcmp("value",HTTPHeader_getvalue(header)));
66 3 : HTTPHeader_del(header);
67 3 : }
68 : END_TEST
69 :
70 3 : START_TEST(libhttp_httpheader_newdel_empty) {
71 3 : HTTPHeader_t* header=NULL;
72 :
73 3 : header = HTTPHeader_new("","");
74 3 : ck_assert(header);
75 3 : ck_assert(0==strcmp("",HTTPHeader_getname(header)));
76 3 : ck_assert(0==strcmp("",HTTPHeader_getvalue(header)));
77 3 : HTTPHeader_del(header);
78 3 : }
79 : END_TEST
80 :
81 3 : START_TEST(libhttp_httpheader_newdel_doublefree) {
82 3 : HTTPHeader_t* header=NULL;
83 :
84 : /* Always SIG11 (not caught by assertions) */
85 3 : forktest_only;
86 :
87 2 : header = HTTPHeader_new("","");
88 2 : ck_assert(header);
89 2 : ck_assert(0==strcmp("",HTTPHeader_getname(header)));
90 2 : ck_assert(0==strcmp("",HTTPHeader_getvalue(header)));
91 2 : HTTPHeader_del(header);
92 2 : HTTPHeader_del(header);
93 : }
94 : END_TEST
95 :
96 3 : START_TEST(libhttp_httpheader_basicauth) {
97 3 : HTTPHeader_t* header=NULL;
98 :
99 3 : header = HTTPHeader_basicauth("admin@demo.com","password");
100 3 : ck_assert(0==strcmp("Authorization",HTTPHeader_getname(header)));
101 : /* Could be dynamically created with base64.header/base64.c inclusion */
102 3 : ck_assert(0==strcmp("Basic YWRtaW5AZGVtby5jb206cGFzc3dvcmQ=",HTTPHeader_getvalue(header)));
103 3 : HTTPHeader_del(header);
104 3 : }
105 : END_TEST
106 :
107 3 : START_TEST(libhttp_newdel) {
108 : HTTP_t* uut;
109 :
110 3 : uut = HTTP_new();
111 3 : HTTP_del(uut);
112 3 : }
113 : END_TEST
114 :
115 3 : START_TEST(libhttp_newdel_doublefree) {
116 : HTTP_t* uut;
117 :
118 : /* Always SIG11 (not caught by assertions) */
119 3 : forktest_only;
120 :
121 2 : uut = HTTP_new();
122 2 : HTTP_del(uut);
123 :
124 : /* Double free should trigger SIG11 */
125 2 : HTTP_del(uut);
126 : }
127 : END_TEST
128 :
129 3 : START_TEST(libhttp_del_null) {
130 : /* SIG11 in release build, SIG6(assert/abort) in debug build */
131 3 : forktest_only;
132 :
133 2 : HTTP_del(NULL);
134 : }
135 : END_TEST
136 :
137 3 : START_TEST(libhttp_setbody_null_empty) {
138 : /* SIG11 in release build, SIG6(assert/abort) in debug build */
139 3 : forktest_only;
140 2 : HTTP_setbody(NULL,"");
141 : }
142 : END_TEST
143 :
144 3 : START_TEST(libhttp_setbody_null) {
145 : /* SIG11 in release build, SIG6(assert/abort) in debug build */
146 3 : forktest_only;
147 : HTTP_t* uut;
148 2 : uut = HTTP_new();
149 2 : HTTP_setbody(uut,NULL);
150 : }
151 : END_TEST
152 :
153 3 : START_TEST(libhttp_setbody_empty) {
154 : HTTP_t* uut;
155 3 : uut = HTTP_new();
156 3 : HTTP_setbody(uut,"");
157 3 : ck_assert(0==strcmp("",HTTP_getbody(uut)));
158 3 : HTTP_del(uut);
159 3 : }
160 : END_TEST
161 :
162 3 : START_TEST(libhttp_setbody_valid) {
163 : HTTP_t* uut;
164 3 : uut = HTTP_new();
165 3 : HTTP_setbody(uut,"abc");
166 3 : ck_assert(0==strcmp("abc",HTTP_getbody(uut)));
167 3 : HTTP_del(uut);
168 3 : }
169 : END_TEST
170 :
171 3 : START_TEST(libhttp_getbody_first) {
172 : HTTP_t* uut;
173 3 : uut = HTTP_new();
174 3 : ck_assert(0==strcmp("",HTTP_getbody(uut)));
175 3 : HTTP_del(uut);
176 3 : }
177 : END_TEST
178 :
179 3 : START_TEST(libhttp_basictest) {
180 : char* tmp;
181 : HTTP_t* uut;
182 3 : uut = HTTP_new();
183 3 : HTTP_setbody(uut,"<HTML/>\n");
184 3 : HTTP_addheader(uut,"Host","localhost");
185 :
186 3 : tmp=HTTP_getrequest(HTTPMETHOD_GET,"/v1/api/bdbs",HTTPVERSION_HTTP11);
187 3 : printf("%s",tmp);
188 3 : free(tmp);
189 :
190 3 : tmp = HTTP_getheaders(uut);
191 3 : printf("%s\r\n",tmp);
192 :
193 3 : printf("%s",HTTP_getbody(uut));
194 3 : HTTP_del(uut);
195 :
196 3 : }
197 : END_TEST
198 :
199 :
200 : /************************************************************************/
201 : /* Begining of test (potentially in child) */
202 48 : void libhttp_checked_uninitialized_setup() {
203 48 : signals_catch();
204 48 : forktest_gprofdir();
205 48 : }
206 :
207 : /* End of test (potentially in child) */
208 32 : void libhttp_checked_uninitialized_teardown() {
209 32 : signals_release();
210 32 : }
211 : /************************************************************************/
212 35 : void libhttp_unchecked_common_setup() {
213 35 : forktest_init();
214 35 : }
215 3 : void libhttp_unchecked_common_teardown() {
216 3 : }
217 : /************************************************************************/
218 36 : Suite* libhttp_suite() {
219 : Suite *s;
220 : TCase *tc;
221 :
222 36 : s = suite_create("LibHTTP");
223 :
224 36 : tc = tcase_create("LibHTTPUninitialized");
225 36 : tcase_set_tags(tc,"LibHTTPTag LibHTTPTag2");
226 : /* tcase_set_timeout(tc,5); */ /* seconds */
227 36 : tcase_add_checked_fixture(tc, libhttp_checked_uninitialized_setup, libhttp_checked_uninitialized_teardown);
228 36 : tcase_add_unchecked_fixture(tc, libhttp_unchecked_common_setup, libhttp_unchecked_common_teardown);
229 36 : suite_add_tcase(s, tc);
230 36 : tcase_add_test_raise_signal(tc, libhttp_test,6);
231 36 : tcase_add_test_raise_signal(tc, libhttp_httpheader_new_null,SIG_ABRTSEGV);
232 36 : tcase_add_test_raise_signal(tc, libhttp_httpheader_del_null,SIG_ABRTSEGV);
233 36 : tcase_add_test(tc, libhttp_httpheader_newdel);
234 36 : tcase_add_test(tc, libhttp_httpheader_newdel_empty);
235 36 : tcase_add_test_raise_signal(tc, libhttp_httpheader_newdel_doublefree,11);
236 36 : tcase_add_test(tc, libhttp_httpheader_basicauth);
237 36 : tcase_add_test(tc, libhttp_newdel);
238 36 : tcase_add_test_raise_signal(tc, libhttp_newdel_doublefree,11);
239 36 : tcase_add_test_raise_signal(tc, libhttp_del_null,SIG_ABRTSEGV);
240 36 : tcase_add_test_raise_signal(tc, libhttp_setbody_null_empty,SIG_ABRTSEGV);
241 36 : tcase_add_test_raise_signal(tc, libhttp_setbody_null,SIG_ABRTSEGV);
242 36 : tcase_add_test(tc, libhttp_setbody_empty);
243 36 : tcase_add_test(tc, libhttp_setbody_valid);
244 36 : tcase_add_test(tc, libhttp_getbody_first);
245 36 : tcase_add_test(tc, libhttp_basictest);
246 :
247 36 : return s;
248 : }
249 : /* vim: set tw=80: */
|