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 : : 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 [ - + ]: 1 : if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
264 : : printf("Error: failed to initialize circular buffer!\n");
265 : 0 : return -1;
266 : : }
267 : :
268 : : /* add string to head */
269 [ - + ]: 1 : 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 : 0 : return -1;
273 : : }
274 : : /* delete string from tail */
275 [ - + ]: 1 : if (cirbuf_del_buf_tail(&cb, sizeof(CIRBUF_STR_HEAD)) < 0) {
276 : : printf("Error: failed to delete string from tail!\n");
277 : 0 : return -1;
278 : : }
279 : : /* verify string was deleted */
280 [ - + ]: 1 : if (cirbuf_del_tail_safe(&cb) == 0) {
281 : : printf("Error: buffer should have been empty!\n");
282 : 0 : 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 [ - + ]: 1 : if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
292 : : printf("Error: failed to reinitialize circular buffer!\n");
293 : 0 : return -1;
294 : : }
295 : :
296 : : /* add string to tail */
297 [ - + ]: 1 : 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 : 0 : return -1;
301 : : }
302 : : /* delete string from head */
303 [ - + ]: 1 : if (cirbuf_del_buf_head(&cb, sizeof(CIRBUF_STR_TAIL)) < 0) {
304 : : printf("Error: failed to delete string from head!\n");
305 : 0 : return -1;
306 : : }
307 : : /* verify string was deleted */
308 [ - + ]: 1 : if (cirbuf_del_head_safe(&cb) == 0) {
309 : : printf("Error: buffer should have been empty!\n");
310 : 0 : return -1;
311 : : }
312 : :
313 : : return 0;
314 : : }
315 : :
316 : : /* try to write more than available */
317 : : static int
318 : 1 : 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 [ - + ]: 1 : if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
331 : : printf("Error: failed to initialize circular buffer!\n");
332 : 0 : return -1;
333 : : }
334 : :
335 : : /* fill the buffer from tail */
336 [ + + ]: 61 : for (i = 0; i < CMDLINE_TEST_BUFSIZE - sizeof(CIRBUF_STR_TAIL) + 1; i++)
337 : 60 : cirbuf_add_tail_safe(&cb, 't');
338 : :
339 : : /* try adding a string to tail */
340 [ - + ]: 1 : 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 : 0 : return -1;
344 : : }
345 : : /* try adding a string to head */
346 [ - + ]: 1 : 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 : 0 : return -1;
350 : : }
351 : :
352 : : /*
353 : : * reinitialize circular buffer
354 : : */
355 : : memset(buf, 0, sizeof(buf));
356 [ - + ]: 1 : if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
357 : : printf("Error: failed to reinitialize circular buffer!\n");
358 : 0 : return -1;
359 : : }
360 : :
361 : : /* fill the buffer from head */
362 [ + + ]: 60 : for (i = 0; i < CMDLINE_TEST_BUFSIZE - sizeof(CIRBUF_STR_HEAD) + 1; i++)
363 : 59 : cirbuf_add_head_safe(&cb, 'h');
364 : :
365 : : /* try adding a string to head */
366 [ - + ]: 1 : 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 : 0 : return -1;
370 : : }
371 : : /* try adding a string to tail */
372 [ - + ]: 1 : 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 : 0 : return -1;
376 : : }
377 : :
378 : : return 0;
379 : : }
380 : :
381 : : /* try to read/delete more than written */
382 : : static int
383 : 1 : 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 [ - + ]: 1 : if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
397 : : printf("Error: failed to initialize circular buffer!\n");
398 : 0 : return -1;
399 : : }
400 : :
401 : :
402 : : /* add string to head */
403 [ - + ]: 1 : 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 : 0 : return -1;
407 : : }
408 : : /* read more than written (head) */
409 [ - + ]: 1 : 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 : 0 : return -1;
413 : : }
414 : : /* read more than written (tail) */
415 [ - + ]: 1 : 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 : 0 : return -1;
419 : : }
420 : : /* delete more than written (head) */
421 [ - + ]: 1 : if (cirbuf_del_buf_head(&cb, sizeof(CIRBUF_STR_HEAD) + 1) == 0) {
422 : : printf("Error: unexpected result when deleting too much data!\n");
423 : 0 : return -1;
424 : : }
425 : : /* delete more than written (tail) */
426 [ - + ]: 1 : if (cirbuf_del_buf_tail(&cb, sizeof(CIRBUF_STR_HEAD) + 1) == 0) {
427 : : printf("Error: unexpected result when deleting too much data!\n");
428 : 0 : return -1;
429 : : }
430 : :
431 : : /*
432 : : * reinitialize circular buffer
433 : : */
434 : : memset(buf, 0, sizeof(buf));
435 [ - + ]: 1 : if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
436 : : printf("Error: failed to reinitialize circular buffer!\n");
437 : 0 : return -1;
438 : : }
439 : :
440 : : /* add string to tail */
441 [ - + ]: 1 : 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 : 0 : return -1;
445 : : }
446 : : /* read more than written (tail) */
447 [ - + ]: 1 : 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 : 0 : return -1;
451 : : }
452 : : /* read more than written (head) */
453 [ - + ]: 1 : 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 : 0 : return -1;
457 : : }
458 : : /* delete more than written (tail) */
459 [ - + ]: 1 : if (cirbuf_del_buf_tail(&cb, sizeof(CIRBUF_STR_TAIL) + 1) == 0) {
460 : : printf("Error: unexpected result when deleting too much data!\n");
461 : 0 : return -1;
462 : : }
463 : : /* delete more than written (head) */
464 [ - + ]: 1 : if (cirbuf_del_buf_tail(&cb, sizeof(CIRBUF_STR_TAIL) + 1) == 0) {
465 : : printf("Error: unexpected result when deleting too much data!\n");
466 : 0 : return -1;
467 : : }
468 : :
469 : : return 0;
470 : : }
471 : :
472 : : /* try to read/delete less than written */
473 : : static int
474 : 1 : 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 : : strlcpy(tmp2, CIRBUF_STR_HEAD, sizeof(tmp2));
487 : :
488 : : /*
489 : : * initialize circular buffer
490 : : */
491 [ - + ]: 1 : if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
492 : : printf("Error: failed to initialize circular buffer!\n");
493 : 0 : return -1;
494 : : }
495 : :
496 : : /* add string to head */
497 [ - + ]: 1 : 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 : 0 : return -1;
501 : : }
502 : : /* read less than written (head) */
503 [ - + ]: 1 : 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 : 0 : return -1;
507 : : }
508 : : /* verify string */
509 [ - + ]: 1 : if (strncmp(tmp, tmp2, sizeof(CIRBUF_STR_HEAD) - 1) != 0) {
510 : : printf("Error: strings mismatch!\n");
511 : 0 : return -1;
512 : : }
513 : : memset(tmp, 0, sizeof(tmp));
514 : : /* read less than written (tail) */
515 [ - + ]: 1 : 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 : 0 : return -1;
519 : : }
520 : : /* verify string */
521 [ - + ]: 1 : if (strncmp(tmp, &tmp2[1], sizeof(CIRBUF_STR_HEAD) - 1) != 0) {
522 : : printf("Error: strings mismatch!\n");
523 : 0 : 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 [ - + ]: 1 : if (cirbuf_del_buf_head(&cb, 1) != 0) {
535 : : printf("Error: delete from head failed!\n");
536 : 0 : return -1;
537 : : }
538 : : /* read from head */
539 [ - + ]: 1 : 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 : 0 : return -1;
543 : : }
544 : : /* since we deleted from head, first char should be deleted */
545 [ - + ]: 1 : if (strncmp(tmp, &tmp2[1], sizeof(CIRBUF_STR_HEAD) - 1) != 0) {
546 : : printf("Error: strings mismatch!\n");
547 : 0 : return -1;
548 : : }
549 : : /* clear buffer */
550 : : memset(tmp, 0, sizeof(tmp));
551 : :
552 : : /* delete less than written (tail) */
553 [ - + ]: 1 : if (cirbuf_del_buf_tail(&cb, 1) != 0) {
554 : : printf("Error: delete from tail failed!\n");
555 : 0 : return -1;
556 : : }
557 : : /* read from tail */
558 [ - + ]: 1 : 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 : 0 : return -1;
562 : : }
563 : : /* since we deleted from tail, last char should be deleted */
564 [ - + ]: 1 : if (strncmp(tmp, &tmp2[1], sizeof(CIRBUF_STR_HEAD) - 2) != 0) {
565 : : printf("Error: strings mismatch!\n");
566 : 0 : return -1;
567 : : }
568 : :
569 : : return 0;
570 : : }
571 : :
572 : : /* test cmdline_cirbuf char add/del functions */
573 : : static int
574 : 1 : 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 [ - + ]: 1 : if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
588 : : printf("Error: failed to initialize circular buffer!\n");
589 : 0 : return -1;
590 : : }
591 : :
592 : : /*
593 : : * try to delete something from cirbuf. since it's empty,
594 : : * these should fail.
595 : : */
596 [ - + ]: 1 : if (cirbuf_del_head_safe(&cb) == 0) {
597 : : printf("Error: deleting from empty cirbuf head succeeded!\n");
598 : 0 : return -1;
599 : : }
600 [ - + ]: 1 : if (cirbuf_del_tail_safe(&cb) == 0) {
601 : : printf("Error: deleting from empty cirbuf tail succeeded!\n");
602 : 0 : return -1;
603 : : }
604 : :
605 : : /*
606 : : * add, verify and delete. these should pass.
607 : : */
608 [ - + ]: 1 : if (cirbuf_add_head_safe(&cb,'h') < 0) {
609 : : printf("Error: adding to cirbuf head failed!\n");
610 : 0 : return -1;
611 : : }
612 [ - + ]: 1 : if (cirbuf_get_head(&cb) != 'h') {
613 : : printf("Error: wrong head content!\n");
614 : 0 : return -1;
615 : : }
616 [ - + ]: 1 : if (cirbuf_del_head_safe(&cb) < 0) {
617 : : printf("Error: deleting from cirbuf head failed!\n");
618 : 0 : return -1;
619 : : }
620 [ - + ]: 1 : if (cirbuf_add_tail_safe(&cb,'t') < 0) {
621 : : printf("Error: adding to cirbuf tail failed!\n");
622 : 0 : return -1;
623 : : }
624 [ - + ]: 1 : if (cirbuf_get_tail(&cb) != 't') {
625 : : printf("Error: wrong tail content!\n");
626 : 0 : return -1;
627 : : }
628 [ - + ]: 1 : if (cirbuf_del_tail_safe(&cb) < 0) {
629 : : printf("Error: deleting from cirbuf tail failed!\n");
630 : 0 : return -1;
631 : : }
632 : : /* do the same for unsafe versions. those are void. */
633 : 1 : cirbuf_add_head(&cb,'h');
634 [ - + ]: 1 : if (cirbuf_get_head(&cb) != 'h') {
635 : : printf("Error: wrong head content!\n");
636 : 0 : return -1;
637 : : }
638 : 1 : 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 [ - + ]: 1 : if (cirbuf_get_buf_head(&cb, tmp, 1) > 0) {
644 : : printf("Error: buffer should have been empty!\n");
645 : 0 : return -1;
646 : : }
647 : :
648 : 1 : cirbuf_add_tail(&cb,'t');
649 [ - + ]: 1 : if (cirbuf_get_tail(&cb) != 't') {
650 : : printf("Error: wrong tail content!\n");
651 : 0 : return -1;
652 : : }
653 : 1 : 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 [ - + ]: 1 : if (cirbuf_get_buf_tail(&cb, tmp, 1) > 0) {
659 : : printf("Error: buffer should have been empty!\n");
660 : 0 : return -1;
661 : : }
662 : :
663 : : return 0;
664 : : }
665 : :
666 : : /* test filling up buffer with chars */
667 : : static int
668 : 1 : 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 [ - + ]: 1 : if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
681 : : printf("Error: failed to initialize circular buffer!\n");
682 : 0 : 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 [ + + ]: 65 : for (i = 0; i < CMDLINE_TEST_BUFSIZE; i++)
692 : 64 : cirbuf_add_tail_safe(&cb, 't');
693 : : /* verify that contents of the buffer are what they are supposed to be */
694 [ + + ]: 65 : for (i = 0; i < sizeof(buf); i++) {
695 [ - + ]: 64 : if (buf[i] != 't') {
696 : : printf("Error: wrong content in buffer!\n");
697 : 0 : return -1;
698 : : }
699 : : }
700 : : /* try to add to a full buffer from tail */
701 [ - + ]: 1 : if (cirbuf_add_tail_safe(&cb, 't') == 0) {
702 : : printf("Error: buffer should have been full!\n");
703 : 0 : return -1;
704 : : }
705 : : /* try to add to a full buffer from head */
706 [ - + ]: 1 : if (cirbuf_add_head_safe(&cb, 'h') == 0) {
707 : : printf("Error: buffer should have been full!\n");
708 : 0 : return -1;
709 : : }
710 : : /* delete buffer from tail */
711 [ + + ]: 65 : for(i = 0; i < CMDLINE_TEST_BUFSIZE; i++)
712 : 64 : cirbuf_del_tail_safe(&cb);
713 : : /* try to delete from an empty buffer */
714 [ - + ]: 1 : if (cirbuf_del_tail_safe(&cb) >= 0) {
715 : : printf("Error: buffer should have been empty!\n");
716 : 0 : return -1;
717 : : }
718 : :
719 : : /* fill the buffer from head */
720 [ + + ]: 65 : for (i = 0; i < CMDLINE_TEST_BUFSIZE; i++)
721 : 64 : cirbuf_add_head_safe(&cb, 'h');
722 : : /* verify that contents of the buffer are what they are supposed to be */
723 [ + + ]: 65 : for (i = 0; i < sizeof(buf); i++) {
724 [ - + ]: 64 : if (buf[i] != 'h') {
725 : : printf("Error: wrong content in buffer!\n");
726 : 0 : return -1;
727 : : }
728 : : }
729 : : /* try to add to a full buffer from head */
730 [ - + ]: 1 : if (cirbuf_add_head_safe(&cb,'h') >= 0) {
731 : : printf("Error: buffer should have been full!\n");
732 : 0 : return -1;
733 : : }
734 : : /* try to add to a full buffer from tail */
735 [ - + ]: 1 : if (cirbuf_add_tail_safe(&cb, 't') == 0) {
736 : : printf("Error: buffer should have been full!\n");
737 : 0 : return -1;
738 : : }
739 : : /* delete buffer from head */
740 [ + + ]: 65 : for(i = 0; i < CMDLINE_TEST_BUFSIZE; i++)
741 : 64 : cirbuf_del_head_safe(&cb);
742 : : /* try to delete from an empty buffer */
743 [ - + ]: 1 : if (cirbuf_del_head_safe(&cb) >= 0) {
744 : : printf("Error: buffer should have been empty!\n");
745 : 0 : 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 [ + + ]: 33 : for (i = 0; i < CMDLINE_TEST_BUFSIZE / 2; i++)
755 [ + + ]: 48 : cirbuf_add_tail_safe(&cb, (char) (i % 2 ? 't' : 'T'));
756 : : /* fill other half of the buffer from head */
757 [ + + ]: 33 : for (i = 0; i < CMDLINE_TEST_BUFSIZE / 2; i++)
758 [ + + ]: 48 : 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 [ + + ]: 33 : for (i = 0; i < sizeof(buf) / 2; i++) {
762 [ + + - + ]: 48 : if (buf[i] != (char) (i % 2 ? 't' : 'T')) {
763 : : printf("Error: wrong content in buffer at %u!\n", i);
764 : 0 : return -1;
765 : : }
766 : : }
767 [ + + ]: 33 : for (i = sizeof(buf) / 2; i < sizeof(buf); i++) {
768 [ + + - + ]: 48 : if (buf[i] != (char) (i % 2 ? 'h' : 'H')) {
769 : : printf("Error: wrong content in buffer %u!\n", i);
770 : 0 : return -1;
771 : : }
772 : : }
773 : :
774 : : return 0;
775 : : }
776 : :
777 : : /* test left alignment */
778 : : static int
779 : 1 : 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 [ - + ]: 1 : if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
803 : : printf("Error: failed to initialize circular buffer!\n");
804 : 0 : return -1;
805 : : }
806 : :
807 : : /* push end into left half */
808 [ + + ]: 32 : for (i = 0; i < HALF_OFFSET - 1; i++)
809 : 31 : cirbuf_add_tail_safe(&cb, 't');
810 : :
811 : : /* push start into left half < end */
812 [ + + ]: 17 : for (i = 0; i < SMALL_OFFSET; i++)
813 : 16 : cirbuf_del_head_safe(&cb);
814 : :
815 : : /* align */
816 [ - + ]: 1 : if (cirbuf_align_left(&cb) < 0) {
817 : : printf("Error: alignment failed!\n");
818 : 0 : return -1;
819 : : }
820 : :
821 : : /* verify result */
822 [ + - + - : 1 : if (cb.start != 0 || cb.len != LEN1 || cb.end != cb.len - 1) {
- + ]
823 : : printf("Error: buffer alignment is wrong!\n");
824 : 0 : 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 [ - + ]: 1 : if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
836 : : printf("Error: failed to reinitialize circular buffer!\n");
837 : 0 : return -1;
838 : : }
839 : :
840 : : /* push start into left half */
841 [ + + ]: 35 : for (i = 0; i < HALF_OFFSET + 2; i++)
842 : 34 : cirbuf_add_head_safe(&cb, 'h');
843 : :
844 : : /* push end into left half > start */
845 [ + + ]: 17 : for (i = 0; i < SMALL_OFFSET; i++)
846 : 16 : cirbuf_add_tail_safe(&cb, 't');
847 : :
848 : : /* align */
849 [ - + ]: 1 : if (cirbuf_align_left(&cb) < 0) {
850 : : printf("Error: alignment failed!\n");
851 : 0 : return -1;
852 : : }
853 : :
854 : : /* verify result */
855 [ + - + - : 1 : if (cb.start != 0 || cb.len != LEN2 || cb.end != cb.len - 1) {
- + ]
856 : : printf("Error: buffer alignment is wrong!");
857 : 0 : 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 [ - + ]: 1 : if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
869 : : printf("Error: failed to reinitialize circular buffer!\n");
870 : 0 : return -1;
871 : : }
872 : :
873 : : /* push start into the right half */
874 [ + + ]: 33 : for (i = 0; i < HALF_OFFSET; i++)
875 : 32 : cirbuf_add_head_safe(&cb, 'h');
876 : :
877 : : /* push end into left half > start */
878 [ + + ]: 17 : for (i = 0; i < SMALL_OFFSET; i++)
879 : 16 : cirbuf_del_tail_safe(&cb);
880 : :
881 : : /* align */
882 [ - + ]: 1 : if (cirbuf_align_left(&cb) < 0) {
883 : : printf("Error: alignment failed!\n");
884 : 0 : return -1;
885 : : }
886 : :
887 : : /* verify result */
888 [ + - + - : 1 : if (cb.start != 0 || cb.len != LEN3 || cb.end != cb.len - 1) {
- + ]
889 : : printf("Error: buffer alignment is wrong!");
890 : 0 : 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 [ - + ]: 1 : if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
902 : : printf("Error: failed to reinitialize circular buffer!\n");
903 : 0 : return -1;
904 : : }
905 : :
906 : : /* push start into the right half */
907 [ + + ]: 32 : for (i = 0; i < HALF_OFFSET - 1; i++)
908 : 31 : cirbuf_add_head_safe(&cb, 'h');
909 : :
910 : : /* push end into left half < start */
911 [ + + ]: 17 : for (i = 0; i < SMALL_OFFSET; i++)
912 : 16 : cirbuf_add_tail_safe(&cb, 't');
913 : :
914 : : /* align */
915 [ - + ]: 1 : if (cirbuf_align_left(&cb) < 0) {
916 : : printf("Error: alignment failed!\n");
917 : 0 : return -1;
918 : : }
919 : :
920 : : /* verify result */
921 [ + - + - ]: 1 : if (cb.start != 0 || cb.len != LEN4 ||
922 [ - + ]: 1 : cb.end != cb.len - 1) {
923 : : printf("Error: buffer alignment is wrong!");
924 : 0 : 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 [ - + ]: 1 : if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
936 : : printf("Error: failed to reinitialize circular buffer!\n");
937 : 0 : return -1;
938 : : }
939 : :
940 : : /* add string to tail and head */
941 [ + - ]: 1 : if (cirbuf_add_buf_head(&cb, CIRBUF_STR_HEAD,
942 [ - + ]: 1 : 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 : 0 : return -1;
946 : : }
947 : :
948 : : /* align */
949 [ - + ]: 1 : if (cirbuf_align_left(&cb) < 0) {
950 : : printf("Error: alignment failed!\n");
951 : 0 : return -1;
952 : : }
953 : :
954 : : /* get string from head */
955 [ - + ]: 1 : 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 : 0 : return -1;
959 : : }
960 : :
961 : : /* verify string */
962 [ - + ]: 1 : 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 : 0 : return -1;
966 : : }
967 : :
968 : : /* reset tmp buffer */
969 : : memset(tmp, 0, sizeof(tmp));
970 : :
971 : : /* get string from tail */
972 [ - + ]: 1 : 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 : 0 : return -1;
976 : : }
977 : :
978 : : /* verify string */
979 [ - + ]: 1 : 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 : 0 : return -1;
983 : : }
984 : :
985 : : return 0;
986 : : }
987 : :
988 : : /* test right alignment */
989 : : static int
990 : 1 : 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 [ - + ]: 1 : if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
1008 : : printf("Error: failed to initialize circular buffer!\n");
1009 : 0 : return -1;
1010 : : }
1011 : :
1012 : : /* push end into left half */
1013 [ + + ]: 32 : for (i = 0; i < HALF_OFFSET - 1; i++)
1014 : 31 : cirbuf_add_tail_safe(&cb, 't');
1015 : :
1016 : : /* push start into left half < end */
1017 [ + + ]: 17 : for (i = 0; i < SMALL_OFFSET; i++)
1018 : 16 : cirbuf_del_head_safe(&cb);
1019 : :
1020 : : /* align */
1021 : 1 : cirbuf_align_right(&cb);
1022 : :
1023 : : /* verify result */
1024 [ + - + - : 1 : if (cb.start != END_OFFSET || cb.len != LEN1 || cb.end != cb.len - 2) {
- + ]
1025 : : printf("Error: buffer alignment is wrong!\n");
1026 : 0 : 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 [ - + ]: 1 : if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
1038 : : printf("Error: failed to reinitialize circular buffer!\n");
1039 : 0 : return -1;
1040 : : }
1041 : :
1042 : : /* push start into left half */
1043 [ + + ]: 35 : for (i = 0; i < HALF_OFFSET + 2; i++)
1044 : 34 : cirbuf_add_head_safe(&cb, 'h');
1045 : :
1046 : : /* push end into left half > start */
1047 [ + + ]: 17 : for (i = 0; i < SMALL_OFFSET; i++)
1048 : 16 : cirbuf_add_tail_safe(&cb, 't');
1049 : :
1050 : : /* align */
1051 : 1 : cirbuf_align_right(&cb);
1052 : :
1053 : : /* verify result */
1054 [ + - + - : 1 : if (cb.start != END_OFFSET || cb.len != LEN2 || cb.end != cb.len - 2) {
- + ]
1055 : : printf("Error: buffer alignment is wrong!");
1056 : 0 : 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 [ - + ]: 1 : if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
1068 : : printf("Error: failed to reinitialize circular buffer!\n");
1069 : 0 : return -1;
1070 : : }
1071 : :
1072 : : /* push start into the right half */
1073 [ + + ]: 33 : for (i = 0; i < HALF_OFFSET; i++)
1074 : 32 : cirbuf_add_head_safe(&cb, 'h');
1075 : :
1076 : : /* push end into left half > start */
1077 [ + + ]: 17 : for (i = 0; i < SMALL_OFFSET; i++)
1078 : 16 : cirbuf_del_tail_safe(&cb);
1079 : :
1080 : : /* align */
1081 : 1 : cirbuf_align_right(&cb);
1082 : :
1083 : : /* verify result */
1084 [ + - - + ]: 1 : if (cb.end != END_OFFSET || cb.len != LEN3 || cb.start != cb.end - cb.len + 1) {
1085 : : printf("Error: buffer alignment is wrong!");
1086 : 0 : 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 [ - + ]: 1 : if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
1098 : : printf("Error: failed to reinitialize circular buffer!\n");
1099 : 0 : return -1;
1100 : : }
1101 : :
1102 : : /* push start into the right half */
1103 [ + + ]: 32 : for (i = 0; i < HALF_OFFSET - 1; i++)
1104 : 31 : cirbuf_add_head_safe(&cb, 'h');
1105 : :
1106 : : /* push end into left half < start */
1107 [ + + ]: 17 : for (i = 0; i < SMALL_OFFSET; i++)
1108 : 16 : cirbuf_add_tail_safe(&cb, 't');
1109 : :
1110 : : /* align */
1111 : 1 : cirbuf_align_right(&cb);
1112 : :
1113 : : /* verify result */
1114 [ + - - + ]: 1 : if (cb.end != END_OFFSET || cb.len != LEN4 || cb.start != cb.end - cb.len + 1) {
1115 : : printf("Error: buffer alignment is wrong!");
1116 : 0 : 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 [ - + ]: 1 : if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
1128 : : printf("Error: failed to reinitialize circular buffer!\n");
1129 : 0 : return -1;
1130 : : }
1131 : :
1132 : : /* add string to tail and head */
1133 [ + - ]: 1 : if (cirbuf_add_buf_tail(&cb, CIRBUF_STR_TAIL,
1134 [ - + ]: 1 : 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 : 0 : return -1;
1138 : : }
1139 : :
1140 : : /* align */
1141 [ - + ]: 1 : if (cirbuf_align_right(&cb) < 0) {
1142 : : printf("Error: alignment failed!\n");
1143 : 0 : return -1;
1144 : : }
1145 : :
1146 : : /* get string from head */
1147 [ - + ]: 1 : 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 : 0 : return -1;
1151 : : }
1152 : :
1153 : : /* verify string */
1154 [ - + ]: 1 : 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 : 0 : return -1;
1158 : : }
1159 : :
1160 : : /* reset tmp buffer */
1161 : : memset(tmp, 0, sizeof(tmp));
1162 : :
1163 : : /* get string from tail */
1164 [ - + ]: 1 : 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 : 0 : return -1;
1168 : : }
1169 : : /* verify string */
1170 [ - + ]: 1 : 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 : 0 : return -1;
1174 : : }
1175 : :
1176 : : return 0;
1177 : : }
1178 : :
1179 : : /* call functions with invalid parameters */
1180 : : int
1181 : 1 : test_cirbuf_invalid_param(void)
1182 : : {
1183 : : struct cirbuf cb;
1184 : : char buf[CMDLINE_TEST_BUFSIZE];
1185 : :
1186 : : /* null cirbuf */
1187 [ + - ]: 1 : if (cirbuf_init(0, buf, 0, sizeof(buf)) == 0)
1188 : : return -1;
1189 : : /* null buffer */
1190 [ + - ]: 1 : if (cirbuf_init(&cb, 0, 0, sizeof(buf)) == 0)
1191 : : return -1;
1192 : : /* null cirbuf */
1193 [ + - ]: 1 : if (cirbuf_add_head_safe(0, 'h') == 0)
1194 : : return -1;
1195 [ + - ]: 1 : if (cirbuf_add_tail_safe(0, 't') == 0)
1196 : : return -1;
1197 [ + - ]: 1 : if (cirbuf_del_head_safe(0) == 0)
1198 : : return -1;
1199 [ + - ]: 1 : if (cirbuf_del_tail_safe(0) == 0)
1200 : : return -1;
1201 : : /* null buffer */
1202 [ + - ]: 1 : if (cirbuf_add_buf_head(&cb, 0, 0) == 0)
1203 : : return -1;
1204 [ + - ]: 1 : if (cirbuf_add_buf_tail(&cb, 0, 0) == 0)
1205 : : return -1;
1206 : : /* null cirbuf */
1207 [ + - ]: 1 : if (cirbuf_add_buf_head(0, buf, 0) == 0)
1208 : : return -1;
1209 [ + - ]: 1 : if (cirbuf_add_buf_tail(0, buf, 0) == 0)
1210 : : return -1;
1211 : : /* null size */
1212 [ + - ]: 1 : if (cirbuf_add_buf_head(&cb, buf, 0) == 0)
1213 : : return -1;
1214 [ + - ]: 1 : if (cirbuf_add_buf_tail(&cb, buf, 0) == 0)
1215 : : return -1;
1216 : : /* null cirbuf */
1217 [ + - ]: 1 : if (cirbuf_del_buf_head(0, 0) == 0)
1218 : : return -1;
1219 [ + - ]: 1 : if (cirbuf_del_buf_tail(0, 0) == 0)
1220 : : return -1;
1221 : : /* null size */
1222 [ + - ]: 1 : if (cirbuf_del_buf_head(&cb, 0) == 0)
1223 : : return -1;
1224 [ + - ]: 1 : if (cirbuf_del_buf_tail(&cb, 0) == 0)
1225 : : return -1;
1226 : : /* null cirbuf */
1227 [ + - ]: 1 : if (cirbuf_get_buf_head(0, 0, 0) == 0)
1228 : : return -1;
1229 [ + - ]: 1 : if (cirbuf_get_buf_tail(0, 0, 0) == 0)
1230 : : return -1;
1231 : : /* null buffer */
1232 [ + - ]: 1 : if (cirbuf_get_buf_head(&cb, 0, 0) == 0)
1233 : : return -1;
1234 [ + - ]: 1 : if (cirbuf_get_buf_tail(&cb, 0, 0) == 0)
1235 : : return -1;
1236 : : /* null size, this is valid but should return 0 */
1237 [ + - ]: 1 : if (cirbuf_get_buf_head(&cb, buf, 0) != 0)
1238 : : return -1;
1239 [ + - ]: 1 : if (cirbuf_get_buf_tail(&cb, buf, 0) != 0)
1240 : : return -1;
1241 : : /* null cirbuf */
1242 [ + - ]: 1 : if (cirbuf_align_left(0) == 0)
1243 : : return -1;
1244 [ - + ]: 1 : if (cirbuf_align_right(0) == 0)
1245 : 0 : return -1;
1246 : :
1247 : : return 0;
1248 : : }
1249 : :
1250 : : /* test cmdline_cirbuf char functions */
1251 : : int
1252 : 1 : test_cirbuf_char(void)
1253 : : {
1254 : : int ret;
1255 : :
1256 : 1 : ret = test_cirbuf_char_add_del();
1257 [ + - ]: 1 : if (ret < 0)
1258 : : return -1;
1259 : :
1260 : 1 : ret = test_cirbuf_char_fill();
1261 [ - + ]: 1 : if (ret < 0)
1262 : 0 : return -1;
1263 : :
1264 : : return 0;
1265 : : }
1266 : :
1267 : : /* test cmdline_cirbuf string functions */
1268 : : int
1269 : 1 : test_cirbuf_string(void)
1270 : : {
1271 [ + - ]: 1 : if (test_cirbuf_string_add_del() < 0)
1272 : : return -1;
1273 : :
1274 [ + - ]: 1 : if (test_cirbuf_string_add_del_reverse() < 0)
1275 : : return -1;
1276 : :
1277 [ + - ]: 1 : if (test_cirbuf_string_add_boundaries() < 0)
1278 : : return -1;
1279 : :
1280 [ + - ]: 1 : if (test_cirbuf_string_get_del_boundaries() < 0)
1281 : : return -1;
1282 : :
1283 [ + - ]: 1 : if (test_cirbuf_string_get_del_partial() < 0)
1284 : : return -1;
1285 : :
1286 [ - + ]: 1 : if (test_cirbuf_string_misc() < 0)
1287 : 0 : return -1;
1288 : :
1289 : : return 0;
1290 : : }
1291 : :
1292 : : /* test cmdline_cirbuf align functions */
1293 : : int
1294 : 1 : test_cirbuf_align(void)
1295 : : {
1296 [ + - ]: 1 : if (test_cirbuf_align_left() < 0)
1297 : : return -1;
1298 [ - + ]: 1 : if (test_cirbuf_align_right() < 0)
1299 : 0 : return -1;
1300 : : return 0;
1301 : : }
|