]> git.proxmox.com Git - ceph.git/blob - ceph/src/spdk/test/lib/scsi/init/init_ut.c
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / spdk / test / lib / scsi / init / init_ut.c
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 <stdint.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <stdbool.h>
38
39 #include "spdk/event.h"
40 #include "spdk/scsi.h"
41
42 #include "spdk_cunit.h"
43
44 #include "scsi.c"
45
46 /* Unit test stubbed bdev subsystem dependency */
47 SPDK_SUBSYSTEM_REGISTER(bdev, NULL, NULL, NULL)
48
49 static int
50 null_init(void)
51 {
52 return 0;
53 }
54
55 static int
56 null_clean(void)
57 {
58 return 0;
59 }
60
61 void
62 spdk_add_subsystem(struct spdk_subsystem *subsystem)
63 {
64 }
65
66 void
67 spdk_add_subsystem_depend(struct spdk_subsystem_depend *depend)
68 {
69 }
70
71 static struct spdk_conf *
72 spdk_config_init_scsi_params(char *key, char *value)
73 {
74 struct spdk_conf *spdk_config;
75 FILE *f;
76 int fd, rc;
77 char filename[] = "/tmp/scsi_init_ut.XXXXXX";
78
79 /* Create temporary file to hold config */
80 fd = mkstemp(filename);
81 SPDK_CU_ASSERT_FATAL(fd != -1);
82
83 f = fdopen(fd, "wb+");
84 SPDK_CU_ASSERT_FATAL(f != NULL);
85
86 fprintf(f, "[Scsi]\n");
87 fprintf(f, "%s %s\n", key, value);
88
89 fclose(f);
90
91 spdk_config = spdk_conf_allocate();
92 SPDK_CU_ASSERT_FATAL(spdk_config != NULL);
93
94 rc = spdk_conf_read(spdk_config, filename);
95 SPDK_CU_ASSERT_FATAL(rc == 0);
96
97 spdk_conf_set_as_default(spdk_config);
98
99 remove(filename);
100
101 return spdk_config;
102 }
103
104 static void
105 set_default_scsi_params(struct spdk_scsi_parameters *params)
106 {
107 memset(params, 0, sizeof(*params));
108 params->max_unmap_lba_count = DEFAULT_MAX_UNMAP_LBA_COUNT;
109 params->max_unmap_block_descriptor_count = DEFAULT_MAX_UNMAP_BLOCK_DESCRIPTOR_COUNT;
110 params->optimal_unmap_granularity = DEFAULT_OPTIMAL_UNMAP_GRANULARITY;
111 params->unmap_granularity_alignment = DEFAULT_UNMAP_GRANULARITY_ALIGNMENT;
112 params->ugavalid = DEFAULT_UGAVALID;
113 params->max_write_same_length = DEFAULT_MAX_WRITE_SAME_LENGTH;
114 }
115
116 static void
117 scsi_init_sp_null(void)
118 {
119 struct spdk_conf *config;
120 int rc;
121
122 config = spdk_conf_allocate();
123 SPDK_CU_ASSERT_FATAL(config != NULL);
124
125 spdk_conf_set_as_default(config);
126
127 rc = spdk_scsi_subsystem_init();
128
129 /* sp = null; set default scsi params */
130 CU_ASSERT_EQUAL(rc, 0);
131
132 spdk_conf_set_as_default(NULL);
133
134 spdk_conf_free(config);
135 }
136
137 static void
138 scsi_init_set_max_unmap_lba_count_config_param(void)
139 {
140 struct spdk_scsi_parameters params;
141 struct spdk_conf *config;
142 int rc;
143
144 /* set scsi_params.max_unmap_lba_count = 65536 of Scsi section*/
145 config = spdk_config_init_scsi_params("MaxUnmapLbaCount", "65536");
146 spdk_conf_set_as_default(config);
147 rc = spdk_scsi_subsystem_init();
148
149 /* Assert the scsi_params.max_unmap_lba_count == 65536 and
150 * assert the rest of the params are set to their default values */
151 set_default_scsi_params(&params);
152 params.max_unmap_lba_count = 65536;
153 CU_ASSERT(memcmp(&g_spdk_scsi.scsi_params, &params, sizeof(params)) == 0);
154 CU_ASSERT_EQUAL(rc, 0);
155
156 spdk_conf_free(config);
157 }
158
159 static void
160 scsi_init_set_max_unmap_block_descriptor_count_config_param(void)
161 {
162 struct spdk_scsi_parameters params;
163 struct spdk_conf *config;
164 int rc;
165
166 /* set scsi_params.max_unmap_block_descriptor_count = 1
167 * of Scsi section*/
168 config = spdk_config_init_scsi_params("MaxUnmapBlockDescriptorCount", "1");
169 spdk_conf_set_as_default(config);
170 rc = spdk_scsi_subsystem_init();
171
172 /* Assert the scsi_params.max_unmap_block_descriptor_count == 1 and
173 * assert the rest of the params are set to their default values */
174 set_default_scsi_params(&params);
175 params.max_unmap_block_descriptor_count = 1;
176 CU_ASSERT(memcmp(&g_spdk_scsi.scsi_params, &params, sizeof(params)) == 0);
177 CU_ASSERT_EQUAL(rc, 0);
178
179 spdk_conf_free(config);
180 }
181
182 static void
183 scsi_init_set_optimal_unmap_granularity_config_param(void)
184 {
185 struct spdk_scsi_parameters params;
186 struct spdk_conf *config;
187 int rc;
188
189 /* set scsi_params.optimal_unmap_granularity = 0
190 * of Scsi section*/
191 config = spdk_config_init_scsi_params("OptimalUnmapGranularity", "0");
192 spdk_conf_set_as_default(config);
193 rc = spdk_scsi_subsystem_init();
194
195 /* Assert the scsi_params.optimal_unmap_granularity == 0 and
196 * assert the rest of the params are set to their default values */
197 set_default_scsi_params(&params);
198 params.optimal_unmap_granularity = 0;
199 CU_ASSERT(memcmp(&g_spdk_scsi.scsi_params, &params, sizeof(params)) == 0);
200 CU_ASSERT_EQUAL(rc, 0);
201
202 spdk_conf_free(config);
203 }
204
205 static void
206 scsi_init_set_unmap_granularity_alignment_config_param(void)
207 {
208 struct spdk_scsi_parameters params;
209 struct spdk_conf *config;
210 int rc;
211
212 /* set scsi_params.unmap_granularity_alignment = 0
213 * of Scsi section*/
214 config = spdk_config_init_scsi_params("UnmapGranularityAlignment", "0");
215 spdk_conf_set_as_default(config);
216 rc = spdk_scsi_subsystem_init();
217
218 /* Assert the scsi_params.unmap_granularity_alignment == 0 and
219 * assert the rest of the params are set to their default values */
220 set_default_scsi_params(&params);
221 params.unmap_granularity_alignment = 0;
222 CU_ASSERT(memcmp(&g_spdk_scsi.scsi_params, &params, sizeof(params)) == 0);
223 CU_ASSERT_EQUAL(rc, 0);
224
225 spdk_conf_free(config);
226 }
227
228 static void
229 scsi_init_ugavalid_yes(void)
230 {
231 struct spdk_scsi_parameters params;
232 struct spdk_conf *config;
233 int rc;
234
235 /* set scsi_params.ugavalid = Yes
236 * of Scsi section*/
237 config = spdk_config_init_scsi_params("Ugavalid", "Yes");
238 spdk_conf_set_as_default(config);
239 rc = spdk_scsi_subsystem_init();
240
241 /* Assert the scsi_params.ugavalid == 1 and
242 * assert the rest of the params are set to their default values */
243 set_default_scsi_params(&params);
244 params.ugavalid = 1;
245 CU_ASSERT(memcmp(&g_spdk_scsi.scsi_params, &params, sizeof(params)) == 0);
246 CU_ASSERT_EQUAL(rc, 0);
247
248 spdk_conf_free(config);
249 }
250
251 static void
252 scsi_init_ugavalid_no(void)
253 {
254 struct spdk_scsi_parameters params;
255 struct spdk_conf *config;
256 int rc;
257
258 /* set scsi_params.ugavalid = No
259 * of Scsi section*/
260 config = spdk_config_init_scsi_params("Ugavalid", "No");
261 spdk_conf_set_as_default(config);
262 rc = spdk_scsi_subsystem_init();
263
264 /* Assert the scsi_params.ugavalid == 0 and
265 * assert the rest of the params are set to their default values */
266 set_default_scsi_params(&params);
267 params.ugavalid = 0;
268 CU_ASSERT(memcmp(&g_spdk_scsi.scsi_params, &params, sizeof(params)) == 0);
269 CU_ASSERT_EQUAL(rc, 0);
270
271 spdk_conf_free(config);
272 }
273
274 static void
275 scsi_init_ugavalid_unknown_value_failure(void)
276 {
277 struct spdk_scsi_parameters params;
278 int rc;
279 struct spdk_conf *config;
280
281 /* set scsi_params.ugavalid = unknown value
282 * of Scsi section*/
283 config = spdk_config_init_scsi_params("Ugavalid", "unknown value");
284 spdk_conf_set_as_default(config);
285 rc = spdk_scsi_subsystem_init();
286 CU_ASSERT_EQUAL(rc, 0);
287
288 /* Assert the scsi_params.ugavalid == DEFAULT_UGAVALID and
289 * assert the rest of the params are set to their default values */
290 set_default_scsi_params(&params);
291 params.ugavalid = DEFAULT_UGAVALID;
292 CU_ASSERT(memcmp(&g_spdk_scsi.scsi_params, &params, sizeof(params)) == 0);
293
294 spdk_conf_free(config);
295 }
296
297 static void
298 scsi_init_max_write_same_length(void)
299 {
300 struct spdk_scsi_parameters params;
301 struct spdk_conf *config;
302 int rc;
303
304 /* set scsi_params.max_write_same_length = 512
305 * of Scsi section*/
306 config = spdk_config_init_scsi_params("MaxWriteSameLength", "512");
307 spdk_conf_set_as_default(config);
308 rc = spdk_scsi_subsystem_init();
309
310 /* Assert the scsi_params.max_write_same_length == 512 and
311 * assert the rest of the params are set to their default values */
312 set_default_scsi_params(&params);
313 params.max_write_same_length = 512;
314 CU_ASSERT(memcmp(&g_spdk_scsi.scsi_params, &params, sizeof(params)) == 0);
315 CU_ASSERT_EQUAL(rc, 0);
316
317 spdk_conf_free(config);
318 }
319
320 static void
321 scsi_init_read_config_scsi_params(void)
322 {
323 struct spdk_scsi_parameters params;
324 struct spdk_conf *config;
325 int rc;
326
327 /* Set null for item's key and value;
328 * set default scsi parameters */
329 config = spdk_config_init_scsi_params("", "");
330 spdk_conf_set_as_default(config);
331 rc = spdk_scsi_subsystem_init();
332
333 /* Sets the default values for all the parameters
334 * of the Scsi section and returns success */
335 set_default_scsi_params(&params);
336 CU_ASSERT(memcmp(&g_spdk_scsi.scsi_params, &params, sizeof(params)) == 0);
337 CU_ASSERT_EQUAL(rc, 0);
338
339 spdk_conf_free(config);
340 }
341
342 static void
343 scsi_init_success(void)
344 {
345 struct spdk_scsi_parameters params;
346 struct spdk_conf *config;
347 int rc;
348
349 /* Set null for item's key and value;
350 * set default scsi parameters */
351 config = spdk_config_init_scsi_params("", "");
352 spdk_conf_set_as_default(config);
353 rc = spdk_scsi_subsystem_init();
354
355 /* Sets the default values for all the parameters
356 * of the Scsi section, initialize th device
357 * and returns success */
358 set_default_scsi_params(&params);
359 CU_ASSERT(memcmp(&g_spdk_scsi.scsi_params, &params, sizeof(params)) == 0);
360 CU_ASSERT_EQUAL(rc, 0);
361
362 spdk_conf_free(config);
363 }
364
365 int
366 main(int argc, char **argv)
367 {
368 CU_pSuite suite = NULL;
369 unsigned int num_failures;
370
371 if (CU_initialize_registry() != CUE_SUCCESS) {
372 return CU_get_error();
373 }
374
375 suite = CU_add_suite("scsi_suite", null_init, null_clean);
376 if (suite == NULL) {
377 CU_cleanup_registry();
378 return CU_get_error();
379 }
380
381 if (
382 CU_add_test(suite, "scsi init - set default scsi params", \
383 scsi_init_sp_null) == NULL
384 || CU_add_test(suite, "scsi init - set max_unmap_lba_count", \
385 scsi_init_set_max_unmap_lba_count_config_param) == NULL
386 || CU_add_test(suite, "scsi init - set max_unmap_block_descriptor_count", \
387 scsi_init_set_max_unmap_block_descriptor_count_config_param) == NULL
388 || CU_add_test(suite, "scsi init - set optimal_unmap_granularity", \
389 scsi_init_set_optimal_unmap_granularity_config_param) == NULL
390 || CU_add_test(suite, "scsi init - set unmap_granularity_alignment", \
391 scsi_init_set_unmap_granularity_alignment_config_param) == NULL
392 || CU_add_test(suite, "scsi init - ugavalid value yes", \
393 scsi_init_ugavalid_yes) == NULL
394 || CU_add_test(suite, "scsi init - ugavalid value no", \
395 scsi_init_ugavalid_no) == NULL
396 || CU_add_test(suite, "scsi init - ugavalid unknown value", \
397 scsi_init_ugavalid_unknown_value_failure) == NULL
398 || CU_add_test(suite, "scsi init - set max_write_same_length", \
399 scsi_init_max_write_same_length) == NULL
400 || CU_add_test(suite, "scsi init - read config scsi parameters", \
401 scsi_init_read_config_scsi_params) == NULL
402 || CU_add_test(suite, "scsi init - success", scsi_init_success) == NULL
403 ) {
404 CU_cleanup_registry();
405 return CU_get_error();
406 }
407
408 CU_basic_set_mode(CU_BRM_VERBOSE);
409 CU_basic_run_tests();
410 num_failures = CU_get_number_of_failures();
411 CU_cleanup_registry();
412 return num_failures;
413 }