LCOV - code coverage report
Current view: top level - ccan/list/test - run-check-corrupt.c (source / functions) Hit Total Coverage
Test: skiboot.info Lines: 33 36 91.7 %
Date: 2024-01-02 21:04:04 Functions: 2 2 100.0 %
Branches: 0 0 -

           Branch data     Line data    Source code
       1                 :            : #include <setjmp.h>
       2                 :            : #include <stdlib.h>
       3                 :            : #include <stdio.h>
       4                 :            : #include <stdarg.h>
       5                 :            : #include <string.h>
       6                 :            : #include <err.h>
       7                 :            : 
       8                 :            : /* We don't actually want it to exit... */
       9                 :            : static jmp_buf aborted;
      10                 :            : #define abort() longjmp(aborted, 1)
      11                 :            : 
      12                 :            : #define fprintf my_fprintf
      13                 :            : static char printf_buffer[1000];
      14                 :            : 
      15                 :         30 : static int my_fprintf(FILE *stream, const char *format, ...)
      16                 :            : {
      17                 :            :         va_list ap;
      18                 :            :         int ret;
      19                 :            :         (void)stream;
      20                 :         30 :         va_start(ap, format);
      21                 :         30 :         ret = vsprintf(printf_buffer, format, ap);
      22                 :         30 :         va_end(ap);
      23                 :         30 :         return ret;
      24                 :            : }
      25                 :            : 
      26                 :            : #include <ccan/list/list.h>
      27                 :            : #include <ccan/tap/tap.h>
      28                 :            : #include <ccan/list/list.c>
      29                 :            : 
      30                 :         10 : int main(void)
      31                 :            : {
      32                 :            :         struct list_head list;
      33                 :            :         struct list_node n1;
      34                 :            :         char expect[100];
      35                 :            : 
      36                 :            :         plan_tests(9);
      37                 :            :         /* Empty list. */
      38                 :         10 :         list.n.next = &list.n;
      39                 :         10 :         list.n.prev = &list.n;
      40                 :         10 :         ok1(list_check(&list, NULL) == &list);
      41                 :            : 
      42                 :            :         /* Bad back ptr */
      43                 :         10 :         list.n.prev = &n1;
      44                 :            :         /* Non-aborting version. */
      45                 :         10 :         ok1(list_check(&list, NULL) == NULL);
      46                 :            : 
      47                 :            :         /* Aborting version. */
      48                 :         10 :         sprintf(expect, "test message: prev corrupt in node %p (0) of %p\n",
      49                 :            :                 &list, &list);
      50                 :         10 :         if (setjmp(aborted) == 0) {
      51                 :         10 :                 list_check(&list, "test message");
      52                 :          0 :                 fail("list_check on empty with bad back ptr didn't fail!");
      53                 :            :         } else {
      54                 :         10 :                 ok1(strcmp(printf_buffer, expect) == 0);
      55                 :            :         }
      56                 :            : 
      57                 :            :         /* n1 in list. */
      58                 :         10 :         list.n.next = &n1;
      59                 :         10 :         list.n.prev = &n1;
      60                 :         10 :         n1.prev = &list.n;
      61                 :         10 :         n1.next = &list.n;
      62                 :         10 :         ok1(list_check(&list, NULL) == &list);
      63                 :         10 :         ok1(list_check_node(&n1, NULL) == &n1);
      64                 :            : 
      65                 :            :         /* Bad back ptr */
      66                 :         10 :         n1.prev = &n1;
      67                 :         10 :         ok1(list_check(&list, NULL) == NULL);
      68                 :         10 :         ok1(list_check_node(&n1, NULL) == NULL);
      69                 :            : 
      70                 :            :         /* Aborting version. */
      71                 :         10 :         sprintf(expect, "test message: prev corrupt in node %p (1) of %p\n",
      72                 :            :                 &n1, &list);
      73                 :         10 :         if (setjmp(aborted) == 0) {
      74                 :         10 :                 list_check(&list, "test message");
      75                 :          0 :                 fail("list_check on n1 bad back ptr didn't fail!");
      76                 :            :         } else {
      77                 :         10 :                 ok1(strcmp(printf_buffer, expect) == 0);
      78                 :            :         }
      79                 :            : 
      80                 :         10 :         sprintf(expect, "test message: prev corrupt in node %p (0) of %p\n",
      81                 :            :                 &n1, &n1);
      82                 :         10 :         if (setjmp(aborted) == 0) {
      83                 :         10 :                 list_check_node(&n1, "test message");
      84                 :          0 :                 fail("list_check_node on n1 bad back ptr didn't fail!");
      85                 :            :         } else {
      86                 :         10 :                 ok1(strcmp(printf_buffer, expect) == 0);
      87                 :            :         }
      88                 :            : 
      89                 :         10 :         return exit_status();
      90                 :            : }

Generated by: LCOV version 1.14