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