1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
11 #include "eal_filesystem.h"
14 test_parse_sysfs_value(void)
16 char filename[PATH_MAX] = "";
17 char proc_path[PATH_MAX];
18 char file_template[] = "/tmp/eal_test_XXXXXX";
19 int tmp_file_handle = -1;
21 unsigned valid_number;
22 unsigned long retval = 0;
24 #ifdef RTE_EXEC_ENV_FREEBSD
25 /* BSD doesn't have /proc/pid/fd */
29 printf("Testing function eal_parse_sysfs_value()\n");
31 /* get a temporary filename to use for all tests - create temp file handle and then
32 * use /proc to get the actual file that we can open */
33 tmp_file_handle = mkstemp(file_template);
34 if (tmp_file_handle == -1) {
35 perror("mkstemp() failure");
38 snprintf(proc_path, sizeof(proc_path), "/proc/self/fd/%d", tmp_file_handle);
39 if (readlink(proc_path, filename, sizeof(filename)) < 0) {
40 perror("readlink() failure");
43 printf("Temporary file is: %s\n", filename);
45 /* test we get an error value if we use file before it's created */
46 printf("Test reading a missing file ...\n");
47 if (eal_parse_sysfs_value("/dev/not-quite-null", &retval) == 0) {
48 printf("Error with eal_parse_sysfs_value() - returned success on reading empty file\n");
51 printf("Confirmed return error when reading empty file\n");
53 /* test reading a valid number value with "\n" on the end */
54 printf("Test reading valid values ...\n");
56 fd = fopen(filename,"w");
58 printf("line %d, Error opening %s: %s\n", __LINE__, filename, strerror(errno));
61 fprintf(fd,"%u\n", valid_number);
64 if (eal_parse_sysfs_value(filename, &retval) < 0) {
65 printf("eal_parse_sysfs_value() returned error - test failed\n");
68 if (retval != valid_number) {
69 printf("Invalid value read by eal_parse_sysfs_value() - test failed\n");
72 printf("Read '%u\\n' ok\n", valid_number);
74 /* test reading a valid hex number value with "\n" on the end */
76 fd = fopen(filename,"w");
78 printf("line %d, Error opening %s: %s\n", __LINE__, filename, strerror(errno));
81 fprintf(fd,"0x%x\n", valid_number);
84 if (eal_parse_sysfs_value(filename, &retval) < 0) {
85 printf("eal_parse_sysfs_value() returned error - test failed\n");
88 if (retval != valid_number) {
89 printf("Invalid value read by eal_parse_sysfs_value() - test failed\n");
92 printf("Read '0x%x\\n' ok\n", valid_number);
94 printf("Test reading invalid values ...\n");
96 /* test reading an empty file - expect failure!*/
97 fd = fopen(filename,"w");
99 printf("line %d, Error opening %s: %s\n", __LINE__, filename, strerror(errno));
104 if (eal_parse_sysfs_value(filename, &retval) == 0) {
105 printf("eal_parse_sysfs_value() read invalid value - test failed\n");
109 /* test reading a valid number value *without* "\n" on the end - expect failure!*/
111 fd = fopen(filename,"w");
113 printf("line %d, Error opening %s: %s\n", __LINE__, filename, strerror(errno));
116 fprintf(fd,"%u", valid_number);
119 if (eal_parse_sysfs_value(filename, &retval) == 0) {
120 printf("eal_parse_sysfs_value() read invalid value - test failed\n");
124 /* test reading a valid number value followed by string - expect failure!*/
126 fd = fopen(filename,"w");
128 printf("line %d, Error opening %s: %s\n", __LINE__, filename, strerror(errno));
131 fprintf(fd,"%uJ\n", valid_number);
134 if (eal_parse_sysfs_value(filename, &retval) == 0) {
135 printf("eal_parse_sysfs_value() read invalid value - test failed\n");
139 /* test reading a non-numeric value - expect failure!*/
140 fd = fopen(filename,"w");
142 printf("line %d, Error opening %s: %s\n", __LINE__, filename, strerror(errno));
145 fprintf(fd,"error\n");
148 if (eal_parse_sysfs_value(filename, &retval) == 0) {
149 printf("eal_parse_sysfs_value() read invalid value - test failed\n");
153 close(tmp_file_handle);
155 printf("eal_parse_sysfs_value() - OK\n");
161 if (tmp_file_handle > 0)
162 close(tmp_file_handle);
163 if (filename[0] != '\0')
171 if (test_parse_sysfs_value() < 0)
176 REGISTER_TEST_COMMAND(eal_fs_autotest, test_eal_fs);