LCOV - code coverage report
Current view: top level - spdk/test/unit/lib/util/file.c - file_ut.c (source / functions) Hit Total Coverage
Test: Combined Lines: 0 71 0.0 %
Date: 2024-07-16 01:31:00 Functions: 0 3 0.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 0 48 0.0 %

           Branch data     Line data    Source code
       1                 :            : /*   SPDX-License-Identifier: BSD-3-Clause
       2                 :            :  *   Copyright (C) 2024 Samsung Electronics Co., Ltd.
       3                 :            :  *   All rights reserved.
       4                 :            :  */
       5                 :            : 
       6                 :            : #include "spdk/stdinc.h"
       7                 :            : #include "spdk_internal/cunit.h"
       8                 :            : #include "util/file.c"
       9                 :            : 
      10                 :            : static void
      11                 :          0 : _read_sysfs_attribute(void)
      12                 :            : {
      13                 :            :         /* Don't try to use real sysfs paths for the unit test. Instead
      14                 :            :          * simulate sysfs attributes with some temporary files.
      15                 :            :          */
      16                 :          0 :         const char *path = "/tmp/spdk_file_ut_2024";
      17                 :          0 :         const char *setup = "spdk_unit_tests\n";
      18                 :          0 :         char *attr = NULL;
      19                 :            :         FILE *f;
      20                 :            :         int rc;
      21                 :            : 
      22                 :          0 :         f = fopen(path, "w");
      23         [ #  # ]:          0 :         SPDK_CU_ASSERT_FATAL(f != NULL);
      24                 :            : 
      25   [ #  #  #  #  :          0 :         rc = fwrite(setup, strlen(setup) + 1, 1, f);
                   #  # ]
      26                 :          0 :         CU_ASSERT(rc == 1);
      27                 :            : 
      28                 :          0 :         rc = fclose(f);
      29                 :          0 :         CU_ASSERT(rc == 0);
      30                 :            : 
      31                 :          0 :         rc = spdk_read_sysfs_attribute(&attr, "/tmp/spdk_file_ut_%d", 2024);
      32                 :          0 :         CU_ASSERT(rc == 0);
      33         [ #  # ]:          0 :         SPDK_CU_ASSERT_FATAL(attr != NULL);
      34   [ #  #  #  #  :          0 :         CU_ASSERT(strncmp(setup, attr, strlen(setup) - 1) == 0);
                   #  # ]
      35                 :          0 :         free(attr);
      36                 :            : 
      37                 :          0 :         rc = spdk_read_sysfs_attribute(&attr, "/tmp/some_non_existent_file");
      38                 :          0 :         CU_ASSERT(rc == -ENOENT);
      39                 :          0 : }
      40                 :            : 
      41                 :            : static void
      42                 :          0 : read_sysfs_attribute_uint32(void)
      43                 :            : {
      44                 :            :         /* Don't try to use real sysfs paths for the unit test. Instead
      45                 :            :          * simulate sysfs attributes with some temporary files.
      46                 :            :          */
      47                 :          0 :         const char *path = "/tmp/spdk_file_ut_2024";
      48                 :          0 :         const char *setup = "111\n";
      49                 :          0 :         uint32_t attr;
      50                 :            :         FILE *f;
      51                 :            :         int rc;
      52                 :            : 
      53                 :          0 :         f = fopen(path, "w");
      54         [ #  # ]:          0 :         SPDK_CU_ASSERT_FATAL(f != NULL);
      55   [ #  #  #  #  :          0 :         rc = fwrite(setup, strlen(setup) + 1, 1, f);
                   #  # ]
      56                 :          0 :         CU_ASSERT(rc == 1);
      57                 :          0 :         rc = fclose(f);
      58                 :          0 :         CU_ASSERT(rc == 0);
      59                 :            : 
      60                 :          0 :         rc = spdk_read_sysfs_attribute_uint32(&attr, "/tmp/spdk_file_ut_%d", 2024);
      61                 :          0 :         CU_ASSERT(rc == 0);
      62                 :          0 :         CU_ASSERT(attr == 111);
      63                 :            : 
      64                 :          0 :         setup = "0xFFFFFFFF\n";
      65                 :          0 :         f = fopen(path, "w");
      66         [ #  # ]:          0 :         SPDK_CU_ASSERT_FATAL(f != NULL);
      67   [ #  #  #  #  :          0 :         rc = fwrite(setup, strlen(setup) + 1, 1, f);
                   #  # ]
      68                 :          0 :         CU_ASSERT(rc == 1);
      69                 :          0 :         rc = fclose(f);
      70                 :          0 :         CU_ASSERT(rc == 0);
      71                 :            : 
      72                 :          0 :         rc = spdk_read_sysfs_attribute_uint32(&attr, "/tmp/spdk_file_ut_%d", 2024);
      73                 :          0 :         CU_ASSERT(rc == 0);
      74                 :          0 :         CU_ASSERT(attr == UINT32_MAX);
      75                 :            : 
      76                 :            :         /* Write a value larger than UINT32_MAX */
      77                 :          0 :         setup = "0x100000000\n";
      78                 :          0 :         f = fopen(path, "w");
      79         [ #  # ]:          0 :         SPDK_CU_ASSERT_FATAL(f != NULL);
      80   [ #  #  #  #  :          0 :         rc = fwrite(setup, strlen(setup) + 1, 1, f);
                   #  # ]
      81                 :          0 :         CU_ASSERT(rc == 1);
      82                 :          0 :         rc = fclose(f);
      83                 :          0 :         CU_ASSERT(rc == 0);
      84                 :            : 
      85                 :          0 :         rc = spdk_read_sysfs_attribute_uint32(&attr, "/tmp/spdk_file_ut_%d", 2024);
      86                 :          0 :         CU_ASSERT(rc == -EINVAL);
      87                 :            : 
      88                 :            :         /* Write a negative number */
      89                 :          0 :         setup = "-1\n";
      90                 :          0 :         f = fopen(path, "w");
      91         [ #  # ]:          0 :         SPDK_CU_ASSERT_FATAL(f != NULL);
      92   [ #  #  #  #  :          0 :         rc = fwrite(setup, strlen(setup) + 1, 1, f);
                   #  # ]
      93                 :          0 :         CU_ASSERT(rc == 1);
      94                 :          0 :         rc = fclose(f);
      95                 :          0 :         CU_ASSERT(rc == 0);
      96                 :            : 
      97                 :          0 :         rc = spdk_read_sysfs_attribute_uint32(&attr, "/tmp/spdk_file_ut_%d", 2024);
      98                 :          0 :         CU_ASSERT(rc == -EINVAL);
      99                 :            : 
     100                 :          0 :         rc = spdk_read_sysfs_attribute_uint32(&attr, "/tmp/some_non_existent_file");
     101                 :          0 :         CU_ASSERT(rc == -ENOENT);
     102                 :          0 : }
     103                 :            : 
     104                 :            : int
     105                 :          0 : main(int argc, char **argv)
     106                 :            : {
     107                 :          0 :         CU_pSuite       suite = NULL;
     108                 :            :         unsigned int    num_failures;
     109                 :            : 
     110                 :          0 :         CU_initialize_registry();
     111                 :            : 
     112                 :          0 :         suite = CU_add_suite("file", NULL, NULL);
     113                 :            : 
     114                 :          0 :         CU_ADD_TEST(suite, _read_sysfs_attribute);
     115                 :          0 :         CU_ADD_TEST(suite, read_sysfs_attribute_uint32);
     116                 :            : 
     117                 :          0 :         num_failures = spdk_ut_run_tests(argc, argv, NULL);
     118                 :            : 
     119                 :          0 :         CU_cleanup_registry();
     120                 :            : 
     121                 :          0 :         return num_failures;
     122                 :            : }

Generated by: LCOV version 1.14