LCOV - code coverage report
Current view: top level - app/test - test_errno.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 19 23 82.6 %
Date: 2025-01-02 22:41:34 Functions: 2 2 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 9 18 50.0 %

           Branch data     Line data    Source code
       1                 :            : /* SPDX-License-Identifier: BSD-3-Clause
       2                 :            :  * Copyright(c) 2010-2014 Intel Corporation
       3                 :            :  */
       4                 :            : 
       5                 :            : #include <stdint.h>
       6                 :            : #include <stdio.h>
       7                 :            : #include <stdarg.h>
       8                 :            : #include <errno.h>
       9                 :            : #include <string.h>
      10                 :            : #include <rte_per_lcore.h>
      11                 :            : #include <rte_errno.h>
      12                 :            : #include <rte_string_fns.h>
      13                 :            : 
      14                 :            : #include "test.h"
      15                 :            : 
      16                 :            : static int
      17                 :          1 : test_errno(void)
      18                 :            : {
      19                 :            :         const char *rte_retval;
      20                 :            :         const char *libc_retval;
      21                 :            : 
      22                 :            : #ifndef RTE_EXEC_ENV_WINDOWS
      23                 :            : #ifdef RTE_EXEC_ENV_FREEBSD
      24                 :            :         /* BSD has a colon in the string, unlike linux */
      25                 :            :         const char unknown_code_result[] = "Unknown error: %d";
      26                 :            : #else
      27                 :          1 :         const char unknown_code_result[] = "Unknown error %d";
      28                 :            : #endif
      29                 :            :         char expected_libc_retval[sizeof(unknown_code_result) + 3];
      30                 :            : #else
      31                 :            :         /* Windows doesn't return error number for error greater than MAX_errno*/
      32                 :            :         static const char expected_libc_retval[] = "Unknown error";
      33                 :            : #endif
      34                 :            : 
      35                 :            :         /* use a small selection of standard errors for testing */
      36                 :          1 :         int std_errs[] = {EAGAIN, EBADF, EACCES, EINTR, EINVAL};
      37                 :            :         /* test ALL registered RTE error codes for overlap */
      38                 :          1 :         int rte_errs[] = {E_RTE_SECONDARY, E_RTE_NO_CONFIG};
      39                 :            :         unsigned i;
      40                 :            : 
      41                 :          1 :         rte_errno = 0;
      42                 :            :         if (rte_errno != 0)
      43                 :            :                 return -1;
      44                 :            :         /* check for standard errors we return the same as libc */
      45         [ +  + ]:          6 :         for (i = 0; i < RTE_DIM(std_errs); i++) {
      46                 :          5 :                 rte_retval = rte_strerror(std_errs[i]);
      47                 :          5 :                 libc_retval = strerror(std_errs[i]);
      48                 :            :                 printf("rte_strerror: '%s', strerror: '%s'\n",
      49                 :            :                                 rte_retval, libc_retval);
      50         [ +  - ]:          5 :                 if (strcmp(rte_retval, libc_retval) != 0)
      51                 :            :                         return -1;
      52                 :            :         }
      53                 :            :         /* for rte-specific errors ensure we return a different string
      54                 :            :          * and that the string for libc is for an unknown error
      55                 :            :          */
      56         [ +  + ]:          3 :         for (i = 0; i < RTE_DIM(rte_errs); i++) {
      57                 :          2 :                 rte_retval = rte_strerror(rte_errs[i]);
      58                 :          2 :                 libc_retval = strerror(rte_errs[i]);
      59                 :            :                 printf("rte_strerror: '%s', strerror: '%s'\n",
      60                 :            :                                 rte_retval, libc_retval);
      61         [ +  - ]:          2 :                 if (strcmp(rte_retval, libc_retval) == 0)
      62                 :            :                         return -1;
      63                 :            : #ifndef RTE_EXEC_ENV_WINDOWS
      64                 :            :                 /* generate appropriate error string for unknown error number
      65                 :            :                  * and then check that this is what we got back. If not, we have
      66                 :            :                  * a duplicate error number that conflicts with errno.h */
      67                 :            :                 snprintf(expected_libc_retval, sizeof(expected_libc_retval),
      68                 :            :                                 unknown_code_result, rte_errs[i]);
      69                 :            : #endif
      70         [ -  + ]:          2 :                 if ((strcmp(expected_libc_retval, libc_retval) != 0) &&
      71         [ #  # ]:          0 :                                 (strcmp("", libc_retval) != 0)){
      72                 :            :                         printf("Error, duplicate error code %d\n", rte_errs[i]);
      73                 :          0 :                         return -1;
      74                 :            :                 }
      75                 :            :         }
      76                 :            : 
      77                 :            :         /* ensure that beyond RTE_MAX_ERRNO, we always get an unknown code */
      78                 :          1 :         rte_retval = rte_strerror(RTE_MAX_ERRNO + 1);
      79                 :          1 :         libc_retval = strerror(RTE_MAX_ERRNO + 1);
      80                 :            : #ifndef RTE_EXEC_ENV_WINDOWS
      81                 :            :         snprintf(expected_libc_retval, sizeof(expected_libc_retval),
      82                 :            :                         unknown_code_result, RTE_MAX_ERRNO + 1);
      83                 :            : #endif
      84                 :            :         printf("rte_strerror: '%s', strerror: '%s'\n",
      85                 :            :                         rte_retval, libc_retval);
      86         [ +  - ]:          1 :         if ((strcmp(rte_retval, libc_retval) != 0) ||
      87         [ -  + ]:          1 :                         (strcmp(expected_libc_retval, libc_retval) != 0)){
      88         [ #  # ]:          0 :                 if (strcmp("", libc_retval) != 0){
      89                 :            :                         printf("Failed test for RTE_MAX_ERRNO + 1 value\n");
      90                 :          0 :                         return -1;
      91                 :            :                 }
      92                 :            :         }
      93                 :            : 
      94                 :            :         return 0;
      95                 :            : }
      96                 :            : 
      97                 :        251 : REGISTER_FAST_TEST(errno_autotest, true, true, test_errno);

Generated by: LCOV version 1.14