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