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