]>
git.proxmox.com Git - qemu.git/blob - tests/test-iov.c
2 #include "qemu-common.h"
5 /* create a randomly-sized iovec with random vectors */
6 static void iov_random(struct iovec
**iovp
, unsigned *iov_cntp
)
8 unsigned niov
= g_test_rand_int_range(3,8);
9 struct iovec
*iov
= g_malloc(niov
* sizeof(*iov
));
11 for (i
= 0; i
< niov
; ++i
) {
12 iov
[i
].iov_len
= g_test_rand_int_range(5,20);
13 iov
[i
].iov_base
= g_malloc(iov
[i
].iov_len
);
19 static void iov_free(struct iovec
*iov
, unsigned niov
)
22 for (i
= 0; i
< niov
; ++i
) {
23 g_free(iov
[i
].iov_base
);
28 static void test_iov_bytes(struct iovec
*iov
, unsigned niov
,
29 size_t offset
, size_t bytes
)
36 /* we walk over all elements, */
37 for (i
= 0; i
< niov
; ++i
) {
39 /* over each char of each element, */
40 for (j
= 0; j
< iov
[i
].iov_len
; ++j
) {
41 /* counting each of them and
42 * verifying that the ones within [offset,offset+bytes)
43 * range are equal to the position number (o) */
44 if (o
>= offset
&& o
< offset
+ bytes
) {
45 g_assert(b
[j
] == (o
& 255));
47 g_assert(b
[j
] == 0xff);
54 static void test_to_from_buf_1(void)
59 unsigned char *ibuf
, *obuf
;
62 iov_random(&iov
, &niov
);
64 sz
= iov_size(iov
, niov
);
66 ibuf
= g_malloc(sz
+ 8) + 4;
67 memcpy(ibuf
-4, "aaaa", 4); memcpy(ibuf
+ sz
, "bbbb", 4);
68 obuf
= g_malloc(sz
+ 8) + 4;
69 memcpy(obuf
-4, "xxxx", 4); memcpy(obuf
+ sz
, "yyyy", 4);
71 /* fill in ibuf with 0123456... */
72 for (i
= 0; i
< sz
; ++i
) {
76 for (i
= 0; i
<= sz
; ++i
) {
78 /* Test from/to buf for offset(i) in [0..sz] up to the end of buffer.
79 * For last iteration with offset == sz, the procedure should
80 * skip whole vector and process exactly 0 bytes */
82 /* first set bytes [i..sz) to some "random" value */
83 n
= iov_memset(iov
, niov
, 0, 0xff, -1);
86 /* next copy bytes [i..sz) from ibuf to iovec */
87 n
= iov_from_buf(iov
, niov
, i
, ibuf
+ i
, -1);
88 g_assert(n
== sz
- i
);
90 /* clear part of obuf */
91 memset(obuf
+ i
, 0, sz
- i
);
92 /* and set this part of obuf to values from iovec */
93 n
= iov_to_buf(iov
, niov
, i
, obuf
+ i
, -1);
94 g_assert(n
== sz
- i
);
96 /* now compare resulting buffers */
97 g_assert(memcmp(ibuf
, obuf
, sz
) == 0);
99 /* test just one char */
100 n
= iov_to_buf(iov
, niov
, i
, obuf
+ i
, 1);
101 g_assert(n
== (i
< sz
));
103 g_assert(obuf
[i
] == (i
& 255));
106 for (j
= i
; j
<= sz
; ++j
) {
107 /* now test num of bytes cap up to byte no. j,
108 * with j in [i..sz]. */
111 n
= iov_memset(iov
, niov
, 0, 0xff, -1);
114 /* copy bytes [i..j) from ibuf to iovec */
115 n
= iov_from_buf(iov
, niov
, i
, ibuf
+ i
, j
- i
);
116 g_assert(n
== j
- i
);
118 /* clear part of obuf */
119 memset(obuf
+ i
, 0, j
- i
);
121 /* copy bytes [i..j) from iovec to obuf */
122 n
= iov_to_buf(iov
, niov
, i
, obuf
+ i
, j
- i
);
123 g_assert(n
== j
- i
);
126 g_assert(memcmp(ibuf
, obuf
, sz
) == 0);
128 /* now actually check if the iovec contains the right data */
129 test_iov_bytes(iov
, niov
, i
, j
- i
);
132 g_assert(!memcmp(ibuf
-4, "aaaa", 4) && !memcmp(ibuf
+sz
, "bbbb", 4));
134 g_assert(!memcmp(obuf
-4, "xxxx", 4) && !memcmp(obuf
+sz
, "yyyy", 4));
139 static void test_to_from_buf(void)
142 for (x
= 0; x
< 4; ++x
) {
143 test_to_from_buf_1();
147 int main(int argc
, char **argv
)
149 g_test_init(&argc
, &argv
, NULL
);
151 g_test_add_func("/basic/iov/from-to-buf", test_to_from_buf
);