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>
42 #include <cmdline_parse.h>
46 #define LOG(...) do {\
47 fprintf(stderr, "%s() ln %d: ", __func__, __LINE__); \
48 fprintf(stderr, __VA_ARGS__); \
54 test_rte_snprintf(void)
56 /* =================================================
57 * First test with a string that will fit in buffer
58 * =================================================*/
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 */
64 /* initialise buffer with characters so it can contain no nulls */
65 memset(buf, DATA_BYTE, sizeof(buf));
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));
74 if (buf[retval] != '\0') {
75 LOG("Error, resultant is not null-terminated\n");
78 if (memcmp(source, buf, sizeof(source)-1) != 0){
79 LOG("Error, corrupt data in buffer\n");
85 /* =================================================
86 * Test with a string that will get truncated
87 * =================================================*/
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 */
92 /* initialise buffer with characters so it can contain no nulls */
93 memset(buf, DATA_BYTE, sizeof(buf));
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));
102 if (buf[sizeof(buf)-1] != '\0') {
103 LOG("Error, buffer is not null-terminated\n");
106 if (memcmp(source, buf, sizeof(buf)-1) != 0){
107 LOG("Error, corrupt data in buffer\n");
113 /* ===========================================================
114 * Test using zero-size buf to check how long a buffer we need
115 * ===========================================================*/
117 const char source[] = "This is a string";
120 /* call with a zero-sized non-NULL buffer, should tell how big a buffer
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);
129 /* call with a zero-sized NULL buffer, should tell how big a buffer
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);
141 /* =================================================
142 * Test with invalid parameter values
143 * =================================================*/
144 const char source[] = "This is a string";
147 /* call with buffer value set to NULL is EINVAL */
148 if (rte_snprintf(NULL, sizeof(buf), "%s\n", source) != -1 ||
150 LOG("Failed to get suitable error when passing NULL buffer\n");
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 ||
159 LOG("Failed to get suitable error when passing NULL buffer\n");
162 if (buf[0] != DATA_BYTE) {
163 LOG("Error, zero-length buffer modified after call with NULL"
168 /* call with a NULL format should return error but also null-terminate
170 if (rte_snprintf(buf, sizeof(buf), NULL) != -1 ||
172 LOG("Failed to get suitable error when passing NULL buffer\n");
175 if (buf[0] != '\0') {
176 LOG("Error, buffer not null-terminated after call with NULL"
182 LOG("%s - PASSED\n", __func__);
187 test_rte_strsplit(void)
191 /* =======================================================
192 * split a mac address correct number of splits requested
193 * =======================================================*/
194 char test_string[] = "54:65:76:87:98:90";
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");
203 for (i = 0; i < 6; i++)
204 LOG("Token %d = %s\n", i + 1, splits[i]);
209 /* =======================================================
210 * split on spaces smaller number of splits requested
211 * =======================================================*/
212 char test_string[] = "54 65 76 87 98 90";
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");
221 for (i = 0; i < 3; i++)
222 LOG("Token %d = %s\n", i + 1, splits[i]);
226 /* =======================================================
227 * split on commas - more splits than commas requested
228 * =======================================================*/
229 char test_string[] = "a,b,c,d";
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);
238 for (i = 0; i < 4; i++)
239 LOG("Token %d = %s\n", i + 1, splits[i]);
243 /* =======================================================
244 * Try splitting on non-existent character.
245 * =======================================================*/
246 char test_string[] = "a,b,c,d";
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);
255 LOG("String not split\n");
259 /* =======================================================
260 * Invalid / edge case parameter checks
261 * =======================================================*/
262 char test_string[] = "a,b,c,d";
265 if (rte_strsplit(NULL, 0, splits, 6, ',') >= 0
267 LOG("Error: rte_strsplit accepted NULL string parameter\n");
271 if (rte_strsplit(test_string, sizeof(test_string), NULL, 0, ',') >= 0
273 LOG("Error: rte_strsplit accepted NULL array parameter\n");
278 if (rte_strsplit(test_string, 0, splits, 6, ',') != 0 || errno != 0) {
279 LOG("Error: rte_strsplit did not accept 0 length string\n");
283 if (rte_strsplit(test_string, sizeof(test_string), splits, 0, ',') != 0
285 LOG("Error: rte_strsplit did not accept 0 length array\n");
289 LOG("Parameter test cases passed\n");
292 LOG("%s - PASSED\n", __func__);
297 test_string_fns(void)
299 if (test_rte_snprintf() < 0 ||
300 test_rte_strsplit() < 0)