1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
11 /* eal_filesystem.h is not a public header file, so use relative path */
12 #include "../../lib/librte_eal/common/eal_filesystem.h"
15 test_parse_sysfs_value(void)
17 char filename[PATH_MAX] = "";
18 char proc_path[PATH_MAX];
19 char file_template[] = "/tmp/eal_test_XXXXXX";
20 int tmp_file_handle = -1;
22 unsigned valid_number;
23 unsigned long retval = 0;
25 #ifdef RTE_EXEC_ENV_BSDAPP
26 /* BSD doesn't have /proc/pid/fd */
30 printf("Testing function eal_parse_sysfs_value()\n");
32 /* get a temporary filename to use for all tests - create temp file handle and then
33 * use /proc to get the actual file that we can open */
34 tmp_file_handle = mkstemp(file_template);
35 if (tmp_file_handle == -1) {
36 perror("mkstemp() failure");
39 snprintf(proc_path, sizeof(proc_path), "/proc/self/fd/%d", tmp_file_handle);
40 if (readlink(proc_path, filename, sizeof(filename)) < 0) {
41 perror("readlink() failure");
44 printf("Temporary file is: %s\n", filename);
46 /* test we get an error value if we use file before it's created */
47 printf("Test reading a missing file ...\n");
48 if (eal_parse_sysfs_value("/dev/not-quite-null", &retval) == 0) {
49 printf("Error with eal_parse_sysfs_value() - returned success on reading empty file\n");
52 printf("Confirmed return error when reading empty file\n");
54 /* test reading a valid number value with "\n" on the end */
55 printf("Test reading valid values ...\n");
57 fd = fopen(filename,"w");
59 printf("line %d, Error opening %s: %s\n", __LINE__, filename, strerror(errno));
62 fprintf(fd,"%u\n", valid_number);
65 if (eal_parse_sysfs_value(filename, &retval) < 0) {
66 printf("eal_parse_sysfs_value() returned error - test failed\n");
69 if (retval != valid_number) {
70 printf("Invalid value read by eal_parse_sysfs_value() - test failed\n");
73 printf("Read '%u\\n' ok\n", valid_number);
75 /* test reading a valid hex number value with "\n" on the end */
77 fd = fopen(filename,"w");
79 printf("line %d, Error opening %s: %s\n", __LINE__, filename, strerror(errno));
82 fprintf(fd,"0x%x\n", valid_number);
85 if (eal_parse_sysfs_value(filename, &retval) < 0) {
86 printf("eal_parse_sysfs_value() returned error - test failed\n");
89 if (retval != valid_number) {
90 printf("Invalid value read by eal_parse_sysfs_value() - test failed\n");
93 printf("Read '0x%x\\n' ok\n", valid_number);
95 printf("Test reading invalid values ...\n");
97 /* test reading an empty file - expect failure!*/
98 fd = fopen(filename,"w");
100 printf("line %d, Error opening %s: %s\n", __LINE__, filename, strerror(errno));
105 if (eal_parse_sysfs_value(filename, &retval) == 0) {
106 printf("eal_parse_sysfs_value() read invalid value - test failed\n");
110 /* test reading a valid number value *without* "\n" on the end - expect failure!*/
112 fd = fopen(filename,"w");
114 printf("line %d, Error opening %s: %s\n", __LINE__, filename, strerror(errno));
117 fprintf(fd,"%u", valid_number);
120 if (eal_parse_sysfs_value(filename, &retval) == 0) {
121 printf("eal_parse_sysfs_value() read invalid value - test failed\n");
125 /* test reading a valid number value followed by string - expect failure!*/
127 fd = fopen(filename,"w");
129 printf("line %d, Error opening %s: %s\n", __LINE__, filename, strerror(errno));
132 fprintf(fd,"%uJ\n", valid_number);
135 if (eal_parse_sysfs_value(filename, &retval) == 0) {
136 printf("eal_parse_sysfs_value() read invalid value - test failed\n");
140 /* test reading a non-numeric value - expect failure!*/
141 fd = fopen(filename,"w");
143 printf("line %d, Error opening %s: %s\n", __LINE__, filename, strerror(errno));
146 fprintf(fd,"error\n");
149 if (eal_parse_sysfs_value(filename, &retval) == 0) {
150 printf("eal_parse_sysfs_value() read invalid value - test failed\n");
154 close(tmp_file_handle);
156 printf("eal_parse_sysfs_value() - OK\n");
162 if (tmp_file_handle > 0)
163 close(tmp_file_handle);
164 if (filename[0] != '\0')
172 if (test_parse_sysfs_value() < 0)
177 REGISTER_TEST_COMMAND(eal_fs_autotest, test_eal_fs);