1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
11 #include "eal_filesystem.h"
13 #ifdef RTE_EXEC_ENV_WINDOWS
17 printf("eal_fs not supported on Windows, skipping test\n");
24 test_parse_sysfs_value(void)
26 char filename[PATH_MAX] = "";
27 char proc_path[PATH_MAX];
28 char file_template[] = "/tmp/eal_test_XXXXXX";
29 int tmp_file_handle = -1;
31 unsigned valid_number;
32 unsigned long retval = 0;
34 #ifdef RTE_EXEC_ENV_FREEBSD
35 /* BSD doesn't have /proc/pid/fd */
39 printf("Testing function eal_parse_sysfs_value()\n");
41 /* get a temporary filename to use for all tests - create temp file handle and then
42 * use /proc to get the actual file that we can open */
43 tmp_file_handle = mkstemp(file_template);
44 if (tmp_file_handle == -1) {
45 perror("mkstemp() failure");
48 snprintf(proc_path, sizeof(proc_path), "/proc/self/fd/%d", tmp_file_handle);
49 if (readlink(proc_path, filename, sizeof(filename)) < 0) {
50 perror("readlink() failure");
53 printf("Temporary file is: %s\n", filename);
55 /* test we get an error value if we use file before it's created */
56 printf("Test reading a missing file ...\n");
57 if (eal_parse_sysfs_value("/dev/not-quite-null", &retval) == 0) {
58 printf("Error with eal_parse_sysfs_value() - returned success on reading empty file\n");
61 printf("Confirmed return error when reading empty file\n");
63 /* test reading a valid number value with "\n" on the end */
64 printf("Test reading valid values ...\n");
66 fd = fopen(filename,"w");
68 printf("line %d, Error opening %s: %s\n", __LINE__, filename, strerror(errno));
71 fprintf(fd,"%u\n", valid_number);
74 if (eal_parse_sysfs_value(filename, &retval) < 0) {
75 printf("eal_parse_sysfs_value() returned error - test failed\n");
78 if (retval != valid_number) {
79 printf("Invalid value read by eal_parse_sysfs_value() - test failed\n");
82 printf("Read '%u\\n' ok\n", valid_number);
84 /* test reading a valid hex number value with "\n" on the end */
86 fd = fopen(filename,"w");
88 printf("line %d, Error opening %s: %s\n", __LINE__, filename, strerror(errno));
91 fprintf(fd,"0x%x\n", valid_number);
94 if (eal_parse_sysfs_value(filename, &retval) < 0) {
95 printf("eal_parse_sysfs_value() returned error - test failed\n");
98 if (retval != valid_number) {
99 printf("Invalid value read by eal_parse_sysfs_value() - test failed\n");
102 printf("Read '0x%x\\n' ok\n", valid_number);
104 printf("Test reading invalid values ...\n");
106 /* test reading an empty file - expect failure!*/
107 fd = fopen(filename,"w");
109 printf("line %d, Error opening %s: %s\n", __LINE__, filename, strerror(errno));
114 if (eal_parse_sysfs_value(filename, &retval) == 0) {
115 printf("eal_parse_sysfs_value() read invalid value - test failed\n");
119 /* test reading a valid number value *without* "\n" on the end - expect failure!*/
121 fd = fopen(filename,"w");
123 printf("line %d, Error opening %s: %s\n", __LINE__, filename, strerror(errno));
126 fprintf(fd,"%u", valid_number);
129 if (eal_parse_sysfs_value(filename, &retval) == 0) {
130 printf("eal_parse_sysfs_value() read invalid value - test failed\n");
134 /* test reading a valid number value followed by string - expect failure!*/
136 fd = fopen(filename,"w");
138 printf("line %d, Error opening %s: %s\n", __LINE__, filename, strerror(errno));
141 fprintf(fd,"%uJ\n", valid_number);
144 if (eal_parse_sysfs_value(filename, &retval) == 0) {
145 printf("eal_parse_sysfs_value() read invalid value - test failed\n");
149 /* test reading a non-numeric value - expect failure!*/
150 fd = fopen(filename,"w");
152 printf("line %d, Error opening %s: %s\n", __LINE__, filename, strerror(errno));
155 fprintf(fd,"error\n");
158 if (eal_parse_sysfs_value(filename, &retval) == 0) {
159 printf("eal_parse_sysfs_value() read invalid value - test failed\n");
163 close(tmp_file_handle);
165 printf("eal_parse_sysfs_value() - OK\n");
171 if (tmp_file_handle > 0)
172 close(tmp_file_handle);
173 if (filename[0] != '\0')
181 if (test_parse_sysfs_value() < 0)
186 #endif /* !RTE_EXEC_ENV_WINDOWS */
188 REGISTER_TEST_COMMAND(eal_fs_autotest, test_eal_fs);