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