From d1c7b17cf5ac5f7fdaea295e5ef841096a9576d5 Mon Sep 17 00:00:00 2001 From: Ronnie Sahlberg Date: Sun, 12 Aug 2012 12:51:37 +1000 Subject: [PATCH] TEST: Start adding tests for invalid BUFFEROFFSET in DATA-IN --- Makefile.am | 3 +- test-tool/1020_bufferoffset_invalid.c | 206 ++++++++++++++++++++++++++ test-tool/iscsi-test.c | 3 + test-tool/iscsi-test.h | 1 + 4 files changed, 212 insertions(+), 1 deletion(-) create mode 100644 test-tool/1020_bufferoffset_invalid.c diff --git a/Makefile.am b/Makefile.am index a8d5259..b7ad460 100644 --- a/Makefile.am +++ b/Makefile.am @@ -121,7 +121,8 @@ bin_iscsi_test_SOURCES = test-tool/iscsi-test.c \ test-tool/0390_mandatory_opcodes_sbc.c \ \ test-tool/1000_cmdsn_invalid.c \ - test-tool/1010_datasn_invalid.c + test-tool/1010_datasn_invalid.c \ + test-tool/1020_bufferoffset_invalid.c endif diff --git a/test-tool/1020_bufferoffset_invalid.c b/test-tool/1020_bufferoffset_invalid.c new file mode 100644 index 0000000..50c935f --- /dev/null +++ b/test-tool/1020_bufferoffset_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 . +*/ + +#include +#include +#include "iscsi.h" +#include "iscsi-private.h" +#include "scsi-lowlevel.h" +#include "iscsi-test.h" + +static int change_bufferoffset; + +static void my_iscsi_queue_pdu(struct iscsi_context *iscsi, struct iscsi_pdu *pdu) +{ + if (pdu->outdata.data[0] != ISCSI_PDU_DATA_OUT) { + return; + } + switch (change_bufferoffset) { + case 1: + /* Add 1M to the buffer offset */ + *(uint32_t *)&pdu->outdata.data[40] = htonl(ntohl(*(uint32_t *)&pdu->outdata.data[40]) + 1024*1024); + break; + case 2: + /* Add -512 to the buffer offset */ + *(uint32_t *)&pdu->outdata.data[40] = htonl(ntohl(*(uint32_t *)&pdu->outdata.data[40]) - 512); + 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 T1020_bufferoffset_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("1020_bufferoffset_invalid:\n"); + printf("==========================\n"); + if (show_info) { + printf("Test sending commands with invalid bufferoffset values.\n"); + printf("We negotiate both DataPDUInOrder and DataSequenceInOrder so BufferOffset must be in sequence both within and across multiple sequences\n"); + printf("1, Test that BufferOffset==1M too high is an error\n"); + printf("2, Test that BufferOffset==-512 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 BUFFEROFFSET 1M too high ... "); + /* 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; + } + change_bufferoffset = 1; + test_state.task = task; + test_state.finished = 0; + test_state.status = 0; + wait_until_test_finished(iscsi, &test_state); + change_bufferoffset = 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); + iscsi->use_immediate_data = ISCSI_IMMEDIATE_DATA_NO; + iscsi->target_max_recv_data_segment_length = 512; + + printf("Write 2 DATA-IN with BUFFEROFFSET==-512 ... "); + /* 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; + } + change_bufferoffset = 2; + test_state.task = task; + test_state.finished = 0; + test_state.status = 0; + wait_until_test_finished(iscsi, &test_state); + change_bufferoffset = 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 test3; + } + scsi_free_scsi_task(task); + printf("[OK]\n"); + + +test3: + /* in case the previous test failed the session */ + iscsi_set_noautoreconnect(iscsi, 0); + iscsi->use_immediate_data = ISCSI_IMMEDIATE_DATA_NO; + iscsi->target_max_recv_data_segment_length = 512; + +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 40adaf5..472fa3b 100644 --- a/test-tool/iscsi-test.c +++ b/test-tool/iscsi-test.c @@ -201,6 +201,9 @@ struct scsi_test tests[] = { /* invalid datasn from initiator */ { "T1010_datasn_invalid", T1010_datasn_invalid }, +/* invalid bufferoffset from initiator */ +{ "T1020_bufferoffset_invalid", T1020_bufferoffset_invalid }, + { NULL, NULL } }; diff --git a/test-tool/iscsi-test.h b/test-tool/iscsi-test.h index 45318b0..ab95d1e 100644 --- a/test-tool/iscsi-test.h +++ b/test-tool/iscsi-test.h @@ -148,3 +148,4 @@ int T0390_mandatory_opcodes_sbc(const char *initiator, const char *url, int data 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); +int T1020_bufferoffset_invalid(const char *initiator, const char *url, int data_loss, int show_info);