]> git.proxmox.com Git - ceph.git/blame - ceph/src/spdk/test/unit/lib/util/pipe.c/pipe_ut.c
update source to Ceph Pacific 16.2.2
[ceph.git] / ceph / src / spdk / test / unit / lib / util / pipe.c / pipe_ut.c
CommitLineData
f67539c2
TL
1/*-
2 * BSD LICENSE
3 *
4 * Copyright (c) Intel Corporation.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34#include "spdk/stdinc.h"
35
36#include "spdk_cunit.h"
37
38#include "util/pipe.c"
39#include "common/lib/test_env.c"
40
41static void
42test_create_destroy(void)
43{
44 struct spdk_pipe *pipe;
45 uint8_t mem[10];
46
47 pipe = spdk_pipe_create(mem, sizeof(mem));
48 SPDK_CU_ASSERT_FATAL(pipe != NULL);
49
50 spdk_pipe_destroy(pipe);
51}
52
53static void
54test_write_get_buffer(void)
55{
56 struct spdk_pipe *pipe;
57 uint8_t mem[10];
58 struct iovec iovs[2];
59 int rc;
60
61 pipe = spdk_pipe_create(mem, sizeof(mem));
62 SPDK_CU_ASSERT_FATAL(pipe != NULL);
63
64 /* Get some available memory. */
65 rc = spdk_pipe_writer_get_buffer(pipe, 5, iovs);
66 CU_ASSERT(rc == 5);
67 CU_ASSERT(iovs[0].iov_base == mem);
68 CU_ASSERT(iovs[0].iov_len == 5);
69 CU_ASSERT(iovs[1].iov_base == NULL);
70 CU_ASSERT(iovs[1].iov_len == 0);
71 CU_ASSERT(pipe->write == 0);
72 CU_ASSERT(pipe->read == 0);
73
74 memset(iovs, 0, sizeof(iovs));
75
76 /* Get 0 bytes. */
77 rc = spdk_pipe_writer_get_buffer(pipe, 0, iovs);
78 CU_ASSERT(rc == 0);
79 CU_ASSERT(iovs[0].iov_base == NULL);
80 CU_ASSERT(iovs[0].iov_len == 0);
81 CU_ASSERT(iovs[1].iov_base == NULL);
82 CU_ASSERT(iovs[1].iov_len == 0);
83 CU_ASSERT(pipe->write == 0);
84 CU_ASSERT(pipe->read == 0);
85
86 memset(iovs, 0, sizeof(iovs));
87
88 /* Get all available memory */
89 rc = spdk_pipe_writer_get_buffer(pipe, 9, iovs);
90 CU_ASSERT(rc == 9);
91 CU_ASSERT(iovs[0].iov_base == mem);
92 CU_ASSERT(iovs[0].iov_len == 9);
93 CU_ASSERT(iovs[1].iov_base == NULL);
94 CU_ASSERT(iovs[1].iov_len == 0);
95 CU_ASSERT(pipe->write == 0);
96 CU_ASSERT(pipe->read == 0);
97
98 memset(iovs, 0, sizeof(iovs));
99
100 /* Get the full size of the data buffer backing the pipe, which isn't allowed */
101 rc = spdk_pipe_writer_get_buffer(pipe, 10, iovs);
102 CU_ASSERT(rc == 9);
103 CU_ASSERT(iovs[0].iov_base == mem);
104 CU_ASSERT(iovs[0].iov_len == 9);
105 CU_ASSERT(iovs[1].iov_base == NULL);
106 CU_ASSERT(iovs[1].iov_len == 0);
107 CU_ASSERT(pipe->write == 0);
108 CU_ASSERT(pipe->read == 0);
109
110 memset(iovs, 0, sizeof(iovs));
111
112 /* Advance the write pointer 7 bytes in. */
113 pipe->write = 7;
114
115 /* Get all of the available memory. */
116 rc = spdk_pipe_writer_get_buffer(pipe, 2, iovs);
117 CU_ASSERT(rc == 2);
118 CU_ASSERT(iovs[0].iov_base == (mem + 7));
119 CU_ASSERT(iovs[0].iov_len == 2);
120 CU_ASSERT(iovs[1].iov_base == NULL);
121 CU_ASSERT(iovs[1].iov_len == 0);
122 CU_ASSERT(pipe->write == 7);
123 CU_ASSERT(pipe->read == 0);
124
125 memset(iovs, 0, sizeof(iovs));
126
127 /* Get more than the available memory */
128 rc = spdk_pipe_writer_get_buffer(pipe, 3, iovs);
129 CU_ASSERT(rc == 2);
130 CU_ASSERT(iovs[0].iov_base == (mem + 7));
131 CU_ASSERT(iovs[0].iov_len == 2);
132 CU_ASSERT(iovs[1].iov_base == NULL);
133 CU_ASSERT(iovs[1].iov_len == 0);
134 CU_ASSERT(pipe->write == 7);
135 CU_ASSERT(pipe->read == 0);
136
137 memset(iovs, 0, sizeof(iovs));
138
139 /* Advance the read pointer 3 bytes in. */
140 pipe->read = 3;
141
142 /* Get all of the available memory. */
143 rc = spdk_pipe_writer_get_buffer(pipe, 5, iovs);
144 CU_ASSERT(rc == 5);
145 CU_ASSERT(iovs[0].iov_base == (mem + 7));
146 CU_ASSERT(iovs[0].iov_len == 3);
147 CU_ASSERT(iovs[1].iov_base == mem);
148 CU_ASSERT(iovs[1].iov_len == 2);
149 CU_ASSERT(pipe->write == 7);
150 CU_ASSERT(pipe->read == 3);
151
152 memset(iovs, 0, sizeof(iovs));
153
154 /* Get more than the available memory */
155 rc = spdk_pipe_writer_get_buffer(pipe, 6, iovs);
156 CU_ASSERT(rc == 5);
157 CU_ASSERT(iovs[0].iov_base == (mem + 7));
158 CU_ASSERT(iovs[0].iov_len == 3);
159 CU_ASSERT(iovs[1].iov_base == mem);
160 CU_ASSERT(iovs[1].iov_len == 2);
161 CU_ASSERT(pipe->write == 7);
162 CU_ASSERT(pipe->read == 3);
163
164 memset(iovs, 0, sizeof(iovs));
165
166 /* Advance the read pointer past the write pointer */
167 pipe->read = 9;
168
169 /* Get all of the available memory. */
170 rc = spdk_pipe_writer_get_buffer(pipe, 1, iovs);
171 CU_ASSERT(rc == 1);
172 CU_ASSERT(iovs[0].iov_base == (mem + 7));
173 CU_ASSERT(iovs[0].iov_len == 1);
174 CU_ASSERT(iovs[1].iov_base == NULL);
175 CU_ASSERT(iovs[1].iov_len == 0);
176 CU_ASSERT(pipe->write == 7);
177 CU_ASSERT(pipe->read == 9);
178
179 memset(iovs, 0, sizeof(iovs));
180
181 /* Get more than the available memory */
182 rc = spdk_pipe_writer_get_buffer(pipe, 2, iovs);
183 CU_ASSERT(rc == 1);
184 CU_ASSERT(iovs[0].iov_base == (mem + 7));
185 CU_ASSERT(iovs[0].iov_len == 1);
186 CU_ASSERT(iovs[1].iov_base == NULL);
187 CU_ASSERT(iovs[1].iov_len == 0);
188 CU_ASSERT(pipe->write == 7);
189 CU_ASSERT(pipe->read == 9);
190
191 memset(iovs, 0, sizeof(iovs));
192
193 /* Fill the pipe */
194 pipe->write = 8;
195
196 /* Get data while the pipe is full */
197 rc = spdk_pipe_writer_get_buffer(pipe, 1, iovs);
198 CU_ASSERT(rc == 0);
199 CU_ASSERT(iovs[0].iov_base == NULL);
200 CU_ASSERT(iovs[0].iov_len == 0);
201 CU_ASSERT(iovs[1].iov_base == NULL);
202 CU_ASSERT(iovs[1].iov_len == 0);
203 CU_ASSERT(pipe->write == 8);
204 CU_ASSERT(pipe->read == 9);
205
206 spdk_pipe_destroy(pipe);
207}
208
209static void
210test_write_advance(void)
211{
212 struct spdk_pipe *pipe;
213 uint8_t mem[10];
214 int rc;
215
216 pipe = spdk_pipe_create(mem, sizeof(mem));
217 SPDK_CU_ASSERT_FATAL(pipe != NULL);
218
219 /* Advance half way through the pipe */
220 rc = spdk_pipe_writer_advance(pipe, 5);
221 CU_ASSERT(rc == 0);
222 CU_ASSERT(pipe->write == 5);
223 CU_ASSERT(pipe->read == 0);
224
225 pipe->write = 0;
226
227 /* Advance to the end of the pipe */
228 rc = spdk_pipe_writer_advance(pipe, 9);
229 CU_ASSERT(rc == 0);
230 CU_ASSERT(pipe->write == 9);
231 CU_ASSERT(pipe->read == 0);
232
233 pipe->write = 0;
234
235 /* Advance beyond the end */
236 rc = spdk_pipe_writer_advance(pipe, 10);
237 CU_ASSERT(rc == -EINVAL);
238 CU_ASSERT(pipe->write == 0);
239 CU_ASSERT(pipe->read == 0);
240
241 /* Move the read pointer forward */
242 pipe->write = 0;
243 pipe->read = 5;
244
245 /* Advance to the end of the pipe */
246 rc = spdk_pipe_writer_advance(pipe, 4);
247 CU_ASSERT(rc == 0);
248 CU_ASSERT(pipe->write == 4);
249 CU_ASSERT(pipe->read == 5);
250
251 pipe->write = 0;
252 pipe->read = 5;
253
254 /* Advance beyond the end */
255 rc = spdk_pipe_writer_advance(pipe, 5);
256 CU_ASSERT(rc == -EINVAL);
257 CU_ASSERT(pipe->write == 0);
258 CU_ASSERT(pipe->read == 5);
259
260 /* Test wrap around */
261 pipe->write = 7;
262 pipe->read = 3;
263
264 /* Advance to the end of the pipe */
265 rc = spdk_pipe_writer_advance(pipe, 5);
266 CU_ASSERT(rc == 0);
267 CU_ASSERT(pipe->write == 2);
268 CU_ASSERT(pipe->read == 3);
269
270 pipe->write = 7;
271 pipe->read = 3;
272
273 /* Advance beyond the end */
274 rc = spdk_pipe_writer_advance(pipe, 6);
275 CU_ASSERT(rc == -EINVAL);
276 CU_ASSERT(pipe->write == 7);
277 CU_ASSERT(pipe->read == 3);
278
279 spdk_pipe_destroy(pipe);
280}
281
282static void
283test_read_get_buffer(void)
284{
285 struct spdk_pipe *pipe;
286 uint8_t mem[10];
287 struct iovec iovs[2];
288 int rc;
289
290 pipe = spdk_pipe_create(mem, sizeof(mem));
291 SPDK_CU_ASSERT_FATAL(pipe != NULL);
292
293 /* Set the write pointer to the end, making all data available. */
294 pipe->write = 9;
295
296 /* Get half the available memory. */
297 rc = spdk_pipe_reader_get_buffer(pipe, 5, iovs);
298 CU_ASSERT(rc == 5);
299 CU_ASSERT(iovs[0].iov_base == mem);
300 CU_ASSERT(iovs[0].iov_len == 5);
301 CU_ASSERT(iovs[1].iov_base == NULL);
302 CU_ASSERT(iovs[1].iov_len == 0);
303 CU_ASSERT(pipe->write == 9);
304 CU_ASSERT(pipe->read == 0);
305
306 memset(iovs, 0, sizeof(iovs));
307
308 /* Get 0 bytes. */
309 rc = spdk_pipe_reader_get_buffer(pipe, 0, iovs);
310 CU_ASSERT(rc == 0);
311 CU_ASSERT(iovs[0].iov_base == NULL);
312 CU_ASSERT(iovs[0].iov_len == 0);
313 CU_ASSERT(iovs[1].iov_base == NULL);
314 CU_ASSERT(iovs[1].iov_len == 0);
315 CU_ASSERT(pipe->write == 9);
316 CU_ASSERT(pipe->read == 0);
317
318 memset(iovs, 0, sizeof(iovs));
319
320 /* Get all available memory */
321 rc = spdk_pipe_reader_get_buffer(pipe, 9, iovs);
322 CU_ASSERT(rc == 9);
323 CU_ASSERT(iovs[0].iov_base == mem);
324 CU_ASSERT(iovs[0].iov_len == 9);
325 CU_ASSERT(iovs[1].iov_base == NULL);
326 CU_ASSERT(iovs[1].iov_len == 0);
327 CU_ASSERT(pipe->write == 9);
328 CU_ASSERT(pipe->read == 0);
329
330 memset(iovs, 0, sizeof(iovs));
331
332 /* Get more bytes than exist */
333 rc = spdk_pipe_reader_get_buffer(pipe, 10, iovs);
334 CU_ASSERT(rc == 9);
335 CU_ASSERT(iovs[0].iov_base == mem);
336 CU_ASSERT(iovs[0].iov_len == 9);
337 CU_ASSERT(iovs[1].iov_base == NULL);
338 CU_ASSERT(iovs[1].iov_len == 0);
339 CU_ASSERT(pipe->write == 9);
340 CU_ASSERT(pipe->read == 0);
341
342 memset(iovs, 0, sizeof(iovs));
343
344 /* Advance the read pointer 5 bytes in. */
345 pipe->read = 5;
346 pipe->write = 0;
347
348 /* Get all of the available memory. */
349 rc = spdk_pipe_reader_get_buffer(pipe, 5, iovs);
350 CU_ASSERT(rc == 5);
351 CU_ASSERT(iovs[0].iov_base == (mem + 5));
352 CU_ASSERT(iovs[0].iov_len == 5);
353 CU_ASSERT(iovs[1].iov_base == NULL);
354 CU_ASSERT(iovs[1].iov_len == 0);
355 CU_ASSERT(pipe->write == 0);
356 CU_ASSERT(pipe->read == 5);
357
358 memset(iovs, 0, sizeof(iovs));
359
360 /* Get more than the available memory */
361 rc = spdk_pipe_reader_get_buffer(pipe, 6, iovs);
362 CU_ASSERT(rc == 5);
363 CU_ASSERT(iovs[0].iov_base == (mem + 5));
364 CU_ASSERT(iovs[0].iov_len == 5);
365 CU_ASSERT(iovs[1].iov_base == NULL);
366 CU_ASSERT(iovs[1].iov_len == 0);
367 CU_ASSERT(pipe->write == 0);
368 CU_ASSERT(pipe->read == 5);
369
370 memset(iovs, 0, sizeof(iovs));
371
372 /* Invert the write and read pointers */
373 pipe->read = 7;
374 pipe->write = 3;
375
376 /* Get all of the available memory. */
377 rc = spdk_pipe_reader_get_buffer(pipe, 6, iovs);
378 CU_ASSERT(rc == 6);
379 CU_ASSERT(iovs[0].iov_base == (mem + 7));
380 CU_ASSERT(iovs[0].iov_len == 3);
381 CU_ASSERT(iovs[1].iov_base == mem);
382 CU_ASSERT(iovs[1].iov_len == 3);
383 CU_ASSERT(pipe->write == 3);
384 CU_ASSERT(pipe->read == 7);
385
386 memset(iovs, 0, sizeof(iovs));
387
388 /* Get more than the available memory */
389 rc = spdk_pipe_reader_get_buffer(pipe, 7, iovs);
390 CU_ASSERT(rc == 6);
391 CU_ASSERT(iovs[0].iov_base == (mem + 7));
392 CU_ASSERT(iovs[0].iov_len == 3);
393 CU_ASSERT(iovs[1].iov_base == mem);
394 CU_ASSERT(iovs[1].iov_len == 3);
395 CU_ASSERT(pipe->write == 3);
396 CU_ASSERT(pipe->read == 7);
397
398 memset(iovs, 0, sizeof(iovs));
399
400 /* Empty the pipe */
401 pipe->read = 8;
402 pipe->write = 8;
403
404 /* Get data while the pipe is empty */
405 rc = spdk_pipe_reader_get_buffer(pipe, 1, iovs);
406 CU_ASSERT(rc == 0);
407 CU_ASSERT(iovs[0].iov_base == NULL);
408 CU_ASSERT(iovs[0].iov_len == 0);
409 CU_ASSERT(iovs[1].iov_base == NULL);
410 CU_ASSERT(iovs[1].iov_len == 0);
411 CU_ASSERT(pipe->write == 8);
412 CU_ASSERT(pipe->read == 8);
413
414 spdk_pipe_destroy(pipe);
415}
416
417static void
418test_read_advance(void)
419{
420 struct spdk_pipe *pipe;
421 uint8_t mem[10];
422 int rc;
423
424 pipe = spdk_pipe_create(mem, sizeof(mem));
425 SPDK_CU_ASSERT_FATAL(pipe != NULL);
426
427 pipe->read = 0;
428 pipe->write = 9;
429
430 /* Advance half way through the pipe */
431 rc = spdk_pipe_reader_advance(pipe, 5);
432 CU_ASSERT(rc == 0);
433 CU_ASSERT(pipe->read == 5);
434 CU_ASSERT(pipe->write == 9);
435
436 pipe->read = 0;
437 pipe->write = 9;
438
439 /* Advance to the end of the pipe */
440 rc = spdk_pipe_reader_advance(pipe, 9);
441 CU_ASSERT(rc == 0);
442 CU_ASSERT(pipe->read == 9);
443 CU_ASSERT(pipe->write == 9);
444
445 pipe->read = 0;
446 pipe->write = 9;
447
448 /* Advance beyond the end */
449 rc = spdk_pipe_reader_advance(pipe, 10);
450 CU_ASSERT(rc == -EINVAL);
451 CU_ASSERT(pipe->read == 0);
452 CU_ASSERT(pipe->write == 9);
453
454 /* Move the write pointer forward */
455 pipe->read = 0;
456 pipe->write = 5;
457
458 /* Advance to the end of the pipe */
459 rc = spdk_pipe_reader_advance(pipe, 5);
460 CU_ASSERT(rc == 0);
461 CU_ASSERT(pipe->write == 5);
462 CU_ASSERT(pipe->read == 5);
463
464 pipe->read = 0;
465 pipe->write = 5;
466
467 /* Advance beyond the end */
468 rc = spdk_pipe_reader_advance(pipe, 6);
469 CU_ASSERT(rc == -EINVAL);
470 CU_ASSERT(pipe->read == 0);
471 CU_ASSERT(pipe->write == 5);
472
473 /* Test wrap around */
474 pipe->read = 7;
475 pipe->write = 3;
476
477 /* Advance to the end of the pipe */
478 rc = spdk_pipe_reader_advance(pipe, 6);
479 CU_ASSERT(rc == 0);
480 CU_ASSERT(pipe->read == 3);
481 CU_ASSERT(pipe->write == 3);
482
483 pipe->read = 7;
484 pipe->write = 3;
485
486 /* Advance beyond the end */
487 rc = spdk_pipe_writer_advance(pipe, 7);
488 CU_ASSERT(rc == -EINVAL);
489 CU_ASSERT(pipe->read == 7);
490 CU_ASSERT(pipe->write == 3);
491
492 spdk_pipe_destroy(pipe);
493}
494
495static void
496test_data(void)
497{
498 struct spdk_pipe *pipe;
499 uint8_t mem[10];
500 struct iovec iovs[2];
501 uint8_t *data;
502 int rc;
503 size_t i;
504
505 memset(mem, 0, sizeof(mem));
506 memset(iovs, 0, sizeof(iovs));
507
508 pipe = spdk_pipe_create(mem, sizeof(mem));
509 SPDK_CU_ASSERT_FATAL(pipe != NULL);
510
511 /* Place 1 byte in the pipe */
512 rc = spdk_pipe_writer_get_buffer(pipe, 1, iovs);
513 CU_ASSERT(rc == 1);
514 CU_ASSERT(iovs[0].iov_base != NULL);
515 CU_ASSERT(iovs[0].iov_len == 1);
516
517 memset(iovs[0].iov_base, 'A', 1);
518
519 rc = spdk_pipe_writer_advance(pipe, 1);
520 CU_ASSERT(rc == 0);
521
522 CU_ASSERT(mem[0] == 'A');
523 CU_ASSERT(mem[1] == 0);
524 CU_ASSERT(mem[2] == 0);
525 CU_ASSERT(mem[3] == 0);
526 CU_ASSERT(mem[4] == 0);
527 CU_ASSERT(mem[5] == 0);
528 CU_ASSERT(mem[6] == 0);
529 CU_ASSERT(mem[7] == 0);
530 CU_ASSERT(mem[8] == 0);
531 CU_ASSERT(mem[9] == 0);
532
533 memset(iovs, 0, sizeof(iovs));
534
535 /* Get 1 byte from the pipe */
536 CU_ASSERT(spdk_pipe_reader_bytes_available(pipe) == 1);
537 rc = spdk_pipe_reader_get_buffer(pipe, 10, iovs);
538 CU_ASSERT(rc == 1);
539
540 data = iovs[0].iov_base;
541 CU_ASSERT(*data = 'A');
542
543 spdk_pipe_reader_advance(pipe, 1);
544
545 /* Put 9 more bytes in the pipe, so every byte has
546 * been written */
547 rc = spdk_pipe_writer_get_buffer(pipe, 9, iovs);
548 CU_ASSERT(rc == 9);
549 CU_ASSERT(iovs[0].iov_len == 9);
550 CU_ASSERT(iovs[1].iov_len == 0);
551
552 memset(iovs[0].iov_base, 'B', iovs[0].iov_len);
553
554 rc = spdk_pipe_writer_advance(pipe, 9);
555 CU_ASSERT(rc == 0);
556
557 CU_ASSERT(mem[0] == 'A');
558 CU_ASSERT(mem[1] == 'B');
559 CU_ASSERT(mem[2] == 'B');
560 CU_ASSERT(mem[3] == 'B');
561 CU_ASSERT(mem[4] == 'B');
562 CU_ASSERT(mem[5] == 'B');
563 CU_ASSERT(mem[6] == 'B');
564 CU_ASSERT(mem[7] == 'B');
565 CU_ASSERT(mem[8] == 'B');
566 CU_ASSERT(mem[9] == 'B');
567
568 memset(iovs, 0, sizeof(iovs));
569
570 /* Get 7 bytes of the previously written 9. */
571 CU_ASSERT(spdk_pipe_reader_bytes_available(pipe) == 9);
572 rc = spdk_pipe_reader_get_buffer(pipe, 7, iovs);
573 CU_ASSERT(rc == 7);
574
575 CU_ASSERT(iovs[0].iov_len == 7);
576 data = iovs[0].iov_base;
577 for (i = 0; i < iovs[0].iov_len; i++) {
578 CU_ASSERT(data[i] == 'B');
579 }
580
581 spdk_pipe_reader_advance(pipe, 7);
582
583 memset(iovs, 0, sizeof(iovs));
584
585 /* Put 1 more byte in the pipe, overwriting the original 'A' */
586 rc = spdk_pipe_writer_get_buffer(pipe, 1, iovs);
587 CU_ASSERT(rc == 1);
588 CU_ASSERT(iovs[0].iov_len == 1);
589 CU_ASSERT(iovs[1].iov_len == 0);
590
591 memset(iovs[0].iov_base, 'C', iovs[0].iov_len);
592
593 rc = spdk_pipe_writer_advance(pipe, 1);
594 CU_ASSERT(rc == 0);
595
596 CU_ASSERT(mem[0] == 'C');
597 CU_ASSERT(mem[1] == 'B');
598 CU_ASSERT(mem[2] == 'B');
599 CU_ASSERT(mem[3] == 'B');
600 CU_ASSERT(mem[4] == 'B');
601 CU_ASSERT(mem[5] == 'B');
602 CU_ASSERT(mem[6] == 'B');
603 CU_ASSERT(mem[7] == 'B');
604 CU_ASSERT(mem[8] == 'B');
605 CU_ASSERT(mem[9] == 'B');
606
607 memset(iovs, 0, sizeof(iovs));
608
609 /* Get all of the data out of the pipe */
610 CU_ASSERT(spdk_pipe_reader_bytes_available(pipe) == 3);
611 rc = spdk_pipe_reader_get_buffer(pipe, 3, iovs);
612 CU_ASSERT(rc == 3);
613 CU_ASSERT(iovs[0].iov_len == 2);
614 CU_ASSERT(iovs[1].iov_len == 1);
615
616 data = iovs[0].iov_base;
617 CU_ASSERT(data[0] == 'B');
618 CU_ASSERT(data[1] == 'B');
619 data = iovs[1].iov_base;
620 CU_ASSERT(data[0] == 'C');
621
622 spdk_pipe_reader_advance(pipe, 3);
623
624 spdk_pipe_destroy(pipe);
625}
626
627int
628main(int argc, char **argv)
629{
630 CU_pSuite suite = NULL;
631 unsigned int num_failures;
632
633 CU_set_error_action(CUEA_ABORT);
634 CU_initialize_registry();
635
636 suite = CU_add_suite("pipe", NULL, NULL);
637
638 CU_ADD_TEST(suite, test_create_destroy);
639 CU_ADD_TEST(suite, test_write_get_buffer);
640 CU_ADD_TEST(suite, test_write_advance);
641 CU_ADD_TEST(suite, test_read_get_buffer);
642 CU_ADD_TEST(suite, test_read_advance);
643 CU_ADD_TEST(suite, test_data);
644
645 CU_basic_set_mode(CU_BRM_VERBOSE);
646
647 CU_basic_run_tests();
648
649 num_failures = CU_get_number_of_failures();
650 CU_cleanup_registry();
651
652 return num_failures;
653}