Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(c) 2010-2014 Intel Corporation
3 : : */
4 : :
5 : : #include "test.h"
6 : : #include <stdio.h>
7 : : #include <stdlib.h>
8 : : #include <string.h>
9 : : #include <errno.h>
10 : :
11 : : #include "eal_filesystem.h"
12 : :
13 : : #ifdef RTE_EXEC_ENV_WINDOWS
14 : : static int
15 : : test_eal_fs(void)
16 : : {
17 : : printf("eal_fs not supported on Windows, skipping test\n");
18 : : return TEST_SKIPPED;
19 : : }
20 : :
21 : : #else
22 : :
23 : : static int
24 : 1 : test_parse_sysfs_value(void)
25 : : {
26 : 1 : char filename[PATH_MAX] = "";
27 : : char proc_path[PATH_MAX];
28 : 1 : char file_template[] = "/tmp/eal_test_XXXXXX";
29 : : int tmp_file_handle = -1;
30 : : FILE *fd = NULL;
31 : : unsigned valid_number;
32 : 1 : unsigned long retval = 0;
33 : :
34 : : #ifdef RTE_EXEC_ENV_FREEBSD
35 : : /* BSD doesn't have /proc/pid/fd */
36 : : return 0;
37 : : #endif
38 : :
39 : : printf("Testing function eal_parse_sysfs_value()\n");
40 : :
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 : 1 : tmp_file_handle = mkstemp(file_template);
44 [ - + ]: 1 : if (tmp_file_handle == -1) {
45 : 0 : perror("mkstemp() failure");
46 : 0 : goto error;
47 : : }
48 : : snprintf(proc_path, sizeof(proc_path), "/proc/self/fd/%d", tmp_file_handle);
49 [ - + ]: 1 : if (readlink(proc_path, filename, sizeof(filename)) < 0) {
50 : 0 : perror("readlink() failure");
51 : 0 : goto error;
52 : : }
53 : : printf("Temporary file is: %s\n", filename);
54 : :
55 : : /* test we get an error value if we use file before it's created */
56 : : printf("Test reading a missing file ...\n");
57 [ - + ]: 1 : 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");
59 : 0 : goto error;
60 : : }
61 : : printf("Confirmed return error when reading empty file\n");
62 : :
63 : : /* test reading a valid number value with "\n" on the end */
64 : : printf("Test reading valid values ...\n");
65 : : valid_number = 15;
66 : 1 : fd = fopen(filename,"w");
67 [ - + ]: 1 : if (fd == NULL) {
68 : 0 : printf("line %d, Error opening %s: %s\n", __LINE__, filename, strerror(errno));
69 : 0 : goto error;
70 : : }
71 : : fprintf(fd,"%u\n", valid_number);
72 : 1 : fclose(fd);
73 : : fd = NULL;
74 [ - + ]: 1 : if (eal_parse_sysfs_value(filename, &retval) < 0) {
75 : : printf("eal_parse_sysfs_value() returned error - test failed\n");
76 : 0 : goto error;
77 : : }
78 [ - + ]: 1 : if (retval != valid_number) {
79 : : printf("Invalid value read by eal_parse_sysfs_value() - test failed\n");
80 : 0 : goto error;
81 : : }
82 : : printf("Read '%u\\n' ok\n", valid_number);
83 : :
84 : : /* test reading a valid hex number value with "\n" on the end */
85 : : valid_number = 25;
86 : 1 : fd = fopen(filename,"w");
87 [ - + ]: 1 : if (fd == NULL) {
88 : 0 : printf("line %d, Error opening %s: %s\n", __LINE__, filename, strerror(errno));
89 : 0 : goto error;
90 : : }
91 : : fprintf(fd,"0x%x\n", valid_number);
92 : 1 : fclose(fd);
93 : : fd = NULL;
94 [ - + ]: 1 : if (eal_parse_sysfs_value(filename, &retval) < 0) {
95 : : printf("eal_parse_sysfs_value() returned error - test failed\n");
96 : 0 : goto error;
97 : : }
98 [ - + ]: 1 : if (retval != valid_number) {
99 : : printf("Invalid value read by eal_parse_sysfs_value() - test failed\n");
100 : 0 : goto error;
101 : : }
102 : : printf("Read '0x%x\\n' ok\n", valid_number);
103 : :
104 : : printf("Test reading invalid values ...\n");
105 : :
106 : : /* test reading an empty file - expect failure!*/
107 : 1 : fd = fopen(filename,"w");
108 [ - + ]: 1 : if (fd == NULL) {
109 : 0 : printf("line %d, Error opening %s: %s\n", __LINE__, filename, strerror(errno));
110 : 0 : goto error;
111 : : }
112 : 1 : fclose(fd);
113 : : fd = NULL;
114 [ - + ]: 1 : if (eal_parse_sysfs_value(filename, &retval) == 0) {
115 : : printf("eal_parse_sysfs_value() read invalid value - test failed\n");
116 : 0 : goto error;
117 : : }
118 : :
119 : : /* test reading a valid number value *without* "\n" on the end - expect failure!*/
120 : : valid_number = 3;
121 : 1 : fd = fopen(filename,"w");
122 [ - + ]: 1 : if (fd == NULL) {
123 : 0 : printf("line %d, Error opening %s: %s\n", __LINE__, filename, strerror(errno));
124 : 0 : goto error;
125 : : }
126 : : fprintf(fd,"%u", valid_number);
127 : 1 : fclose(fd);
128 : : fd = NULL;
129 [ - + ]: 1 : if (eal_parse_sysfs_value(filename, &retval) == 0) {
130 : : printf("eal_parse_sysfs_value() read invalid value - test failed\n");
131 : 0 : goto error;
132 : : }
133 : :
134 : : /* test reading a valid number value followed by string - expect failure!*/
135 : : valid_number = 3;
136 : 1 : fd = fopen(filename,"w");
137 [ - + ]: 1 : if (fd == NULL) {
138 : 0 : printf("line %d, Error opening %s: %s\n", __LINE__, filename, strerror(errno));
139 : 0 : goto error;
140 : : }
141 : : fprintf(fd,"%uJ\n", valid_number);
142 : 1 : fclose(fd);
143 : : fd = NULL;
144 [ - + ]: 1 : if (eal_parse_sysfs_value(filename, &retval) == 0) {
145 : : printf("eal_parse_sysfs_value() read invalid value - test failed\n");
146 : 0 : goto error;
147 : : }
148 : :
149 : : /* test reading a non-numeric value - expect failure!*/
150 : 1 : fd = fopen(filename,"w");
151 [ - + ]: 1 : if (fd == NULL) {
152 : 0 : printf("line %d, Error opening %s: %s\n", __LINE__, filename, strerror(errno));
153 : 0 : goto error;
154 : : }
155 : : fprintf(fd,"error\n");
156 : 1 : fclose(fd);
157 : : fd = NULL;
158 [ - + ]: 1 : if (eal_parse_sysfs_value(filename, &retval) == 0) {
159 : : printf("eal_parse_sysfs_value() read invalid value - test failed\n");
160 : 0 : goto error;
161 : : }
162 : :
163 : 1 : close(tmp_file_handle);
164 : 1 : unlink(filename);
165 : : printf("eal_parse_sysfs_value() - OK\n");
166 : 1 : return 0;
167 : :
168 : : error:
169 : : if (fd)
170 : : fclose(fd);
171 [ # # ]: 0 : if (tmp_file_handle > 0)
172 : 0 : close(tmp_file_handle);
173 [ # # ]: 0 : if (filename[0] != '\0')
174 : 0 : unlink(filename);
175 : : return -1;
176 : : }
177 : :
178 : : static int
179 : 1 : test_eal_fs(void)
180 : : {
181 [ - + ]: 1 : if (test_parse_sysfs_value() < 0)
182 : 0 : return -1;
183 : : return 0;
184 : : }
185 : :
186 : : #endif /* !RTE_EXEC_ENV_WINDOWS */
187 : :
188 : 251 : REGISTER_FAST_TEST(eal_fs_autotest, true, true, test_eal_fs);
|