New upstream version 18.02
[deb_dpdk.git] / test / test / test_cmdline_cirbuf.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  */
4
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8
9 #include <rte_string_fns.h>
10
11 #include <cmdline_cirbuf.h>
12
13 #include "test_cmdline.h"
14
15 /* different length strings */
16 #define CIRBUF_STR_HEAD " HEAD"
17 #define CIRBUF_STR_TAIL "TAIL"
18
19 /* miscellaneous tests - they make bullseye happy */
20 static int
21 test_cirbuf_string_misc(void)
22 {
23         struct cirbuf cb;
24         char buf[CMDLINE_TEST_BUFSIZE];
25         char tmp[CMDLINE_TEST_BUFSIZE];
26
27         /* initialize buffers */
28         memset(buf, 0, sizeof(buf));
29         memset(tmp, 0, sizeof(tmp));
30
31         /*
32          * initialize circular buffer
33          */
34         if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
35                 printf("Error: failed to initialize circular buffer!\n");
36                 return -1;
37         }
38
39         /*
40          * add strings to head and tail, but read only tail
41          * this results in read operation that does not transcend
42          * from buffer end to buffer beginning (in other words,
43          * strlen <= cb->maxlen - cb->end)
44          */
45
46         /* add string to head */
47         if (cirbuf_add_buf_head(&cb, CIRBUF_STR_HEAD, sizeof(CIRBUF_STR_HEAD))
48                         != (sizeof(CIRBUF_STR_HEAD))) {
49                 printf("Error: failed to add string to head!\n");
50                 return -1;
51         }
52         /* add string to tail */
53         if (cirbuf_add_buf_tail(&cb, CIRBUF_STR_TAIL, sizeof(CIRBUF_STR_TAIL))
54                         != (sizeof(CIRBUF_STR_TAIL))) {
55                 printf("Error: failed to add string to head!\n");
56                 return -1;
57         }
58         /* read string from tail */
59         if (cirbuf_get_buf_tail(&cb, tmp, sizeof(CIRBUF_STR_TAIL))
60                         != (sizeof(CIRBUF_STR_TAIL))) {
61                 printf("Error: failed to get string from tail!\n");
62                 return -1;
63         }
64         /* verify string */
65         if (strncmp(tmp, CIRBUF_STR_TAIL, sizeof(CIRBUF_STR_TAIL)) != 0) {
66                 printf("Error: tail strings do not match!\n");
67                 return -1;
68         }
69         /* clear buffers */
70         memset(tmp, 0, sizeof(tmp));
71         memset(buf, 0, sizeof(buf));
72
73
74
75         /*
76          * add a string to buffer when start/end is at end of buffer
77          */
78
79         /*
80          * reinitialize circular buffer with start at the end of cirbuf
81          */
82         if (cirbuf_init(&cb, buf, CMDLINE_TEST_BUFSIZE - 2, sizeof(buf)) < 0) {
83                 printf("Error: failed to reinitialize circular buffer!\n");
84                 return -1;
85         }
86
87
88         /* add string to tail */
89         if (cirbuf_add_buf_tail(&cb, CIRBUF_STR_TAIL, sizeof(CIRBUF_STR_TAIL))
90                         != (sizeof(CIRBUF_STR_TAIL))) {
91                 printf("Error: failed to add string to tail!\n");
92                 return -1;
93         }
94         /* read string from tail */
95         if (cirbuf_get_buf_tail(&cb, tmp, sizeof(CIRBUF_STR_TAIL))
96                         != (sizeof(CIRBUF_STR_TAIL))) {
97                 printf("Error: failed to get string from tail!\n");
98                 return -1;
99         }
100         /* verify string */
101         if (strncmp(tmp, CIRBUF_STR_TAIL, sizeof(CIRBUF_STR_TAIL)) != 0) {
102                 printf("Error: tail strings do not match!\n");
103                 return -1;
104         }
105         /* clear tmp buffer */
106         memset(tmp, 0, sizeof(tmp));
107
108
109         /* add string to head */
110         if (cirbuf_add_buf_head(&cb, CIRBUF_STR_HEAD, sizeof(CIRBUF_STR_HEAD))
111                         != (sizeof(CIRBUF_STR_HEAD))) {
112                 printf("Error: failed to add string to head!\n");
113                 return -1;
114         }
115         /* read string from tail */
116         if (cirbuf_get_buf_head(&cb, tmp, sizeof(CIRBUF_STR_HEAD))
117                         != (sizeof(CIRBUF_STR_HEAD))) {
118                 printf("Error: failed to get string from head!\n");
119                 return -1;
120         }
121         /* verify string */
122         if (strncmp(tmp, CIRBUF_STR_HEAD, sizeof(CIRBUF_STR_HEAD)) != 0) {
123                 printf("Error: headstrings do not match!\n");
124                 return -1;
125         }
126
127         return 0;
128 }
129
130 /* test adding and deleting strings */
131 static int
132 test_cirbuf_string_add_del(void)
133 {
134         struct cirbuf cb;
135         char buf[CMDLINE_TEST_BUFSIZE];
136         char tmp[CMDLINE_TEST_BUFSIZE];
137
138         /* initialize buffers */
139         memset(buf, 0, sizeof(buf));
140         memset(tmp, 0, sizeof(tmp));
141
142         /*
143          * initialize circular buffer
144          */
145         if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
146                 printf("Error: failed to initialize circular buffer!\n");
147                 return -1;
148         }
149
150         /* add string to head */
151         if (cirbuf_add_buf_head(&cb, CIRBUF_STR_HEAD, sizeof(CIRBUF_STR_HEAD))
152                         != (sizeof(CIRBUF_STR_HEAD))) {
153                 printf("Error: failed to add string to head!\n");
154                 return -1;
155         }
156         /* read string from head */
157         if (cirbuf_get_buf_head(&cb, tmp, sizeof(CIRBUF_STR_HEAD))
158                         != (sizeof(CIRBUF_STR_HEAD))) {
159                 printf("Error: failed to get string from head!\n");
160                 return -1;
161         }
162         /* verify string */
163         if (strncmp(tmp, CIRBUF_STR_HEAD, sizeof(CIRBUF_STR_HEAD)) != 0) {
164                 printf("Error: head strings do not match!\n");
165                 return -1;
166         }
167         /* clear tmp buffer */
168         memset(tmp, 0, sizeof(tmp));
169         /* read string from tail */
170         if (cirbuf_get_buf_tail(&cb, tmp, sizeof(CIRBUF_STR_HEAD))
171                         != (sizeof(CIRBUF_STR_HEAD))) {
172                 printf("Error: failed to get string from head!\n");
173                 return -1;
174         }
175         /* verify string */
176         if (strncmp(tmp, CIRBUF_STR_HEAD, sizeof(CIRBUF_STR_HEAD)) != 0) {
177                 printf("Error: head strings do not match!\n");
178                 return -1;
179         }
180         /* delete string from head*/
181         if (cirbuf_del_buf_head(&cb, sizeof(CIRBUF_STR_HEAD)) < 0) {
182                 printf("Error: failed to delete string from head!\n");
183                 return -1;
184         }
185         /* verify string was deleted */
186         if (cirbuf_del_head_safe(&cb) == 0) {
187                 printf("Error: buffer should have been empty!\n");
188                 return -1;
189         }
190         /* clear tmp buffer */
191         memset(tmp, 0, sizeof(tmp));
192
193
194
195         /*
196          * reinitialize circular buffer
197          */
198         memset(buf, 0, sizeof(buf));
199         if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
200                 printf("Error: failed to reinitialize circular buffer!\n");
201                 return -1;
202         }
203
204         /* add string to tail */
205         if (cirbuf_add_buf_tail(&cb, CIRBUF_STR_TAIL, sizeof(CIRBUF_STR_TAIL))
206                         != (sizeof(CIRBUF_STR_TAIL))) {
207                 printf("Error: failed to add string to tail!\n");
208                 return -1;
209         }
210         /* get string from tail */
211         if (cirbuf_get_buf_tail(&cb, tmp, sizeof(CIRBUF_STR_TAIL))
212                         != (sizeof(CIRBUF_STR_TAIL))) {
213                 printf("Error: failed to get string from tail!\n");
214                 return -1;
215         }
216         /* verify string */
217         if (strncmp(tmp, CIRBUF_STR_TAIL, sizeof(CIRBUF_STR_TAIL)) != 0) {
218                 printf("Error: tail strings do not match!\n");
219                 return -1;
220         }
221         /* clear tmp buffer */
222         memset(tmp, 0, sizeof(tmp));
223         /* get string from head */
224         if (cirbuf_get_buf_head(&cb, tmp, sizeof(CIRBUF_STR_TAIL))
225                         != (sizeof(CIRBUF_STR_TAIL))) {
226                 printf("Error: failed to get string from tail!\n");
227                 return -1;
228         }
229         /* verify string */
230         if (strncmp(tmp, CIRBUF_STR_TAIL, sizeof(CIRBUF_STR_TAIL)) != 0) {
231                 printf("Error: tail strings do not match!\n");
232                 return -1;
233         }
234         /* delete string from tail */
235         if (cirbuf_del_buf_tail(&cb, sizeof(CIRBUF_STR_TAIL)) < 0) {
236                 printf("Error: failed to delete string from tail!\n");
237                 return -1;
238         }
239         /* verify string was deleted */
240         if (cirbuf_del_tail_safe(&cb) == 0) {
241                 printf("Error: buffer should have been empty!\n");
242                 return -1;
243         }
244
245         return 0;
246 }
247
248 /* test adding from head and deleting from tail, and vice versa */
249 static int
250 test_cirbuf_string_add_del_reverse(void)
251 {
252         struct cirbuf cb;
253         char buf[CMDLINE_TEST_BUFSIZE];
254         char tmp[CMDLINE_TEST_BUFSIZE];
255
256         /* initialize buffers */
257         memset(buf, 0, sizeof(buf));
258         memset(tmp, 0, sizeof(tmp));
259
260         /*
261          * initialize circular buffer
262          */
263         if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
264                 printf("Error: failed to initialize circular buffer!\n");
265                 return -1;
266         }
267
268         /* add string to head */
269         if (cirbuf_add_buf_head(&cb, CIRBUF_STR_HEAD, sizeof(CIRBUF_STR_HEAD))
270                         != (sizeof(CIRBUF_STR_HEAD))) {
271                 printf("Error: failed to add string to head!\n");
272                 return -1;
273         }
274         /* delete string from tail */
275         if (cirbuf_del_buf_tail(&cb, sizeof(CIRBUF_STR_HEAD)) < 0) {
276                 printf("Error: failed to delete string from tail!\n");
277                 return -1;
278         }
279         /* verify string was deleted */
280         if (cirbuf_del_tail_safe(&cb) == 0) {
281                 printf("Error: buffer should have been empty!\n");
282                 return -1;
283         }
284         /* clear tmp buffer */
285         memset(tmp, 0, sizeof(tmp));
286
287         /*
288          * reinitialize circular buffer
289          */
290         memset(buf, 0, sizeof(buf));
291         if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
292                 printf("Error: failed to reinitialize circular buffer!\n");
293                 return -1;
294         }
295
296         /* add string to tail */
297         if (cirbuf_add_buf_tail(&cb, CIRBUF_STR_TAIL, sizeof(CIRBUF_STR_TAIL))
298                         != (sizeof(CIRBUF_STR_TAIL))) {
299                 printf("Error: failed to add string to tail!\n");
300                 return -1;
301         }
302         /* delete string from head */
303         if (cirbuf_del_buf_head(&cb, sizeof(CIRBUF_STR_TAIL)) < 0) {
304                 printf("Error: failed to delete string from head!\n");
305                 return -1;
306         }
307         /* verify string was deleted */
308         if (cirbuf_del_head_safe(&cb) == 0) {
309                 printf("Error: buffer should have been empty!\n");
310                 return -1;
311         }
312
313         return 0;
314 }
315
316 /* try to write more than available */
317 static int
318 test_cirbuf_string_add_boundaries(void)
319 {
320         struct cirbuf cb;
321         char buf[CMDLINE_TEST_BUFSIZE];
322         unsigned i;
323
324         /* initialize buffers */
325         memset(buf, 0, sizeof(buf));
326
327         /*
328          * initialize circular buffer
329          */
330         if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
331                 printf("Error: failed to initialize circular buffer!\n");
332                 return -1;
333         }
334
335         /* fill the buffer from tail */
336         for (i = 0; i < CMDLINE_TEST_BUFSIZE - sizeof(CIRBUF_STR_TAIL) + 1; i++)
337                 cirbuf_add_tail_safe(&cb, 't');
338
339         /* try adding a string to tail */
340         if (cirbuf_add_buf_tail(&cb, CIRBUF_STR_TAIL, sizeof(CIRBUF_STR_TAIL))
341                         > 0) {
342                 printf("Error: buffer should have been full!\n");
343                 return -1;
344         }
345         /* try adding a string to head */
346         if (cirbuf_add_buf_head(&cb, CIRBUF_STR_TAIL, sizeof(CIRBUF_STR_TAIL))
347                         > 0) {
348                 printf("Error: buffer should have been full!\n");
349                 return -1;
350         }
351
352         /*
353          * reinitialize circular buffer
354          */
355         memset(buf, 0, sizeof(buf));
356         if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
357                 printf("Error: failed to reinitialize circular buffer!\n");
358                 return -1;
359         }
360
361         /* fill the buffer from head */
362         for (i = 0; i < CMDLINE_TEST_BUFSIZE - sizeof(CIRBUF_STR_HEAD) + 1; i++)
363                 cirbuf_add_head_safe(&cb, 'h');
364
365         /* try adding a string to head */
366         if (cirbuf_add_buf_head(&cb, CIRBUF_STR_HEAD, sizeof(CIRBUF_STR_HEAD))
367                         > 0) {
368                 printf("Error: buffer should have been full!\n");
369                 return -1;
370         }
371         /* try adding a string to tail */
372         if (cirbuf_add_buf_tail(&cb, CIRBUF_STR_HEAD, sizeof(CIRBUF_STR_HEAD))
373                         > 0) {
374                 printf("Error: buffer should have been full!\n");
375                 return -1;
376         }
377
378         return 0;
379 }
380
381 /* try to read/delete more than written */
382 static int
383 test_cirbuf_string_get_del_boundaries(void)
384 {
385         struct cirbuf cb;
386         char buf[CMDLINE_TEST_BUFSIZE];
387         char tmp[CMDLINE_TEST_BUFSIZE];
388
389         /* initialize buffers */
390         memset(buf, 0, sizeof(buf));
391         memset(tmp, 0, sizeof(tmp));
392
393         /*
394          * initialize circular buffer
395          */
396         if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
397                 printf("Error: failed to initialize circular buffer!\n");
398                 return -1;
399         }
400
401
402         /* add string to head */
403         if (cirbuf_add_buf_head(&cb, CIRBUF_STR_HEAD, sizeof(CIRBUF_STR_HEAD))
404                                 != (sizeof(CIRBUF_STR_HEAD))) {
405                 printf("Error: failed to add string to head!\n");
406                 return -1;
407         }
408         /* read more than written (head) */
409         if (cirbuf_get_buf_head(&cb, tmp, sizeof(CIRBUF_STR_HEAD) + 1)
410                         != sizeof(CIRBUF_STR_HEAD)) {
411                 printf("Error: unexpected result when reading too much data!\n");
412                 return -1;
413         }
414         /* read more than written (tail) */
415         if (cirbuf_get_buf_tail(&cb, tmp, sizeof(CIRBUF_STR_HEAD) + 1)
416                         != sizeof(CIRBUF_STR_HEAD)) {
417                 printf("Error: unexpected result when reading too much data!\n");
418                 return -1;
419         }
420         /* delete more than written (head) */
421         if (cirbuf_del_buf_head(&cb, sizeof(CIRBUF_STR_HEAD) + 1) == 0) {
422                 printf("Error: unexpected result when deleting too much data!\n");
423                 return -1;
424         }
425         /* delete more than written (tail) */
426         if (cirbuf_del_buf_tail(&cb, sizeof(CIRBUF_STR_HEAD) + 1) == 0) {
427                 printf("Error: unexpected result when deleting too much data!\n");
428                 return -1;
429         }
430
431         /*
432          * reinitialize circular buffer
433          */
434         memset(buf, 0, sizeof(buf));
435         if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
436                 printf("Error: failed to reinitialize circular buffer!\n");
437                 return -1;
438         }
439
440         /* add string to tail */
441         if (cirbuf_add_buf_tail(&cb, CIRBUF_STR_TAIL, sizeof(CIRBUF_STR_TAIL))
442                                 != (sizeof(CIRBUF_STR_TAIL))) {
443                 printf("Error: failed to add string to tail!\n");
444                 return -1;
445         }
446         /* read more than written (tail) */
447         if (cirbuf_get_buf_tail(&cb, tmp, sizeof(CIRBUF_STR_TAIL) + 1)
448                         != sizeof(CIRBUF_STR_TAIL)) {
449                 printf("Error: unexpected result when reading too much data!\n");
450                 return -1;
451         }
452         /* read more than written (head) */
453         if (cirbuf_get_buf_head(&cb, tmp, sizeof(CIRBUF_STR_TAIL) + 1)
454                         != sizeof(CIRBUF_STR_TAIL)) {
455                 printf("Error: unexpected result when reading too much data!\n");
456                 return -1;
457         }
458         /* delete more than written (tail) */
459         if (cirbuf_del_buf_tail(&cb, sizeof(CIRBUF_STR_TAIL) + 1) == 0) {
460                 printf("Error: unexpected result when deleting too much data!\n");
461                 return -1;
462         }
463         /* delete more than written (head) */
464         if (cirbuf_del_buf_tail(&cb, sizeof(CIRBUF_STR_TAIL) + 1) == 0) {
465                 printf("Error: unexpected result when deleting too much data!\n");
466                 return -1;
467         }
468
469         return 0;
470 }
471
472 /* try to read/delete less than written */
473 static int
474 test_cirbuf_string_get_del_partial(void)
475 {
476         struct cirbuf cb;
477         char buf[CMDLINE_TEST_BUFSIZE];
478         char tmp[CMDLINE_TEST_BUFSIZE];
479         char tmp2[CMDLINE_TEST_BUFSIZE];
480
481         /* initialize buffers */
482         memset(buf, 0, sizeof(buf));
483         memset(tmp, 0, sizeof(tmp));
484         memset(tmp2, 0, sizeof(tmp));
485
486         snprintf(tmp2, sizeof(tmp2), "%s", CIRBUF_STR_HEAD);
487
488         /*
489          * initialize circular buffer
490          */
491         if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
492                 printf("Error: failed to initialize circular buffer!\n");
493                 return -1;
494         }
495
496         /* add string to head */
497         if (cirbuf_add_buf_head(&cb, CIRBUF_STR_HEAD, sizeof(CIRBUF_STR_HEAD))
498                                 != (sizeof(CIRBUF_STR_HEAD))) {
499                 printf("Error: failed to add string to head!\n");
500                 return -1;
501         }
502         /* read less than written (head) */
503         if (cirbuf_get_buf_head(&cb, tmp, sizeof(CIRBUF_STR_HEAD) - 1)
504                         != sizeof(CIRBUF_STR_HEAD) - 1) {
505                 printf("Error: unexpected result when reading from head!\n");
506                 return -1;
507         }
508         /* verify string */
509         if (strncmp(tmp, tmp2, sizeof(CIRBUF_STR_HEAD) - 1) != 0) {
510                 printf("Error: strings mismatch!\n");
511                 return -1;
512         }
513         memset(tmp, 0, sizeof(tmp));
514         /* read less than written (tail) */
515         if (cirbuf_get_buf_tail(&cb, tmp, sizeof(CIRBUF_STR_HEAD) - 1)
516                         != sizeof(CIRBUF_STR_HEAD) - 1) {
517                 printf("Error: unexpected result when reading from tail!\n");
518                 return -1;
519         }
520         /* verify string */
521         if (strncmp(tmp, &tmp2[1], sizeof(CIRBUF_STR_HEAD) - 1) != 0) {
522                 printf("Error: strings mismatch!\n");
523                 return -1;
524         }
525
526         /*
527          * verify correct deletion
528          */
529
530         /* clear buffer */
531         memset(tmp, 0, sizeof(tmp));
532
533         /* delete less than written (head) */
534         if (cirbuf_del_buf_head(&cb, 1) != 0) {
535                 printf("Error: delete from head failed!\n");
536                 return -1;
537         }
538         /* read from head */
539         if (cirbuf_get_buf_head(&cb, tmp, sizeof(CIRBUF_STR_HEAD) - 1)
540                         != sizeof(CIRBUF_STR_HEAD) - 1) {
541                 printf("Error: unexpected result when reading from head!\n");
542                 return -1;
543         }
544         /* since we deleted from head, first char should be deleted */
545         if (strncmp(tmp, &tmp2[1], sizeof(CIRBUF_STR_HEAD) - 1) != 0) {
546                 printf("Error: strings mismatch!\n");
547                 return -1;
548         }
549         /* clear buffer */
550         memset(tmp, 0, sizeof(tmp));
551
552         /* delete less than written (tail) */
553         if (cirbuf_del_buf_tail(&cb, 1) != 0) {
554                 printf("Error: delete from tail failed!\n");
555                 return -1;
556         }
557         /* read from tail */
558         if (cirbuf_get_buf_tail(&cb, tmp, sizeof(CIRBUF_STR_HEAD) - 2)
559                         != sizeof(CIRBUF_STR_HEAD) - 2) {
560                 printf("Error: unexpected result when reading from head!\n");
561                 return -1;
562         }
563         /* since we deleted from tail, last char should be deleted */
564         if (strncmp(tmp, &tmp2[1], sizeof(CIRBUF_STR_HEAD) - 2) != 0) {
565                 printf("Error: strings mismatch!\n");
566                 return -1;
567         }
568
569         return 0;
570 }
571
572 /* test cmdline_cirbuf char add/del functions */
573 static int
574 test_cirbuf_char_add_del(void)
575 {
576         struct cirbuf cb;
577         char buf[CMDLINE_TEST_BUFSIZE];
578         char tmp[CMDLINE_TEST_BUFSIZE];
579
580         /* clear buffer */
581         memset(buf, 0, sizeof(buf));
582         memset(tmp, 0, sizeof(tmp));
583
584         /*
585          * initialize circular buffer
586          */
587         if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
588                 printf("Error: failed to initialize circular buffer!\n");
589                 return -1;
590         }
591
592         /*
593          * try to delete something from cirbuf. since it's empty,
594          * these should fail.
595          */
596         if (cirbuf_del_head_safe(&cb) == 0) {
597                 printf("Error: deleting from empty cirbuf head succeeded!\n");
598                 return -1;
599         }
600         if (cirbuf_del_tail_safe(&cb) == 0) {
601                 printf("Error: deleting from empty cirbuf tail succeeded!\n");
602                 return -1;
603         }
604
605         /*
606          * add, verify and delete. these should pass.
607          */
608         if (cirbuf_add_head_safe(&cb,'h') < 0) {
609                 printf("Error: adding to cirbuf head failed!\n");
610                 return -1;
611         }
612         if (cirbuf_get_head(&cb) != 'h') {
613                 printf("Error: wrong head content!\n");
614                 return -1;
615         }
616         if (cirbuf_del_head_safe(&cb) < 0) {
617                 printf("Error: deleting from cirbuf head failed!\n");
618                 return -1;
619         }
620         if (cirbuf_add_tail_safe(&cb,'t') < 0) {
621                 printf("Error: adding to cirbuf tail failed!\n");
622                 return -1;
623         }
624         if (cirbuf_get_tail(&cb) != 't') {
625                 printf("Error: wrong tail content!\n");
626                 return -1;
627         }
628         if (cirbuf_del_tail_safe(&cb) < 0) {
629                 printf("Error: deleting from cirbuf tail failed!\n");
630                 return -1;
631         }
632         /* do the same for unsafe versions. those are void. */
633         cirbuf_add_head(&cb,'h');
634         if (cirbuf_get_head(&cb) != 'h') {
635                 printf("Error: wrong head content!\n");
636                 return -1;
637         }
638         cirbuf_del_head(&cb);
639
640         /* test if char has been deleted. we can't call cirbuf_get_head
641          * because it's unsafe, but we can call cirbuf_get_buf_head.
642          */
643         if (cirbuf_get_buf_head(&cb, tmp, 1) > 0) {
644                 printf("Error: buffer should have been empty!\n");
645                 return -1;
646         }
647
648         cirbuf_add_tail(&cb,'t');
649         if (cirbuf_get_tail(&cb) != 't') {
650                 printf("Error: wrong tail content!\n");
651                 return -1;
652         }
653         cirbuf_del_tail(&cb);
654
655         /* test if char has been deleted. we can't call cirbuf_get_tail
656          * because it's unsafe, but we can call cirbuf_get_buf_tail.
657          */
658         if (cirbuf_get_buf_tail(&cb, tmp, 1) > 0) {
659                 printf("Error: buffer should have been empty!\n");
660                 return -1;
661         }
662
663         return 0;
664 }
665
666 /* test filling up buffer with chars */
667 static int
668 test_cirbuf_char_fill(void)
669 {
670         struct cirbuf cb;
671         char buf[CMDLINE_TEST_BUFSIZE];
672         unsigned i;
673
674         /* clear buffer */
675         memset(buf, 0, sizeof(buf));
676
677         /*
678          * initialize circular buffer
679          */
680         if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
681                 printf("Error: failed to initialize circular buffer!\n");
682                 return -1;
683         }
684
685         /*
686          * fill the buffer from head or tail, verify contents, test boundaries
687          * and clear the buffer
688          */
689
690         /* fill the buffer from tail */
691         for (i = 0; i < CMDLINE_TEST_BUFSIZE; i++)
692                 cirbuf_add_tail_safe(&cb, 't');
693         /* verify that contents of the buffer are what they are supposed to be */
694         for (i = 0; i < sizeof(buf); i++) {
695                 if (buf[i] != 't') {
696                         printf("Error: wrong content in buffer!\n");
697                         return -1;
698                 }
699         }
700         /* try to add to a full buffer from tail */
701         if (cirbuf_add_tail_safe(&cb, 't') == 0) {
702                 printf("Error: buffer should have been full!\n");
703                 return -1;
704         }
705         /* try to add to a full buffer from head */
706         if (cirbuf_add_head_safe(&cb, 'h') == 0) {
707                 printf("Error: buffer should have been full!\n");
708                 return -1;
709         }
710         /* delete buffer from tail */
711         for(i = 0; i < CMDLINE_TEST_BUFSIZE; i++)
712                 cirbuf_del_tail_safe(&cb);
713         /* try to delete from an empty buffer */
714         if (cirbuf_del_tail_safe(&cb) >= 0) {
715                 printf("Error: buffer should have been empty!\n");
716                 return -1;
717         }
718
719         /* fill the buffer from head */
720         for (i = 0; i < CMDLINE_TEST_BUFSIZE; i++)
721                 cirbuf_add_head_safe(&cb, 'h');
722         /* verify that contents of the buffer are what they are supposed to be */
723         for (i = 0; i < sizeof(buf); i++) {
724                 if (buf[i] != 'h') {
725                         printf("Error: wrong content in buffer!\n");
726                         return -1;
727                 }
728         }
729         /* try to add to a full buffer from head */
730         if (cirbuf_add_head_safe(&cb,'h') >= 0) {
731                 printf("Error: buffer should have been full!\n");
732                 return -1;
733         }
734         /* try to add to a full buffer from tail */
735         if (cirbuf_add_tail_safe(&cb, 't') == 0) {
736                 printf("Error: buffer should have been full!\n");
737                 return -1;
738         }
739         /* delete buffer from head */
740         for(i = 0; i < CMDLINE_TEST_BUFSIZE; i++)
741                 cirbuf_del_head_safe(&cb);
742         /* try to delete from an empty buffer */
743         if (cirbuf_del_head_safe(&cb) >= 0) {
744                 printf("Error: buffer should have been empty!\n");
745                 return -1;
746         }
747
748         /*
749          * fill the buffer from both head and tail, with alternating characters,
750          * verify contents and clear the buffer
751          */
752
753         /* fill half of buffer from tail */
754         for (i = 0; i < CMDLINE_TEST_BUFSIZE / 2; i++)
755                 cirbuf_add_tail_safe(&cb, (char) (i % 2 ? 't' : 'T'));
756         /* fill other half of the buffer from head */
757         for (i = 0; i < CMDLINE_TEST_BUFSIZE / 2; i++)
758                 cirbuf_add_head_safe(&cb, (char) (i % 2 ? 'H' : 'h')); /* added in reverse */
759
760         /* verify that contents of the buffer are what they are supposed to be */
761         for (i = 0; i < sizeof(buf) / 2; i++) {
762                 if (buf[i] != (char) (i % 2 ? 't' : 'T')) {
763                         printf("Error: wrong content in buffer at %u!\n", i);
764                         return -1;
765                 }
766         }
767         for (i = sizeof(buf) / 2; i < sizeof(buf); i++) {
768                 if (buf[i] != (char) (i % 2 ? 'h' : 'H')) {
769                         printf("Error: wrong content in buffer %u!\n", i);
770                         return -1;
771                 }
772         }
773
774         return 0;
775 }
776
777 /* test left alignment */
778 static int
779 test_cirbuf_align_left(void)
780 {
781 #define HALF_OFFSET CMDLINE_TEST_BUFSIZE / 2
782 #define SMALL_OFFSET HALF_OFFSET / 2
783 /* resulting buffer lengths for each of the test cases */
784 #define LEN1 HALF_OFFSET - SMALL_OFFSET - 1
785 #define LEN2 HALF_OFFSET + SMALL_OFFSET + 2
786 #define LEN3 HALF_OFFSET - SMALL_OFFSET
787 #define LEN4 HALF_OFFSET + SMALL_OFFSET - 1
788
789         struct cirbuf cb;
790         char buf[CMDLINE_TEST_BUFSIZE];
791         char tmp[CMDLINE_TEST_BUFSIZE];
792         unsigned i;
793
794         /*
795          * align left when start < end and start in left half
796          */
797
798         /*
799          * initialize circular buffer
800          */
801         memset(buf, 0, sizeof(buf));
802         if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
803                 printf("Error: failed to initialize circular buffer!\n");
804                 return -1;
805         }
806
807         /* push end into left half */
808         for (i = 0; i < HALF_OFFSET - 1; i++)
809                 cirbuf_add_tail_safe(&cb, 't');
810
811         /* push start into left half < end */
812         for (i = 0; i < SMALL_OFFSET; i++)
813                 cirbuf_del_head_safe(&cb);
814
815         /* align */
816         if (cirbuf_align_left(&cb) < 0) {
817                 printf("Error: alignment failed!\n");
818                 return -1;
819         }
820
821         /* verify result */
822         if (cb.start != 0 || cb.len != LEN1 || cb.end != cb.len - 1) {
823                 printf("Error: buffer alignment is wrong!\n");
824                 return -1;
825         }
826
827         /*
828          * align left when start > end and start in left half
829          */
830
831         /*
832          * reinitialize circular buffer
833          */
834         memset(buf, 0, sizeof(buf));
835         if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
836                 printf("Error: failed to reinitialize circular buffer!\n");
837                 return -1;
838         }
839
840         /* push start into left half */
841         for (i = 0; i < HALF_OFFSET + 2; i++)
842                 cirbuf_add_head_safe(&cb, 'h');
843
844         /* push end into left half > start */
845         for (i = 0; i < SMALL_OFFSET; i++)
846                 cirbuf_add_tail_safe(&cb, 't');
847
848         /* align */
849         if (cirbuf_align_left(&cb) < 0) {
850                 printf("Error: alignment failed!\n");
851                 return -1;
852         }
853
854         /* verify result */
855         if (cb.start != 0 || cb.len != LEN2 || cb.end != cb.len - 1) {
856                 printf("Error: buffer alignment is wrong!");
857                 return -1;
858         }
859
860         /*
861          * align left when start < end and start in right half
862          */
863
864         /*
865          * reinitialize circular buffer
866          */
867         memset(buf, 0, sizeof(buf));
868         if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
869                 printf("Error: failed to reinitialize circular buffer!\n");
870                 return -1;
871         }
872
873         /* push start into the right half */
874         for (i = 0; i < HALF_OFFSET; i++)
875                 cirbuf_add_head_safe(&cb, 'h');
876
877         /* push end into left half > start */
878         for (i = 0; i < SMALL_OFFSET; i++)
879                 cirbuf_del_tail_safe(&cb);
880
881         /* align */
882         if (cirbuf_align_left(&cb) < 0) {
883                 printf("Error: alignment failed!\n");
884                 return -1;
885         }
886
887         /* verify result */
888         if (cb.start != 0 || cb.len != LEN3 || cb.end != cb.len - 1) {
889                 printf("Error: buffer alignment is wrong!");
890                 return -1;
891         }
892
893         /*
894          * align left when start > end and start in right half
895          */
896
897         /*
898          * reinitialize circular buffer
899          */
900         memset(buf, 0, sizeof(buf));
901         if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
902                 printf("Error: failed to reinitialize circular buffer!\n");
903                 return -1;
904         }
905
906         /* push start into the right half */
907         for (i = 0; i < HALF_OFFSET - 1; i++)
908                 cirbuf_add_head_safe(&cb, 'h');
909
910         /* push end into left half < start */
911         for (i = 0; i < SMALL_OFFSET; i++)
912                 cirbuf_add_tail_safe(&cb, 't');
913
914         /* align */
915         if (cirbuf_align_left(&cb) < 0) {
916                 printf("Error: alignment failed!\n");
917                 return -1;
918         }
919
920         /* verify result */
921         if (cb.start != 0 || cb.len != LEN4 ||
922                         cb.end != cb.len - 1) {
923                 printf("Error: buffer alignment is wrong!");
924                 return -1;
925         }
926
927         /*
928          * Verify that alignment doesn't corrupt data
929          */
930
931         /*
932          * reinitialize circular buffer
933          */
934         memset(buf, 0, sizeof(buf));
935         if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
936                 printf("Error: failed to reinitialize circular buffer!\n");
937                 return -1;
938         }
939
940         /* add string to tail and head */
941         if (cirbuf_add_buf_head(&cb, CIRBUF_STR_HEAD,
942                         sizeof(CIRBUF_STR_HEAD)) < 0 || cirbuf_add_buf_tail(&cb,
943                                         CIRBUF_STR_TAIL, sizeof(CIRBUF_STR_TAIL)) < 0) {
944                 printf("Error: failed to add strings!\n");
945                 return -1;
946         }
947
948         /* align */
949         if (cirbuf_align_left(&cb) < 0) {
950                 printf("Error: alignment failed!\n");
951                 return -1;
952         }
953
954         /* get string from head */
955         if (cirbuf_get_buf_head(&cb, tmp,
956                         sizeof(CIRBUF_STR_HEAD) + sizeof(CIRBUF_STR_TAIL)) < 0) {
957                 printf("Error: failed to read string from head!\n");
958                 return -1;
959         }
960
961         /* verify string */
962         if (strncmp(tmp, CIRBUF_STR_HEAD "\0" CIRBUF_STR_TAIL,
963                         sizeof(CIRBUF_STR_HEAD) + sizeof(CIRBUF_STR_TAIL)) != 0) {
964                 printf("Error: strings mismatch!\n");
965                 return -1;
966         }
967
968         /* reset tmp buffer */
969         memset(tmp, 0, sizeof(tmp));
970
971         /* get string from tail */
972         if (cirbuf_get_buf_tail(&cb, tmp,
973                         sizeof(CIRBUF_STR_HEAD) + sizeof(CIRBUF_STR_TAIL)) < 0) {
974                 printf("Error: failed to read string from head!\n");
975                 return -1;
976         }
977
978         /* verify string */
979         if (strncmp(tmp, CIRBUF_STR_HEAD "\0" CIRBUF_STR_TAIL,
980                         sizeof(CIRBUF_STR_HEAD) + sizeof(CIRBUF_STR_TAIL)) != 0) {
981                 printf("Error: strings mismatch!\n");
982                 return -1;
983         }
984
985         return 0;
986 }
987
988 /* test right alignment */
989 static int
990 test_cirbuf_align_right(void)
991 {
992 #define END_OFFSET CMDLINE_TEST_BUFSIZE - 1
993         struct cirbuf cb;
994         char buf[CMDLINE_TEST_BUFSIZE];
995         char tmp[CMDLINE_TEST_BUFSIZE];
996         unsigned i;
997
998
999         /*
1000          * align right when start < end and start in left half
1001          */
1002
1003         /*
1004          * initialize circular buffer
1005          */
1006         memset(buf, 0, sizeof(buf));
1007         if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
1008                 printf("Error: failed to initialize circular buffer!\n");
1009                 return -1;
1010         }
1011
1012         /* push end into left half */
1013         for (i = 0; i < HALF_OFFSET - 1; i++)
1014                 cirbuf_add_tail_safe(&cb, 't');
1015
1016         /* push start into left half < end */
1017         for (i = 0; i < SMALL_OFFSET; i++)
1018                 cirbuf_del_head_safe(&cb);
1019
1020         /* align */
1021         cirbuf_align_right(&cb);
1022
1023         /* verify result */
1024         if (cb.start != END_OFFSET || cb.len != LEN1 || cb.end != cb.len - 2) {
1025                 printf("Error: buffer alignment is wrong!\n");
1026                 return -1;
1027         }
1028
1029         /*
1030          * align right when start > end and start in left half
1031          */
1032
1033         /*
1034          * reinitialize circular buffer
1035          */
1036         memset(buf, 0, sizeof(buf));
1037         if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
1038                 printf("Error: failed to reinitialize circular buffer!\n");
1039                 return -1;
1040         }
1041
1042         /* push start into left half */
1043         for (i = 0; i < HALF_OFFSET + 2; i++)
1044                 cirbuf_add_head_safe(&cb, 'h');
1045
1046         /* push end into left half > start */
1047         for (i = 0; i < SMALL_OFFSET; i++)
1048                 cirbuf_add_tail_safe(&cb, 't');
1049
1050         /* align */
1051         cirbuf_align_right(&cb);
1052
1053         /* verify result */
1054         if (cb.start != END_OFFSET || cb.len != LEN2 || cb.end != cb.len - 2) {
1055                 printf("Error: buffer alignment is wrong!");
1056                 return -1;
1057         }
1058
1059         /*
1060          * align right when start < end and start in right half
1061          */
1062
1063         /*
1064          * reinitialize circular buffer
1065          */
1066         memset(buf, 0, sizeof(buf));
1067         if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
1068                 printf("Error: failed to reinitialize circular buffer!\n");
1069                 return -1;
1070         }
1071
1072         /* push start into the right half */
1073         for (i = 0; i < HALF_OFFSET; i++)
1074                 cirbuf_add_head_safe(&cb, 'h');
1075
1076         /* push end into left half > start */
1077         for (i = 0; i < SMALL_OFFSET; i++)
1078                 cirbuf_del_tail_safe(&cb);
1079
1080         /* align */
1081         cirbuf_align_right(&cb);
1082
1083         /* verify result */
1084         if (cb.end != END_OFFSET || cb.len != LEN3 || cb.start != cb.end - cb.len + 1) {
1085                 printf("Error: buffer alignment is wrong!");
1086                 return -1;
1087         }
1088
1089         /*
1090          * align right when start > end and start in right half
1091          */
1092
1093         /*
1094          * reinitialize circular buffer
1095          */
1096         memset(buf, 0, sizeof(buf));
1097         if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
1098                 printf("Error: failed to reinitialize circular buffer!\n");
1099                 return -1;
1100         }
1101
1102         /* push start into the right half */
1103         for (i = 0; i < HALF_OFFSET - 1; i++)
1104                 cirbuf_add_head_safe(&cb, 'h');
1105
1106         /* push end into left half < start */
1107         for (i = 0; i < SMALL_OFFSET; i++)
1108                 cirbuf_add_tail_safe(&cb, 't');
1109
1110         /* align */
1111         cirbuf_align_right(&cb);
1112
1113         /* verify result */
1114         if (cb.end != END_OFFSET || cb.len != LEN4 || cb.start != cb.end - cb.len + 1) {
1115                 printf("Error: buffer alignment is wrong!");
1116                 return -1;
1117         }
1118
1119         /*
1120          * Verify that alignment doesn't corrupt data
1121          */
1122
1123         /*
1124          * reinitialize circular buffer
1125          */
1126         memset(buf, 0, sizeof(buf));
1127         if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
1128                 printf("Error: failed to reinitialize circular buffer!\n");
1129                 return -1;
1130         }
1131
1132         /* add string to tail and head */
1133         if (cirbuf_add_buf_tail(&cb, CIRBUF_STR_TAIL,
1134                         sizeof(CIRBUF_STR_TAIL)) < 0 || cirbuf_add_buf_head(&cb,
1135                                         CIRBUF_STR_HEAD, sizeof(CIRBUF_STR_HEAD)) < 0) {
1136                 printf("Error: failed to add strings!\n");
1137                 return -1;
1138         }
1139
1140         /* align */
1141         if (cirbuf_align_right(&cb) < 0) {
1142                 printf("Error: alignment failed!\n");
1143                 return -1;
1144         }
1145
1146         /* get string from head */
1147         if (cirbuf_get_buf_head(&cb, tmp,
1148                         sizeof(CIRBUF_STR_HEAD) + sizeof(CIRBUF_STR_TAIL)) < 0) {
1149                 printf("Error: failed to read string from head!\n");
1150                 return -1;
1151         }
1152
1153         /* verify string */
1154         if (strncmp(tmp, CIRBUF_STR_HEAD "\0" CIRBUF_STR_TAIL,
1155                         sizeof(CIRBUF_STR_HEAD) + sizeof(CIRBUF_STR_TAIL)) != 0) {
1156                 printf("Error: strings mismatch!\n");
1157                 return -1;
1158         }
1159
1160         /* reset tmp buffer */
1161         memset(tmp, 0, sizeof(tmp));
1162
1163         /* get string from tail */
1164         if (cirbuf_get_buf_tail(&cb, tmp,
1165                         sizeof(CIRBUF_STR_HEAD) + sizeof(CIRBUF_STR_TAIL)) < 0) {
1166                 printf("Error: failed to read string from head!\n");
1167                 return -1;
1168         }
1169         /* verify string */
1170         if (strncmp(tmp, CIRBUF_STR_HEAD "\0" CIRBUF_STR_TAIL,
1171                         sizeof(CIRBUF_STR_HEAD) + sizeof(CIRBUF_STR_TAIL)) != 0) {
1172                 printf("Error: strings mismatch!\n");
1173                 return -1;
1174         }
1175
1176         return 0;
1177 }
1178
1179 /* call functions with invalid parameters */
1180 int
1181 test_cirbuf_invalid_param(void)
1182 {
1183         struct cirbuf cb;
1184         char buf[CMDLINE_TEST_BUFSIZE];
1185
1186         /* null cirbuf */
1187         if (cirbuf_init(0, buf, 0, sizeof(buf)) == 0)
1188                 return -1;
1189         /* null buffer */
1190         if (cirbuf_init(&cb, 0, 0, sizeof(buf)) == 0)
1191                 return -1;
1192         /* null cirbuf */
1193         if (cirbuf_add_head_safe(0, 'h') == 0)
1194                 return -1;
1195         if (cirbuf_add_tail_safe(0, 't') == 0)
1196                 return -1;
1197         if (cirbuf_del_head_safe(0) == 0)
1198                 return -1;
1199         if (cirbuf_del_tail_safe(0) == 0)
1200                 return -1;
1201         /* null buffer */
1202         if (cirbuf_add_buf_head(&cb, 0, 0) == 0)
1203                 return -1;
1204         if (cirbuf_add_buf_tail(&cb, 0, 0) == 0)
1205                 return -1;
1206         /* null cirbuf */
1207         if (cirbuf_add_buf_head(0, buf, 0) == 0)
1208                 return -1;
1209         if (cirbuf_add_buf_tail(0, buf, 0) == 0)
1210                 return -1;
1211         /* null size */
1212         if (cirbuf_add_buf_head(&cb, buf, 0) == 0)
1213                 return -1;
1214         if (cirbuf_add_buf_tail(&cb, buf, 0) == 0)
1215                 return -1;
1216         /* null cirbuf */
1217         if (cirbuf_del_buf_head(0, 0) == 0)
1218                 return -1;
1219         if (cirbuf_del_buf_tail(0, 0) == 0)
1220                 return -1;
1221         /* null size */
1222         if (cirbuf_del_buf_head(&cb, 0) == 0)
1223                 return -1;
1224         if (cirbuf_del_buf_tail(&cb, 0) == 0)
1225                 return -1;
1226         /* null cirbuf */
1227         if (cirbuf_get_buf_head(0, 0, 0) == 0)
1228                 return -1;
1229         if (cirbuf_get_buf_tail(0, 0, 0) == 0)
1230                 return -1;
1231         /* null buffer */
1232         if (cirbuf_get_buf_head(&cb, 0, 0) == 0)
1233                 return -1;
1234         if (cirbuf_get_buf_tail(&cb, 0, 0) == 0)
1235                 return -1;
1236         /* null size, this is valid but should return 0 */
1237         if (cirbuf_get_buf_head(&cb, buf, 0) != 0)
1238                 return -1;
1239         if (cirbuf_get_buf_tail(&cb, buf, 0) != 0)
1240                 return -1;
1241         /* null cirbuf */
1242         if (cirbuf_align_left(0) == 0)
1243                 return -1;
1244         if (cirbuf_align_right(0) == 0)
1245                 return -1;
1246
1247         return 0;
1248 }
1249
1250 /* test cmdline_cirbuf char functions */
1251 int
1252 test_cirbuf_char(void)
1253 {
1254         int ret;
1255
1256         ret = test_cirbuf_char_add_del();
1257         if (ret < 0)
1258                 return -1;
1259
1260         ret = test_cirbuf_char_fill();
1261         if (ret < 0)
1262                 return -1;
1263
1264         return 0;
1265 }
1266
1267 /* test cmdline_cirbuf string functions */
1268 int
1269 test_cirbuf_string(void)
1270 {
1271         if (test_cirbuf_string_add_del() < 0)
1272                 return -1;
1273
1274         if (test_cirbuf_string_add_del_reverse() < 0)
1275                 return -1;
1276
1277         if (test_cirbuf_string_add_boundaries() < 0)
1278                 return -1;
1279
1280         if (test_cirbuf_string_get_del_boundaries() < 0)
1281                 return -1;
1282
1283         if (test_cirbuf_string_get_del_partial() < 0)
1284                 return -1;
1285
1286         if (test_cirbuf_string_misc() < 0)
1287                 return -1;
1288
1289         return 0;
1290 }
1291
1292 /* test cmdline_cirbuf align functions */
1293 int
1294 test_cirbuf_align(void)
1295 {
1296         if (test_cirbuf_align_left() < 0)
1297                 return -1;
1298         if (test_cirbuf_align_right() < 0)
1299                 return -1;
1300         return 0;
1301 }