diff --git a/Makefile.am b/Makefile.am index 91b0441..a8d5259 100644 --- a/Makefile.am +++ b/Makefile.am @@ -120,7 +120,8 @@ bin_iscsi_test_SOURCES = test-tool/iscsi-test.c \ test-tool/0386_preventallow_2_it_nexuses.c \ test-tool/0390_mandatory_opcodes_sbc.c \ \ - test-tool/1000_cmdsn_invalid.c + test-tool/1000_cmdsn_invalid.c \ + test-tool/1010_datasn_invalid.c endif diff --git a/test-tool/1000_cmdsn_invalid.c b/test-tool/1000_cmdsn_invalid.c index d1550b1..f6d0521 100644 --- a/test-tool/1000_cmdsn_invalid.c +++ b/test-tool/1000_cmdsn_invalid.c @@ -15,22 +15,16 @@ 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) +static int my_iscsi_queue_pdu(struct iscsi_context *iscsi, struct iscsi_pdu *pdu) { switch (change_cmdsn) { case 1: @@ -44,7 +38,6 @@ int iscsi_queue_pdu(struct iscsi_context *iscsi, struct iscsi_pdu *pdu) } change_cmdsn = 0; - return real_iscsi_queue_pdu(iscsi, pdu); } static void test_cb(struct iscsi_context *iscsi _U_, int status, @@ -73,8 +66,6 @@ int T1000_cmdsn_invalid(const char *initiator, const char *url, int data_loss, i 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) { @@ -128,6 +119,7 @@ int T1000_cmdsn_invalid(const char *initiator, const char *url, int data_loss, i iscsi->use_immediate_data = ISCSI_IMMEDIATE_DATA_NO; iscsi->target_max_recv_data_segment_length = 512; + local_iscsi_queue_pdu = my_iscsi_queue_pdu; printf("Write 2 blocks with CMDSN > MAXCMDSN ... "); change_cmdsn = 1; @@ -200,6 +192,7 @@ test3: iscsi_set_noautoreconnect(iscsi, 0); finished: + local_iscsi_queue_pdu = NULL; iscsi_logout_sync(iscsi); iscsi_destroy_context(iscsi); return ret; diff --git a/test-tool/1010_datasn_invalid.c b/test-tool/1010_datasn_invalid.c new file mode 100644 index 0000000..7785dd0 --- /dev/null +++ b/test-tool/1010_datasn_invalid.c @@ -0,0 +1,157 @@ +/* + 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 . +*/ + +#include +#include +#include "iscsi.h" +#include "iscsi-private.h" +#include "scsi-lowlevel.h" +#include "iscsi-test.h" + +static int clamp_datasn; + +static void my_iscsi_queue_pdu(struct iscsi_context *iscsi, struct iscsi_pdu *pdu) +{ + switch (clamp_datasn) { + case 1: + /* change datasn to 0 */ + *(uint32_t *)&pdu->outdata.data[36] = 0; + break; + } +} + +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 T1010_datasn_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; + + printf("1010_datasn_invalid:\n"); + printf("==================\n"); + if (show_info) { + printf("Test sending commands with invalid datasn values.\n"); + printf("1, Test that 2 DATA-IN with DATASN==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; + local_iscsi_queue_pdu = my_iscsi_queue_pdu; + + printf("Write 2 DATA-IN with DATASN == 0 ... "); + clamp_datasn = 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); + clamp_datasn = 0; + 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); + +finished: + local_iscsi_queue_pdu = NULL; + 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 8c5f7ac..40adaf5 100644 --- a/test-tool/iscsi-test.c +++ b/test-tool/iscsi-test.c @@ -17,6 +17,10 @@ along with this program; if not, see . */ +#define _GNU_SOURCE +#include +#include +#include #include #include #include @@ -26,7 +30,7 @@ #include #include "iscsi.h" #include "scsi-lowlevel.h" -#include "iscsi.h" +#include "iscsi-private.h" #include "iscsi-test.h" const char *initiator = "iqn.2007-10.com.github:sahlberg:libiscsi:iscsi-test"; @@ -194,6 +198,9 @@ struct scsi_test tests[] = { /* invalid cmdsn from initiator */ { "T1000_cmdsn_invalid", T1000_cmdsn_invalid }, +/* invalid datasn from initiator */ +{ "T1010_datasn_invalid", T1010_datasn_invalid }, + { NULL, NULL } }; @@ -304,6 +311,16 @@ void wait_until_test_finished(struct iscsi_context *iscsi, struct iscsi_async_st } } +static int (*real_iscsi_queue_pdu)(struct iscsi_context *iscsi, struct iscsi_pdu *pdu); + +int iscsi_queue_pdu(struct iscsi_context *iscsi, struct iscsi_pdu *pdu) +{ + if (local_iscsi_queue_pdu != NULL) { + local_iscsi_queue_pdu(iscsi, pdu); + } + return real_iscsi_queue_pdu(iscsi, pdu); +} + int main(int argc, const char *argv[]) { poptContext pc; @@ -326,6 +343,8 @@ int main(int argc, const char *argv[]) POPT_TABLEEND }; + real_iscsi_queue_pdu = dlsym(RTLD_NEXT, "iscsi_queue_pdu"); + pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_POSIXMEHARDER); if ((res = poptGetNextOpt(pc)) < -1) { fprintf(stderr, "Failed to parse option : %s %s\n", diff --git a/test-tool/iscsi-test.h b/test-tool/iscsi-test.h index 37fdbdc..45318b0 100644 --- a/test-tool/iscsi-test.h +++ b/test-tool/iscsi-test.h @@ -25,6 +25,7 @@ struct iscsi_async_state { int finished; }; void wait_until_test_finished(struct iscsi_context *iscsi, struct iscsi_async_state *test_state); +void (*local_iscsi_queue_pdu)(struct iscsi_context *iscsi, struct iscsi_pdu *pdu); 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); @@ -146,3 +147,4 @@ int T0386_preventallow_2_itl_nexuses(const char *initiator, const char *url, int 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); +int T1010_datasn_invalid(const char *initiator, const char *url, int data_loss, int show_info);