Skip to content

Commit

Permalink
Add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
uweseimet committed Dec 12, 2023
1 parent ec09efa commit 4f4ee8b
Showing 1 changed file with 52 additions and 45 deletions.
97 changes: 52 additions & 45 deletions cpp/test/primary_device_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,37 +55,37 @@ TEST(PrimaryDeviceTest, Reset)

device->Dispatch(scsi_command::cmd_reserve6);
EXPECT_FALSE(device->CheckReservation(1, scsi_command::cmd_test_unit_ready, false))
<< "Device must be reserved for initiator ID 1";
<< "Device must be reserved for initiator ID 1";
device->Reset();
EXPECT_TRUE(device->CheckReservation(1, scsi_command::cmd_test_unit_ready, false))
<< "Device must not be reserved anymore for initiator ID 1";
<< "Device must not be reserved anymore for initiator ID 1";
}

TEST(PrimaryDeviceTest, CheckReservation)
{
auto [controller, device] = CreatePrimaryDevice();

EXPECT_TRUE(device->CheckReservation(0, scsi_command::cmd_test_unit_ready, false))
<< "Device must not be reserved for initiator ID 0";
<< "Device must not be reserved for initiator ID 0";

device->Dispatch(scsi_command::cmd_reserve6);
EXPECT_TRUE(device->CheckReservation(0, scsi_command::cmd_test_unit_ready, false))
<< "Device must not be reserved for initiator ID 0";
<< "Device must not be reserved for initiator ID 0";
EXPECT_FALSE(device->CheckReservation(1, scsi_command::cmd_test_unit_ready, false))
<< "Device must be reserved for initiator ID 1";
<< "Device must be reserved for initiator ID 1";
EXPECT_FALSE(device->CheckReservation(-1, scsi_command::cmd_test_unit_ready, false))
<< "Device must be reserved for unknown initiator";
<< "Device must be reserved for unknown initiator";
EXPECT_TRUE(device->CheckReservation(1, scsi_command::cmd_inquiry, false))
<< "Device must not be reserved for INQUIRY";
<< "Device must not be reserved for INQUIRY";
EXPECT_TRUE(device->CheckReservation(1, scsi_command::cmd_request_sense, false))
<< "Device must not be reserved for REQUEST SENSE";
<< "Device must not be reserved for REQUEST SENSE";
EXPECT_TRUE(device->CheckReservation(1, scsi_command::cmd_release6, false))
<< "Device must not be reserved for RELEASE (6)";
<< "Device must not be reserved for RELEASE (6)";

EXPECT_TRUE(device->CheckReservation(1, scsi_command::cmd_prevent_allow_medium_removal, false))
<< "Device must not be reserved for PREVENT ALLOW MEDIUM REMOVAL with prevent bit not set";
<< "Device must not be reserved for PREVENT ALLOW MEDIUM REMOVAL with prevent bit not set";
EXPECT_FALSE(device->CheckReservation(1, scsi_command::cmd_prevent_allow_medium_removal, true))
<< "Device must be reserved for PREVENT ALLOW MEDIUM REMOVAL with prevent bit set";
<< "Device must be reserved for PREVENT ALLOW MEDIUM REMOVAL with prevent bit set";
}

TEST(PrimaryDeviceTest, ReserveReleaseUnit)
Expand All @@ -94,20 +94,20 @@ TEST(PrimaryDeviceTest, ReserveReleaseUnit)

device->Dispatch(scsi_command::cmd_reserve6);
EXPECT_FALSE(device->CheckReservation(1, scsi_command::cmd_test_unit_ready, false))
<< "Device must be reserved for initiator ID 1";
<< "Device must be reserved for initiator ID 1";

device->Dispatch(scsi_command::cmd_release6);
EXPECT_TRUE(device->CheckReservation(1, scsi_command::cmd_test_unit_ready, false))
<< "Device must not be reserved anymore for initiator ID 1";
<< "Device must not be reserved anymore for initiator ID 1";

ON_CALL(*controller, GetInitiatorId).WillByDefault(Return(-1));
device->Dispatch(scsi_command::cmd_reserve6);
EXPECT_FALSE(device->CheckReservation(1, scsi_command::cmd_test_unit_ready, false))
<< "Device must be reserved for unknown initiator";
<< "Device must be reserved for unknown initiator";

device->Dispatch(scsi_command::cmd_release6);
EXPECT_TRUE(device->CheckReservation(1, scsi_command::cmd_test_unit_ready, false))
<< "Device must not be reserved anymore for unknown initiator";
<< "Device must not be reserved anymore for unknown initiator";
}

TEST(PrimaryDeviceTest, DiscardReservation)
Expand All @@ -116,10 +116,10 @@ TEST(PrimaryDeviceTest, DiscardReservation)

device->Dispatch(scsi_command::cmd_reserve6);
EXPECT_FALSE(device->CheckReservation(1, scsi_command::cmd_test_unit_ready, false))
<< "Device must be reserved for initiator ID 1";
<< "Device must be reserved for initiator ID 1";
device->DiscardReservation();
EXPECT_TRUE(device->CheckReservation(1, scsi_command::cmd_test_unit_ready, false))
<< "Device must not be reserved anymore for initiator ID 1";
<< "Device must not be reserved anymore for initiator ID 1";
}

TEST(PrimaryDeviceTest, TestUnitReady)
Expand All @@ -133,21 +133,21 @@ TEST(PrimaryDeviceTest, TestUnitReady)
device->SetReady(false);
EXPECT_CALL(*controller, DataIn).Times(0);
EXPECT_THAT([&]
{
d->Dispatch(scsi_command::cmd_test_unit_ready)
;
},
{
d->Dispatch(scsi_command::cmd_test_unit_ready)
;
},
Throws<scsi_exception>(
AllOf(Property(&scsi_exception::get_sense_key, sense_key::unit_attention),
Property(&scsi_exception::get_asc, asc::power_on_or_reset))));

