diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 18:45:59 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 18:45:59 +0000 |
commit | 19fcec84d8d7d21e796c7624e521b60d28ee21ed (patch) | |
tree | 42d26aa27d1e3f7c0b8bd3fd14e7d7082f5008dc /src/spdk/test/unit/lib/iscsi | |
parent | Initial commit. (diff) | |
download | ceph-19fcec84d8d7d21e796c7624e521b60d28ee21ed.tar.xz ceph-19fcec84d8d7d21e796c7624e521b60d28ee21ed.zip |
Adding upstream version 16.2.11+ds.upstream/16.2.11+dsupstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/spdk/test/unit/lib/iscsi')
22 files changed, 5901 insertions, 0 deletions
diff --git a/src/spdk/test/unit/lib/iscsi/Makefile b/src/spdk/test/unit/lib/iscsi/Makefile new file mode 100644 index 000000000..396c5a055 --- /dev/null +++ b/src/spdk/test/unit/lib/iscsi/Makefile @@ -0,0 +1,44 @@ +# +# BSD LICENSE +# +# Copyright (c) Intel Corporation. +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in +# the documentation and/or other materials provided with the +# distribution. +# * Neither the name of Intel Corporation nor the names of its +# contributors may be used to endorse or promote products derived +# from this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +# + +SPDK_ROOT_DIR := $(abspath $(CURDIR)/../../../..) +include $(SPDK_ROOT_DIR)/mk/spdk.common.mk + +DIRS-y = conn.c init_grp.c iscsi.c param.c portal_grp.c tgt_node.c + +.PHONY: all clean $(DIRS-y) + +all: $(DIRS-y) +clean: $(DIRS-y) + +include $(SPDK_ROOT_DIR)/mk/spdk.subdirs.mk diff --git a/src/spdk/test/unit/lib/iscsi/common.c b/src/spdk/test/unit/lib/iscsi/common.c new file mode 100644 index 000000000..e6631848a --- /dev/null +++ b/src/spdk/test/unit/lib/iscsi/common.c @@ -0,0 +1,209 @@ +#include "iscsi/task.h" +#include "iscsi/iscsi.h" +#include "iscsi/conn.h" + +#include "spdk/env.h" +#include "spdk/sock.h" +#include "spdk_cunit.h" + +#include "spdk_internal/log.h" +#include "spdk_internal/mock.h" + +#include "scsi/scsi_internal.h" + +SPDK_LOG_REGISTER_COMPONENT("iscsi", SPDK_LOG_ISCSI) + +struct spdk_trace_histories *g_trace_histories; +DEFINE_STUB_V(spdk_trace_add_register_fn, (struct spdk_trace_register_fn *reg_fn)); +DEFINE_STUB_V(spdk_trace_register_owner, (uint8_t type, char id_prefix)); +DEFINE_STUB_V(spdk_trace_register_object, (uint8_t type, char id_prefix)); +DEFINE_STUB_V(spdk_trace_register_description, (const char *name, + uint16_t tpoint_id, uint8_t owner_type, uint8_t object_type, uint8_t new_object, + uint8_t arg1_type, const char *arg1_name)); +DEFINE_STUB_V(_spdk_trace_record, (uint64_t tsc, uint16_t tpoint_id, uint16_t poller_id, + uint32_t size, uint64_t object_id, uint64_t arg1)); + +TAILQ_HEAD(, spdk_iscsi_pdu) g_write_pdu_list = TAILQ_HEAD_INITIALIZER(g_write_pdu_list); + +static bool g_task_pool_is_empty = false; +static bool g_pdu_pool_is_empty = false; + +struct spdk_iscsi_task * +iscsi_task_get(struct spdk_iscsi_conn *conn, + struct spdk_iscsi_task *parent, + spdk_scsi_task_cpl cpl_fn) +{ + struct spdk_iscsi_task *task; + + if (g_task_pool_is_empty) { + return NULL; + } + + task = calloc(1, sizeof(*task)); + if (!task) { + return NULL; + } + + task->conn = conn; + task->scsi.cpl_fn = cpl_fn; + if (parent) { + parent->scsi.ref++; + task->parent = parent; + task->tag = parent->tag; + task->lun_id = parent->lun_id; + task->scsi.dxfer_dir = parent->scsi.dxfer_dir; + task->scsi.transfer_len = parent->scsi.transfer_len; + task->scsi.lun = parent->scsi.lun; + task->scsi.cdb = parent->scsi.cdb; + task->scsi.target_port = parent->scsi.target_port; + task->scsi.initiator_port = parent->scsi.initiator_port; + if (conn && (task->scsi.dxfer_dir == SPDK_SCSI_DIR_FROM_DEV)) { + conn->data_in_cnt++; + } + } + + task->scsi.iovs = &task->scsi.iov; + return task; +} + +void +spdk_scsi_task_put(struct spdk_scsi_task *task) +{ + free(task); +} + +void +iscsi_put_pdu(struct spdk_iscsi_pdu *pdu) +{ + if (!pdu) { + return; + } + + pdu->ref--; + if (pdu->ref < 0) { + CU_FAIL("negative ref count"); + pdu->ref = 0; + } + + if (pdu->ref == 0) { + if (pdu->data && !pdu->data_from_mempool) { + free(pdu->data); + } + free(pdu); + } +} + +struct spdk_iscsi_pdu * +iscsi_get_pdu(struct spdk_iscsi_conn *conn) +{ + struct spdk_iscsi_pdu *pdu; + + assert(conn != NULL); + if (g_pdu_pool_is_empty) { + return NULL; + } + + pdu = malloc(sizeof(*pdu)); + if (!pdu) { + return NULL; + } + + memset(pdu, 0, offsetof(struct spdk_iscsi_pdu, ahs)); + pdu->ref = 1; + pdu->conn = conn; + + return pdu; +} + +DEFINE_STUB_V(spdk_scsi_task_process_null_lun, (struct spdk_scsi_task *task)); + +DEFINE_STUB_V(spdk_scsi_task_process_abort, (struct spdk_scsi_task *task)); + +DEFINE_STUB_V(spdk_scsi_dev_queue_task, + (struct spdk_scsi_dev *dev, struct spdk_scsi_task *task)); + +DEFINE_STUB(spdk_scsi_dev_find_port_by_id, struct spdk_scsi_port *, + (struct spdk_scsi_dev *dev, uint64_t id), NULL); + +DEFINE_STUB_V(spdk_scsi_dev_queue_mgmt_task, + (struct spdk_scsi_dev *dev, struct spdk_scsi_task *task)); + +const char * +spdk_scsi_dev_get_name(const struct spdk_scsi_dev *dev) +{ + if (dev != NULL) { + return dev->name; + } + + return NULL; +} + +DEFINE_STUB(spdk_scsi_dev_construct, struct spdk_scsi_dev *, + (const char *name, const char **bdev_name_list, + int *lun_id_list, int num_luns, uint8_t protocol_id, + void (*hotremove_cb)(const struct spdk_scsi_lun *, void *), + void *hotremove_ctx), + NULL); + +DEFINE_STUB_V(spdk_scsi_dev_destruct, + (struct spdk_scsi_dev *dev, spdk_scsi_dev_destruct_cb_t cb_fn, void *cb_arg)); + +DEFINE_STUB(spdk_scsi_dev_add_port, int, + (struct spdk_scsi_dev *dev, uint64_t id, const char *name), 0); + +DEFINE_STUB(iscsi_drop_conns, int, + (struct spdk_iscsi_conn *conn, const char *conn_match, int drop_all), + 0); + +DEFINE_STUB(spdk_scsi_dev_delete_port, int, + (struct spdk_scsi_dev *dev, uint64_t id), 0); + +DEFINE_STUB_V(shutdown_iscsi_conns, (void)); + +DEFINE_STUB_V(iscsi_conns_request_logout, (struct spdk_iscsi_tgt_node *target)); + +DEFINE_STUB(iscsi_get_active_conns, int, (struct spdk_iscsi_tgt_node *target), 0); + +void +iscsi_task_cpl(struct spdk_scsi_task *scsi_task) +{ + struct spdk_iscsi_task *iscsi_task; + + if (scsi_task != NULL) { + iscsi_task = iscsi_task_from_scsi_task(scsi_task); + if (iscsi_task->parent && (iscsi_task->scsi.dxfer_dir == SPDK_SCSI_DIR_FROM_DEV)) { + assert(iscsi_task->conn->data_in_cnt > 0); + iscsi_task->conn->data_in_cnt--; + } + + free(iscsi_task); + } +} + +DEFINE_STUB_V(iscsi_task_mgmt_cpl, (struct spdk_scsi_task *scsi_task)); + +DEFINE_STUB(iscsi_conn_read_data, int, + (struct spdk_iscsi_conn *conn, int bytes, void *buf), 0); + +DEFINE_STUB(iscsi_conn_readv_data, int, + (struct spdk_iscsi_conn *conn, struct iovec *iov, int iovcnt), 0); + +void +iscsi_conn_write_pdu(struct spdk_iscsi_conn *conn, struct spdk_iscsi_pdu *pdu, + iscsi_conn_xfer_complete_cb cb_fn, void *cb_arg) +{ + TAILQ_INSERT_TAIL(&g_write_pdu_list, pdu, tailq); +} + +DEFINE_STUB_V(iscsi_conn_logout, (struct spdk_iscsi_conn *conn)); + +DEFINE_STUB_V(spdk_scsi_task_set_status, + (struct spdk_scsi_task *task, int sc, int sk, int asc, int ascq)); + +void +spdk_scsi_task_set_data(struct spdk_scsi_task *task, void *data, uint32_t len) +{ + SPDK_CU_ASSERT_FATAL(task->iovs != NULL); + task->iovs[0].iov_base = data; + task->iovs[0].iov_len = len; +} diff --git a/src/spdk/test/unit/lib/iscsi/conn.c/.gitignore b/src/spdk/test/unit/lib/iscsi/conn.c/.gitignore new file mode 100644 index 000000000..3bb0afd8a --- /dev/null +++ b/src/spdk/test/unit/lib/iscsi/conn.c/.gitignore @@ -0,0 +1 @@ +conn_ut diff --git a/src/spdk/test/unit/lib/iscsi/conn.c/Makefile b/src/spdk/test/unit/lib/iscsi/conn.c/Makefile new file mode 100644 index 000000000..0c208d888 --- /dev/null +++ b/src/spdk/test/unit/lib/iscsi/conn.c/Makefile @@ -0,0 +1,38 @@ +# +# BSD LICENSE +# +# Copyright (c) Intel Corporation. +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in +# the documentation and/or other materials provided with the +# distribution. +# * Neither the name of Intel Corporation nor the names of its +# contributors may be used to endorse or promote products derived +# from this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +# + +SPDK_ROOT_DIR := $(abspath $(CURDIR)/../../../../..) + +TEST_FILE = conn_ut.c + +include $(SPDK_ROOT_DIR)/mk/spdk.unittest.mk diff --git a/src/spdk/test/unit/lib/iscsi/conn.c/conn_ut.c b/src/spdk/test/unit/lib/iscsi/conn.c/conn_ut.c new file mode 100644 index 000000000..967e16ec1 --- /dev/null +++ b/src/spdk/test/unit/lib/iscsi/conn.c/conn_ut.c @@ -0,0 +1,927 @@ +/*- + * BSD LICENSE + * + * Copyright (c) Intel Corporation. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Intel Corporation nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "spdk/stdinc.h" + +#include "common/lib/test_env.c" +#include "spdk_cunit.h" + +#include "iscsi/conn.c" + +#include "spdk_internal/mock.h" + +SPDK_LOG_REGISTER_COMPONENT("iscsi", SPDK_LOG_ISCSI) + +struct spdk_trace_histories *g_trace_histories; +DEFINE_STUB_V(spdk_trace_add_register_fn, (struct spdk_trace_register_fn *reg_fn)); +DEFINE_STUB_V(spdk_trace_register_owner, (uint8_t type, char id_prefix)); +DEFINE_STUB_V(spdk_trace_register_object, (uint8_t type, char id_prefix)); +DEFINE_STUB_V(spdk_trace_register_description, (const char *name, + uint16_t tpoint_id, uint8_t owner_type, uint8_t object_type, uint8_t new_object, + uint8_t arg1_type, const char *arg1_name)); +DEFINE_STUB_V(_spdk_trace_record, (uint64_t tsc, uint16_t tpoint_id, uint16_t poller_id, + uint32_t size, uint64_t object_id, uint64_t arg1)); + +struct spdk_scsi_lun { + uint8_t reserved; +}; + +struct spdk_iscsi_globals g_iscsi; +static TAILQ_HEAD(read_tasks_head, spdk_iscsi_task) g_ut_read_tasks = + TAILQ_HEAD_INITIALIZER(g_ut_read_tasks); +static struct spdk_iscsi_task *g_new_task = NULL; +static ssize_t g_sock_writev_bytes = 0; + +DEFINE_STUB(spdk_app_get_shm_id, int, (void), 0); + +DEFINE_STUB(spdk_sock_getaddr, int, + (struct spdk_sock *sock, char *saddr, int slen, uint16_t *sport, + char *caddr, int clen, uint16_t *cport), + 0); + +int +spdk_sock_close(struct spdk_sock **sock) +{ + *sock = NULL; + return 0; +} + +DEFINE_STUB(spdk_sock_recv, ssize_t, + (struct spdk_sock *sock, void *buf, size_t len), 0); + +DEFINE_STUB(spdk_sock_readv, ssize_t, + (struct spdk_sock *sock, struct iovec *iov, int iovcnt), 0); + +ssize_t +spdk_sock_writev(struct spdk_sock *sock, struct iovec *iov, int iovcnt) +{ + return g_sock_writev_bytes; +} + +DEFINE_STUB(spdk_sock_set_recvlowat, int, (struct spdk_sock *s, int nbytes), 0); + +DEFINE_STUB(spdk_sock_set_recvbuf, int, (struct spdk_sock *sock, int sz), 0); + +DEFINE_STUB(spdk_sock_set_sendbuf, int, (struct spdk_sock *sock, int sz), 0); + +DEFINE_STUB(spdk_sock_group_add_sock, int, + (struct spdk_sock_group *group, struct spdk_sock *sock, + spdk_sock_cb cb_fn, void *cb_arg), + 0); + +DEFINE_STUB(spdk_sock_group_remove_sock, int, + (struct spdk_sock_group *group, struct spdk_sock *sock), 0); + +struct spdk_iscsi_task * +iscsi_task_get(struct spdk_iscsi_conn *conn, + struct spdk_iscsi_task *parent, + spdk_scsi_task_cpl cpl_fn) +{ + struct spdk_iscsi_task *task; + + task = g_new_task; + if (task == NULL) { + return NULL; + } + memset(task, 0, sizeof(*task)); + + task->scsi.ref = 1; + task->conn = conn; + task->scsi.cpl_fn = cpl_fn; + if (parent) { + parent->scsi.ref++; + task->parent = parent; + task->scsi.dxfer_dir = parent->scsi.dxfer_dir; + task->scsi.transfer_len = parent->scsi.transfer_len; + task->scsi.lun = parent->scsi.lun; + if (conn && (task->scsi.dxfer_dir == SPDK_SCSI_DIR_FROM_DEV)) { + conn->data_in_cnt++; + } + } + + return task; +} + +void +spdk_scsi_task_put(struct spdk_scsi_task *scsi_task) +{ + struct spdk_iscsi_task *task; + + CU_ASSERT(scsi_task->ref > 0); + scsi_task->ref--; + + task = iscsi_task_from_scsi_task(scsi_task); + if (task->parent) { + spdk_scsi_task_put(&task->parent->scsi); + } +} + +DEFINE_STUB(spdk_scsi_dev_get_lun, struct spdk_scsi_lun *, + (struct spdk_scsi_dev *dev, int lun_id), NULL); + +DEFINE_STUB(spdk_scsi_dev_has_pending_tasks, bool, + (const struct spdk_scsi_dev *dev, const struct spdk_scsi_port *initiator_port), + true); + +DEFINE_STUB(spdk_scsi_lun_open, int, + (struct spdk_scsi_lun *lun, spdk_scsi_lun_remove_cb_t hotremove_cb, + void *hotremove_ctx, struct spdk_scsi_lun_desc **desc), + 0); + +DEFINE_STUB_V(spdk_scsi_lun_close, (struct spdk_scsi_lun_desc *desc)); + +DEFINE_STUB(spdk_scsi_lun_allocate_io_channel, int, + (struct spdk_scsi_lun_desc *desc), 0); + +DEFINE_STUB_V(spdk_scsi_lun_free_io_channel, (struct spdk_scsi_lun_desc *desc)); + +DEFINE_STUB(spdk_scsi_lun_get_id, int, (const struct spdk_scsi_lun *lun), 0); + +DEFINE_STUB(spdk_scsi_port_get_name, const char *, + (const struct spdk_scsi_port *port), NULL); + +void +spdk_scsi_task_copy_status(struct spdk_scsi_task *dst, + struct spdk_scsi_task *src) +{ + dst->status = src->status; +} + +DEFINE_STUB_V(spdk_scsi_task_set_data, (struct spdk_scsi_task *task, void *data, uint32_t len)); + +DEFINE_STUB_V(spdk_scsi_task_process_null_lun, (struct spdk_scsi_task *task)); + +DEFINE_STUB_V(spdk_scsi_task_process_abort, (struct spdk_scsi_task *task)); + +DEFINE_STUB_V(iscsi_put_pdu, (struct spdk_iscsi_pdu *pdu)); + +DEFINE_STUB_V(iscsi_param_free, (struct iscsi_param *params)); + +DEFINE_STUB(iscsi_conn_params_init, int, (struct iscsi_param **params), 0); + +DEFINE_STUB_V(iscsi_clear_all_transfer_task, + (struct spdk_iscsi_conn *conn, struct spdk_scsi_lun *lun, + struct spdk_iscsi_pdu *pdu)); + +DEFINE_STUB(iscsi_build_iovs, int, + (struct spdk_iscsi_conn *conn, struct iovec *iov, int num_iovs, + struct spdk_iscsi_pdu *pdu, uint32_t *mapped_length), + 0); + +DEFINE_STUB_V(iscsi_queue_task, + (struct spdk_iscsi_conn *conn, struct spdk_iscsi_task *task)); + +DEFINE_STUB_V(iscsi_task_response, + (struct spdk_iscsi_conn *conn, struct spdk_iscsi_task *task)); + +DEFINE_STUB_V(iscsi_task_mgmt_response, + (struct spdk_iscsi_conn *conn, struct spdk_iscsi_task *task)); + +DEFINE_STUB_V(iscsi_send_nopin, (struct spdk_iscsi_conn *conn)); + +bool +iscsi_del_transfer_task(struct spdk_iscsi_conn *conn, uint32_t task_tag) +{ + struct spdk_iscsi_task *task; + + task = TAILQ_FIRST(&conn->active_r2t_tasks); + if (task == NULL || task->tag != task_tag) { + return false; + } + + TAILQ_REMOVE(&conn->active_r2t_tasks, task, link); + task->is_r2t_active = false; + iscsi_task_put(task); + + return true; +} + +DEFINE_STUB(iscsi_handle_incoming_pdus, int, (struct spdk_iscsi_conn *conn), 0); + +DEFINE_STUB_V(iscsi_free_sess, (struct spdk_iscsi_sess *sess)); + +DEFINE_STUB(iscsi_tgt_node_cleanup_luns, int, + (struct spdk_iscsi_conn *conn, struct spdk_iscsi_tgt_node *target), + 0); + +DEFINE_STUB(iscsi_pdu_calc_header_digest, uint32_t, + (struct spdk_iscsi_pdu *pdu), 0); + +DEFINE_STUB(spdk_iscsi_pdu_calc_data_digest, uint32_t, + (struct spdk_iscsi_pdu *pdu), 0); + +DEFINE_STUB_V(shutdown_iscsi_conns_done, (void)); + +static struct spdk_iscsi_task * +ut_conn_task_get(struct spdk_iscsi_task *parent) +{ + struct spdk_iscsi_task *task; + + task = calloc(1, sizeof(*task)); + SPDK_CU_ASSERT_FATAL(task != NULL); + + task->scsi.ref = 1; + + if (parent) { + task->parent = parent; + parent->scsi.ref++; + } + return task; +} + +static void +ut_conn_create_read_tasks(struct spdk_iscsi_task *primary) +{ + struct spdk_iscsi_task *subtask; + uint32_t remaining_size = 0; + + while (1) { + if (primary->current_datain_offset < primary->scsi.transfer_len) { + remaining_size = primary->scsi.transfer_len - primary->current_datain_offset; + + subtask = ut_conn_task_get(primary); + + subtask->scsi.offset = primary->current_datain_offset; + subtask->scsi.length = spdk_min(SPDK_BDEV_LARGE_BUF_MAX_SIZE, remaining_size); + subtask->scsi.status = SPDK_SCSI_STATUS_GOOD; + + primary->current_datain_offset += subtask->scsi.length; + + TAILQ_INSERT_TAIL(&g_ut_read_tasks, subtask, link); + } + + if (primary->current_datain_offset == primary->scsi.transfer_len) { + break; + } + } +} + +static void +read_task_split_in_order_case(void) +{ + struct spdk_iscsi_task primary = {}; + struct spdk_iscsi_task *task, *tmp; + + primary.scsi.transfer_len = SPDK_BDEV_LARGE_BUF_MAX_SIZE * 8; + TAILQ_INIT(&primary.subtask_list); + primary.current_datain_offset = 0; + primary.bytes_completed = 0; + primary.scsi.ref = 1; + + ut_conn_create_read_tasks(&primary); + SPDK_CU_ASSERT_FATAL(!TAILQ_EMPTY(&g_ut_read_tasks)); + + TAILQ_FOREACH(task, &g_ut_read_tasks, link) { + CU_ASSERT(&primary == iscsi_task_get_primary(task)); + process_read_task_completion(NULL, task, &primary); + } + + CU_ASSERT(primary.bytes_completed == primary.scsi.transfer_len); + CU_ASSERT(primary.scsi.ref == 0); + + TAILQ_FOREACH_SAFE(task, &g_ut_read_tasks, link, tmp) { + CU_ASSERT(task->scsi.ref == 0); + TAILQ_REMOVE(&g_ut_read_tasks, task, link); + free(task); + } + +} + +static void +read_task_split_reverse_order_case(void) +{ + struct spdk_iscsi_task primary = {}; + struct spdk_iscsi_task *task, *tmp; + + primary.scsi.transfer_len = SPDK_BDEV_LARGE_BUF_MAX_SIZE * 8; + TAILQ_INIT(&primary.subtask_list); + primary.current_datain_offset = 0; + primary.bytes_completed = 0; + primary.scsi.ref = 1; + + ut_conn_create_read_tasks(&primary); + SPDK_CU_ASSERT_FATAL(!TAILQ_EMPTY(&g_ut_read_tasks)); + + TAILQ_FOREACH_REVERSE(task, &g_ut_read_tasks, read_tasks_head, link) { + CU_ASSERT(&primary == iscsi_task_get_primary(task)); + process_read_task_completion(NULL, task, &primary); + } + + CU_ASSERT(primary.bytes_completed == primary.scsi.transfer_len); + CU_ASSERT(primary.scsi.ref == 0); + + TAILQ_FOREACH_SAFE(task, &g_ut_read_tasks, link, tmp) { + CU_ASSERT(task->scsi.ref == 0); + TAILQ_REMOVE(&g_ut_read_tasks, task, link); + free(task); + } +} + +static void +propagate_scsi_error_status_for_split_read_tasks(void) +{ + struct spdk_iscsi_task primary = {}; + struct spdk_iscsi_task task1 = {}, task2 = {}, task3 = {}, task4 = {}, task5 = {}, task6 = {}; + + primary.scsi.transfer_len = 512 * 6; + primary.rsp_scsi_status = SPDK_SCSI_STATUS_GOOD; + TAILQ_INIT(&primary.subtask_list); + primary.scsi.ref = 7; + + task1.scsi.offset = 0; + task1.scsi.length = 512; + task1.scsi.status = SPDK_SCSI_STATUS_GOOD; + task1.scsi.ref = 1; + task1.parent = &primary; + + task2.scsi.offset = 512; + task2.scsi.length = 512; + task2.scsi.status = SPDK_SCSI_STATUS_CHECK_CONDITION; + task2.scsi.ref = 1; + task2.parent = &primary; + + task3.scsi.offset = 512 * 2; + task3.scsi.length = 512; + task3.scsi.status = SPDK_SCSI_STATUS_GOOD; + task3.scsi.ref = 1; + task3.parent = &primary; + + task4.scsi.offset = 512 * 3; + task4.scsi.length = 512; + task4.scsi.status = SPDK_SCSI_STATUS_GOOD; + task4.scsi.ref = 1; + task4.parent = &primary; + + task5.scsi.offset = 512 * 4; + task5.scsi.length = 512; + task5.scsi.status = SPDK_SCSI_STATUS_GOOD; + task5.scsi.ref = 1; + task5.parent = &primary; + + task6.scsi.offset = 512 * 5; + task6.scsi.length = 512; + task6.scsi.status = SPDK_SCSI_STATUS_GOOD; + task6.scsi.ref = 1; + task6.parent = &primary; + + /* task2 has check condition status, and verify if the check condition + * status is propagated to remaining tasks correctly when these tasks complete + * by the following order, task4, task3, task2, task1, primary, task5, and task6. + */ + process_read_task_completion(NULL, &task4, &primary); + process_read_task_completion(NULL, &task3, &primary); + process_read_task_completion(NULL, &task2, &primary); + process_read_task_completion(NULL, &task1, &primary); + process_read_task_completion(NULL, &task5, &primary); + process_read_task_completion(NULL, &task6, &primary); + + CU_ASSERT(primary.rsp_scsi_status == SPDK_SCSI_STATUS_CHECK_CONDITION); + CU_ASSERT(task1.scsi.status == SPDK_SCSI_STATUS_CHECK_CONDITION); + CU_ASSERT(task2.scsi.status == SPDK_SCSI_STATUS_CHECK_CONDITION); + CU_ASSERT(task3.scsi.status == SPDK_SCSI_STATUS_CHECK_CONDITION); + CU_ASSERT(task4.scsi.status == SPDK_SCSI_STATUS_CHECK_CONDITION); + CU_ASSERT(task5.scsi.status == SPDK_SCSI_STATUS_CHECK_CONDITION); + CU_ASSERT(task6.scsi.status == SPDK_SCSI_STATUS_CHECK_CONDITION); + CU_ASSERT(primary.bytes_completed == primary.scsi.transfer_len); + CU_ASSERT(TAILQ_EMPTY(&primary.subtask_list)); + CU_ASSERT(primary.scsi.ref == 0); + CU_ASSERT(task1.scsi.ref == 0); + CU_ASSERT(task2.scsi.ref == 0); + CU_ASSERT(task3.scsi.ref == 0); + CU_ASSERT(task4.scsi.ref == 0); + CU_ASSERT(task5.scsi.ref == 0); + CU_ASSERT(task6.scsi.ref == 0); +} + +static void +process_non_read_task_completion_test(void) +{ + struct spdk_iscsi_conn conn = {}; + struct spdk_iscsi_task primary = {}; + struct spdk_iscsi_task task = {}; + + TAILQ_INIT(&conn.active_r2t_tasks); + + primary.bytes_completed = 0; + primary.scsi.transfer_len = 4096 * 3; + primary.rsp_scsi_status = SPDK_SCSI_STATUS_GOOD; + primary.scsi.ref = 1; + TAILQ_INSERT_TAIL(&conn.active_r2t_tasks, &primary, link); + primary.is_r2t_active = true; + primary.tag = 1; + + /* First subtask which failed. */ + task.scsi.length = 4096; + task.scsi.data_transferred = 4096; + task.scsi.status = SPDK_SCSI_STATUS_CHECK_CONDITION; + task.scsi.ref = 1; + task.parent = &primary; + primary.scsi.ref++; + + process_non_read_task_completion(&conn, &task, &primary); + CU_ASSERT(!TAILQ_EMPTY(&conn.active_r2t_tasks)); + CU_ASSERT(primary.bytes_completed == 4096); + CU_ASSERT(primary.scsi.data_transferred == 0); + CU_ASSERT(primary.rsp_scsi_status == SPDK_SCSI_STATUS_CHECK_CONDITION); + CU_ASSERT(task.scsi.ref == 0); + CU_ASSERT(primary.scsi.ref == 1); + + /* Second subtask which succeeded. */ + task.scsi.length = 4096; + task.scsi.data_transferred = 4096; + task.scsi.status = SPDK_SCSI_STATUS_GOOD; + task.scsi.ref = 1; + task.parent = &primary; + primary.scsi.ref++; + + process_non_read_task_completion(&conn, &task, &primary); + CU_ASSERT(!TAILQ_EMPTY(&conn.active_r2t_tasks)); + CU_ASSERT(primary.bytes_completed == 4096 * 2); + CU_ASSERT(primary.scsi.data_transferred == 4096); + CU_ASSERT(primary.rsp_scsi_status == SPDK_SCSI_STATUS_CHECK_CONDITION); + CU_ASSERT(task.scsi.ref == 0); + CU_ASSERT(primary.scsi.ref == 1); + + /* Third and final subtask which succeeded. */ + task.scsi.length = 4096; + task.scsi.data_transferred = 4096; + task.scsi.status = SPDK_SCSI_STATUS_GOOD; + task.scsi.ref = 1; + task.parent = &primary; + primary.scsi.ref++; + + process_non_read_task_completion(&conn, &task, &primary); + CU_ASSERT(TAILQ_EMPTY(&conn.active_r2t_tasks)); + CU_ASSERT(primary.bytes_completed == 4096 * 3); + CU_ASSERT(primary.scsi.data_transferred == 4096 * 2); + CU_ASSERT(primary.rsp_scsi_status == SPDK_SCSI_STATUS_CHECK_CONDITION); + CU_ASSERT(task.scsi.ref == 0); + CU_ASSERT(primary.scsi.ref == 0); + + /* Tricky case when the last task completed was the initial task. */ + primary.scsi.length = 4096; + primary.bytes_completed = 4096 * 2; + primary.scsi.data_transferred = 4096 * 2; + primary.scsi.transfer_len = 4096 * 3; + primary.scsi.status = SPDK_SCSI_STATUS_GOOD; + primary.rsp_scsi_status = SPDK_SCSI_STATUS_GOOD; + primary.scsi.ref = 2; + TAILQ_INSERT_TAIL(&conn.active_r2t_tasks, &primary, link); + primary.is_r2t_active = true; + + process_non_read_task_completion(&conn, &primary, &primary); + CU_ASSERT(TAILQ_EMPTY(&conn.active_r2t_tasks)); + CU_ASSERT(primary.bytes_completed == 4096 * 3); + CU_ASSERT(primary.scsi.data_transferred == 4096 * 2); + CU_ASSERT(primary.rsp_scsi_status == SPDK_SCSI_STATUS_GOOD); + CU_ASSERT(primary.scsi.ref == 0); + + /* Further tricky case when the last task completed ws the initial task, + * and the R2T was already terminated. + */ + primary.scsi.ref = 1; + primary.scsi.length = 4096; + primary.bytes_completed = 4096 * 2; + primary.scsi.data_transferred = 4096 * 2; + primary.scsi.transfer_len = 4096 * 3; + primary.scsi.status = SPDK_SCSI_STATUS_GOOD; + primary.rsp_scsi_status = SPDK_SCSI_STATUS_GOOD; + primary.is_r2t_active = false; + + process_non_read_task_completion(&conn, &primary, &primary); + CU_ASSERT(primary.bytes_completed == 4096 * 3); + CU_ASSERT(primary.scsi.data_transferred == 4096 * 2); + CU_ASSERT(primary.rsp_scsi_status == SPDK_SCSI_STATUS_GOOD); + CU_ASSERT(primary.scsi.ref == 0); +} + +static bool +dequeue_pdu(void *_head, struct spdk_iscsi_pdu *pdu) +{ + TAILQ_HEAD(queued_pdus, spdk_iscsi_pdu) *head = _head; + struct spdk_iscsi_pdu *tmp; + + TAILQ_FOREACH(tmp, head, tailq) { + if (tmp == pdu) { + TAILQ_REMOVE(head, tmp, tailq); + return true; + } + } + return false; +} + +static bool +dequeue_task(void *_head, struct spdk_iscsi_task *task) +{ + TAILQ_HEAD(queued_tasks, spdk_iscsi_task) *head = _head; + struct spdk_iscsi_task *tmp; + + TAILQ_FOREACH(tmp, head, link) { + if (tmp == task) { + TAILQ_REMOVE(head, tmp, link); + return true; + } + } + return false; +} + +static void iscsi_conn_pdu_dummy_complete(void *arg) +{ +} + +static void +free_tasks_on_connection(void) +{ + struct spdk_iscsi_conn conn = {}; + struct spdk_iscsi_pdu pdu1 = {}, pdu2 = {}, pdu3 = {}, pdu4 = {}; + struct spdk_iscsi_task task1 = {}, task2 = {}, task3 = {}; + struct spdk_scsi_lun lun1 = {}, lun2 = {}; + + TAILQ_INIT(&conn.write_pdu_list); + TAILQ_INIT(&conn.snack_pdu_list); + TAILQ_INIT(&conn.queued_datain_tasks); + conn.data_in_cnt = MAX_LARGE_DATAIN_PER_CONNECTION; + + pdu1.task = &task1; + pdu2.task = &task2; + pdu3.task = &task3; + + pdu1.cb_fn = iscsi_conn_pdu_dummy_complete; + pdu2.cb_fn = iscsi_conn_pdu_dummy_complete; + pdu3.cb_fn = iscsi_conn_pdu_dummy_complete; + pdu4.cb_fn = iscsi_conn_pdu_dummy_complete; + + task1.scsi.lun = &lun1; + task2.scsi.lun = &lun2; + + task1.is_queued = false; + task2.is_queued = false; + task3.is_queued = true; + + /* Test conn->write_pdu_list. */ + + task1.scsi.ref = 1; + task2.scsi.ref = 1; + task3.scsi.ref = 1; + TAILQ_INSERT_TAIL(&conn.write_pdu_list, &pdu1, tailq); + TAILQ_INSERT_TAIL(&conn.write_pdu_list, &pdu2, tailq); + TAILQ_INSERT_TAIL(&conn.write_pdu_list, &pdu3, tailq); + TAILQ_INSERT_TAIL(&conn.write_pdu_list, &pdu4, tailq); + + /* Free all PDUs when exiting connection. */ + iscsi_conn_free_tasks(&conn); + + CU_ASSERT(TAILQ_EMPTY(&conn.write_pdu_list)); + CU_ASSERT(task1.scsi.ref == 0); + CU_ASSERT(task2.scsi.ref == 0); + CU_ASSERT(task3.scsi.ref == 0); + + /* Test conn->snack_pdu_list */ + + task1.scsi.ref = 1; + task2.scsi.ref = 1; + task3.scsi.ref = 1; + pdu1.cb_fn = iscsi_conn_pdu_dummy_complete; + pdu2.cb_fn = iscsi_conn_pdu_dummy_complete; + pdu3.cb_fn = iscsi_conn_pdu_dummy_complete; + TAILQ_INSERT_TAIL(&conn.snack_pdu_list, &pdu1, tailq); + TAILQ_INSERT_TAIL(&conn.snack_pdu_list, &pdu2, tailq); + TAILQ_INSERT_TAIL(&conn.snack_pdu_list, &pdu3, tailq); + + /* Free all PDUs and associated tasks when exiting connection. */ + iscsi_conn_free_tasks(&conn); + + CU_ASSERT(!dequeue_pdu(&conn.snack_pdu_list, &pdu1)); + CU_ASSERT(!dequeue_pdu(&conn.snack_pdu_list, &pdu2)); + CU_ASSERT(!dequeue_pdu(&conn.snack_pdu_list, &pdu3)); + CU_ASSERT(task1.scsi.ref == 0); + CU_ASSERT(task2.scsi.ref == 0); + CU_ASSERT(task3.scsi.ref == 0); + + /* Test conn->queued_datain_tasks */ + + task1.scsi.ref = 1; + task2.scsi.ref = 1; + task3.scsi.ref = 1; + TAILQ_INSERT_TAIL(&conn.queued_datain_tasks, &task1, link); + TAILQ_INSERT_TAIL(&conn.queued_datain_tasks, &task2, link); + TAILQ_INSERT_TAIL(&conn.queued_datain_tasks, &task3, link); + + /* Free all tasks which is not queued when exiting connection. */ + iscsi_conn_free_tasks(&conn); + + CU_ASSERT(!dequeue_task(&conn.queued_datain_tasks, &task1)); + CU_ASSERT(!dequeue_task(&conn.queued_datain_tasks, &task2)); + CU_ASSERT(dequeue_task(&conn.queued_datain_tasks, &task3)); + CU_ASSERT(task1.scsi.ref == 0); + CU_ASSERT(task2.scsi.ref == 0); + CU_ASSERT(task3.scsi.ref == 1); +} + +static void +free_tasks_with_queued_datain(void) +{ + struct spdk_iscsi_conn conn = {}; + struct spdk_iscsi_pdu pdu1 = {}, pdu2 = {}, pdu3 = {}, pdu4 = {}, pdu5 = {}, pdu6 = {}; + struct spdk_iscsi_task task1 = {}, task2 = {}, task3 = {}, task4 = {}, task5 = {}, task6 = {}; + + TAILQ_INIT(&conn.write_pdu_list); + TAILQ_INIT(&conn.snack_pdu_list); + TAILQ_INIT(&conn.queued_datain_tasks); + + pdu1.task = &task1; + pdu2.task = &task2; + pdu3.task = &task3; + pdu1.cb_fn = iscsi_conn_pdu_dummy_complete; + pdu2.cb_fn = iscsi_conn_pdu_dummy_complete; + pdu3.cb_fn = iscsi_conn_pdu_dummy_complete; + + task1.scsi.ref = 1; + task2.scsi.ref = 1; + task3.scsi.ref = 1; + + pdu3.bhs.opcode = ISCSI_OP_SCSI_DATAIN; + task3.scsi.offset = 1; + conn.data_in_cnt = 1; + + TAILQ_INSERT_TAIL(&conn.write_pdu_list, &pdu1, tailq); + TAILQ_INSERT_TAIL(&conn.write_pdu_list, &pdu2, tailq); + TAILQ_INSERT_TAIL(&conn.write_pdu_list, &pdu3, tailq); + + task4.scsi.ref = 1; + task5.scsi.ref = 1; + task6.scsi.ref = 1; + + task4.pdu = &pdu4; + task5.pdu = &pdu5; + task6.pdu = &pdu6; + pdu4.cb_fn = iscsi_conn_pdu_dummy_complete; + pdu5.cb_fn = iscsi_conn_pdu_dummy_complete; + pdu6.cb_fn = iscsi_conn_pdu_dummy_complete; + + TAILQ_INSERT_TAIL(&conn.queued_datain_tasks, &task4, link); + TAILQ_INSERT_TAIL(&conn.queued_datain_tasks, &task5, link); + TAILQ_INSERT_TAIL(&conn.queued_datain_tasks, &task6, link); + + iscsi_conn_free_tasks(&conn); + + CU_ASSERT(TAILQ_EMPTY(&conn.write_pdu_list)); + CU_ASSERT(TAILQ_EMPTY(&conn.queued_datain_tasks)); +} + +static void +abort_queued_datain_task_test(void) +{ + struct spdk_iscsi_conn conn = {}; + struct spdk_iscsi_task task = {}, subtask = {}; + struct spdk_iscsi_pdu pdu = {}; + struct iscsi_bhs_scsi_req *scsi_req; + int rc; + + TAILQ_INIT(&conn.queued_datain_tasks); + task.scsi.ref = 1; + task.scsi.dxfer_dir = SPDK_SCSI_DIR_FROM_DEV; + task.pdu = &pdu; + TAILQ_INIT(&task.subtask_list); + scsi_req = (struct iscsi_bhs_scsi_req *)&pdu.bhs; + scsi_req->read_bit = 1; + + g_new_task = &subtask; + + /* Case1: Queue one task, and this task is not executed */ + task.scsi.transfer_len = SPDK_BDEV_LARGE_BUF_MAX_SIZE * 3; + task.scsi.offset = 0; + TAILQ_INSERT_TAIL(&conn.queued_datain_tasks, &task, link); + + /* No slots for sub read tasks */ + conn.data_in_cnt = MAX_LARGE_DATAIN_PER_CONNECTION; + rc = _iscsi_conn_abort_queued_datain_task(&conn, &task); + CU_ASSERT(rc != 0); + CU_ASSERT(!TAILQ_EMPTY(&conn.queued_datain_tasks)); + + /* Have slots for sub read tasks */ + conn.data_in_cnt = 0; + rc = _iscsi_conn_abort_queued_datain_task(&conn, &task); + CU_ASSERT(rc == 0); + CU_ASSERT(TAILQ_EMPTY(&conn.queued_datain_tasks)); + CU_ASSERT(task.current_datain_offset == SPDK_BDEV_LARGE_BUF_MAX_SIZE * 3); + CU_ASSERT(task.scsi.ref == 0); + CU_ASSERT(subtask.scsi.offset == 0); + CU_ASSERT(subtask.scsi.length == SPDK_BDEV_LARGE_BUF_MAX_SIZE * 3); + CU_ASSERT(subtask.scsi.ref == 0); + + /* Case2: Queue one task, and this task is partially executed */ + task.scsi.ref = 1; + task.scsi.transfer_len = SPDK_BDEV_LARGE_BUF_MAX_SIZE * 3; + task.current_datain_offset = SPDK_BDEV_LARGE_BUF_MAX_SIZE; + TAILQ_INSERT_TAIL(&conn.queued_datain_tasks, &task, link); + + /* No slots for sub read tasks */ + conn.data_in_cnt = MAX_LARGE_DATAIN_PER_CONNECTION; + rc = _iscsi_conn_abort_queued_datain_task(&conn, &task); + CU_ASSERT(rc != 0); + CU_ASSERT(!TAILQ_EMPTY(&conn.queued_datain_tasks)); + + /* have slots for sub read tasks */ + conn.data_in_cnt = 0; + rc = _iscsi_conn_abort_queued_datain_task(&conn, &task); + CU_ASSERT(rc == 0); + CU_ASSERT(task.current_datain_offset == SPDK_BDEV_LARGE_BUF_MAX_SIZE * 3); + CU_ASSERT(task.scsi.ref == 2); + CU_ASSERT(TAILQ_FIRST(&task.subtask_list) == &subtask); + CU_ASSERT(subtask.scsi.offset == SPDK_BDEV_LARGE_BUF_MAX_SIZE); + CU_ASSERT(subtask.scsi.length == SPDK_BDEV_LARGE_BUF_MAX_SIZE * 2); + CU_ASSERT(subtask.scsi.ref == 1); + + g_new_task = NULL; +} + +static bool +datain_task_is_queued(struct spdk_iscsi_conn *conn, + struct spdk_iscsi_task *task) +{ + struct spdk_iscsi_task *tmp; + + TAILQ_FOREACH(tmp, &conn->queued_datain_tasks, link) { + if (tmp == task) { + return true; + } + } + return false; +} +static void +abort_queued_datain_tasks_test(void) +{ + struct spdk_iscsi_conn conn = {}; + struct spdk_iscsi_task task1 = {}, task2 = {}, task3 = {}, task4 = {}, task5 = {}, task6 = {}; + struct spdk_iscsi_task subtask = {}; + struct spdk_iscsi_pdu pdu1 = {}, pdu2 = {}, pdu3 = {}, pdu4 = {}, pdu5 = {}, pdu6 = {}; + struct spdk_iscsi_pdu mgmt_pdu1 = {}, mgmt_pdu2 = {}; + struct spdk_scsi_lun lun1 = {}, lun2 = {}; + uint32_t alloc_cmd_sn; + struct iscsi_bhs_scsi_req *scsi_req; + int rc; + + TAILQ_INIT(&conn.queued_datain_tasks); + conn.data_in_cnt = 0; + + g_new_task = &subtask; + + alloc_cmd_sn = 88; + + pdu1.cmd_sn = alloc_cmd_sn; + alloc_cmd_sn++; + scsi_req = (struct iscsi_bhs_scsi_req *)&pdu1.bhs; + scsi_req->read_bit = 1; + task1.scsi.ref = 1; + task1.current_datain_offset = 0; + task1.scsi.transfer_len = 512; + task1.scsi.lun = &lun1; + iscsi_task_set_pdu(&task1, &pdu1); + TAILQ_INSERT_TAIL(&conn.queued_datain_tasks, &task1, link); + + pdu2.cmd_sn = alloc_cmd_sn; + alloc_cmd_sn++; + scsi_req = (struct iscsi_bhs_scsi_req *)&pdu2.bhs; + scsi_req->read_bit = 1; + task2.scsi.ref = 1; + task2.current_datain_offset = 0; + task2.scsi.transfer_len = 512; + task2.scsi.lun = &lun2; + iscsi_task_set_pdu(&task2, &pdu2); + TAILQ_INSERT_TAIL(&conn.queued_datain_tasks, &task2, link); + + mgmt_pdu1.cmd_sn = alloc_cmd_sn; + alloc_cmd_sn++; + + pdu3.cmd_sn = alloc_cmd_sn; + alloc_cmd_sn++; + scsi_req = (struct iscsi_bhs_scsi_req *)&pdu3.bhs; + scsi_req->read_bit = 1; + task3.scsi.ref = 1; + task3.current_datain_offset = 0; + task3.scsi.transfer_len = 512; + task3.scsi.lun = &lun1; + iscsi_task_set_pdu(&task3, &pdu3); + TAILQ_INSERT_TAIL(&conn.queued_datain_tasks, &task3, link); + + pdu4.cmd_sn = alloc_cmd_sn; + alloc_cmd_sn++; + scsi_req = (struct iscsi_bhs_scsi_req *)&pdu4.bhs; + scsi_req->read_bit = 1; + task4.scsi.ref = 1; + task4.current_datain_offset = 0; + task4.scsi.transfer_len = 512; + task4.scsi.lun = &lun2; + iscsi_task_set_pdu(&task4, &pdu4); + TAILQ_INSERT_TAIL(&conn.queued_datain_tasks, &task4, link); + + pdu5.cmd_sn = alloc_cmd_sn; + alloc_cmd_sn++; + scsi_req = (struct iscsi_bhs_scsi_req *)&pdu5.bhs; + scsi_req->read_bit = 1; + task5.scsi.ref = 1; + task5.current_datain_offset = 0; + task5.scsi.transfer_len = 512; + task5.scsi.lun = &lun1; + iscsi_task_set_pdu(&task5, &pdu5); + TAILQ_INSERT_TAIL(&conn.queued_datain_tasks, &task5, link); + + mgmt_pdu2.cmd_sn = alloc_cmd_sn; + alloc_cmd_sn++; + + pdu6.cmd_sn = alloc_cmd_sn; + alloc_cmd_sn++; + scsi_req = (struct iscsi_bhs_scsi_req *)&pdu6.bhs; + scsi_req->read_bit = 1; + task6.scsi.ref = 1; + task6.current_datain_offset = 0; + task6.scsi.transfer_len = 512; + task6.scsi.lun = &lun2; + iscsi_task_set_pdu(&task6, &pdu6); + TAILQ_INSERT_TAIL(&conn.queued_datain_tasks, &task6, link); + + rc = iscsi_conn_abort_queued_datain_tasks(&conn, &lun1, &mgmt_pdu1); + CU_ASSERT(rc == 0); + CU_ASSERT(!datain_task_is_queued(&conn, &task1)); + CU_ASSERT(datain_task_is_queued(&conn, &task2)); + CU_ASSERT(datain_task_is_queued(&conn, &task3)); + CU_ASSERT(datain_task_is_queued(&conn, &task4)); + CU_ASSERT(datain_task_is_queued(&conn, &task5)); + CU_ASSERT(datain_task_is_queued(&conn, &task6)); + + rc = iscsi_conn_abort_queued_datain_tasks(&conn, &lun2, &mgmt_pdu2); + CU_ASSERT(rc == 0); + CU_ASSERT(!datain_task_is_queued(&conn, &task2)); + CU_ASSERT(datain_task_is_queued(&conn, &task3)); + CU_ASSERT(!datain_task_is_queued(&conn, &task4)); + CU_ASSERT(datain_task_is_queued(&conn, &task5)); + CU_ASSERT(datain_task_is_queued(&conn, &task6)); + + CU_ASSERT(task1.scsi.ref == 0); + CU_ASSERT(task2.scsi.ref == 0); + CU_ASSERT(task3.scsi.ref == 1); + CU_ASSERT(task4.scsi.ref == 0); + CU_ASSERT(task5.scsi.ref == 1); + CU_ASSERT(task6.scsi.ref == 1); + CU_ASSERT(subtask.scsi.ref == 0); + + g_new_task = NULL; +} + +int +main(int argc, char **argv) +{ + CU_pSuite suite = NULL; + unsigned int num_failures; + + CU_set_error_action(CUEA_ABORT); + CU_initialize_registry(); + + suite = CU_add_suite("conn_suite", NULL, NULL); + + CU_ADD_TEST(suite, read_task_split_in_order_case); + CU_ADD_TEST(suite, read_task_split_reverse_order_case); + CU_ADD_TEST(suite, propagate_scsi_error_status_for_split_read_tasks); + CU_ADD_TEST(suite, process_non_read_task_completion_test); + CU_ADD_TEST(suite, free_tasks_on_connection); + CU_ADD_TEST(suite, free_tasks_with_queued_datain); + CU_ADD_TEST(suite, abort_queued_datain_task_test); + CU_ADD_TEST(suite, abort_queued_datain_tasks_test); + + CU_basic_set_mode(CU_BRM_VERBOSE); + CU_basic_run_tests(); + num_failures = CU_get_number_of_failures(); + CU_cleanup_registry(); + return num_failures; +} diff --git a/src/spdk/test/unit/lib/iscsi/init_grp.c/.gitignore b/src/spdk/test/unit/lib/iscsi/init_grp.c/.gitignore new file mode 100644 index 000000000..8fbc2b636 --- /dev/null +++ b/src/spdk/test/unit/lib/iscsi/init_grp.c/.gitignore @@ -0,0 +1 @@ +init_grp_ut diff --git a/src/spdk/test/unit/lib/iscsi/init_grp.c/Makefile b/src/spdk/test/unit/lib/iscsi/init_grp.c/Makefile new file mode 100644 index 000000000..708e691a5 --- /dev/null +++ b/src/spdk/test/unit/lib/iscsi/init_grp.c/Makefile @@ -0,0 +1,39 @@ +# +# BSD LICENSE +# +# Copyright (c) Intel Corporation. +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in +# the documentation and/or other materials provided with the +# distribution. +# * Neither the name of Intel Corporation nor the names of its +# contributors may be used to endorse or promote products derived +# from this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +# + +SPDK_ROOT_DIR := $(abspath $(CURDIR)/../../../../..) + +SPDK_LIB_LIST = conf +TEST_FILE = init_grp_ut.c + +include $(SPDK_ROOT_DIR)/mk/spdk.unittest.mk diff --git a/src/spdk/test/unit/lib/iscsi/init_grp.c/init_grp.conf b/src/spdk/test/unit/lib/iscsi/init_grp.c/init_grp.conf new file mode 100644 index 000000000..aaa660def --- /dev/null +++ b/src/spdk/test/unit/lib/iscsi/init_grp.c/init_grp.conf @@ -0,0 +1,31 @@ +[IG_Valid0] +# Success is expected. + InitiatorName iqn.2017-10.spdk.io:0001 + Netmask 192.168.2.0 + +[IG_Valid1] +# Success is expected. + InitiatorName iqn.2017-10.spdk.io:0001 + Netmask 192.168.2.0 + Netmask 192.168.2.1 + +[IG_Valid2] +# Success is expected. + InitiatorName iqn.2017-10.spdk.io:0001 + InitiatorName iqn.2017-10.spdk.io:0002 + Netmask 192.168.2.0 + +[IG_Valid3] +# Success is expected. + InitiatorName iqn.2017-10.spdk.io:0001 + InitiatorName iqn.2017-10.spdk.io:0002 + Netmask 192.168.2.0 + Netmask 192.168.2.1 + +[IG_Invalid0] +# Failure is expected. + InitiatorName iqn.2017-10.spdk.io:0001 + +[IG_Invalid1] +# Failure is expected. + Netmask 192.168.2.0 diff --git a/src/spdk/test/unit/lib/iscsi/init_grp.c/init_grp_ut.c b/src/spdk/test/unit/lib/iscsi/init_grp.c/init_grp_ut.c new file mode 100644 index 000000000..199aad8b8 --- /dev/null +++ b/src/spdk/test/unit/lib/iscsi/init_grp.c/init_grp_ut.c @@ -0,0 +1,674 @@ +/*- + * BSD LICENSE + * + * Copyright (c) Intel Corporation. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Intel Corporation nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +#include "spdk/stdinc.h" + +#include "spdk_cunit.h" +#include "CUnit/Basic.h" + +#include "iscsi/init_grp.c" +#include "unit/lib/json_mock.c" + +SPDK_LOG_REGISTER_COMPONENT("iscsi", SPDK_LOG_ISCSI) + +struct spdk_iscsi_globals g_iscsi; + +const char *config_file; + +static int +test_setup(void) +{ + TAILQ_INIT(&g_iscsi.ig_head); + return 0; +} + +static void +create_from_config_file_cases(void) +{ + struct spdk_conf *config; + struct spdk_conf_section *sp; + char section_name[64]; + int section_index; + int rc; + + config = spdk_conf_allocate(); + + rc = spdk_conf_read(config, config_file); + CU_ASSERT(rc == 0); + + section_index = 0; + while (true) { + snprintf(section_name, sizeof(section_name), "IG_Valid%d", section_index); + + sp = spdk_conf_find_section(config, section_name); + if (sp == NULL) { + break; + } + + rc = iscsi_parse_init_grp(sp); + CU_ASSERT(rc == 0); + + iscsi_init_grps_destroy(); + + section_index++; + } + + section_index = 0; + while (true) { + snprintf(section_name, sizeof(section_name), "IG_Invalid%d", section_index); + + sp = spdk_conf_find_section(config, section_name); + if (sp == NULL) { + break; + } + + rc = iscsi_parse_init_grp(sp); + CU_ASSERT(rc != 0); + + iscsi_init_grps_destroy(); + + section_index++; + } + + spdk_conf_free(config); +} + + +static void +create_initiator_group_success_case(void) +{ + struct spdk_iscsi_init_grp *ig; + + ig = iscsi_init_grp_create(1); + CU_ASSERT(ig != NULL); + + iscsi_init_grp_destroy(ig); +} + +static void +find_initiator_group_success_case(void) +{ + struct spdk_iscsi_init_grp *ig, *tmp; + int rc; + + ig = iscsi_init_grp_create(1); + CU_ASSERT(ig != NULL); + + rc = iscsi_init_grp_register(ig); + CU_ASSERT(rc == 0); + + ig = iscsi_init_grp_find_by_tag(1); + CU_ASSERT(ig != NULL); + + tmp = iscsi_init_grp_unregister(1); + CU_ASSERT(ig == tmp); + iscsi_init_grp_destroy(ig); + + ig = iscsi_init_grp_find_by_tag(1); + CU_ASSERT(ig == NULL); +} + +static void +register_initiator_group_twice_case(void) +{ + struct spdk_iscsi_init_grp *ig, *tmp; + int rc; + + ig = iscsi_init_grp_create(1); + CU_ASSERT(ig != NULL); + + rc = iscsi_init_grp_register(ig); + CU_ASSERT(rc == 0); + + rc = iscsi_init_grp_register(ig); + CU_ASSERT(rc != 0); + + ig = iscsi_init_grp_find_by_tag(1); + CU_ASSERT(ig != NULL); + + tmp = iscsi_init_grp_unregister(1); + CU_ASSERT(tmp == ig); + iscsi_init_grp_destroy(ig); + + ig = iscsi_init_grp_find_by_tag(1); + CU_ASSERT(ig == NULL); +} + +static void +add_initiator_name_success_case(void) +{ + + int rc; + struct spdk_iscsi_init_grp *ig; + struct spdk_iscsi_initiator_name *iname; + char *name1 = "iqn.2017-10.spdk.io:0001"; + char *name2 = "iqn.2017-10.spdk.io:0002"; + + ig = iscsi_init_grp_create(1); + CU_ASSERT(ig != NULL); + + /* add two different names to the empty name list */ + rc = iscsi_init_grp_add_initiator(ig, name1); + CU_ASSERT(rc == 0); + + rc = iscsi_init_grp_add_initiator(ig, name2); + CU_ASSERT(rc == 0); + + /* check if two names are added correctly. */ + iname = iscsi_init_grp_find_initiator(ig, name1); + CU_ASSERT(iname != NULL); + + iname = iscsi_init_grp_find_initiator(ig, name2); + CU_ASSERT(iname != NULL); + + /* restore the initial state */ + rc = iscsi_init_grp_delete_initiator(ig, name1); + CU_ASSERT(rc == 0); + + iname = iscsi_init_grp_find_initiator(ig, name1); + CU_ASSERT(iname == NULL); + + rc = iscsi_init_grp_delete_initiator(ig, name2); + CU_ASSERT(rc == 0); + + iname = iscsi_init_grp_find_initiator(ig, name2); + CU_ASSERT(iname == NULL); + + iscsi_init_grp_destroy(ig); +} + +static void +add_initiator_name_fail_case(void) +{ + int rc; + struct spdk_iscsi_init_grp *ig; + struct spdk_iscsi_initiator_name *iname; + char *name1 = "iqn.2017-10.spdk.io:0001"; + + ig = iscsi_init_grp_create(1); + CU_ASSERT(ig != NULL); + + /* add an name to the full name list */ + ig->ninitiators = MAX_INITIATOR; + + rc = iscsi_init_grp_add_initiator(ig, name1); + CU_ASSERT(rc != 0); + + ig->ninitiators = 0; + + /* add the same name to the name list twice */ + rc = iscsi_init_grp_add_initiator(ig, name1); + CU_ASSERT(rc == 0); + + rc = iscsi_init_grp_add_initiator(ig, name1); + CU_ASSERT(rc != 0); + + /* restore the initial state */ + rc = iscsi_init_grp_delete_initiator(ig, name1); + CU_ASSERT(rc == 0); + + iname = iscsi_init_grp_find_initiator(ig, name1); + CU_ASSERT(iname == NULL); + + iscsi_init_grp_destroy(ig); +} + +static void +delete_all_initiator_names_success_case(void) +{ + int rc; + struct spdk_iscsi_init_grp *ig; + struct spdk_iscsi_initiator_name *iname; + char *name1 = "iqn.2017-10.spdk.io:0001"; + char *name2 = "iqn.2017-10.spdk.io:0002"; + + ig = iscsi_init_grp_create(1); + CU_ASSERT(ig != NULL); + + /* add two different names to the empty name list */ + rc = iscsi_init_grp_add_initiator(ig, name1); + CU_ASSERT(rc == 0); + + rc = iscsi_init_grp_add_initiator(ig, name2); + CU_ASSERT(rc == 0); + + /* delete all initiator names */ + iscsi_init_grp_delete_all_initiators(ig); + + /* check if two names are deleted correctly. */ + iname = iscsi_init_grp_find_initiator(ig, name1); + CU_ASSERT(iname == NULL); + + iname = iscsi_init_grp_find_initiator(ig, name2); + CU_ASSERT(iname == NULL); + + /* restore the initial state */ + iscsi_init_grp_destroy(ig); +} + +static void +add_netmask_success_case(void) +{ + int rc; + struct spdk_iscsi_init_grp *ig; + struct spdk_iscsi_initiator_netmask *imask; + char *netmask1 = "192.168.2.0"; + char *netmask2 = "192.168.2.1"; + + ig = iscsi_init_grp_create(1); + CU_ASSERT(ig != NULL); + + /* add two different netmasks to the empty netmask list */ + rc = iscsi_init_grp_add_netmask(ig, netmask1); + CU_ASSERT(rc == 0); + + rc = iscsi_init_grp_add_netmask(ig, netmask2); + CU_ASSERT(rc == 0); + + /* check if two netmasks are added correctly. */ + imask = iscsi_init_grp_find_netmask(ig, netmask1); + CU_ASSERT(imask != NULL); + + imask = iscsi_init_grp_find_netmask(ig, netmask2); + CU_ASSERT(imask != NULL); + + /* restore the initial state */ + rc = iscsi_init_grp_delete_netmask(ig, netmask1); + CU_ASSERT(rc == 0); + + imask = iscsi_init_grp_find_netmask(ig, netmask1); + CU_ASSERT(imask == NULL); + + rc = iscsi_init_grp_delete_netmask(ig, netmask2); + CU_ASSERT(rc == 0); + + imask = iscsi_init_grp_find_netmask(ig, netmask2); + CU_ASSERT(imask == NULL); + + iscsi_init_grp_destroy(ig); +} + +static void +add_netmask_fail_case(void) +{ + int rc; + struct spdk_iscsi_init_grp *ig; + struct spdk_iscsi_initiator_netmask *imask; + char *netmask1 = "192.168.2.0"; + + ig = iscsi_init_grp_create(1); + CU_ASSERT(ig != NULL); + + /* add an netmask to the full netmask list */ + ig->nnetmasks = MAX_NETMASK; + + rc = iscsi_init_grp_add_netmask(ig, netmask1); + CU_ASSERT(rc != 0); + + ig->nnetmasks = 0; + + /* add the same netmask to the netmask list twice */ + rc = iscsi_init_grp_add_netmask(ig, netmask1); + CU_ASSERT(rc == 0); + + rc = iscsi_init_grp_add_netmask(ig, netmask1); + CU_ASSERT(rc != 0); + + /* restore the initial state */ + rc = iscsi_init_grp_delete_netmask(ig, netmask1); + CU_ASSERT(rc == 0); + + imask = iscsi_init_grp_find_netmask(ig, netmask1); + CU_ASSERT(imask == NULL); + + iscsi_init_grp_destroy(ig); +} + +static void +delete_all_netmasks_success_case(void) +{ + int rc; + struct spdk_iscsi_init_grp *ig; + struct spdk_iscsi_initiator_netmask *imask; + char *netmask1 = "192.168.2.0"; + char *netmask2 = "192.168.2.1"; + + ig = iscsi_init_grp_create(1); + CU_ASSERT(ig != NULL); + + /* add two different netmasks to the empty netmask list */ + rc = iscsi_init_grp_add_netmask(ig, netmask1); + CU_ASSERT(rc == 0); + + rc = iscsi_init_grp_add_netmask(ig, netmask2); + CU_ASSERT(rc == 0); + + /* delete all netmasks */ + iscsi_init_grp_delete_all_netmasks(ig); + + /* check if two netmasks are deleted correctly. */ + imask = iscsi_init_grp_find_netmask(ig, netmask1); + CU_ASSERT(imask == NULL); + + imask = iscsi_init_grp_find_netmask(ig, netmask2); + CU_ASSERT(imask == NULL); + + /* restore the initial state */ + iscsi_init_grp_destroy(ig); +} + +static void +initiator_name_overwrite_all_to_any_case(void) +{ + int rc; + struct spdk_iscsi_init_grp *ig; + struct spdk_iscsi_initiator_name *iname; + char *all = "ALL"; + char *any = "ANY"; + char *all_not = "!ALL"; + char *any_not = "!ANY"; + + ig = iscsi_init_grp_create(1); + CU_ASSERT(ig != NULL); + + rc = iscsi_init_grp_add_initiator(ig, all); + CU_ASSERT(rc == 0); + + iname = iscsi_init_grp_find_initiator(ig, all); + CU_ASSERT(iname == NULL); + + iname = iscsi_init_grp_find_initiator(ig, any); + CU_ASSERT(iname != NULL); + + rc = iscsi_init_grp_delete_initiator(ig, any); + CU_ASSERT(rc == 0); + + rc = iscsi_init_grp_add_initiator(ig, all_not); + CU_ASSERT(rc == 0); + + iname = iscsi_init_grp_find_initiator(ig, all_not); + CU_ASSERT(iname == NULL); + + iname = iscsi_init_grp_find_initiator(ig, any_not); + CU_ASSERT(iname != NULL); + + rc = iscsi_init_grp_delete_initiator(ig, any_not); + CU_ASSERT(rc == 0); + + iscsi_init_grp_destroy(ig); +} + +static void +netmask_overwrite_all_to_any_case(void) +{ + int rc; + struct spdk_iscsi_init_grp *ig; + struct spdk_iscsi_initiator_netmask *imask; + char *all = "ALL"; + char *any = "ANY"; + + ig = iscsi_init_grp_create(1); + CU_ASSERT(ig != NULL); + + rc = iscsi_init_grp_add_netmask(ig, all); + CU_ASSERT(rc == 0); + + imask = iscsi_init_grp_find_netmask(ig, all); + CU_ASSERT(imask == NULL); + + imask = iscsi_init_grp_find_netmask(ig, any); + CU_ASSERT(imask != NULL); + + rc = iscsi_init_grp_delete_netmask(ig, any); + CU_ASSERT(rc == 0); + + iscsi_init_grp_destroy(ig); +} + +static void +add_delete_initiator_names_case(void) +{ + int rc, i; + struct spdk_iscsi_init_grp *ig; + struct spdk_iscsi_initiator_name *iname; + char *names[3] = {"iqn.2018-02.spdk.io:0001", "iqn.2018-02.spdk.io:0002", "iqn.2018-02.spdk.io:0003"}; + + ig = iscsi_init_grp_create(1); + SPDK_CU_ASSERT_FATAL(ig != NULL); + + rc = iscsi_init_grp_add_initiators(ig, 3, names); + CU_ASSERT(rc == 0); + + for (i = 0; i < 3; i++) { + iname = iscsi_init_grp_find_initiator(ig, names[i]); + CU_ASSERT(iname != NULL); + } + + rc = iscsi_init_grp_delete_initiators(ig, 3, names); + CU_ASSERT(rc == 0); + + if (ig != NULL) { + CU_ASSERT(TAILQ_EMPTY(&ig->initiator_head)); + } + + iscsi_init_grp_destroy(ig); +} + +static void +add_duplicated_initiator_names_case(void) +{ + int rc; + struct spdk_iscsi_init_grp *ig; + char *names[3] = {"iqn.2018-02.spdk.io:0001", "iqn.2018-02.spdk.io:0002", "iqn.2018-02.spdk.io:0001"}; + + ig = iscsi_init_grp_create(1); + SPDK_CU_ASSERT_FATAL(ig != NULL); + + rc = iscsi_init_grp_add_initiators(ig, 3, names); + CU_ASSERT(rc != 0); + + if (ig != NULL) { + CU_ASSERT(TAILQ_EMPTY(&ig->initiator_head)); + } + + iscsi_init_grp_destroy(ig); +} + +static void +delete_nonexisting_initiator_names_case(void) +{ + int rc, i; + struct spdk_iscsi_init_grp *ig; + struct spdk_iscsi_initiator_name *iname; + char *names1[3] = {"iqn.2018-02.spdk.io:0001", "iqn.2018-02.spdk.io:0002", "iqn.2018-02.spdk.io:0003"}; + char *names2[3] = {"iqn.2018-02.spdk.io:0001", "iqn.2018-02.spdk.io:0002", "iqn.2018-02.spdk.io:0004"}; + + ig = iscsi_init_grp_create(1); + SPDK_CU_ASSERT_FATAL(ig != NULL); + + rc = iscsi_init_grp_add_initiators(ig, 3, names1); + CU_ASSERT(rc == 0); + + for (i = 0; i < 3; i++) { + iname = iscsi_init_grp_find_initiator(ig, names1[i]); + CU_ASSERT(iname != NULL); + } + + rc = iscsi_init_grp_delete_initiators(ig, 3, names2); + CU_ASSERT(rc != 0); + + for (i = 0; i < 3; i++) { + iname = iscsi_init_grp_find_initiator(ig, names1[i]); + CU_ASSERT(iname != NULL); + } + + rc = iscsi_init_grp_delete_initiators(ig, 3, names1); + CU_ASSERT(rc == 0); + + if (ig != NULL) { + CU_ASSERT(TAILQ_EMPTY(&ig->initiator_head)); + } + + iscsi_init_grp_destroy(ig); +} + +static void +add_delete_netmasks_case(void) +{ + int rc, i; + struct spdk_iscsi_init_grp *ig; + struct spdk_iscsi_initiator_netmask *netmask; + char *netmasks[3] = {"192.168.2.0", "192.168.2.1", "192.168.2.2"}; + + ig = iscsi_init_grp_create(1); + SPDK_CU_ASSERT_FATAL(ig != NULL); + + rc = iscsi_init_grp_add_netmasks(ig, 3, netmasks); + CU_ASSERT(rc == 0); + + for (i = 0; i < 3; i++) { + netmask = iscsi_init_grp_find_netmask(ig, netmasks[i]); + CU_ASSERT(netmask != NULL); + } + + rc = iscsi_init_grp_delete_netmasks(ig, 3, netmasks); + CU_ASSERT(rc == 0); + + if (ig != NULL) { + CU_ASSERT(TAILQ_EMPTY(&ig->netmask_head)); + } + + iscsi_init_grp_destroy(ig); +} + +static void +add_duplicated_netmasks_case(void) +{ + int rc; + struct spdk_iscsi_init_grp *ig; + char *netmasks[3] = {"192.168.2.0", "192.168.2.1", "192.168.2.0"}; + + ig = iscsi_init_grp_create(1); + SPDK_CU_ASSERT_FATAL(ig != NULL); + + rc = iscsi_init_grp_add_netmasks(ig, 3, netmasks); + CU_ASSERT(rc != 0); + + if (ig != NULL) { + CU_ASSERT(TAILQ_EMPTY(&ig->netmask_head)); + } + + iscsi_init_grp_destroy(ig); +} + +static void +delete_nonexisting_netmasks_case(void) +{ + int rc, i; + struct spdk_iscsi_init_grp *ig; + struct spdk_iscsi_initiator_netmask *netmask; + char *netmasks1[3] = {"192.168.2.0", "192.168.2.1", "192.168.2.2"}; + char *netmasks2[3] = {"192.168.2.0", "192.168.2.1", "192.168.2.3"}; + + ig = iscsi_init_grp_create(1); + SPDK_CU_ASSERT_FATAL(ig != NULL); + + rc = iscsi_init_grp_add_netmasks(ig, 3, netmasks1); + CU_ASSERT(rc == 0); + + for (i = 0; i < 3; i++) { + netmask = iscsi_init_grp_find_netmask(ig, netmasks1[i]); + CU_ASSERT(netmask != NULL); + } + + rc = iscsi_init_grp_delete_netmasks(ig, 3, netmasks2); + CU_ASSERT(rc != 0); + + for (i = 0; i < 3; i++) { + netmask = iscsi_init_grp_find_netmask(ig, netmasks1[i]); + CU_ASSERT(netmask != NULL); + } + + rc = iscsi_init_grp_delete_netmasks(ig, 3, netmasks1); + CU_ASSERT(rc == 0); + + if (ig != NULL) { + CU_ASSERT(TAILQ_EMPTY(&ig->netmask_head)); + } + + iscsi_init_grp_destroy(ig); +} + + +int +main(int argc, char **argv) +{ + CU_pSuite suite = NULL; + unsigned int num_failures; + + if (argc < 2) { + fprintf(stderr, "usage: %s <config file>\n", argv[0]); + exit(1); + } + + CU_set_error_action(CUEA_ABORT); + CU_initialize_registry(); + + config_file = argv[1]; + + suite = CU_add_suite("init_grp_suite", test_setup, NULL); + + CU_ADD_TEST(suite, create_from_config_file_cases); + CU_ADD_TEST(suite, create_initiator_group_success_case); + CU_ADD_TEST(suite, find_initiator_group_success_case); + CU_ADD_TEST(suite, register_initiator_group_twice_case); + CU_ADD_TEST(suite, add_initiator_name_success_case); + CU_ADD_TEST(suite, add_initiator_name_fail_case); + CU_ADD_TEST(suite, delete_all_initiator_names_success_case); + CU_ADD_TEST(suite, add_netmask_success_case); + CU_ADD_TEST(suite, add_netmask_fail_case); + CU_ADD_TEST(suite, delete_all_netmasks_success_case); + CU_ADD_TEST(suite, initiator_name_overwrite_all_to_any_case); + CU_ADD_TEST(suite, netmask_overwrite_all_to_any_case); + CU_ADD_TEST(suite, add_delete_initiator_names_case); + CU_ADD_TEST(suite, add_duplicated_initiator_names_case); + CU_ADD_TEST(suite, delete_nonexisting_initiator_names_case); + CU_ADD_TEST(suite, add_delete_netmasks_case); + CU_ADD_TEST(suite, add_duplicated_netmasks_case); + CU_ADD_TEST(suite, delete_nonexisting_netmasks_case); + + CU_basic_set_mode(CU_BRM_VERBOSE); + CU_basic_run_tests(); + num_failures = CU_get_number_of_failures(); + CU_cleanup_registry(); + return num_failures; +} diff --git a/src/spdk/test/unit/lib/iscsi/iscsi.c/.gitignore b/src/spdk/test/unit/lib/iscsi/iscsi.c/.gitignore new file mode 100644 index 000000000..4d41887c8 --- /dev/null +++ b/src/spdk/test/unit/lib/iscsi/iscsi.c/.gitignore @@ -0,0 +1 @@ +iscsi_ut diff --git a/src/spdk/test/unit/lib/iscsi/iscsi.c/Makefile b/src/spdk/test/unit/lib/iscsi/iscsi.c/Makefile new file mode 100644 index 000000000..66d7334a4 --- /dev/null +++ b/src/spdk/test/unit/lib/iscsi/iscsi.c/Makefile @@ -0,0 +1,46 @@ +# +# BSD LICENSE +# +# Copyright (c) Intel Corporation. +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in +# the documentation and/or other materials provided with the +# distribution. +# * Neither the name of Intel Corporation nor the names of its +# contributors may be used to endorse or promote products derived +# from this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +# + +SPDK_ROOT_DIR := $(abspath $(CURDIR)/../../../../..) + +SPDK_LIB_LIST = conf util + +SCSI_OBJS = port +ISCSI_OBJS = md5 param +LIBS += $(SCSI_OBJS:%=$(SPDK_ROOT_DIR)/lib/scsi/%.o) +LIBS += $(ISCSI_OBJS:%=$(SPDK_ROOT_DIR)/lib/iscsi/%.o) +LIBS += -lcunit + +TEST_FILE = iscsi_ut.c + +include $(SPDK_ROOT_DIR)/mk/spdk.unittest.mk diff --git a/src/spdk/test/unit/lib/iscsi/iscsi.c/iscsi_ut.c b/src/spdk/test/unit/lib/iscsi/iscsi.c/iscsi_ut.c new file mode 100644 index 000000000..f96afd999 --- /dev/null +++ b/src/spdk/test/unit/lib/iscsi/iscsi.c/iscsi_ut.c @@ -0,0 +1,2024 @@ +/*- + * BSD LICENSE + * + * Copyright (c) Intel Corporation. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Intel Corporation nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "spdk/stdinc.h" + +#include "spdk/endian.h" +#include "spdk/scsi.h" +#include "spdk_cunit.h" + +#include "CUnit/Basic.h" + +#include "iscsi/iscsi.c" + +#include "../common.c" +#include "iscsi/portal_grp.h" +#include "scsi/scsi_internal.h" +#include "common/lib/test_env.c" + +#include "spdk_internal/mock.h" + +#define UT_TARGET_NAME1 "iqn.2017-11.spdk.io:t0001" +#define UT_TARGET_NAME2 "iqn.2017-11.spdk.io:t0002" +#define UT_INITIATOR_NAME1 "iqn.2017-11.spdk.io:i0001" +#define UT_INITIATOR_NAME2 "iqn.2017-11.spdk.io:i0002" +#define UT_ISCSI_TSIH 256 + +struct spdk_iscsi_tgt_node g_tgt; + +struct spdk_iscsi_tgt_node * +iscsi_find_tgt_node(const char *target_name) +{ + if (strcasecmp(target_name, UT_TARGET_NAME1) == 0) { + g_tgt.dev = NULL; + return (struct spdk_iscsi_tgt_node *)&g_tgt; + } else { + return NULL; + } +} + +bool +iscsi_tgt_node_access(struct spdk_iscsi_conn *conn, + struct spdk_iscsi_tgt_node *target, + const char *iqn, const char *addr) +{ + if (strcasecmp(conn->initiator_name, UT_INITIATOR_NAME1) == 0) { + return true; + } else { + return false; + } +} + +DEFINE_STUB(iscsi_send_tgts, int, + (struct spdk_iscsi_conn *conn, const char *iiqn, const char *iaddr, + const char *tiqn, uint8_t *data, int alloc_len, int data_len), + 0); + +DEFINE_STUB(iscsi_tgt_node_is_destructed, bool, + (struct spdk_iscsi_tgt_node *target), false); + +DEFINE_STUB_V(iscsi_portal_grp_close_all, (void)); + +DEFINE_STUB_V(iscsi_conn_schedule, (struct spdk_iscsi_conn *conn)); + +DEFINE_STUB_V(iscsi_conn_free_pdu, + (struct spdk_iscsi_conn *conn, struct spdk_iscsi_pdu *pdu)); + +DEFINE_STUB_V(iscsi_conn_pdu_generic_complete, (void *cb_arg)); + +DEFINE_STUB(iscsi_conn_handle_queued_datain_tasks, int, + (struct spdk_iscsi_conn *conn), 0); + +DEFINE_STUB(iscsi_conn_abort_queued_datain_task, int, + (struct spdk_iscsi_conn *conn, uint32_t ref_task_tag), 0); + +DEFINE_STUB(iscsi_conn_abort_queued_datain_tasks, int, + (struct spdk_iscsi_conn *conn, struct spdk_scsi_lun *lun, + struct spdk_iscsi_pdu *pdu), 0); + +DEFINE_STUB(iscsi_chap_get_authinfo, int, + (struct iscsi_chap_auth *auth, const char *authuser, int ag_tag), + 0); + +DEFINE_STUB(spdk_sock_set_recvbuf, int, (struct spdk_sock *sock, int sz), 0); + +int +spdk_scsi_lun_get_id(const struct spdk_scsi_lun *lun) +{ + return lun->id; +} + +DEFINE_STUB(spdk_scsi_lun_is_removing, bool, (const struct spdk_scsi_lun *lun), + true); + +struct spdk_scsi_lun * +spdk_scsi_dev_get_lun(struct spdk_scsi_dev *dev, int lun_id) +{ + if (lun_id < 0 || lun_id >= SPDK_SCSI_DEV_MAX_LUN) { + return NULL; + } + + return dev->lun[lun_id]; +} + +DEFINE_STUB(spdk_scsi_lun_id_int_to_fmt, uint64_t, (int lun_id), 0); + +DEFINE_STUB(spdk_scsi_lun_id_fmt_to_int, int, (uint64_t lun_fmt), 0); + +DEFINE_STUB(spdk_scsi_lun_get_dif_ctx, bool, + (struct spdk_scsi_lun *lun, struct spdk_scsi_task *task, + struct spdk_dif_ctx *dif_ctx), false); + +static void +op_login_check_target_test(void) +{ + struct spdk_iscsi_conn conn = {}; + struct spdk_iscsi_pdu rsp_pdu = {}; + struct spdk_iscsi_tgt_node *target; + int rc; + + /* expect success */ + snprintf(conn.initiator_name, sizeof(conn.initiator_name), + "%s", UT_INITIATOR_NAME1); + + rc = iscsi_op_login_check_target(&conn, &rsp_pdu, + UT_TARGET_NAME1, &target); + CU_ASSERT(rc == 0); + + /* expect failure */ + snprintf(conn.initiator_name, sizeof(conn.initiator_name), + "%s", UT_INITIATOR_NAME1); + + rc = iscsi_op_login_check_target(&conn, &rsp_pdu, + UT_TARGET_NAME2, &target); + CU_ASSERT(rc != 0); + + /* expect failure */ + snprintf(conn.initiator_name, sizeof(conn.initiator_name), + "%s", UT_INITIATOR_NAME2); + + rc = iscsi_op_login_check_target(&conn, &rsp_pdu, + UT_TARGET_NAME1, &target); + CU_ASSERT(rc != 0); +} + +static void +op_login_session_normal_test(void) +{ + struct spdk_iscsi_conn conn = {}; + struct spdk_iscsi_portal portal = {}; + struct spdk_iscsi_portal_grp group = {}; + struct spdk_iscsi_pdu rsp_pdu = {}; + struct iscsi_bhs_login_rsp *rsph; + struct spdk_iscsi_sess sess = {}; + struct iscsi_param param = {}; + int rc; + + /* setup related data structures */ + rsph = (struct iscsi_bhs_login_rsp *)&rsp_pdu.bhs; + rsph->tsih = 0; + memset(rsph->isid, 0, sizeof(rsph->isid)); + conn.portal = &portal; + portal.group = &group; + conn.portal->group->tag = 0; + conn.params = NULL; + + /* expect failure: NULL params for target name */ + rc = iscsi_op_login_session_normal(&conn, &rsp_pdu, UT_INITIATOR_NAME1, + NULL, 0); + CU_ASSERT(rc != 0); + CU_ASSERT(rsph->status_class == ISCSI_CLASS_INITIATOR_ERROR); + CU_ASSERT(rsph->status_detail == ISCSI_LOGIN_MISSING_PARMS); + + /* expect failure: incorrect key for target name */ + param.next = NULL; + rc = iscsi_op_login_session_normal(&conn, &rsp_pdu, UT_INITIATOR_NAME1, + ¶m, 0); + CU_ASSERT(rc != 0); + CU_ASSERT(rsph->status_class == ISCSI_CLASS_INITIATOR_ERROR); + CU_ASSERT(rsph->status_detail == ISCSI_LOGIN_MISSING_PARMS); + + /* expect failure: NULL target name */ + param.key = "TargetName"; + param.val = NULL; + rc = iscsi_op_login_session_normal(&conn, &rsp_pdu, UT_INITIATOR_NAME1, + ¶m, 0); + CU_ASSERT(rc != 0); + CU_ASSERT(rsph->status_class == ISCSI_CLASS_INITIATOR_ERROR); + CU_ASSERT(rsph->status_detail == ISCSI_LOGIN_MISSING_PARMS); + + /* expect failure: session not found */ + param.key = "TargetName"; + param.val = "iqn.2017-11.spdk.io:t0001"; + snprintf(conn.initiator_name, sizeof(conn.initiator_name), + "%s", UT_INITIATOR_NAME1); + rsph->tsih = 1; /* to append the session */ + rc = iscsi_op_login_session_normal(&conn, &rsp_pdu, UT_INITIATOR_NAME1, + ¶m, 0); + CU_ASSERT(conn.target_port == NULL); + CU_ASSERT(rc != 0); + CU_ASSERT(rsph->status_class == ISCSI_CLASS_INITIATOR_ERROR); + CU_ASSERT(rsph->status_detail == ISCSI_LOGIN_CONN_ADD_FAIL); + + /* expect failure: session found while tag is wrong */ + g_iscsi.MaxSessions = UT_ISCSI_TSIH * 2; + g_iscsi.session = calloc(1, sizeof(void *) * g_iscsi.MaxSessions); + g_iscsi.session[UT_ISCSI_TSIH - 1] = &sess; + sess.tsih = UT_ISCSI_TSIH; + rsph->tsih = UT_ISCSI_TSIH >> 8; /* to append the session */ + sess.tag = 1; + rc = iscsi_op_login_session_normal(&conn, &rsp_pdu, UT_INITIATOR_NAME1, + ¶m, 0); + CU_ASSERT(conn.target_port == NULL); + CU_ASSERT(rc != 0); + CU_ASSERT(rsph->status_class == ISCSI_CLASS_INITIATOR_ERROR); + CU_ASSERT(rsph->status_detail == ISCSI_LOGIN_CONN_ADD_FAIL); + + /* expect suceess: drop the session */ + rsph->tsih = 0; /* to create the session */ + g_iscsi.AllowDuplicateIsid = false; + rc = iscsi_op_login_session_normal(&conn, &rsp_pdu, UT_INITIATOR_NAME1, + ¶m, 0); + CU_ASSERT(rc == 0); + + /* expect suceess: create the session */ + rsph->tsih = 0; /* to create the session */ + g_iscsi.AllowDuplicateIsid = true; + rc = iscsi_op_login_session_normal(&conn, &rsp_pdu, UT_INITIATOR_NAME1, + ¶m, 0); + CU_ASSERT(rc == 0); + + free(g_iscsi.session); +} + +static void +maxburstlength_test(void) +{ + struct spdk_iscsi_sess sess = {}; + struct spdk_iscsi_conn conn = {}; + struct spdk_scsi_dev dev = {}; + struct spdk_scsi_lun lun = {}; + struct spdk_iscsi_pdu *req_pdu, *data_out_pdu, *r2t_pdu; + struct iscsi_bhs_scsi_req *req; + struct iscsi_bhs_r2t *r2t; + struct iscsi_bhs_data_out *data_out; + struct spdk_iscsi_pdu *response_pdu; + int rc; + + req_pdu = iscsi_get_pdu(&conn); + data_out_pdu = iscsi_get_pdu(&conn); + + sess.ExpCmdSN = 0; + sess.MaxCmdSN = 64; + sess.session_type = SESSION_TYPE_NORMAL; + sess.MaxBurstLength = 1024; + + lun.id = 0; + + dev.lun[0] = &lun; + + conn.full_feature = 1; + conn.sess = &sess; + conn.dev = &dev; + conn.state = ISCSI_CONN_STATE_RUNNING; + TAILQ_INIT(&conn.write_pdu_list); + TAILQ_INIT(&conn.active_r2t_tasks); + + req_pdu->bhs.opcode = ISCSI_OP_SCSI; + req_pdu->data_segment_len = 0; + + req = (struct iscsi_bhs_scsi_req *)&req_pdu->bhs; + + to_be32(&req->cmd_sn, 0); + to_be32(&req->expected_data_xfer_len, 1028); + to_be32(&req->itt, 0x1234); + req->write_bit = 1; + req->final_bit = 1; + + rc = iscsi_pdu_hdr_handle(&conn, req_pdu); + if (rc == 0 && !req_pdu->is_rejected) { + rc = iscsi_pdu_payload_handle(&conn, req_pdu); + } + CU_ASSERT(rc == 0); + + response_pdu = TAILQ_FIRST(&g_write_pdu_list); + SPDK_CU_ASSERT_FATAL(response_pdu != NULL); + + /* + * Confirm that a correct R2T reply was sent in response to the + * SCSI request. + */ + TAILQ_REMOVE(&g_write_pdu_list, response_pdu, tailq); + CU_ASSERT(response_pdu->bhs.opcode == ISCSI_OP_R2T); + r2t = (struct iscsi_bhs_r2t *)&response_pdu->bhs; + CU_ASSERT(from_be32(&r2t->desired_xfer_len) == 1024); + CU_ASSERT(from_be32(&r2t->buffer_offset) == 0); + CU_ASSERT(from_be32(&r2t->itt) == 0x1234); + + data_out_pdu->bhs.opcode = ISCSI_OP_SCSI_DATAOUT; + data_out_pdu->bhs.flags = ISCSI_FLAG_FINAL; + data_out_pdu->data_segment_len = 1028; + data_out = (struct iscsi_bhs_data_out *)&data_out_pdu->bhs; + data_out->itt = r2t->itt; + data_out->ttt = r2t->ttt; + DSET24(data_out->data_segment_len, 1028); + + rc = iscsi_pdu_hdr_handle(&conn, data_out_pdu); + if (rc == 0 && !data_out_pdu->is_rejected) { + rc = iscsi_pdu_payload_handle(&conn, data_out_pdu); + } + CU_ASSERT(rc == SPDK_ISCSI_CONNECTION_FATAL); + + SPDK_CU_ASSERT_FATAL(response_pdu->task != NULL); + iscsi_task_disassociate_pdu(response_pdu->task); + iscsi_task_put(response_pdu->task); + iscsi_put_pdu(response_pdu); + + r2t_pdu = TAILQ_FIRST(&g_write_pdu_list); + CU_ASSERT(r2t_pdu != NULL); + TAILQ_REMOVE(&g_write_pdu_list, r2t_pdu, tailq); + iscsi_put_pdu(r2t_pdu); + + iscsi_put_pdu(data_out_pdu); + iscsi_put_pdu(req_pdu); +} + +static void +underflow_for_read_transfer_test(void) +{ + struct spdk_iscsi_sess sess = {}; + struct spdk_iscsi_conn conn = {}; + struct spdk_iscsi_task task = {}; + struct spdk_scsi_dev dev = {}; + struct spdk_scsi_lun lun = {}; + struct spdk_iscsi_pdu *pdu; + struct iscsi_bhs_scsi_req *scsi_req; + struct iscsi_bhs_data_in *datah; + uint32_t residual_count = 0; + + sess.MaxBurstLength = SPDK_ISCSI_MAX_BURST_LENGTH; + + conn.sess = &sess; + conn.MaxRecvDataSegmentLength = 8192; + + dev.lun[0] = &lun; + conn.dev = &dev; + + pdu = iscsi_get_pdu(&conn); + SPDK_CU_ASSERT_FATAL(pdu != NULL); + + scsi_req = (struct iscsi_bhs_scsi_req *)&pdu->bhs; + scsi_req->read_bit = 1; + + iscsi_task_set_pdu(&task, pdu); + task.parent = NULL; + + task.scsi.iovs = &task.scsi.iov; + task.scsi.iovcnt = 1; + task.scsi.length = 512; + task.scsi.transfer_len = 512; + task.bytes_completed = 512; + task.scsi.data_transferred = 256; + task.scsi.status = SPDK_SCSI_STATUS_GOOD; + + iscsi_task_response(&conn, &task); + iscsi_put_pdu(pdu); + + /* + * In this case, a SCSI Data-In PDU should contain the Status + * for the data transfer. + */ + to_be32(&residual_count, 256); + + pdu = TAILQ_FIRST(&g_write_pdu_list); + SPDK_CU_ASSERT_FATAL(pdu != NULL); + + CU_ASSERT(pdu->bhs.opcode == ISCSI_OP_SCSI_DATAIN); + + datah = (struct iscsi_bhs_data_in *)&pdu->bhs; + + CU_ASSERT(datah->flags == (ISCSI_DATAIN_UNDERFLOW | ISCSI_FLAG_FINAL | ISCSI_DATAIN_STATUS)); + CU_ASSERT(datah->res_cnt == residual_count); + + TAILQ_REMOVE(&g_write_pdu_list, pdu, tailq); + iscsi_put_pdu(pdu); + + CU_ASSERT(TAILQ_EMPTY(&g_write_pdu_list)); +} + +static void +underflow_for_zero_read_transfer_test(void) +{ + struct spdk_iscsi_sess sess = {}; + struct spdk_iscsi_conn conn = {}; + struct spdk_iscsi_task task = {}; + struct spdk_scsi_dev dev = {}; + struct spdk_scsi_lun lun = {}; + struct spdk_iscsi_pdu *pdu; + struct iscsi_bhs_scsi_req *scsi_req; + struct iscsi_bhs_scsi_resp *resph; + uint32_t residual_count = 0, data_segment_len; + + sess.MaxBurstLength = SPDK_ISCSI_MAX_BURST_LENGTH; + + conn.sess = &sess; + conn.MaxRecvDataSegmentLength = 8192; + + dev.lun[0] = &lun; + conn.dev = &dev; + + pdu = iscsi_get_pdu(&conn); + SPDK_CU_ASSERT_FATAL(pdu != NULL); + + scsi_req = (struct iscsi_bhs_scsi_req *)&pdu->bhs; + scsi_req->read_bit = 1; + + iscsi_task_set_pdu(&task, pdu); + task.parent = NULL; + + task.scsi.length = 512; + task.scsi.transfer_len = 512; + task.bytes_completed = 512; + task.scsi.data_transferred = 0; + task.scsi.status = SPDK_SCSI_STATUS_GOOD; + + iscsi_task_response(&conn, &task); + iscsi_put_pdu(pdu); + + /* + * In this case, only a SCSI Response PDU is expected and + * underflow must be set in it. + * */ + to_be32(&residual_count, 512); + + pdu = TAILQ_FIRST(&g_write_pdu_list); + SPDK_CU_ASSERT_FATAL(pdu != NULL); + + CU_ASSERT(pdu->bhs.opcode == ISCSI_OP_SCSI_RSP); + + resph = (struct iscsi_bhs_scsi_resp *)&pdu->bhs; + + CU_ASSERT(resph->flags == (ISCSI_SCSI_UNDERFLOW | 0x80)); + + data_segment_len = DGET24(resph->data_segment_len); + CU_ASSERT(data_segment_len == 0); + CU_ASSERT(resph->res_cnt == residual_count); + + TAILQ_REMOVE(&g_write_pdu_list, pdu, tailq); + iscsi_put_pdu(pdu); + + CU_ASSERT(TAILQ_EMPTY(&g_write_pdu_list)); +} + +static void +underflow_for_request_sense_test(void) +{ + struct spdk_iscsi_sess sess = {}; + struct spdk_iscsi_conn conn = {}; + struct spdk_iscsi_task task = {}; + struct spdk_scsi_dev dev = {}; + struct spdk_scsi_lun lun = {}; + struct spdk_iscsi_pdu *pdu1, *pdu2; + struct iscsi_bhs_scsi_req *scsi_req; + struct iscsi_bhs_data_in *datah; + struct iscsi_bhs_scsi_resp *resph; + uint32_t residual_count = 0, data_segment_len; + + sess.MaxBurstLength = SPDK_ISCSI_MAX_BURST_LENGTH; + + conn.sess = &sess; + conn.MaxRecvDataSegmentLength = 8192; + + dev.lun[0] = &lun; + conn.dev = &dev; + + pdu1 = iscsi_get_pdu(&conn); + SPDK_CU_ASSERT_FATAL(pdu1 != NULL); + + scsi_req = (struct iscsi_bhs_scsi_req *)&pdu1->bhs; + scsi_req->read_bit = 1; + + iscsi_task_set_pdu(&task, pdu1); + task.parent = NULL; + + task.scsi.iovs = &task.scsi.iov; + task.scsi.iovcnt = 1; + task.scsi.length = 512; + task.scsi.transfer_len = 512; + task.bytes_completed = 512; + + task.scsi.sense_data_len = 18; + task.scsi.data_transferred = 18; + task.scsi.status = SPDK_SCSI_STATUS_GOOD; + + iscsi_task_response(&conn, &task); + iscsi_put_pdu(pdu1); + + /* + * In this case, a SCSI Data-In PDU and a SCSI Response PDU are returned. + * Sense data are set both in payload and sense area. + * The SCSI Data-In PDU sets FINAL and the SCSI Response PDU sets UNDERFLOW. + * + * Probably there will be different implementation but keeping current SPDK + * implementation by adding UT will be valuable for any implementation. + */ + to_be32(&residual_count, 494); + + pdu1 = TAILQ_FIRST(&g_write_pdu_list); + SPDK_CU_ASSERT_FATAL(pdu1 != NULL); + + CU_ASSERT(pdu1->bhs.opcode == ISCSI_OP_SCSI_DATAIN); + + datah = (struct iscsi_bhs_data_in *)&pdu1->bhs; + + CU_ASSERT(datah->flags == ISCSI_FLAG_FINAL); + + data_segment_len = DGET24(datah->data_segment_len); + CU_ASSERT(data_segment_len == 18); + CU_ASSERT(datah->res_cnt == 0); + + TAILQ_REMOVE(&g_write_pdu_list, pdu1, tailq); + iscsi_put_pdu(pdu1); + + pdu2 = TAILQ_FIRST(&g_write_pdu_list); + /* inform scan-build (clang 6) that these pointers are not the same */ + SPDK_CU_ASSERT_FATAL(pdu1 != pdu2); + SPDK_CU_ASSERT_FATAL(pdu2 != NULL); + + CU_ASSERT(pdu2->bhs.opcode == ISCSI_OP_SCSI_RSP); + + resph = (struct iscsi_bhs_scsi_resp *)&pdu2->bhs; + + CU_ASSERT(resph->flags == (ISCSI_SCSI_UNDERFLOW | 0x80)); + + data_segment_len = DGET24(resph->data_segment_len); + CU_ASSERT(data_segment_len == task.scsi.sense_data_len + 2); + CU_ASSERT(resph->res_cnt == residual_count); + + TAILQ_REMOVE(&g_write_pdu_list, pdu2, tailq); + iscsi_put_pdu(pdu2); + + CU_ASSERT(TAILQ_EMPTY(&g_write_pdu_list)); +} + +static void +underflow_for_check_condition_test(void) +{ + struct spdk_iscsi_sess sess = {}; + struct spdk_iscsi_conn conn = {}; + struct spdk_iscsi_task task = {}; + struct spdk_scsi_dev dev = {}; + struct spdk_scsi_lun lun = {}; + struct spdk_iscsi_pdu *pdu; + struct iscsi_bhs_scsi_req *scsi_req; + struct iscsi_bhs_scsi_resp *resph; + uint32_t data_segment_len; + + sess.MaxBurstLength = SPDK_ISCSI_MAX_BURST_LENGTH; + + conn.sess = &sess; + conn.MaxRecvDataSegmentLength = 8192; + + dev.lun[0] = &lun; + conn.dev = &dev; + + pdu = iscsi_get_pdu(&conn); + SPDK_CU_ASSERT_FATAL(pdu != NULL); + + scsi_req = (struct iscsi_bhs_scsi_req *)&pdu->bhs; + scsi_req->read_bit = 1; + + iscsi_task_set_pdu(&task, pdu); + task.parent = NULL; + + task.scsi.iovs = &task.scsi.iov; + task.scsi.iovcnt = 1; + task.scsi.length = 512; + task.scsi.transfer_len = 512; + task.bytes_completed = 512; + + task.scsi.sense_data_len = 18; + task.scsi.data_transferred = 18; + task.scsi.status = SPDK_SCSI_STATUS_CHECK_CONDITION; + + iscsi_task_response(&conn, &task); + iscsi_put_pdu(pdu); + + /* + * In this case, a SCSI Response PDU is returned. + * Sense data is set in sense area. + * Underflow is not set. + */ + pdu = TAILQ_FIRST(&g_write_pdu_list); + SPDK_CU_ASSERT_FATAL(pdu != NULL); + + CU_ASSERT(pdu->bhs.opcode == ISCSI_OP_SCSI_RSP); + + resph = (struct iscsi_bhs_scsi_resp *)&pdu->bhs; + + CU_ASSERT(resph->flags == 0x80); + + data_segment_len = DGET24(resph->data_segment_len); + CU_ASSERT(data_segment_len == task.scsi.sense_data_len + 2); + CU_ASSERT(resph->res_cnt == 0); + + TAILQ_REMOVE(&g_write_pdu_list, pdu, tailq); + iscsi_put_pdu(pdu); + + CU_ASSERT(TAILQ_EMPTY(&g_write_pdu_list)); +} + +static void +add_transfer_task_test(void) +{ + struct spdk_iscsi_sess sess = {}; + struct spdk_iscsi_conn conn = {}; + struct spdk_iscsi_task task = {}; + struct spdk_iscsi_pdu *pdu, *tmp; + struct iscsi_bhs_r2t *r2th; + int rc, count = 0; + uint32_t buffer_offset, desired_xfer_len; + + sess.MaxBurstLength = SPDK_ISCSI_MAX_BURST_LENGTH; /* 1M */ + sess.MaxOutstandingR2T = DEFAULT_MAXR2T; /* 4 */ + + conn.sess = &sess; + TAILQ_INIT(&conn.queued_r2t_tasks); + TAILQ_INIT(&conn.active_r2t_tasks); + + pdu = iscsi_get_pdu(&conn); + SPDK_CU_ASSERT_FATAL(pdu != NULL); + + pdu->data_segment_len = SPDK_ISCSI_MAX_RECV_DATA_SEGMENT_LENGTH; /* 64K */ + task.scsi.transfer_len = 16 * 1024 * 1024; + iscsi_task_set_pdu(&task, pdu); + + /* The following tests if the task is queued because R2T tasks are full. */ + conn.pending_r2t = DEFAULT_MAXR2T; + + rc = add_transfer_task(&conn, &task); + + CU_ASSERT(rc == 0); + CU_ASSERT(TAILQ_FIRST(&conn.queued_r2t_tasks) == &task); + + TAILQ_REMOVE(&conn.queued_r2t_tasks, &task, link); + CU_ASSERT(TAILQ_EMPTY(&conn.queued_r2t_tasks)); + + /* The following tests if multiple R2Ts are issued. */ + conn.pending_r2t = 0; + + rc = add_transfer_task(&conn, &task); + + CU_ASSERT(rc == 0); + CU_ASSERT(TAILQ_FIRST(&conn.active_r2t_tasks) == &task); + + TAILQ_REMOVE(&conn.active_r2t_tasks, &task, link); + CU_ASSERT(TAILQ_EMPTY(&conn.active_r2t_tasks)); + + CU_ASSERT(conn.data_out_cnt == 255); + CU_ASSERT(conn.pending_r2t == 1); + CU_ASSERT(conn.ttt == 1); + + CU_ASSERT(task.data_out_cnt == 255); + CU_ASSERT(task.ttt == 1); + CU_ASSERT(task.outstanding_r2t == sess.MaxOutstandingR2T); + CU_ASSERT(task.next_r2t_offset == + pdu->data_segment_len + sess.MaxBurstLength * sess.MaxOutstandingR2T); + + + while (!TAILQ_EMPTY(&g_write_pdu_list)) { + tmp = TAILQ_FIRST(&g_write_pdu_list); + TAILQ_REMOVE(&g_write_pdu_list, tmp, tailq); + + r2th = (struct iscsi_bhs_r2t *)&tmp->bhs; + + buffer_offset = from_be32(&r2th->buffer_offset); + CU_ASSERT(buffer_offset == pdu->data_segment_len + sess.MaxBurstLength * count); + + desired_xfer_len = from_be32(&r2th->desired_xfer_len); + CU_ASSERT(desired_xfer_len == sess.MaxBurstLength); + + iscsi_put_pdu(tmp); + count++; + } + + CU_ASSERT(count == DEFAULT_MAXR2T); + + iscsi_put_pdu(pdu); +} + +static void +get_transfer_task_test(void) +{ + struct spdk_iscsi_sess sess = {}; + struct spdk_iscsi_conn conn = {}; + struct spdk_iscsi_task task1 = {}, task2 = {}, *task; + struct spdk_iscsi_pdu *pdu1, *pdu2, *pdu; + int rc; + + sess.MaxBurstLength = SPDK_ISCSI_MAX_RECV_DATA_SEGMENT_LENGTH; + sess.MaxOutstandingR2T = 1; + + conn.sess = &sess; + TAILQ_INIT(&conn.active_r2t_tasks); + + pdu1 = iscsi_get_pdu(&conn); + SPDK_CU_ASSERT_FATAL(pdu1 != NULL); + + pdu1->data_segment_len = SPDK_ISCSI_MAX_RECV_DATA_SEGMENT_LENGTH; + task1.scsi.transfer_len = SPDK_ISCSI_MAX_RECV_DATA_SEGMENT_LENGTH; + iscsi_task_set_pdu(&task1, pdu1); + + rc = add_transfer_task(&conn, &task1); + CU_ASSERT(rc == 0); + + pdu2 = iscsi_get_pdu(&conn); + SPDK_CU_ASSERT_FATAL(pdu2 != NULL); + + pdu2->data_segment_len = SPDK_ISCSI_MAX_RECV_DATA_SEGMENT_LENGTH; + task2.scsi.transfer_len = SPDK_ISCSI_MAX_RECV_DATA_SEGMENT_LENGTH; + iscsi_task_set_pdu(&task2, pdu2); + + rc = add_transfer_task(&conn, &task2); + CU_ASSERT(rc == 0); + + task = get_transfer_task(&conn, 1); + CU_ASSERT(task == &task1); + + task = get_transfer_task(&conn, 2); + CU_ASSERT(task == &task2); + + while (!TAILQ_EMPTY(&conn.active_r2t_tasks)) { + task = TAILQ_FIRST(&conn.active_r2t_tasks); + TAILQ_REMOVE(&conn.active_r2t_tasks, task, link); + } + + while (!TAILQ_EMPTY(&g_write_pdu_list)) { + pdu = TAILQ_FIRST(&g_write_pdu_list); + TAILQ_REMOVE(&g_write_pdu_list, pdu, tailq); + iscsi_put_pdu(pdu); + } + + iscsi_put_pdu(pdu2); + iscsi_put_pdu(pdu1); +} + +static void +del_transfer_task_test(void) +{ + struct spdk_iscsi_sess sess = {}; + struct spdk_iscsi_conn conn = {}; + struct spdk_iscsi_task *task1, *task2, *task3, *task4, *task5; + struct spdk_iscsi_pdu *pdu1, *pdu2, *pdu3, *pdu4, *pdu5, *pdu; + int rc; + + sess.MaxBurstLength = SPDK_ISCSI_MAX_RECV_DATA_SEGMENT_LENGTH; + sess.MaxOutstandingR2T = 1; + + conn.sess = &sess; + TAILQ_INIT(&conn.active_r2t_tasks); + TAILQ_INIT(&conn.queued_r2t_tasks); + + pdu1 = iscsi_get_pdu(&conn); + SPDK_CU_ASSERT_FATAL(pdu1 != NULL); + + pdu1->data_segment_len = SPDK_ISCSI_MAX_RECV_DATA_SEGMENT_LENGTH; + + task1 = iscsi_task_get(&conn, NULL, NULL); + SPDK_CU_ASSERT_FATAL(task1 != NULL); + + task1->scsi.transfer_len = SPDK_ISCSI_MAX_RECV_DATA_SEGMENT_LENGTH; + iscsi_task_set_pdu(task1, pdu1); + task1->tag = 11; + + rc = add_transfer_task(&conn, task1); + CU_ASSERT(rc == 0); + + pdu2 = iscsi_get_pdu(&conn); + SPDK_CU_ASSERT_FATAL(pdu2 != NULL); + + pdu2->data_segment_len = SPDK_ISCSI_MAX_RECV_DATA_SEGMENT_LENGTH; + + task2 = iscsi_task_get(&conn, NULL, NULL); + SPDK_CU_ASSERT_FATAL(task2 != NULL); + + task2->scsi.transfer_len = SPDK_ISCSI_MAX_RECV_DATA_SEGMENT_LENGTH; + iscsi_task_set_pdu(task2, pdu2); + task2->tag = 12; + + rc = add_transfer_task(&conn, task2); + CU_ASSERT(rc == 0); + + pdu3 = iscsi_get_pdu(&conn); + SPDK_CU_ASSERT_FATAL(pdu3 != NULL); + + pdu3->data_segment_len = SPDK_ISCSI_MAX_RECV_DATA_SEGMENT_LENGTH; + + task3 = iscsi_task_get(&conn, NULL, NULL); + SPDK_CU_ASSERT_FATAL(task3 != NULL); + + task3->scsi.transfer_len = SPDK_ISCSI_MAX_RECV_DATA_SEGMENT_LENGTH; + iscsi_task_set_pdu(task3, pdu3); + task3->tag = 13; + + rc = add_transfer_task(&conn, task3); + CU_ASSERT(rc == 0); + + pdu4 = iscsi_get_pdu(&conn); + SPDK_CU_ASSERT_FATAL(pdu4 != NULL); + + pdu4->data_segment_len = SPDK_ISCSI_MAX_RECV_DATA_SEGMENT_LENGTH; + + task4 = iscsi_task_get(&conn, NULL, NULL); + SPDK_CU_ASSERT_FATAL(task4 != NULL); + + task4->scsi.transfer_len = SPDK_ISCSI_MAX_RECV_DATA_SEGMENT_LENGTH; + iscsi_task_set_pdu(task4, pdu4); + task4->tag = 14; + + rc = add_transfer_task(&conn, task4); + CU_ASSERT(rc == 0); + + pdu5 = iscsi_get_pdu(&conn); + SPDK_CU_ASSERT_FATAL(pdu5 != NULL); + + pdu5->data_segment_len = SPDK_ISCSI_MAX_RECV_DATA_SEGMENT_LENGTH; + + task5 = iscsi_task_get(&conn, NULL, NULL); + SPDK_CU_ASSERT_FATAL(task5 != NULL); + + task5->scsi.transfer_len = SPDK_ISCSI_MAX_RECV_DATA_SEGMENT_LENGTH; + iscsi_task_set_pdu(task5, pdu5); + task5->tag = 15; + + rc = add_transfer_task(&conn, task5); + CU_ASSERT(rc == 0); + + CU_ASSERT(get_transfer_task(&conn, 1) == task1); + CU_ASSERT(get_transfer_task(&conn, 5) == NULL); + iscsi_del_transfer_task(&conn, 11); + CU_ASSERT(get_transfer_task(&conn, 1) == NULL); + CU_ASSERT(get_transfer_task(&conn, 5) == task5); + + CU_ASSERT(get_transfer_task(&conn, 2) == task2); + iscsi_del_transfer_task(&conn, 12); + CU_ASSERT(get_transfer_task(&conn, 2) == NULL); + + CU_ASSERT(get_transfer_task(&conn, 3) == task3); + iscsi_del_transfer_task(&conn, 13); + CU_ASSERT(get_transfer_task(&conn, 3) == NULL); + + CU_ASSERT(get_transfer_task(&conn, 4) == task4); + iscsi_del_transfer_task(&conn, 14); + CU_ASSERT(get_transfer_task(&conn, 4) == NULL); + + CU_ASSERT(get_transfer_task(&conn, 5) == task5); + iscsi_del_transfer_task(&conn, 15); + CU_ASSERT(get_transfer_task(&conn, 5) == NULL); + + CU_ASSERT(TAILQ_EMPTY(&conn.active_r2t_tasks)); + + while (!TAILQ_EMPTY(&g_write_pdu_list)) { + pdu = TAILQ_FIRST(&g_write_pdu_list); + TAILQ_REMOVE(&g_write_pdu_list, pdu, tailq); + iscsi_put_pdu(pdu); + } + + iscsi_put_pdu(pdu5); + iscsi_put_pdu(pdu4); + iscsi_put_pdu(pdu3); + iscsi_put_pdu(pdu2); + iscsi_put_pdu(pdu1); +} + +static void +clear_all_transfer_tasks_test(void) +{ + struct spdk_iscsi_sess sess = {}; + struct spdk_iscsi_conn conn = {}; + struct spdk_iscsi_task *task1, *task2, *task3, *task4, *task5, *task6; + struct spdk_iscsi_pdu *pdu1, *pdu2, *pdu3, *pdu4, *pdu5, *pdu6, *pdu; + struct spdk_iscsi_pdu *mgmt_pdu1, *mgmt_pdu2; + struct spdk_scsi_lun lun1 = {}, lun2 = {}; + uint32_t alloc_cmd_sn; + int rc; + + sess.MaxBurstLength = SPDK_ISCSI_MAX_RECV_DATA_SEGMENT_LENGTH; + sess.MaxOutstandingR2T = 1; + + conn.sess = &sess; + TAILQ_INIT(&conn.active_r2t_tasks); + TAILQ_INIT(&conn.queued_r2t_tasks); + + alloc_cmd_sn = 10; + + task1 = iscsi_task_get(&conn, NULL, NULL); + SPDK_CU_ASSERT_FATAL(task1 != NULL); + pdu1 = iscsi_get_pdu(&conn); + SPDK_CU_ASSERT_FATAL(pdu1 != NULL); + + pdu1->data_segment_len = SPDK_ISCSI_MAX_RECV_DATA_SEGMENT_LENGTH; + pdu1->cmd_sn = alloc_cmd_sn; + alloc_cmd_sn++; + task1->scsi.transfer_len = SPDK_ISCSI_MAX_RECV_DATA_SEGMENT_LENGTH; + task1->scsi.lun = &lun1; + iscsi_task_set_pdu(task1, pdu1); + + rc = add_transfer_task(&conn, task1); + CU_ASSERT(rc == 0); + + mgmt_pdu1 = iscsi_get_pdu(&conn); + SPDK_CU_ASSERT_FATAL(mgmt_pdu1 != NULL); + + mgmt_pdu1->cmd_sn = alloc_cmd_sn; + alloc_cmd_sn++; + + task2 = iscsi_task_get(&conn, NULL, NULL); + SPDK_CU_ASSERT_FATAL(task2 != NULL); + pdu2 = iscsi_get_pdu(&conn); + SPDK_CU_ASSERT_FATAL(pdu2 != NULL); + + pdu2->data_segment_len = SPDK_ISCSI_MAX_RECV_DATA_SEGMENT_LENGTH; + pdu2->cmd_sn = alloc_cmd_sn; + alloc_cmd_sn++; + task2->scsi.transfer_len = SPDK_ISCSI_MAX_RECV_DATA_SEGMENT_LENGTH; + task2->scsi.lun = &lun1; + iscsi_task_set_pdu(task2, pdu2); + + rc = add_transfer_task(&conn, task2); + CU_ASSERT(rc == 0); + + task3 = iscsi_task_get(&conn, NULL, NULL); + SPDK_CU_ASSERT_FATAL(task3 != NULL); + pdu3 = iscsi_get_pdu(&conn); + SPDK_CU_ASSERT_FATAL(pdu3 != NULL); + + pdu3->data_segment_len = SPDK_ISCSI_MAX_RECV_DATA_SEGMENT_LENGTH; + pdu3->cmd_sn = alloc_cmd_sn; + alloc_cmd_sn++; + task3->scsi.transfer_len = SPDK_ISCSI_MAX_RECV_DATA_SEGMENT_LENGTH; + task3->scsi.lun = &lun1; + iscsi_task_set_pdu(task3, pdu3); + + rc = add_transfer_task(&conn, task3); + CU_ASSERT(rc == 0); + + task4 = iscsi_task_get(&conn, NULL, NULL); + SPDK_CU_ASSERT_FATAL(task4 != NULL); + pdu4 = iscsi_get_pdu(&conn); + SPDK_CU_ASSERT_FATAL(pdu4 != NULL); + + pdu4->data_segment_len = SPDK_ISCSI_MAX_RECV_DATA_SEGMENT_LENGTH; + pdu4->cmd_sn = alloc_cmd_sn; + alloc_cmd_sn++; + task4->scsi.transfer_len = SPDK_ISCSI_MAX_RECV_DATA_SEGMENT_LENGTH; + task4->scsi.lun = &lun2; + iscsi_task_set_pdu(task4, pdu4); + + rc = add_transfer_task(&conn, task4); + CU_ASSERT(rc == 0); + + task5 = iscsi_task_get(&conn, NULL, NULL); + SPDK_CU_ASSERT_FATAL(task5 != NULL); + pdu5 = iscsi_get_pdu(&conn); + SPDK_CU_ASSERT_FATAL(pdu5 != NULL); + + pdu5->data_segment_len = SPDK_ISCSI_MAX_RECV_DATA_SEGMENT_LENGTH; + pdu5->cmd_sn = alloc_cmd_sn; + alloc_cmd_sn++; + task5->scsi.transfer_len = SPDK_ISCSI_MAX_RECV_DATA_SEGMENT_LENGTH; + task5->scsi.lun = &lun2; + iscsi_task_set_pdu(task5, pdu5); + + rc = add_transfer_task(&conn, task5); + CU_ASSERT(rc == 0); + + mgmt_pdu2 = iscsi_get_pdu(&conn); + SPDK_CU_ASSERT_FATAL(mgmt_pdu2 != NULL); + + mgmt_pdu2->cmd_sn = alloc_cmd_sn; + alloc_cmd_sn++; + + task6 = iscsi_task_get(&conn, NULL, NULL); + SPDK_CU_ASSERT_FATAL(task6 != NULL); + pdu6 = iscsi_get_pdu(&conn); + SPDK_CU_ASSERT_FATAL(pdu6 != NULL); + + pdu6->data_segment_len = SPDK_ISCSI_MAX_RECV_DATA_SEGMENT_LENGTH; + pdu6->cmd_sn = alloc_cmd_sn; + alloc_cmd_sn++; + task5->scsi.transfer_len = SPDK_ISCSI_MAX_RECV_DATA_SEGMENT_LENGTH; + task6->scsi.lun = &lun2; + iscsi_task_set_pdu(task6, pdu6); + + rc = add_transfer_task(&conn, task6); + CU_ASSERT(rc == 0); + + CU_ASSERT(conn.ttt == 4); + + CU_ASSERT(get_transfer_task(&conn, 1) == task1); + CU_ASSERT(get_transfer_task(&conn, 2) == task2); + CU_ASSERT(get_transfer_task(&conn, 3) == task3); + CU_ASSERT(get_transfer_task(&conn, 4) == task4); + CU_ASSERT(get_transfer_task(&conn, 5) == NULL); + + iscsi_clear_all_transfer_task(&conn, &lun1, mgmt_pdu1); + + CU_ASSERT(!TAILQ_EMPTY(&conn.queued_r2t_tasks)); + CU_ASSERT(get_transfer_task(&conn, 1) == NULL); + CU_ASSERT(get_transfer_task(&conn, 2) == task2); + CU_ASSERT(get_transfer_task(&conn, 3) == task3); + CU_ASSERT(get_transfer_task(&conn, 4) == task4); + CU_ASSERT(get_transfer_task(&conn, 5) == task5); + CU_ASSERT(get_transfer_task(&conn, 6) == NULL); + + iscsi_clear_all_transfer_task(&conn, &lun1, NULL); + + CU_ASSERT(TAILQ_EMPTY(&conn.queued_r2t_tasks)); + CU_ASSERT(get_transfer_task(&conn, 1) == NULL); + CU_ASSERT(get_transfer_task(&conn, 2) == NULL); + CU_ASSERT(get_transfer_task(&conn, 3) == NULL); + CU_ASSERT(get_transfer_task(&conn, 4) == task4); + CU_ASSERT(get_transfer_task(&conn, 5) == task5); + CU_ASSERT(get_transfer_task(&conn, 6) == task6); + + iscsi_clear_all_transfer_task(&conn, &lun2, mgmt_pdu2); + + CU_ASSERT(get_transfer_task(&conn, 4) == NULL); + CU_ASSERT(get_transfer_task(&conn, 5) == NULL); + CU_ASSERT(get_transfer_task(&conn, 6) == task6); + + iscsi_clear_all_transfer_task(&conn, NULL, NULL); + + CU_ASSERT(get_transfer_task(&conn, 6) == NULL); + + CU_ASSERT(TAILQ_EMPTY(&conn.active_r2t_tasks)); + while (!TAILQ_EMPTY(&g_write_pdu_list)) { + pdu = TAILQ_FIRST(&g_write_pdu_list); + TAILQ_REMOVE(&g_write_pdu_list, pdu, tailq); + iscsi_put_pdu(pdu); + } + + iscsi_put_pdu(mgmt_pdu2); + iscsi_put_pdu(mgmt_pdu1); + iscsi_put_pdu(pdu6); + iscsi_put_pdu(pdu5); + iscsi_put_pdu(pdu4); + iscsi_put_pdu(pdu3); + iscsi_put_pdu(pdu2); + iscsi_put_pdu(pdu1); +} + +static void +build_iovs_test(void) +{ + struct spdk_iscsi_conn conn = {}; + struct spdk_iscsi_pdu pdu = {}; + struct iovec iovs[5] = {}; + uint8_t *data; + uint32_t mapped_length = 0; + int rc; + + conn.header_digest = true; + conn.data_digest = true; + + DSET24(&pdu.bhs.data_segment_len, 512); + data = calloc(1, 512); + SPDK_CU_ASSERT_FATAL(data != NULL); + pdu.data = data; + + pdu.bhs.total_ahs_len = 0; + pdu.bhs.opcode = ISCSI_OP_SCSI; + + pdu.writev_offset = 0; + rc = iscsi_build_iovs(&conn, iovs, 5, &pdu, &mapped_length); + CU_ASSERT(rc == 4); + CU_ASSERT(iovs[0].iov_base == (void *)&pdu.bhs); + CU_ASSERT(iovs[0].iov_len == ISCSI_BHS_LEN); + CU_ASSERT(iovs[1].iov_base == (void *)pdu.header_digest); + CU_ASSERT(iovs[1].iov_len == ISCSI_DIGEST_LEN); + CU_ASSERT(iovs[2].iov_base == (void *)pdu.data); + CU_ASSERT(iovs[2].iov_len == 512); + CU_ASSERT(iovs[3].iov_base == (void *)pdu.data_digest); + CU_ASSERT(iovs[3].iov_len == ISCSI_DIGEST_LEN); + CU_ASSERT(mapped_length == ISCSI_BHS_LEN + ISCSI_DIGEST_LEN + 512 + ISCSI_DIGEST_LEN); + + pdu.writev_offset = ISCSI_BHS_LEN / 2; + rc = iscsi_build_iovs(&conn, iovs, 5, &pdu, &mapped_length); + CU_ASSERT(rc == 4); + CU_ASSERT(iovs[0].iov_base == (void *)((uint8_t *)&pdu.bhs + ISCSI_BHS_LEN / 2)); + CU_ASSERT(iovs[0].iov_len == ISCSI_BHS_LEN / 2); + CU_ASSERT(iovs[1].iov_base == (void *)pdu.header_digest); + CU_ASSERT(iovs[1].iov_len == ISCSI_DIGEST_LEN); + CU_ASSERT(iovs[2].iov_base == (void *)pdu.data); + CU_ASSERT(iovs[2].iov_len == 512); + CU_ASSERT(iovs[3].iov_base == (void *)pdu.data_digest); + CU_ASSERT(iovs[3].iov_len == ISCSI_DIGEST_LEN); + CU_ASSERT(mapped_length == ISCSI_BHS_LEN / 2 + ISCSI_DIGEST_LEN + 512 + ISCSI_DIGEST_LEN); + + pdu.writev_offset = ISCSI_BHS_LEN; + rc = iscsi_build_iovs(&conn, iovs, 5, &pdu, &mapped_length); + CU_ASSERT(rc == 3); + CU_ASSERT(iovs[0].iov_base == (void *)pdu.header_digest); + CU_ASSERT(iovs[0].iov_len == ISCSI_DIGEST_LEN); + CU_ASSERT(iovs[1].iov_base == (void *)pdu.data); + CU_ASSERT(iovs[1].iov_len == 512); + CU_ASSERT(iovs[2].iov_base == (void *)pdu.data_digest); + CU_ASSERT(iovs[2].iov_len == ISCSI_DIGEST_LEN); + CU_ASSERT(mapped_length == ISCSI_DIGEST_LEN + 512 + ISCSI_DIGEST_LEN); + + pdu.writev_offset = ISCSI_BHS_LEN + ISCSI_DIGEST_LEN / 2; + rc = iscsi_build_iovs(&conn, iovs, 5, &pdu, &mapped_length); + CU_ASSERT(rc == 3); + CU_ASSERT(iovs[0].iov_base == (void *)((uint8_t *)pdu.header_digest + ISCSI_DIGEST_LEN / 2)); + CU_ASSERT(iovs[0].iov_len == ISCSI_DIGEST_LEN / 2); + CU_ASSERT(iovs[1].iov_base == (void *)pdu.data); + CU_ASSERT(iovs[1].iov_len == 512); + CU_ASSERT(iovs[2].iov_base == (void *)pdu.data_digest); + CU_ASSERT(iovs[2].iov_len == ISCSI_DIGEST_LEN); + CU_ASSERT(mapped_length == ISCSI_DIGEST_LEN / 2 + 512 + ISCSI_DIGEST_LEN); + + pdu.writev_offset = ISCSI_BHS_LEN + ISCSI_DIGEST_LEN; + rc = iscsi_build_iovs(&conn, iovs, 5, &pdu, &mapped_length); + CU_ASSERT(rc == 2); + CU_ASSERT(iovs[0].iov_base == (void *)pdu.data); + CU_ASSERT(iovs[0].iov_len == 512); + CU_ASSERT(iovs[1].iov_base == (void *)pdu.data_digest); + CU_ASSERT(iovs[1].iov_len == ISCSI_DIGEST_LEN); + CU_ASSERT(mapped_length == 512 + ISCSI_DIGEST_LEN); + + pdu.writev_offset = ISCSI_BHS_LEN + ISCSI_DIGEST_LEN + 512; + rc = iscsi_build_iovs(&conn, iovs, 5, &pdu, &mapped_length); + CU_ASSERT(rc == 1); + CU_ASSERT(iovs[0].iov_base == (void *)pdu.data_digest); + CU_ASSERT(iovs[0].iov_len == ISCSI_DIGEST_LEN); + CU_ASSERT(mapped_length == ISCSI_DIGEST_LEN); + + pdu.writev_offset = ISCSI_BHS_LEN + ISCSI_DIGEST_LEN + 512 + ISCSI_DIGEST_LEN / 2; + rc = iscsi_build_iovs(&conn, iovs, 5, &pdu, &mapped_length); + CU_ASSERT(rc == 1); + CU_ASSERT(iovs[0].iov_base == (void *)((uint8_t *)pdu.data_digest + ISCSI_DIGEST_LEN / 2)); + CU_ASSERT(iovs[0].iov_len == ISCSI_DIGEST_LEN / 2); + CU_ASSERT(mapped_length == ISCSI_DIGEST_LEN / 2); + + pdu.writev_offset = ISCSI_BHS_LEN + ISCSI_DIGEST_LEN + 512 + ISCSI_DIGEST_LEN; + rc = iscsi_build_iovs(&conn, iovs, 5, &pdu, &mapped_length); + CU_ASSERT(rc == 0); + CU_ASSERT(mapped_length == 0); + + pdu.writev_offset = 0; + rc = iscsi_build_iovs(&conn, iovs, 1, &pdu, &mapped_length); + CU_ASSERT(rc == 1); + CU_ASSERT(iovs[0].iov_base == (void *)&pdu.bhs); + CU_ASSERT(iovs[0].iov_len == ISCSI_BHS_LEN); + CU_ASSERT(mapped_length == ISCSI_BHS_LEN); + + rc = iscsi_build_iovs(&conn, iovs, 2, &pdu, &mapped_length); + CU_ASSERT(rc == 2); + CU_ASSERT(iovs[0].iov_base == (void *)&pdu.bhs); + CU_ASSERT(iovs[0].iov_len == ISCSI_BHS_LEN); + CU_ASSERT(iovs[1].iov_base == (void *)pdu.header_digest); + CU_ASSERT(iovs[1].iov_len == ISCSI_DIGEST_LEN); + CU_ASSERT(mapped_length == ISCSI_BHS_LEN + ISCSI_DIGEST_LEN); + + rc = iscsi_build_iovs(&conn, iovs, 3, &pdu, &mapped_length); + CU_ASSERT(rc == 3); + CU_ASSERT(iovs[0].iov_base == (void *)&pdu.bhs); + CU_ASSERT(iovs[0].iov_len == ISCSI_BHS_LEN); + CU_ASSERT(iovs[1].iov_base == (void *)pdu.header_digest); + CU_ASSERT(iovs[1].iov_len == ISCSI_DIGEST_LEN); + CU_ASSERT(iovs[2].iov_base == (void *)pdu.data); + CU_ASSERT(iovs[2].iov_len == 512); + CU_ASSERT(mapped_length == ISCSI_BHS_LEN + ISCSI_DIGEST_LEN + 512); + + rc = iscsi_build_iovs(&conn, iovs, 4, &pdu, &mapped_length); + CU_ASSERT(rc == 4); + CU_ASSERT(iovs[0].iov_base == (void *)&pdu.bhs); + CU_ASSERT(iovs[0].iov_len == ISCSI_BHS_LEN); + CU_ASSERT(iovs[1].iov_base == (void *)pdu.header_digest); + CU_ASSERT(iovs[1].iov_len == ISCSI_DIGEST_LEN); + CU_ASSERT(iovs[2].iov_base == (void *)pdu.data); + CU_ASSERT(iovs[2].iov_len == 512); + CU_ASSERT(iovs[3].iov_base == (void *)pdu.data_digest); + CU_ASSERT(iovs[3].iov_len == ISCSI_DIGEST_LEN); + CU_ASSERT(mapped_length == ISCSI_BHS_LEN + ISCSI_DIGEST_LEN + 512 + ISCSI_DIGEST_LEN); + + free(data); +} + +static void +build_iovs_with_md_test(void) +{ + struct spdk_iscsi_conn conn = {}; + struct spdk_iscsi_pdu pdu = {}; + struct iovec iovs[6] = {}; + uint8_t *data; + uint32_t mapped_length = 0; + int rc; + + conn.header_digest = true; + conn.data_digest = true; + + DSET24(&pdu.bhs.data_segment_len, 4096 * 2); + data = calloc(1, (4096 + 128) * 2); + SPDK_CU_ASSERT_FATAL(data != NULL); + pdu.data = data; + pdu.data_buf_len = (4096 + 128) * 2; + + pdu.bhs.total_ahs_len = 0; + pdu.bhs.opcode = ISCSI_OP_SCSI; + + rc = spdk_dif_ctx_init(&pdu.dif_ctx, 4096 + 128, 128, true, false, SPDK_DIF_TYPE1, + 0, 0, 0, 0, 0, 0); + CU_ASSERT(rc == 0); + + pdu.dif_insert_or_strip = true; + + pdu.writev_offset = 0; + rc = iscsi_build_iovs(&conn, iovs, 6, &pdu, &mapped_length); + CU_ASSERT(rc == 5); + CU_ASSERT(iovs[0].iov_base == (void *)&pdu.bhs); + CU_ASSERT(iovs[0].iov_len == ISCSI_BHS_LEN); + CU_ASSERT(iovs[1].iov_base == (void *)pdu.header_digest); + CU_ASSERT(iovs[1].iov_len == ISCSI_DIGEST_LEN); + CU_ASSERT(iovs[2].iov_base == (void *)pdu.data); + CU_ASSERT(iovs[2].iov_len == 4096); + CU_ASSERT(iovs[3].iov_base == (void *)(pdu.data + 4096 + 128)); + CU_ASSERT(iovs[3].iov_len == 4096); + CU_ASSERT(iovs[4].iov_base == (void *)pdu.data_digest); + CU_ASSERT(iovs[4].iov_len == ISCSI_DIGEST_LEN); + CU_ASSERT(mapped_length == ISCSI_BHS_LEN + ISCSI_DIGEST_LEN + 4096 * 2 + ISCSI_DIGEST_LEN); + + pdu.writev_offset = ISCSI_BHS_LEN + ISCSI_DIGEST_LEN + 2048; + rc = iscsi_build_iovs(&conn, iovs, 6, &pdu, &mapped_length); + CU_ASSERT(rc == 3); + CU_ASSERT(iovs[0].iov_base == (void *)(pdu.data + 2048)); + CU_ASSERT(iovs[0].iov_len == 2048); + CU_ASSERT(iovs[1].iov_base == (void *)(pdu.data + 4096 + 128)); + CU_ASSERT(iovs[1].iov_len == 4096); + CU_ASSERT(iovs[2].iov_base == (void *)pdu.data_digest); + CU_ASSERT(iovs[2].iov_len == ISCSI_DIGEST_LEN); + CU_ASSERT(mapped_length == 2048 + 4096 + ISCSI_DIGEST_LEN); + + pdu.writev_offset = ISCSI_BHS_LEN + ISCSI_DIGEST_LEN + 4096 * 2; + rc = iscsi_build_iovs(&conn, iovs, 6, &pdu, &mapped_length); + CU_ASSERT(rc == 1); + CU_ASSERT(iovs[0].iov_base == (void *)pdu.data_digest); + CU_ASSERT(iovs[0].iov_len == ISCSI_DIGEST_LEN); + CU_ASSERT(mapped_length == ISCSI_DIGEST_LEN); + + pdu.writev_offset = 0; + rc = iscsi_build_iovs(&conn, iovs, 3, &pdu, &mapped_length); + CU_ASSERT(rc == 3); + CU_ASSERT(iovs[0].iov_base == (void *)&pdu.bhs); + CU_ASSERT(iovs[0].iov_len == ISCSI_BHS_LEN); + CU_ASSERT(iovs[1].iov_base == (void *)pdu.header_digest); + CU_ASSERT(iovs[1].iov_len == ISCSI_DIGEST_LEN); + CU_ASSERT(iovs[2].iov_base == (void *)pdu.data); + CU_ASSERT(iovs[2].iov_len == 4096); + CU_ASSERT(mapped_length == ISCSI_BHS_LEN + ISCSI_DIGEST_LEN + 4096); + + free(data); +} + +static void +check_iscsi_reject(struct spdk_iscsi_pdu *pdu, uint8_t reason) +{ + struct spdk_iscsi_pdu *rsp_pdu; + struct iscsi_bhs_reject *reject_bhs; + + CU_ASSERT(pdu->is_rejected == true); + rsp_pdu = TAILQ_FIRST(&g_write_pdu_list); + CU_ASSERT(rsp_pdu != NULL); + reject_bhs = (struct iscsi_bhs_reject *)&rsp_pdu->bhs; + CU_ASSERT(reject_bhs->reason == reason); + + TAILQ_REMOVE(&g_write_pdu_list, rsp_pdu, tailq); + iscsi_put_pdu(rsp_pdu); + pdu->is_rejected = false; +} + +static void +check_login_response(uint8_t status_class, uint8_t status_detail) +{ + struct spdk_iscsi_pdu *rsp_pdu; + struct iscsi_bhs_login_rsp *login_rsph; + + rsp_pdu = TAILQ_FIRST(&g_write_pdu_list); + CU_ASSERT(rsp_pdu != NULL); + login_rsph = (struct iscsi_bhs_login_rsp *)&rsp_pdu->bhs; + CU_ASSERT(login_rsph->status_class == status_class); + CU_ASSERT(login_rsph->status_detail == status_detail); + + TAILQ_REMOVE(&g_write_pdu_list, rsp_pdu, tailq); + iscsi_put_pdu(rsp_pdu); +} + +static void +pdu_hdr_op_login_test(void) +{ + struct spdk_iscsi_sess sess = {}; + struct spdk_iscsi_conn conn = {}; + struct spdk_iscsi_pdu pdu = {}; + struct iscsi_bhs_login_req *login_reqh; + int rc; + + login_reqh = (struct iscsi_bhs_login_req *)&pdu.bhs; + + /* Case 1 - On discovery session, target only accepts text requests with the + * SendTargets key and logout request with reason "close the session". + */ + sess.session_type = SESSION_TYPE_DISCOVERY; + conn.full_feature = true; + conn.sess = &sess; + + rc = iscsi_pdu_hdr_op_login(&conn, &pdu); + CU_ASSERT(rc == SPDK_ISCSI_CONNECTION_FATAL); + + /* Case 2 - Data segment length is limited to be not more than 8KB, the default + * FirstBurstLength, for login request. + */ + sess.session_type = SESSION_TYPE_INVALID; + conn.full_feature = false; + conn.sess = NULL; + pdu.data_segment_len = SPDK_ISCSI_FIRST_BURST_LENGTH + 1; + + rc = iscsi_pdu_hdr_op_login(&conn, &pdu); + CU_ASSERT(rc == 0); + check_iscsi_reject(&pdu, ISCSI_REASON_PROTOCOL_ERROR); + + /* Case 3 - PDU pool is empty */ + pdu.data_segment_len = SPDK_ISCSI_FIRST_BURST_LENGTH; + g_pdu_pool_is_empty = true; + + rc = iscsi_pdu_hdr_op_login(&conn, &pdu); + CU_ASSERT(rc == SPDK_ISCSI_CONNECTION_FATAL); + + /* Case 4 - A login request with the C bit set to 1 must have the T bit set to 0. */ + g_pdu_pool_is_empty = false; + login_reqh->flags |= ISCSI_LOGIN_TRANSIT; + login_reqh->flags |= ISCSI_LOGIN_CONTINUE; + + rc = iscsi_pdu_hdr_op_login(&conn, &pdu); + CU_ASSERT(rc == 0); + check_login_response(ISCSI_CLASS_INITIATOR_ERROR, ISCSI_LOGIN_INITIATOR_ERROR); + + /* Case 5 - Both version-min and version-max must be set to 0x00. */ + login_reqh->flags = 0; + login_reqh->version_min = ISCSI_VERSION + 1; + + rc = iscsi_pdu_hdr_op_login(&conn, &pdu); + CU_ASSERT(rc == 0); + check_login_response(ISCSI_CLASS_INITIATOR_ERROR, ISCSI_LOGIN_UNSUPPORTED_VERSION); + + /* Case 6 - T bit is set to 1 correctly but invalid stage code is set to NSG. */ + login_reqh->version_min = ISCSI_VERSION; + login_reqh->flags |= ISCSI_LOGIN_TRANSIT; + login_reqh->flags |= ISCSI_NSG_RESERVED_CODE; + + rc = iscsi_pdu_hdr_op_login(&conn, &pdu); + CU_ASSERT(rc == 0); + check_login_response(ISCSI_CLASS_INITIATOR_ERROR, ISCSI_LOGIN_INITIATOR_ERROR); + + /* Case 7 - Login request is correct. Login response is initialized and set to + * the current connection. + */ + login_reqh->flags = 0; + + rc = iscsi_pdu_hdr_op_login(&conn, &pdu); + CU_ASSERT(rc == 0); + CU_ASSERT(conn.login_rsp_pdu != NULL); + + iscsi_put_pdu(conn.login_rsp_pdu); +} + +static void +pdu_hdr_op_text_test(void) +{ + struct spdk_iscsi_sess sess = {}; + struct spdk_iscsi_conn conn = {}; + struct spdk_iscsi_pdu pdu = {}; + struct iscsi_bhs_text_req *text_reqh; + int rc; + + text_reqh = (struct iscsi_bhs_text_req *)&pdu.bhs; + + conn.sess = &sess; + + /* Case 1 - Data segment length for text request must not be more than + * FirstBurstLength plus extra space to account for digests. + */ + pdu.data_segment_len = iscsi_get_max_immediate_data_size() + 1; + + rc = iscsi_pdu_hdr_op_text(&conn, &pdu); + CU_ASSERT(rc == 0); + check_iscsi_reject(&pdu, ISCSI_REASON_PROTOCOL_ERROR); + + /* Case 2 - A text request with the C bit set to 1 must have the F bit set to 0. */ + pdu.data_segment_len = iscsi_get_max_immediate_data_size(); + text_reqh->flags |= ISCSI_FLAG_FINAL; + text_reqh->flags |= ISCSI_TEXT_CONTINUE; + + rc = iscsi_pdu_hdr_op_text(&conn, &pdu); + CU_ASSERT(rc == -1); + + /* Case 3 - ExpStatSN of the text request is expected to match StatSN of the current + * connection. But StarPort iSCSI initiator didn't follow the expectation. In this + * case we overwrite StatSN by ExpStatSN and processes the request as correct. + */ + text_reqh->flags = 0; + to_be32(&text_reqh->exp_stat_sn, 1234); + to_be32(&conn.StatSN, 4321); + + rc = iscsi_pdu_hdr_op_text(&conn, &pdu); + CU_ASSERT(rc == 0); + CU_ASSERT(conn.StatSN == 1234); + + /* Case 4 - Text request is the first in the sequence of text requests and responses, + * and so its ITT is hold to the current connection. + */ + sess.current_text_itt = 0xffffffffU; + to_be32(&text_reqh->itt, 5678); + + rc = iscsi_pdu_hdr_op_text(&conn, &pdu); + CU_ASSERT(rc == 0); + CU_ASSERT(sess.current_text_itt == 5678); + + /* Case 5 - If text request is sent as part of a sequence of text requests and responses, + * its ITT must be the same for all the text requests. But it was not. */ + sess.current_text_itt = 5679; + + rc = iscsi_pdu_hdr_op_text(&conn, &pdu); + CU_ASSERT(rc == 0); + check_iscsi_reject(&pdu, ISCSI_REASON_PROTOCOL_ERROR); + + /* Case 6 - Different from case 5, its ITT matches the value saved in the connection. */ + text_reqh->flags = 0; + sess.current_text_itt = 5678; + + rc = iscsi_pdu_hdr_op_text(&conn, &pdu); + CU_ASSERT(rc == 0); +} + +static void +check_logout_response(uint8_t response, uint32_t stat_sn, uint32_t exp_cmd_sn, + uint32_t max_cmd_sn) +{ + struct spdk_iscsi_pdu *rsp_pdu; + struct iscsi_bhs_logout_resp *logout_rsph; + + rsp_pdu = TAILQ_FIRST(&g_write_pdu_list); + CU_ASSERT(rsp_pdu != NULL); + logout_rsph = (struct iscsi_bhs_logout_resp *)&rsp_pdu->bhs; + CU_ASSERT(logout_rsph->response == response); + CU_ASSERT(from_be32(&logout_rsph->stat_sn) == stat_sn); + CU_ASSERT(from_be32(&logout_rsph->exp_cmd_sn) == exp_cmd_sn); + CU_ASSERT(from_be32(&logout_rsph->max_cmd_sn) == max_cmd_sn); + + TAILQ_REMOVE(&g_write_pdu_list, rsp_pdu, tailq); + iscsi_put_pdu(rsp_pdu); +} + +static void +pdu_hdr_op_logout_test(void) +{ + struct spdk_iscsi_sess sess = {}; + struct spdk_iscsi_conn conn = {}; + struct spdk_iscsi_pdu pdu = {}; + struct iscsi_bhs_logout_req *logout_reqh; + int rc; + + logout_reqh = (struct iscsi_bhs_logout_req *)&pdu.bhs; + + /* Case 1 - Target can accept logout request only with the reason "close the session" + * on discovery session. + */ + logout_reqh->reason = 1; + conn.sess = &sess; + sess.session_type = SESSION_TYPE_DISCOVERY; + + rc = iscsi_pdu_hdr_op_logout(&conn, &pdu); + CU_ASSERT(rc == SPDK_ISCSI_CONNECTION_FATAL); + + /* Case 2 - Session is not established yet but connection was closed successfully. */ + conn.sess = NULL; + conn.StatSN = 1234; + to_be32(&logout_reqh->exp_stat_sn, 1234); + pdu.cmd_sn = 5678; + + rc = iscsi_pdu_hdr_op_logout(&conn, &pdu); + CU_ASSERT(rc == 0); + check_logout_response(0, 1234, 5678, 5678); + CU_ASSERT(conn.StatSN == 1235); + + /* Case 3 - Session type is normal but CID was not found. Hence connection or session + * was not closed. + */ + sess.session_type = SESSION_TYPE_NORMAL; + sess.ExpCmdSN = 5679; + sess.connections = 1; + conn.sess = &sess; + conn.id = 1; + + rc = iscsi_pdu_hdr_op_logout(&conn, &pdu); + CU_ASSERT(rc == 0); + check_logout_response(1, 1235, 5679, 1); + CU_ASSERT(conn.StatSN == 1236); + CU_ASSERT(sess.MaxCmdSN == 1); + + /* Case 4 - Session type is normal and CID was found. Connection or session was closed + * successfully. + */ + to_be16(&logout_reqh->cid, 1); + + rc = iscsi_pdu_hdr_op_logout(&conn, &pdu); + CU_ASSERT(rc == 0); + check_logout_response(0, 1236, 5679, 2); + CU_ASSERT(conn.StatSN == 1237); + CU_ASSERT(sess.MaxCmdSN == 2); + + /* Case 5 - PDU pool is empty. */ + g_pdu_pool_is_empty = true; + + rc = iscsi_pdu_hdr_op_logout(&conn, &pdu); + CU_ASSERT(rc == SPDK_ISCSI_CONNECTION_FATAL); + + g_pdu_pool_is_empty = false; +} + +static void +check_scsi_task(struct spdk_iscsi_pdu *pdu, enum spdk_scsi_data_dir dir) +{ + struct spdk_iscsi_task *task; + + task = pdu->task; + CU_ASSERT(task != NULL); + CU_ASSERT(task->pdu == pdu); + CU_ASSERT(task->scsi.dxfer_dir == dir); + + iscsi_task_put(task); + pdu->task = NULL; +} + +static void +pdu_hdr_op_scsi_test(void) +{ + struct spdk_iscsi_sess sess = {}; + struct spdk_iscsi_conn conn = {}; + struct spdk_iscsi_pdu pdu = {}; + struct spdk_scsi_dev dev = {}; + struct spdk_scsi_lun lun = {}; + struct iscsi_bhs_scsi_req *scsi_reqh; + int rc; + + scsi_reqh = (struct iscsi_bhs_scsi_req *)&pdu.bhs; + + conn.sess = &sess; + conn.dev = &dev; + + /* Case 1 - SCSI command is acceptable only on normal session. */ + sess.session_type = SESSION_TYPE_DISCOVERY; + + rc = iscsi_pdu_hdr_op_scsi(&conn, &pdu); + CU_ASSERT(rc == SPDK_ISCSI_CONNECTION_FATAL); + + /* Case 2 - Task pool is empty. */ + g_task_pool_is_empty = true; + + rc = iscsi_pdu_hdr_op_scsi(&conn, &pdu); + CU_ASSERT(rc == SPDK_ISCSI_CONNECTION_FATAL); + + g_task_pool_is_empty = false; + + /* Case 3 - bidirectional operations (both R and W flags are set to 1) are not supported. */ + sess.session_type = SESSION_TYPE_NORMAL; + scsi_reqh->read_bit = 1; + scsi_reqh->write_bit = 1; + + rc = iscsi_pdu_hdr_op_scsi(&conn, &pdu); + CU_ASSERT(rc == SPDK_ISCSI_CONNECTION_FATAL); + + /* Case 4 - LUN is hot-removed, and return immediately. */ + scsi_reqh->write_bit = 0; + + rc = iscsi_pdu_hdr_op_scsi(&conn, &pdu); + CU_ASSERT(rc == 0); + CU_ASSERT(pdu.task == NULL); + + /* Case 5 - SCSI read command PDU is correct, and the configured iSCSI task is set to the PDU. */ + dev.lun[0] = &lun; + + rc = iscsi_pdu_hdr_op_scsi(&conn, &pdu); + CU_ASSERT(rc == 0); + check_scsi_task(&pdu, SPDK_SCSI_DIR_FROM_DEV); + + /* Case 6 - For SCSI write command PDU, its data segment length must not be more than + * FirstBurstLength plus extra space to account for digests. + */ + scsi_reqh->read_bit = 0; + scsi_reqh->write_bit = 1; + pdu.data_segment_len = iscsi_get_max_immediate_data_size() + 1; + + rc = iscsi_pdu_hdr_op_scsi(&conn, &pdu); + CU_ASSERT(rc == 0); + check_iscsi_reject(&pdu, ISCSI_REASON_PROTOCOL_ERROR); + + /* Case 7 - For SCSI write command PDU, its data segment length must not be more than + * Expected Data Transfer Length (EDTL). + */ + pdu.data_segment_len = iscsi_get_max_immediate_data_size(); + to_be32(&scsi_reqh->expected_data_xfer_len, pdu.data_segment_len - 1); + + rc = iscsi_pdu_hdr_op_scsi(&conn, &pdu); + CU_ASSERT(rc == 0); + check_iscsi_reject(&pdu, ISCSI_REASON_PROTOCOL_ERROR); + + /* Case 8 - If ImmediateData is not enabled for the session, SCSI write command PDU + * cannot have data segment. + */ + to_be32(&scsi_reqh->expected_data_xfer_len, pdu.data_segment_len); + + rc = iscsi_pdu_hdr_op_scsi(&conn, &pdu); + CU_ASSERT(rc == 0); + check_iscsi_reject(&pdu, ISCSI_REASON_PROTOCOL_ERROR); + + /* Case 9 - For SCSI write command PDU, its data segment length must not be more + * than FirstBurstLength. + */ + sess.ImmediateData = true; + + rc = iscsi_pdu_hdr_op_scsi(&conn, &pdu); + CU_ASSERT(rc == 0); + check_iscsi_reject(&pdu, ISCSI_REASON_PROTOCOL_ERROR); + + /* Case 10 - SCSI write command PDU is correct, and the configured iSCSI task is set to the PDU. */ + sess.FirstBurstLength = pdu.data_segment_len; + + rc = iscsi_pdu_hdr_op_scsi(&conn, &pdu); + CU_ASSERT(rc == 0); + check_scsi_task(&pdu, SPDK_SCSI_DIR_TO_DEV); + + /* Case 11 - R and W must not both be 0 when EDTL is not 0. */ + scsi_reqh->write_bit = 0; + + rc = iscsi_pdu_hdr_op_scsi(&conn, &pdu); + CU_ASSERT(rc == 0); + check_iscsi_reject(&pdu, ISCSI_REASON_INVALID_PDU_FIELD); + + /* Case 11 - R and W are both 0 and EDTL is also 0, and hence SCSI command PDU is accepted. */ + to_be32(&scsi_reqh->expected_data_xfer_len, 0); + + rc = iscsi_pdu_hdr_op_scsi(&conn, &pdu); + CU_ASSERT(rc == 0); + check_scsi_task(&pdu, SPDK_SCSI_DIR_NONE); +} + +static void +check_iscsi_task_mgmt_response(uint8_t response, uint32_t task_tag, uint32_t stat_sn, + uint32_t exp_cmd_sn, uint32_t max_cmd_sn) +{ + struct spdk_iscsi_pdu *rsp_pdu; + struct iscsi_bhs_task_resp *rsph; + + rsp_pdu = TAILQ_FIRST(&g_write_pdu_list); + CU_ASSERT(rsp_pdu != NULL); + rsph = (struct iscsi_bhs_task_resp *)&rsp_pdu->bhs; + CU_ASSERT(rsph->response == response); + CU_ASSERT(from_be32(&rsph->itt) == task_tag); + CU_ASSERT(from_be32(&rsph->exp_cmd_sn) == exp_cmd_sn); + CU_ASSERT(from_be32(&rsph->max_cmd_sn) == max_cmd_sn); + + TAILQ_REMOVE(&g_write_pdu_list, rsp_pdu, tailq); + iscsi_put_pdu(rsp_pdu); +} + +static void +pdu_hdr_op_task_mgmt_test(void) +{ + struct spdk_iscsi_sess sess = {}; + struct spdk_iscsi_conn conn = {}; + struct spdk_iscsi_pdu pdu = {}; + struct spdk_scsi_dev dev = {}; + struct spdk_scsi_lun lun = {}; + struct iscsi_bhs_task_req *task_reqh; + int rc; + + /* TBD: This test covers only error paths before creating iSCSI task for now. + * Testing iSCSI task creation in iscsi_pdu_hdr_op_task() by UT is not simple + * and do it separately later. + */ + + task_reqh = (struct iscsi_bhs_task_req *)&pdu.bhs; + + conn.sess = &sess; + conn.dev = &dev; + + /* Case 1 - Task Management Function request PDU is acceptable only on normal session. */ + sess.session_type = SESSION_TYPE_DISCOVERY; + + rc = iscsi_pdu_hdr_op_task(&conn, &pdu); + CU_ASSERT(rc == SPDK_ISCSI_CONNECTION_FATAL); + + /* Case 2 - LUN is hot removed. "LUN does not exist" response is sent. */ + sess.session_type = SESSION_TYPE_NORMAL; + task_reqh->immediate = 0; + to_be32(&task_reqh->itt, 1234); + + rc = iscsi_pdu_hdr_op_task(&conn, &pdu); + CU_ASSERT(rc == 0); + check_iscsi_task_mgmt_response(ISCSI_TASK_FUNC_RESP_LUN_NOT_EXIST, 1234, 0, 0, 1); + + /* Case 3 - Unassigned function is specified. "Function rejected" response is sent. */ + dev.lun[0] = &lun; + task_reqh->flags = 0; + + rc = iscsi_pdu_hdr_op_task(&conn, &pdu); + CU_ASSERT(rc == 0); + check_iscsi_task_mgmt_response(ISCSI_TASK_FUNC_REJECTED, 1234, 0, 0, 2); + + /* Case 4 - CLEAR TASK SET is not supported. "Task management function not supported" + * response is sent. + */ + task_reqh->flags = ISCSI_TASK_FUNC_CLEAR_TASK_SET; + + rc = iscsi_pdu_hdr_op_task(&conn, &pdu); + CU_ASSERT(rc == 0); + check_iscsi_task_mgmt_response(ISCSI_TASK_FUNC_RESP_FUNC_NOT_SUPPORTED, 1234, 0, 0, 3); + + /* Case 5 - CLEAR ACA is not supported. "Task management function not supported" is sent. */ + task_reqh->flags = ISCSI_TASK_FUNC_CLEAR_ACA; + + rc = iscsi_pdu_hdr_op_task(&conn, &pdu); + CU_ASSERT(rc == 0); + check_iscsi_task_mgmt_response(ISCSI_TASK_FUNC_RESP_FUNC_NOT_SUPPORTED, 1234, 0, 0, 4); + + /* Case 6 - TARGET WARM RESET is not supported. "Task management function not supported + * is sent. + */ + task_reqh->flags = ISCSI_TASK_FUNC_TARGET_WARM_RESET; + + rc = iscsi_pdu_hdr_op_task(&conn, &pdu); + CU_ASSERT(rc == 0); + check_iscsi_task_mgmt_response(ISCSI_TASK_FUNC_RESP_FUNC_NOT_SUPPORTED, 1234, 0, 0, 5); + + /* Case 7 - TARGET COLD RESET is not supported. "Task management function not supported + * is sent. + */ + task_reqh->flags = ISCSI_TASK_FUNC_TARGET_COLD_RESET; + + rc = iscsi_pdu_hdr_op_task(&conn, &pdu); + CU_ASSERT(rc == 0); + check_iscsi_task_mgmt_response(ISCSI_TASK_FUNC_RESP_FUNC_NOT_SUPPORTED, 1234, 0, 0, 6); + + /* Case 8 - TASK REASSIGN is not supported. "Task management function not supported" is sent. */ + task_reqh->flags = ISCSI_TASK_FUNC_TASK_REASSIGN; + + rc = iscsi_pdu_hdr_op_task(&conn, &pdu); + CU_ASSERT(rc == 0); + check_iscsi_task_mgmt_response(ISCSI_TASK_FUNC_RESP_FUNC_NOT_SUPPORTED, 1234, 0, 0, 7); +} + +static void +pdu_hdr_op_nopout_test(void) +{ + struct spdk_iscsi_sess sess = {}; + struct spdk_iscsi_conn conn = {}; + struct spdk_iscsi_pdu pdu = {}; + struct iscsi_bhs_nop_out *nopout_reqh; + int rc; + + nopout_reqh = (struct iscsi_bhs_nop_out *)&pdu.bhs; + + conn.sess = &sess; + + /* Case 1 - NOP-Out PDU is acceptable only on normal session. */ + sess.session_type = SESSION_TYPE_DISCOVERY; + + rc = iscsi_pdu_hdr_op_nopout(&conn, &pdu); + CU_ASSERT(rc == SPDK_ISCSI_CONNECTION_FATAL); + + /* Case 2 - The length of the reflected ping data is limited to MaxRecvDataSegmentLength. */ + sess.session_type = SESSION_TYPE_NORMAL; + pdu.data_segment_len = SPDK_ISCSI_MAX_RECV_DATA_SEGMENT_LENGTH + 1; + + rc = iscsi_pdu_hdr_op_nopout(&conn, &pdu); + CU_ASSERT(rc == 0); + check_iscsi_reject(&pdu, ISCSI_REASON_PROTOCOL_ERROR); + + /* Case 3 - If Initiator Task Tag contains 0xffffffff, the I bit must be set + * to 1 and Target Transfer Tag should be copied from NOP-In PDU. This case + * satisfies the former but doesn't satisfy the latter, but ignore the error + * for now. + */ + pdu.data_segment_len = SPDK_ISCSI_MAX_RECV_DATA_SEGMENT_LENGTH; + conn.id = 1234; + to_be32(&nopout_reqh->ttt, 1235); + to_be32(&nopout_reqh->itt, 0xffffffffU); + nopout_reqh->immediate = 1; + + rc = iscsi_pdu_hdr_op_nopout(&conn, &pdu); + CU_ASSERT(rc == 0); + + /* Case 4 - This case doesn't satisfy the above former. This error is not ignored. */ + nopout_reqh->immediate = 0; + + rc = iscsi_pdu_hdr_op_nopout(&conn, &pdu); + CU_ASSERT(rc == SPDK_ISCSI_CONNECTION_FATAL); +} + +static void +check_iscsi_r2t(struct spdk_iscsi_task *task, uint32_t len) +{ + struct spdk_iscsi_pdu *rsp_pdu; + struct iscsi_bhs_r2t *rsph; + + rsp_pdu = TAILQ_FIRST(&g_write_pdu_list); + CU_ASSERT(rsp_pdu != NULL); + rsph = (struct iscsi_bhs_r2t *)&rsp_pdu->bhs; + CU_ASSERT(rsph->opcode == ISCSI_OP_R2T); + CU_ASSERT(from_be64(&rsph->lun) == spdk_scsi_lun_id_int_to_fmt(task->lun_id)); + CU_ASSERT(from_be32(&rsph->buffer_offset) == task->next_r2t_offset); + CU_ASSERT(from_be32(&rsph->desired_xfer_len) == len); + + TAILQ_REMOVE(&g_write_pdu_list, rsp_pdu, tailq); + iscsi_put_pdu(rsp_pdu); +} + +static void +pdu_hdr_op_data_test(void) +{ + struct spdk_iscsi_sess sess = {}; + struct spdk_iscsi_conn conn = {}; + struct spdk_iscsi_pdu pdu = {}; + struct spdk_iscsi_task primary = {}; + struct spdk_scsi_dev dev = {}; + struct spdk_scsi_lun lun = {}; + struct iscsi_bhs_data_out *data_reqh; + int rc; + + data_reqh = (struct iscsi_bhs_data_out *)&pdu.bhs; + + conn.sess = &sess; + conn.dev = &dev; + TAILQ_INIT(&conn.active_r2t_tasks); + + /* Case 1 - SCSI Data-Out PDU is acceptable only on normal session. */ + sess.session_type = SESSION_TYPE_DISCOVERY; + + rc = iscsi_pdu_hdr_op_data(&conn, &pdu); + CU_ASSERT(rc == SPDK_ISCSI_CONNECTION_FATAL); + + /* Case 2 - Data segment length must not be more than MaxRecvDataSegmentLength. */ + sess.session_type = SESSION_TYPE_NORMAL; + pdu.data_segment_len = SPDK_ISCSI_MAX_RECV_DATA_SEGMENT_LENGTH + 1; + + rc = iscsi_pdu_hdr_op_data(&conn, &pdu); + CU_ASSERT(rc == 0); + check_iscsi_reject(&pdu, ISCSI_REASON_PROTOCOL_ERROR); + + /* Case 3 - R2T task whose Target Transfer Tag matches is not found. */ + pdu.data_segment_len = SPDK_ISCSI_MAX_RECV_DATA_SEGMENT_LENGTH; + + rc = iscsi_pdu_hdr_op_data(&conn, &pdu); + CU_ASSERT(rc == 0); + check_iscsi_reject(&pdu, ISCSI_REASON_INVALID_PDU_FIELD); + + /* Case 4 - R2T task whose Target Transfer Tag matches is found but data segment length + * is more than Desired Data Transfer Length of the R2T. + */ + primary.desired_data_transfer_length = SPDK_ISCSI_MAX_RECV_DATA_SEGMENT_LENGTH - 1; + conn.pending_r2t = 1; + TAILQ_INSERT_TAIL(&conn.active_r2t_tasks, &primary, link); + + rc = iscsi_pdu_hdr_op_data(&conn, &pdu); + CU_ASSERT(rc == SPDK_ISCSI_CONNECTION_FATAL); + + /* Case 5 - Initiator task tag doesn't match tag of R2T task. */ + primary.desired_data_transfer_length = SPDK_ISCSI_MAX_RECV_DATA_SEGMENT_LENGTH; + to_be32(&data_reqh->itt, 1); + + rc = iscsi_pdu_hdr_op_data(&conn, &pdu); + CU_ASSERT(rc == 0); + check_iscsi_reject(&pdu, ISCSI_REASON_INVALID_PDU_FIELD); + + /* Case 6 - DataSN doesn't match the Data-Out PDU number within the current + * output sequence. + */ + to_be32(&data_reqh->itt, 0); + to_be32(&data_reqh->data_sn, 1); + + rc = iscsi_pdu_hdr_op_data(&conn, &pdu); + CU_ASSERT(rc == 0); + check_iscsi_reject(&pdu, ISCSI_REASON_PROTOCOL_ERROR); + + /* Case 7 - Output sequence must be in increasing buffer offset and must not + * be overlaid but they are not satisfied. + */ + to_be32(&data_reqh->data_sn, 0); + to_be32(&data_reqh->buffer_offset, 4096); + + rc = iscsi_pdu_hdr_op_data(&conn, &pdu); + CU_ASSERT(rc == SPDK_ISCSI_CONNECTION_FATAL); + + /* Case 8 - Data segment length must not exceed MaxBurstLength. */ + to_be32(&data_reqh->buffer_offset, 0); + sess.MaxBurstLength = pdu.data_segment_len - 1; + + rc = iscsi_pdu_hdr_op_data(&conn, &pdu); + CU_ASSERT(rc == SPDK_ISCSI_CONNECTION_FATAL); + + /* Case 9 - LUN is hot removed. */ + sess.MaxBurstLength = pdu.data_segment_len * 4; + to_be32(&data_reqh->data_sn, primary.r2t_datasn); + to_be32(&data_reqh->buffer_offset, primary.next_expected_r2t_offset); + + rc = iscsi_pdu_hdr_op_data(&conn, &pdu); + CU_ASSERT(rc == 0); + CU_ASSERT(pdu.task == NULL); + + /* Case 10 - SCSI Data-Out PDU is correct and processed. Created task is held + * to the PDU, but its F bit is 0 and hence R2T is not sent. + */ + dev.lun[0] = &lun; + to_be32(&data_reqh->data_sn, primary.r2t_datasn); + to_be32(&data_reqh->buffer_offset, primary.next_expected_r2t_offset); + + rc = iscsi_pdu_hdr_op_data(&conn, &pdu); + CU_ASSERT(rc == 0); + CU_ASSERT(pdu.task != NULL); + iscsi_task_put(pdu.task); + pdu.task = NULL; + + /* Case 11 - SCSI Data-Out PDU is correct and processed. Created task is held + * to the PDU, and Its F bit is 1 and hence R2T is sent. + */ + data_reqh->flags |= ISCSI_FLAG_FINAL; + to_be32(&data_reqh->data_sn, primary.r2t_datasn); + to_be32(&data_reqh->buffer_offset, primary.next_expected_r2t_offset); + primary.scsi.transfer_len = pdu.data_segment_len * 5; + + rc = iscsi_pdu_hdr_op_data(&conn, &pdu); + CU_ASSERT(rc == 0); + CU_ASSERT(pdu.task != NULL); + check_iscsi_r2t(pdu.task, pdu.data_segment_len * 4); + iscsi_task_put(pdu.task); + + /* Case 12 - Task pool is empty. */ + to_be32(&data_reqh->data_sn, primary.r2t_datasn); + to_be32(&data_reqh->buffer_offset, primary.next_expected_r2t_offset); + g_task_pool_is_empty = true; + + rc = iscsi_pdu_hdr_op_data(&conn, &pdu); + CU_ASSERT(rc == SPDK_ISCSI_CONNECTION_FATAL); + + g_task_pool_is_empty = false; +} + +int +main(int argc, char **argv) +{ + CU_pSuite suite = NULL; + unsigned int num_failures; + + CU_set_error_action(CUEA_ABORT); + CU_initialize_registry(); + + suite = CU_add_suite("iscsi_suite", NULL, NULL); + + CU_ADD_TEST(suite, op_login_check_target_test); + CU_ADD_TEST(suite, op_login_session_normal_test); + CU_ADD_TEST(suite, maxburstlength_test); + CU_ADD_TEST(suite, underflow_for_read_transfer_test); + CU_ADD_TEST(suite, underflow_for_zero_read_transfer_test); + CU_ADD_TEST(suite, underflow_for_request_sense_test); + CU_ADD_TEST(suite, underflow_for_check_condition_test); + CU_ADD_TEST(suite, add_transfer_task_test); + CU_ADD_TEST(suite, get_transfer_task_test); + CU_ADD_TEST(suite, del_transfer_task_test); + CU_ADD_TEST(suite, clear_all_transfer_tasks_test); + CU_ADD_TEST(suite, build_iovs_test); + CU_ADD_TEST(suite, build_iovs_with_md_test); + CU_ADD_TEST(suite, pdu_hdr_op_login_test); + CU_ADD_TEST(suite, pdu_hdr_op_text_test); + CU_ADD_TEST(suite, pdu_hdr_op_logout_test); + CU_ADD_TEST(suite, pdu_hdr_op_scsi_test); + CU_ADD_TEST(suite, pdu_hdr_op_task_mgmt_test); + CU_ADD_TEST(suite, pdu_hdr_op_nopout_test); + CU_ADD_TEST(suite, pdu_hdr_op_data_test); + + CU_basic_set_mode(CU_BRM_VERBOSE); + CU_basic_run_tests(); + num_failures = CU_get_number_of_failures(); + CU_cleanup_registry(); + return num_failures; +} diff --git a/src/spdk/test/unit/lib/iscsi/param.c/.gitignore b/src/spdk/test/unit/lib/iscsi/param.c/.gitignore new file mode 100644 index 000000000..269921462 --- /dev/null +++ b/src/spdk/test/unit/lib/iscsi/param.c/.gitignore @@ -0,0 +1 @@ +param_ut diff --git a/src/spdk/test/unit/lib/iscsi/param.c/Makefile b/src/spdk/test/unit/lib/iscsi/param.c/Makefile new file mode 100644 index 000000000..d1b567b54 --- /dev/null +++ b/src/spdk/test/unit/lib/iscsi/param.c/Makefile @@ -0,0 +1,38 @@ +# +# BSD LICENSE +# +# Copyright (c) Intel Corporation. +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in +# the documentation and/or other materials provided with the +# distribution. +# * Neither the name of Intel Corporation nor the names of its +# contributors may be used to endorse or promote products derived +# from this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +# + +SPDK_ROOT_DIR := $(abspath $(CURDIR)/../../../../..) + +TEST_FILE = param_ut.c + +include $(SPDK_ROOT_DIR)/mk/spdk.unittest.mk diff --git a/src/spdk/test/unit/lib/iscsi/param.c/param_ut.c b/src/spdk/test/unit/lib/iscsi/param.c/param_ut.c new file mode 100644 index 000000000..ccf62643f --- /dev/null +++ b/src/spdk/test/unit/lib/iscsi/param.c/param_ut.c @@ -0,0 +1,400 @@ +/*- + * BSD LICENSE + * + * Copyright (c) Intel Corporation. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Intel Corporation nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "spdk/stdinc.h" + +#include "spdk/scsi.h" + +#include "spdk_cunit.h" + +#include "../common.c" +#include "iscsi/param.c" + +#include "spdk_internal/mock.h" + +struct spdk_iscsi_globals g_iscsi; + +DEFINE_STUB(iscsi_find_tgt_node, struct spdk_iscsi_tgt_node *, + (const char *target_name), NULL); + +DEFINE_STUB(iscsi_tgt_node_access, bool, + (struct spdk_iscsi_conn *conn, struct spdk_iscsi_tgt_node *target, + const char *iqn, const char *addr), + false); + +DEFINE_STUB(iscsi_send_tgts, int, + (struct spdk_iscsi_conn *conn, const char *iiqn, const char *iaddr, + const char *tiqn, uint8_t *data, int alloc_len, int data_len), + 0); + +static void +burst_length_param_negotation(int FirstBurstLength, int MaxBurstLength, + int initialR2T) +{ + struct spdk_iscsi_sess sess; + struct spdk_iscsi_conn conn; + struct iscsi_param *params; + struct iscsi_param **params_p; + char data[8192]; + int rc; + int total, len; + + total = 0; + params = NULL; + params_p = ¶ms; + + memset(&sess, 0, sizeof(sess)); + memset(&conn, 0, sizeof(conn)); + memset(data, 0, 8192); + + sess.ExpCmdSN = 0; + sess.MaxCmdSN = 64; + sess.session_type = SESSION_TYPE_NORMAL; + sess.params = NULL; + sess.MaxBurstLength = 65536; + sess.InitialR2T = true; + sess.FirstBurstLength = SPDK_ISCSI_FIRST_BURST_LENGTH; + sess.MaxOutstandingR2T = 1; + + /* set default params */ + rc = iscsi_sess_params_init(&sess.params); + CU_ASSERT(rc == 0); + + rc = iscsi_param_set_int(sess.params, "FirstBurstLength", + sess.FirstBurstLength); + CU_ASSERT(rc == 0); + + rc = iscsi_param_set_int(sess.params, "MaxBurstLength", + sess.MaxBurstLength); + CU_ASSERT(rc == 0); + + rc = iscsi_param_set(sess.params, "InitialR2T", + sess.InitialR2T ? "Yes" : "No"); + CU_ASSERT(rc == 0); + + conn.full_feature = 1; + conn.sess = &sess; + conn.MaxRecvDataSegmentLength = 65536; + + rc = iscsi_conn_params_init(&conn.params); + CU_ASSERT(rc == 0); + + /* construct the data */ + len = snprintf(data + total, 8192 - total, "%s=%d", + "FirstBurstLength", FirstBurstLength); + total += len + 1; + + len = snprintf(data + total, 8192 - total, "%s=%d", + "MaxBurstLength", MaxBurstLength); + total += len + 1; + + len = snprintf(data + total, 8192 - total, "%s=%d", + "InitialR2T", initialR2T); + total += len + 1; + + /* add one extra NUL byte at the end to match real iSCSI params */ + total++; + + /* store incoming parameters */ + rc = iscsi_parse_params(params_p, data, total, false, NULL); + CU_ASSERT(rc == 0); + + /* negotiate parameters */ + rc = iscsi_negotiate_params(&conn, params_p, + data, 8192, rc); + CU_ASSERT(rc > 0); + + rc = iscsi_copy_param2var(&conn); + CU_ASSERT(rc == 0); + CU_ASSERT(conn.sess->FirstBurstLength <= SPDK_ISCSI_FIRST_BURST_LENGTH); + CU_ASSERT(conn.sess->FirstBurstLength <= conn.sess->MaxBurstLength); + CU_ASSERT(conn.sess->MaxBurstLength <= SPDK_ISCSI_MAX_BURST_LENGTH); + CU_ASSERT(conn.sess->MaxOutstandingR2T == 1); + + iscsi_param_free(sess.params); + iscsi_param_free(conn.params); + iscsi_param_free(*params_p); +} + +static void +param_negotiation_test(void) +{ + burst_length_param_negotation(8192, 16384, 0); + burst_length_param_negotation(8192, 16384, 1); + burst_length_param_negotation(8192, 1024, 1); + burst_length_param_negotation(8192, 1024, 0); + burst_length_param_negotation(512, 1024, 1); + burst_length_param_negotation(512, 1024, 0); +} + +static void +list_negotiation_test(void) +{ + int add_param_value = 0; + struct iscsi_param param = {}; + char *new_val; + char valid_list_buf[1024]; + char in_val_buf[1024]; + +#define TEST_LIST(valid_list, in_val, expected_result) \ + do { \ + snprintf(valid_list_buf, sizeof(valid_list_buf), "%s", valid_list); \ + snprintf(in_val_buf, sizeof(in_val_buf), "%s", in_val); \ + new_val = iscsi_negotiate_param_list(&add_param_value, ¶m, valid_list_buf, in_val_buf, NULL); \ + if (expected_result) { \ + SPDK_CU_ASSERT_FATAL(new_val != NULL); \ + CU_ASSERT_STRING_EQUAL(new_val, expected_result); \ + } \ + } while (0) + + TEST_LIST("None", "None", "None"); + TEST_LIST("CHAP,None", "None", "None"); + TEST_LIST("CHAP,None", "CHAP", "CHAP"); + TEST_LIST("KRB5,SRP,CHAP,None", "SRP,CHAP,None", "SRP"); + TEST_LIST("KRB5,SRP,CHAP,None", "CHAP,SRP,None", "CHAP"); + TEST_LIST("KRB5,SRP,CHAP,None", "SPKM1,SRP,CHAP,None", "SRP"); + TEST_LIST("KRB5,SRP,None", "CHAP,None", "None"); +} + +#define PARSE(strconst, partial_enabled, partial_text) \ + data = strconst; \ + len = sizeof(strconst) - 1; \ + rc = iscsi_parse_params(¶ms, data, len, partial_enabled, partial_text) + +#define EXPECT_VAL(key, expected_value) \ + { \ + const char *val = iscsi_param_get_val(params, key); \ + CU_ASSERT(val != NULL); \ + if (val != NULL) { \ + CU_ASSERT(strcmp(val, expected_value) == 0); \ + } \ + } + +#define EXPECT_NULL(key) \ + CU_ASSERT(iscsi_param_get_val(params, key) == NULL) + +static void +parse_valid_test(void) +{ + struct iscsi_param *params = NULL; + int rc; + char *data; + int len; + char *partial_parameter = NULL; + + /* simple test with a single key=value */ + PARSE("Abc=def\0", false, NULL); + CU_ASSERT(rc == 0); + EXPECT_VAL("Abc", "def"); + + /* multiple key=value pairs */ + PARSE("Aaa=bbbbbb\0Xyz=test\0", false, NULL); + CU_ASSERT(rc == 0); + EXPECT_VAL("Aaa", "bbbbbb"); + EXPECT_VAL("Xyz", "test"); + + /* value with embedded '=' */ + PARSE("A=b=c\0", false, NULL); + CU_ASSERT(rc == 0); + EXPECT_VAL("A", "b=c"); + + /* CHAP_C=AAAA.... with value length 8192 */ + len = strlen("CHAP_C=") + ISCSI_TEXT_MAX_VAL_LEN + 1/* null terminators */; + data = malloc(len); + SPDK_CU_ASSERT_FATAL(data != NULL); + memset(data, 'A', len); + memcpy(data, "CHAP_C", 6); + data[6] = '='; + data[len - 1] = '\0'; + rc = iscsi_parse_params(¶ms, data, len, false, NULL); + CU_ASSERT(rc == 0); + free(data); + + /* partial parameter: value is partial */ + PARSE("C=AAA\0D=B", true, &partial_parameter); + SPDK_CU_ASSERT_FATAL(partial_parameter != NULL); + CU_ASSERT_STRING_EQUAL(partial_parameter, "D=B"); + CU_ASSERT(rc == 0); + EXPECT_VAL("C", "AAA"); + EXPECT_NULL("D"); + PARSE("XXXX\0E=UUUU\0", false, &partial_parameter); + CU_ASSERT(rc == 0); + EXPECT_VAL("D", "BXXXX"); + EXPECT_VAL("E", "UUUU"); + CU_ASSERT_PTR_NULL(partial_parameter); + + /* partial parameter: key is partial */ + PARSE("IAMAFAK", true, &partial_parameter); + CU_ASSERT_STRING_EQUAL(partial_parameter, "IAMAFAK"); + CU_ASSERT(rc == 0); + EXPECT_NULL("IAMAFAK"); + PARSE("EDKEY=TTTT\0F=IIII", false, &partial_parameter); + CU_ASSERT(rc == 0); + EXPECT_VAL("IAMAFAKEDKEY", "TTTT"); + EXPECT_VAL("F", "IIII"); + CU_ASSERT_PTR_NULL(partial_parameter); + + /* Second partial parameter is the only parameter */ + PARSE("OOOO", true, &partial_parameter); + CU_ASSERT_STRING_EQUAL(partial_parameter, "OOOO"); + CU_ASSERT(rc == 0); + EXPECT_NULL("OOOO"); + PARSE("LL=MMMM", false, &partial_parameter); + CU_ASSERT(rc == 0); + EXPECT_VAL("OOOOLL", "MMMM"); + CU_ASSERT_PTR_NULL(partial_parameter); + + partial_parameter = NULL; + data = "PartialKey="; + len = 7; + rc = iscsi_parse_params(¶ms, data, len, true, &partial_parameter); + CU_ASSERT(rc == 0); + CU_ASSERT_STRING_EQUAL(partial_parameter, "Partial"); + EXPECT_NULL("PartialKey"); + PARSE("Key=Value", false, &partial_parameter); + CU_ASSERT(rc == 0); + EXPECT_VAL("PartialKey", "Value"); + CU_ASSERT_PTR_NULL(partial_parameter); + + iscsi_param_free(params); +} + +static void +parse_invalid_test(void) +{ + struct iscsi_param *params = NULL; + int rc; + char *data; + int len; + + /* key without '=' */ + PARSE("Abc\0", false, NULL); + CU_ASSERT(rc != 0); + EXPECT_NULL("Abc"); + + /* multiple key=value pairs, one missing '=' */ + PARSE("Abc=def\0Xyz\0Www=test\0", false, NULL); + CU_ASSERT(rc != 0); + EXPECT_VAL("Abc", "def"); + EXPECT_NULL("Xyz"); + EXPECT_NULL("Www"); + + /* empty key */ + PARSE("=abcdef", false, NULL); + CU_ASSERT(rc != 0); + EXPECT_NULL(""); + + /* CHAP_C=AAAA.... with value length 8192 + 1 */ + len = strlen("CHAP_C=") + ISCSI_TEXT_MAX_VAL_LEN + 1 /* max value len + 1 */ + + 1 /* null terminators */; + data = malloc(len); + SPDK_CU_ASSERT_FATAL(data != NULL); + memset(data, 'A', len); + memcpy(data, "CHAP_C", 6); + data[6] = '='; + data[len - 1] = '\0'; + rc = iscsi_parse_params(¶ms, data, len, false, NULL); + free(data); + CU_ASSERT(rc != 0); + EXPECT_NULL("CHAP_C"); + + /* Test simple value, length of value bigger than 255 */ + len = strlen("A=") + ISCSI_TEXT_MAX_SIMPLE_VAL_LEN + 1 /* max simple value len + 1 */ + + 1 /* null terminators */; + data = malloc(len); + SPDK_CU_ASSERT_FATAL(data != NULL); + memset(data, 'A', len); + data[1] = '='; + data[len - 1] = '\0'; + rc = iscsi_parse_params(¶ms, data, len, false, NULL); + free(data); + CU_ASSERT(rc != 0); + EXPECT_NULL("A"); + + /* key length bigger than 63 */ + len = ISCSI_TEXT_MAX_KEY_LEN + 1 /* max key length + 1 */ + 1 /* = */ + 1 /* A */ + + 1 /* null terminators */; + data = malloc(len); + SPDK_CU_ASSERT_FATAL(data != NULL); + memset(data, 'A', len); + data[64] = '='; + data[len - 1] = '\0'; + rc = iscsi_parse_params(¶ms, data, len, false, NULL); + free(data); + CU_ASSERT(rc != 0); + EXPECT_NULL("A"); + + /* duplicated key */ + PARSE("B=BB", false, NULL); + CU_ASSERT(rc == 0); + PARSE("B=BBBB", false, NULL); + CU_ASSERT(rc != 0); + EXPECT_VAL("B", "BB"); + + /* Test where data buffer has non-NULL characters past the end of + * the valid data region. This can happen with SPDK iSCSI target, + * since data buffers are reused and we do not zero the data buffers + * after they are freed since it would be too expensive. Added as + * part of fixing an intermittent Calsoft failure that triggered this + * bug. + */ + data = "MaxRecvDataSegmentLength=81928"; + len = strlen(data) - 1; + rc = iscsi_parse_params(¶ms, data, len, false, NULL); + EXPECT_VAL("MaxRecvDataSegmentLength", "8192"); + CU_ASSERT(rc == 0); + iscsi_param_free(params); +} + +int +main(int argc, char **argv) +{ + CU_pSuite suite = NULL; + unsigned int num_failures; + + CU_set_error_action(CUEA_ABORT); + CU_initialize_registry(); + + suite = CU_add_suite("iscsi_suite", NULL, NULL); + + CU_ADD_TEST(suite, param_negotiation_test); + CU_ADD_TEST(suite, list_negotiation_test); + CU_ADD_TEST(suite, parse_valid_test); + CU_ADD_TEST(suite, parse_invalid_test); + + CU_basic_set_mode(CU_BRM_VERBOSE); + CU_basic_run_tests(); + num_failures = CU_get_number_of_failures(); + CU_cleanup_registry(); + return num_failures; +} diff --git a/src/spdk/test/unit/lib/iscsi/portal_grp.c/.gitignore b/src/spdk/test/unit/lib/iscsi/portal_grp.c/.gitignore new file mode 100644 index 000000000..106ffebc2 --- /dev/null +++ b/src/spdk/test/unit/lib/iscsi/portal_grp.c/.gitignore @@ -0,0 +1 @@ +portal_grp_ut diff --git a/src/spdk/test/unit/lib/iscsi/portal_grp.c/Makefile b/src/spdk/test/unit/lib/iscsi/portal_grp.c/Makefile new file mode 100644 index 000000000..f3ca0646f --- /dev/null +++ b/src/spdk/test/unit/lib/iscsi/portal_grp.c/Makefile @@ -0,0 +1,40 @@ +# +# BSD LICENSE +# +# Copyright (c) Intel Corporation. +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in +# the documentation and/or other materials provided with the +# distribution. +# * Neither the name of Intel Corporation nor the names of its +# contributors may be used to endorse or promote products derived +# from this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +# + +SPDK_ROOT_DIR := $(abspath $(CURDIR)/../../../../..) + +SPDK_LIB_LIST = conf + +TEST_FILE = portal_grp_ut.c + +include $(SPDK_ROOT_DIR)/mk/spdk.unittest.mk diff --git a/src/spdk/test/unit/lib/iscsi/portal_grp.c/portal_grp_ut.c b/src/spdk/test/unit/lib/iscsi/portal_grp.c/portal_grp_ut.c new file mode 100644 index 000000000..a89a1567f --- /dev/null +++ b/src/spdk/test/unit/lib/iscsi/portal_grp.c/portal_grp_ut.c @@ -0,0 +1,419 @@ +/*- + * BSD LICENSE + * + * Copyright (c) Intel Corporation. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Intel Corporation nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "spdk/stdinc.h" + +#include "spdk_cunit.h" + +#include "common/lib/ut_multithread.c" +#include "common/lib/test_sock.c" + +#include "../common.c" +#include "iscsi/portal_grp.c" +#include "unit/lib/json_mock.c" + +#include "spdk_internal/thread.h" + +DEFINE_STUB(iscsi_conn_construct, int, + (struct spdk_iscsi_portal *portal, struct spdk_sock *sock), + 0); + +struct spdk_iscsi_globals g_iscsi; + +static int +test_setup(void) +{ + TAILQ_INIT(&g_iscsi.portal_head); + TAILQ_INIT(&g_iscsi.pg_head); + pthread_mutex_init(&g_iscsi.mutex, NULL); + return 0; +} + +static void +portal_create_ipv4_normal_case(void) +{ + struct spdk_iscsi_portal *p; + + const char *host = "192.168.2.0"; + const char *port = "3260"; + + p = iscsi_portal_create(host, port); + CU_ASSERT(p != NULL); + + iscsi_portal_destroy(p); + CU_ASSERT(TAILQ_EMPTY(&g_iscsi.portal_head)); +} + +static void +portal_create_ipv6_normal_case(void) +{ + struct spdk_iscsi_portal *p; + + const char *host = "[2001:ad6:1234::]"; + const char *port = "3260"; + + p = iscsi_portal_create(host, port); + CU_ASSERT(p != NULL); + + iscsi_portal_destroy(p); + CU_ASSERT(TAILQ_EMPTY(&g_iscsi.portal_head)); +} + +static void +portal_create_ipv4_wildcard_case(void) +{ + struct spdk_iscsi_portal *p; + + const char *host = "*"; + const char *port = "3260"; + + p = iscsi_portal_create(host, port); + CU_ASSERT(p != NULL); + + iscsi_portal_destroy(p); + CU_ASSERT(TAILQ_EMPTY(&g_iscsi.portal_head)); +} + +static void +portal_create_ipv6_wildcard_case(void) +{ + struct spdk_iscsi_portal *p; + + const char *host = "[*]"; + const char *port = "3260"; + + p = iscsi_portal_create(host, port); + CU_ASSERT(p != NULL); + + iscsi_portal_destroy(p); + CU_ASSERT(TAILQ_EMPTY(&g_iscsi.portal_head)); +} + +static void +portal_create_twice_case(void) +{ + struct spdk_iscsi_portal *p1, *p2; + + const char *host = "192.168.2.0"; + const char *port = "3260"; + + p1 = iscsi_portal_create(host, port); + CU_ASSERT(p1 != NULL); + + p2 = iscsi_portal_create(host, port); + CU_ASSERT(p2 == NULL); + + iscsi_portal_destroy(p1); + CU_ASSERT(TAILQ_EMPTY(&g_iscsi.portal_head)); +} + +static void +parse_portal_ipv4_normal_case(void) +{ + const char *string = "192.168.2.0:3260"; + const char *host_str = "192.168.2.0"; + const char *port_str = "3260"; + struct spdk_iscsi_portal *p = NULL; + int rc; + + rc = iscsi_parse_portal(string, &p); + CU_ASSERT(rc == 0); + SPDK_CU_ASSERT_FATAL(p != NULL); + CU_ASSERT(strcmp(p->host, host_str) == 0); + CU_ASSERT(strcmp(p->port, port_str) == 0); + + iscsi_portal_destroy(p); + CU_ASSERT(TAILQ_EMPTY(&g_iscsi.portal_head)); + +} + +static void +parse_portal_ipv6_normal_case(void) +{ + const char *string = "[2001:ad6:1234::]:3260"; + const char *host_str = "[2001:ad6:1234::]"; + const char *port_str = "3260"; + struct spdk_iscsi_portal *p = NULL; + int rc; + + rc = iscsi_parse_portal(string, &p); + CU_ASSERT(rc == 0); + SPDK_CU_ASSERT_FATAL(p != NULL); + CU_ASSERT(strcmp(p->host, host_str) == 0); + CU_ASSERT(strcmp(p->port, port_str) == 0); + + iscsi_portal_destroy(p); + CU_ASSERT(TAILQ_EMPTY(&g_iscsi.portal_head)); +} + +static void +parse_portal_ipv4_skip_port_case(void) +{ + const char *string = "192.168.2.0"; + const char *host_str = "192.168.2.0"; + const char *port_str = "3260"; + struct spdk_iscsi_portal *p = NULL; + int rc; + + rc = iscsi_parse_portal(string, &p); + CU_ASSERT(rc == 0); + SPDK_CU_ASSERT_FATAL(p != NULL); + CU_ASSERT(strcmp(p->host, host_str) == 0); + CU_ASSERT(strcmp(p->port, port_str) == 0); + + iscsi_portal_destroy(p); + CU_ASSERT(TAILQ_EMPTY(&g_iscsi.portal_head)); +} + +static void +parse_portal_ipv6_skip_port_case(void) +{ + const char *string = "[2001:ad6:1234::]"; + const char *host_str = "[2001:ad6:1234::]"; + const char *port_str = "3260"; + struct spdk_iscsi_portal *p = NULL; + int rc; + + rc = iscsi_parse_portal(string, &p); + CU_ASSERT(rc == 0); + SPDK_CU_ASSERT_FATAL(p != NULL); + CU_ASSERT(strcmp(p->host, host_str) == 0); + CU_ASSERT(strcmp(p->port, port_str) == 0); + + iscsi_portal_destroy(p); + CU_ASSERT(TAILQ_EMPTY(&g_iscsi.portal_head)); +} + +static void +portal_grp_register_unregister_case(void) +{ + struct spdk_iscsi_portal *p; + struct spdk_iscsi_portal_grp *pg1, *pg2; + int rc; + const char *host = "192.168.2.0"; + const char *port = "3260"; + + pg1 = iscsi_portal_grp_create(1); + CU_ASSERT(pg1 != NULL); + + p = iscsi_portal_create(host, port); + CU_ASSERT(p != NULL); + + iscsi_portal_grp_add_portal(pg1, p); + + rc = iscsi_portal_grp_register(pg1); + CU_ASSERT(rc == 0); + + pg2 = iscsi_portal_grp_unregister(1); + CU_ASSERT(pg2 != NULL); + CU_ASSERT(pg1 == pg2); + + CU_ASSERT(TAILQ_EMPTY(&g_iscsi.pg_head)); + + iscsi_portal_grp_destroy(pg1); + + CU_ASSERT(TAILQ_EMPTY(&g_iscsi.portal_head)); +} + +static void +portal_grp_register_twice_case(void) +{ + struct spdk_iscsi_portal *p; + struct spdk_iscsi_portal_grp *pg1, *pg2; + int rc; + const char *host = "192.168.2.0"; + const char *port = "3260"; + + pg1 = iscsi_portal_grp_create(1); + CU_ASSERT(pg1 != NULL); + + p = iscsi_portal_create(host, port); + CU_ASSERT(p != NULL); + + iscsi_portal_grp_add_portal(pg1, p); + + rc = iscsi_portal_grp_register(pg1); + CU_ASSERT(rc == 0); + + rc = iscsi_portal_grp_register(pg1); + CU_ASSERT(rc != 0); + + pg2 = iscsi_portal_grp_unregister(1); + CU_ASSERT(pg2 != NULL); + CU_ASSERT(pg1 == pg2); + + CU_ASSERT(TAILQ_EMPTY(&g_iscsi.pg_head)); + + iscsi_portal_grp_destroy(pg1); + + CU_ASSERT(TAILQ_EMPTY(&g_iscsi.portal_head)); +} + +static void +portal_grp_add_delete_case(void) +{ + struct spdk_sock sock = {}; + struct spdk_iscsi_portal_grp *pg1, *pg2; + struct spdk_iscsi_portal *p; + int rc; + + const char *host = "192.168.2.0"; + const char *port = "3260"; + + allocate_threads(1); + set_thread(0); + + /* internal of iscsi_create_portal_group */ + pg1 = iscsi_portal_grp_create(1); + CU_ASSERT(pg1 != NULL); + + p = iscsi_portal_create(host, port); + CU_ASSERT(p != NULL); + + iscsi_portal_grp_add_portal(pg1, p); + + MOCK_SET(spdk_sock_listen, &sock); + rc = iscsi_portal_grp_open(pg1); + CU_ASSERT(rc == 0); + MOCK_CLEAR_P(spdk_sock_listen); + + rc = iscsi_portal_grp_register(pg1); + CU_ASSERT(rc == 0); + + /* internal of delete_portal_group */ + pg2 = iscsi_portal_grp_unregister(1); + CU_ASSERT(pg2 != NULL); + CU_ASSERT(pg1 == pg2); + + iscsi_portal_grp_release(pg2); + + poll_thread(0); + + CU_ASSERT(TAILQ_EMPTY(&g_iscsi.portal_head)); + CU_ASSERT(TAILQ_EMPTY(&g_iscsi.pg_head)); + + free_threads(); +} + +static void +portal_grp_add_delete_twice_case(void) +{ + struct spdk_sock sock = {}; + struct spdk_iscsi_portal_grp *pg1, *pg2; + struct spdk_iscsi_portal *p; + int rc; + + const char *host = "192.168.2.0"; + const char *port1 = "3260", *port2 = "3261"; + + allocate_threads(1); + set_thread(0); + + /* internal of iscsi_create_portal_group related */ + pg1 = iscsi_portal_grp_create(1); + CU_ASSERT(pg1 != NULL); + + p = iscsi_portal_create(host, port1); + CU_ASSERT(p != NULL); + + iscsi_portal_grp_add_portal(pg1, p); + + MOCK_SET(spdk_sock_listen, &sock); + rc = iscsi_portal_grp_open(pg1); + CU_ASSERT(rc == 0); + + rc = iscsi_portal_grp_register(pg1); + CU_ASSERT(rc == 0); + + /* internal of iscsi_create_portal_group related */ + pg2 = iscsi_portal_grp_create(2); + CU_ASSERT(pg2 != NULL); + + p = iscsi_portal_create(host, port2); + CU_ASSERT(p != NULL); + + iscsi_portal_grp_add_portal(pg2, p); + + rc = iscsi_portal_grp_open(pg2); + CU_ASSERT(rc == 0); + + rc = iscsi_portal_grp_register(pg2); + CU_ASSERT(rc == 0); + + /* internal of destroy_portal_group related */ + iscsi_portal_grp_close(pg1); + iscsi_portal_grp_close(pg2); + + poll_thread(0); + + iscsi_portal_grps_destroy(); + + CU_ASSERT(TAILQ_EMPTY(&g_iscsi.portal_head)); + CU_ASSERT(TAILQ_EMPTY(&g_iscsi.pg_head)); + + MOCK_CLEAR_P(spdk_sock_listen); + + free_threads(); +} + +int +main(int argc, char **argv) +{ + CU_pSuite suite = NULL; + unsigned int num_failures; + + CU_set_error_action(CUEA_ABORT); + CU_initialize_registry(); + + suite = CU_add_suite("portal_grp_suite", test_setup, NULL); + + CU_ADD_TEST(suite, portal_create_ipv4_normal_case); + CU_ADD_TEST(suite, portal_create_ipv6_normal_case); + CU_ADD_TEST(suite, portal_create_ipv4_wildcard_case); + CU_ADD_TEST(suite, portal_create_ipv6_wildcard_case); + CU_ADD_TEST(suite, portal_create_twice_case); + CU_ADD_TEST(suite, parse_portal_ipv4_normal_case); + CU_ADD_TEST(suite, parse_portal_ipv6_normal_case); + CU_ADD_TEST(suite, parse_portal_ipv4_skip_port_case); + CU_ADD_TEST(suite, parse_portal_ipv6_skip_port_case); + CU_ADD_TEST(suite, portal_grp_register_unregister_case); + CU_ADD_TEST(suite, portal_grp_register_twice_case); + CU_ADD_TEST(suite, portal_grp_add_delete_case); + CU_ADD_TEST(suite, portal_grp_add_delete_twice_case); + + CU_basic_set_mode(CU_BRM_VERBOSE); + CU_basic_run_tests(); + num_failures = CU_get_number_of_failures(); + CU_cleanup_registry(); + return num_failures; +} diff --git a/src/spdk/test/unit/lib/iscsi/tgt_node.c/.gitignore b/src/spdk/test/unit/lib/iscsi/tgt_node.c/.gitignore new file mode 100644 index 000000000..010d84b83 --- /dev/null +++ b/src/spdk/test/unit/lib/iscsi/tgt_node.c/.gitignore @@ -0,0 +1 @@ +tgt_node_ut diff --git a/src/spdk/test/unit/lib/iscsi/tgt_node.c/Makefile b/src/spdk/test/unit/lib/iscsi/tgt_node.c/Makefile new file mode 100644 index 000000000..90bd4f990 --- /dev/null +++ b/src/spdk/test/unit/lib/iscsi/tgt_node.c/Makefile @@ -0,0 +1,39 @@ +# +# BSD LICENSE +# +# Copyright (c) Intel Corporation. +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in +# the documentation and/or other materials provided with the +# distribution. +# * Neither the name of Intel Corporation nor the names of its +# contributors may be used to endorse or promote products derived +# from this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +# + +SPDK_ROOT_DIR := $(abspath $(CURDIR)/../../../../..) + +SPDK_LIB_LIST = conf +TEST_FILE = tgt_node_ut.c + +include $(SPDK_ROOT_DIR)/mk/spdk.unittest.mk diff --git a/src/spdk/test/unit/lib/iscsi/tgt_node.c/tgt_node.conf b/src/spdk/test/unit/lib/iscsi/tgt_node.c/tgt_node.conf new file mode 100644 index 000000000..6bf5aa664 --- /dev/null +++ b/src/spdk/test/unit/lib/iscsi/tgt_node.c/tgt_node.conf @@ -0,0 +1,95 @@ +[Global] + +# Test that parsing fails if there is no TargetName +[Failure0] + TargetAlias "Data Disk1" + Mapping PortalGroup1 InitiatorGroup1 + AuthMethod Auto + AuthGroup AuthGroup1 + UseDigest Auto + QueueDepth 128 + LUN0 Malloc0 + LUN1 Malloc1 + +# Test that parsing fails if there is no Mapping +[Failure1] + TargetName target1 + TargetAlias "Data Disk1" + AuthMethod Auto + AuthGroup AuthGroup1 + UseDigest Auto + QueueDepth 128 + LUN0 Malloc0 + LUN1 Malloc1 + +# Test that parsing fails if Mapping does not define Portal or InitiatorGroup +[Failure2] + TargetName target1 + TargetAlias "Data Disk1" + Mapping + AuthMethod Auto + AuthGroup AuthGroup1 + UseDigest Auto + QueueDepth 128 + LUN0 Malloc0 + LUN1 Malloc1 + +# Test that parsing fails if Mapping does not define InitiatorGroup +[Failure3] + TargetName target1 + TargetAlias "Data Disk1" + Mapping PortalGroup1 + AuthMethod Auto + AuthGroup AuthGroup1 + UseDigest Auto + QueueDepth 128 + LUN0 Malloc0 + LUN1 Malloc1 + +# Test that parsing fails if Mapping switches PortalGroup/InitiatorGroup order +[Failure4] + TargetName target1 + TargetAlias "Data Disk1" + Mapping InitiatorGroup1 PortalGroup1 + AuthMethod Auto + AuthGroup AuthGroup1 + UseDigest Auto + QueueDepth 128 + LUN0 Malloc0 + LUN1 Malloc1 + +# Test that parsing fails if Mapping uses invalid InitiatorGroup0 +[Failure5] + TargetName target1 + TargetAlias "Data Disk1" + Mapping PortalGroup1 InitiatorGroup0 + AuthMethod Auto + AuthGroup AuthGroup1 + UseDigest Auto + QueueDepth 128 + LUN0 Malloc0 + LUN1 Malloc1 + +# Test that parsing fails if Mapping uses invalid PortalGroup0 +[Failure6] + TargetName target1 + TargetAlias "Data Disk1" + Mapping PortalGroup0 InitiatorGroup1 + AuthMethod Auto + AuthGroup AuthGroup1 + UseDigest Auto + QueueDepth 128 + LUN0 Malloc0 + LUN1 Malloc1 + +# Test that parsing fails if AuthMethod is invalid +[Failure7] + TargetName target1 + TargetAlias "Data Disk1" + Mapping PortalGroup1 InitiatorGroup1 + AuthMethod SomeGarbage + AuthGroup AuthGroup1 + UseDigest Auto + QueueDepth 128 + LUN0 Malloc0 + LUN1 Malloc1 diff --git a/src/spdk/test/unit/lib/iscsi/tgt_node.c/tgt_node_ut.c b/src/spdk/test/unit/lib/iscsi/tgt_node.c/tgt_node_ut.c new file mode 100644 index 000000000..3f3bda39b --- /dev/null +++ b/src/spdk/test/unit/lib/iscsi/tgt_node.c/tgt_node_ut.c @@ -0,0 +1,832 @@ +/*- + * BSD LICENSE + * + * Copyright (c) Intel Corporation. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Intel Corporation nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "spdk/stdinc.h" + +#include "spdk/scsi.h" + +#include "CUnit/Basic.h" +#include "spdk_internal/mock.h" + +#include "../common.c" +#include "iscsi/tgt_node.c" +#include "scsi/scsi_internal.h" +#include "unit/lib/json_mock.c" +#include "common/lib/test_env.c" + +struct spdk_iscsi_globals g_iscsi; + +const char *config_file; + +DEFINE_STUB(spdk_scsi_dev_get_id, + int, + (const struct spdk_scsi_dev *dev), + 0); + +DEFINE_STUB(spdk_scsi_lun_get_bdev_name, + const char *, + (const struct spdk_scsi_lun *lun), + NULL); + +DEFINE_STUB(spdk_scsi_lun_get_id, + int, + (const struct spdk_scsi_lun *lun), + 0); + +DEFINE_STUB_V(spdk_iscsi_op_abort_task_set, + (struct spdk_iscsi_task *task, + uint8_t function)); + +DEFINE_STUB(spdk_sock_is_ipv6, bool, (struct spdk_sock *sock), false); + +DEFINE_STUB(spdk_sock_is_ipv4, bool, (struct spdk_sock *sock), false); + +DEFINE_STUB(iscsi_portal_grp_find_by_tag, + struct spdk_iscsi_portal_grp *, (int tag), NULL); + +DEFINE_STUB(iscsi_init_grp_find_by_tag, struct spdk_iscsi_init_grp *, + (int tag), NULL); + +struct spdk_scsi_lun * +spdk_scsi_dev_get_lun(struct spdk_scsi_dev *dev, int lun_id) +{ + if (lun_id < 0 || lun_id >= SPDK_SCSI_DEV_MAX_LUN) { + return NULL; + } + + return dev->lun[lun_id]; +} + +int +spdk_scsi_dev_add_lun(struct spdk_scsi_dev *dev, const char *bdev_name, int lun_id, + void (*hotremove_cb)(const struct spdk_scsi_lun *, void *), + void *hotremove_ctx) +{ + if (bdev_name == NULL) { + return -1; + } else { + return 0; + } +} + +static void +add_lun_test_cases(void) +{ + struct spdk_iscsi_tgt_node tgtnode = {}; + int lun_id = 0; + char *bdev_name = NULL; + struct spdk_scsi_dev scsi_dev = {}; + int rc; + + /* case 1 */ + tgtnode.num_active_conns = 1; + + rc = iscsi_tgt_node_add_lun(&tgtnode, bdev_name, lun_id); + CU_ASSERT(rc != 0); + + /* case 2 */ + tgtnode.num_active_conns = 0; + lun_id = -2; + + rc = iscsi_tgt_node_add_lun(&tgtnode, bdev_name, lun_id); + CU_ASSERT(rc != 0); + + /* case 3 */ + lun_id = SPDK_SCSI_DEV_MAX_LUN; + + rc = iscsi_tgt_node_add_lun(&tgtnode, bdev_name, lun_id); + CU_ASSERT(rc != 0); + + /* case 4 */ + lun_id = -1; + tgtnode.dev = NULL; + + rc = iscsi_tgt_node_add_lun(&tgtnode, bdev_name, lun_id); + CU_ASSERT(rc != 0); + + /* case 5 */ + tgtnode.dev = &scsi_dev; + + rc = iscsi_tgt_node_add_lun(&tgtnode, bdev_name, lun_id); + CU_ASSERT(rc != 0); + + /* case 6 */ + bdev_name = "LUN0"; + + rc = iscsi_tgt_node_add_lun(&tgtnode, bdev_name, lun_id); + CU_ASSERT(rc == 0); +} + +static void +config_file_fail_cases(void) +{ + struct spdk_conf *config; + struct spdk_conf_section *sp; + char section_name[64]; + int section_index; + int rc; + + config = spdk_conf_allocate(); + + rc = spdk_conf_read(config, config_file); + CU_ASSERT(rc == 0); + + section_index = 0; + while (true) { + snprintf(section_name, sizeof(section_name), "Failure%d", section_index); + sp = spdk_conf_find_section(config, section_name); + if (sp == NULL) { + break; + } + rc = iscsi_parse_tgt_node(sp); + CU_ASSERT(rc < 0); + section_index++; + } + + spdk_conf_free(config); +} + +static void +allow_any_allowed(void) +{ + bool result; + char *netmask; + char *addr1, *addr2; + + netmask = "ANY"; + addr1 = "2001:ad6:1234:5678:9abc::"; + addr2 = "192.168.2.1"; + + result = iscsi_netmask_allow_addr(netmask, addr1); + CU_ASSERT(result == true); + + result = iscsi_netmask_allow_addr(netmask, addr2); + CU_ASSERT(result == true); +} + +static void +allow_ipv6_allowed(void) +{ + bool result; + char *netmask; + char *addr; + + netmask = "[2001:ad6:1234::]/48"; + addr = "2001:ad6:1234:5678:9abc::"; + + result = iscsi_ipv6_netmask_allow_addr(netmask, addr); + CU_ASSERT(result == true); + + result = iscsi_netmask_allow_addr(netmask, addr); + CU_ASSERT(result == true); + + /* Netmask prefix bits == 128 (all bits must match) */ + netmask = "[2001:ad6:1234:5678:9abc::1]/128"; + addr = "2001:ad6:1234:5678:9abc::1"; + result = iscsi_ipv6_netmask_allow_addr(netmask, addr); + CU_ASSERT(result == true); +} + +static void +allow_ipv6_denied(void) +{ + bool result; + char *netmask; + char *addr; + + netmask = "[2001:ad6:1234::]/56"; + addr = "2001:ad6:1234:5678:9abc::"; + + result = iscsi_ipv6_netmask_allow_addr(netmask, addr); + CU_ASSERT(result == false); + + result = iscsi_netmask_allow_addr(netmask, addr); + CU_ASSERT(result == false); + + /* Netmask prefix bits == 128 (all bits must match) */ + netmask = "[2001:ad6:1234:5678:9abc::1]/128"; + addr = "2001:ad6:1234:5678:9abc::2"; + result = iscsi_ipv6_netmask_allow_addr(netmask, addr); + CU_ASSERT(result == false); +} + +static void +allow_ipv6_invalid(void) +{ + bool result; + char *netmask; + char *addr; + + /* Netmask prefix bits > 128 */ + netmask = "[2001:ad6:1234::]/129"; + addr = "2001:ad6:1234:5678:9abc::"; + result = iscsi_ipv6_netmask_allow_addr(netmask, addr); + CU_ASSERT(result == false); + + /* Netmask prefix bits == 0 */ + netmask = "[2001:ad6:1234::]/0"; + addr = "2001:ad6:1234:5678:9abc::"; + result = iscsi_ipv6_netmask_allow_addr(netmask, addr); + CU_ASSERT(result == false); + + /* Netmask prefix bits < 0 */ + netmask = "[2001:ad6:1234::]/-1"; + addr = "2001:ad6:1234:5678:9abc::"; + result = iscsi_ipv6_netmask_allow_addr(netmask, addr); + CU_ASSERT(result == false); +} + +static void +allow_ipv4_allowed(void) +{ + bool result; + char *netmask; + char *addr; + + netmask = "192.168.2.0/24"; + addr = "192.168.2.1"; + + result = iscsi_ipv4_netmask_allow_addr(netmask, addr); + CU_ASSERT(result == true); + + result = iscsi_netmask_allow_addr(netmask, addr); + CU_ASSERT(result == true); + + /* Netmask prefix == 32 (all bits must match) */ + netmask = "192.168.2.1/32"; + addr = "192.168.2.1"; + result = iscsi_ipv4_netmask_allow_addr(netmask, addr); + CU_ASSERT(result == true); +} + +static void +allow_ipv4_denied(void) +{ + bool result; + char *netmask; + char *addr; + + netmask = "192.168.2.0"; + addr = "192.168.2.1"; + + result = iscsi_ipv4_netmask_allow_addr(netmask, addr); + CU_ASSERT(result == false); + + result = iscsi_netmask_allow_addr(netmask, addr); + CU_ASSERT(result == false); + + /* Netmask prefix == 32 (all bits must match) */ + netmask = "192.168.2.1/32"; + addr = "192.168.2.2"; + result = iscsi_ipv4_netmask_allow_addr(netmask, addr); + CU_ASSERT(result == false); +} + +static void +allow_ipv4_invalid(void) +{ + bool result; + char *netmask; + char *addr; + + /* Netmask prefix bits > 32 */ + netmask = "192.168.2.0/33"; + addr = "192.168.2.1"; + result = iscsi_ipv4_netmask_allow_addr(netmask, addr); + CU_ASSERT(result == false); + + /* Netmask prefix bits == 0 */ + netmask = "192.168.2.0/0"; + addr = "192.168.2.1"; + result = iscsi_ipv4_netmask_allow_addr(netmask, addr); + CU_ASSERT(result == false); + + /* Netmask prefix bits < 0 */ + netmask = "192.168.2.0/-1"; + addr = "192.168.2.1"; + result = iscsi_ipv4_netmask_allow_addr(netmask, addr); + CU_ASSERT(result == false); +} + +static void +node_access_allowed(void) +{ + struct spdk_iscsi_tgt_node tgtnode = {}; + struct spdk_iscsi_portal_grp pg = {}; + struct spdk_iscsi_init_grp ig = {}; + struct spdk_iscsi_conn conn = {}; + struct spdk_iscsi_portal portal = {}; + struct spdk_iscsi_initiator_name iname = {}; + struct spdk_iscsi_initiator_netmask imask = {}; + struct spdk_scsi_dev scsi_dev = {}; + struct spdk_iscsi_pg_map *pg_map; + char *iqn, *addr; + bool result; + + /* portal group initialization */ + pg.tag = 1; + + /* initiator group initialization */ + ig.tag = 1; + + ig.ninitiators = 1; + snprintf(iname.name, sizeof(iname.name), "iqn.2017-10.spdk.io:0001"); + TAILQ_INIT(&ig.initiator_head); + TAILQ_INSERT_TAIL(&ig.initiator_head, &iname, tailq); + + ig.nnetmasks = 1; + snprintf(imask.mask, sizeof(imask.mask), "192.168.2.0/24"); + TAILQ_INIT(&ig.netmask_head); + TAILQ_INSERT_TAIL(&ig.netmask_head, &imask, tailq); + + /* target initialization */ + snprintf(tgtnode.name, sizeof(tgtnode.name), "iqn.2017-10.spdk.io:0001"); + TAILQ_INIT(&tgtnode.pg_map_head); + + snprintf(scsi_dev.name, sizeof(scsi_dev.name), "iqn.2017-10.spdk.io:0001"); + tgtnode.dev = &scsi_dev; + + pg_map = iscsi_tgt_node_add_pg_map(&tgtnode, &pg); + iscsi_pg_map_add_ig_map(pg_map, &ig); + + /* portal initialization */ + portal.group = &pg; + snprintf(portal.host, sizeof(portal.host), "192.168.2.0"); + snprintf(portal.port, sizeof(portal.port), "3260"); + + /* input for UT */ + conn.portal = &portal; + + iqn = "iqn.2017-10.spdk.io:0001"; + addr = "192.168.2.1"; + + result = iscsi_tgt_node_access(&conn, &tgtnode, iqn, addr); + CU_ASSERT(result == true); + + iscsi_pg_map_delete_ig_map(pg_map, &ig); + iscsi_tgt_node_delete_pg_map(&tgtnode, &pg); +} + +static void +node_access_denied_by_empty_netmask(void) +{ + struct spdk_iscsi_tgt_node tgtnode = {}; + struct spdk_iscsi_portal_grp pg = {}; + struct spdk_iscsi_init_grp ig = {}; + struct spdk_iscsi_conn conn = {}; + struct spdk_iscsi_portal portal = {}; + struct spdk_iscsi_initiator_name iname = {}; + struct spdk_scsi_dev scsi_dev = {}; + struct spdk_iscsi_pg_map *pg_map; + char *iqn, *addr; + bool result; + + /* portal group initialization */ + pg.tag = 1; + + /* initiator group initialization */ + ig.tag = 1; + + ig.ninitiators = 1; + snprintf(iname.name, sizeof(iname.name), "iqn.2017-10.spdk.io:0001"); + TAILQ_INIT(&ig.initiator_head); + TAILQ_INSERT_TAIL(&ig.initiator_head, &iname, tailq); + + ig.nnetmasks = 0; + TAILQ_INIT(&ig.netmask_head); + + /* target initialization */ + snprintf(tgtnode.name, sizeof(tgtnode.name), "iqn.2017-10.spdk.io:0001"); + TAILQ_INIT(&tgtnode.pg_map_head); + + snprintf(scsi_dev.name, sizeof(scsi_dev.name), "iqn.2017-10.spdk.io:0001"); + tgtnode.dev = &scsi_dev; + + pg_map = iscsi_tgt_node_add_pg_map(&tgtnode, &pg); + iscsi_pg_map_add_ig_map(pg_map, &ig); + + /* portal initialization */ + portal.group = &pg; + snprintf(portal.host, sizeof(portal.host), "192.168.2.0"); + snprintf(portal.port, sizeof(portal.port), "3260"); + + /* input for UT */ + conn.portal = &portal; + + iqn = "iqn.2017-10.spdk.io:0001"; + addr = "192.168.3.1"; + + result = iscsi_tgt_node_access(&conn, &tgtnode, iqn, addr); + CU_ASSERT(result == false); + + iscsi_pg_map_delete_ig_map(pg_map, &ig); + iscsi_tgt_node_delete_pg_map(&tgtnode, &pg); +} + +#define IQN1 "iqn.2017-11.spdk.io:0001" +#define NO_IQN1 "!iqn.2017-11.spdk.io:0001" +#define IQN2 "iqn.2017-11.spdk.io:0002" +#define IP1 "192.168.2.0" +#define IP2 "192.168.2.1" + +static void +node_access_multi_initiator_groups_cases(void) +{ + struct spdk_iscsi_tgt_node tgtnode = {}; + struct spdk_iscsi_conn conn = {}; + struct spdk_iscsi_portal_grp pg = {}; + struct spdk_iscsi_portal portal = {}; + struct spdk_iscsi_init_grp ig1 = {}, ig2 = {}; + struct spdk_iscsi_initiator_name iname1 = {}, iname2 = {}; + struct spdk_iscsi_initiator_netmask imask1 = {}, imask2 = {}; + struct spdk_scsi_dev scsi_dev = {}; + struct spdk_iscsi_pg_map *pg_map; + char *iqn, *addr; + bool result; + + /* target initialization */ + snprintf(tgtnode.name, sizeof(tgtnode.name), IQN1); + TAILQ_INIT(&tgtnode.pg_map_head); + + snprintf(scsi_dev.name, sizeof(scsi_dev.name), IQN1); + tgtnode.dev = &scsi_dev; + + /* initiator group initialization */ + ig1.tag = 1; + TAILQ_INIT(&ig1.initiator_head); + TAILQ_INIT(&ig1.netmask_head); + + ig1.ninitiators = 1; + TAILQ_INSERT_TAIL(&ig1.initiator_head, &iname1, tailq); + + ig1.nnetmasks = 1; + TAILQ_INSERT_TAIL(&ig1.netmask_head, &imask1, tailq); + + ig2.tag = 2; + TAILQ_INIT(&ig2.initiator_head); + TAILQ_INIT(&ig2.netmask_head); + + ig2.ninitiators = 1; + TAILQ_INSERT_TAIL(&ig2.initiator_head, &iname2, tailq); + + ig2.nnetmasks = 1; + TAILQ_INSERT_TAIL(&ig2.netmask_head, &imask2, tailq); + + /* portal group initialization */ + pg.tag = 1; + + pg_map = iscsi_tgt_node_add_pg_map(&tgtnode, &pg); + iscsi_pg_map_add_ig_map(pg_map, &ig1); + iscsi_pg_map_add_ig_map(pg_map, &ig2); + + /* portal initialization */ + portal.group = &pg; + snprintf(portal.host, sizeof(portal.host), IP1); + snprintf(portal.port, sizeof(portal.port), "3260"); + + /* connection initialization */ + conn.portal = &portal; + + iqn = IQN1; + addr = IP1; + + /* + * case 1: + * +-------------------------------------------+---------+ + * | IG1 | IG2 | | + * +-------------------------------------------+ | + * | name | addr | name | addr | result | + * +-------------------------------------------+---------+ + * +-------------------------------------------+---------+ + * | denied | - | - | - | denied | + * +-------------------------------------------+---------+ + */ + snprintf(iname1.name, sizeof(iname1.name), NO_IQN1); + + result = iscsi_tgt_node_access(&conn, &tgtnode, iqn, addr); + CU_ASSERT(result == false); + + /* + * case 2: + * +-------------------------------------------+---------+ + * | IG1 | IG2 | | + * +-------------------------------------------+ | + * | name | addr | name | addr | result | + * +-------------------------------------------+---------+ + * +-------------------------------------------+---------+ + * | allowed | allowed | - | - | allowed | + * +-------------------------------------------+---------+ + */ + snprintf(iname1.name, sizeof(iname1.name), IQN1); + snprintf(imask1.mask, sizeof(imask1.mask), IP1); + + result = iscsi_tgt_node_access(&conn, &tgtnode, iqn, addr); + CU_ASSERT(result == true); + + /* + * case 3: + * +-------------------------------------------+---------+ + * | IG1 | IG2 | | + * +-------------------------------------------+ | + * | name | addr | name | addr | result | + * +-------------------------------------------+---------+ + * +-------------------------------------------+---------+ + * | allowed | denied | denied | - | denied | + * +-------------------------------------------+---------+ + */ + snprintf(iname1.name, sizeof(iname1.name), IQN1); + snprintf(imask1.mask, sizeof(imask1.mask), IP2); + snprintf(iname2.name, sizeof(iname2.name), NO_IQN1); + + result = iscsi_tgt_node_access(&conn, &tgtnode, iqn, addr); + CU_ASSERT(result == false); + + /* + * case 4: + * +-------------------------------------------+---------+ + * | IG1 | IG2 | | + * +-------------------------------------------+ | + * | name | addr | name | addr | result | + * +-------------------------------------------+---------+ + * +-------------------------------------------+---------+ + * | allowed | denied | allowed | allowed | allowed | + * +-------------------------------------------+---------+ + */ + snprintf(iname1.name, sizeof(iname1.name), IQN1); + snprintf(imask1.mask, sizeof(imask1.mask), IP2); + snprintf(iname2.name, sizeof(iname2.name), IQN1); + snprintf(imask2.mask, sizeof(imask2.mask), IP1); + + result = iscsi_tgt_node_access(&conn, &tgtnode, iqn, addr); + CU_ASSERT(result == true); + + /* + * case 5: + * +---------------------------------------------+---------+ + * | IG1 | IG2 | | + * +---------------------------------------------+ | + * | name | addr | name | addr | result | + * +---------------------------------------------+---------+ + * +---------------------------------------------+---------+ + * | allowed | denied | allowed | denied | denied | + * +---------------------------------------------+---------+ + */ + snprintf(iname1.name, sizeof(iname1.name), IQN1); + snprintf(imask1.mask, sizeof(imask1.mask), IP2); + snprintf(iname2.name, sizeof(iname2.name), IQN1); + snprintf(imask2.mask, sizeof(imask2.mask), IP2); + + result = iscsi_tgt_node_access(&conn, &tgtnode, iqn, addr); + CU_ASSERT(result == false); + + /* + * case 6: + * +---------------------------------------------+---------+ + * | IG1 | IG2 | | + * +---------------------------------------------+ | + * | name | addr | name | addr | result | + * +---------------------------------------------+---------+ + * +---------------------------------------------+---------+ + * | allowed | denied | not found | - | denied | + * +---------------------------------------------+---------+ + */ + snprintf(iname1.name, sizeof(iname1.name), IQN1); + snprintf(imask1.mask, sizeof(imask1.mask), IP2); + snprintf(iname2.name, sizeof(iname2.name), IQN2); + + result = iscsi_tgt_node_access(&conn, &tgtnode, iqn, addr); + CU_ASSERT(result == false); + + /* + * case 7: + * +---------------------------------------------+---------+ + * | IG1 | IG2 | | + * +---------------------------------------------+ | + * | name | addr | name | addr | result | + * +---------------------------------------------+---------+ + * +---------------------------------------------+---------+ + * | not found | - | denied | - | denied | + * +---------------------------------------------+---------+ + */ + snprintf(iname1.name, sizeof(iname1.name), IQN2); + snprintf(iname2.name, sizeof(iname2.name), NO_IQN1); + + result = iscsi_tgt_node_access(&conn, &tgtnode, iqn, addr); + CU_ASSERT(result == false); + + /* + * case 8: + * +---------------------------------------------+---------+ + * | IG1 | IG2 | | + * +---------------------------------------------+ | + * | name | addr | name | addr | result | + * +---------------------------------------------+---------+ + * +---------------------------------------------+---------+ + * | not found | - | allowed | allowed | allowed | + * +---------------------------------------------+---------+ + */ + snprintf(iname1.name, sizeof(iname1.name), IQN2); + snprintf(iname2.name, sizeof(iname2.name), IQN1); + snprintf(imask2.mask, sizeof(imask2.mask), IP1); + + result = iscsi_tgt_node_access(&conn, &tgtnode, iqn, addr); + CU_ASSERT(result == true); + + /* + * case 9: + * +---------------------------------------------+---------+ + * | IG1 | IG2 | | + * +---------------------------------------------+ | + * | name | addr | name | addr | result | + * +---------------------------------------------+---------+ + * +---------------------------------------------+---------+ + * | not found | - | allowed | denied | denied | + * +---------------------------------------------+---------+ + */ + snprintf(iname1.name, sizeof(iname1.name), IQN2); + snprintf(iname2.name, sizeof(iname2.name), IQN1); + snprintf(imask2.mask, sizeof(imask2.mask), IP2); + + result = iscsi_tgt_node_access(&conn, &tgtnode, iqn, addr); + CU_ASSERT(result == false); + + /* + * case 10: + * +---------------------------------------------+---------+ + * | IG1 | IG2 | | + * +---------------------------------------------+ | + * | name | addr | name | addr | result | + * +---------------------------------------------+---------+ + * +---------------------------------------------+---------+ + * | not found | - | not found | - | denied | + * +---------------------------------------------+---------+ + */ + snprintf(iname1.name, sizeof(iname1.name), IQN2); + snprintf(iname2.name, sizeof(iname2.name), IQN2); + + result = iscsi_tgt_node_access(&conn, &tgtnode, iqn, addr); + CU_ASSERT(result == false); + + iscsi_pg_map_delete_ig_map(pg_map, &ig1); + iscsi_pg_map_delete_ig_map(pg_map, &ig2); + iscsi_tgt_node_delete_pg_map(&tgtnode, &pg); +} + +static void +allow_iscsi_name_multi_maps_case(void) +{ + struct spdk_iscsi_tgt_node tgtnode = {}; + struct spdk_iscsi_portal_grp pg1 = {}, pg2 = {}; + struct spdk_iscsi_init_grp ig = {}; + struct spdk_iscsi_initiator_name iname = {}; + struct spdk_iscsi_pg_map *pg_map1, *pg_map2; + struct spdk_scsi_dev scsi_dev = {}; + char *iqn; + bool result; + + /* target initialization */ + TAILQ_INIT(&tgtnode.pg_map_head); + + snprintf(scsi_dev.name, sizeof(scsi_dev.name), IQN1); + tgtnode.dev = &scsi_dev; + + /* initiator group initialization */ + TAILQ_INIT(&ig.initiator_head); + + ig.ninitiators = 1; + TAILQ_INSERT_TAIL(&ig.initiator_head, &iname, tailq); + + /* portal group initialization */ + pg1.tag = 1; + pg2.tag = 1; + + pg_map1 = iscsi_tgt_node_add_pg_map(&tgtnode, &pg1); + pg_map2 = iscsi_tgt_node_add_pg_map(&tgtnode, &pg2); + iscsi_pg_map_add_ig_map(pg_map1, &ig); + iscsi_pg_map_add_ig_map(pg_map2, &ig); + + /* test for IG1 <-> PG1, PG2 case */ + iqn = IQN1; + + snprintf(iname.name, sizeof(iname.name), IQN1); + + result = iscsi_tgt_node_allow_iscsi_name(&tgtnode, iqn); + CU_ASSERT(result == true); + + snprintf(iname.name, sizeof(iname.name), IQN2); + + result = iscsi_tgt_node_allow_iscsi_name(&tgtnode, iqn); + CU_ASSERT(result == false); + + iscsi_pg_map_delete_ig_map(pg_map1, &ig); + iscsi_pg_map_delete_ig_map(pg_map2, &ig); + iscsi_tgt_node_delete_pg_map(&tgtnode, &pg1); + iscsi_tgt_node_delete_pg_map(&tgtnode, &pg2); +} + +/* + * static bool + * iscsi_check_chap_params(bool disable_chap, bool require_chap, + * bool mutual_chap, int chap_group); + */ +static void +chap_param_test_cases(void) +{ + /* Auto */ + CU_ASSERT(iscsi_check_chap_params(false, false, false, 0) == true); + + /* None */ + CU_ASSERT(iscsi_check_chap_params(true, false, false, 0) == true); + + /* CHAP */ + CU_ASSERT(iscsi_check_chap_params(false, true, false, 0) == true); + + /* CHAP Mutual */ + CU_ASSERT(iscsi_check_chap_params(false, true, true, 0) == true); + + /* Check mutual exclusiveness of disabled and required */ + CU_ASSERT(iscsi_check_chap_params(true, true, false, 0) == false); + + /* Mutual requires Required */ + CU_ASSERT(iscsi_check_chap_params(false, false, true, 0) == false); + + /* Remaining combinations */ + CU_ASSERT(iscsi_check_chap_params(true, false, true, 0) == false); + CU_ASSERT(iscsi_check_chap_params(true, true, true, 0) == false); + + /* Valid auth group ID */ + CU_ASSERT(iscsi_check_chap_params(false, false, false, 1) == true); + + /* Invalid auth group ID */ + CU_ASSERT(iscsi_check_chap_params(false, false, false, -1) == false); +} + +int +main(int argc, char **argv) +{ + CU_pSuite suite = NULL; + unsigned int num_failures; + + if (argc < 2) { + fprintf(stderr, "usage: %s <config file>\n", argv[0]); + exit(1); + } + + CU_set_error_action(CUEA_ABORT); + CU_initialize_registry(); + + config_file = argv[1]; + + suite = CU_add_suite("iscsi_target_node_suite", NULL, NULL); + + CU_ADD_TEST(suite, add_lun_test_cases); + CU_ADD_TEST(suite, config_file_fail_cases); + CU_ADD_TEST(suite, allow_any_allowed); + CU_ADD_TEST(suite, allow_ipv6_allowed); + CU_ADD_TEST(suite, allow_ipv6_denied); + CU_ADD_TEST(suite, allow_ipv6_invalid); + CU_ADD_TEST(suite, allow_ipv4_allowed); + CU_ADD_TEST(suite, allow_ipv4_denied); + CU_ADD_TEST(suite, allow_ipv4_invalid); + CU_ADD_TEST(suite, node_access_allowed); + CU_ADD_TEST(suite, node_access_denied_by_empty_netmask); + CU_ADD_TEST(suite, node_access_multi_initiator_groups_cases); + CU_ADD_TEST(suite, allow_iscsi_name_multi_maps_case); + CU_ADD_TEST(suite, chap_param_test_cases); + + CU_basic_set_mode(CU_BRM_VERBOSE); + CU_basic_run_tests(); + num_failures = CU_get_number_of_failures(); + CU_cleanup_registry(); + return num_failures; +} |