LCOV - code coverage report
Current view: top level - test - suite_libhttp.c (source / functions) Hit Total Coverage
Test: mkernel.info Lines: 52 52 100.0 %
Date: 2024-11-20 21:09:21 Functions: 7 7 100.0 %

          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 20:57
       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_newdel) {
      44             :     char* tmp;
      45             :     HTTP_t* uut;
      46             : 
      47             : 
      48             :     HTTPHeader_t* h;
      49             : 
      50           3 :     h = NULL;
      51             :     /* SIG11 in release build, SIG6(assert/abort) in debug build */
      52             : #if 0
      53             :     h = HTTPHeader_new(NULL,NULL);
      54             : #endif
      55             : 
      56           3 :     h = NULL;
      57             : #if 0
      58             :     HTTPHeader_del(h);
      59             : #endif
      60             : 
      61             : #if 0
      62             :     h->name=NULL;
      63             :     HTTPHeader_del(h);
      64             : #endif
      65             : 
      66           3 :     h = NULL;
      67           3 :     h = HTTPHeader_new("name","value");
      68           3 :     ck_assert(h);
      69           3 :     ck_assert(0==strcmp("name",HTTPHeader_getname(h)));
      70           3 :     ck_assert(0==strcmp("value",HTTPHeader_getvalue(h)));
      71           3 :     printf("%s: %s\r\n",HTTPHeader_getname(h), HTTPHeader_getvalue(h));
      72           3 :     HTTPHeader_del(h);
      73             : 
      74             :     /* Double free failure */
      75             : #if 0
      76             :     HTTPHeader_del(h);
      77             : #endif
      78             : 
      79           3 :     h = NULL;
      80           3 :     h = HTTPHeader_basicauth("admin@demo.com","password");
      81           3 :     ck_assert(0==strcmp("Authorization",HTTPHeader_getname(h)));
      82             :     /* Could be dynamically created with base64.h/base64.c inclusion */
      83           3 :     ck_assert(0==strcmp("Basic YWRtaW5AZGVtby5jb206cGFzc3dvcmQ=",HTTPHeader_getvalue(h)));
      84           3 :     printf("%s: %s\r\n",HTTPHeader_getname(h), HTTPHeader_getvalue(h));
      85           3 :     HTTPHeader_del(h);
      86             : 
      87             : 
      88             : 
      89             : 
      90           3 :     uut = HTTP_new();
      91           3 :     HTTP_setbody(uut,"<HTML/>\n");
      92           3 :     HTTP_addheader(uut,"Host","localhost");
      93             : 
      94           3 :     tmp=HTTP_getrequest(HTTPMETHOD_GET,"/v1/api/bdbs",HTTPVERSION_HTTP11);
      95           3 :     printf("%s",tmp);
      96           3 :     free(tmp);
      97             : 
      98           3 :     tmp = HTTP_getheaders(uut);
      99           3 :     printf("%s\r\n",tmp);
     100             : 
     101           3 :     printf("%s",HTTP_getbody(uut));
     102           3 :     HTTP_del(uut);
     103             : 
     104           3 : }
     105             : END_TEST
     106             : 
     107             : /************************************************************************/
     108             : /* Begining of test (potentially in child) */
     109           6 : void libhttp_checked_uninitialized_setup() {
     110           6 :     signals_catch();
     111           6 :     forktest_gprofdir();
     112           6 : }
     113             : 
     114             : /* End of test (potentially in child) */
     115           4 : void libhttp_checked_uninitialized_teardown() {
     116           4 :     signals_release();
     117           4 : }
     118             : /************************************************************************/
     119           7 : void libhttp_unchecked_common_setup() {
     120           7 :     forktest_init();
     121           7 : }
     122           3 : void libhttp_unchecked_common_teardown() {
     123           3 : }
     124             : /************************************************************************/
     125           8 : Suite* libhttp_suite() {
     126             :     Suite *s;
     127             :     TCase *tc;
     128             : 
     129           8 :     s = suite_create("LibHTTP");
     130             : 
     131           8 :     tc = tcase_create("LibHTTPUninitialized");
     132           8 :     tcase_set_tags(tc,"LibHTTPTag LibHTTPTag2");
     133             :     /* tcase_set_timeout(tc,5); */ /* seconds */
     134           8 :     tcase_add_checked_fixture(tc, libhttp_checked_uninitialized_setup, libhttp_checked_uninitialized_teardown);
     135           8 :     tcase_add_unchecked_fixture(tc, libhttp_unchecked_common_setup, libhttp_unchecked_common_teardown);
     136           8 :     suite_add_tcase(s, tc);
     137           8 :     tcase_add_test_raise_signal(tc, libhttp_test,6);
     138           8 :     tcase_add_test(tc, libhttp_newdel);
     139             : 
     140           8 :     return s;
     141             : }
     142             : /* vim: set tw=80: */

Generated by: LCOV version 1.16