xref: /dpdk/app/test/test_eal_fs.c (revision 3c60274c)
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
test_eal_fs(void)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
test_parse_sysfs_value(void)24 test_parse_sysfs_value(void)
25 {
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;
30 	FILE *fd = NULL;
31 	unsigned valid_number;
32 	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 	tmp_file_handle = mkstemp(file_template);
44 	if (tmp_file_handle == -1) {
45 		perror("mkstemp() failure");
46 		goto error;
47 	}
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");
51 		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 	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 		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 	fd = fopen(filename,"w");
67 	if (fd == NULL) {
68 		printf("line %d, Error opening %s: %s\n", __LINE__, filename, strerror(errno));
69 		goto error;
70 	}
71 	fprintf(fd,"%u\n", valid_number);
72 	fclose(fd);
73 	fd = NULL;
74 	if (eal_parse_sysfs_value(filename, &retval) < 0) {
75 		printf("eal_parse_sysfs_value() returned error - test failed\n");
76 		goto error;
77 	}
78 	if (retval != valid_number) {
79 		printf("Invalid value read by eal_parse_sysfs_value() - test failed\n");
80 		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 	fd = fopen(filename,"w");
87 	if (fd == NULL) {
88 		printf("line %d, Error opening %s: %s\n", __LINE__, filename, strerror(errno));
89 		goto error;
90 	}
91 	fprintf(fd,"0x%x\n", valid_number);
92 	fclose(fd);
93 	fd = NULL;
94 	if (eal_parse_sysfs_value(filename, &retval) < 0) {
95 		printf("eal_parse_sysfs_value() returned error - test failed\n");
96 		goto error;
97 	}
98 	if (retval != valid_number) {
99 		printf("Invalid value read by eal_parse_sysfs_value() - test failed\n");
100 		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 	fd = fopen(filename,"w");
108 	if (fd == NULL) {
109 		printf("line %d, Error opening %s: %s\n", __LINE__, filename, strerror(errno));
110 		goto error;
111 	}
112 	fclose(fd);
113 	fd = NULL;
114 	if (eal_parse_sysfs_value(filename, &retval) == 0) {
115 		printf("eal_parse_sysfs_value() read invalid value  - test failed\n");
116 		goto error;
117 	}
118 
119 	/* test reading a valid number value *without* "\n" on the end - expect failure!*/
120 	valid_number = 3;
121 	fd = fopen(filename,"w");
122 	if (fd == NULL) {
123 		printf("line %d, Error opening %s: %s\n", __LINE__, filename, strerror(errno));
124 		goto error;
125 	}
126 	fprintf(fd,"%u", valid_number);
127 	fclose(fd);
128 	fd = NULL;
129 	if (eal_parse_sysfs_value(filename, &retval) == 0) {
130 		printf("eal_parse_sysfs_value() read invalid value  - test failed\n");
131 		goto error;
132 	}
133 
134 	/* test reading a valid number value followed by string - expect failure!*/
135 	valid_number = 3;
136 	fd = fopen(filename,"w");
137 	if (fd == NULL) {
138 		printf("line %d, Error opening %s: %s\n", __LINE__, filename, strerror(errno));
139 		goto error;
140 	}
141 	fprintf(fd,"%uJ\n", valid_number);
142 	fclose(fd);
143 	fd = NULL;
144 	if (eal_parse_sysfs_value(filename, &retval) == 0) {
145 		printf("eal_parse_sysfs_value() read invalid value  - test failed\n");
146 		goto error;
147 	}
148 
149 	/* test reading a non-numeric value - expect failure!*/
150 	fd = fopen(filename,"w");
151 	if (fd == NULL) {
152 		printf("line %d, Error opening %s: %s\n", __LINE__, filename, strerror(errno));
153 		goto error;
154 	}
155 	fprintf(fd,"error\n");
156 	fclose(fd);
157 	fd = NULL;
158 	if (eal_parse_sysfs_value(filename, &retval) == 0) {
159 		printf("eal_parse_sysfs_value() read invalid value  - test failed\n");
160 		goto error;
161 	}
162 
163 	close(tmp_file_handle);
164 	unlink(filename);
165 	printf("eal_parse_sysfs_value() - OK\n");
166 	return 0;
167 
168 error:
169 	if (fd)
170 		fclose(fd);
171 	if (tmp_file_handle > 0)
172 		close(tmp_file_handle);
173 	if (filename[0] != '\0')
174 		unlink(filename);
175 	return -1;
176 }
177 
178 static int
test_eal_fs(void)179 test_eal_fs(void)
180 {
181 	if (test_parse_sysfs_value() < 0)
182 		return -1;
183 	return 0;
184 }
185 
186 #endif /* !RTE_EXEC_ENV_WINDOWS */
187 
188 REGISTER_TEST_COMMAND(eal_fs_autotest, test_eal_fs);
189