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