LCOV - code coverage report
Current view: top level - test - suite_clusterlst.c (source / functions) Hit Total Coverage
Test: mkernel.info Lines: 246 262 93.9 %
Date: 2024-12-05 21:00:54 Functions: 17 17 100.0 %

          Line data    Source code
       1             : /**   @file  suite_clusterlst.c
       2             :  *   @brief  Check testsuite for clusterlst 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-19 18: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 "clusterlst.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           9 : START_TEST(clusterlst_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           9 :     forktest_only;
      38             : 
      39           6 :     abort();
      40             : }
      41             : END_TEST
      42             : 
      43             : /* Test variables used in setups, teardowns and tests */
      44             : void       (*func_clusterlist_add)(cluster_t* cluster)=NULL;
      45             : cluster_t* (*func_clusterlist_find) (const char* host)=NULL;
      46             : cluster_t* (*func_clusterlist_first)()=NULL;
      47             : cluster_t* (*func_clusterlist_next)()=NULL;
      48             : cluster_t* (*func_clusterlist_get)()=NULL;
      49             : cluster_t* ptr_cluster=NULL;
      50             : 
      51           3 : START_TEST(clusterlst_uninitialized_add) {
      52             :     /* Need to have a forked env to start uninitialized */
      53           3 :     forktest_only;
      54             : 
      55             :     /* Currently leaked */
      56           2 :     if (NULL==(ptr_cluster=malloc(sizeof(struct cluster_s)))) {
      57           0 :         perror("clusterlst_uninitialized_add");
      58           0 :         abort();
      59             :     }
      60           2 :     ptr_cluster->host=NULL;
      61           2 :     ptr_cluster->user=NULL;
      62           2 :     ptr_cluster->pass=NULL;
      63           2 :     ptr_cluster->insecure=NULL;
      64           2 :     ptr_cluster->cacert=NULL;
      65           2 :     clusterlist_add(ptr_cluster);
      66             : 
      67             :     /* Check functors update by init */
      68           2 :     ck_assert(func_clusterlist_add!=clusterlist_add);
      69           2 :     ck_assert(func_clusterlist_find!=clusterlist_find);
      70           2 :     ck_assert(func_clusterlist_first!=clusterlist_first);
      71           2 :     ck_assert(func_clusterlist_next!=clusterlist_next);
      72           2 :     ck_assert(func_clusterlist_get!=clusterlist_get);
      73             : 
      74             :     /* Second run should not invoke init and change the pointers and functors */
      75           2 :     func_clusterlist_add=clusterlist_add;
      76           2 :     func_clusterlist_find=clusterlist_find;
      77           2 :     func_clusterlist_first=clusterlist_first;
      78           2 :     func_clusterlist_next=clusterlist_next;
      79           2 :     func_clusterlist_get=clusterlist_get;
      80             : 
      81             :     /* No need for another cluster data, reuse the same */
      82           2 :     clusterlist_add(ptr_cluster);
      83             : 
      84           2 :     ck_assert(func_clusterlist_add==clusterlist_add);
      85           2 :     ck_assert(func_clusterlist_find==clusterlist_find);
      86           2 :     ck_assert(func_clusterlist_first==clusterlist_first);
      87           2 :     ck_assert(func_clusterlist_next==clusterlist_next);
      88           2 :     ck_assert(func_clusterlist_get==clusterlist_get);
      89             : }
      90             : END_TEST
      91             : 
      92           3 : START_TEST(clusterlst_uninitialized_find) {
      93             :     /* Need to have a forked env to start uninitialized */
      94           3 :     forktest_only;
      95             : 
      96           2 :     ptr_cluster=clusterlist_find("");
      97           2 :     ck_assert(ptr_cluster==NULL);
      98             : 
      99             :     /* Check functors update by init */
     100           2 :     ck_assert(func_clusterlist_add!=clusterlist_add);
     101           2 :     ck_assert(func_clusterlist_find!=clusterlist_find);
     102           2 :     ck_assert(func_clusterlist_first!=clusterlist_first);
     103           2 :     ck_assert(func_clusterlist_next!=clusterlist_next);
     104           2 :     ck_assert(func_clusterlist_get!=clusterlist_get);
     105             : 
     106             :     /* Second run should not invoke init and change the pointers and functors */
     107           2 :     func_clusterlist_add=clusterlist_add;
     108           2 :     func_clusterlist_find=clusterlist_find;
     109           2 :     func_clusterlist_first=clusterlist_first;
     110           2 :     func_clusterlist_next=clusterlist_next;
     111           2 :     func_clusterlist_get=clusterlist_get;
     112             : 
     113           2 :     ptr_cluster=clusterlist_find("");
     114           2 :     ck_assert(ptr_cluster==NULL);
     115             : 
     116           2 :     ck_assert(func_clusterlist_add==clusterlist_add);
     117           2 :     ck_assert(func_clusterlist_find==clusterlist_find);
     118           2 :     ck_assert(func_clusterlist_first==clusterlist_first);
     119           2 :     ck_assert(func_clusterlist_next==clusterlist_next);
     120           2 :     ck_assert(func_clusterlist_get==clusterlist_get);
     121             : }
     122             : END_TEST
     123             : 
     124           3 : START_TEST(clusterlst_uninitialized_first) {
     125             :     /* Need to have a forked env to start uninitialized */
     126           3 :     forktest_only;
     127             : 
     128           2 :     ptr_cluster=clusterlist_first();
     129           2 :     ck_assert(ptr_cluster==NULL);
     130             : 
     131             :     /* Check functors update by init */
     132           2 :     ck_assert(func_clusterlist_add!=clusterlist_add);
     133           2 :     ck_assert(func_clusterlist_find!=clusterlist_find);
     134           2 :     ck_assert(func_clusterlist_first!=clusterlist_first);
     135           2 :     ck_assert(func_clusterlist_next!=clusterlist_next);
     136           2 :     ck_assert(func_clusterlist_get!=clusterlist_get);
     137             : 
     138             :     /* Second run should not invoke init and change the pointers and functors */
     139           2 :     func_clusterlist_add=clusterlist_add;
     140           2 :     func_clusterlist_find=clusterlist_find;
     141           2 :     func_clusterlist_first=clusterlist_first;
     142           2 :     func_clusterlist_next=clusterlist_next;
     143           2 :     func_clusterlist_get=clusterlist_get;
     144             : 
     145           2 :     ptr_cluster=clusterlist_first();
     146           2 :     ck_assert(ptr_cluster==NULL);
     147             : 
     148           2 :     ck_assert(func_clusterlist_add==clusterlist_add);
     149           2 :     ck_assert(func_clusterlist_find==clusterlist_find);
     150           2 :     ck_assert(func_clusterlist_first==clusterlist_first);
     151           2 :     ck_assert(func_clusterlist_next==clusterlist_next);
     152           2 :     ck_assert(func_clusterlist_get==clusterlist_get);
     153             : }
     154             : END_TEST
     155             : 
     156           3 : START_TEST(clusterlst_uninitialized_next) {
     157             :     /* Need to have a forked env to start uninitialized */
     158           3 :     forktest_only;
     159             : 
     160           2 :     ptr_cluster=clusterlist_next();
     161           2 :     ck_assert(ptr_cluster==NULL);
     162             : 
     163             :     /* Check functors update by init */
     164           2 :     ck_assert(func_clusterlist_add!=clusterlist_add);
     165           2 :     ck_assert(func_clusterlist_find!=clusterlist_find);
     166           2 :     ck_assert(func_clusterlist_first!=clusterlist_first);
     167           2 :     ck_assert(func_clusterlist_next!=clusterlist_next);
     168           2 :     ck_assert(func_clusterlist_get!=clusterlist_get);
     169             : 
     170             :     /* Second run should not invoke init and change the pointers and functors */
     171           2 :     func_clusterlist_add=clusterlist_add;
     172           2 :     func_clusterlist_find=clusterlist_find;
     173           2 :     func_clusterlist_first=clusterlist_first;
     174           2 :     func_clusterlist_next=clusterlist_next;
     175           2 :     func_clusterlist_get=clusterlist_get;
     176             : 
     177           2 :     ptr_cluster=clusterlist_next();
     178           2 :     ck_assert(ptr_cluster==NULL);
     179             : 
     180           2 :     ck_assert(func_clusterlist_add==clusterlist_add);
     181           2 :     ck_assert(func_clusterlist_find==clusterlist_find);
     182           2 :     ck_assert(func_clusterlist_first==clusterlist_first);
     183           2 :     ck_assert(func_clusterlist_next==clusterlist_next);
     184           2 :     ck_assert(func_clusterlist_get==clusterlist_get);
     185             : }
     186             : END_TEST
     187             : 
     188           3 : START_TEST(clusterlst_uninitialized_get) {
     189             :     /* Need to have a forked env to start uninitialized */
     190           3 :     forktest_only;
     191             : 
     192           2 :     ptr_cluster=clusterlist_get();
     193           2 :     ck_assert(ptr_cluster==NULL);
     194             : 
     195             :     /* Check functors update by init */
     196           2 :     ck_assert(func_clusterlist_add!=clusterlist_add);
     197           2 :     ck_assert(func_clusterlist_find!=clusterlist_find);
     198           2 :     ck_assert(func_clusterlist_first!=clusterlist_first);
     199           2 :     ck_assert(func_clusterlist_next!=clusterlist_next);
     200           2 :     ck_assert(func_clusterlist_get!=clusterlist_get);
     201             : 
     202             :     /* Second run should not invoke init and change the pointers and functors */
     203           2 :     func_clusterlist_add=clusterlist_add;
     204           2 :     func_clusterlist_find=clusterlist_find;
     205           2 :     func_clusterlist_first=clusterlist_first;
     206           2 :     func_clusterlist_next=clusterlist_next;
     207           2 :     func_clusterlist_get=clusterlist_get;
     208             : 
     209           2 :     ptr_cluster=clusterlist_get();
     210           2 :     ck_assert(ptr_cluster==NULL);
     211             : 
     212           2 :     ck_assert(func_clusterlist_add==clusterlist_add);
     213           2 :     ck_assert(func_clusterlist_find==clusterlist_find);
     214           2 :     ck_assert(func_clusterlist_first==clusterlist_first);
     215           2 :     ck_assert(func_clusterlist_next==clusterlist_next);
     216           2 :     ck_assert(func_clusterlist_get==clusterlist_get);
     217             : }
     218             : END_TEST
     219             : 
     220           6 : START_TEST(clusterlst_initialized_add) {
     221             :     /* Need to have a forked env to start initialized and empty */
     222           6 :     forktest_only;
     223             : 
     224             :     /* Currently leaked */
     225           4 :     if (NULL==(ptr_cluster=malloc(sizeof(struct cluster_s)))) {
     226           0 :         perror("clusterlst_uninitialized_add");
     227           0 :         abort();
     228             :     }
     229           4 :     ptr_cluster->host=NULL;
     230           4 :     ptr_cluster->user=NULL;
     231           4 :     ptr_cluster->pass=NULL;
     232           4 :     ptr_cluster->insecure=NULL;
     233           4 :     ptr_cluster->cacert=NULL;
     234           4 :     clusterlist_add(ptr_cluster);
     235             : 
     236           4 :     ck_assert(func_clusterlist_add==clusterlist_add);
     237           4 :     ck_assert(func_clusterlist_find==clusterlist_find);
     238           4 :     ck_assert(func_clusterlist_first==clusterlist_first);
     239           4 :     ck_assert(func_clusterlist_next==clusterlist_next);
     240           4 :     ck_assert(func_clusterlist_get==clusterlist_get);
     241             : }
     242             : END_TEST
     243             : 
     244           3 : START_TEST(clusterlst_initialized_find) {
     245             :     /* Need to have a forked env to start initialized and empty */
     246           3 :     forktest_only;
     247             : 
     248             :     /* Find in empty list */
     249           2 :     ck_assert(NULL==clusterlist_find(NULL));
     250           2 :     ck_assert(func_clusterlist_add==clusterlist_add);
     251           2 :     ck_assert(func_clusterlist_find==clusterlist_find);
     252           2 :     ck_assert(func_clusterlist_first==clusterlist_first);
     253           2 :     ck_assert(func_clusterlist_next==clusterlist_next);
     254           2 :     ck_assert(func_clusterlist_get==clusterlist_get);
     255             : 
     256           2 :     ck_assert(NULL==clusterlist_find(""));
     257           2 :     ck_assert(func_clusterlist_add==clusterlist_add);
     258           2 :     ck_assert(func_clusterlist_find==clusterlist_find);
     259           2 :     ck_assert(func_clusterlist_first==clusterlist_first);
     260           2 :     ck_assert(func_clusterlist_next==clusterlist_next);
     261           2 :     ck_assert(func_clusterlist_get==clusterlist_get);
     262             : 
     263           2 :     ck_assert(NULL==clusterlist_find("a"));
     264           2 :     ck_assert(func_clusterlist_add==clusterlist_add);
     265           2 :     ck_assert(func_clusterlist_find==clusterlist_find);
     266           2 :     ck_assert(func_clusterlist_first==clusterlist_first);
     267           2 :     ck_assert(func_clusterlist_next==clusterlist_next);
     268           2 :     ck_assert(func_clusterlist_get==clusterlist_get);
     269             : }
     270             : END_TEST
     271             : 
     272           6 : START_TEST(clusterlst_initialized_firstnextget) {
     273             :     /* Need to have a forked env to start initialized and empty */
     274           6 :     forktest_only;
     275             : 
     276           4 :     ptr_cluster = clusterlist_first();
     277          16 :     while (ptr_cluster) {
     278          12 :         ptr_cluster = clusterlist_next();
     279          12 :         ck_assert(clusterlist_get()==ptr_cluster);
     280             :     }
     281           4 :     ck_assert(NULL==clusterlist_next());
     282           4 :     ck_assert(NULL==clusterlist_get());
     283             : }
     284             : END_TEST
     285             : 
     286           3 : START_TEST(clusterlst_populated_find) {
     287             :     /* Find in NULL,"","a","b","",NULL */
     288             :     /* Find first, middle, last, nonexistant, duplicate */
     289           3 :     ck_assert(NULL!=clusterlist_find(NULL));
     290           3 :     ck_assert(NULL!=clusterlist_find(""));
     291           3 :     ck_assert(NULL!=clusterlist_find("a"));
     292           3 :     ck_assert(NULL==clusterlist_find("c"));
     293           3 : }
     294             : END_TEST
     295             : 
     296             : /************************************************************************/
     297             : /* Begining of test (potentially in child) */
     298          18 : void clusterlst_checked_uninitialized_setup() {
     299          18 :     signals_catch();
     300          18 :     forktest_gprofdir();
     301             : 
     302          18 :     func_clusterlist_add=clusterlist_add;
     303          18 :     func_clusterlist_find=clusterlist_find;
     304          18 :     func_clusterlist_first=clusterlist_first;
     305          18 :     func_clusterlist_next=clusterlist_next;
     306          18 :     func_clusterlist_get=clusterlist_get;
     307          18 : }
     308             : 
     309             : /* Begining of test (potentially in child) */
     310          12 : void clusterlst_checked_initialized_setup() {
     311          12 :     signals_catch();
     312          12 :     forktest_gprofdir();
     313             : 
     314          12 :     clusterlist_get();
     315          12 :     func_clusterlist_add=clusterlist_add;
     316          12 :     func_clusterlist_find=clusterlist_find;
     317          12 :     func_clusterlist_first=clusterlist_first;
     318          12 :     func_clusterlist_next=clusterlist_next;
     319          12 :     func_clusterlist_get=clusterlist_get;
     320          12 : }
     321             : 
     322             : /* Begining of test (potentially in child) */
     323          12 : void clusterlst_checked_populated_setup() {
     324             : 
     325          12 :     signals_catch();
     326          12 :     forktest_gprofdir();
     327             : 
     328             :     /* Currently leaked */
     329          12 :     if (NULL==(ptr_cluster=malloc(sizeof(struct cluster_s)))) {
     330           0 :         perror("clusterlst_uninitialized_add");
     331           0 :         abort();
     332             :     }
     333          12 :     ptr_cluster->host=NULL;
     334          12 :     clusterlist_add(ptr_cluster);
     335             : 
     336             :     /* Currently leaked */
     337          12 :     if (NULL==(ptr_cluster=malloc(sizeof(struct cluster_s)))) {
     338           0 :         perror("clusterlst_uninitialized_add");
     339           0 :         abort();
     340             :     }
     341          12 :     ptr_cluster->host=strdup("");
     342          12 :     clusterlist_add(ptr_cluster);
     343             : 
     344             :     /* Currently leaked */
     345          12 :     if (NULL==(ptr_cluster=malloc(sizeof(struct cluster_s)))) {
     346           0 :         perror("clusterlst_uninitialized_add");
     347           0 :         abort();
     348             :     }
     349          12 :     ptr_cluster->host=strdup("a");
     350          12 :     clusterlist_add(ptr_cluster);
     351             : 
     352             :     /* Currently leaked */
     353          12 :     if (NULL==(ptr_cluster=malloc(sizeof(struct cluster_s)))) {
     354           0 :         perror("clusterlst_uninitialized_add");
     355           0 :         abort();
     356             :     }
     357          12 :     ptr_cluster->host=strdup("b");
     358          12 :     clusterlist_add(ptr_cluster);
     359             : 
     360             :     /* Currently leaked */
     361          12 :     if (NULL==(ptr_cluster=malloc(sizeof(struct cluster_s)))) {
     362           0 :         perror("clusterlst_uninitialized_add");
     363           0 :         abort();
     364             :     }
     365          12 :     ptr_cluster->host=strdup("");
     366          12 :     clusterlist_add(ptr_cluster);
     367             : 
     368             :     /* Currently leaked */
     369          12 :     if (NULL==(ptr_cluster=malloc(sizeof(struct cluster_s)))) {
     370           0 :         perror("clusterlst_uninitialized_add");
     371           0 :         abort();
     372             :     }
     373          12 :     ptr_cluster->host=NULL;
     374          12 :     clusterlist_add(ptr_cluster);
     375             : 
     376          12 :     func_clusterlist_add=clusterlist_add;
     377          12 :     func_clusterlist_find=clusterlist_find;
     378          12 :     func_clusterlist_first=clusterlist_first;
     379          12 :     func_clusterlist_next=clusterlist_next;
     380          12 :     func_clusterlist_get=clusterlist_get;
     381          12 : }
     382             : 
     383             : /* End of test (potentially in child) */
     384          36 : void clusterlst_checked_uninitialized_teardown() {
     385          36 :     signals_release();
     386          36 : }
     387             : /************************************************************************/
     388          61 : void clusterlst_unchecked_common_setup() {
     389          61 :     forktest_init();
     390          61 : }
     391          33 : void clusterlst_unchecked_common_teardown() {
     392          33 : }
     393             : /************************************************************************/
     394          32 : Suite* clusterlst_suite() {
     395             :     Suite *s;
     396             :     TCase *tc;
     397             : 
     398          32 :     s = suite_create("Clusterlst");
     399             : 
     400          32 :     tc = tcase_create("ClusterlstUninitialized");
     401          32 :     tcase_set_tags(tc,"ClusterlstTag ClusterlstTag2");
     402             :     /* tcase_set_timeout(tc,5); */ /* seconds */
     403          32 :     tcase_add_checked_fixture(tc, clusterlst_checked_uninitialized_setup, clusterlst_checked_uninitialized_teardown);
     404          32 :     tcase_add_unchecked_fixture(tc, clusterlst_unchecked_common_setup, clusterlst_unchecked_common_teardown);
     405          32 :     suite_add_tcase(s, tc);
     406          32 :     tcase_add_test_raise_signal(tc, clusterlst_test,6);
     407          32 :     tcase_add_test(tc, clusterlst_uninitialized_add);
     408          32 :     tcase_add_test(tc, clusterlst_uninitialized_find);
     409          32 :     tcase_add_test(tc, clusterlst_uninitialized_first);
     410          32 :     tcase_add_test(tc, clusterlst_uninitialized_next);
     411          32 :     tcase_add_test(tc, clusterlst_uninitialized_get);
     412             : 
     413          32 :     tc = tcase_create("ClusterlstInitialized");
     414          32 :     tcase_set_tags(tc,"ClusterlstTag ClusterlstTag2");
     415             :     /* tcase_set_timeout(tc,5); */ /* seconds */
     416          32 :     tcase_add_checked_fixture(tc, clusterlst_checked_initialized_setup, clusterlst_checked_uninitialized_teardown);
     417          32 :     tcase_add_unchecked_fixture(tc, clusterlst_unchecked_common_setup, clusterlst_unchecked_common_teardown);
     418          32 :     suite_add_tcase(s, tc);
     419          32 :     tcase_add_test_raise_signal(tc, clusterlst_test,6);
     420          32 :     tcase_add_test(tc, clusterlst_initialized_add);
     421          32 :     tcase_add_test(tc, clusterlst_initialized_find);
     422          32 :     tcase_add_test(tc, clusterlst_initialized_firstnextget);
     423             : 
     424          32 :     tc = tcase_create("ClusterlstPopulated");
     425          32 :     tcase_set_tags(tc,"ClusterlstTag ClusterlstTag2");
     426             :     /* tcase_set_timeout(tc,5); */ /* seconds */
     427          32 :     tcase_add_checked_fixture(tc, clusterlst_checked_populated_setup, clusterlst_checked_uninitialized_teardown);
     428          32 :     tcase_add_unchecked_fixture(tc, clusterlst_unchecked_common_setup, clusterlst_unchecked_common_teardown);
     429          32 :     suite_add_tcase(s, tc);
     430          32 :     tcase_add_test_raise_signal(tc, clusterlst_test,6);
     431          32 :     tcase_add_test(tc, clusterlst_initialized_add);
     432          32 :     tcase_add_test(tc, clusterlst_populated_find);
     433          32 :     tcase_add_test(tc, clusterlst_initialized_firstnextget);
     434             : 
     435          32 :     return s;
     436             : }
     437             : /* vim: set tw=80: */

Generated by: LCOV version 1.16