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.
41 #include <rte_string_fns.h>
43 #include <cmdline_parse.h>
47 #define LOG(...) do {\
48 fprintf(stderr, "%s() ln %d: ", __func__, __LINE__); \
49 fprintf(stderr, __VA_ARGS__); \
55 test_rte_snprintf(void)
57 /* =================================================
58 * First test with a string that will fit in buffer
59 * =================================================*/
62 const char source[] = "This is a string that will fit in buffer";
63 char buf[sizeof(source)+2]; /* make buffer big enough to fit string */
65 /* initialise buffer with characters so it can contain no nulls */
66 memset(buf, DATA_BYTE, sizeof(buf));
68 /* run rte_snprintf and check results */
69 retval = rte_snprintf(buf, sizeof(buf), "%s", source);
70 if (retval != sizeof(source) - 1) {
71 LOG("Error, retval = %d, expected = %u\n",
72 retval, (unsigned)sizeof(source));
75 if (buf[retval] != '\0') {
76 LOG("Error, resultant is not null-terminated\n");
79 if (memcmp(source, buf, sizeof(source)-1) != 0){
80 LOG("Error, corrupt data in buffer\n");
86 /* =================================================
87 * Test with a string that will get truncated
88 * =================================================*/
90 const char source[] = "This is a long string that won't fit in buffer";
91 char buf[sizeof(source)/2]; /* make buffer half the size */
93 /* initialise buffer with characters so it can contain no nulls */
94 memset(buf, DATA_BYTE, sizeof(buf));
96 /* run rte_snprintf and check results */
97 retval = rte_snprintf(buf, sizeof(buf), "%s", source);
98 if (retval != sizeof(source) - 1) {
99 LOG("Error, retval = %d, expected = %u\n",
100 retval, (unsigned)sizeof(source));
103 if (buf[sizeof(buf)-1] != '\0') {
104 LOG("Error, buffer is not null-terminated\n");
107 if (memcmp(source, buf, sizeof(buf)-1) != 0){
108 LOG("Error, corrupt data in buffer\n");
114 /* ===========================================================
115 * Test using zero-size buf to check how long a buffer we need
116 * ===========================================================*/
118 const char source[] = "This is a string";
121 /* call with a zero-sized non-NULL buffer, should tell how big a buffer
123 retval = rte_snprintf(buf, 0, "%s", source);
124 if (retval != sizeof(source) - 1) {
125 LOG("Call with 0-length buffer does not return correct size."
126 "Expected: %zu, got: %d\n", sizeof(source), retval);
130 /* call with a zero-sized NULL buffer, should tell how big a buffer
132 retval = rte_snprintf(NULL, 0, "%s", source);
133 if (retval != sizeof(source) - 1) {
134 LOG("Call with 0-length buffer does not return correct size."
135 "Expected: %zu, got: %d\n", sizeof(source), retval);
142 /* =================================================
143 * Test with invalid parameter values
144 * =================================================*/
145 const char source[] = "This is a string";
148 /* call with buffer value set to NULL is EINVAL */
149 if (rte_snprintf(NULL, sizeof(buf), "%s\n", source) != -1 ||
151 LOG("Failed to get suitable error when passing NULL buffer\n");
155 memset(buf, DATA_BYTE, sizeof(buf));
156 /* call with a NULL format and zero-size should return error
157 * without affecting the buffer */
158 if (rte_snprintf(buf, 0, NULL) != -1 ||
160 LOG("Failed to get suitable error when passing NULL buffer\n");
163 if (buf[0] != DATA_BYTE) {
164 LOG("Error, zero-length buffer modified after call with NULL"
169 /* call with a NULL format should return error but also null-terminate
171 if (rte_snprintf(buf, sizeof(buf), NULL) != -1 ||
173 LOG("Failed to get suitable error when passing NULL buffer\n");
176 if (buf[0] != '\0') {
177 LOG("Error, buffer not null-terminated after call with NULL"
183 LOG("%s - PASSED\n", __func__);
188 test_rte_strsplit(void)
192 /* =======================================================
193 * split a mac address correct number of splits requested
194 * =======================================================*/
195 char test_string[] = "54:65:76:87:98:90";
198 LOG("Source string: '%s', to split on ':'\n", test_string);
199 if (rte_strsplit(test_string, sizeof(test_string),
200 splits, 6, ':') != 6) {
201 LOG("Error splitting mac address\n");
204 for (i = 0; i < 6; i++)
205 LOG("Token %d = %s\n", i + 1, splits[i]);
210 /* =======================================================
211 * split on spaces smaller number of splits requested
212 * =======================================================*/
213 char test_string[] = "54 65 76 87 98 90";
216 LOG("Source string: '%s', to split on ' '\n", test_string);
217 if (rte_strsplit(test_string, sizeof(test_string),
218 splits, 3, ' ') != 3) {
219 LOG("Error splitting mac address for max 2 splits\n");
222 for (i = 0; i < 3; i++)
223 LOG("Token %d = %s\n", i + 1, splits[i]);
227 /* =======================================================
228 * split on commas - more splits than commas requested
229 * =======================================================*/
230 char test_string[] = "a,b,c,d";
233 LOG("Source string: '%s', to split on ','\n", test_string);
234 if (rte_strsplit(test_string, sizeof(test_string),
235 splits, 6, ',') != 4) {
236 LOG("Error splitting %s on ','\n", test_string);
239 for (i = 0; i < 4; i++)
240 LOG("Token %d = %s\n", i + 1, splits[i]);
244 /* =======================================================
245 * Try splitting on non-existent character.
246 * =======================================================*/
247 char test_string[] = "a,b,c,d";
250 LOG("Source string: '%s', to split on ' '\n", test_string);
251 if (rte_strsplit(test_string, sizeof(test_string),
252 splits, 6, ' ') != 1) {
253 LOG("Error splitting %s on ' '\n", test_string);
256 LOG("String not split\n");
260 /* =======================================================
261 * Invalid / edge case parameter checks
262 * =======================================================*/
263 char test_string[] = "a,b,c,d";
266 if (rte_strsplit(NULL, 0, splits, 6, ',') >= 0
268 LOG("Error: rte_strsplit accepted NULL string parameter\n");
272 if (rte_strsplit(test_string, sizeof(test_string), NULL, 0, ',') >= 0
274 LOG("Error: rte_strsplit accepted NULL array parameter\n");
279 if (rte_strsplit(test_string, 0, splits, 6, ',') != 0 || errno != 0) {
280 LOG("Error: rte_strsplit did not accept 0 length string\n");
284 if (rte_strsplit(test_string, sizeof(test_string), splits, 0, ',') != 0
286 LOG("Error: rte_strsplit did not accept 0 length array\n");
290 LOG("Parameter test cases passed\n");
293 LOG("%s - PASSED\n", __func__);
298 test_string_fns(void)
300 if (test_rte_snprintf() < 0 ||
301 test_rte_strsplit() < 0)