2022-10-08 17:26:04 +00:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// SCSI Target Emulator RaSCSI Reloaded
|
|
|
|
// for Raspberry Pi
|
|
|
|
//
|
|
|
|
// Copyright (C) 2022 Uwe Seimet
|
|
|
|
//
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#include "mocks.h"
|
2022-11-10 06:44:06 +00:00
|
|
|
#include "shared/rascsi_exceptions.h"
|
2022-10-08 17:26:04 +00:00
|
|
|
#include "controllers/controller_manager.h"
|
|
|
|
#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)
|
|
|
|
{
|
2022-11-04 07:22:32 +00:00
|
|
|
auto bus = make_shared<MockBus>();
|
|
|
|
auto controller_manager = make_shared<ControllerManager>(*bus);
|
|
|
|
auto controller = make_shared<MockAbstractController>(controller_manager, 0);
|
|
|
|
auto services = CreateDevice(SCHS, *controller);
|
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);
|
2022-11-04 07:22:32 +00:00
|
|
|
EXPECT_EQ(status::GOOD, controller->GetStatus());
|
2022-10-08 17:26:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(HostServicesTest, Inquiry)
|
|
|
|
{
|
2022-10-23 19:51:39 +00:00
|
|
|
TestInquiry(SCHS, device_type::PROCESSOR, scsi_level::SPC_3, "RaSCSI Host Services ", 0x1f, false);
|
2022-10-08 17:26:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(HostServicesTest, StartStopUnit)
|
|
|
|
{
|
2022-11-04 07:22:32 +00:00
|
|
|
auto bus = make_shared<MockBus>();
|
|
|
|
auto controller_manager = make_shared<ControllerManager>(*bus);
|
|
|
|
auto controller = make_shared<MockAbstractController>(controller_manager, 0);
|
|
|
|
auto services = CreateDevice(SCHS, *controller);
|
2022-10-08 17:26:04 +00:00
|
|
|
|
2022-11-04 07:22:32 +00:00
|
|
|
auto& cmd = controller->GetCmd();
|
2022-10-08 17:26:04 +00:00
|
|
|
|
|
|
|
// STOP
|
2022-11-04 07:22:32 +00:00
|
|
|
EXPECT_CALL(*controller, ScheduleShutdown(AbstractController::rascsi_shutdown_mode::STOP_RASCSI));
|
|
|
|
EXPECT_CALL(*controller, Status());
|
2022-11-02 14:36:19 +00:00
|
|
|
services->Dispatch(scsi_command::eCmdStartStop);
|
2022-11-04 07:22:32 +00:00
|
|
|
EXPECT_EQ(status::GOOD, controller->GetStatus());
|
2022-10-08 17:26:04 +00:00
|
|
|
|
|
|
|
// LOAD
|
|
|
|
cmd[4] = 0x02;
|
2022-11-04 07:22:32 +00:00
|
|
|
EXPECT_CALL(*controller, ScheduleShutdown(AbstractController::rascsi_shutdown_mode::STOP_PI));
|
|
|
|
EXPECT_CALL(*controller, Status());
|
2022-11-02 14:36:19 +00:00
|
|
|
services->Dispatch(scsi_command::eCmdStartStop);
|
2022-11-04 07:22:32 +00:00
|
|
|
EXPECT_EQ(status::GOOD, controller->GetStatus());
|
2022-10-08 17:26:04 +00:00
|
|
|
|
|
|
|
// UNLOAD
|
|
|
|
cmd[4] = 0x03;
|
2022-11-04 07:22:32 +00:00
|
|
|
EXPECT_CALL(*controller, ScheduleShutdown(AbstractController::rascsi_shutdown_mode::RESTART_PI));
|
|
|
|
EXPECT_CALL(*controller, Status());
|
2022-11-02 14:36:19 +00:00
|
|
|
services->Dispatch(scsi_command::eCmdStartStop);
|
2022-11-04 07:22:32 +00:00
|
|
|
EXPECT_EQ(status::GOOD, controller->GetStatus());
|
2022-10-08 17:26:04 +00:00
|
|
|
|
|
|
|
// START
|
|
|
|
cmd[4] = 0x01;
|
2022-11-02 06:36:25 +00:00
|
|
|
EXPECT_THAT([&] { services->Dispatch(scsi_command::eCmdStartStop); }, Throws<scsi_exception>(AllOf(
|
2022-10-29 16:16:03 +00:00
|
|
|
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)
|
|
|
|
{
|
2022-11-04 07:22:32 +00:00
|
|
|
auto bus = make_shared<MockBus>();
|
|
|
|
auto controller_manager = make_shared<ControllerManager>(*bus);
|
|
|
|
auto controller = make_shared<MockAbstractController>(controller_manager, 0);
|
|
|
|
auto services = CreateDevice(SCHS, *controller);
|
2022-11-02 14:36:19 +00:00
|
|
|
const unordered_map<string, string> params;
|
|
|
|
services->Init(params);
|
2022-10-08 17:26:04 +00:00
|
|
|
|
2022-11-04 07:22:32 +00:00
|
|
|
auto& cmd = controller->GetCmd();
|
2022-10-08 17:26:04 +00:00
|
|
|
|
2022-11-02 06:36:25 +00:00
|
|
|
EXPECT_THAT([&] { services->Dispatch(scsi_command::eCmdModeSense6); }, Throws<scsi_exception>(AllOf(
|
2022-10-29 16:16:03 +00:00
|
|
|
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";
|
|
|
|
|
|
|
|
cmd[2] = 0x20;
|
2022-11-02 06:36:25 +00:00
|
|
|
EXPECT_THAT([&] { services->Dispatch(scsi_command::eCmdModeSense6); }, Throws<scsi_exception>(AllOf(
|
2022-10-29 16:16:03 +00:00
|
|
|
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";
|
|
|
|
|
|
|
|
cmd[1] = 0x08;
|
|
|
|
// ALLOCATION LENGTH
|
|
|
|
cmd[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
|
|
|
|
EXPECT_NE(0x00, buffer[9]);
|
|
|
|
// Day
|
|
|
|
EXPECT_NE(0x00, buffer[10]);
|
|
|
|
|
|
|
|
// ALLOCATION LENGTH
|
|
|
|
cmd[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)
|
|
|
|
{
|
2022-11-04 07:22:32 +00:00
|
|
|
auto bus = make_shared<MockBus>();
|
|
|
|
auto controller_manager = make_shared<ControllerManager>(*bus);
|
|
|
|
auto controller = make_shared<MockAbstractController>(controller_manager, 0);
|
|
|
|
auto services = CreateDevice(SCHS, *controller);
|
2022-11-02 14:36:19 +00:00
|
|
|
const unordered_map<string, string> params;
|
|
|
|
services->Init(params);
|
2022-10-08 17:26:04 +00:00
|
|
|
|
2022-11-04 07:22:32 +00:00
|
|
|
auto& cmd = controller->GetCmd();
|
2022-10-08 17:26:04 +00:00
|
|
|
|
2022-11-02 06:36:25 +00:00
|
|
|
EXPECT_THAT([&] { services->Dispatch(scsi_command::eCmdModeSense10); }, Throws<scsi_exception>(AllOf(
|
2022-10-29 16:16:03 +00:00
|
|
|
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";
|
|
|
|
|
|
|
|
cmd[2] = 0x20;
|
2022-11-02 06:36:25 +00:00
|
|
|
EXPECT_THAT([&] { services->Dispatch(scsi_command::eCmdModeSense10); }, Throws<scsi_exception>(AllOf(
|
2022-10-29 16:16:03 +00:00
|
|
|
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";
|
|
|
|
|
|
|
|
cmd[1] = 0x08;
|
|
|
|
// ALLOCATION LENGTH
|
|
|
|
cmd[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
|
|
|
|
EXPECT_NE(0x00, buffer[13]);
|
|
|
|
// Day
|
|
|
|
EXPECT_NE(0x00, buffer[14]);
|
|
|
|
|
|
|
|
// ALLOCATION LENGTH
|
|
|
|
cmd[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
|
|
|
}
|