xref: /spdk/test/unit/lib/util/iov.c/iov_ut.c (revision b30d57cdad6d2bc75cc1e4e2ebbcebcb0d98dcfa)
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/iov.c"
39 
40 static int
41 _check_val(void *buf, size_t len, uint8_t val)
42 {
43 	size_t i;
44 	uint8_t *data = buf;
45 
46 	for (i = 0; i < len; i++) {
47 		if (data[i] != val) {
48 			return -1;
49 		}
50 	}
51 
52 	return 0;
53 }
54 
55 static void
56 test_single_iov(void)
57 {
58 	struct iovec siov[1];
59 	struct iovec diov[1];
60 	uint8_t sdata[64];
61 	uint8_t ddata[64];
62 	ssize_t rc;
63 
64 	/* Simplest cases- 1 element in each iovec. */
65 
66 	/* Same size. */
67 	memset(sdata, 1, sizeof(sdata));
68 	memset(ddata, 0, sizeof(ddata));
69 	siov[0].iov_base = sdata;
70 	siov[0].iov_len = sizeof(sdata);
71 	diov[0].iov_base = ddata;
72 	diov[0].iov_len = sizeof(ddata);
73 
74 	rc = spdk_iovcpy(siov, 1, diov, 1);
75 	CU_ASSERT(rc == sizeof(sdata));
76 	CU_ASSERT(_check_val(ddata, 64, 1) == 0);
77 
78 	/* Source smaller than dest */
79 	memset(sdata, 1, sizeof(sdata));
80 	memset(ddata, 0, sizeof(ddata));
81 	siov[0].iov_base = sdata;
82 	siov[0].iov_len = 48;
83 	diov[0].iov_base = ddata;
84 	diov[0].iov_len = sizeof(ddata);
85 
86 	rc = spdk_iovcpy(siov, 1, diov, 1);
87 	CU_ASSERT(rc == 48);
88 	CU_ASSERT(_check_val(ddata, 48, 1) == 0);
89 	CU_ASSERT(_check_val(&ddata[48], 16, 0) == 0);
90 
91 	/* Dest smaller than source */
92 	memset(sdata, 1, sizeof(sdata));
93 	memset(ddata, 0, sizeof(ddata));
94 	siov[0].iov_base = sdata;
95 	siov[0].iov_len = sizeof(sdata);
96 	diov[0].iov_base = ddata;
97 	diov[0].iov_len = 48;
98 
99 	rc = spdk_iovcpy(siov, 1, diov, 1);
100 	CU_ASSERT(rc == 48);
101 	CU_ASSERT(_check_val(ddata, 48, 1) == 0);
102 	CU_ASSERT(_check_val(&ddata[48], 16, 0) == 0);
103 }
104 
105 static void
106 test_simple_iov(void)
107 {
108 	struct iovec siov[4];
109 	struct iovec diov[4];
110 	uint8_t sdata[64];
111 	uint8_t ddata[64];
112 	ssize_t rc;
113 	int i;
114 
115 	/* Simple cases with 4 iov elements */
116 
117 	/* Same size. */
118 	memset(sdata, 1, sizeof(sdata));
119 	memset(ddata, 0, sizeof(ddata));
120 	for (i = 0; i < 4; i++) {
121 		siov[i].iov_base = sdata + (16 * i);
122 		siov[i].iov_len = 16;
123 		diov[i].iov_base = ddata + (16 * i);
124 		diov[i].iov_len = 16;
125 	}
126 
127 	rc = spdk_iovcpy(siov, 4, diov, 4);
128 	CU_ASSERT(rc == sizeof(sdata));
129 	CU_ASSERT(_check_val(ddata, 64, 1) == 0);
130 
131 	/* Source smaller than dest */
132 	memset(sdata, 1, sizeof(sdata));
133 	memset(ddata, 0, sizeof(ddata));
134 	for (i = 0; i < 4; i++) {
135 		siov[i].iov_base = sdata + (8 * i);
136 		siov[i].iov_len = 8;
137 		diov[i].iov_base = ddata + (16 * i);
138 		diov[i].iov_len = 16;
139 	}
140 
141 	rc = spdk_iovcpy(siov, 4, diov, 4);
142 	CU_ASSERT(rc == 32);
143 	CU_ASSERT(_check_val(ddata, 32, 1) == 0);
144 	CU_ASSERT(_check_val(&ddata[32], 32, 0) == 0);
145 
146 	/* Dest smaller than source */
147 	memset(sdata, 1, sizeof(sdata));
148 	memset(ddata, 0, sizeof(ddata));
149 	for (i = 0; i < 4; i++) {
150 		siov[i].iov_base = sdata + (16 * i);
151 		siov[i].iov_len = 16;
152 		diov[i].iov_base = ddata + (8 * i);
153 		diov[i].iov_len = 8;
154 	}
155 
156 	rc = spdk_iovcpy(siov, 4, diov, 4);
157 	CU_ASSERT(rc == 32);
158 	CU_ASSERT(_check_val(ddata, 32, 1) == 0);
159 	CU_ASSERT(_check_val(&ddata[32], 32, 0) == 0);
160 }
161 
162 static void
163 test_complex_iov(void)
164 {
165 	struct iovec siov[4];
166 	struct iovec diov[4];
167 	uint8_t sdata[64];
168 	uint8_t ddata[64];
169 	ssize_t rc;
170 	int i;
171 
172 	/* More source elements */
173 	memset(sdata, 1, sizeof(sdata));
174 	memset(ddata, 0, sizeof(ddata));
175 	for (i = 0; i < 4; i++) {
176 		siov[i].iov_base = sdata + (16 * i);
177 		siov[i].iov_len = 16;
178 	}
179 	diov[0].iov_base = ddata;
180 	diov[0].iov_len = sizeof(ddata);
181 
182 	rc = spdk_iovcpy(siov, 4, diov, 1);
183 	CU_ASSERT(rc == sizeof(sdata));
184 	CU_ASSERT(_check_val(ddata, 64, 1) == 0);
185 
186 	/* More dest elements */
187 	memset(sdata, 1, sizeof(sdata));
188 	memset(ddata, 0, sizeof(ddata));
189 	for (i = 0; i < 4; i++) {
190 		diov[i].iov_base = ddata + (16 * i);
191 		diov[i].iov_len = 16;
192 	}
193 	siov[0].iov_base = sdata;
194 	siov[0].iov_len = sizeof(sdata);
195 
196 	rc = spdk_iovcpy(siov, 1, diov, 4);
197 	CU_ASSERT(rc == sizeof(sdata));
198 	CU_ASSERT(_check_val(ddata, 64, 1) == 0);
199 
200 	/* Build one by hand that's really terrible */
201 	memset(sdata, 1, sizeof(sdata));
202 	memset(ddata, 0, sizeof(ddata));
203 	siov[0].iov_base = sdata;
204 	siov[0].iov_len = 1;
205 	siov[1].iov_base = siov[0].iov_base + siov[0].iov_len;
206 	siov[1].iov_len = 13;
207 	siov[2].iov_base = siov[1].iov_base + siov[1].iov_len;
208 	siov[2].iov_len = 6;
209 	siov[3].iov_base = siov[2].iov_base + siov[2].iov_len;
210 	siov[3].iov_len = 44;
211 
212 	diov[0].iov_base = ddata;
213 	diov[0].iov_len = 31;
214 	diov[1].iov_base = diov[0].iov_base + diov[0].iov_len;
215 	diov[1].iov_len = 9;
216 	diov[2].iov_base = diov[1].iov_base + diov[1].iov_len;
217 	diov[2].iov_len = 1;
218 	diov[3].iov_base = diov[2].iov_base + diov[2].iov_len;
219 	diov[3].iov_len = 23;
220 
221 	rc = spdk_iovcpy(siov, 4, diov, 4);
222 	CU_ASSERT(rc == 64);
223 	CU_ASSERT(_check_val(ddata, 64, 1) == 0);
224 }
225 
226 int
227 main(int argc, char **argv)
228 {
229 	CU_pSuite	suite = NULL;
230 	unsigned int	num_failures;
231 
232 	CU_set_error_action(CUEA_ABORT);
233 	CU_initialize_registry();
234 
235 	suite = CU_add_suite("iov", NULL, NULL);
236 
237 	CU_ADD_TEST(suite, test_single_iov);
238 	CU_ADD_TEST(suite, test_simple_iov);
239 	CU_ADD_TEST(suite, test_complex_iov);
240 
241 	CU_basic_set_mode(CU_BRM_VERBOSE);
242 
243 	CU_basic_run_tests();
244 
245 	num_failures = CU_get_number_of_failures();
246 	CU_cleanup_registry();
247 
248 	return num_failures;
249 }
250