Added rascsi filter to limit logging to a particular device (#978)

* Support for ID and LUN parameter for the -L option in rascsi

* Added DeviceLogger class

* Removed dupiicate code

* Fixed SonarQube issues

* Added unit tests, improved code sharing

* Fixed regression (#979)
This commit is contained in:
Uwe Seimet
2022-11-11 21:08:48 +01:00
committed by GitHub
parent 4fa513090a
commit 454c61ac0d
47 changed files with 673 additions and 409 deletions
+1 -2
View File
@@ -199,14 +199,13 @@ TEST(AbstractControllerTest, ExtractInitiatorId)
{
const int ID = 1;
const int INITIATOR_ID = 7;
const int UNKNOWN_INITIATOR_ID = -1;
auto bus = make_shared<MockBus>();
auto controller_manager = make_shared<ControllerManager>(*bus);
MockAbstractController controller(controller_manager, ID);
EXPECT_EQ(INITIATOR_ID, controller.ExtractInitiatorId((1 << INITIATOR_ID) | ( 1 << ID)));
EXPECT_EQ(UNKNOWN_INITIATOR_ID, controller.ExtractInitiatorId(1 << ID));
EXPECT_EQ(AbstractController::UNKNOWN_INITIATOR_ID, controller.ExtractInitiatorId(1 << ID));
}
TEST(AbstractControllerTest, GetOpcode)
-25
View File
@@ -140,9 +140,6 @@ TEST(DeviceFactoryTest, SCHD_Device_Defaults)
auto device = device_factory.CreateDevice(UNDEFINED, 0, "test.hda");
const unordered_map<string, string> params;
device->Init(params);
EXPECT_NE(nullptr, device);
EXPECT_EQ(SCHD, device->GetType());
EXPECT_TRUE(device->SupportsFile());
@@ -179,8 +176,6 @@ void TestRemovableDrive(PbDeviceType type, const string& filename, const string&
{
DeviceFactory device_factory;
auto device = device_factory.CreateDevice(UNDEFINED, 0, filename);
const unordered_map<string, string> params;
device->Init(params);
EXPECT_NE(nullptr, device);
EXPECT_EQ(type, device->GetType());
@@ -218,10 +213,6 @@ TEST(DeviceFactoryTest, SCCD_Device_Defaults)
DeviceFactory device_factory;
auto device = device_factory.CreateDevice(UNDEFINED, 0, "test.iso");
const unordered_map<string, string> params;
device->Init(params);
EXPECT_NE(nullptr, device);
EXPECT_EQ(SCCD, device->GetType());
EXPECT_TRUE(device->SupportsFile());
@@ -247,10 +238,6 @@ TEST(DeviceFactoryTest, SCBR_Device_Defaults)
DeviceFactory device_factory;
auto device = device_factory.CreateDevice(UNDEFINED, 0, "bridge");
const unordered_map<string, string> params;
device->Init(params);
EXPECT_NE(nullptr, device);
EXPECT_EQ(SCBR, device->GetType());
EXPECT_FALSE(device->SupportsFile());
@@ -276,10 +263,6 @@ TEST(DeviceFactoryTest, SCDP_Device_Defaults)
DeviceFactory device_factory;
auto device = device_factory.CreateDevice(UNDEFINED, 0, "daynaport");
const unordered_map<string, string> params;
device->Init(params);
EXPECT_NE(nullptr, device);
EXPECT_EQ(SCDP, device->GetType());
EXPECT_FALSE(device->SupportsFile());
@@ -304,10 +287,6 @@ TEST(DeviceFactoryTest, SCHS_Device_Defaults)
DeviceFactory device_factory;
auto device = device_factory.CreateDevice(UNDEFINED, 0, "services");
const unordered_map<string, string> params;
device->Init(params);
EXPECT_NE(nullptr, device);
EXPECT_EQ(SCHS, device->GetType());
EXPECT_FALSE(device->SupportsFile());
@@ -333,10 +312,6 @@ TEST(DeviceFactoryTest, SCLP_Device_Defaults)
DeviceFactory device_factory;
auto device = device_factory.CreateDevice(UNDEFINED, 0, "printer");
const unordered_map<string, string> params;
device->Init(params);
EXPECT_NE(nullptr, device);
EXPECT_EQ(SCLP, device->GetType());
EXPECT_FALSE(device->SupportsFile());
+37
View File
@@ -106,3 +106,40 @@ TEST(ProtobufUtil, ListDevices)
EXPECT_NE(string::npos, device_list.find("Host Services"));
EXPECT_NE(string::npos, device_list.find("SCSI Printer"));
}
TEST(ProtobufUtil, SetProductData)
{
PbDeviceDefinition device;
SetProductData(device, "");
EXPECT_EQ("", device.vendor());
EXPECT_EQ("", device.product());
EXPECT_EQ("", device.revision());
SetProductData(device, "vendor");
EXPECT_EQ("vendor", device.vendor());
EXPECT_EQ("", device.product());
EXPECT_EQ("", device.revision());
SetProductData(device, "vendor:product");
EXPECT_EQ("vendor", device.vendor());
EXPECT_EQ("product", device.product());
EXPECT_EQ("", device.revision());
SetProductData(device, "vendor:product:revision");
EXPECT_EQ("vendor", device.vendor());
EXPECT_EQ("product", device.product());
EXPECT_EQ("revision", device.revision());
}
TEST(ProtobufUtil, SetIdAndLun)
{
PbDeviceDefinition device;
EXPECT_NE("", SetIdAndLun(device, "", 32));
EXPECT_EQ("", SetIdAndLun(device, "1", 32));
EXPECT_EQ(1, device.id());
EXPECT_EQ("", SetIdAndLun(device, "2:0", 32));
EXPECT_EQ(2, device.id());
EXPECT_EQ(0, device.unit());
}
+36
View File
@@ -19,6 +19,42 @@ using namespace std;
using namespace rascsi_interface;
using namespace ras_util;
TEST(RasUtilTest, ProcessId)
{
int id = -1;
int lun = -1;
string error = ProcessId("", 32, id, lun);
EXPECT_FALSE(error.empty());
EXPECT_EQ(-1, id);
EXPECT_EQ(-1, lun);
error = ProcessId("0:32", 32, id, lun);
EXPECT_FALSE(error.empty());
EXPECT_EQ(-1, id);
EXPECT_EQ(-1, lun);
error = ProcessId("-1:", 32, id, lun);
EXPECT_FALSE(error.empty());
EXPECT_EQ(-1, id);
EXPECT_EQ(-1, lun);
error = ProcessId("0:-1", 32, id, lun);
EXPECT_FALSE(error.empty());
EXPECT_EQ(-1, id);
EXPECT_EQ(-1, lun);
error = ProcessId("0", 32, id, lun);
EXPECT_TRUE(error.empty());
EXPECT_EQ(0, id);
EXPECT_EQ(0, lun);
error = ProcessId("7:31", 32, id, lun);
EXPECT_TRUE(error.empty());
EXPECT_EQ(7, id);
EXPECT_EQ(31, lun);
}
TEST(RasUtilTest, GetAsUnsignedInt)
{
int result;
+23 -12
View File
@@ -10,6 +10,7 @@
#include "mocks.h"
#include "shared/scsi.h"
#include "shared/rascsi_exceptions.h"
#include "devices/device_logger.h"
#include "devices/scsi_command_util.h"
using namespace scsi_command_util;
@@ -17,13 +18,14 @@ using namespace scsi_command_util;
TEST(ScsiCommandUtilTest, ModeSelect6)
{
const int LENGTH = 26;
DeviceLogger logger;
vector<int> cdb(6);
vector<uint8_t> buf(LENGTH);
// PF (vendor-specific parameter format) must not fail but be ignored
cdb[1] = 0x00;
ModeSelect(scsi_command::eCmdModeSelect6, cdb, buf, LENGTH, 0);
ModeSelect(logger, scsi_command::eCmdModeSelect6, cdb, buf, LENGTH, 0);
cdb[0] = 0x15;
// PF (standard parameter format)
@@ -32,45 +34,50 @@ TEST(ScsiCommandUtilTest, ModeSelect6)
buf[9] = 0x00;
buf[10] = 0x02;
buf[11] = 0x00;
EXPECT_THAT([&] { ModeSelect(scsi_command::eCmdModeSelect6, cdb, buf, LENGTH, 256); }, Throws<scsi_exception>(AllOf(
EXPECT_THAT([&] { ModeSelect(logger, scsi_command::eCmdModeSelect6, cdb, buf, LENGTH, 256); },
Throws<scsi_exception>(AllOf(
Property(&scsi_exception::get_sense_key, sense_key::ILLEGAL_REQUEST),
Property(&scsi_exception::get_asc, asc::INVALID_FIELD_IN_PARAMETER_LIST))))
<< "Requested sector size does not match current sector size";
// Page 0
buf[12] = 0x00;
EXPECT_THAT([&] { ModeSelect(scsi_command::eCmdModeSelect6, cdb, buf, LENGTH, 512); }, Throws<scsi_exception>(AllOf(
EXPECT_THAT([&] { ModeSelect(logger, scsi_command::eCmdModeSelect6, cdb, buf, LENGTH, 512); },
Throws<scsi_exception>(AllOf(
Property(&scsi_exception::get_sense_key, sense_key::ILLEGAL_REQUEST),
Property(&scsi_exception::get_asc, asc::INVALID_FIELD_IN_PARAMETER_LIST))))
<< "Unsupported page 0 was not rejected";
// Page 3 (Format Device Page)
buf[12] = 0x03;
EXPECT_THAT([&] { ModeSelect(scsi_command::eCmdModeSelect6, cdb, buf, LENGTH, 512); }, Throws<scsi_exception>(AllOf(
EXPECT_THAT([&] { ModeSelect(logger, scsi_command::eCmdModeSelect6, cdb, buf, LENGTH, 512); },
Throws<scsi_exception>(AllOf(
Property(&scsi_exception::get_sense_key, sense_key::ILLEGAL_REQUEST),
Property(&scsi_exception::get_asc, asc::INVALID_FIELD_IN_PARAMETER_LIST))))
<< "Requested sector size does not match current sector size";
// Match the requested to the current sector size
buf[24] = 0x02;
EXPECT_THAT([&] { ModeSelect(scsi_command::eCmdModeSelect6, cdb, buf, LENGTH - 1, 512); }, Throws<scsi_exception>(AllOf(
EXPECT_THAT([&] { ModeSelect(logger, scsi_command::eCmdModeSelect6, cdb, buf, LENGTH - 1, 512); },
Throws<scsi_exception>(AllOf(
Property(&scsi_exception::get_sense_key, sense_key::ILLEGAL_REQUEST),
Property(&scsi_exception::get_asc, asc::INVALID_FIELD_IN_PARAMETER_LIST))))
<< "Not enough command parameters";
ModeSelect(scsi_command::eCmdModeSelect6, cdb, buf, LENGTH, 512);
ModeSelect(logger, scsi_command::eCmdModeSelect6, cdb, buf, LENGTH, 512);
}
TEST(ScsiCommandUtilTest, ModeSelect10)
{
const int LENGTH = 30;
DeviceLogger logger;
vector<int> cdb(10);
vector<uint8_t> buf(LENGTH);
// PF (vendor-specific parameter format) must not fail but be ignored
cdb[1] = 0x00;
ModeSelect(scsi_command::eCmdModeSelect10, cdb, buf, LENGTH, 0);
ModeSelect(logger, scsi_command::eCmdModeSelect10, cdb, buf, LENGTH, 0);
// PF (standard parameter format)
cdb[1] = 0x10;
@@ -78,33 +85,37 @@ TEST(ScsiCommandUtilTest, ModeSelect10)
buf[13] = 0x00;
buf[14] = 0x02;
buf[15] = 0x00;
EXPECT_THAT([&] { ModeSelect(scsi_command::eCmdModeSelect10, cdb, buf, LENGTH, 256); }, Throws<scsi_exception>(AllOf(
EXPECT_THAT([&] { ModeSelect(logger, scsi_command::eCmdModeSelect10, cdb, buf, LENGTH, 256); },
Throws<scsi_exception>(AllOf(
Property(&scsi_exception::get_sense_key, sense_key::ILLEGAL_REQUEST),
Property(&scsi_exception::get_asc, asc::INVALID_FIELD_IN_PARAMETER_LIST))))
<< "Requested sector size does not match current sector size";
// Page 0
buf[16] = 0x00;
EXPECT_THAT([&] { ModeSelect(scsi_command::eCmdModeSelect10, cdb, buf, LENGTH, 512); }, Throws<scsi_exception>(AllOf(
EXPECT_THAT([&] { ModeSelect(logger, scsi_command::eCmdModeSelect10, cdb, buf, LENGTH, 512); },
Throws<scsi_exception>(AllOf(
Property(&scsi_exception::get_sense_key, sense_key::ILLEGAL_REQUEST),
Property(&scsi_exception::get_asc, asc::INVALID_FIELD_IN_PARAMETER_LIST))))
<< "Unsupported page 0 was not rejected";
// Page 3 (Format Device Page)
buf[16] = 0x03;
EXPECT_THAT([&] { ModeSelect(scsi_command::eCmdModeSelect10, cdb, buf, LENGTH, 512); }, Throws<scsi_exception>(AllOf(
EXPECT_THAT([&] { ModeSelect(logger, scsi_command::eCmdModeSelect10, cdb, buf, LENGTH, 512); },
Throws<scsi_exception>(AllOf(
Property(&scsi_exception::get_sense_key, sense_key::ILLEGAL_REQUEST),
Property(&scsi_exception::get_asc, asc::INVALID_FIELD_IN_PARAMETER_LIST))))
<< "Requested sector size does not match current sector size";
// Match the requested to the current sector size
buf[28] = 0x02;
EXPECT_THAT([&] { ModeSelect(scsi_command::eCmdModeSelect10, cdb, buf, LENGTH - 1, 512); }, Throws<scsi_exception>(AllOf(
EXPECT_THAT([&] { ModeSelect(logger, scsi_command::eCmdModeSelect10, cdb, buf, LENGTH - 1, 512); },
Throws<scsi_exception>(AllOf(
Property(&scsi_exception::get_sense_key, sense_key::ILLEGAL_REQUEST),
Property(&scsi_exception::get_asc, asc::INVALID_FIELD_IN_PARAMETER_LIST))))
<< "Not enough command parameters";
ModeSelect(scsi_command::eCmdModeSelect10, cdb, buf, LENGTH, 512);
ModeSelect(logger, scsi_command::eCmdModeSelect10, cdb, buf, LENGTH, 512);
}
TEST(ScsiCommandUtilTest, EnrichFormatPage)