]> git.proxmox.com Git - ceph.git/blob - ceph/src/spdk/test/unit/lib/iscsi/tgt_node.c/tgt_node_ut.c
update source to Ceph Pacific 16.2.2
[ceph.git] / ceph / src / spdk / test / unit / lib / iscsi / tgt_node.c / tgt_node_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 "spdk/stdinc.h"
35
36 #include "spdk/scsi.h"
37
38 #include "CUnit/Basic.h"
39 #include "spdk_internal/mock.h"
40
41 #include "../common.c"
42 #include "iscsi/tgt_node.c"
43 #include "scsi/scsi_internal.h"
44 #include "unit/lib/json_mock.c"
45 #include "common/lib/test_env.c"
46
47 struct spdk_iscsi_globals g_iscsi;
48
49 const char *config_file;
50
51 DEFINE_STUB(spdk_scsi_dev_get_id,
52 int,
53 (const struct spdk_scsi_dev *dev),
54 0);
55
56 DEFINE_STUB(spdk_scsi_lun_get_bdev_name,
57 const char *,
58 (const struct spdk_scsi_lun *lun),
59 NULL);
60
61 DEFINE_STUB(spdk_scsi_lun_get_id,
62 int,
63 (const struct spdk_scsi_lun *lun),
64 0);
65
66 DEFINE_STUB_V(spdk_iscsi_op_abort_task_set,
67 (struct spdk_iscsi_task *task,
68 uint8_t function));
69
70 DEFINE_STUB(spdk_sock_is_ipv6, bool, (struct spdk_sock *sock), false);
71
72 DEFINE_STUB(spdk_sock_is_ipv4, bool, (struct spdk_sock *sock), false);
73
74 DEFINE_STUB(iscsi_portal_grp_find_by_tag,
75 struct spdk_iscsi_portal_grp *, (int tag), NULL);
76
77 DEFINE_STUB(iscsi_init_grp_find_by_tag, struct spdk_iscsi_init_grp *,
78 (int tag), NULL);
79
80 struct spdk_scsi_lun *
81 spdk_scsi_dev_get_lun(struct spdk_scsi_dev *dev, int lun_id)
82 {
83 if (lun_id < 0 || lun_id >= SPDK_SCSI_DEV_MAX_LUN) {
84 return NULL;
85 }
86
87 return dev->lun[lun_id];
88 }
89
90 int
91 spdk_scsi_dev_add_lun(struct spdk_scsi_dev *dev, const char *bdev_name, int lun_id,
92 void (*hotremove_cb)(const struct spdk_scsi_lun *, void *),
93 void *hotremove_ctx)
94 {
95 if (bdev_name == NULL) {
96 return -1;
97 } else {
98 return 0;
99 }
100 }
101
102 static void
103 add_lun_test_cases(void)
104 {
105 struct spdk_iscsi_tgt_node tgtnode = {};
106 int lun_id = 0;
107 char *bdev_name = NULL;
108 struct spdk_scsi_dev scsi_dev = {};
109 int rc;
110
111 /* case 1 */
112 tgtnode.num_active_conns = 1;
113
114 rc = iscsi_tgt_node_add_lun(&tgtnode, bdev_name, lun_id);
115 CU_ASSERT(rc != 0);
116
117 /* case 2 */
118 tgtnode.num_active_conns = 0;
119 lun_id = -2;
120
121 rc = iscsi_tgt_node_add_lun(&tgtnode, bdev_name, lun_id);
122 CU_ASSERT(rc != 0);
123
124 /* case 3 */
125 lun_id = SPDK_SCSI_DEV_MAX_LUN;
126
127 rc = iscsi_tgt_node_add_lun(&tgtnode, bdev_name, lun_id);
128 CU_ASSERT(rc != 0);
129
130 /* case 4 */
131 lun_id = -1;
132 tgtnode.dev = NULL;
133
134 rc = iscsi_tgt_node_add_lun(&tgtnode, bdev_name, lun_id);
135 CU_ASSERT(rc != 0);
136
137 /* case 5 */
138 tgtnode.dev = &scsi_dev;
139
140 rc = iscsi_tgt_node_add_lun(&tgtnode, bdev_name, lun_id);
141 CU_ASSERT(rc != 0);
142
143 /* case 6 */
144 bdev_name = "LUN0";
145
146 rc = iscsi_tgt_node_add_lun(&tgtnode, bdev_name, lun_id);
147 CU_ASSERT(rc == 0);
148 }
149
150 static void
151 config_file_fail_cases(void)
152 {
153 struct spdk_conf *config;
154 struct spdk_conf_section *sp;
155 char section_name[64];
156 int section_index;
157 int rc;
158
159 config = spdk_conf_allocate();
160
161 rc = spdk_conf_read(config, config_file);
162 CU_ASSERT(rc == 0);
163
164 section_index = 0;
165 while (true) {
166 snprintf(section_name, sizeof(section_name), "Failure%d", section_index);
167 sp = spdk_conf_find_section(config, section_name);
168 if (sp == NULL) {
169 break;
170 }
171 rc = iscsi_parse_tgt_node(sp);
172 CU_ASSERT(rc < 0);
173 section_index++;
174 }
175
176 spdk_conf_free(config);
177 }
178
179 static void
180 allow_any_allowed(void)
181 {
182 bool result;
183 char *netmask;
184 char *addr1, *addr2;
185
186 netmask = "ANY";
187 addr1 = "2001:ad6:1234:5678:9abc::";
188 addr2 = "192.168.2.1";
189
190 result = iscsi_netmask_allow_addr(netmask, addr1);
191 CU_ASSERT(result == true);
192
193 result = iscsi_netmask_allow_addr(netmask, addr2);
194 CU_ASSERT(result == true);
195 }
196
197 static void
198 allow_ipv6_allowed(void)
199 {
200 bool result;
201 char *netmask;
202 char *addr;
203
204 netmask = "[2001:ad6:1234::]/48";
205 addr = "2001:ad6:1234:5678:9abc::";
206
207 result = iscsi_ipv6_netmask_allow_addr(netmask, addr);
208 CU_ASSERT(result == true);
209
210 result = iscsi_netmask_allow_addr(netmask, addr);
211 CU_ASSERT(result == true);
212
213 /* Netmask prefix bits == 128 (all bits must match) */
214 netmask = "[2001:ad6:1234:5678:9abc::1]/128";
215 addr = "2001:ad6:1234:5678:9abc::1";
216 result = iscsi_ipv6_netmask_allow_addr(netmask, addr);
217 CU_ASSERT(result == true);
218 }
219
220 static void
221 allow_ipv6_denied(void)
222 {
223 bool result;
224 char *netmask;
225 char *addr;
226
227 netmask = "[2001:ad6:1234::]/56";
228 addr = "2001:ad6:1234:5678:9abc::";
229
230 result = iscsi_ipv6_netmask_allow_addr(netmask, addr);
231 CU_ASSERT(result == false);
232
233 result = iscsi_netmask_allow_addr(netmask, addr);
234 CU_ASSERT(result == false);
235
236 /* Netmask prefix bits == 128 (all bits must match) */
237 netmask = "[2001:ad6:1234:5678:9abc::1]/128";
238 addr = "2001:ad6:1234:5678:9abc::2";
239 result = iscsi_ipv6_netmask_allow_addr(netmask, addr);
240 CU_ASSERT(result == false);
241 }
242
243 static void
244 allow_ipv6_invalid(void)
245 {
246 bool result;
247 char *netmask;
248 char *addr;
249
250 /* Netmask prefix bits > 128 */
251 netmask = "[2001:ad6:1234::]/129";
252 addr = "2001:ad6:1234:5678:9abc::";
253 result = iscsi_ipv6_netmask_allow_addr(netmask, addr);
254 CU_ASSERT(result == false);
255
256 /* Netmask prefix bits == 0 */
257 netmask = "[2001:ad6:1234::]/0";
258 addr = "2001:ad6:1234:5678:9abc::";
259 result = iscsi_ipv6_netmask_allow_addr(netmask, addr);
260 CU_ASSERT(result == false);
261
262 /* Netmask prefix bits < 0 */
263 netmask = "[2001:ad6:1234::]/-1";
264 addr = "2001:ad6:1234:5678:9abc::";
265 result = iscsi_ipv6_netmask_allow_addr(netmask, addr);
266 CU_ASSERT(result == false);
267 }
268
269 static void
270 allow_ipv4_allowed(void)
271 {
272 bool result;
273 char *netmask;
274 char *addr;
275
276 netmask = "192.168.2.0/24";
277 addr = "192.168.2.1";
278
279 result = iscsi_ipv4_netmask_allow_addr(netmask, addr);
280 CU_ASSERT(result == true);
281
282 result = iscsi_netmask_allow_addr(netmask, addr);
283 CU_ASSERT(result == true);
284
285 /* Netmask prefix == 32 (all bits must match) */
286 netmask = "192.168.2.1/32";
287 addr = "192.168.2.1";
288 result = iscsi_ipv4_netmask_allow_addr(netmask, addr);
289 CU_ASSERT(result == true);
290 }
291
292 static void
293 allow_ipv4_denied(void)
294 {
295 bool result;
296 char *netmask;
297 char *addr;
298
299 netmask = "192.168.2.0";
300 addr = "192.168.2.1";
301
302 result = iscsi_ipv4_netmask_allow_addr(netmask, addr);
303 CU_ASSERT(result == false);
304
305 result = iscsi_netmask_allow_addr(netmask, addr);
306 CU_ASSERT(result == false);
307
308 /* Netmask prefix == 32 (all bits must match) */
309 netmask = "192.168.2.1/32";
310 addr = "192.168.2.2";
311 result = iscsi_ipv4_netmask_allow_addr(netmask, addr);
312 CU_ASSERT(result == false);
313 }
314
315 static void
316 allow_ipv4_invalid(void)
317 {
318 bool result;
319 char *netmask;
320 char *addr;
321
322 /* Netmask prefix bits > 32 */
323 netmask = "192.168.2.0/33";
324 addr = "192.168.2.1";
325 result = iscsi_ipv4_netmask_allow_addr(netmask, addr);
326 CU_ASSERT(result == false);
327
328 /* Netmask prefix bits == 0 */
329 netmask = "192.168.2.0/0";
330 addr = "192.168.2.1";
331 result = iscsi_ipv4_netmask_allow_addr(netmask, addr);
332 CU_ASSERT(result == false);
333
334 /* Netmask prefix bits < 0 */
335 netmask = "192.168.2.0/-1";
336 addr = "192.168.2.1";
337 result = iscsi_ipv4_netmask_allow_addr(netmask, addr);
338 CU_ASSERT(result == false);
339 }
340
341 static void
342 node_access_allowed(void)
343 {
344 struct spdk_iscsi_tgt_node tgtnode = {};
345 struct spdk_iscsi_portal_grp pg = {};
346 struct spdk_iscsi_init_grp ig = {};
347 struct spdk_iscsi_conn conn = {};
348 struct spdk_iscsi_portal portal = {};
349 struct spdk_iscsi_initiator_name iname = {};
350 struct spdk_iscsi_initiator_netmask imask = {};
351 struct spdk_scsi_dev scsi_dev = {};
352 struct spdk_iscsi_pg_map *pg_map;
353 char *iqn, *addr;
354 bool result;
355
356 /* portal group initialization */
357 pg.tag = 1;
358
359 /* initiator group initialization */
360 ig.tag = 1;
361
362 ig.ninitiators = 1;
363 snprintf(iname.name, sizeof(iname.name), "iqn.2017-10.spdk.io:0001");
364 TAILQ_INIT(&ig.initiator_head);
365 TAILQ_INSERT_TAIL(&ig.initiator_head, &iname, tailq);
366
367 ig.nnetmasks = 1;
368 snprintf(imask.mask, sizeof(imask.mask), "192.168.2.0/24");
369 TAILQ_INIT(&ig.netmask_head);
370 TAILQ_INSERT_TAIL(&ig.netmask_head, &imask, tailq);
371
372 /* target initialization */
373 snprintf(tgtnode.name, sizeof(tgtnode.name), "iqn.2017-10.spdk.io:0001");
374 TAILQ_INIT(&tgtnode.pg_map_head);
375
376 snprintf(scsi_dev.name, sizeof(scsi_dev.name), "iqn.2017-10.spdk.io:0001");
377 tgtnode.dev = &scsi_dev;
378
379 pg_map = iscsi_tgt_node_add_pg_map(&tgtnode, &pg);
380 iscsi_pg_map_add_ig_map(pg_map, &ig);
381
382 /* portal initialization */
383 portal.group = &pg;
384 snprintf(portal.host, sizeof(portal.host), "192.168.2.0");
385 snprintf(portal.port, sizeof(portal.port), "3260");
386
387 /* input for UT */
388 conn.portal = &portal;
389
390 iqn = "iqn.2017-10.spdk.io:0001";
391 addr = "192.168.2.1";
392
393 result = iscsi_tgt_node_access(&conn, &tgtnode, iqn, addr);
394 CU_ASSERT(result == true);
395
396 iscsi_pg_map_delete_ig_map(pg_map, &ig);
397 iscsi_tgt_node_delete_pg_map(&tgtnode, &pg);
398 }
399
400 static void
401 node_access_denied_by_empty_netmask(void)
402 {
403 struct spdk_iscsi_tgt_node tgtnode = {};
404 struct spdk_iscsi_portal_grp pg = {};
405 struct spdk_iscsi_init_grp ig = {};
406 struct spdk_iscsi_conn conn = {};
407 struct spdk_iscsi_portal portal = {};
408 struct spdk_iscsi_initiator_name iname = {};
409 struct spdk_scsi_dev scsi_dev = {};
410 struct spdk_iscsi_pg_map *pg_map;
411 char *iqn, *addr;
412 bool result;
413
414 /* portal group initialization */
415 pg.tag = 1;
416
417 /* initiator group initialization */
418 ig.tag = 1;
419
420 ig.ninitiators = 1;
421 snprintf(iname.name, sizeof(iname.name), "iqn.2017-10.spdk.io:0001");
422 TAILQ_INIT(&ig.initiator_head);
423 TAILQ_INSERT_TAIL(&ig.initiator_head, &iname, tailq);
424
425 ig.nnetmasks = 0;
426 TAILQ_INIT(&ig.netmask_head);
427
428 /* target initialization */
429 snprintf(tgtnode.name, sizeof(tgtnode.name), "iqn.2017-10.spdk.io:0001");
430 TAILQ_INIT(&tgtnode.pg_map_head);
431
432 snprintf(scsi_dev.name, sizeof(scsi_dev.name), "iqn.2017-10.spdk.io:0001");
433 tgtnode.dev = &scsi_dev;
434
435 pg_map = iscsi_tgt_node_add_pg_map(&tgtnode, &pg);
436 iscsi_pg_map_add_ig_map(pg_map, &ig);
437
438 /* portal initialization */
439 portal.group = &pg;
440 snprintf(portal.host, sizeof(portal.host), "192.168.2.0");
441 snprintf(portal.port, sizeof(portal.port), "3260");
442
443 /* input for UT */
444 conn.portal = &portal;
445
446 iqn = "iqn.2017-10.spdk.io:0001";
447 addr = "192.168.3.1";
448
449 result = iscsi_tgt_node_access(&conn, &tgtnode, iqn, addr);
450 CU_ASSERT(result == false);
451
452 iscsi_pg_map_delete_ig_map(pg_map, &ig);
453 iscsi_tgt_node_delete_pg_map(&tgtnode, &pg);
454 }
455
456 #define IQN1 "iqn.2017-11.spdk.io:0001"
457 #define NO_IQN1 "!iqn.2017-11.spdk.io:0001"
458 #define IQN2 "iqn.2017-11.spdk.io:0002"
459 #define IP1 "192.168.2.0"
460 #define IP2 "192.168.2.1"
461
462 static void
463 node_access_multi_initiator_groups_cases(void)
464 {
465 struct spdk_iscsi_tgt_node tgtnode = {};
466 struct spdk_iscsi_conn conn = {};
467 struct spdk_iscsi_portal_grp pg = {};
468 struct spdk_iscsi_portal portal = {};
469 struct spdk_iscsi_init_grp ig1 = {}, ig2 = {};
470 struct spdk_iscsi_initiator_name iname1 = {}, iname2 = {};
471 struct spdk_iscsi_initiator_netmask imask1 = {}, imask2 = {};
472 struct spdk_scsi_dev scsi_dev = {};
473 struct spdk_iscsi_pg_map *pg_map;
474 char *iqn, *addr;
475 bool result;
476
477 /* target initialization */
478 snprintf(tgtnode.name, sizeof(tgtnode.name), IQN1);
479 TAILQ_INIT(&tgtnode.pg_map_head);
480
481 snprintf(scsi_dev.name, sizeof(scsi_dev.name), IQN1);
482 tgtnode.dev = &scsi_dev;
483
484 /* initiator group initialization */
485 ig1.tag = 1;
486 TAILQ_INIT(&ig1.initiator_head);
487 TAILQ_INIT(&ig1.netmask_head);
488
489 ig1.ninitiators = 1;
490 TAILQ_INSERT_TAIL(&ig1.initiator_head, &iname1, tailq);
491
492 ig1.nnetmasks = 1;
493 TAILQ_INSERT_TAIL(&ig1.netmask_head, &imask1, tailq);
494
495 ig2.tag = 2;
496 TAILQ_INIT(&ig2.initiator_head);
497 TAILQ_INIT(&ig2.netmask_head);
498
499 ig2.ninitiators = 1;
500 TAILQ_INSERT_TAIL(&ig2.initiator_head, &iname2, tailq);
501
502 ig2.nnetmasks = 1;
503 TAILQ_INSERT_TAIL(&ig2.netmask_head, &imask2, tailq);
504
505 /* portal group initialization */
506 pg.tag = 1;
507
508 pg_map = iscsi_tgt_node_add_pg_map(&tgtnode, &pg);
509 iscsi_pg_map_add_ig_map(pg_map, &ig1);
510 iscsi_pg_map_add_ig_map(pg_map, &ig2);
511
512 /* portal initialization */
513 portal.group = &pg;
514 snprintf(portal.host, sizeof(portal.host), IP1);
515 snprintf(portal.port, sizeof(portal.port), "3260");
516
517 /* connection initialization */
518 conn.portal = &portal;
519
520 iqn = IQN1;
521 addr = IP1;
522
523 /*
524 * case 1:
525 * +-------------------------------------------+---------+
526 * | IG1 | IG2 | |
527 * +-------------------------------------------+ |
528 * | name | addr | name | addr | result |
529 * +-------------------------------------------+---------+
530 * +-------------------------------------------+---------+
531 * | denied | - | - | - | denied |
532 * +-------------------------------------------+---------+
533 */
534 snprintf(iname1.name, sizeof(iname1.name), NO_IQN1);
535
536 result = iscsi_tgt_node_access(&conn, &tgtnode, iqn, addr);
537 CU_ASSERT(result == false);
538
539 /*
540 * case 2:
541 * +-------------------------------------------+---------+
542 * | IG1 | IG2 | |
543 * +-------------------------------------------+ |
544 * | name | addr | name | addr | result |
545 * +-------------------------------------------+---------+
546 * +-------------------------------------------+---------+
547 * | allowed | allowed | - | - | allowed |
548 * +-------------------------------------------+---------+
549 */
550 snprintf(iname1.name, sizeof(iname1.name), IQN1);
551 snprintf(imask1.mask, sizeof(imask1.mask), IP1);
552
553 result = iscsi_tgt_node_access(&conn, &tgtnode, iqn, addr);
554 CU_ASSERT(result == true);
555
556 /*
557 * case 3:
558 * +-------------------------------------------+---------+
559 * | IG1 | IG2 | |
560 * +-------------------------------------------+ |
561 * | name | addr | name | addr | result |
562 * +-------------------------------------------+---------+
563 * +-------------------------------------------+---------+
564 * | allowed | denied | denied | - | denied |
565 * +-------------------------------------------+---------+
566 */
567 snprintf(iname1.name, sizeof(iname1.name), IQN1);
568 snprintf(imask1.mask, sizeof(imask1.mask), IP2);
569 snprintf(iname2.name, sizeof(iname2.name), NO_IQN1);
570
571 result = iscsi_tgt_node_access(&conn, &tgtnode, iqn, addr);
572 CU_ASSERT(result == false);
573
574 /*
575 * case 4:
576 * +-------------------------------------------+---------+
577 * | IG1 | IG2 | |
578 * +-------------------------------------------+ |
579 * | name | addr | name | addr | result |
580 * +-------------------------------------------+---------+
581 * +-------------------------------------------+---------+
582 * | allowed | denied | allowed | allowed | allowed |
583 * +-------------------------------------------+---------+
584 */
585 snprintf(iname1.name, sizeof(iname1.name), IQN1);
586 snprintf(imask1.mask, sizeof(imask1.mask), IP2);
587 snprintf(iname2.name, sizeof(iname2.name), IQN1);
588 snprintf(imask2.mask, sizeof(imask2.mask), IP1);
589
590 result = iscsi_tgt_node_access(&conn, &tgtnode, iqn, addr);
591 CU_ASSERT(result == true);
592
593 /*
594 * case 5:
595 * +---------------------------------------------+---------+
596 * | IG1 | IG2 | |
597 * +---------------------------------------------+ |
598 * | name | addr | name | addr | result |
599 * +---------------------------------------------+---------+
600 * +---------------------------------------------+---------+
601 * | allowed | denied | allowed | denied | denied |
602 * +---------------------------------------------+---------+
603 */
604 snprintf(iname1.name, sizeof(iname1.name), IQN1);
605 snprintf(imask1.mask, sizeof(imask1.mask), IP2);
606 snprintf(iname2.name, sizeof(iname2.name), IQN1);
607 snprintf(imask2.mask, sizeof(imask2.mask), IP2);
608
609 result = iscsi_tgt_node_access(&conn, &tgtnode, iqn, addr);
610 CU_ASSERT(result == false);
611
612 /*
613 * case 6:
614 * +---------------------------------------------+---------+
615 * | IG1 | IG2 | |
616 * +---------------------------------------------+ |
617 * | name | addr | name | addr | result |
618 * +---------------------------------------------+---------+
619 * +---------------------------------------------+---------+
620 * | allowed | denied | not found | - | denied |
621 * +---------------------------------------------+---------+
622 */
623 snprintf(iname1.name, sizeof(iname1.name), IQN1);
624 snprintf(imask1.mask, sizeof(imask1.mask), IP2);
625 snprintf(iname2.name, sizeof(iname2.name), IQN2);
626
627 result = iscsi_tgt_node_access(&conn, &tgtnode, iqn, addr);
628 CU_ASSERT(result == false);
629
630 /*
631 * case 7:
632 * +---------------------------------------------+---------+
633 * | IG1 | IG2 | |
634 * +---------------------------------------------+ |
635 * | name | addr | name | addr | result |
636 * +---------------------------------------------+---------+
637 * +---------------------------------------------+---------+
638 * | not found | - | denied | - | denied |
639 * +---------------------------------------------+---------+
640 */
641 snprintf(iname1.name, sizeof(iname1.name), IQN2);
642 snprintf(iname2.name, sizeof(iname2.name), NO_IQN1);
643
644 result = iscsi_tgt_node_access(&conn, &tgtnode, iqn, addr);
645 CU_ASSERT(result == false);
646
647 /*
648 * case 8:
649 * +---------------------------------------------+---------+
650 * | IG1 | IG2 | |
651 * +---------------------------------------------+ |
652 * | name | addr | name | addr | result |
653 * +---------------------------------------------+---------+
654 * +---------------------------------------------+---------+
655 * | not found | - | allowed | allowed | allowed |
656 * +---------------------------------------------+---------+
657 */
658 snprintf(iname1.name, sizeof(iname1.name), IQN2);
659 snprintf(iname2.name, sizeof(iname2.name), IQN1);
660 snprintf(imask2.mask, sizeof(imask2.mask), IP1);
661
662 result = iscsi_tgt_node_access(&conn, &tgtnode, iqn, addr);
663 CU_ASSERT(result == true);
664
665 /*
666 * case 9:
667 * +---------------------------------------------+---------+
668 * | IG1 | IG2 | |
669 * +---------------------------------------------+ |
670 * | name | addr | name | addr | result |
671 * +---------------------------------------------+---------+
672 * +---------------------------------------------+---------+
673 * | not found | - | allowed | denied | denied |
674 * +---------------------------------------------+---------+
675 */
676 snprintf(iname1.name, sizeof(iname1.name), IQN2);
677 snprintf(iname2.name, sizeof(iname2.name), IQN1);
678 snprintf(imask2.mask, sizeof(imask2.mask), IP2);
679
680 result = iscsi_tgt_node_access(&conn, &tgtnode, iqn, addr);
681 CU_ASSERT(result == false);
682
683 /*
684 * case 10:
685 * +---------------------------------------------+---------+
686 * | IG1 | IG2 | |
687 * +---------------------------------------------+ |
688 * | name | addr | name | addr | result |
689 * +---------------------------------------------+---------+
690 * +---------------------------------------------+---------+
691 * | not found | - | not found | - | denied |
692 * +---------------------------------------------+---------+
693 */
694 snprintf(iname1.name, sizeof(iname1.name), IQN2);
695 snprintf(iname2.name, sizeof(iname2.name), IQN2);
696
697 result = iscsi_tgt_node_access(&conn, &tgtnode, iqn, addr);
698 CU_ASSERT(result == false);
699
700 iscsi_pg_map_delete_ig_map(pg_map, &ig1);
701 iscsi_pg_map_delete_ig_map(pg_map, &ig2);
702 iscsi_tgt_node_delete_pg_map(&tgtnode, &pg);
703 }
704
705 static void
706 allow_iscsi_name_multi_maps_case(void)
707 {
708 struct spdk_iscsi_tgt_node tgtnode = {};
709 struct spdk_iscsi_portal_grp pg1 = {}, pg2 = {};
710 struct spdk_iscsi_init_grp ig = {};
711 struct spdk_iscsi_initiator_name iname = {};
712 struct spdk_iscsi_pg_map *pg_map1, *pg_map2;
713 struct spdk_scsi_dev scsi_dev = {};
714 char *iqn;
715 bool result;
716
717 /* target initialization */
718 TAILQ_INIT(&tgtnode.pg_map_head);
719
720 snprintf(scsi_dev.name, sizeof(scsi_dev.name), IQN1);
721 tgtnode.dev = &scsi_dev;
722
723 /* initiator group initialization */
724 TAILQ_INIT(&ig.initiator_head);
725
726 ig.ninitiators = 1;
727 TAILQ_INSERT_TAIL(&ig.initiator_head, &iname, tailq);
728
729 /* portal group initialization */
730 pg1.tag = 1;
731 pg2.tag = 1;
732
733 pg_map1 = iscsi_tgt_node_add_pg_map(&tgtnode, &pg1);
734 pg_map2 = iscsi_tgt_node_add_pg_map(&tgtnode, &pg2);
735 iscsi_pg_map_add_ig_map(pg_map1, &ig);
736 iscsi_pg_map_add_ig_map(pg_map2, &ig);
737
738 /* test for IG1 <-> PG1, PG2 case */
739 iqn = IQN1;
740
741 snprintf(iname.name, sizeof(iname.name), IQN1);
742
743 result = iscsi_tgt_node_allow_iscsi_name(&tgtnode, iqn);
744 CU_ASSERT(result == true);
745
746 snprintf(iname.name, sizeof(iname.name), IQN2);
747
748 result = iscsi_tgt_node_allow_iscsi_name(&tgtnode, iqn);
749 CU_ASSERT(result == false);
750
751 iscsi_pg_map_delete_ig_map(pg_map1, &ig);
752 iscsi_pg_map_delete_ig_map(pg_map2, &ig);
753 iscsi_tgt_node_delete_pg_map(&tgtnode, &pg1);
754 iscsi_tgt_node_delete_pg_map(&tgtnode, &pg2);
755 }
756
757 /*
758 * static bool
759 * iscsi_check_chap_params(bool disable_chap, bool require_chap,
760 * bool mutual_chap, int chap_group);
761 */
762 static void
763 chap_param_test_cases(void)
764 {
765 /* Auto */
766 CU_ASSERT(iscsi_check_chap_params(false, false, false, 0) == true);
767
768 /* None */
769 CU_ASSERT(iscsi_check_chap_params(true, false, false, 0) == true);
770
771 /* CHAP */
772 CU_ASSERT(iscsi_check_chap_params(false, true, false, 0) == true);
773
774 /* CHAP Mutual */
775 CU_ASSERT(iscsi_check_chap_params(false, true, true, 0) == true);
776
777 /* Check mutual exclusiveness of disabled and required */
778 CU_ASSERT(iscsi_check_chap_params(true, true, false, 0) == false);
779
780 /* Mutual requires Required */
781 CU_ASSERT(iscsi_check_chap_params(false, false, true, 0) == false);
782
783 /* Remaining combinations */
784 CU_ASSERT(iscsi_check_chap_params(true, false, true, 0) == false);
785 CU_ASSERT(iscsi_check_chap_params(true, true, true, 0) == false);
786
787 /* Valid auth group ID */
788 CU_ASSERT(iscsi_check_chap_params(false, false, false, 1) == true);
789
790 /* Invalid auth group ID */
791 CU_ASSERT(iscsi_check_chap_params(false, false, false, -1) == false);
792 }
793
794 int
795 main(int argc, char **argv)
796 {
797 CU_pSuite suite = NULL;
798 unsigned int num_failures;
799
800 if (argc < 2) {
801 fprintf(stderr, "usage: %s <config file>\n", argv[0]);
802 exit(1);
803 }
804
805 CU_set_error_action(CUEA_ABORT);
806 CU_initialize_registry();
807
808 config_file = argv[1];
809
810 suite = CU_add_suite("iscsi_target_node_suite", NULL, NULL);
811
812 CU_ADD_TEST(suite, add_lun_test_cases);
813 CU_ADD_TEST(suite, config_file_fail_cases);
814 CU_ADD_TEST(suite, allow_any_allowed);
815 CU_ADD_TEST(suite, allow_ipv6_allowed);
816 CU_ADD_TEST(suite, allow_ipv6_denied);
817 CU_ADD_TEST(suite, allow_ipv6_invalid);
818 CU_ADD_TEST(suite, allow_ipv4_allowed);
819 CU_ADD_TEST(suite, allow_ipv4_denied);
820 CU_ADD_TEST(suite, allow_ipv4_invalid);
821 CU_ADD_TEST(suite, node_access_allowed);
822 CU_ADD_TEST(suite, node_access_denied_by_empty_netmask);
823 CU_ADD_TEST(suite, node_access_multi_initiator_groups_cases);
824 CU_ADD_TEST(suite, allow_iscsi_name_multi_maps_case);
825 CU_ADD_TEST(suite, chap_param_test_cases);
826
827 CU_basic_set_mode(CU_BRM_VERBOSE);
828 CU_basic_run_tests();
829 num_failures = CU_get_number_of_failures();
830 CU_cleanup_registry();
831 return num_failures;
832 }