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-10-15 06:38:15 +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/mode_page_device.h"
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
2022-10-23 19:51:39 +00:00
|
|
|
TEST(ModePageDeviceTest, SupportsSaveParameters)
|
|
|
|
{
|
|
|
|
MockModePageDevice device;
|
|
|
|
|
|
|
|
EXPECT_FALSE(device.SupportsSaveParameters()) << "Wrong default value";
|
|
|
|
device.SupportsSaveParameters(true);
|
|
|
|
EXPECT_TRUE(device.SupportsSaveParameters());
|
|
|
|
device.SupportsSaveParameters(false);
|
|
|
|
EXPECT_FALSE(device.SupportsSaveParameters());
|
|
|
|
}
|
|
|
|
|
2022-10-08 17:26:04 +00:00
|
|
|
TEST(ModePageDeviceTest, AddModePages)
|
|
|
|
{
|
|
|
|
vector<int> cdb(6);
|
2022-11-02 06:36:25 +00:00
|
|
|
vector<uint8_t> buf(512);
|
2022-10-08 17:26:04 +00:00
|
|
|
MockModePageDevice device;
|
|
|
|
|
2022-10-23 19:51:39 +00:00
|
|
|
// Page 0
|
|
|
|
cdb[2] = 0x00;
|
2022-11-02 06:36:25 +00:00
|
|
|
EXPECT_THAT([&] { device.AddModePages(cdb, buf, 0, 12, 255); }, Throws<scsi_exception>(AllOf(
|
2023-10-15 06:38:15 +00:00
|
|
|
Property(&scsi_exception::get_sense_key, sense_key::illegal_request),
|
|
|
|
Property(&scsi_exception::get_asc, asc::invalid_field_in_cdb))))
|
2022-10-23 19:51:39 +00:00
|
|
|
<< "Data were returned for non-existing mode page 0";
|
|
|
|
|
|
|
|
// All pages, non changeable
|
2022-10-08 17:26:04 +00:00
|
|
|
cdb[2] = 0x3f;
|
2022-10-23 19:51:39 +00:00
|
|
|
EXPECT_EQ(0, device.AddModePages(cdb, buf, 0, 0, 255));
|
|
|
|
EXPECT_EQ(3, device.AddModePages(cdb, buf, 0, 3, 255));
|
2022-11-02 06:36:25 +00:00
|
|
|
EXPECT_THAT([&] { device.AddModePages(cdb, buf, 0, 12, -1); }, Throws<scsi_exception>(AllOf(
|
2023-10-15 06:38:15 +00:00
|
|
|
Property(&scsi_exception::get_sense_key, sense_key::illegal_request),
|
|
|
|
Property(&scsi_exception::get_asc, asc::invalid_field_in_cdb))))
|
2022-11-02 06:36:25 +00:00
|
|
|
<< "Maximum size was ignored";
|
2022-10-23 19:51:39 +00:00
|
|
|
|
|
|
|
// All pages, changeable
|
|
|
|
cdb[2]= 0x7f;
|
|
|
|
EXPECT_EQ(0, device.AddModePages(cdb, buf, 0, 0, 255));
|
|
|
|
EXPECT_EQ(3, device.AddModePages(cdb, buf, 0, 3, 255));
|
2022-11-02 06:36:25 +00:00
|
|
|
EXPECT_THAT([&] { device.AddModePages(cdb, buf, 0, 12, -1); }, Throws<scsi_exception>(AllOf(
|
2023-10-15 06:38:15 +00:00
|
|
|
Property(&scsi_exception::get_sense_key, sense_key::illegal_request),
|
|
|
|
Property(&scsi_exception::get_asc, asc::invalid_field_in_cdb))))
|
2022-11-02 06:36:25 +00:00
|
|
|
<< "Maximum size was ignored";
|
2022-10-23 19:51:39 +00:00
|
|
|
}
|
2022-10-08 17:26:04 +00:00
|
|
|
|
2022-10-23 19:51:39 +00:00
|
|
|
TEST(ModePageDeviceTest, Page0)
|
|
|
|
{
|
|
|
|
vector<int> cdb(6);
|
2022-11-02 06:36:25 +00:00
|
|
|
vector<uint8_t> buf(512);
|
2022-10-23 19:51:39 +00:00
|
|
|
MockPage0ModePageDevice device;
|
2022-10-08 17:26:04 +00:00
|
|
|
|
2022-10-23 19:51:39 +00:00
|
|
|
cdb[2] = 0x3f;
|
|
|
|
EXPECT_EQ(0, device.AddModePages(cdb, buf, 0, 0, 255));
|
|
|
|
EXPECT_EQ(1, device.AddModePages(cdb, buf, 0, 1, 255));
|
2022-10-08 17:26:04 +00:00
|
|
|
}
|
|
|
|
|
2022-10-23 19:51:39 +00:00
|
|
|
TEST(ModePageDeviceTest, AddVendorPage)
|
|
|
|
{
|
|
|
|
map<int, vector<byte>> pages;
|
|
|
|
MockModePageDevice device;
|
|
|
|
|
|
|
|
device.AddVendorPage(pages, 0x3f, false);
|
|
|
|
EXPECT_TRUE(pages.empty()) << "There must not be any default vendor page";
|
|
|
|
device.AddVendorPage(pages, 0x3f, true);
|
|
|
|
EXPECT_TRUE(pages.empty()) << "There must not be any default vendor page";
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(ModePageDeviceTest, ModeSense6)
|
|
|
|
{
|
2023-10-15 06:38:15 +00:00
|
|
|
auto controller = make_shared<MockAbstractController>(0);
|
2022-10-23 19:51:39 +00:00
|
|
|
auto device = make_shared<NiceMock<MockModePageDevice>>();
|
2023-10-15 06:38:15 +00:00
|
|
|
EXPECT_TRUE(device->Init({}));
|
2022-10-23 19:51:39 +00:00
|
|
|
|
2022-11-04 07:22:32 +00:00
|
|
|
controller->AddDevice(device);
|
2022-10-23 19:51:39 +00:00
|
|
|
|
2022-11-04 07:22:32 +00:00
|
|
|
EXPECT_CALL(*controller, DataIn());
|
2022-11-02 14:36:19 +00:00
|
|
|
device->Dispatch(scsi_command::eCmdModeSense6);
|
2022-10-08 17:26:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(ModePageDeviceTest, ModeSense10)
|
|
|
|
{
|
2023-10-15 06:38:15 +00:00
|
|
|
auto controller = make_shared<MockAbstractController>(0);
|
2022-10-08 17:26:04 +00:00
|
|
|
auto device = make_shared<NiceMock<MockModePageDevice>>();
|
2023-10-15 06:38:15 +00:00
|
|
|
EXPECT_TRUE(device->Init({}));
|
2022-10-08 17:26:04 +00:00
|
|
|
|
2022-11-04 07:22:32 +00:00
|
|
|
controller->AddDevice(device);
|
2022-10-08 17:26:04 +00:00
|
|
|
|
2022-11-04 07:22:32 +00:00
|
|
|
EXPECT_CALL(*controller, DataIn());
|
2022-11-02 14:36:19 +00:00
|
|
|
device->Dispatch(scsi_command::eCmdModeSense10);
|
2022-10-08 17:26:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(ModePageDeviceTest, ModeSelect)
|
|
|
|
{
|
|
|
|
MockModePageDevice device;
|
|
|
|
vector<int> cmd;
|
2022-11-02 06:36:25 +00:00
|
|
|
vector<uint8_t> buf;
|
2022-10-08 17:26:04 +00:00
|
|
|
|
2022-11-02 06:36:25 +00:00
|
|
|
EXPECT_THAT([&] { device.ModeSelect(scsi_command::eCmdModeSelect6, cmd, buf, 0); }, Throws<scsi_exception>(AllOf(
|
2023-10-15 06:38:15 +00:00
|
|
|
Property(&scsi_exception::get_sense_key, sense_key::illegal_request),
|
|
|
|
Property(&scsi_exception::get_asc, asc::invalid_command_operation_code))))
|
2022-10-23 19:51:39 +00:00
|
|
|
<< "Unexpected MODE SELECT(6) default implementation";
|
2022-11-02 06:36:25 +00:00
|
|
|
EXPECT_THAT([&] { device.ModeSelect(scsi_command::eCmdModeSelect10, cmd, buf, 0); }, Throws<scsi_exception>(AllOf(
|
2023-10-15 06:38:15 +00:00
|
|
|
Property(&scsi_exception::get_sense_key, sense_key::illegal_request),
|
|
|
|
Property(&scsi_exception::get_asc, asc::invalid_command_operation_code))))
|
2022-10-23 19:51:39 +00:00
|
|
|
<< "Unexpected MODE SELECT(10) default implementation";
|
2022-10-08 17:26:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(ModePageDeviceTest, ModeSelect6)
|
|
|
|
{
|
2023-10-15 06:38:15 +00:00
|
|
|
auto controller = make_shared<MockAbstractController>(0);
|
2022-10-08 17:26:04 +00:00
|
|
|
auto device = make_shared<MockModePageDevice>();
|
2023-10-15 06:38:15 +00:00
|
|
|
EXPECT_TRUE(device->Init({}));
|
2022-10-08 17:26:04 +00:00
|
|
|
|
2022-11-04 07:22:32 +00:00
|
|
|
controller->AddDevice(device);
|
2022-10-08 17:26:04 +00:00
|
|
|
|
2022-11-04 07:22:32 +00:00
|
|
|
EXPECT_CALL(*controller, DataOut());
|
2022-11-02 14:36:19 +00:00
|
|
|
device->Dispatch(scsi_command::eCmdModeSelect6);
|
2022-10-08 17:26:04 +00:00
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
controller->SetCmdByte(1, 0x01);
|
2022-11-02 06:36:25 +00:00
|
|
|
EXPECT_THAT([&] { device->Dispatch(scsi_command::eCmdModeSelect6); }, Throws<scsi_exception>(AllOf(
|
2023-10-15 06:38:15 +00:00
|
|
|
Property(&scsi_exception::get_sense_key, sense_key::illegal_request),
|
|
|
|
Property(&scsi_exception::get_asc, asc::invalid_field_in_cdb))))
|
2022-10-29 16:16:03 +00:00
|
|
|
<< "Saving parameters is not supported by base class";
|
2022-10-08 17:26:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(ModePageDeviceTest, ModeSelect10)
|
|
|
|
{
|
2023-10-15 06:38:15 +00:00
|
|
|
auto controller = make_shared<MockAbstractController>(0);
|
2022-10-08 17:26:04 +00:00
|
|
|
auto device = make_shared<MockModePageDevice>();
|
2023-10-15 06:38:15 +00:00
|
|
|
EXPECT_TRUE(device->Init({}));
|
2022-10-08 17:26:04 +00:00
|
|
|
|
2022-11-04 07:22:32 +00:00
|
|
|
controller->AddDevice(device);
|
2022-10-08 17:26:04 +00:00
|
|
|
|
2022-11-04 07:22:32 +00:00
|
|
|
EXPECT_CALL(*controller, DataOut());
|
2022-11-02 14:36:19 +00:00
|
|
|
device->Dispatch(scsi_command::eCmdModeSelect10);
|
2022-10-08 17:26:04 +00:00
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
controller->SetCmdByte(1, 0x01);
|
2022-11-02 06:36:25 +00:00
|
|
|
EXPECT_THAT([&] { device->Dispatch(scsi_command::eCmdModeSelect10); }, Throws<scsi_exception>(AllOf(
|
2023-10-15 06:38:15 +00:00
|
|
|
Property(&scsi_exception::get_sense_key, sense_key::illegal_request),
|
|
|
|
Property(&scsi_exception::get_asc, asc::invalid_field_in_cdb))))
|
2022-10-29 16:16:03 +00:00
|
|
|
<< "Saving parameters is not supported for by base class";
|
2022-10-08 17:26:04 +00:00
|
|
|
}
|