device->SetReset(false);
EXPECT_CALL(*controller, DataIn).Times(0);
EXPECT_THAT([&]
{
d->Dispatch(scsi_command::cmd_test_unit_ready)
;
},
{
d->Dispatch(scsi_command::cmd_test_unit_ready)
;
},
Throws<scsi_exception>(
AllOf(Property(&scsi_exception::get_sense_key, sense_key::unit_attention),
Property(&scsi_exception::get_asc, asc::not_ready_to_ready_change))));
Expand All @@ -156,10 +156,10 @@ TEST(PrimaryDeviceTest, TestUnitReady)
device->SetAttn(false);
EXPECT_CALL(*controller, DataIn).Times(0);
EXPECT_THAT([&]
{
d->Dispatch(scsi_command::cmd_test_unit_ready)
;
},
{
d->Dispatch(scsi_command::cmd_test_unit_ready)
;
},
Throws<scsi_exception>(
AllOf(Property(&scsi_exception::get_sense_key, sense_key::unit_attention),
Property(&scsi_exception::get_asc, asc::power_on_or_reset))));
Expand All @@ -168,21 +168,21 @@ TEST(PrimaryDeviceTest, TestUnitReady)
device->SetAttn(true);
EXPECT_CALL(*controller, DataIn).Times(0);
EXPECT_THAT([&]
{
d->Dispatch(scsi_command::cmd_test_unit_ready)
;
},
{
d->Dispatch(scsi_command::cmd_test_unit_ready)
;
},
Throws<scsi_exception>(
AllOf(Property(&scsi_exception::get_sense_key, sense_key::unit_attention),
Property(&scsi_exception::get_asc, asc::not_ready_to_ready_change))));

device->SetAttn(false);
EXPECT_CALL(*controller, DataIn).Times(0);
EXPECT_THAT([&]
{
d->Dispatch(scsi_command::cmd_test_unit_ready)
;
},
{
d->Dispatch(scsi_command::cmd_test_unit_ready)
;
},
Throws<scsi_exception>(
AllOf(Property(&scsi_exception::get_sense_key, sense_key::not_ready),
Property(&scsi_exception::get_asc, asc::medium_not_present))));
Expand Down Expand Up @@ -268,10 +268,10 @@ TEST(PrimaryDeviceTest, RequestSense)

device->SetReady(false);
EXPECT_THAT([&]
{
d->Dispatch(scsi_command::cmd_request_sense)
;
},
{
d->Dispatch(scsi_command::cmd_request_sense)
;
},
Throws<scsi_exception>(
AllOf(Property(&scsi_exception::get_sense_key, sense_key::not_ready),
Property(&scsi_exception::get_asc, asc::medium_not_present))));
Expand All @@ -296,20 +296,20 @@ TEST(PrimaryDeviceTest, SendDiagnostic)
EXPECT_THAT([&] {d->Dispatch(scsi_command::cmd_send_diagnostic);}, Throws<scsi_exception>(AllOf(
Property(&scsi_exception::get_sense_key, sense_key::illegal_request),
Property(&scsi_exception::get_asc, asc::invalid_field_in_cdb))))
<< "SEND DIAGNOSTIC must fail because PF bit is not supported";
<< "SEND DIAGNOSTIC must fail because PF bit is not supported";
controller->SetCmdByte(1, 0);

controller->SetCmdByte(3, 1);
EXPECT_THAT([&] {d->Dispatch(scsi_command::cmd_send_diagnostic);}, Throws<scsi_exception>(AllOf(
Property(&scsi_exception::get_sense_key, sense_key::illegal_request),
Property(&scsi_exception::get_asc, asc::invalid_field_in_cdb))))
<< "SEND DIAGNOSTIC must fail because parameter list is not supported";
<< "SEND DIAGNOSTIC must fail because parameter list is not supported";
controller->SetCmdByte(3, 0);
controller->SetCmdByte(4, 1);
EXPECT_THAT([&] {d->Dispatch(scsi_command::cmd_send_diagnostic);}, Throws<scsi_exception>(AllOf(
Property(&scsi_exception::get_sense_key, sense_key::illegal_request),
Property(&scsi_exception::get_asc, asc::invalid_field_in_cdb))))
<< "SEND DIAGNOSTIC must fail because parameter list is not supported";
<< "SEND DIAGNOSTIC must fail because parameter list is not supported";
}

TEST(PrimaryDeviceTest, ReportLuns)
Expand Down Expand Up @@ -349,7 +349,7 @@ TEST(PrimaryDeviceTest, ReportLuns)
EXPECT_THAT([&] {device1->Dispatch(scsi_command::cmd_reportLuns);}, Throws<scsi_exception>(AllOf(
Property(&scsi_exception::get_sense_key, sense_key::illegal_request),
Property(&scsi_exception::get_asc, asc::invalid_field_in_cdb))))
<< "Only SELECT REPORT mode 0 is supported";
<< "Only SELECT REPORT mode 0 is supported";
}

TEST(PrimaryDeviceTest, Dispatch)
Expand All @@ -373,3 +373,10 @@ TEST(PrimaryDeviceTest, Init)

EXPECT_TRUE(device.Init(params)) << "Initialization of primary device must not fail";
}

TEST(PrimaryDeviceTest, GetStatistics)
{
MockPrimaryDevice device(0);

EXPECT_TRUE(device.GetStatistics().empty());
}

0 comments on commit 4f4ee8b

Please sign in to comment.