RASCSI/cpp/test/device_test.cpp

280 lines
7.0 KiB
C++
Raw Normal View History

//---------------------------------------------------------------------------
//
// SCSI Target Emulator PiSCSI
// for Raspberry Pi
//
// Copyright (C) 2022 Uwe Seimet
//
//---------------------------------------------------------------------------
#include "mocks.h"
#include "shared/piscsi_exceptions.h"
SASI code removal, error handling update, bug fixes, code cleanup (#806) Summary ov most important changes triggered by the SASI code removal: - Removed the SASI controller code - New controller management. There is a new controller base class AbstractController and a class ControllerManager managing the controller lifecycle. The lifecycle management was removed from rasci.cpp and is covered by unit tests. - New device management. The DeviceFactory manages the device lifecycle instead of rascsi.cpp. The new code is covered by unit tests. - The lifecycle managment uses C++ collections with variable size instead of arrays with hard-coded sizes. - The ScsiController method contains most of what was previously contained in scsidev_ctrl.cpp plus the code from sasidev_ctrl.cpp that was relevant for SCSI. - scsi_command_util contains helper methods used for identical SCSI command implementations of more than one device - Devices know their controllers, so that the controller instance does not need to be passed to each SCSI command. This change helps to decouple the devices from the controller. The phase_handler interface is also part of this decoupling. - Use scsi_command_exception for propagating SCSI command execution errors, This resolves issues with the previous error handling, which was based on return values and often on magic numbers. - Removed legacy SCSI error codes, all errors are now encoded by sense_key::, asc:: and status::. - Fixed various warnings reported with -Wextra, -Weffc++ and -Wpedantic. - Use constructor member initialization lists (recommended for ISO C++) - Consistently use new/delete instead of malloc/free (recommended for ISO C++), resulting in better type safety and error handling - Replaced variable sized arrays on the stack (violates ISO C++ and can cause a stack overflow) - Replaced NULL by nullptr (recommended for C++), resulting in better type safety - Use more const member functions in order to avoid side effects - The format device page can now also be changed for hard disk drives (Fujitsu M2624S supports this, for instance), not just for MOs. - Better encapsulation, updated access specifiers in many places - Removed unused methods and method arguments - Fixed a number of TODOs - Added/updated unit tests for a lot of non-legacy classes - Makefile support for creating HTML coverage reports with lcov/genhtml
2022-09-03 14:53:53 +00:00
#include "devices/device.h"
TEST(DeviceTest, Properties)
{
const int LUN = 5;
MockDevice device(LUN);
EXPECT_FALSE(device.IsReady()) << "Wrong default value";
device.SetReady(true);
EXPECT_TRUE(device.IsReady());
device.SetReady(false);
EXPECT_FALSE(device.IsReady());
EXPECT_FALSE(device.IsReset()) << "Wrong default value";
device.SetReset(true);
EXPECT_TRUE(device.IsReset());
device.SetReset(false);
EXPECT_FALSE(device.IsReset());
EXPECT_FALSE(device.IsAttn()) << "Wrong default value";
device.SetAttn(true);
EXPECT_TRUE(device.IsAttn());
device.SetAttn(false);
EXPECT_FALSE(device.IsAttn());
EXPECT_FALSE(device.IsReadOnly()) << "Wrong default value";
device.SetReadOnly(true);
EXPECT_TRUE(device.IsReadOnly());
device.SetReadOnly(false);
EXPECT_FALSE(device.IsReadOnly());
EXPECT_FALSE(device.IsProtectable()) << "Wrong default value";
device.SetProtectable(true);
EXPECT_TRUE(device.IsProtectable());
device.SetProtectable(false);
EXPECT_FALSE(device.IsProtectable());
EXPECT_FALSE(device.IsProtected()) << "Wrong default value";
device.SetProtected(true);
EXPECT_FALSE(device.IsProtected());
device.SetProtectable(true);
device.SetProtected(true);
EXPECT_TRUE(device.IsProtected());
device.SetProtected(false);
EXPECT_FALSE(device.IsProtected());
device.SetProtectable(false);
device.SetReadOnly(true);
device.SetProtected(true);
EXPECT_FALSE(device.IsProtected()) << "Read-only or not protectable devices cannot be protected";
device.SetReadOnly(false);
device.SetProtected(true);
EXPECT_FALSE(device.IsProtected()) << "Read-only or not protectable devices cannot be protected";
EXPECT_FALSE(device.IsStoppable()) << "Wrong default value";
device.SetStoppable(true);
EXPECT_TRUE(device.IsStoppable());
device.SetStoppable(false);
EXPECT_FALSE(device.IsStoppable());
EXPECT_FALSE(device.IsStopped()) << "Wrong default value";
device.SetStopped(true);
EXPECT_TRUE(device.IsStopped());
device.SetStopped(false);
EXPECT_FALSE(device.IsStopped());
EXPECT_FALSE(device.IsRemovable()) << "Wrong default value";
device.SetRemovable(true);
EXPECT_TRUE(device.IsRemovable());
device.SetRemovable(false);
EXPECT_FALSE(device.IsRemovable());
EXPECT_FALSE(device.IsRemoved()) << "Wrong default value";
device.SetRemoved(true);
EXPECT_TRUE(device.IsRemoved());
device.SetRemoved(false);
EXPECT_FALSE(device.IsRemoved());
EXPECT_FALSE(device.IsLockable()) << "Wrong default value";
device.SetLockable(true);
EXPECT_TRUE(device.IsLockable());
device.SetLockable(false);
EXPECT_FALSE(device.IsLockable());
EXPECT_FALSE(device.IsLocked()) << "Wrong default value";
device.SetLocked(true);
EXPECT_TRUE(device.IsLocked());
device.SetLocked(false);
EXPECT_FALSE(device.IsLocked());
EXPECT_FALSE(device.SupportsParams()) << "Wrong default value";
device.SupportsParams(true);
EXPECT_TRUE(device.SupportsParams());
device.SupportsParams(false);
EXPECT_FALSE(device.SupportsParams());
EXPECT_FALSE(device.SupportsFile()) << "Wrong default value";
device.SupportsFile(true);
EXPECT_TRUE(device.SupportsFile());
device.SupportsFile(false);
EXPECT_FALSE(device.SupportsFile());
EXPECT_EQ(LUN, device.GetLun());
}
TEST(DeviceTest, GetTypeString)
{
MockDevice schd(SCHD);
EXPECT_STREQ("SCHD", schd.GetTypeString());
MockDevice scrm(SCRM);
EXPECT_STREQ("SCRM", scrm.GetTypeString());
MockDevice scmo(SCMO);
EXPECT_STREQ("SCMO", scmo.GetTypeString());
MockDevice sccd(SCCD);
EXPECT_STREQ("SCCD", sccd.GetTypeString());
MockDevice schs(SCHS);
EXPECT_STREQ("SCHS", schs.GetTypeString());
MockDevice scbr(SCBR);
EXPECT_STREQ("SCBR", scbr.GetTypeString());
MockDevice scdp(SCDP);
EXPECT_STREQ("SCDP", scdp.GetTypeString());
MockDevice sclp(SCLP);
EXPECT_STREQ("SCLP", sclp.GetTypeString());
}
TEST(DeviceTest, Vendor)
{
MockDevice device(0);
EXPECT_THROW(device.SetVendor(""), invalid_argument);
EXPECT_THROW(device.SetVendor("123456789"), invalid_argument);
SASI code removal, error handling update, bug fixes, code cleanup (#806) Summary ov most important changes triggered by the SASI code removal: - Removed the SASI controller code - New controller management. There is a new controller base class AbstractController and a class ControllerManager managing the controller lifecycle. The lifecycle management was removed from rasci.cpp and is covered by unit tests. - New device management. The DeviceFactory manages the device lifecycle instead of rascsi.cpp. The new code is covered by unit tests. - The lifecycle managment uses C++ collections with variable size instead of arrays with hard-coded sizes. - The ScsiController method contains most of what was previously contained in scsidev_ctrl.cpp plus the code from sasidev_ctrl.cpp that was relevant for SCSI. - scsi_command_util contains helper methods used for identical SCSI command implementations of more than one device - Devices know their controllers, so that the controller instance does not need to be passed to each SCSI command. This change helps to decouple the devices from the controller. The phase_handler interface is also part of this decoupling. - Use scsi_command_exception for propagating SCSI command execution errors, This resolves issues with the previous error handling, which was based on return values and often on magic numbers. - Removed legacy SCSI error codes, all errors are now encoded by sense_key::, asc:: and status::. - Fixed various warnings reported with -Wextra, -Weffc++ and -Wpedantic. - Use constructor member initialization lists (recommended for ISO C++) - Consistently use new/delete instead of malloc/free (recommended for ISO C++), resulting in better type safety and error handling - Replaced variable sized arrays on the stack (violates ISO C++ and can cause a stack overflow) - Replaced NULL by nullptr (recommended for C++), resulting in better type safety - Use more const member functions in order to avoid side effects - The format device page can now also be changed for hard disk drives (Fujitsu M2624S supports this, for instance), not just for MOs. - Better encapsulation, updated access specifiers in many places - Removed unused methods and method arguments - Fixed a number of TODOs - Added/updated unit tests for a lot of non-legacy classes - Makefile support for creating HTML coverage reports with lcov/genhtml
2022-09-03 14:53:53 +00:00
device.SetVendor("12345678");
EXPECT_EQ("12345678", device.GetVendor());
}
TEST(DeviceTest, Product)
{
MockDevice device(0);
EXPECT_THROW(device.SetProduct(""), invalid_argument);
EXPECT_THROW(device.SetProduct("12345678901234567"), invalid_argument);
SASI code removal, error handling update, bug fixes, code cleanup (#806) Summary ov most important changes triggered by the SASI code removal: - Removed the SASI controller code - New controller management. There is a new controller base class AbstractController and a class ControllerManager managing the controller lifecycle. The lifecycle management was removed from rasci.cpp and is covered by unit tests. - New device management. The DeviceFactory manages the device lifecycle instead of rascsi.cpp. The new code is covered by unit tests. - The lifecycle managment uses C++ collections with variable size instead of arrays with hard-coded sizes. - The ScsiController method contains most of what was previously contained in scsidev_ctrl.cpp plus the code from sasidev_ctrl.cpp that was relevant for SCSI. - scsi_command_util contains helper methods used for identical SCSI command implementations of more than one device - Devices know their controllers, so that the controller instance does not need to be passed to each SCSI command. This change helps to decouple the devices from the controller. The phase_handler interface is also part of this decoupling. - Use scsi_command_exception for propagating SCSI command execution errors, This resolves issues with the previous error handling, which was based on return values and often on magic numbers. - Removed legacy SCSI error codes, all errors are now encoded by sense_key::, asc:: and status::. - Fixed various warnings reported with -Wextra, -Weffc++ and -Wpedantic. - Use constructor member initialization lists (recommended for ISO C++) - Consistently use new/delete instead of malloc/free (recommended for ISO C++), resulting in better type safety and error handling - Replaced variable sized arrays on the stack (violates ISO C++ and can cause a stack overflow) - Replaced NULL by nullptr (recommended for C++), resulting in better type safety - Use more const member functions in order to avoid side effects - The format device page can now also be changed for hard disk drives (Fujitsu M2624S supports this, for instance), not just for MOs. - Better encapsulation, updated access specifiers in many places - Removed unused methods and method arguments - Fixed a number of TODOs - Added/updated unit tests for a lot of non-legacy classes - Makefile support for creating HTML coverage reports with lcov/genhtml
2022-09-03 14:53:53 +00:00
device.SetProduct("1234567890123456");
EXPECT_EQ("1234567890123456", device.GetProduct());
device.SetProduct("xyz", false);
EXPECT_EQ("1234567890123456", device.GetProduct()) << "Changing vital product data is not SCSI compliant";
}
TEST(DeviceTest, Revision)
{
MockDevice device(0);
EXPECT_THROW(device.SetRevision(""), invalid_argument);
EXPECT_THROW(device.SetRevision("12345"), invalid_argument);
SASI code removal, error handling update, bug fixes, code cleanup (#806) Summary ov most important changes triggered by the SASI code removal: - Removed the SASI controller code - New controller management. There is a new controller base class AbstractController and a class ControllerManager managing the controller lifecycle. The lifecycle management was removed from rasci.cpp and is covered by unit tests. - New device management. The DeviceFactory manages the device lifecycle instead of rascsi.cpp. The new code is covered by unit tests. - The lifecycle managment uses C++ collections with variable size instead of arrays with hard-coded sizes. - The ScsiController method contains most of what was previously contained in scsidev_ctrl.cpp plus the code from sasidev_ctrl.cpp that was relevant for SCSI. - scsi_command_util contains helper methods used for identical SCSI command implementations of more than one device - Devices know their controllers, so that the controller instance does not need to be passed to each SCSI command. This change helps to decouple the devices from the controller. The phase_handler interface is also part of this decoupling. - Use scsi_command_exception for propagating SCSI command execution errors, This resolves issues with the previous error handling, which was based on return values and often on magic numbers. - Removed legacy SCSI error codes, all errors are now encoded by sense_key::, asc:: and status::. - Fixed various warnings reported with -Wextra, -Weffc++ and -Wpedantic. - Use constructor member initialization lists (recommended for ISO C++) - Consistently use new/delete instead of malloc/free (recommended for ISO C++), resulting in better type safety and error handling - Replaced variable sized arrays on the stack (violates ISO C++ and can cause a stack overflow) - Replaced NULL by nullptr (recommended for C++), resulting in better type safety - Use more const member functions in order to avoid side effects - The format device page can now also be changed for hard disk drives (Fujitsu M2624S supports this, for instance), not just for MOs. - Better encapsulation, updated access specifiers in many places - Removed unused methods and method arguments - Fixed a number of TODOs - Added/updated unit tests for a lot of non-legacy classes - Makefile support for creating HTML coverage reports with lcov/genhtml
2022-09-03 14:53:53 +00:00
device.SetRevision("1234");
EXPECT_EQ("1234", device.GetRevision());
}
TEST(DeviceTest, GetPaddedName)
{
MockDevice device(0);
SASI code removal, error handling update, bug fixes, code cleanup (#806) Summary ov most important changes triggered by the SASI code removal: - Removed the SASI controller code - New controller management. There is a new controller base class AbstractController and a class ControllerManager managing the controller lifecycle. The lifecycle management was removed from rasci.cpp and is covered by unit tests. - New device management. The DeviceFactory manages the device lifecycle instead of rascsi.cpp. The new code is covered by unit tests. - The lifecycle managment uses C++ collections with variable size instead of arrays with hard-coded sizes. - The ScsiController method contains most of what was previously contained in scsidev_ctrl.cpp plus the code from sasidev_ctrl.cpp that was relevant for SCSI. - scsi_command_util contains helper methods used for identical SCSI command implementations of more than one device - Devices know their controllers, so that the controller instance does not need to be passed to each SCSI command. This change helps to decouple the devices from the controller. The phase_handler interface is also part of this decoupling. - Use scsi_command_exception for propagating SCSI command execution errors, This resolves issues with the previous error handling, which was based on return values and often on magic numbers. - Removed legacy SCSI error codes, all errors are now encoded by sense_key::, asc:: and status::. - Fixed various warnings reported with -Wextra, -Weffc++ and -Wpedantic. - Use constructor member initialization lists (recommended for ISO C++) - Consistently use new/delete instead of malloc/free (recommended for ISO C++), resulting in better type safety and error handling - Replaced variable sized arrays on the stack (violates ISO C++ and can cause a stack overflow) - Replaced NULL by nullptr (recommended for C++), resulting in better type safety - Use more const member functions in order to avoid side effects - The format device page can now also be changed for hard disk drives (Fujitsu M2624S supports this, for instance), not just for MOs. - Better encapsulation, updated access specifiers in many places - Removed unused methods and method arguments - Fixed a number of TODOs - Added/updated unit tests for a lot of non-legacy classes - Makefile support for creating HTML coverage reports with lcov/genhtml
2022-09-03 14:53:53 +00:00
device.SetVendor("V");
device.SetProduct("P");
device.SetRevision("R");
SASI code removal, error handling update, bug fixes, code cleanup (#806) Summary ov most important changes triggered by the SASI code removal: - Removed the SASI controller code - New controller management. There is a new controller base class AbstractController and a class ControllerManager managing the controller lifecycle. The lifecycle management was removed from rasci.cpp and is covered by unit tests. - New device management. The DeviceFactory manages the device lifecycle instead of rascsi.cpp. The new code is covered by unit tests. - The lifecycle managment uses C++ collections with variable size instead of arrays with hard-coded sizes. - The ScsiController method contains most of what was previously contained in scsidev_ctrl.cpp plus the code from sasidev_ctrl.cpp that was relevant for SCSI. - scsi_command_util contains helper methods used for identical SCSI command implementations of more than one device - Devices know their controllers, so that the controller instance does not need to be passed to each SCSI command. This change helps to decouple the devices from the controller. The phase_handler interface is also part of this decoupling. - Use scsi_command_exception for propagating SCSI command execution errors, This resolves issues with the previous error handling, which was based on return values and often on magic numbers. - Removed legacy SCSI error codes, all errors are now encoded by sense_key::, asc:: and status::. - Fixed various warnings reported with -Wextra, -Weffc++ and -Wpedantic. - Use constructor member initialization lists (recommended for ISO C++) - Consistently use new/delete instead of malloc/free (recommended for ISO C++), resulting in better type safety and error handling - Replaced variable sized arrays on the stack (violates ISO C++ and can cause a stack overflow) - Replaced NULL by nullptr (recommended for C++), resulting in better type safety - Use more const member functions in order to avoid side effects - The format device page can now also be changed for hard disk drives (Fujitsu M2624S supports this, for instance), not just for MOs. - Better encapsulation, updated access specifiers in many places - Removed unused methods and method arguments - Fixed a number of TODOs - Added/updated unit tests for a lot of non-legacy classes - Makefile support for creating HTML coverage reports with lcov/genhtml
2022-09-03 14:53:53 +00:00
EXPECT_EQ("V P R ", device.GetPaddedName());
}
TEST(DeviceTest, Params)
{
MockDevice device(0);
unordered_map<string, string> params;
params["key"] = "value";
EXPECT_EQ("", device.GetParam("key"));
device.SetParams(params);
EXPECT_EQ("", device.GetParam("key"));
unordered_map<string, string> default_params;
default_params["key"] = "value";
device.SetDefaultParams(default_params);
EXPECT_EQ("", device.GetParam("key"));
device.SetParams(params);
EXPECT_EQ("value", device.GetParam("key"));
}
TEST(DeviceTest, StatusCode)
{
MockDevice device(0);
device.SetStatusCode(123);
EXPECT_EQ(123, device.GetStatusCode());
}
TEST(DeviceTest, Reset)
{
MockDevice device(0);
device.SetLocked(true);
device.SetAttn(true);
device.SetReset(true);
device.Reset();
EXPECT_FALSE(device.IsLocked());
EXPECT_FALSE(device.IsAttn());
EXPECT_FALSE(device.IsReset());
}
TEST(DeviceTest, Start)
{
MockDevice device(0);
device.SetStopped(true);
device.SetReady(false);
EXPECT_FALSE(device.Start());
EXPECT_TRUE(device.IsStopped());
device.SetReady(true);
EXPECT_TRUE(device.Start());
EXPECT_FALSE(device.IsStopped());
}
TEST(DeviceTest, Stop)
{
MockDevice device(0);
device.SetReady(true);
device.SetAttn(true);
device.SetStopped(false);
device.Stop();
EXPECT_FALSE(device.IsReady());
EXPECT_FALSE(device.IsAttn());
EXPECT_TRUE(device.IsStopped());
}
TEST(DeviceTest, Eject)
{
MockDevice device(0);
device.SetReady(false);
device.SetRemovable(false);
EXPECT_FALSE(device.Eject(false));
device.SetReady(true);
EXPECT_FALSE(device.Eject(false));
device.SetRemovable(true);
device.SetLocked(true);
EXPECT_FALSE(device.Eject(false));
EXPECT_TRUE(device.Eject(true));
device.SetReady(true);
device.SetLocked(false);
EXPECT_TRUE(device.Eject(false));
EXPECT_FALSE(device.IsReady());
EXPECT_FALSE(device.IsAttn());
EXPECT_TRUE(device.IsRemoved());
EXPECT_FALSE(device.IsLocked());
EXPECT_TRUE(device.IsStopped());
}