diff --git a/Makefile.am b/Makefile.am index 2fa7aea..91b0441 100644 --- a/Makefile.am +++ b/Makefile.am @@ -54,6 +54,7 @@ bin_iscsi_dd_SOURCES = examples/iscsi-dd.c noinst_PROGRAMS += bin/iscsi-test dist_noinst_HEADERS += test-tool/iscsi-test.h bin_iscsi_test_CPPFLAGS = $(AM_CPPFLAGS) -I$(srcdir)/test-tool +bin_iscsi_test_LDFLAGS = -ldl bin_iscsi_test_SOURCES = test-tool/iscsi-test.c \ test-tool/0100_read10_simple.c test-tool/0101_read10_beyond_eol.c \ test-tool/0102_read10_0blocks.c test-tool/0103_read10_rdprotect.c \ @@ -117,7 +118,9 @@ bin_iscsi_test_SOURCES = test-tool/iscsi-test.c \ test-tool/0384_preventallow_target_cold_reset.c \ test-tool/0385_preventallow_lun_reset.c \ test-tool/0386_preventallow_2_it_nexuses.c \ - test-tool/0390_mandatory_opcodes_sbc.c + test-tool/0390_mandatory_opcodes_sbc.c \ + \ + test-tool/1000_cmdsn_invalid.c endif diff --git a/include/iscsi-private.h b/include/iscsi-private.h index 2e87c43..40a96e3 100644 --- a/include/iscsi-private.h +++ b/include/iscsi-private.h @@ -225,7 +225,8 @@ void iscsi_pdu_set_datasn(struct iscsi_pdu *pdu, uint32_t datasn); void iscsi_pdu_set_bufferoffset(struct iscsi_pdu *pdu, uint32_t bufferoffset); int iscsi_pdu_add_data(struct iscsi_context *iscsi, struct iscsi_pdu *pdu, unsigned char *dptr, int dsize); -int iscsi_queue_pdu(struct iscsi_context *iscsi, struct iscsi_pdu *pdu); +EXTERN 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); diff --git a/lib/libiscsi.def b/lib/libiscsi.def index 3e3ddab..9ba792f 100644 --- a/lib/libiscsi.def +++ b/lib/libiscsi.def @@ -33,6 +33,7 @@ iscsi_prefetch16_task iscsi_preventallow_sync iscsi_preventallow_task iscsi_queue_length +iscsi_queue_pdu iscsi_read10_sync iscsi_read10_task iscsi_read12_sync diff --git a/lib/libiscsi.syms b/lib/libiscsi.syms index 1bb1719..1828616 100644 --- a/lib/libiscsi.syms +++ b/lib/libiscsi.syms @@ -31,6 +31,7 @@ iscsi_prefetch16_task iscsi_preventallow_sync iscsi_preventallow_task iscsi_queue_length +iscsi_queue_pdu iscsi_read10_sync iscsi_read10_task iscsi_read12_sync diff --git a/test-tool/1000_cmdsn_invalid.c b/test-tool/1000_cmdsn_invalid.c new file mode 100644 index 0000000..d1550b1 --- /dev/null +++ b/test-tool/1000_cmdsn_invalid.c @@ -0,0 +1,206 @@ +/* + Copyright (C) 2012 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 "iscsi.h" +#include "iscsi-private.h" +#include "scsi-lowlevel.h" +#include "iscsi-test.h" + +static int (*real_iscsi_queue_pdu)(struct iscsi_context *iscsi, struct iscsi_pdu *pdu); + +static int change_cmdsn; + +int iscsi_queue_pdu(struct iscsi_context *iscsi, struct iscsi_pdu *pdu) +{ + switch (change_cmdsn) { + case 1: + /* change the cmdsn so it becomes too big */ + *(uint32_t *)&pdu->outdata.data[24] = htonl(iscsi->maxcmdsn + 1); + break; + case 2: + /* change the cmdsn so it becomes too small */ + *(uint32_t *)&pdu->outdata.data[24] = 0; + break; + } + + change_cmdsn = 0; + return real_iscsi_queue_pdu(iscsi, pdu); +} + +static void test_cb(struct iscsi_context *iscsi _U_, int status, + void *command_data _U_, void *private_data) +{ + struct scsi_task *task = command_data; + struct iscsi_async_state *state = private_data; + + state->finished = 1; + state->status = status; + + if (status) { + task->status = status; + } +} + + +int T1000_cmdsn_invalid(const char *initiator, const char *url, int data_loss, int show_info) +{ + struct iscsi_context *iscsi; + struct scsi_task *task; + struct scsi_readcapacity16 *rc16; + int ret, i, lun; + uint32_t block_size; + uint32_t num_blocks; + unsigned char data[512 * 256]; + struct iscsi_async_state test_state; + + real_iscsi_queue_pdu = dlsym(RTLD_NEXT, "iscsi_queue_pdu"); + + printf("1000_cmdsn_invalid:\n"); + printf("==================\n"); + if (show_info) { + printf("Test sending commands with invalid cmdsn values.\n"); + printf("CMDSN MUST be in the range EXPCMDSN and MAXCMDSN\n"); + printf("1, Test that a CMDSN > MAXCMDSN is an error\n"); + printf("2, Test that a CMDSN == 0 is an error\n"); + printf("\n"); + return 0; + } + + iscsi = iscsi_context_login(initiator, url, &lun); + if (iscsi == NULL) { + printf("Failed to login to target\n"); + return -1; + } + + /* find the size of the LUN */ + task = iscsi_readcapacity16_sync(iscsi, lun); + if (task == NULL) { + printf("Failed to send READCAPACITY16 command: %s\n", iscsi_get_error(iscsi)); + ret = -1; + goto finished; + } + if (task->status != SCSI_STATUS_GOOD) { + printf("READCAPACITY16 command: failed with sense. %s\n", iscsi_get_error(iscsi)); + ret = -1; + scsi_free_scsi_task(task); + goto finished; + } + rc16 = scsi_datain_unmarshall(task); + if (rc16 == NULL) { + printf("failed to unmarshall READCAPACITY16 data. %s\n", iscsi_get_error(iscsi)); + ret = -1; + scsi_free_scsi_task(task); + goto finished; + } + block_size = rc16->block_length; + num_blocks = rc16->returned_lba; + scsi_free_scsi_task(task); + + + if (!data_loss) { + printf("--dataloss flag is not set. Skipping test\n"); + ret = -1; + goto finished; + } + + + ret = 0; + + iscsi->use_immediate_data = ISCSI_IMMEDIATE_DATA_NO; + iscsi->target_max_recv_data_segment_length = 512; + + printf("Write 2 blocks with CMDSN > MAXCMDSN ... "); + change_cmdsn = 1; + /* we dont want autoreconnect since some targets will drop the + * on this condition. + */ + iscsi_set_noautoreconnect(iscsi, 1); + + task = iscsi_write10_task(iscsi, lun, 0, data, 2 * block_size, block_size, + 0, 0, 0, 0, 0, + test_cb, &test_state); + if (task == NULL) { + printf("[FAILED]\n"); + printf("Failed to send WRITE10 command: %s\n", iscsi_get_error(iscsi)); + ret++; + goto test2; + } + test_state.task = task; + test_state.finished = 0; + test_state.status = 0; + wait_until_test_finished(iscsi, &test_state); + if (task->status == SCSI_STATUS_GOOD) { + printf("[FAILED]\n"); + printf("WRITE10 command successful. Should have failed with error\n"); + ret++; + scsi_free_scsi_task(task); + goto test2; + } + scsi_free_scsi_task(task); + printf("[OK]\n"); + + +test2: + /* in case the previous test failed the session */ + iscsi_set_noautoreconnect(iscsi, 0); + + printf("Write 2 blocks with CMDSN == 0 ... ");fflush(stdout); + change_cmdsn = 2; + /* we dont want autoreconnect since some targets will drop the + * on this condition. + */ + + iscsi_set_noautoreconnect(iscsi, 1); + task = iscsi_write10_task(iscsi, lun, 0, data, 2 * block_size, block_size, + 0, 0, 0, 0, 0, + test_cb, &test_state); + if (task == NULL) { + printf("[FAILED]\n"); + printf("Failed to send WRITE10 command: %s\n", iscsi_get_error(iscsi)); + ret++; + goto test3; + } + test_state.task = task; + test_state.finished = 0; + test_state.status = 0; + wait_until_test_finished(iscsi, &test_state); + if (task->status == SCSI_STATUS_GOOD) { + printf("[FAILED]\n"); + printf("WRITE10 command successful. Should have failed with error\n"); + ret++; + scsi_free_scsi_task(task); + goto test3; + } + scsi_free_scsi_task(task); + printf("[OK]\n"); + + +test3: + /* in case the previous test failed the session */ + iscsi_set_noautoreconnect(iscsi, 0); + +finished: + iscsi_logout_sync(iscsi); + iscsi_destroy_context(iscsi); + return ret; +} diff --git a/test-tool/iscsi-test.c b/test-tool/iscsi-test.c index 6476d4e..8c5f7ac 100644 --- a/test-tool/iscsi-test.c +++ b/test-tool/iscsi-test.c @@ -39,6 +39,8 @@ struct scsi_test { }; struct scsi_test tests[] = { +/* SCSI protocol tests */ + /* read10*/ { "T0100_read10_simple", T0100_read10_simple }, { "T0101_read10_beyond_eol", T0101_read10_beyond_eol }, @@ -186,6 +188,12 @@ struct scsi_test tests[] = { /* support for mandatory opcodes*/ { "T0390_mandatory_opcodes_sbc", T0390_mandatory_opcodes_sbc }, + +/* iSCSI protocol tests */ + +/* invalid cmdsn from initiator */ +{ "T1000_cmdsn_invalid", T1000_cmdsn_invalid }, + { NULL, NULL } }; @@ -265,6 +273,37 @@ struct iscsi_context *iscsi_context_login(const char *initiatorname, const char } +void wait_until_test_finished(struct iscsi_context *iscsi, struct iscsi_async_state *state) +{ + struct pollfd pfd; + int count = 0; + int ret; + + while (state->finished == 0) { + pfd.fd = iscsi_get_fd(iscsi); + pfd.events = iscsi_which_events(iscsi); + + ret = poll(&pfd, 1, 1000); + if (ret < 0) { + printf("Poll failed"); + exit(10); + } + if (ret == 0) { + if (count++ > 5) { + state->finished = 1; + state->status = SCSI_STATUS_CANCELLED; + state->task->status = SCSI_STATUS_CANCELLED; + return; + } + continue; + } + if (iscsi_service(iscsi, pfd.revents) < 0) { + printf("iscsi_service failed with : %s\n", iscsi_get_error(iscsi)); + break; + } + } +} + int main(int argc, const char *argv[]) { poptContext pc; diff --git a/test-tool/iscsi-test.h b/test-tool/iscsi-test.h index ff34631..37fdbdc 100644 --- a/test-tool/iscsi-test.h +++ b/test-tool/iscsi-test.h @@ -19,6 +19,13 @@ struct iscsi_context *iscsi_context_login(const char *initiatorname, const char *url, int *lun); +struct iscsi_async_state { + struct scsi_task *task; + int status; + int finished; +}; +void wait_until_test_finished(struct iscsi_context *iscsi, struct iscsi_async_state *test_state); + int T0100_read10_simple(const char *initiator, const char *url, int data_loss, int show_info); int T0101_read10_beyond_eol(const char *initiator, const char *url, int data_loss, int show_info); int T0102_read10_0blocks(const char *initiator, const char *url, int data_loss, int show_info); @@ -137,3 +144,5 @@ int T0385_preventallow_lun_reset(const char *initiator, const char *url, int dat int T0386_preventallow_2_itl_nexuses(const char *initiator, const char *url, int data_loss, int show_info); int T0390_mandatory_opcodes_sbc(const char *initiator, const char *url, int data_loss, int show_info); + +int T1000_cmdsn_invalid(const char *initiator, const char *url, int data_loss, int show_info);