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