2022-10-08 17:26:04 +00:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
//
|
2022-12-05 17:58:23 +00:00
|
|
|
// SCSI Target Emulator PiSCSI
|
2022-10-08 17:26:04 +00:00
|
|
|
// for Raspberry Pi
|
|
|
|
//
|
2023-01-07 08:50:37 +00:00
|
|
|
// Copyright (C) 2022-2023 Uwe Seimet
|
2022-10-08 17:26:04 +00:00
|
|
|
//
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#include "mocks.h"
|
2022-12-05 17:58:23 +00:00
|
|
|
#include "shared/piscsi_exceptions.h"
|
2022-10-08 17:26:04 +00:00
|
|
|
#include "devices/host_services.h"
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
2022-10-25 08:29:57 +00:00
|
|
|
void HostServices_SetUpModePages(map<int, vector<byte>>& pages)
|
|
|
|
{
|
|
|
|
EXPECT_EQ(1, pages.size()) << "Unexpected number of mode pages";
|
|
|
|
EXPECT_EQ(10, pages[32].size());
|
|
|
|
}
|
|
|
|
|
2022-10-08 17:26:04 +00:00
|
|
|
TEST(HostServicesTest, TestUnitReady)
|
|
|
|
{
|
2023-10-15 06:38:15 +00:00
|
|
|
auto [controller, services] = CreateDevice(SCHS);
|
2022-10-08 17:26:04 +00:00
|
|
|
|
2022-11-04 07:22:32 +00:00
|
|
|
EXPECT_CALL(*controller, Status());
|
2022-11-02 14:36:19 +00:00
|
|
|
services->Dispatch(scsi_command::eCmdTestUnitReady);
|
2023-10-15 06:38:15 +00:00
|
|
|
EXPECT_EQ(status::good, controller->GetStatus());
|
2022-10-08 17:26:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(HostServicesTest, Inquiry)
|
|
|
|
{
|
2023-10-15 06:38:15 +00:00
|
|
|
TestInquiry::Inquiry(SCHS, device_type::processor, scsi_level::spc_3, "PiSCSI Host Services ", 0x1f, false);
|
2022-10-08 17:26:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(HostServicesTest, StartStopUnit)
|
|
|
|
{
|
2023-10-15 06:38:15 +00:00
|
|
|
auto [controller, services] = CreateDevice(SCHS);
|
|
|
|
// Required by the bullseye clang++ compiler
|
|
|
|
auto s = services;
|
2022-10-08 17:26:04 +00:00
|
|
|
|
|
|
|
// STOP
|
2022-11-04 07:22:32 +00:00
|
|
|
EXPECT_CALL(*controller, Status());
|
2022-11-02 14:36:19 +00:00
|
|
|
services->Dispatch(scsi_command::eCmdStartStop);
|
2023-10-15 06:38:15 +00:00
|
|
|
EXPECT_EQ(status::good, controller->GetStatus());
|
2022-10-08 17:26:04 +00:00
|
|
|
|
|
|
|
// LOAD
|
2023-10-15 06:38:15 +00:00
|
|
|
controller->SetCmdByte(4, 0x02);
|
2022-11-04 07:22:32 +00:00
|
|
|
EXPECT_CALL(*controller, Status());
|
2022-11-02 14:36:19 +00:00
|
|
|
services->Dispatch(scsi_command::eCmdStartStop);
|
2023-10-15 06:38:15 +00:00
|
|
|
EXPECT_EQ(status::good, controller->GetStatus());
|
2022-10-08 17:26:04 +00:00
|
|
|
|
|
|
|
// UNLOAD
|
2023-10-15 06:38:15 +00:00
|
|
|
controller->SetCmdByte(4, 0x03);
|
2022-11-04 07:22:32 +00:00
|
|
|
EXPECT_CALL(*controller, Status());
|
2022-11-02 14:36:19 +00:00
|
|
|
services->Dispatch(scsi_command::eCmdStartStop);
|
2023-10-15 06:38:15 +00:00
|
|
|
EXPECT_EQ(status::good, controller->GetStatus());
|
2022-10-08 17:26:04 +00:00
|
|
|
|
|
|
|
// START
|
2023-10-15 06:38:15 +00:00
|
|
|
controller->SetCmdByte(4, 0x01);
|
|
|
|
EXPECT_THAT([&] { s->Dispatch(scsi_command::eCmdStartStop); }, Throws<scsi_exception>(AllOf(
|
|
|
|
Property(&scsi_exception::get_sense_key, sense_key::illegal_request),
|
|
|
|
Property(&scsi_exception::get_asc, asc::invalid_field_in_cdb))));
|
2022-10-08 17:26:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(HostServicesTest, ModeSense6)
|
|
|
|
{
|
2023-10-15 06:38:15 +00:00
|
|
|
auto [controller, services] = CreateDevice(SCHS);
|
|
|
|
// Required by the bullseye clang++ compiler
|
|
|
|
auto s = services;
|
|
|
|
|
|
|
|
EXPECT_TRUE(services->Init({}));
|
|
|
|
|
|
|
|
EXPECT_THAT([&] { s->Dispatch(scsi_command::eCmdModeSense6); }, Throws<scsi_exception>(AllOf(
|
|
|
|
Property(&scsi_exception::get_sense_key, sense_key::illegal_request),
|
|
|
|
Property(&scsi_exception::get_asc, asc::invalid_field_in_cdb))))
|
2022-10-08 17:26:04 +00:00
|
|
|
<< "Unsupported mode page was returned";
|
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
controller->SetCmdByte(2, 0x20);
|
|
|
|
EXPECT_THAT([&] { s->Dispatch(scsi_command::eCmdModeSense6); }, Throws<scsi_exception>(AllOf(
|
|
|
|
Property(&scsi_exception::get_sense_key, sense_key::illegal_request),
|
|
|
|
Property(&scsi_exception::get_asc, asc::invalid_field_in_cdb))))
|
2022-10-08 17:26:04 +00:00
|
|
|
<< "Block descriptors are not supported";
|
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
controller->SetCmdByte(1, 0x08);
|
2022-10-08 17:26:04 +00:00
|
|
|
// ALLOCATION LENGTH
|
2023-10-15 06:38:15 +00:00
|
|
|
controller->SetCmdByte(4, 255);
|
2022-11-04 07:22:32 +00:00
|
|
|
EXPECT_CALL(*controller, DataIn());
|
2022-11-02 14:36:19 +00:00
|
|
|
services->Dispatch(scsi_command::eCmdModeSense6);
|
2022-11-04 07:22:32 +00:00
|
|
|
vector<uint8_t>& buffer = controller->GetBuffer();
|
2022-10-08 17:26:04 +00:00
|
|
|
// Major version 1
|
|
|
|
EXPECT_EQ(0x01, buffer[6]);
|
|
|
|
// Minor version 0
|
|
|
|
EXPECT_EQ(0x00, buffer[7]);
|
|
|
|
// Year
|
2023-01-07 08:50:37 +00:00
|
|
|
EXPECT_NE(0x00, buffer[8]);
|
2022-10-08 17:26:04 +00:00
|
|
|
// Day
|
|
|
|
EXPECT_NE(0x00, buffer[10]);
|
|
|
|
|
|
|
|
// ALLOCATION LENGTH
|
2023-10-15 06:38:15 +00:00
|
|
|
controller->SetCmdByte(4, 2);
|
2022-11-04 07:22:32 +00:00
|
|
|
EXPECT_CALL(*controller, DataIn());
|
2022-11-02 14:36:19 +00:00
|
|
|
services->Dispatch(scsi_command::eCmdModeSense6);
|
2022-11-04 07:22:32 +00:00
|
|
|
buffer = controller->GetBuffer();
|
2022-10-08 17:26:04 +00:00
|
|
|
EXPECT_EQ(0x02, buffer[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(HostServicesTest, ModeSense10)
|
|
|
|
{
|
2023-10-15 06:38:15 +00:00
|
|
|
auto [controller, services] = CreateDevice(SCHS);
|
|
|
|
// Required by the bullseye clang++ compiler
|
|
|
|
auto s = services;
|
|
|
|
|
|
|
|
EXPECT_TRUE(services->Init({}));
|
|
|
|
|
|
|
|
EXPECT_THAT([&] { s->Dispatch(scsi_command::eCmdModeSense10); }, Throws<scsi_exception>(AllOf(
|
|
|
|
Property(&scsi_exception::get_sense_key, sense_key::illegal_request),
|
|
|
|
Property(&scsi_exception::get_asc, asc::invalid_field_in_cdb))))
|
2022-10-08 17:26:04 +00:00
|
|
|
<< "Unsupported mode page was returned";
|
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
controller->SetCmdByte(2, 0x20);
|
|
|
|
EXPECT_THAT([&] { s->Dispatch(scsi_command::eCmdModeSense10); }, Throws<scsi_exception>(AllOf(
|
|
|
|
Property(&scsi_exception::get_sense_key, sense_key::illegal_request),
|
|
|
|
Property(&scsi_exception::get_asc, asc::invalid_field_in_cdb))))
|
2022-10-08 17:26:04 +00:00
|
|
|
<< "Block descriptors are not supported";
|
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
controller->SetCmdByte(1, 0x08);
|
2022-10-08 17:26:04 +00:00
|
|
|
// ALLOCATION LENGTH
|
2023-10-15 06:38:15 +00:00
|
|
|
controller->SetCmdByte(8, 255);
|
2022-11-04 07:22:32 +00:00
|
|
|
EXPECT_CALL(*controller, DataIn());
|
2022-11-02 14:36:19 +00:00
|
|
|
services->Dispatch(scsi_command::eCmdModeSense10);
|
2022-11-04 07:22:32 +00:00
|
|
|
vector<uint8_t>& buffer = controller->GetBuffer();
|
2022-10-08 17:26:04 +00:00
|
|
|
// Major version 1
|
|
|
|
EXPECT_EQ(0x01, buffer[10]);
|
|
|
|
// Minor version 0
|
|
|
|
EXPECT_EQ(0x00, buffer[11]);
|
|
|
|
// Year
|
2023-01-07 08:50:37 +00:00
|
|
|
EXPECT_NE(0x00, buffer[12]);
|
2022-10-08 17:26:04 +00:00
|
|
|
// Day
|
|
|
|
EXPECT_NE(0x00, buffer[14]);
|
|
|
|
|
|
|
|
// ALLOCATION LENGTH
|
2023-10-15 06:38:15 +00:00
|
|
|
controller->SetCmdByte(8, 2);
|
2022-11-04 07:22:32 +00:00
|
|
|
EXPECT_CALL(*controller, DataIn());
|
2022-11-02 14:36:19 +00:00
|
|
|
services->Dispatch(scsi_command::eCmdModeSense10);
|
2022-11-04 07:22:32 +00:00
|
|
|
buffer = controller->GetBuffer();
|
2022-10-08 17:26:04 +00:00
|
|
|
EXPECT_EQ(0x02, buffer[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(HostServicesTest, SetUpModePages)
|
|
|
|
{
|
2022-11-04 07:22:32 +00:00
|
|
|
MockHostServices services(0);
|
2022-10-23 19:51:39 +00:00
|
|
|
map<int, vector<byte>> pages;
|
2022-10-08 17:26:04 +00:00
|
|
|
|
2022-10-25 08:29:57 +00:00
|
|
|
// Non changeable
|
2022-10-23 19:51:39 +00:00
|
|
|
services.SetUpModePages(pages, 0x3f, false);
|
2022-10-25 08:29:57 +00:00
|
|
|
HostServices_SetUpModePages(pages);
|
|
|
|
|
|
|
|
// Changeable
|
|
|
|
pages.clear();
|
|
|
|
services.SetUpModePages(pages, 0x3f, true);
|
|
|
|
HostServices_SetUpModePages(pages);
|
2022-10-08 17:26:04 +00:00
|
|
|
}
|