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