]>
git.proxmox.com Git - qemu.git/blob - tests/test-iov.c
2 #include "qemu-common.h"
4 #include "qemu/sockets.h"
6 /* create a randomly-sized iovec with random vectors */
7 static void iov_random(struct iovec
**iovp
, unsigned *iov_cntp
)
9 unsigned niov
= g_test_rand_int_range(3,8);
10 struct iovec
*iov
= g_malloc(niov
* sizeof(*iov
));
12 for (i
= 0; i
< niov
; ++i
) {
13 iov
[i
].iov_len
= g_test_rand_int_range(5,20);
14 iov
[i
].iov_base
= g_malloc(iov
[i
].iov_len
);
20 static void iov_free(struct iovec
*iov
, unsigned niov
)
23 for (i
= 0; i
< niov
; ++i
) {
24 g_free(iov
[i
].iov_base
);
29 static void test_iov_bytes(struct iovec
*iov
, unsigned niov
,
30 size_t offset
, size_t bytes
)
37 /* we walk over all elements, */
38 for (i
= 0; i
< niov
; ++i
) {
40 /* over each char of each element, */
41 for (j
= 0; j
< iov
[i
].iov_len
; ++j
) {
42 /* counting each of them and
43 * verifying that the ones within [offset,offset+bytes)
44 * range are equal to the position number (o) */
45 if (o
>= offset
&& o
< offset
+ bytes
) {
46 g_assert(b
[j
] == (o
& 255));
48 g_assert(b
[j
] == 0xff);
55 static void test_to_from_buf_1(void)
60 unsigned char *ibuf
, *obuf
;
63 iov_random(&iov
, &niov
);
65 sz
= iov_size(iov
, niov
);
67 ibuf
= g_malloc(sz
+ 8) + 4;
68 memcpy(ibuf
-4, "aaaa", 4); memcpy(ibuf
+ sz
, "bbbb", 4);
69 obuf
= g_malloc(sz
+ 8) + 4;
70 memcpy(obuf
-4, "xxxx", 4); memcpy(obuf
+ sz
, "yyyy", 4);
72 /* fill in ibuf with 0123456... */
73 for (i
= 0; i
< sz
; ++i
) {
77 for (i
= 0; i
<= sz
; ++i
) {
79 /* Test from/to buf for offset(i) in [0..sz] up to the end of buffer.
80 * For last iteration with offset == sz, the procedure should
81 * skip whole vector and process exactly 0 bytes */
83 /* first set bytes [i..sz) to some "random" value */
84 n
= iov_memset(iov
, niov
, 0, 0xff, -1);
87 /* next copy bytes [i..sz) from ibuf to iovec */
88 n
= iov_from_buf(iov
, niov
, i
, ibuf
+ i
, -1);
89 g_assert(n
== sz
- i
);
91 /* clear part of obuf */
92 memset(obuf
+ i
, 0, sz
- i
);
93 /* and set this part of obuf to values from iovec */
94 n
= iov_to_buf(iov
, niov
, i
, obuf
+ i
, -1);
95 g_assert(n
== sz
- i
);
97 /* now compare resulting buffers */
98 g_assert(memcmp(ibuf
, obuf
, sz
) == 0);
100 /* test just one char */
101 n
= iov_to_buf(iov
, niov
, i
, obuf
+ i
, 1);
102 g_assert(n
== (i
< sz
));
104 g_assert(obuf
[i
] == (i
& 255));
107 for (j
= i
; j
<= sz
; ++j
) {
108 /* now test num of bytes cap up to byte no. j,
109 * with j in [i..sz]. */
112 n
= iov_memset(iov
, niov
, 0, 0xff, -1);
115 /* copy bytes [i..j) from ibuf to iovec */
116 n
= iov_from_buf(iov
, niov
, i
, ibuf
+ i
, j
- i
);
117 g_assert(n
== j
- i
);
119 /* clear part of obuf */
120 memset(obuf
+ i
, 0, j
- i
);
122 /* copy bytes [i..j) from iovec to obuf */
123 n
= iov_to_buf(iov
, niov
, i
, obuf
+ i
, j
- i
);
124 g_assert(n
== j
- i
);
127 g_assert(memcmp(ibuf
, obuf
, sz
) == 0);
129 /* now actually check if the iovec contains the right data */
130 test_iov_bytes(iov
, niov
, i
, j
- i
);
133 g_assert(!memcmp(ibuf
-4, "aaaa", 4) && !memcmp(ibuf
+sz
, "bbbb", 4));
135 g_assert(!memcmp(obuf
-4, "xxxx", 4) && !memcmp(obuf
+sz
, "yyyy", 4));
140 static void test_to_from_buf(void)
143 for (x
= 0; x
< 4; ++x
) {
144 test_to_from_buf_1();
148 static void test_io(void)
151 /* socketpair(PF_UNIX) which does not exist on windows */
155 unsigned i
, j
, k
, s
, t
;
158 struct iovec
*iov
, *siov
;
162 iov_random(&iov
, &niov
);
163 sz
= iov_size(iov
, niov
);
165 for (i
= 0; i
< sz
; ++i
) {
168 iov_from_buf(iov
, niov
, 0, buf
, sz
);
170 siov
= g_malloc(sizeof(*iov
) * niov
);
171 memcpy(siov
, iov
, sizeof(*iov
) * niov
);
173 if (socketpair(PF_UNIX
, SOCK_STREAM
, 0, sv
) < 0) {
174 perror("socketpair");
186 fcntl(sv
[1], F_SETFL
, O_RDWR
|O_NONBLOCK
);
187 r
= g_test_rand_int_range(sz
/ 2, sz
);
188 setsockopt(sv
[1], SOL_SOCKET
, SO_SNDBUF
, &r
, sizeof(r
));
190 for (i
= 0; i
<= sz
; ++i
) {
191 for (j
= i
; j
<= sz
; ++j
) {
194 s
= g_test_rand_int_range(0, j
- k
+ 1);
195 r
= iov_send(sv
[1], iov
, niov
, k
, s
);
196 g_assert(memcmp(iov
, siov
, sizeof(*iov
)*niov
) == 0);
200 usleep(g_test_rand_int_range(0, 30));
201 } else if (errno
== EAGAIN
) {
202 select(sv
[1]+1, NULL
, &fds
, NULL
, NULL
);
214 /* reader & verifier */
218 fcntl(sv
[0], F_SETFL
, O_RDWR
|O_NONBLOCK
);
219 r
= g_test_rand_int_range(sz
/ 2, sz
);
220 setsockopt(sv
[0], SOL_SOCKET
, SO_RCVBUF
, &r
, sizeof(r
));
223 for (i
= 0; i
<= sz
; ++i
) {
224 for (j
= i
; j
<= sz
; ++j
) {
226 iov_memset(iov
, niov
, 0, 0xff, -1);
228 s
= g_test_rand_int_range(0, j
- k
+ 1);
229 r
= iov_recv(sv
[0], iov
, niov
, k
, s
);
230 g_assert(memcmp(iov
, siov
, sizeof(*iov
)*niov
) == 0);
238 } else if (errno
== EAGAIN
) {
239 select(sv
[0]+1, &fds
, NULL
, NULL
, NULL
);
246 test_iov_bytes(iov
, niov
, i
, j
- i
);
253 static void test_discard_front(void)
256 struct iovec
*iov_tmp
;
257 unsigned int iov_cnt
;
258 unsigned int iov_cnt_tmp
;
263 /* Discard zero bytes */
264 iov_random(&iov
, &iov_cnt
);
266 iov_cnt_tmp
= iov_cnt
;
267 ret
= iov_discard_front(&iov_tmp
, &iov_cnt_tmp
, 0);
269 g_assert(iov_tmp
== iov
);
270 g_assert(iov_cnt_tmp
== iov_cnt
);
271 iov_free(iov
, iov_cnt
);
273 /* Discard more bytes than vector size */
274 iov_random(&iov
, &iov_cnt
);
276 iov_cnt_tmp
= iov_cnt
;
277 size
= iov_size(iov
, iov_cnt
);
278 ret
= iov_discard_front(&iov_tmp
, &iov_cnt_tmp
, size
+ 1);
279 g_assert(ret
== size
);
280 g_assert(iov_cnt_tmp
== 0);
281 iov_free(iov
, iov_cnt
);
283 /* Discard entire vector */
284 iov_random(&iov
, &iov_cnt
);
286 iov_cnt_tmp
= iov_cnt
;
287 size
= iov_size(iov
, iov_cnt
);
288 ret
= iov_discard_front(&iov_tmp
, &iov_cnt_tmp
, size
);
289 g_assert(ret
== size
);
290 g_assert(iov_cnt_tmp
== 0);
291 iov_free(iov
, iov_cnt
);
293 /* Discard within first element */
294 iov_random(&iov
, &iov_cnt
);
296 iov_cnt_tmp
= iov_cnt
;
297 old_base
= iov
->iov_base
;
298 size
= g_test_rand_int_range(1, iov
->iov_len
);
299 ret
= iov_discard_front(&iov_tmp
, &iov_cnt_tmp
, size
);
300 g_assert(ret
== size
);
301 g_assert(iov_tmp
== iov
);
302 g_assert(iov_cnt_tmp
== iov_cnt
);
303 g_assert(iov_tmp
->iov_base
== old_base
+ size
);
304 iov_tmp
->iov_base
= old_base
; /* undo before g_free() */
305 iov_free(iov
, iov_cnt
);
307 /* Discard entire first element */
308 iov_random(&iov
, &iov_cnt
);
310 iov_cnt_tmp
= iov_cnt
;
311 ret
= iov_discard_front(&iov_tmp
, &iov_cnt_tmp
, iov
->iov_len
);
312 g_assert(ret
== iov
->iov_len
);
313 g_assert(iov_tmp
== iov
+ 1);
314 g_assert(iov_cnt_tmp
== iov_cnt
- 1);
315 iov_free(iov
, iov_cnt
);
317 /* Discard within second element */
318 iov_random(&iov
, &iov_cnt
);
320 iov_cnt_tmp
= iov_cnt
;
321 old_base
= iov
[1].iov_base
;
322 size
= iov
->iov_len
+ g_test_rand_int_range(1, iov
[1].iov_len
);
323 ret
= iov_discard_front(&iov_tmp
, &iov_cnt_tmp
, size
);
324 g_assert(ret
== size
);
325 g_assert(iov_tmp
== iov
+ 1);
326 g_assert(iov_cnt_tmp
== iov_cnt
- 1);
327 g_assert(iov_tmp
->iov_base
== old_base
+ (size
- iov
->iov_len
));
328 iov_tmp
->iov_base
= old_base
; /* undo before g_free() */
329 iov_free(iov
, iov_cnt
);
332 static void test_discard_back(void)
335 unsigned int iov_cnt
;
336 unsigned int iov_cnt_tmp
;
341 /* Discard zero bytes */
342 iov_random(&iov
, &iov_cnt
);
343 iov_cnt_tmp
= iov_cnt
;
344 ret
= iov_discard_back(iov
, &iov_cnt_tmp
, 0);
346 g_assert(iov_cnt_tmp
== iov_cnt
);
347 iov_free(iov
, iov_cnt
);
349 /* Discard more bytes than vector size */
350 iov_random(&iov
, &iov_cnt
);
351 iov_cnt_tmp
= iov_cnt
;
352 size
= iov_size(iov
, iov_cnt
);
353 ret
= iov_discard_back(iov
, &iov_cnt_tmp
, size
+ 1);
354 g_assert(ret
== size
);
355 g_assert(iov_cnt_tmp
== 0);
356 iov_free(iov
, iov_cnt
);
358 /* Discard entire vector */
359 iov_random(&iov
, &iov_cnt
);
360 iov_cnt_tmp
= iov_cnt
;
361 size
= iov_size(iov
, iov_cnt
);
362 ret
= iov_discard_back(iov
, &iov_cnt_tmp
, size
);
363 g_assert(ret
== size
);
364 g_assert(iov_cnt_tmp
== 0);
365 iov_free(iov
, iov_cnt
);
367 /* Discard within last element */
368 iov_random(&iov
, &iov_cnt
);
369 iov_cnt_tmp
= iov_cnt
;
370 old_base
= iov
[iov_cnt
- 1].iov_base
;
371 size
= g_test_rand_int_range(1, iov
[iov_cnt
- 1].iov_len
);
372 ret
= iov_discard_back(iov
, &iov_cnt_tmp
, size
);
373 g_assert(ret
== size
);
374 g_assert(iov_cnt_tmp
== iov_cnt
);
375 g_assert(iov
[iov_cnt
- 1].iov_base
== old_base
);
376 iov_free(iov
, iov_cnt
);
378 /* Discard entire last element */
379 iov_random(&iov
, &iov_cnt
);
380 iov_cnt_tmp
= iov_cnt
;
381 old_base
= iov
[iov_cnt
- 1].iov_base
;
382 size
= iov
[iov_cnt
- 1].iov_len
;
383 ret
= iov_discard_back(iov
, &iov_cnt_tmp
, size
);
384 g_assert(ret
== size
);
385 g_assert(iov_cnt_tmp
== iov_cnt
- 1);
386 iov_free(iov
, iov_cnt
);
388 /* Discard within second-to-last element */
389 iov_random(&iov
, &iov_cnt
);
390 iov_cnt_tmp
= iov_cnt
;
391 old_base
= iov
[iov_cnt
- 2].iov_base
;
392 size
= iov
[iov_cnt
- 1].iov_len
+
393 g_test_rand_int_range(1, iov
[iov_cnt
- 2].iov_len
);
394 ret
= iov_discard_back(iov
, &iov_cnt_tmp
, size
);
395 g_assert(ret
== size
);
396 g_assert(iov_cnt_tmp
== iov_cnt
- 1);
397 g_assert(iov
[iov_cnt
- 2].iov_base
== old_base
);
398 iov_free(iov
, iov_cnt
);
401 int main(int argc
, char **argv
)
403 g_test_init(&argc
, &argv
, NULL
);
405 g_test_add_func("/basic/iov/from-to-buf", test_to_from_buf
);
406 g_test_add_func("/basic/iov/io", test_io
);
407 g_test_add_func("/basic/iov/discard-front", test_discard_front
);
408 g_test_add_func("/basic/iov/discard-back", test_discard_back
);