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-23 16:52
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_basictest) {
184 : char* output_str;
185 : HTTP_t* http;
186 :
187 : /* Build HTTP */
188 3 : http = HTTP_new();
189 3 : ck_assert(HTTP_addheader(http,"Host","localhost"));
190 3 : ck_assert(HTTP_addheader(http,"Host","localhost"));
191 3 : ck_assert(HTTP_addheader(http,"Content-type","text/html"));
192 3 : HTTP_setbody(http,"<HTML></HTML>\r\n");
193 :
194 : /* Generate request line */
195 3 : output_str=HTTP_buildrequest(HTTPMETHOD_GET,"/v1/api/bdbs",HTTPVERSION_HTTP11);
196 3 : printf("%s",output_str);
197 3 : free(output_str);
198 :
199 : /* Generate headers */
200 3 : output_str = HTTP_buildheaders(http);
201 3 : printf("%s",output_str);
202 3 : free(output_str);
203 :
204 : /* Generate body */
205 3 : printf("%s",HTTP_getbody(http));
206 :
207 : /* Cleanup */
208 3 : HTTP_del(http);
209 3 : }
210 : END_TEST
211 :
212 : /************************************************************************/
213 : /* Begining of test (potentially in child) */
214 36 : void libhttp_checked_uninitialized_setup() {
215 36 : signals_catch();
216 36 : forktest_gprofdir();
217 36 : }
218 :
219 : /* End of test (potentially in child) */
220 26 : void libhttp_checked_uninitialized_teardown() {
221 26 : signals_release();
222 26 : }
223 : /************************************************************************/
224 27 : void libhttp_unchecked_common_setup() {
225 27 : forktest_init();
226 27 : }
227 3 : void libhttp_unchecked_common_teardown() {
228 3 : }
229 : /************************************************************************/
230 28 : Suite* libhttp_suite() {
231 : Suite *s;
232 : TCase *tc;
233 :
234 28 : s = suite_create("LibHTTP");
235 :
236 28 : tc = tcase_create("LibHTTPUninitialized");
237 28 : tcase_set_tags(tc,"LibHTTPTag LibHTTPTag2");
238 : /* tcase_set_timeout(tc,5); */ /* seconds */
239 28 : tcase_add_checked_fixture(tc, libhttp_checked_uninitialized_setup, libhttp_checked_uninitialized_teardown);
240 28 : tcase_add_unchecked_fixture(tc, libhttp_unchecked_common_setup, libhttp_unchecked_common_teardown);
241 28 : suite_add_tcase(s, tc);
242 28 : tcase_add_test_raise_signal(tc, libhttp_test,6);
243 28 : tcase_add_test(tc, libhttp_new);
244 28 : tcase_add_test_raise_signal(tc, libhttp_del_null,SIG_ABRTSEGV);
245 28 : tcase_add_test(tc, libhttp_del_empty);
246 28 : tcase_add_test(tc, libhttp_del_populated);
247 28 : tcase_add_test_raise_signal(tc, libhttp_setbody_nullhttp,SIG_ABRTSEGV);
248 28 : tcase_add_test_raise_signal(tc, libhttp_setbody_nullbody,SIG_ABRTSEGV);
249 28 : tcase_add_test(tc, libhttp_setbody_empty);
250 28 : tcase_add_test(tc, libhttp_setbody_valid);
251 28 : tcase_add_test_raise_signal(tc, libhttp_getbody_null,SIG_ABRTSEGV);
252 28 : tcase_add_test(tc, libhttp_getbody_populated);
253 28 : tcase_add_test(tc, libhttp_basictest);
254 :
255 28 : return s;
256 : }
257 : /* vim: set tw=80: */
|