4 * Copyright(c) 2010-2012 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.
33 * version: DPDK.L.1.2.3-3
42 #include <rte_string_fns.h>
44 #include <cmdline_parse.h>
48 #define LOG(...) do {\
49 fprintf(stderr, "%s() ln %d: ", __func__, __LINE__); \
50 fprintf(stderr, __VA_ARGS__); \
56 test_rte_snprintf(void)
58 /* =================================================
59 * First test with a string that will fit in buffer
60 * =================================================*/
63 const char source[] = "This is a string that will fit in buffer";
64 char buf[sizeof(source)+2]; /* make buffer big enough to fit string */
66 /* initialise buffer with characters so it can contain no nulls */
67 memset(buf, DATA_BYTE, sizeof(buf));
69 /* run rte_snprintf and check results */
70 retval = rte_snprintf(buf, sizeof(buf), "%s", source);
71 if (retval != sizeof(source) - 1) {
72 LOG("Error, retval = %d, expected = %u\n",
73 retval, (unsigned)sizeof(source));
76 if (buf[retval] != '\0') {
77 LOG("Error, resultant is not null-terminated\n");
80 if (memcmp(source, buf, sizeof(source)-1) != 0){
81 LOG("Error, corrupt data in buffer\n");
87 /* =================================================
88 * Test with a string that will get truncated
89 * =================================================*/
91 const char source[] = "This is a long string that won't fit in buffer";
92 char buf[sizeof(source)/2]; /* make buffer half the size */
94 /* initialise buffer with characters so it can contain no nulls */
95 memset(buf, DATA_BYTE, sizeof(buf));
97 /* run rte_snprintf and check results */
98 retval = rte_snprintf(buf, sizeof(buf), "%s", source);
99 if (retval != sizeof(source) - 1) {
100 LOG("Error, retval = %d, expected = %u\n",
101 retval, (unsigned)sizeof(source));
104 if (buf[sizeof(buf)-1] != '\0') {
105 LOG("Error, buffer is not null-terminated\n");
108 if (memcmp(source, buf, sizeof(buf)-1) != 0){
109 LOG("Error, corrupt data in buffer\n");
115 /* ===========================================================
116 * Test using zero-size buf to check how long a buffer we need
117 * ===========================================================*/
119 const char source[] = "This is a string";
122 /* call with a zero-sized non-NULL buffer, should tell how big a buffer
124 retval = rte_snprintf(buf, 0, "%s", source);
125 if (retval != sizeof(source) - 1) {
126 LOG("Call with 0-length buffer does not return correct size."
127 "Expected: %zu, got: %d\n", sizeof(source), retval);
131 /* call with a zero-sized NULL buffer, should tell how big a buffer
133 retval = rte_snprintf(NULL, 0, "%s", source);
134 if (retval != sizeof(source) - 1) {
135 LOG("Call with 0-length buffer does not return correct size."
136 "Expected: %zu, got: %d\n", sizeof(source), retval);
143 /* =================================================
144 * Test with invalid parameter values
145 * =================================================*/
146 const char source[] = "This is a string";
149 /* call with buffer value set to NULL is EINVAL */
150 if (rte_snprintf(NULL, sizeof(buf), "%s\n", source) != -1 ||
152 LOG("Failed to get suitable error when passing NULL buffer\n");
156 memset(buf, DATA_BYTE, sizeof(buf));
157 /* call with a NULL format and zero-size should return error
158 * without affecting the buffer */
159 if (rte_snprintf(buf, 0, NULL) != -1 ||
161 LOG("Failed to get suitable error when passing NULL buffer\n");
164 if (buf[0] != DATA_BYTE) {
165 LOG("Error, zero-length buffer modified after call with NULL"
170 /* call with a NULL format should return error but also null-terminate
172 if (rte_snprintf(buf, sizeof(buf), NULL) != -1 ||
174 LOG("Failed to get suitable error when passing NULL buffer\n");
177 if (buf[0] != '\0') {
178 LOG("Error, buffer not null-terminated after call with NULL"
184 LOG("%s - PASSED\n", __func__);
189 test_rte_strsplit(void)
193 /* =======================================================
194 * split a mac address correct number of splits requested
195 * =======================================================*/
196 char test_string[] = "54:65:76:87:98:90";
199 LOG("Source string: '%s', to split on ':'\n", test_string);
200 if (rte_strsplit(test_string, sizeof(test_string),
201 splits, 6, ':') != 6) {
202 LOG("Error splitting mac address\n");
205 for (i = 0; i < 6; i++)
206 LOG("Token %d = %s\n", i + 1, splits[i]);
211 /* =======================================================
212 * split on spaces smaller number of splits requested
213 * =======================================================*/
214 char test_string[] = "54 65 76 87 98 90";
217 LOG("Source string: '%s', to split on ' '\n", test_string);
218 if (rte_strsplit(test_string, sizeof(test_string),
219 splits, 3, ' ') != 3) {
220 LOG("Error splitting mac address for max 2 splits\n");
223 for (i = 0; i < 3; i++)
224 LOG("Token %d = %s\n", i + 1, splits[i]);
228 /* =======================================================
229 * split on commas - more splits than commas requested
230 * =======================================================*/
231 char test_string[] = "a,b,c,d";
234 LOG("Source string: '%s', to split on ','\n", test_string);
235 if (rte_strsplit(test_string, sizeof(test_string),
236 splits, 6, ',') != 4) {
237 LOG("Error splitting %s on ','\n", test_string);
240 for (i = 0; i < 4; i++)
241 LOG("Token %d = %s\n", i + 1, splits[i]);
245 /* =======================================================
246 * Try splitting on non-existent character.
247 * =======================================================*/
248 char test_string[] = "a,b,c,d";
251 LOG("Source string: '%s', to split on ' '\n", test_string);
252 if (rte_strsplit(test_string, sizeof(test_string),
253 splits, 6, ' ') != 1) {
254 LOG("Error splitting %s on ' '\n", test_string);
257 LOG("String not split\n");
261 /* =======================================================
262 * Invalid / edge case parameter checks
263 * =======================================================*/
264 char test_string[] = "a,b,c,d";
267 if (rte_strsplit(NULL, 0, splits, 6, ',') >= 0
269 LOG("Error: rte_strsplit accepted NULL string parameter\n");
273 if (rte_strsplit(test_string, sizeof(test_string), NULL, 0, ',') >= 0
275 LOG("Error: rte_strsplit accepted NULL array parameter\n");
280 if (rte_strsplit(test_string, 0, splits, 6, ',') != 0 || errno != 0) {
281 LOG("Error: rte_strsplit did not accept 0 length string\n");
285 if (rte_strsplit(test_string, sizeof(test_string), splits, 0, ',') != 0
287 LOG("Error: rte_strsplit did not accept 0 length array\n");
291 LOG("Parameter test cases passed\n");
294 LOG("%s - PASSED\n", __func__);
299 test_string_fns(void)
301 if (test_rte_snprintf() < 0 ||
302 test_rte_strsplit() < 0)