add FreeBSD support
[dpdk.git] / app / test / test_eal_fs.c
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  * 
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  * 
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  * 
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <cmdline_parse.h>
35
36 #include "test.h"
37 #ifndef RTE_EXEC_ENV_BAREMETAL
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41
42 /* eal_filesystem.h is not a public header file, so use relative path */
43 #include "../../lib/librte_eal/linuxapp/eal/include/eal_filesystem.h"
44
45 static int
46 test_parse_sysfs_value(void)
47 {
48         char filename[PATH_MAX] = "";
49         char proc_path[PATH_MAX];
50         char file_template[] = "/tmp/eal_test_XXXXXX";
51         int tmp_file_handle = -1;
52         FILE *fd = NULL;
53         unsigned valid_number;
54         unsigned long retval = 0;
55
56 #ifdef RTE_EXEC_ENV_BSDAPP
57         /* BSD doesn't have /proc/pid/fd */
58         return 0;
59 #endif
60
61         printf("Testing function eal_parse_sysfs_value()\n");
62
63         /* get a temporary filename to use for all tests - create temp file handle and then
64          * use /proc to get the actual file that we can open */
65         tmp_file_handle = mkstemp(file_template);
66         if (tmp_file_handle == -1) {
67                 perror("mkstemp() failure");
68                 goto error;
69         }
70         rte_snprintf(proc_path, sizeof(proc_path), "/proc/self/fd/%d", tmp_file_handle);
71         if (readlink(proc_path, filename, sizeof(filename)) < 0) {
72                 perror("readlink() failure");
73                 goto error;
74         }
75         printf("Temporary file is: %s\n", filename);
76
77         /* test we get an error value if we use file before it's created */
78         printf("Test reading a missing file ...\n");
79         if (eal_parse_sysfs_value("/dev/not-quite-null", &retval) == 0) {
80                 printf("Error with eal_parse_sysfs_value() - returned success on reading empty file\n");
81                 goto error;
82         }
83         printf("Confirmed return error when reading empty file\n");
84
85         /* test reading a valid number value with "\n" on the end */
86         printf("Test reading valid values ...\n");
87         valid_number = 15;
88         fd = fopen(filename,"w");
89         if (fd == NULL) {
90                 printf("line %d, Error opening %s: %s\n", __LINE__, filename, strerror(errno));
91                 goto error;
92         }
93         fprintf(fd,"%u\n", valid_number);
94         fclose(fd);
95         fd = NULL;
96         if (eal_parse_sysfs_value(filename, &retval) < 0) {
97                 printf("eal_parse_sysfs_value() returned error - test failed\n");
98                 goto error;
99         }
100         if (retval != valid_number) {
101                 printf("Invalid value read by eal_parse_sysfs_value() - test failed\n");
102                 goto error;
103         }
104         printf("Read '%u\\n' ok\n", valid_number);
105
106         /* test reading a valid hex number value with "\n" on the end */
107         valid_number = 25;
108         fd = fopen(filename,"w");
109         if (fd == NULL) {
110                 printf("line %d, Error opening %s: %s\n", __LINE__, filename, strerror(errno));
111                 goto error;
112         }
113         fprintf(fd,"0x%x\n", valid_number);
114         fclose(fd);
115         fd = NULL;
116         if (eal_parse_sysfs_value(filename, &retval) < 0) {
117                 printf("eal_parse_sysfs_value() returned error - test failed\n");
118                 goto error;
119         }
120         if (retval != valid_number) {
121                 printf("Invalid value read by eal_parse_sysfs_value() - test failed\n");
122                 goto error;
123         }
124         printf("Read '0x%x\\n' ok\n", valid_number);
125
126         printf("Test reading invalid values ...\n");
127
128         /* test reading an empty file - expect failure!*/
129         fd = fopen(filename,"w");
130         if (fd == NULL) {
131                 printf("line %d, Error opening %s: %s\n", __LINE__, filename, strerror(errno));
132                 goto error;
133         }
134         fclose(fd);
135         fd = NULL;
136         if (eal_parse_sysfs_value(filename, &retval) == 0) {
137                 printf("eal_parse_sysfs_value() read invalid value  - test failed\n");
138                 goto error;
139         }
140
141         /* test reading a valid number value *without* "\n" on the end - expect failure!*/
142         valid_number = 3;
143         fd = fopen(filename,"w");
144         if (fd == NULL) {
145                 printf("line %d, Error opening %s: %s\n", __LINE__, filename, strerror(errno));
146                 goto error;
147         }
148         fprintf(fd,"%u", valid_number);
149         fclose(fd);
150         fd = NULL;
151         if (eal_parse_sysfs_value(filename, &retval) == 0) {
152                 printf("eal_parse_sysfs_value() read invalid value  - test failed\n");
153                 goto error;
154         }
155
156         /* test reading a valid number value followed by string - expect failure!*/
157         valid_number = 3;
158         fd = fopen(filename,"w");
159         if (fd == NULL) {
160                 printf("line %d, Error opening %s: %s\n", __LINE__, filename, strerror(errno));
161                 goto error;
162         }
163         fprintf(fd,"%uJ\n", valid_number);
164         fclose(fd);
165         fd = NULL;
166         if (eal_parse_sysfs_value(filename, &retval) == 0) {
167                 printf("eal_parse_sysfs_value() read invalid value  - test failed\n");
168                 goto error;
169         }
170
171         /* test reading a non-numeric value - expect failure!*/
172         fd = fopen(filename,"w");
173         if (fd == NULL) {
174                 printf("line %d, Error opening %s: %s\n", __LINE__, filename, strerror(errno));
175                 goto error;
176         }
177         fprintf(fd,"error\n");
178         fclose(fd);
179         fd = NULL;
180         if (eal_parse_sysfs_value(filename, &retval) == 0) {
181                 printf("eal_parse_sysfs_value() read invalid value  - test failed\n");
182                 goto error;
183         }
184
185         close(tmp_file_handle);
186         unlink(filename);
187         printf("eal_parse_sysfs_value() - OK\n");
188         return 0;
189
190 error:
191         if (fd)
192                 fclose(fd);
193         if (tmp_file_handle > 0)
194                 close(tmp_file_handle);
195         if (filename[0] != '\0')
196                 unlink(filename);
197         return -1;
198 }
199
200 int
201 test_eal_fs(void)
202 {
203         if (test_parse_sysfs_value() < 0)
204                 return -1;
205         return 0;
206 }
207 #else
208 /* baremetal does not have a filesystem */
209 int
210 test_eal_fs(void)
211 {
212         return 0;
213 }
214 #endif