From d42fcd89ce24ff3f4f907c0c7915471e66ac7bf1 Mon Sep 17 00:00:00 2001 From: David Disseldorp Date: Thu, 12 Sep 2019 19:39:57 +0200 Subject: [PATCH 1/7] lib: use const for add_data buffers The buffer is memcopied into the PDU. const makes it a little clearer that the caller isn't handing over ownership. Signed-off-by: David Disseldorp --- include/iscsi-private.h | 4 ++-- lib/pdu.c | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/include/iscsi-private.h b/include/iscsi-private.h index dfea31c..dccd8c3 100644 --- a/include/iscsi-private.h +++ b/include/iscsi-private.h @@ -288,10 +288,10 @@ void iscsi_pdu_set_datasn(struct iscsi_pdu *pdu, uint32_t datasn); void iscsi_pdu_set_bufferoffset(struct iscsi_pdu *pdu, uint32_t bufferoffset); void iscsi_cancel_pdus(struct iscsi_context *iscsi); int iscsi_pdu_add_data(struct iscsi_context *iscsi, struct iscsi_pdu *pdu, - unsigned char *dptr, int dsize); + const unsigned char *dptr, int dsize); int iscsi_queue_pdu(struct iscsi_context *iscsi, struct iscsi_pdu *pdu); int iscsi_add_data(struct iscsi_context *iscsi, struct iscsi_data *data, - unsigned char *dptr, int dsize, int pdualignment); + const unsigned char *dptr, int dsize, int pdualignment); struct scsi_task; void iscsi_pdu_set_cdb(struct iscsi_pdu *pdu, struct scsi_task *task); diff --git a/lib/pdu.c b/lib/pdu.c index f9bdbab..8c8fc99 100644 --- a/lib/pdu.c +++ b/lib/pdu.c @@ -169,7 +169,7 @@ iscsi_tcp_free_pdu(struct iscsi_context *iscsi, struct iscsi_pdu *pdu) int iscsi_add_data(struct iscsi_context *iscsi, struct iscsi_data *data, - unsigned char *dptr, int dsize, int pdualignment) + const unsigned char *dptr, int dsize, int pdualignment) { size_t len, aligned; @@ -216,7 +216,7 @@ iscsi_add_data(struct iscsi_context *iscsi, struct iscsi_data *data, int iscsi_pdu_add_data(struct iscsi_context *iscsi, struct iscsi_pdu *pdu, - unsigned char *dptr, int dsize) + const unsigned char *dptr, int dsize) { if (pdu == NULL) { iscsi_set_error(iscsi, "trying to add data to NULL pdu"); From 955b16b3e3a9bbb1fee471c90af5f01c149442da Mon Sep 17 00:00:00 2001 From: David Disseldorp Date: Wed, 11 Sep 2019 16:02:04 +0200 Subject: [PATCH 2/7] test-tool: add iSCSI SendTargets.Simple test This test currently only sends a SendTargets request via existing normal iSCSI sessions. Signed-off-by: David Disseldorp --- test-tool/Makefile.am | 1 + test-tool/iscsi-test-cu.c | 8 +++ test-tool/iscsi-test-cu.h | 1 + test-tool/test_iscsi_sendtargets.c | 100 +++++++++++++++++++++++++++++ 4 files changed, 110 insertions(+) create mode 100644 test-tool/test_iscsi_sendtargets.c diff --git a/test-tool/Makefile.am b/test-tool/Makefile.am index 2d42173..45acf27 100644 --- a/test-tool/Makefile.am +++ b/test-tool/Makefile.am @@ -40,6 +40,7 @@ iscsi_test_cu_SOURCES = iscsi-test-cu.c \ test_iscsi_cmdsn_toohigh.c \ test_iscsi_cmdsn_toolow.c \ test_iscsi_datasn_invalid.c \ + test_iscsi_sendtargets.c \ test_mandatory_sbc.c \ test_modesense6_all_pages.c \ test_modesense6_control.c \ diff --git a/test-tool/iscsi-test-cu.c b/test-tool/iscsi-test-cu.c index 26e1b05..ea3e026 100644 --- a/test-tool/iscsi-test-cu.c +++ b/test-tool/iscsi-test-cu.c @@ -558,6 +558,11 @@ static CU_TestInfo tests_iscsi_datasn[] = { CU_TEST_INFO_NULL }; +static CU_TestInfo tests_iscsi_sendtargets[] = { + { (char *)"Simple", test_iscsi_sendtargets_simple }, + CU_TEST_INFO_NULL +}; + static CU_TestInfo tests_iscsi_residuals[] = { { (char *)"Read10Invalid", test_read10_invalid }, { (char *)"Read10Residuals", test_read10_residuals }, @@ -588,6 +593,8 @@ static libiscsi_suite_info iscsi_suites[] = { tests_iscsi_residuals }, { "iSCSITMF", NON_PGR_FUNCS, tests_iscsi_tmf }, + { "iSCSISendTargets", NON_PGR_FUNCS, + tests_iscsi_sendtargets }, { NULL, NULL, NULL, NULL, NULL, NULL } }; @@ -645,6 +652,7 @@ static libiscsi_suite_info all_suites[] = { { "iSCSIdatasn", NON_PGR_FUNCS, tests_iscsi_datasn }, { "iSCSIResiduals", NON_PGR_FUNCS, tests_iscsi_residuals }, { "iSCSITMF", NON_PGR_FUNCS, tests_iscsi_tmf }, + { "iSCSISendTargets", NON_PGR_FUNCS, tests_iscsi_sendtargets }, { "MultipathIO", NON_PGR_FUNCS, tests_multipathio }, { NULL, NULL, NULL, NULL, NULL, NULL }, }; diff --git a/test-tool/iscsi-test-cu.h b/test-tool/iscsi-test-cu.h index 086b949..be1753b 100644 --- a/test-tool/iscsi-test-cu.h +++ b/test-tool/iscsi-test-cu.h @@ -78,6 +78,7 @@ void test_iscsi_cmdsn_toohigh(void); void test_iscsi_cmdsn_toolow(void); void test_iscsi_datasn_invalid(void); +void test_iscsi_sendtargets_simple(void); void test_mandatory_sbc(void); diff --git a/test-tool/test_iscsi_sendtargets.c b/test-tool/test_iscsi_sendtargets.c new file mode 100644 index 0000000..8d21155 --- /dev/null +++ b/test-tool/test_iscsi_sendtargets.c @@ -0,0 +1,100 @@ +/* + Copyright (C) 2019 SUSE LLC + Copyright (C) 2013 by Ronnie Sahlberg + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, see . +*/ + +#include +#include +#include +#include + +#include "iscsi.h" +#include "iscsi-private.h" +#include "scsi-lowlevel.h" +#include "iscsi-test-cu.h" + +struct test_iscsi_sendtargets_state { + int dispatched; + int completed; +}; + +static int +test_iscsi_sendtargets_txrx(struct test_iscsi_sendtargets_state *state) +{ + while (state->completed < state->dispatched) { + struct pollfd pfd; + int ret; + + pfd.fd = iscsi_get_fd(sd->iscsi_ctx); + pfd.events = iscsi_which_events(sd->iscsi_ctx); + + ret = poll(&pfd, 1, -1); + if (ret < 0) { + return ret; + } + + ret = iscsi_service(sd->iscsi_ctx, pfd.revents); + if (ret != 0) { + return ret; + } + } + return 0; +} + +static void +test_iscsi_sendtargets_simple_cb(struct iscsi_context *iscsi _U_, int status, + void *command_data, void *private_data) +{ + struct test_iscsi_sendtargets_state *state = private_data; + struct iscsi_discovery_address *da; + + state->completed++; + CU_ASSERT_EQUAL(status, 0); + for (da = command_data; da != NULL; da = da->next) { + struct iscsi_target_portal *po; + logging(LOG_VERBOSE, "Target: %s", da->target_name); + for (po = da->portals; po != NULL; po = po->next) { + logging(LOG_VERBOSE, "+ Portal: %s", po->portal); + } + } +} + +void +test_iscsi_sendtargets_simple(void) +{ + struct test_iscsi_sendtargets_state state; + int ret; + + logging(LOG_VERBOSE, LOG_BLANK_LINE); + logging(LOG_VERBOSE, "Test SendTargets in FFP"); + + if (sd->iscsi_ctx == NULL) { + const char *err = "[SKIPPED] This test is " + "only supported for iSCSI backends"; + logging(LOG_NORMAL, "%s", err); + CU_PASS(err); + return; + } + + memset(&state, 0, sizeof(state)); + ret = iscsi_discovery_async(sd->iscsi_ctx, + test_iscsi_sendtargets_simple_cb, &state); + CU_ASSERT_EQUAL(ret, 0); + state.dispatched++; + + ret = test_iscsi_sendtargets_txrx(&state); + CU_ASSERT_EQUAL(ret, 0); +} From 98d383d15940e93c72d4bcece52623255095975e Mon Sep 17 00:00:00 2001 From: David Disseldorp Date: Tue, 17 Sep 2019 16:07:52 +0200 Subject: [PATCH 3/7] test-tool: add iSCSI SendTargets.Invalid test case This test sends a series of Text request PDUs with key-value data that doesn't match SendTargets=All. Signed-off-by: David Disseldorp --- test-tool/iscsi-test-cu.c | 1 + test-tool/iscsi-test-cu.h | 1 + test-tool/test_iscsi_sendtargets.c | 143 ++++++++++++++++++++++++++++- 3 files changed, 141 insertions(+), 4 deletions(-) diff --git a/test-tool/iscsi-test-cu.c b/test-tool/iscsi-test-cu.c index ea3e026..865e668 100644 --- a/test-tool/iscsi-test-cu.c +++ b/test-tool/iscsi-test-cu.c @@ -560,6 +560,7 @@ static CU_TestInfo tests_iscsi_datasn[] = { static CU_TestInfo tests_iscsi_sendtargets[] = { { (char *)"Simple", test_iscsi_sendtargets_simple }, + { (char *)"Invalid", test_iscsi_sendtargets_invalid }, CU_TEST_INFO_NULL }; diff --git a/test-tool/iscsi-test-cu.h b/test-tool/iscsi-test-cu.h index be1753b..b19a12f 100644 --- a/test-tool/iscsi-test-cu.h +++ b/test-tool/iscsi-test-cu.h @@ -79,6 +79,7 @@ void test_iscsi_cmdsn_toolow(void); void test_iscsi_datasn_invalid(void); void test_iscsi_sendtargets_simple(void); +void test_iscsi_sendtargets_invalid(void); void test_mandatory_sbc(void); diff --git a/test-tool/test_iscsi_sendtargets.c b/test-tool/test_iscsi_sendtargets.c index 8d21155..3607af8 100644 --- a/test-tool/test_iscsi_sendtargets.c +++ b/test-tool/test_iscsi_sendtargets.c @@ -17,6 +17,7 @@ */ #include +#include #include #include #include @@ -29,6 +30,9 @@ struct test_iscsi_sendtargets_state { int dispatched; int completed; + int failed; + int succeeded; + bool expect_failure; }; static int @@ -55,14 +59,35 @@ test_iscsi_sendtargets_txrx(struct test_iscsi_sendtargets_state *state) } static void -test_iscsi_sendtargets_simple_cb(struct iscsi_context *iscsi _U_, int status, - void *command_data, void *private_data) +test_iscsi_sendtargets_cb(struct iscsi_context *iscsi _U_, int status, + void *command_data _U_, void *private_data) { struct test_iscsi_sendtargets_state *state = private_data; - struct iscsi_discovery_address *da; state->completed++; - CU_ASSERT_EQUAL(status, 0); + if (state->expect_failure) { + CU_ASSERT_NOT_EQUAL(status, 0); + } else { + CU_ASSERT_EQUAL(status, 0); + } + + if (status != 0) { + logging(LOG_VERBOSE, "non-zero Text response %d", + state->completed); + state->failed++; + return; + } + state->succeeded++; + logging(LOG_VERBOSE, "zero Text response %d", state->completed); +} + +static void +test_iscsi_sendtargets_simple_cb(struct iscsi_context *iscsi, int status, + void *command_data, void *private_data) +{ + struct iscsi_discovery_address *da; + + test_iscsi_sendtargets_cb(iscsi, status, command_data, private_data); for (da = command_data; da != NULL; da = da->next) { struct iscsi_target_portal *po; logging(LOG_VERBOSE, "Target: %s", da->target_name); @@ -95,6 +120,116 @@ test_iscsi_sendtargets_simple(void) CU_ASSERT_EQUAL(ret, 0); state.dispatched++; + state.expect_failure = false; ret = test_iscsi_sendtargets_txrx(&state); CU_ASSERT_EQUAL(ret, 0); + CU_ASSERT_EQUAL(state.failed, 0); + CU_ASSERT_EQUAL(state.succeeded, 1); +} + +int +test_iscsi_text_req_queue(struct iscsi_context *iscsi, + const char *kv_data, + iscsi_command_cb cb, + struct test_iscsi_sendtargets_state *state) +{ + struct iscsi_pdu *pdu; + int ret; + + pdu = iscsi_allocate_pdu(iscsi, ISCSI_PDU_TEXT_REQUEST, + ISCSI_PDU_TEXT_RESPONSE, + iscsi_itt_post_increment(iscsi), + ISCSI_PDU_DROP_ON_RECONNECT); + CU_ASSERT_PTR_NOT_NULL_FATAL(pdu); + + iscsi_pdu_set_immediate(pdu); + iscsi_pdu_set_cmdsn(pdu, iscsi->cmdsn); + iscsi_pdu_set_pduflags(pdu, ISCSI_PDU_TEXT_FINAL); + iscsi_pdu_set_ttt(pdu, 0xffffffff); + + ret = iscsi_pdu_add_data(iscsi, pdu, (unsigned char *)kv_data, + strlen(kv_data) + 1); + CU_ASSERT_EQUAL_FATAL(ret, 0); + + pdu->callback = cb; + pdu->private_data = state; + + ret = iscsi_queue_pdu(iscsi, pdu); + CU_ASSERT_EQUAL_FATAL(ret, 0); + state->dispatched++; + logging(LOG_VERBOSE, "queued Text request %d with %s", + state->dispatched, kv_data); + + return 0; +} + +void +test_iscsi_sendtargets_invalid(void) +{ + struct test_iscsi_sendtargets_state state; + int ret; + + logging(LOG_VERBOSE, LOG_BLANK_LINE); + logging(LOG_VERBOSE, "Test invalid SendTargets Text requests"); + + if (sd->iscsi_ctx == NULL) { + const char *err = "[SKIPPED] This test is " + "only supported for iSCSI backends"; + logging(LOG_NORMAL, "%s", err); + CU_PASS(err); + return; + } + + memset(&state, 0, sizeof(state)); + ret = test_iscsi_text_req_queue(sd->iscsi_ctx, + "SendTargetsPlease=All", /* bad key */ + test_iscsi_sendtargets_cb, + &state); + CU_ASSERT_EQUAL(ret, 0); + + state.expect_failure = true; + ret = test_iscsi_sendtargets_txrx(&state); + CU_ASSERT_EQUAL(ret, 0); + + ret = test_iscsi_text_req_queue(sd->iscsi_ctx, + "SendTargets=Alle", /* bad val */ + test_iscsi_sendtargets_cb, + &state); + CU_ASSERT_EQUAL(ret, 0); + + state.expect_failure = true; + ret = test_iscsi_sendtargets_txrx(&state); + CU_ASSERT_EQUAL(ret, 0); + + ret = test_iscsi_text_req_queue(sd->iscsi_ctx, + "SendTargets=A", /* bad val */ + test_iscsi_sendtargets_cb, + &state); + CU_ASSERT_EQUAL(ret, 0); + + state.expect_failure = true; + ret = test_iscsi_sendtargets_txrx(&state); + CU_ASSERT_EQUAL(ret, 0); + + ret = test_iscsi_text_req_queue(sd->iscsi_ctx, + "sENDtARGETS=aLL", /* bad case */ + test_iscsi_sendtargets_cb, + &state); + CU_ASSERT_EQUAL(ret, 0); + + state.expect_failure = true; + ret = test_iscsi_sendtargets_txrx(&state); + CU_ASSERT_EQUAL(ret, 0); + + ret = test_iscsi_text_req_queue(sd->iscsi_ctx, + "SendTargets=All", /* valid */ + test_iscsi_sendtargets_cb, + &state); + CU_ASSERT_EQUAL(ret, 0); + + state.expect_failure = false; + ret = test_iscsi_sendtargets_txrx(&state); + CU_ASSERT_EQUAL(ret, 0); + CU_ASSERT_EQUAL(state.failed, 4); + CU_ASSERT_EQUAL(state.succeeded, 1); } From 1511ddcbe4b5b00dcbc98009d6227000c81e6dc9 Mon Sep 17 00:00:00 2001 From: David Disseldorp Date: Mon, 16 Sep 2019 11:19:12 +0200 Subject: [PATCH 4/7] test-tool: add simple iSCSI NOP-Out test Send a ping with/without data and expect a valid response. Signed-off-by: David Disseldorp --- test-tool/Makefile.am | 1 + test-tool/iscsi-test-cu.c | 8 +++ test-tool/iscsi-test-cu.h | 1 + test-tool/test_iscsi_nop_simple.c | 115 ++++++++++++++++++++++++++++++ 4 files changed, 125 insertions(+) create mode 100644 test-tool/test_iscsi_nop_simple.c diff --git a/test-tool/Makefile.am b/test-tool/Makefile.am index 45acf27..f9faa90 100644 --- a/test-tool/Makefile.am +++ b/test-tool/Makefile.am @@ -41,6 +41,7 @@ iscsi_test_cu_SOURCES = iscsi-test-cu.c \ test_iscsi_cmdsn_toolow.c \ test_iscsi_datasn_invalid.c \ test_iscsi_sendtargets.c \ + test_iscsi_nop_simple.c \ test_mandatory_sbc.c \ test_modesense6_all_pages.c \ test_modesense6_control.c \ diff --git a/test-tool/iscsi-test-cu.c b/test-tool/iscsi-test-cu.c index 865e668..670a705 100644 --- a/test-tool/iscsi-test-cu.c +++ b/test-tool/iscsi-test-cu.c @@ -564,6 +564,11 @@ static CU_TestInfo tests_iscsi_sendtargets[] = { CU_TEST_INFO_NULL }; +static CU_TestInfo tests_iscsi_nop[] = { + { (char *)"Simple", test_iscsi_nop_simple }, + CU_TEST_INFO_NULL +}; + static CU_TestInfo tests_iscsi_residuals[] = { { (char *)"Read10Invalid", test_read10_invalid }, { (char *)"Read10Residuals", test_read10_residuals }, @@ -596,6 +601,8 @@ static libiscsi_suite_info iscsi_suites[] = { tests_iscsi_tmf }, { "iSCSISendTargets", NON_PGR_FUNCS, tests_iscsi_sendtargets }, + { "iSCSINop", NON_PGR_FUNCS, + tests_iscsi_nop }, { NULL, NULL, NULL, NULL, NULL, NULL } }; @@ -654,6 +661,7 @@ static libiscsi_suite_info all_suites[] = { { "iSCSIResiduals", NON_PGR_FUNCS, tests_iscsi_residuals }, { "iSCSITMF", NON_PGR_FUNCS, tests_iscsi_tmf }, { "iSCSISendTargets", NON_PGR_FUNCS, tests_iscsi_sendtargets }, + { "iSCSINop", NON_PGR_FUNCS, tests_iscsi_nop }, { "MultipathIO", NON_PGR_FUNCS, tests_multipathio }, { NULL, NULL, NULL, NULL, NULL, NULL }, }; diff --git a/test-tool/iscsi-test-cu.h b/test-tool/iscsi-test-cu.h index b19a12f..b05bbe2 100644 --- a/test-tool/iscsi-test-cu.h +++ b/test-tool/iscsi-test-cu.h @@ -80,6 +80,7 @@ void test_iscsi_cmdsn_toolow(void); void test_iscsi_datasn_invalid(void); void test_iscsi_sendtargets_simple(void); void test_iscsi_sendtargets_invalid(void); +void test_iscsi_nop_simple(void); void test_mandatory_sbc(void); diff --git a/test-tool/test_iscsi_nop_simple.c b/test-tool/test_iscsi_nop_simple.c new file mode 100644 index 0000000..3a5c9bc --- /dev/null +++ b/test-tool/test_iscsi_nop_simple.c @@ -0,0 +1,115 @@ +/* + Copyright (C) 2019 SUSE LLC + Copyright (C) 2013 by Ronnie Sahlberg + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, see . +*/ + +#include +#include +#include +#include + +#include "iscsi.h" +#include "iscsi-private.h" +#include "scsi-lowlevel.h" +#include "iscsi-test-cu.h" + +struct test_iscsi_nop_state { + int dispatched; + int completed; + char nop_buf[1024]; + size_t nop_datalen; +}; + +static int +test_iscsi_nop_txrx(struct test_iscsi_nop_state *state) +{ + while (state->completed < state->dispatched) { + struct pollfd pfd; + int ret; + + pfd.fd = iscsi_get_fd(sd->iscsi_ctx); + pfd.events = iscsi_which_events(sd->iscsi_ctx); + + ret = poll(&pfd, 1, -1); + if (ret < 0) { + return ret; + } + + ret = iscsi_service(sd->iscsi_ctx, pfd.revents); + if (ret != 0) { + return ret; + } + } + return 0; +} + +static void +test_iscsi_nop_cb(struct iscsi_context *iscsi _U_, int status, + void *command_data, void *private_data) +{ + struct test_iscsi_nop_state *state = private_data; + struct iscsi_data *data = command_data; + + CU_ASSERT_EQUAL(status, 0); + CU_ASSERT_PTR_NOT_NULL(data); + CU_ASSERT_EQUAL_FATAL(state->nop_datalen, data->size); + if (state->nop_datalen > 0) { + CU_ASSERT_EQUAL(0, memcmp(state->nop_buf, data->data, + state->nop_datalen)); + } + + state->completed++; +} + +void +test_iscsi_nop_simple(void) +{ + struct test_iscsi_nop_state state; + int ret; + + logging(LOG_VERBOSE, LOG_BLANK_LINE); + logging(LOG_VERBOSE, "Test Nop Out Pings"); + + if (sd->iscsi_ctx == NULL) { + const char *err = "[SKIPPED] This test is " + "only supported for iSCSI backends"; + logging(LOG_NORMAL, "%s", err); + CU_PASS(err); + return; + } + + memset(&state, 0, sizeof(state)); + strncpy(state.nop_buf, "nopping", sizeof(state.nop_buf)); + state.nop_datalen = strlen(state.nop_buf) + 1; + ret = iscsi_nop_out_async(sd->iscsi_ctx, test_iscsi_nop_cb, + (unsigned char *)state.nop_buf, + state.nop_datalen, &state); + CU_ASSERT_EQUAL(ret, 0); + state.dispatched++; + + ret = test_iscsi_nop_txrx(&state); + CU_ASSERT_EQUAL(ret, 0); + + /* no data */ + state.nop_datalen = 0; + ret = iscsi_nop_out_async(sd->iscsi_ctx, test_iscsi_nop_cb, + NULL, state.nop_datalen, &state); + CU_ASSERT_EQUAL(ret, 0); + state.dispatched++; + + ret = test_iscsi_nop_txrx(&state); + CU_ASSERT_EQUAL(ret, 0); +} From 2600e538d8886ac1b65a3733dd5de597d3b9e2fe Mon Sep 17 00:00:00 2001 From: David Disseldorp Date: Wed, 18 Sep 2019 16:17:20 +0200 Subject: [PATCH 5/7] test-tool: add CHECK_FOR_ISCSI() test filter macro Skip the test if the provided iscsi_ctx is null. Signed-off-by: David Disseldorp --- test-tool/iscsi-support.h | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/test-tool/iscsi-support.h b/test-tool/iscsi-support.h index 2b20a1f..7aa9e45 100644 --- a/test-tool/iscsi-support.h +++ b/test-tool/iscsi-support.h @@ -186,6 +186,16 @@ do { \ } \ } while (0); +#define CHECK_FOR_ISCSI(_sd) \ +do { \ + if (_sd->iscsi_ctx == NULL) { \ + logging(LOG_NORMAL, "[SKIPPED] Not an iSCSI device." \ + " Skipping test"); \ + CU_PASS("[SKIPPED] Not an iSCSI device. Skipping test"); \ + return; \ + } \ +} while (0); + #define CHECK_SIZE(_min_blocks, _c) \ do { \ if (num_blocks < _min_blocks) { \ From e716691a4573888c65e3b768440a93510cc226d6 Mon Sep 17 00:00:00 2001 From: David Disseldorp Date: Wed, 18 Sep 2019 16:19:29 +0200 Subject: [PATCH 6/7] test-tool: use CHECK_FOR_ISCSI() helper macro This removes a large amount of boilerplate code. Signed-off-by: David Disseldorp --- test-tool/test_async_abort_simple.c | 5 +- test-tool/test_async_lu_reset_simple.c | 5 +- test-tool/test_async_read.c | 16 +++---- test-tool/test_async_write.c | 5 +- ...est_compareandwrite_invalid_dataout_size.c | 8 +--- test-tool/test_iscsi_cmdsn_toohigh.c | 8 +--- test-tool/test_iscsi_cmdsn_toolow.c | 8 +--- test-tool/test_iscsi_datasn_invalid.c | 9 +--- test-tool/test_iscsi_nop_simple.c | 8 +--- test-tool/test_iscsi_sendtargets.c | 16 +------ test-tool/test_preventallow_2_itnexuses.c | 9 +--- test-tool/test_preventallow_cold_reset.c | 9 +--- test-tool/test_preventallow_itnexus_loss.c | 9 +--- test-tool/test_preventallow_logout.c | 9 +--- test-tool/test_preventallow_lun_reset.c | 9 +--- test-tool/test_preventallow_warm_reset.c | 9 +--- test-tool/test_prout_preempt.c | 9 +--- test-tool/test_prout_reserve_access.c | 48 +++---------------- test-tool/test_prout_reserve_ownership.c | 48 +++---------------- test-tool/test_read10_invalid.c | 8 +--- test-tool/test_read10_residuals.c | 8 +--- test-tool/test_read12_residuals.c | 8 +--- test-tool/test_read16_residuals.c | 8 +--- test-tool/test_reserve6_2initiators.c | 8 +--- test-tool/test_reserve6_itnexus_loss.c | 8 +--- test-tool/test_reserve6_logout.c | 8 +--- test-tool/test_reserve6_lun_reset.c | 8 +--- test-tool/test_reserve6_target_cold_reset.c | 8 +--- test-tool/test_reserve6_target_warm_reset.c | 8 +--- .../test_sanitize_block_erase_reserved.c | 8 +--- .../test_sanitize_crypto_erase_reserved.c | 8 +--- test-tool/test_sanitize_overwrite_reserved.c | 8 +--- test-tool/test_sanitize_readonly.c | 9 +--- test-tool/test_sanitize_reservations.c | 9 +--- test-tool/test_sanitize_reset.c | 9 +--- test-tool/test_write10_residuals.c | 9 +--- test-tool/test_write12_residuals.c | 9 +--- test-tool/test_write16_residuals.c | 9 +--- test-tool/test_writeverify10_residuals.c | 9 +--- test-tool/test_writeverify12_residuals.c | 9 +--- test-tool/test_writeverify16_residuals.c | 17 +------ 41 files changed, 58 insertions(+), 382 deletions(-) diff --git a/test-tool/test_async_abort_simple.c b/test-tool/test_async_abort_simple.c index 3f3ff58..5add7ce 100644 --- a/test-tool/test_async_abort_simple.c +++ b/test-tool/test_async_abort_simple.c @@ -99,10 +99,7 @@ test_async_abort_simple(void) CHECK_FOR_DATALOSS; CHECK_FOR_SBC; - if (sd->iscsi_ctx == NULL) { - CU_PASS("[SKIPPED] Non-iSCSI"); - return; - } + CHECK_FOR_ISCSI(sd); if (maximum_transfer_length && (maximum_transfer_length < (int)(blocks_per_io))) { diff --git a/test-tool/test_async_lu_reset_simple.c b/test-tool/test_async_lu_reset_simple.c index 0a9b1cb..1b7943f 100644 --- a/test-tool/test_async_lu_reset_simple.c +++ b/test-tool/test_async_lu_reset_simple.c @@ -98,10 +98,7 @@ test_async_lu_reset_simple(void) CHECK_FOR_DATALOSS; CHECK_FOR_SBC; - if (sd->iscsi_ctx == NULL) { - CU_PASS("[SKIPPED] Non-iSCSI"); - return; - } + CHECK_FOR_ISCSI(sd); if (maximum_transfer_length && (maximum_transfer_length < (int)(blocks_per_io))) { diff --git a/test-tool/test_async_read.c b/test-tool/test_async_read.c index d2d7b72..447c8bd 100644 --- a/test-tool/test_async_read.c +++ b/test-tool/test_async_read.c @@ -62,19 +62,17 @@ test_async_read(void) struct tests_async_read_state state = { 0, 0, 0 }; const int blocks_per_io = 8; const int num_ios = 1000; - /* IOs in flight concurrently, so need a buffer large enough for all */ - unsigned char *buf = calloc(block_size * blocks_per_io, num_ios); - - CU_ASSERT_NOT_EQUAL(buf, NULL); - if (!buf) - goto out; + unsigned char *buf = NULL; CHECK_FOR_DATALOSS; CHECK_FOR_SBC; - if (sd->iscsi_ctx == NULL) { - CU_PASS("[SKIPPED] Non-iSCSI"); + CHECK_FOR_ISCSI(sd); + + /* IOs in flight concurrently, so need a buffer large enough for all */ + buf = calloc(block_size * blocks_per_io, num_ios); + CU_ASSERT_NOT_EQUAL(buf, NULL); + if (!buf) goto out; - } if (maximum_transfer_length && (maximum_transfer_length < (blocks_per_io * num_ios))) { diff --git a/test-tool/test_async_write.c b/test-tool/test_async_write.c index f68dbb5..abf4289 100644 --- a/test-tool/test_async_write.c +++ b/test-tool/test_async_write.c @@ -66,10 +66,7 @@ test_async_write(void) CHECK_FOR_DATALOSS; CHECK_FOR_SBC; - if (sd->iscsi_ctx == NULL) { - CU_PASS("[SKIPPED] Non-iSCSI"); - return; - } + CHECK_FOR_ISCSI(sd); if (maximum_transfer_length && (maximum_transfer_length < (blocks_per_io * num_ios))) { diff --git a/test-tool/test_compareandwrite_invalid_dataout_size.c b/test-tool/test_compareandwrite_invalid_dataout_size.c index 9fcd739..fbb32a5 100644 --- a/test-tool/test_compareandwrite_invalid_dataout_size.c +++ b/test-tool/test_compareandwrite_invalid_dataout_size.c @@ -55,13 +55,7 @@ test_compareandwrite_invalid_dataout_size(void) CHECK_FOR_THIN_PROVISIONING; CHECK_FOR_LBPPB_GT_1; CHECK_FOR_SBC; - if (sd->iscsi_ctx == NULL) { - const char *err = "[SKIPPED] This test is " - "only supported for iSCSI backends"; - logging(LOG_NORMAL, "%s", err); - CU_PASS(err); - return; - } + CHECK_FOR_ISCSI(sd); /* override transport queue_pdu callback for PDU manipulation */ iscsi_drv_orig = *sd->iscsi_ctx->drv; diff --git a/test-tool/test_iscsi_cmdsn_toohigh.c b/test-tool/test_iscsi_cmdsn_toohigh.c index 0bffd52..175f015 100644 --- a/test-tool/test_iscsi_cmdsn_toohigh.c +++ b/test-tool/test_iscsi_cmdsn_toohigh.c @@ -55,13 +55,7 @@ void test_iscsi_cmdsn_toohigh(void) logging(LOG_VERBOSE, "Test sending invalid iSCSI CMDSN"); logging(LOG_VERBOSE, "CMDSN MUST be in the range EXPCMDSN and MAXCMDSN"); - if (sd->iscsi_ctx == NULL) { - const char *err = "[SKIPPED] This test is " - "only supported for iSCSI backends"; - logging(LOG_NORMAL, "%s", err); - CU_PASS(err); - return; - } + CHECK_FOR_ISCSI(sd); logging(LOG_VERBOSE, "RFC3720:3.2.2.1 CMDSN > MAXCMDSN must be silently ignored by the target"); logging(LOG_VERBOSE, "Send a TESTUNITREADY with CMDSN == MAXCMDSN+1. Should be ignored by the target."); diff --git a/test-tool/test_iscsi_cmdsn_toolow.c b/test-tool/test_iscsi_cmdsn_toolow.c index d16b161..803e429 100644 --- a/test-tool/test_iscsi_cmdsn_toolow.c +++ b/test-tool/test_iscsi_cmdsn_toolow.c @@ -55,13 +55,7 @@ void test_iscsi_cmdsn_toolow(void) logging(LOG_VERBOSE, "Test sending invalid iSCSI CMDSN"); logging(LOG_VERBOSE, "CMDSN MUST be in the range EXPCMDSN and MAXCMDSN"); - if (sd->iscsi_ctx == NULL) { - const char *err = "[SKIPPED] This test is " - "only supported for iSCSI backends"; - logging(LOG_NORMAL, "%s", err); - CU_PASS(err); - return; - } + CHECK_FOR_ISCSI(sd); logging(LOG_VERBOSE, "RFC3720:3.2.2.1 CMDSN < EXPCMDSN must be silently ignored by the target"); logging(LOG_VERBOSE, "Send a TESTUNITREADY with CMDSN == EXPCMDSN-1. Should be ignored by the target."); diff --git a/test-tool/test_iscsi_datasn_invalid.c b/test-tool/test_iscsi_datasn_invalid.c index 2698885..3a04b3c 100644 --- a/test-tool/test_iscsi_datasn_invalid.c +++ b/test-tool/test_iscsi_datasn_invalid.c @@ -62,18 +62,11 @@ void test_iscsi_datasn_invalid(void) int ret; CHECK_FOR_DATALOSS; + CHECK_FOR_ISCSI(sd); logging(LOG_VERBOSE, LOG_BLANK_LINE); logging(LOG_VERBOSE, "Test sending invalid iSCSI DataSN"); - if (sd->iscsi_ctx == NULL) { - const char *err = "[SKIPPED] This test is " - "only supported for iSCSI backends"; - logging(LOG_NORMAL, "%s", err); - CU_PASS(err); - return; - } - logging(LOG_VERBOSE, "Send two Data-Out PDU's with DataSN==0. Should fail."); change_datasn = 1; diff --git a/test-tool/test_iscsi_nop_simple.c b/test-tool/test_iscsi_nop_simple.c index 3a5c9bc..b45ab30 100644 --- a/test-tool/test_iscsi_nop_simple.c +++ b/test-tool/test_iscsi_nop_simple.c @@ -83,13 +83,7 @@ test_iscsi_nop_simple(void) logging(LOG_VERBOSE, LOG_BLANK_LINE); logging(LOG_VERBOSE, "Test Nop Out Pings"); - if (sd->iscsi_ctx == NULL) { - const char *err = "[SKIPPED] This test is " - "only supported for iSCSI backends"; - logging(LOG_NORMAL, "%s", err); - CU_PASS(err); - return; - } + CHECK_FOR_ISCSI(sd); memset(&state, 0, sizeof(state)); strncpy(state.nop_buf, "nopping", sizeof(state.nop_buf)); diff --git a/test-tool/test_iscsi_sendtargets.c b/test-tool/test_iscsi_sendtargets.c index 3607af8..1fe4d37 100644 --- a/test-tool/test_iscsi_sendtargets.c +++ b/test-tool/test_iscsi_sendtargets.c @@ -106,13 +106,7 @@ test_iscsi_sendtargets_simple(void) logging(LOG_VERBOSE, LOG_BLANK_LINE); logging(LOG_VERBOSE, "Test SendTargets in FFP"); - if (sd->iscsi_ctx == NULL) { - const char *err = "[SKIPPED] This test is " - "only supported for iSCSI backends"; - logging(LOG_NORMAL, "%s", err); - CU_PASS(err); - return; - } + CHECK_FOR_ISCSI(sd); memset(&state, 0, sizeof(state)); ret = iscsi_discovery_async(sd->iscsi_ctx, @@ -172,13 +166,7 @@ test_iscsi_sendtargets_invalid(void) logging(LOG_VERBOSE, LOG_BLANK_LINE); logging(LOG_VERBOSE, "Test invalid SendTargets Text requests"); - if (sd->iscsi_ctx == NULL) { - const char *err = "[SKIPPED] This test is " - "only supported for iSCSI backends"; - logging(LOG_NORMAL, "%s", err); - CU_PASS(err); - return; - } + CHECK_FOR_ISCSI(sd); memset(&state, 0, sizeof(state)); ret = test_iscsi_text_req_queue(sd->iscsi_ctx, diff --git a/test-tool/test_preventallow_2_itnexuses.c b/test-tool/test_preventallow_2_itnexuses.c index 98d8f4d..cc24631 100644 --- a/test-tool/test_preventallow_2_itnexuses.c +++ b/test-tool/test_preventallow_2_itnexuses.c @@ -33,18 +33,11 @@ test_preventallow_2_itnexuses(void) CHECK_FOR_SBC; CHECK_FOR_REMOVABLE; + CHECK_FOR_ISCSI(sd); logging(LOG_VERBOSE, LOG_BLANK_LINE); logging(LOG_VERBOSE, "Test that PREVENT MEDIUM REMOVAL are seen on other nexuses as well"); - if (sd->iscsi_ctx == NULL) { - const char *err = "[SKIPPED] This PREVENTALLOW test is " - "only supported for iSCSI backends"; - logging(LOG_NORMAL, "%s", err); - CU_PASS(err); - return; - } - logging(LOG_VERBOSE, "Set the PREVENT flag"); PREVENTALLOW(sd, 1); diff --git a/test-tool/test_preventallow_cold_reset.c b/test-tool/test_preventallow_cold_reset.c index f196219..18df00d 100644 --- a/test-tool/test_preventallow_cold_reset.c +++ b/test-tool/test_preventallow_cold_reset.c @@ -31,18 +31,11 @@ test_preventallow_cold_reset(void) CHECK_FOR_SBC; CHECK_FOR_REMOVABLE; + CHECK_FOR_ISCSI(sd); logging(LOG_VERBOSE, LOG_BLANK_LINE); logging(LOG_VERBOSE, "Test that Target Warm Reset clears PREVENT MEDIUM REMOVAL"); - if (sd->iscsi_ctx == NULL) { - const char *err = "[SKIPPED] This PREVENTALLOW test is " - "only supported for iSCSI backends"; - logging(LOG_NORMAL, "%s", err); - CU_PASS(err); - return; - } - logging(LOG_VERBOSE, "Set the PREVENT flag"); PREVENTALLOW(sd, 1); diff --git a/test-tool/test_preventallow_itnexus_loss.c b/test-tool/test_preventallow_itnexus_loss.c index e28297b..3d2aa5a 100644 --- a/test-tool/test_preventallow_itnexus_loss.c +++ b/test-tool/test_preventallow_itnexus_loss.c @@ -29,18 +29,11 @@ test_preventallow_itnexus_loss(void) { CHECK_FOR_SBC; CHECK_FOR_REMOVABLE; + CHECK_FOR_ISCSI(sd); logging(LOG_VERBOSE, LOG_BLANK_LINE); logging(LOG_VERBOSE, "Test that IT-Nexus loss clears PREVENT MEDIUM REMOVAL"); - if (sd->iscsi_ctx == NULL) { - const char *err = "[SKIPPED] This PREVENTALLOW test is " - "only supported for iSCSI backends"; - logging(LOG_NORMAL, "%s", err); - CU_PASS(err); - return; - } - logging(LOG_VERBOSE, "Set the PREVENT flag"); PREVENTALLOW(sd, 1); diff --git a/test-tool/test_preventallow_logout.c b/test-tool/test_preventallow_logout.c index a70b7ea..db48b7c 100644 --- a/test-tool/test_preventallow_logout.c +++ b/test-tool/test_preventallow_logout.c @@ -29,18 +29,11 @@ test_preventallow_logout(void) { CHECK_FOR_SBC; CHECK_FOR_REMOVABLE; + CHECK_FOR_ISCSI(sd); logging(LOG_VERBOSE, LOG_BLANK_LINE); logging(LOG_VERBOSE, "Test that Logout loss clears PREVENT MEDIUM REMOVAL"); - if (sd->iscsi_ctx == NULL) { - const char *err = "[SKIPPED] This PREVENTALLOW test is " - "only supported for iSCSI backends"; - logging(LOG_NORMAL, "%s", err); - CU_PASS(err); - return; - } - logging(LOG_VERBOSE, "Set the PREVENT flag"); PREVENTALLOW(sd, 1); diff --git a/test-tool/test_preventallow_lun_reset.c b/test-tool/test_preventallow_lun_reset.c index a1c0957..e3f7402 100644 --- a/test-tool/test_preventallow_lun_reset.c +++ b/test-tool/test_preventallow_lun_reset.c @@ -31,18 +31,11 @@ test_preventallow_lun_reset(void) CHECK_FOR_SBC; CHECK_FOR_REMOVABLE; + CHECK_FOR_ISCSI(sd); logging(LOG_VERBOSE, LOG_BLANK_LINE); logging(LOG_VERBOSE, "Test that Target Warm Reset clears PREVENT MEDIUM REMOVAL"); - if (sd->iscsi_ctx == NULL) { - const char *err = "[SKIPPED] This PREVENTALLOW test is " - "only supported for iSCSI backends"; - logging(LOG_NORMAL, "%s", err); - CU_PASS(err); - return; - } - logging(LOG_VERBOSE, "Set the PREVENT flag"); PREVENTALLOW(sd, 1); diff --git a/test-tool/test_preventallow_warm_reset.c b/test-tool/test_preventallow_warm_reset.c index c5ec74f..6dac257 100644 --- a/test-tool/test_preventallow_warm_reset.c +++ b/test-tool/test_preventallow_warm_reset.c @@ -31,18 +31,11 @@ test_preventallow_warm_reset(void) CHECK_FOR_SBC; CHECK_FOR_REMOVABLE; + CHECK_FOR_ISCSI(sd); logging(LOG_VERBOSE, LOG_BLANK_LINE); logging(LOG_VERBOSE, "Test that Target Warm Reset clears PREVENT MEDIUM REMOVAL"); - if (sd->iscsi_ctx == NULL) { - const char *err = "[SKIPPED] This PREVENTALLOW test is " - "only supported for iSCSI backends"; - logging(LOG_NORMAL, "%s", err); - CU_PASS(err); - return; - } - logging(LOG_VERBOSE, "Set the PREVENT flag"); PREVENTALLOW(sd, 1); diff --git a/test-tool/test_prout_preempt.c b/test-tool/test_prout_preempt.c index 32edd46..d4424c8 100644 --- a/test-tool/test_prout_preempt.c +++ b/test-tool/test_prout_preempt.c @@ -39,14 +39,7 @@ test_prout_preempt_rm_reg(void) struct scsi_persistent_reserve_in_read_keys *rk; CHECK_FOR_DATALOSS; - - if (sd->iscsi_ctx == NULL) { - const char *err = "[SKIPPED] This PERSISTENT RESERVE test is " - "only supported for iSCSI backends"; - logging(LOG_NORMAL, "%s", err); - CU_PASS(err); - return; - } + CHECK_FOR_ISCSI(sd); logging(LOG_VERBOSE, LOG_BLANK_LINE); logging(LOG_VERBOSE, "Test Persistent Reserve IN PREEMPT works."); diff --git a/test-tool/test_prout_reserve_access.c b/test-tool/test_prout_reserve_access.c index 562362f..e52c160 100644 --- a/test-tool/test_prout_reserve_access.c +++ b/test-tool/test_prout_reserve_access.c @@ -123,13 +123,7 @@ test_prout_reserve_access_ea(void) struct scsi_device *sd2; int ret; - if (sd->iscsi_ctx == NULL) { - const char *err = "[SKIPPED] This PERSISTENT RESERVE test is " - "only supported for iSCSI backends"; - logging(LOG_NORMAL, "%s", err); - CU_PASS(err); - return; - } + CHECK_FOR_ISCSI(sd); ret = mpath_sd2_get_or_clone(sd, &sd2); CU_ASSERT_EQUAL(ret, 0); @@ -147,13 +141,7 @@ test_prout_reserve_access_we(void) struct scsi_device *sd2; int ret; - if (sd->iscsi_ctx == NULL) { - const char *err = "[SKIPPED] This PERSISTENT RESERVE test is " - "only supported for iSCSI backends"; - logging(LOG_NORMAL, "%s", err); - CU_PASS(err); - return; - } + CHECK_FOR_ISCSI(sd); ret = mpath_sd2_get_or_clone(sd, &sd2); CU_ASSERT_EQUAL(ret, 0); @@ -171,13 +159,7 @@ test_prout_reserve_access_earo(void) struct scsi_device *sd2; int ret; - if (sd->iscsi_ctx == NULL) { - const char *err = "[SKIPPED] This PERSISTENT RESERVE test is " - "only supported for iSCSI backends"; - logging(LOG_NORMAL, "%s", err); - CU_PASS(err); - return; - } + CHECK_FOR_ISCSI(sd); ret = mpath_sd2_get_or_clone(sd, &sd2); CU_ASSERT_EQUAL(ret, 0); @@ -195,13 +177,7 @@ test_prout_reserve_access_wero(void) struct scsi_device *sd2; int ret; - if (sd->iscsi_ctx == NULL) { - const char *err = "[SKIPPED] This PERSISTENT RESERVE test is " - "only supported for iSCSI backends"; - logging(LOG_NORMAL, "%s", err); - CU_PASS(err); - return; - } + CHECK_FOR_ISCSI(sd); ret = mpath_sd2_get_or_clone(sd, &sd2); CU_ASSERT_EQUAL(ret, 0); @@ -219,13 +195,7 @@ test_prout_reserve_access_eaar(void) struct scsi_device *sd2; int ret; - if (sd->iscsi_ctx == NULL) { - const char *err = "[SKIPPED] This PERSISTENT RESERVE test is " - "only supported for iSCSI backends"; - logging(LOG_NORMAL, "%s", err); - CU_PASS(err); - return; - } + CHECK_FOR_ISCSI(sd); ret = mpath_sd2_get_or_clone(sd, &sd2); CU_ASSERT_EQUAL(ret, 0); @@ -243,13 +213,7 @@ test_prout_reserve_access_wear(void) struct scsi_device *sd2; int ret; - if (sd->iscsi_ctx == NULL) { - const char *err = "[SKIPPED] This PERSISTENT RESERVE test is " - "only supported for iSCSI backends"; - logging(LOG_NORMAL, "%s", err); - CU_PASS(err); - return; - } + CHECK_FOR_ISCSI(sd); ret = mpath_sd2_get_or_clone(sd, &sd2); CU_ASSERT_EQUAL(ret, 0); diff --git a/test-tool/test_prout_reserve_ownership.c b/test-tool/test_prout_reserve_ownership.c index 010a3b4..622df95 100644 --- a/test-tool/test_prout_reserve_ownership.c +++ b/test-tool/test_prout_reserve_ownership.c @@ -98,13 +98,7 @@ test_prout_reserve_ownership_ea(void) struct scsi_device *sd2; int ret; - if (sd->iscsi_ctx == NULL) { - const char *err = "[SKIPPED] This PERSISTENT RESERVE test is " - "only supported for iSCSI backends"; - logging(LOG_NORMAL, "%s", err); - CU_PASS(err); - return; - } + CHECK_FOR_ISCSI(sd); ret = mpath_sd2_get_or_clone(sd, &sd2); CU_ASSERT_EQUAL(ret, 0); @@ -121,13 +115,7 @@ test_prout_reserve_ownership_we(void) struct scsi_device *sd2; int ret; - if (sd->iscsi_ctx == NULL) { - const char *err = "[SKIPPED] This PERSISTENT RESERVE test is " - "only supported for iSCSI backends"; - logging(LOG_NORMAL, "%s", err); - CU_PASS(err); - return; - } + CHECK_FOR_ISCSI(sd); ret = mpath_sd2_get_or_clone(sd, &sd2); CU_ASSERT_EQUAL(ret, 0); @@ -144,13 +132,7 @@ test_prout_reserve_ownership_earo(void) struct scsi_device *sd2; int ret; - if (sd->iscsi_ctx == NULL) { - const char *err = "[SKIPPED] This PERSISTENT RESERVE test is " - "only supported for iSCSI backends"; - logging(LOG_NORMAL, "%s", err); - CU_PASS(err); - return; - } + CHECK_FOR_ISCSI(sd); ret = mpath_sd2_get_or_clone(sd, &sd2); CU_ASSERT_EQUAL(ret, 0); @@ -167,13 +149,7 @@ test_prout_reserve_ownership_wero(void) struct scsi_device *sd2; int ret; - if (sd->iscsi_ctx == NULL) { - const char *err = "[SKIPPED] This PERSISTENT RESERVE test is " - "only supported for iSCSI backends"; - logging(LOG_NORMAL, "%s", err); - CU_PASS(err); - return; - } + CHECK_FOR_ISCSI(sd); ret = mpath_sd2_get_or_clone(sd, &sd2); CU_ASSERT_EQUAL(ret, 0); @@ -190,13 +166,7 @@ test_prout_reserve_ownership_eaar(void) struct scsi_device *sd2; int ret; - if (sd->iscsi_ctx == NULL) { - const char *err = "[SKIPPED] This PERSISTENT RESERVE test is " - "only supported for iSCSI backends"; - logging(LOG_NORMAL, "%s", err); - CU_PASS(err); - return; - } + CHECK_FOR_ISCSI(sd); ret = mpath_sd2_get_or_clone(sd, &sd2); CU_ASSERT_EQUAL(ret, 0); @@ -213,13 +183,7 @@ test_prout_reserve_ownership_wear(void) struct scsi_device *sd2; int ret; - if (sd->iscsi_ctx == NULL) { - const char *err = "[SKIPPED] This PERSISTENT RESERVE test is " - "only supported for iSCSI backends"; - logging(LOG_NORMAL, "%s", err); - CU_PASS(err); - return; - } + CHECK_FOR_ISCSI(sd); ret = mpath_sd2_get_or_clone(sd, &sd2); CU_ASSERT_EQUAL(ret, 0); diff --git a/test-tool/test_read10_invalid.c b/test-tool/test_read10_invalid.c index 05ea7ce..0265e99 100644 --- a/test-tool/test_read10_invalid.c +++ b/test-tool/test_read10_invalid.c @@ -38,13 +38,7 @@ test_read10_invalid(void) logging(LOG_VERBOSE, "Test invalid READ10 commands"); logging(LOG_VERBOSE, "Block size is %zu", block_size); - if (sd->iscsi_ctx == NULL) { - const char *err = "[SKIPPED] This READ10 test is only " - "supported for iSCSI backends"; - logging(LOG_NORMAL, "%s", err); - CU_PASS(err); - return; - } + CHECK_FOR_ISCSI(sd); /* Try a read10 of 1 block but xferlength == 0 */ task = malloc(sizeof(struct scsi_task)); diff --git a/test-tool/test_read10_residuals.c b/test-tool/test_read10_residuals.c index 1ad6851..24eb1c5 100644 --- a/test-tool/test_read10_residuals.c +++ b/test-tool/test_read10_residuals.c @@ -40,13 +40,7 @@ test_read10_residuals(void) logging(LOG_VERBOSE, "Test READ10 commands with residuals"); logging(LOG_VERBOSE, "Block size is %zu", block_size); - if (sd->iscsi_ctx == NULL) { - const char *err = "[SKIPPED] This READ10 test is only " - "supported for iSCSI backends"; - logging(LOG_NORMAL, "%s", err); - CU_PASS(err); - return; - } + CHECK_FOR_ISCSI(sd); /* Try a read10 of 1 block but xferlength == 0 */ task = malloc(sizeof(struct scsi_task)); diff --git a/test-tool/test_read12_residuals.c b/test-tool/test_read12_residuals.c index 8ac9384..d239acd 100644 --- a/test-tool/test_read12_residuals.c +++ b/test-tool/test_read12_residuals.c @@ -36,13 +36,7 @@ test_read12_residuals(void) logging(LOG_VERBOSE, "Test READ12 commands with residuals"); logging(LOG_VERBOSE, "Block size is %zu", block_size); - if (sd->iscsi_ctx == NULL) { - const char *err = "[SKIPPED] This READ12 test is only " - "supported for iSCSI backends"; - logging(LOG_NORMAL, "%s", err); - CU_PASS(err); - return; - } + CHECK_FOR_ISCSI(sd); task = malloc(sizeof(struct scsi_task)); CU_ASSERT_PTR_NOT_NULL_FATAL(task); diff --git a/test-tool/test_read16_residuals.c b/test-tool/test_read16_residuals.c index 72fcbf7..5326bc4 100644 --- a/test-tool/test_read16_residuals.c +++ b/test-tool/test_read16_residuals.c @@ -36,13 +36,7 @@ test_read16_residuals(void) logging(LOG_VERBOSE, "Test READ16 commands with residuals"); logging(LOG_VERBOSE, "Block size is %zu", block_size); - if (sd->iscsi_ctx == NULL) { - const char *err = "[SKIPPED] This READ16 test is only " - "supported for iSCSI backends"; - logging(LOG_NORMAL, "%s", err); - CU_PASS(err); - return; - } + CHECK_FOR_ISCSI(sd); task = malloc(sizeof(struct scsi_task)); CU_ASSERT_PTR_NOT_NULL_FATAL(task); diff --git a/test-tool/test_reserve6_2initiators.c b/test-tool/test_reserve6_2initiators.c index 89eeea1..1ed89ea 100644 --- a/test-tool/test_reserve6_2initiators.c +++ b/test-tool/test_reserve6_2initiators.c @@ -34,13 +34,7 @@ test_reserve6_2initiators(void) logging(LOG_VERBOSE, LOG_BLANK_LINE); logging(LOG_VERBOSE, "Test RESERVE6/RELEASE6 across two initiators"); - if (sd->iscsi_ctx == NULL) { - const char *err = "[SKIPPED] This RESERVE6 test is only " - "supported for iSCSI backends"; - logging(LOG_NORMAL, "%s", err); - CU_PASS(err); - return; - } + CHECK_FOR_ISCSI(sd); logging(LOG_NORMAL, "Take out a RESERVE6 from the first initiator"); RESERVE6(sd); diff --git a/test-tool/test_reserve6_itnexus_loss.c b/test-tool/test_reserve6_itnexus_loss.c index 9a35d43..85da632 100644 --- a/test-tool/test_reserve6_itnexus_loss.c +++ b/test-tool/test_reserve6_itnexus_loss.c @@ -34,13 +34,7 @@ test_reserve6_itnexus_loss(void) logging(LOG_VERBOSE, LOG_BLANK_LINE); logging(LOG_VERBOSE, "Test that RESERVE6 is released on it-nexus loss"); - if (sd->iscsi_ctx == NULL) { - const char *err = "[SKIPPED] This RESERVE6 test is only " - "supported for iSCSI backends"; - logging(LOG_NORMAL, "%s", err); - CU_PASS(err); - return; - } + CHECK_FOR_ISCSI(sd); logging(LOG_NORMAL, "Take out a RESERVE6 from the first initiator"); RESERVE6(sd); diff --git a/test-tool/test_reserve6_logout.c b/test-tool/test_reserve6_logout.c index ab6ff96..39396b9 100644 --- a/test-tool/test_reserve6_logout.c +++ b/test-tool/test_reserve6_logout.c @@ -34,13 +34,7 @@ test_reserve6_logout(void) logging(LOG_VERBOSE, LOG_BLANK_LINE); logging(LOG_VERBOSE, "Test that RESERVE6 is released on logout"); - if (sd->iscsi_ctx == NULL) { - const char *err = "[SKIPPED] This RESERVE6 test is only " - "supported for iSCSI backends"; - logging(LOG_NORMAL, "%s", err); - CU_PASS(err); - return; - } + CHECK_FOR_ISCSI(sd); logging(LOG_NORMAL, "Take out a RESERVE6 from the first initiator"); RESERVE6(sd); diff --git a/test-tool/test_reserve6_lun_reset.c b/test-tool/test_reserve6_lun_reset.c index ef228ca..9b836e5 100644 --- a/test-tool/test_reserve6_lun_reset.c +++ b/test-tool/test_reserve6_lun_reset.c @@ -34,13 +34,7 @@ test_reserve6_lun_reset(void) logging(LOG_VERBOSE, LOG_BLANK_LINE); logging(LOG_VERBOSE, "Test that RESERVE6 is released on lun reset"); - if (sd->iscsi_ctx == NULL) { - const char *err = "[SKIPPED] This RESERVE6 test is only " - "supported for iSCSI backends"; - logging(LOG_NORMAL, "%s", err); - CU_PASS(err); - return; - } + CHECK_FOR_ISCSI(sd); logging(LOG_VERBOSE, "Take out a RESERVE6 from the first initiator"); RESERVE6(sd); diff --git a/test-tool/test_reserve6_target_cold_reset.c b/test-tool/test_reserve6_target_cold_reset.c index 4ab3d88..b24f9d2 100644 --- a/test-tool/test_reserve6_target_cold_reset.c +++ b/test-tool/test_reserve6_target_cold_reset.c @@ -34,13 +34,7 @@ test_reserve6_target_cold_reset(void) logging(LOG_VERBOSE, LOG_BLANK_LINE); logging(LOG_VERBOSE, "Test that RESERVE6 is released on target cold reset"); - if (sd->iscsi_ctx == NULL) { - const char *err = "[SKIPPED] This RESERVE6 test is only " - "supported for iSCSI backends"; - logging(LOG_NORMAL, "%s", err); - CU_PASS(err); - return; - } + CHECK_FOR_ISCSI(sd); logging(LOG_VERBOSE, "Take out a RESERVE6 from the first initiator"); RESERVE6(sd); diff --git a/test-tool/test_reserve6_target_warm_reset.c b/test-tool/test_reserve6_target_warm_reset.c index dbd0af8..e6de6e1 100644 --- a/test-tool/test_reserve6_target_warm_reset.c +++ b/test-tool/test_reserve6_target_warm_reset.c @@ -34,13 +34,7 @@ test_reserve6_target_warm_reset(void) logging(LOG_VERBOSE, LOG_BLANK_LINE); logging(LOG_VERBOSE, "Test that RESERVE6 is released on target warm reset"); - if (sd->iscsi_ctx == NULL) { - const char *err = "[SKIPPED] This RESERVE6 test is only " - "supported for iSCSI backends"; - logging(LOG_NORMAL, "%s", err); - CU_PASS(err); - return; - } + CHECK_FOR_ISCSI(sd); logging(LOG_VERBOSE, "Take out a RESERVE6 from the first initiator"); RESERVE6(sd); diff --git a/test-tool/test_sanitize_block_erase_reserved.c b/test-tool/test_sanitize_block_erase_reserved.c index 4a0d2cb..0b11c22 100644 --- a/test-tool/test_sanitize_block_erase_reserved.c +++ b/test-tool/test_sanitize_block_erase_reserved.c @@ -57,13 +57,7 @@ void test_sanitize_block_erase_reserved(void) CHECK_FOR_SANITIZE; CHECK_FOR_DATALOSS; - if (sd->iscsi_ctx == NULL) { - const char *err = "[SKIPPED] This test is " - "only supported for iSCSI backends"; - logging(LOG_NORMAL, "%s", err); - CU_PASS(err); - return; - } + CHECK_FOR_ISCSI(sd); /* override transport queue_pdu callback for PDU manipulation */ iscsi_drv_orig = *sd->iscsi_ctx->drv; diff --git a/test-tool/test_sanitize_crypto_erase_reserved.c b/test-tool/test_sanitize_crypto_erase_reserved.c index 646cbea..2621b79 100644 --- a/test-tool/test_sanitize_crypto_erase_reserved.c +++ b/test-tool/test_sanitize_crypto_erase_reserved.c @@ -57,13 +57,7 @@ void test_sanitize_crypto_erase_reserved(void) CHECK_FOR_SANITIZE; CHECK_FOR_DATALOSS; - if (sd->iscsi_ctx == NULL) { - const char *err = "[SKIPPED] This test is " - "only supported for iSCSI backends"; - logging(LOG_NORMAL, "%s", err); - CU_PASS(err); - return; - } + CHECK_FOR_ISCSI(sd); /* override transport queue_pdu callback for PDU manipulation */ iscsi_drv_orig = *sd->iscsi_ctx->drv; diff --git a/test-tool/test_sanitize_overwrite_reserved.c b/test-tool/test_sanitize_overwrite_reserved.c index 91090b4..3df740a 100644 --- a/test-tool/test_sanitize_overwrite_reserved.c +++ b/test-tool/test_sanitize_overwrite_reserved.c @@ -68,13 +68,7 @@ void test_sanitize_overwrite_reserved(void) CHECK_FOR_SANITIZE; CHECK_FOR_DATALOSS; - if (sd->iscsi_ctx == NULL) { - const char *err = "[SKIPPED] This test is " - "only supported for iSCSI backends"; - logging(LOG_NORMAL, "%s", err); - CU_PASS(err); - return; - } + CHECK_FOR_ISCSI(sd); /* override transport queue_pdu callback for PDU manipulation */ iscsi_drv_orig = *sd->iscsi_ctx->drv; diff --git a/test-tool/test_sanitize_readonly.c b/test-tool/test_sanitize_readonly.c index 769ee0e..3e309b1 100644 --- a/test-tool/test_sanitize_readonly.c +++ b/test-tool/test_sanitize_readonly.c @@ -40,14 +40,7 @@ test_sanitize_readonly(void) CHECK_FOR_SANITIZE; CHECK_FOR_DATALOSS; - - if (sd->iscsi_ctx == NULL) { - const char *err = "[SKIPPED] This SANITIZE test is only " - "supported for iSCSI backends"; - logging(LOG_NORMAL, "%s", err); - CU_PASS(err); - return; - } + CHECK_FOR_ISCSI(sd); logging(LOG_VERBOSE, "Create a second connection to the target"); ret = mpath_sd2_get_or_clone(sd, &sd2); diff --git a/test-tool/test_sanitize_reservations.c b/test-tool/test_sanitize_reservations.c index b04a380..71d0d6a 100644 --- a/test-tool/test_sanitize_reservations.c +++ b/test-tool/test_sanitize_reservations.c @@ -40,14 +40,7 @@ test_sanitize_reservations(void) CHECK_FOR_SANITIZE; CHECK_FOR_DATALOSS; - - if (sd->iscsi_ctx == NULL) { - const char *err = "[SKIPPED] This SANITIZE test is only " - "supported for iSCSI backends"; - logging(LOG_NORMAL, "%s", err); - CU_PASS(err); - return; - } + CHECK_FOR_ISCSI(sd); logging(LOG_VERBOSE, "Create a second connection to the target"); ret = mpath_sd2_get_or_clone(sd, &sd2); diff --git a/test-tool/test_sanitize_reset.c b/test-tool/test_sanitize_reset.c index 89ec870..19d1fb5 100644 --- a/test-tool/test_sanitize_reset.c +++ b/test-tool/test_sanitize_reset.c @@ -45,14 +45,7 @@ test_sanitize_reset(void) CHECK_FOR_SANITIZE; CHECK_FOR_DATALOSS; - - if (sd->iscsi_ctx == NULL) { - const char *err = "[SKIPPED] This SANITIZE test is only " - "supported for iSCSI backends"; - logging(LOG_NORMAL, "%s", err); - CU_PASS(err); - return; - } + CHECK_FOR_ISCSI(sd); logging(LOG_VERBOSE, "Check that SANITIZE OVERWRITE will continue " "even after Task/Lun/Target/* reset."); diff --git a/test-tool/test_write10_residuals.c b/test-tool/test_write10_residuals.c index 7c2628f..911c39c 100644 --- a/test-tool/test_write10_residuals.c +++ b/test-tool/test_write10_residuals.c @@ -42,14 +42,7 @@ test_write10_residuals(void) CHECK_FOR_DATALOSS; CHECK_FOR_SBC; - - if (sd->iscsi_ctx == NULL) { - const char *err = "[SKIPPED] This WRITE10 test is only " - "supported for iSCSI backends"; - logging(LOG_NORMAL, "%s", err); - CU_PASS(err); - return; - } + CHECK_FOR_ISCSI(sd); /* Try a write10 of 1 block but xferlength == 0 */ task = malloc(sizeof(struct scsi_task)); diff --git a/test-tool/test_write12_residuals.c b/test-tool/test_write12_residuals.c index fef2511..02819ba 100644 --- a/test-tool/test_write12_residuals.c +++ b/test-tool/test_write12_residuals.c @@ -42,14 +42,7 @@ test_write12_residuals(void) CHECK_FOR_DATALOSS; CHECK_FOR_SBC; - - if (sd->iscsi_ctx == NULL) { - const char *err = "[SKIPPED] This WRITE12 test is only " - "supported for iSCSI backends"; - logging(LOG_NORMAL, "%s", err); - CU_PASS(err); - return; - } + CHECK_FOR_ISCSI(sd); /* Try a write12 of 1 block but xferlength == 0 */ task = malloc(sizeof(struct scsi_task)); diff --git a/test-tool/test_write16_residuals.c b/test-tool/test_write16_residuals.c index 2f2de94..7088cac 100644 --- a/test-tool/test_write16_residuals.c +++ b/test-tool/test_write16_residuals.c @@ -42,14 +42,7 @@ test_write16_residuals(void) CHECK_FOR_DATALOSS; CHECK_FOR_SBC; - - if (sd->iscsi_ctx == NULL) { - const char *err = "[SKIPPED] This WRITE16 test is only " - "supported for iSCSI backends"; - logging(LOG_NORMAL, "%s", err); - CU_PASS(err); - return; - } + CHECK_FOR_ISCSI(sd); /* Try a write16 of 1 block but xferlength == 0 */ task = malloc(sizeof(struct scsi_task)); diff --git a/test-tool/test_writeverify10_residuals.c b/test-tool/test_writeverify10_residuals.c index fbc6eef..7fa100f 100644 --- a/test-tool/test_writeverify10_residuals.c +++ b/test-tool/test_writeverify10_residuals.c @@ -42,14 +42,7 @@ test_writeverify10_residuals(void) CHECK_FOR_DATALOSS; CHECK_FOR_SBC; - - if (sd->iscsi_ctx == NULL) { - const char *err = "[SKIPPED] This WRITEVERIFY10 test is only " - "supported for iSCSI backends"; - logging(LOG_NORMAL, "%s", err); - CU_PASS(err); - return; - } + CHECK_FOR_ISCSI(sd); /* check if writeverify10 is supported */ WRITEVERIFY10(sd, 0, 0, block_size, 0, 0, 0, 0, NULL, diff --git a/test-tool/test_writeverify12_residuals.c b/test-tool/test_writeverify12_residuals.c index c78c368..04b6992 100644 --- a/test-tool/test_writeverify12_residuals.c +++ b/test-tool/test_writeverify12_residuals.c @@ -42,14 +42,7 @@ test_writeverify12_residuals(void) CHECK_FOR_DATALOSS; CHECK_FOR_SBC; - - if (sd->iscsi_ctx == NULL) { - const char *err = "[SKIPPED] This WRITEVERIFY12 test is only " - "supported for iSCSI backends"; - logging(LOG_NORMAL, "%s", err); - CU_PASS(err); - return; - } + CHECK_FOR_ISCSI(sd); /* check if writeverify12 is supported */ WRITEVERIFY12(sd, 0, 0, block_size, 0, 0, 0, 0, NULL, diff --git a/test-tool/test_writeverify16_residuals.c b/test-tool/test_writeverify16_residuals.c index 4d45ec8..95df7cb 100644 --- a/test-tool/test_writeverify16_residuals.c +++ b/test-tool/test_writeverify16_residuals.c @@ -42,27 +42,12 @@ test_writeverify16_residuals(void) CHECK_FOR_DATALOSS; CHECK_FOR_SBC; - - if (sd->iscsi_ctx == NULL) { - const char *err = "[SKIPPED] This WRITEVERIFY16 test is only " - "supported for iSCSI backends"; - logging(LOG_NORMAL, "%s", err); - CU_PASS(err); - return; - } + CHECK_FOR_ISCSI(sd); /* check if writeverify16 is supported */ WRITEVERIFY16(sd, 0, 0, block_size, 0, 0, 1, 0, NULL, EXPECT_STATUS_GOOD); - if (sd->iscsi_ctx == NULL) { - const char *err = "[SKIPPED] WRITEVERIFY16 tests are only " - "supported for iSCSI backends"; - logging(LOG_NORMAL, "%s", err); - CU_PASS(err); - return; - } - /* Try a writeverify16 of 1 block but xferlength == 0 */ task = malloc(sizeof(struct scsi_task)); CU_ASSERT_PTR_NOT_NULL_FATAL(task); From 84d80878a6bafa0f662904918c7e0e81eb6a4a7f Mon Sep 17 00:00:00 2001 From: David Disseldorp Date: Mon, 23 Sep 2019 16:46:26 +0200 Subject: [PATCH 7/7] test-tool: add iSCSI CHAP_A negotiation tests The iSCSICHAP.Simple test ensures that CHAP_A comma separated lists are correctly handled. iSCSICHAP.Invalid tests a range of invalid CHAP_A parameters. Signed-off-by: David Disseldorp --- test-tool/Makefile.am | 1 + test-tool/iscsi-test-cu.c | 9 ++ test-tool/iscsi-test-cu.h | 2 + test-tool/test_iscsi_chap.c | 221 ++++++++++++++++++++++++++++++++++++ 4 files changed, 233 insertions(+) create mode 100644 test-tool/test_iscsi_chap.c diff --git a/test-tool/Makefile.am b/test-tool/Makefile.am index f9faa90..642704b 100644 --- a/test-tool/Makefile.am +++ b/test-tool/Makefile.am @@ -42,6 +42,7 @@ iscsi_test_cu_SOURCES = iscsi-test-cu.c \ test_iscsi_datasn_invalid.c \ test_iscsi_sendtargets.c \ test_iscsi_nop_simple.c \ + test_iscsi_chap.c \ test_mandatory_sbc.c \ test_modesense6_all_pages.c \ test_modesense6_control.c \ diff --git a/test-tool/iscsi-test-cu.c b/test-tool/iscsi-test-cu.c index 670a705..7a795e4 100644 --- a/test-tool/iscsi-test-cu.c +++ b/test-tool/iscsi-test-cu.c @@ -569,6 +569,12 @@ static CU_TestInfo tests_iscsi_nop[] = { CU_TEST_INFO_NULL }; +static CU_TestInfo tests_iscsi_chap[] = { + { (char *)"Simple", test_iscsi_chap_simple }, + { (char *)"Invalid", test_iscsi_chap_invalid }, + CU_TEST_INFO_NULL +}; + static CU_TestInfo tests_iscsi_residuals[] = { { (char *)"Read10Invalid", test_read10_invalid }, { (char *)"Read10Residuals", test_read10_residuals }, @@ -603,6 +609,8 @@ static libiscsi_suite_info iscsi_suites[] = { tests_iscsi_sendtargets }, { "iSCSINop", NON_PGR_FUNCS, tests_iscsi_nop }, + { "iSCSICHAP", NON_PGR_FUNCS, + tests_iscsi_chap }, { NULL, NULL, NULL, NULL, NULL, NULL } }; @@ -662,6 +670,7 @@ static libiscsi_suite_info all_suites[] = { { "iSCSITMF", NON_PGR_FUNCS, tests_iscsi_tmf }, { "iSCSISendTargets", NON_PGR_FUNCS, tests_iscsi_sendtargets }, { "iSCSINop", NON_PGR_FUNCS, tests_iscsi_nop }, + { "iSCSICHAP", NON_PGR_FUNCS, tests_iscsi_chap }, { "MultipathIO", NON_PGR_FUNCS, tests_multipathio }, { NULL, NULL, NULL, NULL, NULL, NULL }, }; diff --git a/test-tool/iscsi-test-cu.h b/test-tool/iscsi-test-cu.h index b05bbe2..2cf26a5 100644 --- a/test-tool/iscsi-test-cu.h +++ b/test-tool/iscsi-test-cu.h @@ -81,6 +81,8 @@ void test_iscsi_datasn_invalid(void); void test_iscsi_sendtargets_simple(void); void test_iscsi_sendtargets_invalid(void); void test_iscsi_nop_simple(void); +void test_iscsi_chap_simple(void); +void test_iscsi_chap_invalid(void); void test_mandatory_sbc(void); diff --git a/test-tool/test_iscsi_chap.c b/test-tool/test_iscsi_chap.c new file mode 100644 index 0000000..ebefc1e --- /dev/null +++ b/test-tool/test_iscsi_chap.c @@ -0,0 +1,221 @@ +/* + Copyright (C) 2019 SUSE LLC + Copyright (C) 2013 by Ronnie Sahlberg + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, see . +*/ +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include + +#include "iscsi.h" +#include "iscsi-private.h" +#include "scsi-lowlevel.h" +#include "iscsi-test-cu.h" + +static struct iscsi_transport iscsi_drv_orig; + +static int +test_iscsi_strip_tag(struct iscsi_context *iscsi, struct iscsi_pdu *pdu, + const char *tag) +{ + unsigned char *s; + unsigned char *end; + size_t remain; + size_t toklen; + + toklen = strlen(tag); + if ((toklen < 2) || (tag[toklen - 1] != '=')) { + return -EINVAL; + } + + s = memmem(pdu->outdata.data, pdu->outdata.size, tag, toklen); + if (s == NULL) { + return -ENOENT; + } + + remain = pdu->outdata.size - (s - pdu->outdata.data); + if ((remain == 0) || (remain > pdu->outdata.size)) { + return -EINVAL; + } + + end = memchr(s, 0, remain); + if (end == NULL) { + return -EINVAL; + } + + toklen = end - s; + assert(toklen > 0); + + /* handle padding */ + while ((toklen < remain) && (s[toklen] == '\0')) { + toklen++; + } + + memmove(s, s + toklen, remain - toklen); + pdu->outdata.size -= toklen; + + /* update data segment length */ + scsi_set_uint32(&pdu->outdata.data[4], pdu->outdata.size + - ISCSI_HEADER_SIZE(iscsi->header_digest)); + logging(LOG_VERBOSE, "stripped %s key and value from PDU", tag); + + return 0; +} + +static int +chap_mod_strip_replace_queue(struct iscsi_context *iscsi, struct iscsi_pdu *pdu, + const char *new_chap_a) +{ + int ret; + + if ((pdu->outdata.data[0] & 0x3f) != ISCSI_PDU_LOGIN_REQUEST) { + goto out; + } + + ret = test_iscsi_strip_tag(iscsi, pdu, "CHAP_A="); + if (ret == -ENOENT) { + logging(LOG_VERBOSE, "ignoring login PDU without CHAP_A"); + goto out;; + } + if (ret < 0) { + return ret; + } + ret = iscsi_pdu_add_data(iscsi, pdu, (const unsigned char *)new_chap_a, + strlen(new_chap_a) + 1); + if (ret < 0) { + return ret; + } + logging(LOG_VERBOSE, "replaced Login PDU CHAP_A setting with %s", new_chap_a); + /* restore drv */ + *iscsi->drv = iscsi_drv_orig; +out: + return iscsi_drv_orig.queue_pdu(iscsi, pdu); + +} + +static int +chap_mod_many_types_queue(struct iscsi_context *iscsi, struct iscsi_pdu *pdu) +{ + return chap_mod_strip_replace_queue(iscsi, pdu, "CHAP_A=5,6,7,8"); +} + +static int +chap_mod_no_type_queue(struct iscsi_context *iscsi, struct iscsi_pdu *pdu) +{ + return chap_mod_strip_replace_queue(iscsi, pdu, "CHAP_A="); +} + +static int +chap_mod_bad_type_queue(struct iscsi_context *iscsi, struct iscsi_pdu *pdu) +{ + /* value starts with '5', to catch targets that only check one byte */ + return chap_mod_strip_replace_queue(iscsi, pdu, "CHAP_A=56"); +} + +static int +test_iscsi_chap_login(int (*test_queue_pdu)(struct iscsi_context *iscsi, + struct iscsi_pdu *pdu)) +{ + struct iscsi_context *iscsi; + struct iscsi_url *iscsi_url; + int ret; + + iscsi = iscsi_create_context(initiatorname2); + if (iscsi == NULL) { + return -ENOMEM; + } + + iscsi_url = iscsi_parse_full_url(iscsi, sd->iscsi_url); + if (iscsi_url == NULL) { + ret = -ENOMEM; + goto err_iscsi_destroy; + } + + iscsi_set_targetname(iscsi, iscsi_url->target); + iscsi_set_session_type(iscsi, ISCSI_SESSION_NORMAL); + iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_NONE_CRC32C); + iscsi_set_noautoreconnect(iscsi, 1); + + iscsi_set_initiator_username_pwd(iscsi, iscsi_url->user, + iscsi_url->passwd); + + /* override transport queue_pdu callback for PDU manipulation */ + iscsi_drv_orig = *iscsi->drv; + iscsi->drv->queue_pdu = test_queue_pdu; + + ret = iscsi_full_connect_sync(iscsi, iscsi_url->portal, iscsi_url->lun); + if (ret < 0) { + ret = -EIO; + goto err_url_destroy; + } + + ret = 0; +err_url_destroy: + iscsi_destroy_url(iscsi_url); + /* XXX no need to restore iscsi_drv_orig */ +err_iscsi_destroy: + iscsi_destroy_context(iscsi); + return ret; +} + +void +test_iscsi_chap_simple(void) +{ + int ret; + + logging(LOG_VERBOSE, LOG_BLANK_LINE); + logging(LOG_VERBOSE, "Test CHAP_A negotiation"); + + CHECK_FOR_ISCSI(sd); + if (sd->iscsi_ctx->chap_a != 5) { + const char *err = "[SKIPPED] This test requires " + "an iSCSI session with CHAP_A=5"; + logging(LOG_NORMAL, "%s", err); + CU_PASS(err); + return; + } + + ret = test_iscsi_chap_login(chap_mod_many_types_queue); + CU_ASSERT_EQUAL(ret, 0); +} + +void +test_iscsi_chap_invalid(void) +{ + int ret; + + logging(LOG_VERBOSE, LOG_BLANK_LINE); + logging(LOG_VERBOSE, "Test CHAP_A negotiation"); + + CHECK_FOR_ISCSI(sd); + if (sd->iscsi_ctx->chap_a != 5) { + const char *err = "[SKIPPED] This test requires " + "an iSCSI session with CHAP_A=5"; + logging(LOG_NORMAL, "%s", err); + CU_PASS(err); + return; + } + + ret = test_iscsi_chap_login(chap_mod_bad_type_queue); + CU_ASSERT_EQUAL(ret, -EIO); + + ret = test_iscsi_chap_login(chap_mod_no_type_queue); + CU_ASSERT_EQUAL(ret, -EIO); +}