64a70e10959827b75c867b5fe1542cb48d516e55
[dpdk.git] / app / test / test_eal_fs.c
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2012 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
35 #include <cmdline_parse.h>
36
37 #include "test.h"
38 #ifndef RTE_EXEC_ENV_BAREMETAL
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42
43 /* eal_filesystem.h is not a public header file, so use relative path */
44 #include "../../lib/librte_eal/linuxapp/eal/include/eal_filesystem.h"
45
46 static int
47 test_parse_sysfs_value(void)
48 {
49         char filename[PATH_MAX] = "";
50         char proc_path[PATH_MAX];
51         char file_template[] = "/tmp/eal_test_XXXXXX";
52         int tmp_file_handle = -1;
53         FILE *fd = NULL;
54         unsigned valid_number;
55         unsigned long retval = 0;
56
57         printf("Testing function eal_parse_sysfs_value()\n");
58
59         /* get a temporary filename to use for all tests - create temp file handle and then
60          * use /proc to get the actual file that we can open */
61         tmp_file_handle = mkstemp(file_template);
62         if (tmp_file_handle == -1) {
63                 perror("mkstemp() failure");
64                 goto error;
65         }
66         rte_snprintf(proc_path, sizeof(proc_path), "/proc/self/fd/%d", tmp_file_handle);
67         if (readlink(proc_path, filename, sizeof(filename)) < 0) {
68                 perror("readlink() failure");
69                 goto error;
70         }
71         printf("Temporary file is: %s\n", filename);
72
73         /* test we get an error value if we use file before it's created */
74         printf("Test reading a missing file ...\n");
75         if (eal_parse_sysfs_value("/dev/not-quite-null", &retval) == 0) {
76                 printf("Error with eal_parse_sysfs_value() - returned success on reading empty file\n");
77                 goto error;
78         }
79         printf("Confirmed return error when reading empty file\n");
80
81         /* test reading a valid number value with "\n" on the end */
82         printf("Test reading valid values ...\n");
83         valid_number = 15;
84         fd = fopen(filename,"w");
85         if (fd == NULL) {
86                 printf("line %d, Error opening %s: %s\n", __LINE__, filename, strerror(errno));
87                 goto error;
88         }
89         fprintf(fd,"%u\n", valid_number);
90         fclose(fd);
91         fd = NULL;
92         if (eal_parse_sysfs_value(filename, &retval) < 0) {
93                 printf("eal_parse_sysfs_value() returned error - test failed\n");
94                 goto error;
95         }
96         if (retval != valid_number) {
97                 printf("Invalid value read by eal_parse_sysfs_value() - test failed\n");
98                 goto error;
99         }
100         printf("Read '%u\\n' ok\n", valid_number);
101
102         /* test reading a valid hex number value with "\n" on the end */
103         valid_number = 25;
104         fd = fopen(filename,"w");
105         if (fd == NULL) {
106                 printf("line %d, Error opening %s: %s\n", __LINE__, filename, strerror(errno));
107                 goto error;
108         }
109         fprintf(fd,"0x%x\n", valid_number);
110         fclose(fd);
111         fd = NULL;
112         if (eal_parse_sysfs_value(filename, &retval) < 0) {
113                 printf("eal_parse_sysfs_value() returned error - test failed\n");
114                 goto error;
115         }
116         if (retval != valid_number) {
117                 printf("Invalid value read by eal_parse_sysfs_value() - test failed\n");
118                 goto error;
119         }
120         printf("Read '0x%x\\n' ok\n", valid_number);
121
122         printf("Test reading invalid values ...\n");
123
124         /* test reading an empty file - expect failure!*/
125         fd = fopen(filename,"w");
126         if (fd == NULL) {
127                 printf("line %d, Error opening %s: %s\n", __LINE__, filename, strerror(errno));
128                 goto error;
129         }
130         fclose(fd);
131         fd = NULL;
132         if (eal_parse_sysfs_value(filename, &retval) == 0) {
133                 printf("eal_parse_sysfs_value() read invalid value  - test failed\n");
134                 goto error;
135         }
136
137         /* test reading a valid number value *without* "\n" on the end - expect failure!*/
138         valid_number = 3;
139         fd = fopen(filename,"w");
140         if (fd == NULL) {
141                 printf("line %d, Error opening %s: %s\n", __LINE__, filename, strerror(errno));
142                 goto error;
143         }
144         fprintf(fd,"%u", valid_number);
145         fclose(fd);
146         fd = NULL;
147         if (eal_parse_sysfs_value(filename, &retval) == 0) {
148                 printf("eal_parse_sysfs_value() read invalid value  - test failed\n");
149                 goto error;
150         }
151
152         /* test reading a valid number value followed by string - expect failure!*/
153         valid_number = 3;
154         fd = fopen(filename,"w");
155         if (fd == NULL) {
156                 printf("line %d, Error opening %s: %s\n", __LINE__, filename, strerror(errno));
157                 goto error;
158         }
159         fprintf(fd,"%uJ\n", valid_number);
160         fclose(fd);
161         fd = NULL;
162         if (eal_parse_sysfs_value(filename, &retval) == 0) {
163                 printf("eal_parse_sysfs_value() read invalid value  - test failed\n");
164                 goto error;
165         }
166
167         /* test reading a non-numeric value - expect failure!*/
168         fd = fopen(filename,"w");
169         if (fd == NULL) {
170                 printf("line %d, Error opening %s: %s\n", __LINE__, filename, strerror(errno));
171                 goto error;
172         }
173         fprintf(fd,"error\n");
174         fclose(fd);
175         fd = NULL;
176         if (eal_parse_sysfs_value(filename, &retval) == 0) {
177                 printf("eal_parse_sysfs_value() read invalid value  - test failed\n");
178                 goto error;
179         }
180
181         close(tmp_file_handle);
182         unlink(filename);
183         printf("eal_parse_sysfs_value() - OK\n");
184         return 0;
185
186 error:
187         if (fd)
188                 fclose(fd);
189         if (tmp_file_handle > 0)
190                 close(tmp_file_handle);
191         if (filename[0] != '\0')
192                 unlink(filename);
193         return -1;
194 }
195
196 int
197 test_eal_fs(void)
198 {
199         if (test_parse_sysfs_value() < 0)
200                 return -1;
201         return 0;
202 }
203 #else
204 /* baremetal does not have a filesystem */
205 int
206 test_eal_fs(void)
207 {
208         return 0;
209 }
210 #endif