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 <stdio.h> 35 #include <stdarg.h> 36 #include <stddef.h> 37 #include <errno.h> 38 #include <string.h> 39 40 #include <rte_string_fns.h> 41 42 #include <cmdline_parse.h> 43 44 #include "test.h" 45 46 #define LOG(...) do {\ 47 fprintf(stderr, "%s() ln %d: ", __func__, __LINE__); \ 48 fprintf(stderr, __VA_ARGS__); \ 49 } while(0) 50 51 #define DATA_BYTE 'a' 52 53 static int 54 test_rte_snprintf(void) 55 { 56 /* ================================================= 57 * First test with a string that will fit in buffer 58 * =================================================*/ 59 do { 60 int retval; 61 const char source[] = "This is a string that will fit in buffer"; 62 char buf[sizeof(source)+2]; /* make buffer big enough to fit string */ 63 64 /* initialise buffer with characters so it can contain no nulls */ 65 memset(buf, DATA_BYTE, sizeof(buf)); 66 67 /* run rte_snprintf and check results */ 68 retval = rte_snprintf(buf, sizeof(buf), "%s", source); 69 if (retval != sizeof(source) - 1) { 70 LOG("Error, retval = %d, expected = %u\n", 71 retval, (unsigned)sizeof(source)); 72 return -1; 73 } 74 if (buf[retval] != '\0') { 75 LOG("Error, resultant is not null-terminated\n"); 76 return -1; 77 } 78 if (memcmp(source, buf, sizeof(source)-1) != 0){ 79 LOG("Error, corrupt data in buffer\n"); 80 return -1; 81 } 82 } while (0); 83 84 do { 85 /* ================================================= 86 * Test with a string that will get truncated 87 * =================================================*/ 88 int retval; 89 const char source[] = "This is a long string that won't fit in buffer"; 90 char buf[sizeof(source)/2]; /* make buffer half the size */ 91 92 /* initialise buffer with characters so it can contain no nulls */ 93 memset(buf, DATA_BYTE, sizeof(buf)); 94 95 /* run rte_snprintf and check results */ 96 retval = rte_snprintf(buf, sizeof(buf), "%s", source); 97 if (retval != sizeof(source) - 1) { 98 LOG("Error, retval = %d, expected = %u\n", 99 retval, (unsigned)sizeof(source)); 100 return -1; 101 } 102 if (buf[sizeof(buf)-1] != '\0') { 103 LOG("Error, buffer is not null-terminated\n"); 104 return -1; 105 } 106 if (memcmp(source, buf, sizeof(buf)-1) != 0){ 107 LOG("Error, corrupt data in buffer\n"); 108 return -1; 109 } 110 } while (0); 111 112 do { 113 /* =========================================================== 114 * Test using zero-size buf to check how long a buffer we need 115 * ===========================================================*/ 116 int retval; 117 const char source[] = "This is a string"; 118 char buf[10]; 119 120 /* call with a zero-sized non-NULL buffer, should tell how big a buffer 121 * we need */ 122 retval = rte_snprintf(buf, 0, "%s", source); 123 if (retval != sizeof(source) - 1) { 124 LOG("Call with 0-length buffer does not return correct size." 125 "Expected: %zu, got: %d\n", sizeof(source), retval); 126 return -1; 127 } 128 129 /* call with a zero-sized NULL buffer, should tell how big a buffer 130 * we need */ 131 retval = rte_snprintf(NULL, 0, "%s", source); 132 if (retval != sizeof(source) - 1) { 133 LOG("Call with 0-length buffer does not return correct size." 134 "Expected: %zu, got: %d\n", sizeof(source), retval); 135 return -1; 136 } 137 138 } while (0); 139 140 do { 141 /* ================================================= 142 * Test with invalid parameter values 143 * =================================================*/ 144 const char source[] = "This is a string"; 145 char buf[10]; 146 147 /* call with buffer value set to NULL is EINVAL */ 148 if (rte_snprintf(NULL, sizeof(buf), "%s\n", source) != -1 || 149 errno != EINVAL) { 150 LOG("Failed to get suitable error when passing NULL buffer\n"); 151 return -1; 152 } 153 154 memset(buf, DATA_BYTE, sizeof(buf)); 155 /* call with a NULL format and zero-size should return error 156 * without affecting the buffer */ 157 if (rte_snprintf(buf, 0, NULL) != -1 || 158 errno != EINVAL) { 159 LOG("Failed to get suitable error when passing NULL buffer\n"); 160 return -1; 161 } 162 if (buf[0] != DATA_BYTE) { 163 LOG("Error, zero-length buffer modified after call with NULL" 164 " format string\n"); 165 return -1; 166 } 167 168 /* call with a NULL format should return error but also null-terminate 169 * the buffer */ 170 if (rte_snprintf(buf, sizeof(buf), NULL) != -1 || 171 errno != EINVAL) { 172 LOG("Failed to get suitable error when passing NULL buffer\n"); 173 return -1; 174 } 175 if (buf[0] != '\0') { 176 LOG("Error, buffer not null-terminated after call with NULL" 177 " format string\n"); 178 return -1; 179 } 180 } while (0); 181 182 LOG("%s - PASSED\n", __func__); 183 return 0; 184 } 185 186 static int 187 test_rte_strsplit(void) 188 { 189 int i; 190 do { 191 /* ======================================================= 192 * split a mac address correct number of splits requested 193 * =======================================================*/ 194 char test_string[] = "54:65:76:87:98:90"; 195 char *splits[6]; 196 197 LOG("Source string: '%s', to split on ':'\n", test_string); 198 if (rte_strsplit(test_string, sizeof(test_string), 199 splits, 6, ':') != 6) { 200 LOG("Error splitting mac address\n"); 201 return -1; 202 } 203 for (i = 0; i < 6; i++) 204 LOG("Token %d = %s\n", i + 1, splits[i]); 205 } while (0); 206 207 208 do { 209 /* ======================================================= 210 * split on spaces smaller number of splits requested 211 * =======================================================*/ 212 char test_string[] = "54 65 76 87 98 90"; 213 char *splits[6]; 214 215 LOG("Source string: '%s', to split on ' '\n", test_string); 216 if (rte_strsplit(test_string, sizeof(test_string), 217 splits, 3, ' ') != 3) { 218 LOG("Error splitting mac address for max 2 splits\n"); 219 return -1; 220 } 221 for (i = 0; i < 3; i++) 222 LOG("Token %d = %s\n", i + 1, splits[i]); 223 } while (0); 224 225 do { 226 /* ======================================================= 227 * split on commas - more splits than commas requested 228 * =======================================================*/ 229 char test_string[] = "a,b,c,d"; 230 char *splits[6]; 231 232 LOG("Source string: '%s', to split on ','\n", test_string); 233 if (rte_strsplit(test_string, sizeof(test_string), 234 splits, 6, ',') != 4) { 235 LOG("Error splitting %s on ','\n", test_string); 236 return -1; 237 } 238 for (i = 0; i < 4; i++) 239 LOG("Token %d = %s\n", i + 1, splits[i]); 240 } while(0); 241 242 do { 243 /* ======================================================= 244 * Try splitting on non-existent character. 245 * =======================================================*/ 246 char test_string[] = "a,b,c,d"; 247 char *splits[6]; 248 249 LOG("Source string: '%s', to split on ' '\n", test_string); 250 if (rte_strsplit(test_string, sizeof(test_string), 251 splits, 6, ' ') != 1) { 252 LOG("Error splitting %s on ' '\n", test_string); 253 return -1; 254 } 255 LOG("String not split\n"); 256 } while(0); 257 258 do { 259 /* ======================================================= 260 * Invalid / edge case parameter checks 261 * =======================================================*/ 262 char test_string[] = "a,b,c,d"; 263 char *splits[6]; 264 265 if (rte_strsplit(NULL, 0, splits, 6, ',') >= 0 266 || errno != EINVAL){ 267 LOG("Error: rte_strsplit accepted NULL string parameter\n"); 268 return -1; 269 } 270 271 if (rte_strsplit(test_string, sizeof(test_string), NULL, 0, ',') >= 0 272 || errno != EINVAL){ 273 LOG("Error: rte_strsplit accepted NULL array parameter\n"); 274 return -1; 275 } 276 277 errno = 0; 278 if (rte_strsplit(test_string, 0, splits, 6, ',') != 0 || errno != 0) { 279 LOG("Error: rte_strsplit did not accept 0 length string\n"); 280 return -1; 281 } 282 283 if (rte_strsplit(test_string, sizeof(test_string), splits, 0, ',') != 0 284 || errno != 0) { 285 LOG("Error: rte_strsplit did not accept 0 length array\n"); 286 return -1; 287 } 288 289 LOG("Parameter test cases passed\n"); 290 } while(0); 291 292 LOG("%s - PASSED\n", __func__); 293 return 0; 294 } 295 296 int 297 test_string_fns(void) 298 { 299 if (test_rte_snprintf() < 0 || 300 test_rte_strsplit() < 0) 301 return -1; 302 return 0; 303 } 304