From e1f15c8464d6860cd0e54eae0979f17bbb720935 Mon Sep 17 00:00:00 2001 From: Ronnie Sahlberg Date: Sun, 24 Mar 2013 08:56:23 -0700 Subject: [PATCH] TESTS: Add tests that WRITE10/12/16 handle residuals correctly Also test that IF the iSCSI layer provides 2 blocks of DATA-OUT but the SCSI WRITE* command only covers one single block, then only the first block will be modified but not the second block. --- Makefile.am | 3 + test-tool/iscsi-test-cu.c | 3 + test-tool/iscsi-test-cu.h | 3 + test-tool/test_write10_residuals.c | 299 +++++++++++++++++++++++++++++ test-tool/test_write12_residuals.c | 299 +++++++++++++++++++++++++++++ test-tool/test_write16_residuals.c | 299 +++++++++++++++++++++++++++++ 6 files changed, 906 insertions(+) create mode 100644 test-tool/test_write10_residuals.c create mode 100644 test-tool/test_write12_residuals.c create mode 100644 test-tool/test_write16_residuals.c diff --git a/Makefile.am b/Makefile.am index 8fda3be..b5c9f22 100644 --- a/Makefile.am +++ b/Makefile.am @@ -284,16 +284,19 @@ bin_iscsi_test_cu_SOURCES = test-tool/iscsi-test-cu.c \ test-tool/test_write10_0blocks.c \ test-tool/test_write10_wrprotect.c \ test-tool/test_write10_flags.c \ + test-tool/test_write10_residuals.c \ test-tool/test_write12_simple.c \ test-tool/test_write12_beyond_eol.c \ test-tool/test_write12_0blocks.c \ test-tool/test_write12_wrprotect.c \ test-tool/test_write12_flags.c \ + test-tool/test_write12_residuals.c \ test-tool/test_write16_simple.c \ test-tool/test_write16_beyond_eol.c \ test-tool/test_write16_0blocks.c \ test-tool/test_write16_wrprotect.c \ test-tool/test_write16_flags.c \ + test-tool/test_write16_residuals.c \ test-tool/test_writesame10_simple.c \ test-tool/test_writesame10_beyond_eol.c \ test-tool/test_writesame10_0blocks.c \ diff --git a/test-tool/iscsi-test-cu.c b/test-tool/iscsi-test-cu.c index 183f4b5..5a8a97e 100644 --- a/test-tool/iscsi-test-cu.c +++ b/test-tool/iscsi-test-cu.c @@ -282,6 +282,7 @@ static CU_TestInfo tests_write10[] = { { (char *)"testWrite10ZeroBlocks", test_write10_0blocks }, { (char *)"testWrite10WriteProtect", test_write10_wrprotect }, { (char *)"testWrite10Flags", test_write10_flags }, + { (char *)"testWrite10Residuals", test_write10_residuals }, CU_TEST_INFO_NULL }; @@ -291,6 +292,7 @@ static CU_TestInfo tests_write12[] = { { (char *)"testWrite12ZeroBlocks", test_write12_0blocks }, { (char *)"testWrite12WriteProtect", test_write12_wrprotect }, { (char *)"testWrite12Flags", test_write12_flags }, + { (char *)"testWrite12Residuals", test_write12_residuals }, CU_TEST_INFO_NULL }; @@ -300,6 +302,7 @@ static CU_TestInfo tests_write16[] = { { (char *)"testWrite16ZeroBlocks", test_write16_0blocks }, { (char *)"testWrite16WriteProtect", test_write16_wrprotect }, { (char *)"testWrite16Flags", test_write16_flags }, + { (char *)"testWrite16Residuals", test_write16_residuals }, CU_TEST_INFO_NULL }; diff --git a/test-tool/iscsi-test-cu.h b/test-tool/iscsi-test-cu.h index 22bab0d..a693045 100644 --- a/test-tool/iscsi-test-cu.h +++ b/test-tool/iscsi-test-cu.h @@ -173,18 +173,21 @@ void test_write10_beyond_eol(void); void test_write10_0blocks(void); void test_write10_wrprotect(void); void test_write10_flags(void); +void test_write10_residuals(void); void test_write12_simple(void); void test_write12_beyond_eol(void); void test_write12_0blocks(void); void test_write12_wrprotect(void); void test_write12_flags(void); +void test_write12_residuals(void); void test_write16_simple(void); void test_write16_beyond_eol(void); void test_write16_0blocks(void); void test_write16_wrprotect(void); void test_write16_flags(void); +void test_write16_residuals(void); void test_writesame10_simple(void); void test_writesame10_beyond_eol(void); diff --git a/test-tool/test_write10_residuals.c b/test-tool/test_write10_residuals.c new file mode 100644 index 0000000..add96fa --- /dev/null +++ b/test-tool/test_write10_residuals.c @@ -0,0 +1,299 @@ +/* + 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" + + +void +test_write10_residuals(void) +{ + struct scsi_task *task_ret; + unsigned char buf[10000]; + struct iscsi_data data; + int ret; + unsigned int i; + + logging(LOG_VERBOSE, LOG_BLANK_LINE); + logging(LOG_VERBOSE, "Test WRITE10 commands with residuals"); + logging(LOG_VERBOSE, "Block size is %zu", block_size); + + CHECK_FOR_DATALOSS; + CHECK_FOR_SBC; + + /* Try a write10 of 1 block but xferlength == 0 */ + task = malloc(sizeof(struct scsi_task)); + CU_ASSERT_PTR_NOT_NULL(task); + + memset(task, 0, sizeof(struct scsi_task)); + task->cdb[0] = SCSI_OPCODE_WRITE10; + task->cdb[8] = 1; + task->cdb_size = 10; + task->xfer_dir = SCSI_XFER_WRITE; + task->expxferlen = 0; + + /* + * we dont want autoreconnect since some targets will drop the session + * on this condition. + */ + iscsi_set_noautoreconnect(iscsic, 1); + + logging(LOG_VERBOSE, "Try writing one block but with iSCSI expected transfer length==0"); + + task_ret = iscsi_scsi_command_sync(iscsic, tgt_lun, task, NULL); + CU_ASSERT_PTR_NOT_NULL(task_ret); + CU_ASSERT_NOT_EQUAL(task->status, SCSI_STATUS_CANCELLED); /* XXX redundant? */ + + logging(LOG_VERBOSE, "Verify that the target returned SUCCESS"); + if (task->status != SCSI_STATUS_GOOD) { + logging(LOG_VERBOSE, "[FAILED] Target returned error %s", + iscsi_get_error(iscsic)); + } + CU_ASSERT_EQUAL(task->status, SCSI_STATUS_GOOD); + + logging(LOG_VERBOSE, "Verify residual overflow flag is set"); + if (task->residual_status != SCSI_RESIDUAL_OVERFLOW) { + logging(LOG_VERBOSE, "[FAILED] Target did not set residual " + "overflow flag"); + } + CU_ASSERT_EQUAL(task->residual_status, SCSI_RESIDUAL_OVERFLOW); + + logging(LOG_VERBOSE, "Verify we got %zu bytes of residual overflow", + block_size); + if (task->residual != block_size) { + logging(LOG_VERBOSE, "[FAILED] Target did not set correct " + "amount of residual. Expected %zu but got %zu.", + block_size, task->residual); + } + CU_ASSERT_EQUAL(task->residual, block_size); + scsi_free_scsi_task(task); + task = NULL; + + /* in case the previous test failed the session */ + iscsi_set_noautoreconnect(iscsic, 0); + + + logging(LOG_VERBOSE, "Try writing one block but with iSCSI expected transfer length==10000"); + task = malloc(sizeof(struct scsi_task)); + CU_ASSERT_PTR_NOT_NULL(task); + + memset(task, 0, sizeof(struct scsi_task)); + task->cdb[0] = SCSI_OPCODE_WRITE10; + task->cdb[8] = 1; + task->cdb_size = 10; + task->xfer_dir = SCSI_XFER_WRITE; + task->expxferlen = 10000; + + data.size = task->expxferlen; + data.data = &buf[0]; + task_ret = iscsi_scsi_command_sync(iscsic, tgt_lun, task, &data); + CU_ASSERT_PTR_NOT_NULL(task_ret); + + logging(LOG_VERBOSE, "Verify that the target returned SUCCESS"); + if (task->status != SCSI_STATUS_GOOD) { + logging(LOG_VERBOSE, "[FAILED] Target returned error %s", + iscsi_get_error(iscsic)); + } + CU_ASSERT_EQUAL(task->status, SCSI_STATUS_GOOD); + + logging(LOG_VERBOSE, "Verify residual underflow flag is set"); + if (task->residual_status != SCSI_RESIDUAL_UNDERFLOW) { + logging(LOG_VERBOSE, "[FAILED] Target did not set residual " + "underflow flag"); + } + CU_ASSERT_EQUAL(task->residual_status, SCSI_RESIDUAL_UNDERFLOW); + + logging(LOG_VERBOSE, "Verify we got %zu bytes of residual underflow", + 10000 - block_size); + if (task->residual != 10000 - block_size) { + logging(LOG_VERBOSE, "[FAILED] Target did not set correct " + "amount of residual. Expected %zu but got %zu.", + 10000 - block_size, task->residual); + } + CU_ASSERT_EQUAL(task->residual, 10000 - block_size); + scsi_free_scsi_task(task); + task = NULL; + + + logging(LOG_VERBOSE, "Try writing one block but with iSCSI expected transfer length==200"); + task = malloc(sizeof(struct scsi_task)); + CU_ASSERT_PTR_NOT_NULL(task); + + memset(task, 0, sizeof(struct scsi_task)); + task->cdb[0] = SCSI_OPCODE_WRITE10; + task->cdb[8] = 1; + task->cdb_size = 10; + task->xfer_dir = SCSI_XFER_WRITE; + task->expxferlen = 200; + + data.size = task->expxferlen; + data.data = &buf[0]; + task_ret = iscsi_scsi_command_sync(iscsic, tgt_lun, task, &data); + CU_ASSERT_PTR_NOT_NULL(task_ret); + + logging(LOG_VERBOSE, "Verify that the target returned SUCCESS"); + if (task->status != SCSI_STATUS_GOOD) { + logging(LOG_VERBOSE, "[FAILED] Target returned error %s", + iscsi_get_error(iscsic)); + } + CU_ASSERT_EQUAL(task->status, SCSI_STATUS_GOOD); + + logging(LOG_VERBOSE, "Verify residual overflow flag is set"); + if (task->residual_status != SCSI_RESIDUAL_OVERFLOW) { + logging(LOG_VERBOSE, "[FAILED] Target did not set residual " + "overflow flag"); + } + CU_ASSERT_EQUAL(task->residual_status, SCSI_RESIDUAL_OVERFLOW); + + logging(LOG_VERBOSE, "Verify we got %zu bytes of residual overflow", + block_size - 200); + if (task->residual != block_size - 200) { + logging(LOG_VERBOSE, "[FAILED] Target did not set correct " + "amount of residual. Expected %zu but got %zu.", + block_size - 200, task->residual); + } + CU_ASSERT_EQUAL(task->residual, block_size - 200); + + scsi_free_scsi_task(task); + task = NULL; + + + + logging(LOG_VERBOSE, "Try writing two blocks but iSCSI expected " + "transfer length==%zu (==one block)", block_size); + task = malloc(sizeof(struct scsi_task)); + CU_ASSERT_PTR_NOT_NULL(task); + + memset(task, 0, sizeof(struct scsi_task)); + task->cdb[0] = SCSI_OPCODE_WRITE10; + task->cdb[8] = 2; + task->cdb_size = 10; + task->xfer_dir = SCSI_XFER_WRITE; + task->expxferlen = block_size; + + data.size = task->expxferlen; + data.data = &buf[0]; + task_ret = iscsi_scsi_command_sync(iscsic, tgt_lun, task, &data); + CU_ASSERT_PTR_NOT_NULL(task_ret); + + logging(LOG_VERBOSE, "Verify that the target returned SUCCESS"); + if (task->status != SCSI_STATUS_GOOD) { + logging(LOG_VERBOSE, "[FAILED] Target returned error %s", + iscsi_get_error(iscsic)); + } + CU_ASSERT_EQUAL(task->status, SCSI_STATUS_GOOD); + + logging(LOG_VERBOSE, "Verify residual overflow flag is set"); + if (task->residual_status != SCSI_RESIDUAL_OVERFLOW) { + logging(LOG_VERBOSE, "[FAILED] Target did not set residual " + "overflow flag"); + } + CU_ASSERT_EQUAL(task->residual_status, SCSI_RESIDUAL_OVERFLOW); + + logging(LOG_VERBOSE, "Verify we got one block of residual overflow"); + if (task->residual != block_size) { + logging(LOG_VERBOSE, "[FAILED] Target did not set correct " + "amount of residual. Expected %zu but got %zu.", + block_size, task->residual); + } + CU_ASSERT_EQUAL(task->residual, block_size); + + scsi_free_scsi_task(task); + task = NULL; + + + + + logging(LOG_VERBOSE, "Write two blocks of 'a'"); + memset(buf, 'a', 10000); + ret = write10(iscsic, tgt_lun, 0, 2 * block_size, + block_size, 0, 0, 0, 0, 0, buf); + CU_ASSERT_EQUAL(ret, 0); + + logging(LOG_VERBOSE, "Write one block of 'b' but set iSCSI EDTL to 2 blocks."); + task = malloc(sizeof(struct scsi_task)); + CU_ASSERT_PTR_NOT_NULL(task); + + memset(buf, 'b', 10000); + + memset(task, 0, sizeof(struct scsi_task)); + task->cdb[0] = SCSI_OPCODE_WRITE10; + task->cdb[8] = 1; + task->cdb_size = 10; + task->xfer_dir = SCSI_XFER_WRITE; + task->expxferlen = 2 * block_size; + + data.size = task->expxferlen; + data.data = &buf[0]; + task_ret = iscsi_scsi_command_sync(iscsic, tgt_lun, task, &data); + CU_ASSERT_PTR_NOT_NULL(task_ret); + + logging(LOG_VERBOSE, "Verify that the target returned SUCCESS"); + if (task->status != SCSI_STATUS_GOOD) { + logging(LOG_VERBOSE, "[FAILED] Target returned error %s", + iscsi_get_error(iscsic)); + } + CU_ASSERT_EQUAL(task->status, SCSI_STATUS_GOOD); + + logging(LOG_VERBOSE, "Verify residual underflow flag is set"); + if (task->residual_status != SCSI_RESIDUAL_UNDERFLOW) { + logging(LOG_VERBOSE, "[FAILED] Target did not set residual " + "underflow flag"); + } + CU_ASSERT_EQUAL(task->residual_status, SCSI_RESIDUAL_UNDERFLOW); + + logging(LOG_VERBOSE, "Verify we got one block of residual underflow"); + if (task->residual != block_size) { + logging(LOG_VERBOSE, "[FAILED] Target did not set correct " + "amount of residual. Expected %zu but got %zu.", + block_size, task->residual); + } + CU_ASSERT_EQUAL(task->residual, block_size); + scsi_free_scsi_task(task); + task = NULL; + + logging(LOG_VERBOSE, "Read the two blocks"); + ret = read10(iscsic, tgt_lun, 0, 2* block_size, + block_size, 0, 0, 0, 0, 0, buf); + CU_ASSERT_EQUAL(ret, 0); + + logging(LOG_VERBOSE, "Verify that the first block was changed to 'b'"); + for (i = 0; i < block_size; i++) { + if (buf[i] != 'b') { + logging(LOG_NORMAL, "First block did not contain expected 'b'"); + CU_FAIL("Block was not written correctly"); + break; + } + } + + logging(LOG_VERBOSE, "Verify that the second block was NOT overwritten and still contains 'a'"); + for (i = block_size; i < 2 * block_size; i++) { + if (buf[i] != 'a') { + logging(LOG_NORMAL, "Second block was overwritten and no longer contain 'a'"); + CU_FAIL("Second block was incorrectly overwritten correctly"); + break; + } + } +} diff --git a/test-tool/test_write12_residuals.c b/test-tool/test_write12_residuals.c new file mode 100644 index 0000000..7d2ffef --- /dev/null +++ b/test-tool/test_write12_residuals.c @@ -0,0 +1,299 @@ +/* + 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" + + +void +test_write12_residuals(void) +{ + struct scsi_task *task_ret; + unsigned char buf[10000]; + struct iscsi_data data; + int ret; + unsigned int i; + + logging(LOG_VERBOSE, LOG_BLANK_LINE); + logging(LOG_VERBOSE, "Test WRITE12 commands with residuals"); + logging(LOG_VERBOSE, "Block size is %zu", block_size); + + CHECK_FOR_DATALOSS; + CHECK_FOR_SBC; + + /* Try a write12 of 1 block but xferlength == 0 */ + task = malloc(sizeof(struct scsi_task)); + CU_ASSERT_PTR_NOT_NULL(task); + + memset(task, 0, sizeof(struct scsi_task)); + task->cdb[0] = SCSI_OPCODE_WRITE12; + task->cdb[9] = 1; + task->cdb_size = 12; + task->xfer_dir = SCSI_XFER_WRITE; + task->expxferlen = 0; + + /* + * we dont want autoreconnect since some targets will drop the session + * on this condition. + */ + iscsi_set_noautoreconnect(iscsic, 1); + + logging(LOG_VERBOSE, "Try writing one block but with iSCSI expected transfer length==0"); + + task_ret = iscsi_scsi_command_sync(iscsic, tgt_lun, task, NULL); + CU_ASSERT_PTR_NOT_NULL(task_ret); + CU_ASSERT_NOT_EQUAL(task->status, SCSI_STATUS_CANCELLED); /* XXX redundant? */ + + logging(LOG_VERBOSE, "Verify that the target returned SUCCESS"); + if (task->status != SCSI_STATUS_GOOD) { + logging(LOG_VERBOSE, "[FAILED] Target returned error %s", + iscsi_get_error(iscsic)); + } + CU_ASSERT_EQUAL(task->status, SCSI_STATUS_GOOD); + + logging(LOG_VERBOSE, "Verify residual overflow flag is set"); + if (task->residual_status != SCSI_RESIDUAL_OVERFLOW) { + logging(LOG_VERBOSE, "[FAILED] Target did not set residual " + "overflow flag"); + } + CU_ASSERT_EQUAL(task->residual_status, SCSI_RESIDUAL_OVERFLOW); + + logging(LOG_VERBOSE, "Verify we got %zu bytes of residual overflow", + block_size); + if (task->residual != block_size) { + logging(LOG_VERBOSE, "[FAILED] Target did not set correct " + "amount of residual. Expected %zu but got %zu.", + block_size, task->residual); + } + CU_ASSERT_EQUAL(task->residual, block_size); + scsi_free_scsi_task(task); + task = NULL; + + /* in case the previous test failed the session */ + iscsi_set_noautoreconnect(iscsic, 0); + + + logging(LOG_VERBOSE, "Try writing one block but with iSCSI expected transfer length==10000"); + task = malloc(sizeof(struct scsi_task)); + CU_ASSERT_PTR_NOT_NULL(task); + + memset(task, 0, sizeof(struct scsi_task)); + task->cdb[0] = SCSI_OPCODE_WRITE12; + task->cdb[9] = 1; + task->cdb_size = 12; + task->xfer_dir = SCSI_XFER_WRITE; + task->expxferlen = 10000; + + data.size = task->expxferlen; + data.data = &buf[0]; + task_ret = iscsi_scsi_command_sync(iscsic, tgt_lun, task, &data); + CU_ASSERT_PTR_NOT_NULL(task_ret); + + logging(LOG_VERBOSE, "Verify that the target returned SUCCESS"); + if (task->status != SCSI_STATUS_GOOD) { + logging(LOG_VERBOSE, "[FAILED] Target returned error %s", + iscsi_get_error(iscsic)); + } + CU_ASSERT_EQUAL(task->status, SCSI_STATUS_GOOD); + + logging(LOG_VERBOSE, "Verify residual underflow flag is set"); + if (task->residual_status != SCSI_RESIDUAL_UNDERFLOW) { + logging(LOG_VERBOSE, "[FAILED] Target did not set residual " + "underflow flag"); + } + CU_ASSERT_EQUAL(task->residual_status, SCSI_RESIDUAL_UNDERFLOW); + + logging(LOG_VERBOSE, "Verify we got %zu bytes of residual underflow", + 10000 - block_size); + if (task->residual != 10000 - block_size) { + logging(LOG_VERBOSE, "[FAILED] Target did not set correct " + "amount of residual. Expected %zu but got %zu.", + 10000 - block_size, task->residual); + } + CU_ASSERT_EQUAL(task->residual, 10000 - block_size); + scsi_free_scsi_task(task); + task = NULL; + + + logging(LOG_VERBOSE, "Try writing one block but with iSCSI expected transfer length==200"); + task = malloc(sizeof(struct scsi_task)); + CU_ASSERT_PTR_NOT_NULL(task); + + memset(task, 0, sizeof(struct scsi_task)); + task->cdb[0] = SCSI_OPCODE_WRITE12; + task->cdb[9] = 1; + task->cdb_size = 12; + task->xfer_dir = SCSI_XFER_WRITE; + task->expxferlen = 200; + + data.size = task->expxferlen; + data.data = &buf[0]; + task_ret = iscsi_scsi_command_sync(iscsic, tgt_lun, task, &data); + CU_ASSERT_PTR_NOT_NULL(task_ret); + + logging(LOG_VERBOSE, "Verify that the target returned SUCCESS"); + if (task->status != SCSI_STATUS_GOOD) { + logging(LOG_VERBOSE, "[FAILED] Target returned error %s", + iscsi_get_error(iscsic)); + } + CU_ASSERT_EQUAL(task->status, SCSI_STATUS_GOOD); + + logging(LOG_VERBOSE, "Verify residual overflow flag is set"); + if (task->residual_status != SCSI_RESIDUAL_OVERFLOW) { + logging(LOG_VERBOSE, "[FAILED] Target did not set residual " + "overflow flag"); + } + CU_ASSERT_EQUAL(task->residual_status, SCSI_RESIDUAL_OVERFLOW); + + logging(LOG_VERBOSE, "Verify we got %zu bytes of residual overflow", + block_size - 200); + if (task->residual != block_size - 200) { + logging(LOG_VERBOSE, "[FAILED] Target did not set correct " + "amount of residual. Expected %zu but got %zu.", + block_size - 200, task->residual); + } + CU_ASSERT_EQUAL(task->residual, block_size - 200); + + scsi_free_scsi_task(task); + task = NULL; + + + + logging(LOG_VERBOSE, "Try writing two blocks but iSCSI expected " + "transfer length==%zu (==one block)", block_size); + task = malloc(sizeof(struct scsi_task)); + CU_ASSERT_PTR_NOT_NULL(task); + + memset(task, 0, sizeof(struct scsi_task)); + task->cdb[0] = SCSI_OPCODE_WRITE12; + task->cdb[9] = 2; + task->cdb_size = 12; + task->xfer_dir = SCSI_XFER_WRITE; + task->expxferlen = block_size; + + data.size = task->expxferlen; + data.data = &buf[0]; + task_ret = iscsi_scsi_command_sync(iscsic, tgt_lun, task, &data); + CU_ASSERT_PTR_NOT_NULL(task_ret); + + logging(LOG_VERBOSE, "Verify that the target returned SUCCESS"); + if (task->status != SCSI_STATUS_GOOD) { + logging(LOG_VERBOSE, "[FAILED] Target returned error %s", + iscsi_get_error(iscsic)); + } + CU_ASSERT_EQUAL(task->status, SCSI_STATUS_GOOD); + + logging(LOG_VERBOSE, "Verify residual overflow flag is set"); + if (task->residual_status != SCSI_RESIDUAL_OVERFLOW) { + logging(LOG_VERBOSE, "[FAILED] Target did not set residual " + "overflow flag"); + } + CU_ASSERT_EQUAL(task->residual_status, SCSI_RESIDUAL_OVERFLOW); + + logging(LOG_VERBOSE, "Verify we got one block of residual overflow"); + if (task->residual != block_size) { + logging(LOG_VERBOSE, "[FAILED] Target did not set correct " + "amount of residual. Expected %zu but got %zu.", + block_size, task->residual); + } + CU_ASSERT_EQUAL(task->residual, block_size); + + scsi_free_scsi_task(task); + task = NULL; + + + + + logging(LOG_VERBOSE, "Write two blocks of 'a'"); + memset(buf, 'a', 10000); + ret = write12(iscsic, tgt_lun, 0, 2 * block_size, + block_size, 0, 0, 0, 0, 0, buf); + CU_ASSERT_EQUAL(ret, 0); + + logging(LOG_VERBOSE, "Write one block of 'b' but set iSCSI EDTL to 2 blocks."); + task = malloc(sizeof(struct scsi_task)); + CU_ASSERT_PTR_NOT_NULL(task); + + memset(buf, 'b', 10000); + + memset(task, 0, sizeof(struct scsi_task)); + task->cdb[0] = SCSI_OPCODE_WRITE12; + task->cdb[9] = 1; + task->cdb_size = 12; + task->xfer_dir = SCSI_XFER_WRITE; + task->expxferlen = 2 * block_size; + + data.size = task->expxferlen; + data.data = &buf[0]; + task_ret = iscsi_scsi_command_sync(iscsic, tgt_lun, task, &data); + CU_ASSERT_PTR_NOT_NULL(task_ret); + + logging(LOG_VERBOSE, "Verify that the target returned SUCCESS"); + if (task->status != SCSI_STATUS_GOOD) { + logging(LOG_VERBOSE, "[FAILED] Target returned error %s", + iscsi_get_error(iscsic)); + } + CU_ASSERT_EQUAL(task->status, SCSI_STATUS_GOOD); + + logging(LOG_VERBOSE, "Verify residual underflow flag is set"); + if (task->residual_status != SCSI_RESIDUAL_UNDERFLOW) { + logging(LOG_VERBOSE, "[FAILED] Target did not set residual " + "underflow flag"); + } + CU_ASSERT_EQUAL(task->residual_status, SCSI_RESIDUAL_UNDERFLOW); + + logging(LOG_VERBOSE, "Verify we got one block of residual underflow"); + if (task->residual != block_size) { + logging(LOG_VERBOSE, "[FAILED] Target did not set correct " + "amount of residual. Expected %zu but got %zu.", + block_size, task->residual); + } + CU_ASSERT_EQUAL(task->residual, block_size); + scsi_free_scsi_task(task); + task = NULL; + + logging(LOG_VERBOSE, "Read the two blocks"); + ret = read12(iscsic, tgt_lun, 0, 2* block_size, + block_size, 0, 0, 0, 0, 0, buf); + CU_ASSERT_EQUAL(ret, 0); + + logging(LOG_VERBOSE, "Verify that the first block was changed to 'b'"); + for (i = 0; i < block_size; i++) { + if (buf[i] != 'b') { + logging(LOG_NORMAL, "First block did not contain expected 'b'"); + CU_FAIL("Block was not written correctly"); + break; + } + } + + logging(LOG_VERBOSE, "Verify that the second block was NOT overwritten and still contains 'a'"); + for (i = block_size; i < 2 * block_size; i++) { + if (buf[i] != 'a') { + logging(LOG_NORMAL, "Second block was overwritten and no longer contain 'a'"); + CU_FAIL("Second block was incorrectly overwritten correctly"); + break; + } + } +} diff --git a/test-tool/test_write16_residuals.c b/test-tool/test_write16_residuals.c new file mode 100644 index 0000000..fbe69b8 --- /dev/null +++ b/test-tool/test_write16_residuals.c @@ -0,0 +1,299 @@ +/* + 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" + + +void +test_write16_residuals(void) +{ + struct scsi_task *task_ret; + unsigned char buf[10000]; + struct iscsi_data data; + int ret; + unsigned int i; + + logging(LOG_VERBOSE, LOG_BLANK_LINE); + logging(LOG_VERBOSE, "Test WRITE16 commands with residuals"); + logging(LOG_VERBOSE, "Block size is %zu", block_size); + + CHECK_FOR_DATALOSS; + CHECK_FOR_SBC; + + /* Try a write16 of 1 block but xferlength == 0 */ + task = malloc(sizeof(struct scsi_task)); + CU_ASSERT_PTR_NOT_NULL(task); + + memset(task, 0, sizeof(struct scsi_task)); + task->cdb[0] = SCSI_OPCODE_WRITE16; + task->cdb[13] = 1; + task->cdb_size = 16; + task->xfer_dir = SCSI_XFER_WRITE; + task->expxferlen = 0; + + /* + * we dont want autoreconnect since some targets will drop the session + * on this condition. + */ + iscsi_set_noautoreconnect(iscsic, 1); + + logging(LOG_VERBOSE, "Try writing one block but with iSCSI expected transfer length==0"); + + task_ret = iscsi_scsi_command_sync(iscsic, tgt_lun, task, NULL); + CU_ASSERT_PTR_NOT_NULL(task_ret); + CU_ASSERT_NOT_EQUAL(task->status, SCSI_STATUS_CANCELLED); /* XXX redundant? */ + + logging(LOG_VERBOSE, "Verify that the target returned SUCCESS"); + if (task->status != SCSI_STATUS_GOOD) { + logging(LOG_VERBOSE, "[FAILED] Target returned error %s", + iscsi_get_error(iscsic)); + } + CU_ASSERT_EQUAL(task->status, SCSI_STATUS_GOOD); + + logging(LOG_VERBOSE, "Verify residual overflow flag is set"); + if (task->residual_status != SCSI_RESIDUAL_OVERFLOW) { + logging(LOG_VERBOSE, "[FAILED] Target did not set residual " + "overflow flag"); + } + CU_ASSERT_EQUAL(task->residual_status, SCSI_RESIDUAL_OVERFLOW); + + logging(LOG_VERBOSE, "Verify we got %zu bytes of residual overflow", + block_size); + if (task->residual != block_size) { + logging(LOG_VERBOSE, "[FAILED] Target did not set correct " + "amount of residual. Expected %zu but got %zu.", + block_size, task->residual); + } + CU_ASSERT_EQUAL(task->residual, block_size); + scsi_free_scsi_task(task); + task = NULL; + + /* in case the previous test failed the session */ + iscsi_set_noautoreconnect(iscsic, 0); + + + logging(LOG_VERBOSE, "Try writing one block but with iSCSI expected transfer length==10000"); + task = malloc(sizeof(struct scsi_task)); + CU_ASSERT_PTR_NOT_NULL(task); + + memset(task, 0, sizeof(struct scsi_task)); + task->cdb[0] = SCSI_OPCODE_WRITE16; + task->cdb[13] = 1; + task->cdb_size = 16; + task->xfer_dir = SCSI_XFER_WRITE; + task->expxferlen = 10000; + + data.size = task->expxferlen; + data.data = &buf[0]; + task_ret = iscsi_scsi_command_sync(iscsic, tgt_lun, task, &data); + CU_ASSERT_PTR_NOT_NULL(task_ret); + + logging(LOG_VERBOSE, "Verify that the target returned SUCCESS"); + if (task->status != SCSI_STATUS_GOOD) { + logging(LOG_VERBOSE, "[FAILED] Target returned error %s", + iscsi_get_error(iscsic)); + } + CU_ASSERT_EQUAL(task->status, SCSI_STATUS_GOOD); + + logging(LOG_VERBOSE, "Verify residual underflow flag is set"); + if (task->residual_status != SCSI_RESIDUAL_UNDERFLOW) { + logging(LOG_VERBOSE, "[FAILED] Target did not set residual " + "underflow flag"); + } + CU_ASSERT_EQUAL(task->residual_status, SCSI_RESIDUAL_UNDERFLOW); + + logging(LOG_VERBOSE, "Verify we got %zu bytes of residual underflow", + 10000 - block_size); + if (task->residual != 10000 - block_size) { + logging(LOG_VERBOSE, "[FAILED] Target did not set correct " + "amount of residual. Expected %zu but got %zu.", + 10000 - block_size, task->residual); + } + CU_ASSERT_EQUAL(task->residual, 10000 - block_size); + scsi_free_scsi_task(task); + task = NULL; + + + logging(LOG_VERBOSE, "Try writing one block but with iSCSI expected transfer length==200"); + task = malloc(sizeof(struct scsi_task)); + CU_ASSERT_PTR_NOT_NULL(task); + + memset(task, 0, sizeof(struct scsi_task)); + task->cdb[0] = SCSI_OPCODE_WRITE16; + task->cdb[13] = 1; + task->cdb_size = 16; + task->xfer_dir = SCSI_XFER_WRITE; + task->expxferlen = 200; + + data.size = task->expxferlen; + data.data = &buf[0]; + task_ret = iscsi_scsi_command_sync(iscsic, tgt_lun, task, &data); + CU_ASSERT_PTR_NOT_NULL(task_ret); + + logging(LOG_VERBOSE, "Verify that the target returned SUCCESS"); + if (task->status != SCSI_STATUS_GOOD) { + logging(LOG_VERBOSE, "[FAILED] Target returned error %s", + iscsi_get_error(iscsic)); + } + CU_ASSERT_EQUAL(task->status, SCSI_STATUS_GOOD); + + logging(LOG_VERBOSE, "Verify residual overflow flag is set"); + if (task->residual_status != SCSI_RESIDUAL_OVERFLOW) { + logging(LOG_VERBOSE, "[FAILED] Target did not set residual " + "overflow flag"); + } + CU_ASSERT_EQUAL(task->residual_status, SCSI_RESIDUAL_OVERFLOW); + + logging(LOG_VERBOSE, "Verify we got %zu bytes of residual overflow", + block_size - 200); + if (task->residual != block_size - 200) { + logging(LOG_VERBOSE, "[FAILED] Target did not set correct " + "amount of residual. Expected %zu but got %zu.", + block_size - 200, task->residual); + } + CU_ASSERT_EQUAL(task->residual, block_size - 200); + + scsi_free_scsi_task(task); + task = NULL; + + + + logging(LOG_VERBOSE, "Try writing two blocks but iSCSI expected " + "transfer length==%zu (==one block)", block_size); + task = malloc(sizeof(struct scsi_task)); + CU_ASSERT_PTR_NOT_NULL(task); + + memset(task, 0, sizeof(struct scsi_task)); + task->cdb[0] = SCSI_OPCODE_WRITE16; + task->cdb[13] = 2; + task->cdb_size = 16; + task->xfer_dir = SCSI_XFER_WRITE; + task->expxferlen = block_size; + + data.size = task->expxferlen; + data.data = &buf[0]; + task_ret = iscsi_scsi_command_sync(iscsic, tgt_lun, task, &data); + CU_ASSERT_PTR_NOT_NULL(task_ret); + + logging(LOG_VERBOSE, "Verify that the target returned SUCCESS"); + if (task->status != SCSI_STATUS_GOOD) { + logging(LOG_VERBOSE, "[FAILED] Target returned error %s", + iscsi_get_error(iscsic)); + } + CU_ASSERT_EQUAL(task->status, SCSI_STATUS_GOOD); + + logging(LOG_VERBOSE, "Verify residual overflow flag is set"); + if (task->residual_status != SCSI_RESIDUAL_OVERFLOW) { + logging(LOG_VERBOSE, "[FAILED] Target did not set residual " + "overflow flag"); + } + CU_ASSERT_EQUAL(task->residual_status, SCSI_RESIDUAL_OVERFLOW); + + logging(LOG_VERBOSE, "Verify we got one block of residual overflow"); + if (task->residual != block_size) { + logging(LOG_VERBOSE, "[FAILED] Target did not set correct " + "amount of residual. Expected %zu but got %zu.", + block_size, task->residual); + } + CU_ASSERT_EQUAL(task->residual, block_size); + + scsi_free_scsi_task(task); + task = NULL; + + + + + logging(LOG_VERBOSE, "Write two blocks of 'a'"); + memset(buf, 'a', 10000); + ret = write16(iscsic, tgt_lun, 0, 2 * block_size, + block_size, 0, 0, 0, 0, 0, buf); + CU_ASSERT_EQUAL(ret, 0); + + logging(LOG_VERBOSE, "Write one block of 'b' but set iSCSI EDTL to 2 blocks."); + task = malloc(sizeof(struct scsi_task)); + CU_ASSERT_PTR_NOT_NULL(task); + + memset(buf, 'b', 10000); + + memset(task, 0, sizeof(struct scsi_task)); + task->cdb[0] = SCSI_OPCODE_WRITE16; + task->cdb[13] = 1; + task->cdb_size = 16; + task->xfer_dir = SCSI_XFER_WRITE; + task->expxferlen = 2 * block_size; + + data.size = task->expxferlen; + data.data = &buf[0]; + task_ret = iscsi_scsi_command_sync(iscsic, tgt_lun, task, &data); + CU_ASSERT_PTR_NOT_NULL(task_ret); + + logging(LOG_VERBOSE, "Verify that the target returned SUCCESS"); + if (task->status != SCSI_STATUS_GOOD) { + logging(LOG_VERBOSE, "[FAILED] Target returned error %s", + iscsi_get_error(iscsic)); + } + CU_ASSERT_EQUAL(task->status, SCSI_STATUS_GOOD); + + logging(LOG_VERBOSE, "Verify residual underflow flag is set"); + if (task->residual_status != SCSI_RESIDUAL_UNDERFLOW) { + logging(LOG_VERBOSE, "[FAILED] Target did not set residual " + "underflow flag"); + } + CU_ASSERT_EQUAL(task->residual_status, SCSI_RESIDUAL_UNDERFLOW); + + logging(LOG_VERBOSE, "Verify we got one block of residual underflow"); + if (task->residual != block_size) { + logging(LOG_VERBOSE, "[FAILED] Target did not set correct " + "amount of residual. Expected %zu but got %zu.", + block_size, task->residual); + } + CU_ASSERT_EQUAL(task->residual, block_size); + scsi_free_scsi_task(task); + task = NULL; + + logging(LOG_VERBOSE, "Read the two blocks"); + ret = read16(iscsic, tgt_lun, 0, 2* block_size, + block_size, 0, 0, 0, 0, 0, buf); + CU_ASSERT_EQUAL(ret, 0); + + logging(LOG_VERBOSE, "Verify that the first block was changed to 'b'"); + for (i = 0; i < block_size; i++) { + if (buf[i] != 'b') { + logging(LOG_NORMAL, "First block did not contain expected 'b'"); + CU_FAIL("Block was not written correctly"); + break; + } + } + + logging(LOG_VERBOSE, "Verify that the second block was NOT overwritten and still contains 'a'"); + for (i = block_size; i < 2 * block_size; i++) { + if (buf[i] != 'a') { + logging(LOG_NORMAL, "Second block was overwritten and no longer contain 'a'"); + CU_FAIL("Second block was incorrectly overwritten correctly"); + break; + } + } +}