4 * Copyright(c) 2010-2014 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.
40 #include <rte_string_fns.h>
44 #define LOG(...) do {\
45 fprintf(stderr, "%s() ln %d: ", __func__, __LINE__); \
46 fprintf(stderr, __VA_ARGS__); \
52 test_rte_snprintf(void)
54 /* =================================================
55 * First test with a string that will fit in buffer
56 * =================================================*/
59 const char source[] = "This is a string that will fit in buffer";
60 char buf[sizeof(source)+2]; /* make buffer big enough to fit string */
62 /* initialise buffer with characters so it can contain no nulls */
63 memset(buf, DATA_BYTE, sizeof(buf));
65 /* run rte_snprintf and check results */
66 retval = rte_snprintf(buf, sizeof(buf), "%s", source);
67 if (retval != sizeof(source) - 1) {
68 LOG("Error, retval = %d, expected = %u\n",
69 retval, (unsigned)sizeof(source));
72 if (buf[retval] != '\0') {
73 LOG("Error, resultant is not null-terminated\n");
76 if (memcmp(source, buf, sizeof(source)-1) != 0){
77 LOG("Error, corrupt data in buffer\n");
83 /* =================================================
84 * Test with a string that will get truncated
85 * =================================================*/
87 const char source[] = "This is a long string that won't fit in buffer";
88 char buf[sizeof(source)/2]; /* make buffer half the size */
90 /* initialise buffer with characters so it can contain no nulls */
91 memset(buf, DATA_BYTE, sizeof(buf));
93 /* run rte_snprintf and check results */
94 retval = rte_snprintf(buf, sizeof(buf), "%s", source);
95 if (retval != sizeof(source) - 1) {
96 LOG("Error, retval = %d, expected = %u\n",
97 retval, (unsigned)sizeof(source));
100 if (buf[sizeof(buf)-1] != '\0') {
101 LOG("Error, buffer is not null-terminated\n");
104 if (memcmp(source, buf, sizeof(buf)-1) != 0){
105 LOG("Error, corrupt data in buffer\n");
111 /* ===========================================================
112 * Test using zero-size buf to check how long a buffer we need
113 * ===========================================================*/
115 const char source[] = "This is a string";
118 /* call with a zero-sized non-NULL buffer, should tell how big a buffer
120 retval = rte_snprintf(buf, 0, "%s", source);
121 if (retval != sizeof(source) - 1) {
122 LOG("Call with 0-length buffer does not return correct size."
123 "Expected: %zu, got: %d\n", sizeof(source), retval);
127 /* call with a zero-sized NULL buffer, should tell how big a buffer
129 retval = rte_snprintf(NULL, 0, "%s", source);
130 if (retval != sizeof(source) - 1) {
131 LOG("Call with 0-length buffer does not return correct size."
132 "Expected: %zu, got: %d\n", sizeof(source), retval);
139 /* =================================================
140 * Test with invalid parameter values
141 * =================================================*/
142 const char source[] = "This is a string";
145 /* call with buffer value set to NULL is EINVAL */
146 if (rte_snprintf(NULL, sizeof(buf), "%s\n", source) != -1 ||
148 LOG("Failed to get suitable error when passing NULL buffer\n");
152 memset(buf, DATA_BYTE, sizeof(buf));
153 /* call with a NULL format and zero-size should return error
154 * without affecting the buffer */
155 if (rte_snprintf(buf, 0, NULL) != -1 ||
157 LOG("Failed to get suitable error when passing NULL buffer\n");
160 if (buf[0] != DATA_BYTE) {
161 LOG("Error, zero-length buffer modified after call with NULL"
166 /* call with a NULL format should return error but also null-terminate
168 if (rte_snprintf(buf, sizeof(buf), NULL) != -1 ||
170 LOG("Failed to get suitable error when passing NULL buffer\n");
173 if (buf[0] != '\0') {
174 LOG("Error, buffer not null-terminated after call with NULL"
180 LOG("%s - PASSED\n", __func__);
185 test_rte_strsplit(void)
189 /* =======================================================
190 * split a mac address correct number of splits requested
191 * =======================================================*/
192 char test_string[] = "54:65:76:87:98:90";
195 LOG("Source string: '%s', to split on ':'\n", test_string);
196 if (rte_strsplit(test_string, sizeof(test_string),
197 splits, 6, ':') != 6) {
198 LOG("Error splitting mac address\n");
201 for (i = 0; i < 6; i++)
202 LOG("Token %d = %s\n", i + 1, splits[i]);
207 /* =======================================================
208 * split on spaces smaller number of splits requested
209 * =======================================================*/
210 char test_string[] = "54 65 76 87 98 90";
213 LOG("Source string: '%s', to split on ' '\n", test_string);
214 if (rte_strsplit(test_string, sizeof(test_string),
215 splits, 3, ' ') != 3) {
216 LOG("Error splitting mac address for max 2 splits\n");
219 for (i = 0; i < 3; i++)
220 LOG("Token %d = %s\n", i + 1, splits[i]);
224 /* =======================================================
225 * split on commas - more splits than commas requested
226 * =======================================================*/
227 char test_string[] = "a,b,c,d";
230 LOG("Source string: '%s', to split on ','\n", test_string);
231 if (rte_strsplit(test_string, sizeof(test_string),
232 splits, 6, ',') != 4) {
233 LOG("Error splitting %s on ','\n", test_string);
236 for (i = 0; i < 4; i++)
237 LOG("Token %d = %s\n", i + 1, splits[i]);
241 /* =======================================================
242 * Try splitting on non-existent character.
243 * =======================================================*/
244 char test_string[] = "a,b,c,d";
247 LOG("Source string: '%s', to split on ' '\n", test_string);
248 if (rte_strsplit(test_string, sizeof(test_string),
249 splits, 6, ' ') != 1) {
250 LOG("Error splitting %s on ' '\n", test_string);
253 LOG("String not split\n");
257 /* =======================================================
258 * Invalid / edge case parameter checks
259 * =======================================================*/
260 char test_string[] = "a,b,c,d";
263 if (rte_strsplit(NULL, 0, splits, 6, ',') >= 0
265 LOG("Error: rte_strsplit accepted NULL string parameter\n");
269 if (rte_strsplit(test_string, sizeof(test_string), NULL, 0, ',') >= 0
271 LOG("Error: rte_strsplit accepted NULL array parameter\n");
276 if (rte_strsplit(test_string, 0, splits, 6, ',') != 0 || errno != 0) {
277 LOG("Error: rte_strsplit did not accept 0 length string\n");
281 if (rte_strsplit(test_string, sizeof(test_string), splits, 0, ',') != 0
283 LOG("Error: rte_strsplit did not accept 0 length array\n");
287 LOG("Parameter test cases passed\n");
290 LOG("%s - PASSED\n", __func__);
295 test_string_fns(void)
297 if (test_rte_snprintf() < 0 ||
298 test_rte_strsplit() < 0)
303 static struct test_command string_cmd = {
304 .command = "string_autotest",
305 .callback = test_string_fns,
307 REGISTER_TEST_COMMAND(string_cmd);