From 92ef1a64ecab767bfd6c638f15b01f4d28c3e5d6 Mon Sep 17 00:00:00 2001 From: Shuhei Matsumoto Date: Wed, 15 Nov 2017 09:10:00 +0900 Subject: [PATCH] iscsi and ut/iscsi: return code to fail of IG creation spdk_iscsi_init_grp_create_from_configfile() returns 0 even if creating an initiator group (IG) is failed due to empty netmask or initiator name. This will be unexpected behavior for end users. Hence change the return code from 0 to -ENVAL for this failure. Besides do the following. Change other error return codes in the function by using errno too. Current IG don't have any UT code in repository. UT code to confirm this fix is also submitted. Change-Id: Ic918feb5c6a3e772b325664d881b2673b553a217 Signed-off-by: Shuhei Matsumoto Reviewed-on: https://review.gerrithub.io/384408 Tested-by: SPDK Automated Test System Reviewed-by: Daniel Verkamp --- lib/iscsi/init_grp.c | 10 +- test/unit/lib/iscsi/Makefile | 2 +- test/unit/lib/iscsi/init_grp.c/.gitignore | 1 + test/unit/lib/iscsi/init_grp.c/Makefile | 57 ++++++++ test/unit/lib/iscsi/init_grp.c/init_grp.conf | 31 +++++ test/unit/lib/iscsi/init_grp.c/init_grp_ut.c | 139 +++++++++++++++++++ unittest.sh | 1 + 7 files changed, 235 insertions(+), 6 deletions(-) create mode 100644 test/unit/lib/iscsi/init_grp.c/.gitignore create mode 100644 test/unit/lib/iscsi/init_grp.c/Makefile create mode 100644 test/unit/lib/iscsi/init_grp.c/init_grp.conf create mode 100644 test/unit/lib/iscsi/init_grp.c/init_grp_ut.c diff --git a/lib/iscsi/init_grp.c b/lib/iscsi/init_grp.c index 12f1e1c6a..b4ab61898 100644 --- a/lib/iscsi/init_grp.c +++ b/lib/iscsi/init_grp.c @@ -184,12 +184,12 @@ spdk_iscsi_init_grp_create_from_configfile(struct spdk_conf_section *sp) } if (i == 0) { SPDK_ERRLOG("num_initiator_names = 0\n"); - goto cleanup; + return -EINVAL; } num_initiator_names = i; if (num_initiator_names > MAX_INITIATOR) { SPDK_ERRLOG("%d > MAX_INITIATOR\n", num_initiator_names); - return -1; + return -E2BIG; } for (i = 0; ; i++) { val = spdk_conf_section_get_nval(sp, "Netmask", i); @@ -198,18 +198,18 @@ spdk_iscsi_init_grp_create_from_configfile(struct spdk_conf_section *sp) } if (i == 0) { SPDK_ERRLOG("num_initiator_mask = 0\n"); - goto cleanup; + return -EINVAL; } num_initiator_masks = i; if (num_initiator_masks > MAX_NETMASK) { SPDK_ERRLOG("%d > MAX_NETMASK\n", num_initiator_masks); - return -1; + return -E2BIG; } initiators = calloc(num_initiator_names, sizeof(char *)); if (!initiators) { perror("initiators"); - return -1; + return -ENOMEM; } for (i = 0; i < num_initiator_names; i++) { val = spdk_conf_section_get_nval(sp, "InitiatorName", i); diff --git a/test/unit/lib/iscsi/Makefile b/test/unit/lib/iscsi/Makefile index a31853898..90d500fc6 100644 --- a/test/unit/lib/iscsi/Makefile +++ b/test/unit/lib/iscsi/Makefile @@ -34,7 +34,7 @@ SPDK_ROOT_DIR := $(abspath $(CURDIR)/../../../..) include $(SPDK_ROOT_DIR)/mk/spdk.common.mk -DIRS-y = iscsi.c param.c tgt_node.c +DIRS-y = init_grp.c iscsi.c param.c tgt_node.c .PHONY: all clean $(DIRS-y) diff --git a/test/unit/lib/iscsi/init_grp.c/.gitignore b/test/unit/lib/iscsi/init_grp.c/.gitignore new file mode 100644 index 000000000..8fbc2b636 --- /dev/null +++ b/test/unit/lib/iscsi/init_grp.c/.gitignore @@ -0,0 +1 @@ +init_grp_ut diff --git a/test/unit/lib/iscsi/init_grp.c/Makefile b/test/unit/lib/iscsi/init_grp.c/Makefile new file mode 100644 index 000000000..def11a373 --- /dev/null +++ b/test/unit/lib/iscsi/init_grp.c/Makefile @@ -0,0 +1,57 @@ +# +# 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 +include $(SPDK_ROOT_DIR)/mk/spdk.app.mk + +SPDK_LIB_LIST = log conf util cunit + +CFLAGS += $(ENV_CFLAGS) +CFLAGS += -I$(SPDK_ROOT_DIR)/test +CFLAGS += -I$(SPDK_ROOT_DIR)/lib +LIBS += $(SPDK_LIB_LINKER_ARGS) +LIBS += -lcunit + +APP = init_grp_ut +C_SRCS = init_grp_ut.c + +all: $(APP) + +$(APP): $(OBJS) $(SPDK_LIB_FILES) + $(LINK_C) + +clean: + $(CLEAN_C) $(APP) + +include $(SPDK_ROOT_DIR)/mk/spdk.deps.mk diff --git a/test/unit/lib/iscsi/init_grp.c/init_grp.conf b/test/unit/lib/iscsi/init_grp.c/init_grp.conf new file mode 100644 index 000000000..aaa660def --- /dev/null +++ b/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/test/unit/lib/iscsi/init_grp.c/init_grp_ut.c b/test/unit/lib/iscsi/init_grp.c/init_grp_ut.c new file mode 100644 index 000000000..3fb20e876 --- /dev/null +++ b/test/unit/lib/iscsi/init_grp.c/init_grp_ut.c @@ -0,0 +1,139 @@ +/*- + * 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 "CUnit/Basic.h" + +#include "iscsi/init_grp.c" + +SPDK_LOG_REGISTER_TRACE_FLAG("iscsi", SPDK_TRACE_ISCSI) + +struct spdk_iscsi_globals g_spdk_iscsi; + +const char *config_file; + +static int +test_setup(void) +{ + TAILQ_INIT(&g_spdk_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 = spdk_iscsi_init_grp_create_from_configfile(sp); + CU_ASSERT(rc == 0); + + spdk_iscsi_init_grp_array_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 = spdk_iscsi_init_grp_create_from_configfile(sp); + CU_ASSERT(rc != 0); + + spdk_iscsi_init_grp_array_destroy(); + + section_index++; + } + + spdk_conf_free(config); +} + +int +main(int argc, char **argv) +{ + CU_pSuite suite = NULL; + unsigned int num_failures; + + if (argc < 2) { + fprintf(stderr, "usage: %s \n", argv[0]); + exit(1); + } + + if (CU_initialize_registry() != CUE_SUCCESS) { + return CU_get_error(); + } + + config_file = argv[1]; + + suite = CU_add_suite("init_grp_suite", test_setup, NULL); + if (suite == NULL) { + CU_cleanup_registry(); + return CU_get_error(); + } + + if ( + CU_add_test(suite, "create from config file cases", + create_from_config_file_cases) == NULL + ) { + CU_cleanup_registry(); + return CU_get_error(); + } + + 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/unittest.sh b/unittest.sh index 3bc24cfdb..1a1d48e10 100755 --- a/unittest.sh +++ b/unittest.sh @@ -96,6 +96,7 @@ $valgrind test/unit/lib/lvol/lvol.c/lvol_ut $valgrind test/unit/lib/iscsi/param.c/param_ut $valgrind test/unit/lib/iscsi/tgt_node.c/tgt_node_ut test/unit/lib/iscsi/tgt_node.c/tgt_node.conf $valgrind test/unit/lib/iscsi/iscsi.c/iscsi_ut +$valgrind test/unit/lib/iscsi/init_grp.c/init_grp_ut test/unit/lib/iscsi/init_grp.c/init_grp.conf $valgrind test/unit/lib/util/bit_array.c/bit_array_ut $valgrind test/unit/lib/util/crc32_ieee.c/crc32_ieee_ut