4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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.
35 #ifndef RTE_EXEC_ENV_BAREMETAL
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"
45 test_parse_sysfs_value(void)
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;
52 unsigned valid_number;
53 unsigned long retval = 0;
55 #ifdef RTE_EXEC_ENV_BSDAPP
56 /* BSD doesn't have /proc/pid/fd */
60 printf("Testing function eal_parse_sysfs_value()\n");
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");
69 rte_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");
74 printf("Temporary file is: %s\n", filename);
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");
82 printf("Confirmed return error when reading empty file\n");
84 /* test reading a valid number value with "\n" on the end */
85 printf("Test reading valid values ...\n");
87 fd = fopen(filename,"w");
89 printf("line %d, Error opening %s: %s\n", __LINE__, filename, strerror(errno));
92 fprintf(fd,"%u\n", valid_number);
95 if (eal_parse_sysfs_value(filename, &retval) < 0) {
96 printf("eal_parse_sysfs_value() returned error - test failed\n");
99 if (retval != valid_number) {
100 printf("Invalid value read by eal_parse_sysfs_value() - test failed\n");
103 printf("Read '%u\\n' ok\n", valid_number);
105 /* test reading a valid hex number value with "\n" on the end */
107 fd = fopen(filename,"w");
109 printf("line %d, Error opening %s: %s\n", __LINE__, filename, strerror(errno));
112 fprintf(fd,"0x%x\n", valid_number);
115 if (eal_parse_sysfs_value(filename, &retval) < 0) {
116 printf("eal_parse_sysfs_value() returned error - test failed\n");
119 if (retval != valid_number) {
120 printf("Invalid value read by eal_parse_sysfs_value() - test failed\n");
123 printf("Read '0x%x\\n' ok\n", valid_number);
125 printf("Test reading invalid values ...\n");
127 /* test reading an empty file - expect failure!*/
128 fd = fopen(filename,"w");
130 printf("line %d, Error opening %s: %s\n", __LINE__, filename, strerror(errno));
135 if (eal_parse_sysfs_value(filename, &retval) == 0) {
136 printf("eal_parse_sysfs_value() read invalid value - test failed\n");
140 /* test reading a valid number value *without* "\n" on the end - expect failure!*/
142 fd = fopen(filename,"w");
144 printf("line %d, Error opening %s: %s\n", __LINE__, filename, strerror(errno));
147 fprintf(fd,"%u", valid_number);
150 if (eal_parse_sysfs_value(filename, &retval) == 0) {
151 printf("eal_parse_sysfs_value() read invalid value - test failed\n");
155 /* test reading a valid number value followed by string - expect failure!*/
157 fd = fopen(filename,"w");
159 printf("line %d, Error opening %s: %s\n", __LINE__, filename, strerror(errno));
162 fprintf(fd,"%uJ\n", valid_number);
165 if (eal_parse_sysfs_value(filename, &retval) == 0) {
166 printf("eal_parse_sysfs_value() read invalid value - test failed\n");
170 /* test reading a non-numeric value - expect failure!*/
171 fd = fopen(filename,"w");
173 printf("line %d, Error opening %s: %s\n", __LINE__, filename, strerror(errno));
176 fprintf(fd,"error\n");
179 if (eal_parse_sysfs_value(filename, &retval) == 0) {
180 printf("eal_parse_sysfs_value() read invalid value - test failed\n");
184 close(tmp_file_handle);
186 printf("eal_parse_sysfs_value() - OK\n");
192 if (tmp_file_handle > 0)
193 close(tmp_file_handle);
194 if (filename[0] != '\0')
202 if (test_parse_sysfs_value() < 0)
207 /* baremetal does not have a filesystem */