void
test_preventallow_warm_reset(void)
{
	int ret;

	CHECK_FOR_SBC;
	CHECK_FOR_REMOVABLE;

	logging(LOG_VERBOSE, LOG_BLANK_LINE);
	logging(LOG_VERBOSE, "Test that Target Warm Reset clears PREVENT MEDIUM REMOVAL");

	logging(LOG_VERBOSE, "Set the PREVENT flag");
	ret = preventallow(iscsic, tgt_lun, 1);
	CU_ASSERT_EQUAL(ret, 0);

	logging(LOG_VERBOSE, "Try to eject the medium");
	ret = startstopunit_preventremoval(iscsic, tgt_lun, 0, 0, 0, 0, 1, 0);
	CU_ASSERT_EQUAL(ret, 0);

	logging(LOG_VERBOSE, "Verify we can still access the media.");
	ret = testunitready(iscsic, tgt_lun);
	CU_ASSERT_EQUAL(ret, 0);

	
	logging(LOG_VERBOSE, "Perform warm reset on target");
	ret = iscsi_task_mgmt_target_warm_reset_sync(iscsic);
	CU_ASSERT_EQUAL(ret, 0);
	logging(LOG_VERBOSE, "Wait until all unit attentions clear");
	while (testunitready(iscsic, tgt_lun) != 0);


	logging(LOG_VERBOSE, "Try to eject the medium");
	ret = startstopunit(iscsic, tgt_lun, 0, 0, 0, 0, 1, 0);
	CU_ASSERT_EQUAL(ret, 0);

	logging(LOG_VERBOSE, "Verify we can not access the media.");
	ret = testunitready_nomedium(iscsic, tgt_lun);
	CU_ASSERT_EQUAL(ret, 0);

	logging(LOG_VERBOSE, "Load the medium");
	ret = startstopunit(iscsic, tgt_lun, 0, 0, 0, 0, 1, 0);
	CU_ASSERT_EQUAL(ret, 0);


	logging(LOG_VERBOSE, "Clear PREVENT and load medium in case target failed");
	logging(LOG_VERBOSE, "Test we can clear PREVENT flag");
	ret = preventallow(iscsic, tgt_lun, 0);
	CU_ASSERT_EQUAL(ret, 0);

	logging(LOG_VERBOSE, "Load the medium");
	ret = startstopunit(iscsic, tgt_lun, 0, 0, 0, 0, 1, 1);
	CU_ASSERT_EQUAL(ret, 0);

}
void
test_preventallow_2_itnexuses(void)
{
	int ret;
	struct scsi_device sd2;

	CHECK_FOR_SBC;
	CHECK_FOR_REMOVABLE;

	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");
	ret = preventallow(sd, 1);
	CU_ASSERT_EQUAL(ret, 0);

	logging(LOG_VERBOSE, "Try to eject the medium");
	ret = startstopunit(sd, 0, 0, 0, 0, 1, 0,
			    EXPECT_REMOVAL_PREVENTED);
	CU_ASSERT_EQUAL(ret, 0);

	logging(LOG_VERBOSE, "Verify we can still access the media.");
	ret = testunitready(sd,
			    EXPECT_STATUS_GOOD);
	CU_ASSERT_EQUAL(ret, 0);

	logging(LOG_VERBOSE, "Create a second connection to the target");
	memset(&sd2, 0, sizeof(sd2));
	sd2.iscsi_ctx = iscsi_context_login(initiatorname2, sd->iscsi_url, &sd->iscsi_lun);
	if (sd2.iscsi_ctx == NULL) {
		logging(LOG_VERBOSE, "Failed to login to target");
		return;
	}

	logging(LOG_VERBOSE, "Try to eject the medium on the second connection");
	ret = startstopunit(&sd2, 0, 0, 0, 0, 1, 0,
			    EXPECT_REMOVAL_PREVENTED);
	CU_ASSERT_EQUAL(ret, 0);


	logging(LOG_VERBOSE, "Logout the second connection from target");
	iscsi_logout_sync(sd2.iscsi_ctx);
	iscsi_destroy_context(sd2.iscsi_ctx);



	logging(LOG_VERBOSE, "Clear PREVENT and load medium in case target failed");
	logging(LOG_VERBOSE, "Test we can clear PREVENT flag");
	ret = preventallow(sd, 0);
	CU_ASSERT_EQUAL(ret, 0);

	logging(LOG_VERBOSE, "Load the medium");
	ret = startstopunit(sd, 0, 0, 0, 0, 1, 1,
			    EXPECT_STATUS_GOOD);
	CU_ASSERT_EQUAL(ret, 0);

}
Esempio n. 3
0
void
test_nomedia_sbc(void)
{
	int ret;
	unsigned char buf[4096];
	struct unmap_list list[1];

	CHECK_FOR_SBC;

	logging(LOG_VERBOSE, LOG_BLANK_LINE);
	logging(LOG_VERBOSE, "Test that Medium commands fail when medium is ejected on SBC devices");

	if (!inq->rmb) {
		logging(LOG_VERBOSE, "[SKIPPED] LUN is not removable. "
			"Skipping test.");
		return;
	}

	logging(LOG_VERBOSE, "Eject the medium.");
	ret = startstopunit(iscsic, tgt_lun, 1, 0, 0, 0, 1, 0);
	CU_ASSERT_EQUAL(ret, 0);

	logging(LOG_VERBOSE, "Test TESTUNITREADY when medium is ejected.");
	ret = testunitready_nomedium(iscsic, tgt_lun);
	CU_ASSERT_EQUAL(ret, 0);

	logging(LOG_VERBOSE, "Test SYNCHRONIZECACHE10 when medium is ejected.");
	ret = synchronizecache10_nomedium(iscsic, tgt_lun, 0, 1, 1, 1);
	if (ret == -2) {
		logging(LOG_NORMAL, "[SKIPPED] target does not support "
			"SYNCHRONIZECACHE10");
	} else {
		CU_ASSERT_EQUAL(ret, 0);
	}

	logging(LOG_VERBOSE, "Test SYNCHRONIZECACHE16 when medium is ejected.");
	ret = synchronizecache16_nomedium(iscsic, tgt_lun, 0, 1, 1, 1);
	if (ret == -2) {
		logging(LOG_NORMAL, "[SKIPPED] target does not support "
			"SYNCHRONIZECACHE16");
	} else {
		CU_ASSERT_EQUAL(ret, 0);
	}

	logging(LOG_VERBOSE, "Test READ10 when medium is ejected.");
	ret = read10_nomedium(iscsic, tgt_lun, 0, block_size, block_size,
			      0, 0, 0, 0, 0, NULL);
	CU_ASSERT_EQUAL(ret, 0);

	logging(LOG_VERBOSE, "Test READ12 when medium is ejected.");
	ret = read12_nomedium(iscsic, tgt_lun, 0, block_size, block_size,
			      0, 0, 0, 0, 0, NULL);
	CU_ASSERT_EQUAL(ret, 0);

	logging(LOG_VERBOSE, "Test READ16 when medium is ejected.");
	ret = read16_nomedium(iscsic, tgt_lun, 0, block_size, block_size,
			      0, 0, 0, 0, 0, NULL);
	CU_ASSERT_EQUAL(ret, 0);

	logging(LOG_VERBOSE, "Test READCAPACITY10 when medium is ejected.");
	ret = readcapacity10_nomedium(iscsic, tgt_lun, 0, 0);
	CU_ASSERT_EQUAL(ret, 0);

	logging(LOG_VERBOSE, "Test READCAPACITY16 when medium is ejected.");
	ret = readcapacity16_nomedium(iscsic, tgt_lun, 15);
	if (ret == -2) {
		if (sbc3_support) {
			logging(LOG_NORMAL, "[FAILED] READCAPACITY16 is not available but the device claims SBC-3 support.");
			CU_FAIL("READCAPACITY16 failed but the device claims SBC-3 support.");
		} else {
			logging(LOG_NORMAL, "[SKIPPED] READCAPACITY16 is not implemented on this target and it does not claim SBC-3 support.");
		}
	} else {
		CU_ASSERT_EQUAL(ret, 0);
	}

	logging(LOG_VERBOSE, "Test GET_LBA_STATUS when medium is ejected.");
	ret = get_lba_status_nomedium(iscsic, tgt_lun, 0, 24);
	if (ret == -2) {
		logging(LOG_NORMAL, "[SKIPPED] target does not support "
			"GET_LBA_STATUS");
	} else {
		CU_ASSERT_EQUAL(ret, 0);
	}

	logging(LOG_VERBOSE, "Test PREFETCH10 when medium is ejected.");
	ret = prefetch10_nomedium(iscsic, tgt_lun, 0, 1, 1, 0);
	if (ret == -2) {
		logging(LOG_NORMAL, "[SKIPPED] target does not support "
			"PREFETCH10");
	} else {
		CU_ASSERT_EQUAL(ret, 0);
	}

	logging(LOG_VERBOSE, "Test PREFETCH16 when medium is ejected.");
	ret = prefetch16_nomedium(iscsic, tgt_lun, 0, 1, 1, 0);
	if (ret == -2) {
		logging(LOG_NORMAL, "[SKIPPED] target does not support "
			"PREFETCH16");
	} else {
		CU_ASSERT_EQUAL(ret, 0);
	}

	logging(LOG_VERBOSE, "Test VERIFY10 when medium is ejected.");
	ret = verify10_nomedium(iscsic, tgt_lun, 0, block_size, block_size,
				0, 0, 1, buf);
	if (ret == -2) {
		logging(LOG_NORMAL, "[SKIPPED] target does not support "
			"VERIFY10");
	} else {
		CU_ASSERT_EQUAL(ret, 0);
	}

	logging(LOG_VERBOSE, "Test VERIFY12 when medium is ejected.");
	ret = verify12_nomedium(iscsic, tgt_lun, 0, block_size, block_size,
				0, 0, 1, buf);
	if (ret == -2) {
		logging(LOG_NORMAL, "[SKIPPED] target does not support "
			"VERIFY102");
	} else {
		CU_ASSERT_EQUAL(ret, 0);
	}

	logging(LOG_VERBOSE, "Test VERIFY16 when medium is ejected.");
	ret = verify16_nomedium(iscsic, tgt_lun, 0, block_size, block_size,
				0, 0, 1, buf);
	if (ret == -2) {
		logging(LOG_NORMAL, "[SKIPPED] target does not support "
			"VERIFY16");
	} else {
		CU_ASSERT_EQUAL(ret, 0);
	}

	if (!data_loss) {
		logging(LOG_VERBOSE, "[SKIPPING] Dataloss flag not set. Skipping test for WRITE commands");
		goto finished;
	}

	logging(LOG_VERBOSE, "Test WRITE10 when medium is ejected.");
	ret = write10_nomedium(iscsic, tgt_lun, 0, block_size, block_size,
			       0, 0, 0, 0, 0, buf);
	CU_ASSERT_EQUAL(ret, 0);

	logging(LOG_VERBOSE, "Test WRITE12 when medium is ejected.");
	ret = write12_nomedium(iscsic, tgt_lun, 0, block_size, block_size,
			       0, 0, 0, 0, 0, buf);
	CU_ASSERT_EQUAL(ret, 0);

	logging(LOG_VERBOSE, "Test WRITE16 when medium is ejected.");
	ret = write16_nomedium(iscsic, tgt_lun, 0, block_size, block_size,
			       0, 0, 0, 0, 0, buf);
	CU_ASSERT_EQUAL(ret, 0);

	logging(LOG_VERBOSE, "Test WRITEVERIFY10 when medium is ejected.");
	ret = writeverify10_nomedium(iscsic, tgt_lun, 0, block_size, block_size,
			       0, 0, 0, 0, buf);
	if (ret == -2) {
		logging(LOG_NORMAL, "[SKIPPED] target does not support "
			"WRITEVERIFY10");
	} else {
		CU_ASSERT_EQUAL(ret, 0);
	}

	logging(LOG_VERBOSE, "Test WRITEVERIFY12 when medium is ejected.");
	ret = writeverify12_nomedium(iscsic, tgt_lun, 0, block_size, block_size,
			       0, 0, 0, 0, buf);
	if (ret == -2) {
		logging(LOG_NORMAL, "[SKIPPED] target does not support "
			"WRITEVERIFY12");
	} else {
		CU_ASSERT_EQUAL(ret, 0);
	}

	logging(LOG_VERBOSE, "Test WRITEVERIFY16 when medium is ejected.");
	ret = writeverify16_nomedium(iscsic, tgt_lun, 0, block_size, block_size,
			       0, 0, 0, 0, buf);
	if (ret == -2) {
		logging(LOG_NORMAL, "[SKIPPED] target does not support "
			"WRITEVERIFY16");
	} else {
		CU_ASSERT_EQUAL(ret, 0);
	}

	logging(LOG_VERBOSE, "Test ORWRITE when medium is ejected.");
	ret = orwrite_nomedium(iscsic, tgt_lun, 0, block_size, block_size,
			       0, 0, 0, 0, 0, buf);
	if (ret == -2) {
		logging(LOG_NORMAL, "[SKIPPED] target does not support "
			"ORWRITE");
	} else {
		CU_ASSERT_EQUAL(ret, 0);
	}

	logging(LOG_VERBOSE, "Test COMPAREWRITE when medium is ejected.");
	logging(LOG_VERBOSE, "[SKIPPED] Test not implemented yet");

	logging(LOG_VERBOSE, "Test WRITESAME10 when medium is ejected.");
	ret = writesame10_nomedium(iscsic, tgt_lun, 0, block_size,
				   1, 0, 0, 0, 0, buf);
	if (ret == -2) {
		logging(LOG_NORMAL, "[SKIPPED] target does not support "
			"WRITESAME10");
	} else {
		CU_ASSERT_EQUAL(ret, 0);
	}

	logging(LOG_VERBOSE, "Test WRITESAME16 when medium is ejected.");
	ret = writesame16_nomedium(iscsic, tgt_lun, 0, block_size,
				   1, 0, 0, 0, 0, buf);
	if (ret == -2) {
		logging(LOG_NORMAL, "[SKIPPED] target does not support "
			"WRITESAME16");
	} else {
		CU_ASSERT_EQUAL(ret, 0);
	}

	logging(LOG_VERBOSE, "Test UNMAP when medium is ejected.");
	list[0].lba = 0;
	list[0].num = lbppb;
	ret = unmap_nomedium(iscsic, tgt_lun, 0, list, 1);
	if (ret == -2) {
		logging(LOG_NORMAL, "[SKIPPED] target does not support "
			"UNMAP");
	} else {
		CU_ASSERT_EQUAL(ret, 0);
	}


finished:
	logging(LOG_VERBOSE, "Load the medium again.");
	ret = startstopunit(iscsic, tgt_lun, 1, 0, 0, 0, 1, 1);
	CU_ASSERT_EQUAL(ret, 0);
}
void
test_startstopunit_simple(void)
{ 
	int ret;

	logging(LOG_VERBOSE, LOG_BLANK_LINE);
	logging(LOG_VERBOSE, "Test basic STARTSTOPUNIT");


	logging(LOG_VERBOSE, "Test we can eject removable the media with IMMED==1");
	if (inq->rmb) {
		logging(LOG_VERBOSE, "Media is not removable. STARTSTOPUNIT should fail");
	} else {
		logging(LOG_VERBOSE, "Media is removable. STARTSTOPUNIT should work");
	}

	ret = startstopunit(iscsic, tgt_lun,
			    1, 0, 0, 0, 1, 0);
	if (!inq->rmb) {
		CU_ASSERT_NOT_EQUAL(ret, 0);
		return;
	}
	CU_ASSERT_EQUAL(ret, 0);


	logging(LOG_VERBOSE, "Test TESTUNITREADY that medium is ejected.");
	ret = testunitready_nomedium(iscsic, tgt_lun);
	CU_ASSERT_EQUAL(ret, 0);


	logging(LOG_VERBOSE, "Test we can load the removable the media with IMMED==1");
	ret = startstopunit(iscsic, tgt_lun,
			    1, 0, 0, 0, 1, 1);
	CU_ASSERT_EQUAL(ret, 0);

	logging(LOG_VERBOSE, "Verify we can read from the media.");
	ret = testunitready(iscsic, tgt_lun);
	CU_ASSERT_EQUAL(ret, 0);



	logging(LOG_VERBOSE, "Test we can eject removable the media with IMMED==1");
	ret = startstopunit(iscsic, tgt_lun,
			    0, 0, 0, 0, 1, 0);
	CU_ASSERT_EQUAL(ret, 0);


	logging(LOG_VERBOSE, "Test TESTUNITREADY that medium is ejected.");
	ret = testunitready_nomedium(iscsic, tgt_lun);
	CU_ASSERT_EQUAL(ret, 0);


	logging(LOG_VERBOSE, "Test we can load the removable the media with IMMED==1");
	ret = startstopunit(iscsic, tgt_lun,
			    0, 0, 0, 0, 1, 1);
	CU_ASSERT_EQUAL(ret, 0);

	logging(LOG_VERBOSE, "Verify we can access the media again.");
	ret = testunitready(iscsic, tgt_lun);
	CU_ASSERT_EQUAL(ret, 0);
}
void
test_startstopunit_noloej(void)
{ 
	int ret;

	logging(LOG_VERBOSE, LOG_BLANK_LINE);
	logging(LOG_VERBOSE, "Test STARTSTOPUNIT LOEJ==0");
	if (!inq->rmb) {
		logging(LOG_VERBOSE, "[SKIPPED] LUN is not removable. "
			"Skipping test.");
		return;
	}

	logging(LOG_VERBOSE, "Test that media is not ejected when LOEJ==0 IMMED==0 NO_FLUSH==0 START==0");
	ret = startstopunit(iscsic, tgt_lun,
			    0, 0, 0, 0, 0, 0);
	CU_ASSERT_EQUAL(ret, 0);

	logging(LOG_VERBOSE, "Test TESTUNITREADY that medium is not ejected.");
	ret = testunitready(iscsic, tgt_lun);
	CU_ASSERT_EQUAL(ret, 0);

	logging(LOG_VERBOSE, "Test that media is not ejected when LOEJ==0 IMMED==0 NO_FLUSH==0 START==1");
	ret = startstopunit(iscsic, tgt_lun,
			    0, 0, 0, 0, 0, 1);
	CU_ASSERT_EQUAL(ret, 0);

	logging(LOG_VERBOSE, "Test TESTUNITREADY that medium is not ejected.");
	ret = testunitready(iscsic, tgt_lun);
	CU_ASSERT_EQUAL(ret, 0);

	logging(LOG_VERBOSE, "Test that media is not ejected when LOEJ==0 IMMED==1 NO_FLUSH==0 START==0");
	ret = startstopunit(iscsic, tgt_lun,
			    1, 0, 0, 0, 0, 0);
	CU_ASSERT_EQUAL(ret, 0);

	logging(LOG_VERBOSE, "Test TESTUNITREADY that medium is not ejected.");
	ret = testunitready(iscsic, tgt_lun);
	CU_ASSERT_EQUAL(ret, 0);

	logging(LOG_VERBOSE, "Test that media is not ejected when LOEJ==0 IMMED==1 NO_FLUSH==0 START==1");
	ret = startstopunit(iscsic, tgt_lun,
			    1, 0, 0, 0, 0, 1);
	CU_ASSERT_EQUAL(ret, 0);

	logging(LOG_VERBOSE, "Test TESTUNITREADY that medium is not ejected.");
	ret = testunitready(iscsic, tgt_lun);
	CU_ASSERT_EQUAL(ret, 0);

	logging(LOG_VERBOSE, "Test that media is not ejected when LOEJ==0 IMMED==0 NO_FLUSH==1 START==0");
	ret = startstopunit(iscsic, tgt_lun,
			    0, 0, 0, 1, 0, 0);
	CU_ASSERT_EQUAL(ret, 0);

	logging(LOG_VERBOSE, "Test TESTUNITREADY that medium is not ejected.");
	ret = testunitready(iscsic, tgt_lun);
	CU_ASSERT_EQUAL(ret, 0);

	logging(LOG_VERBOSE, "Test that media is not ejected when LOEJ==0 IMMED==0 NO_FLUSH==1 START==1");
	ret = startstopunit(iscsic, tgt_lun,
			    0, 0, 0, 1, 0, 1);
	CU_ASSERT_EQUAL(ret, 0);

	logging(LOG_VERBOSE, "Test TESTUNITREADY that medium is not ejected.");
	ret = testunitready(iscsic, tgt_lun);
	CU_ASSERT_EQUAL(ret, 0);

	logging(LOG_VERBOSE, "Test that media is not ejected when LOEJ==0 IMMED==1 NO_FLUSH==1 START==0");
	ret = startstopunit(iscsic, tgt_lun,
			    1, 0, 0, 1, 0, 0);
	CU_ASSERT_EQUAL(ret, 0);

	logging(LOG_VERBOSE, "Test TESTUNITREADY that medium is not ejected.");
	ret = testunitready(iscsic, tgt_lun);
	CU_ASSERT_EQUAL(ret, 0);

	logging(LOG_VERBOSE, "Test that media is not ejected when LOEJ==0 IMMED==1 NO_FLUSH==1 START==1");
	ret = startstopunit(iscsic, tgt_lun,
			    1, 0, 0, 1, 0, 1);
	CU_ASSERT_EQUAL(ret, 0);

	logging(LOG_VERBOSE, "Test TESTUNITREADY that medium is not ejected.");
	ret = testunitready(iscsic, tgt_lun);
	CU_ASSERT_EQUAL(ret, 0);


	logging(LOG_VERBOSE, "In case the target did eject the medium, load it again.");
	startstopunit(iscsic, tgt_lun, 1, 0, 0, 0, 1, 1);
}