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 _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 const char *path = "/tmp/spdk_file_ut_2024"; 17 const char *setup = "spdk_unit_tests\n"; 18 char *attr = NULL; 19 FILE *f; 20 int rc; 21 22 f = fopen(path, "w"); 23 SPDK_CU_ASSERT_FATAL(f != NULL); 24 25 rc = fwrite(setup, strlen(setup) + 1, 1, f); 26 CU_ASSERT(rc == 1); 27 28 rc = fclose(f); 29 CU_ASSERT(rc == 0); 30 31 rc = spdk_read_sysfs_attribute(&attr, "/tmp/spdk_file_ut_%d", 2024); 32 CU_ASSERT(rc == 0); 33 SPDK_CU_ASSERT_FATAL(attr != NULL); 34 CU_ASSERT(strncmp(setup, attr, strlen(setup) - 1) == 0); 35 free(attr); 36 37 rc = spdk_read_sysfs_attribute(&attr, "/tmp/some_non_existent_file"); 38 CU_ASSERT(rc == -ENOENT); 39 } 40 41 static void 42 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 const char *path = "/tmp/spdk_file_ut_2024"; 48 const char *setup = "111\n"; 49 uint32_t attr; 50 FILE *f; 51 int rc; 52 53 f = fopen(path, "w"); 54 SPDK_CU_ASSERT_FATAL(f != NULL); 55 rc = fwrite(setup, strlen(setup) + 1, 1, f); 56 CU_ASSERT(rc == 1); 57 rc = fclose(f); 58 CU_ASSERT(rc == 0); 59 60 rc = spdk_read_sysfs_attribute_uint32(&attr, "/tmp/spdk_file_ut_%d", 2024); 61 CU_ASSERT(rc == 0); 62 CU_ASSERT(attr == 111); 63 64 setup = "0xFFFFFFFF\n"; 65 f = fopen(path, "w"); 66 SPDK_CU_ASSERT_FATAL(f != NULL); 67 rc = fwrite(setup, strlen(setup) + 1, 1, f); 68 CU_ASSERT(rc == 1); 69 rc = fclose(f); 70 CU_ASSERT(rc == 0); 71 72 rc = spdk_read_sysfs_attribute_uint32(&attr, "/tmp/spdk_file_ut_%d", 2024); 73 CU_ASSERT(rc == 0); 74 CU_ASSERT(attr == UINT32_MAX); 75 76 /* Write a value larger than UINT32_MAX */ 77 setup = "0x100000000\n"; 78 f = fopen(path, "w"); 79 SPDK_CU_ASSERT_FATAL(f != NULL); 80 rc = fwrite(setup, strlen(setup) + 1, 1, f); 81 CU_ASSERT(rc == 1); 82 rc = fclose(f); 83 CU_ASSERT(rc == 0); 84 85 rc = spdk_read_sysfs_attribute_uint32(&attr, "/tmp/spdk_file_ut_%d", 2024); 86 CU_ASSERT(rc == -EINVAL); 87 88 /* Write a negative number */ 89 setup = "-1\n"; 90 f = fopen(path, "w"); 91 SPDK_CU_ASSERT_FATAL(f != NULL); 92 rc = fwrite(setup, strlen(setup) + 1, 1, f); 93 CU_ASSERT(rc == 1); 94 rc = fclose(f); 95 CU_ASSERT(rc == 0); 96 97 rc = spdk_read_sysfs_attribute_uint32(&attr, "/tmp/spdk_file_ut_%d", 2024); 98 CU_ASSERT(rc == -EINVAL); 99 100 rc = spdk_read_sysfs_attribute_uint32(&attr, "/tmp/some_non_existent_file"); 101 CU_ASSERT(rc == -ENOENT); 102 } 103 104 int 105 main(int argc, char **argv) 106 { 107 CU_pSuite suite = NULL; 108 unsigned int num_failures; 109 110 CU_initialize_registry(); 111 112 suite = CU_add_suite("file", NULL, NULL); 113 114 CU_ADD_TEST(suite, _read_sysfs_attribute); 115 CU_ADD_TEST(suite, read_sysfs_attribute_uint32); 116 117 num_failures = spdk_ut_run_tests(argc, argv, NULL); 118 119 CU_cleanup_registry(); 120 121 return num_failures; 122 } 123