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 : : int 42 : 0 : main(int argc, char **argv) 43 : : { 44 : 0 : CU_pSuite suite = NULL; 45 : : unsigned int num_failures; 46 : : 47 : 0 : CU_initialize_registry(); 48 : : 49 : 0 : suite = CU_add_suite("file", NULL, NULL); 50 : : 51 : 0 : CU_ADD_TEST(suite, _read_sysfs_attribute); 52 : : 53 : 0 : num_failures = spdk_ut_run_tests(argc, argv, NULL); 54 : : 55 : 0 : CU_cleanup_registry(); 56 : : 57 : 0 : return num_failures; 58 : : }