2022-10-04 15:23:42 +00:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
//
|
2022-12-05 17:58:23 +00:00
|
|
|
// SCSI Target Emulator PiSCSI
|
2022-10-04 15:23:42 +00:00
|
|
|
// for Raspberry Pi
|
|
|
|
//
|
2023-10-15 06:38:15 +00:00
|
|
|
// Copyright (C) 2022-2023 Uwe Seimet
|
2022-10-04 15:23:42 +00:00
|
|
|
//
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#include "mocks.h"
|
2022-12-05 17:58:23 +00:00
|
|
|
#include "shared/piscsi_version.h"
|
2022-10-04 15:23:42 +00:00
|
|
|
#include "controllers/controller_manager.h"
|
|
|
|
#include "devices/device_factory.h"
|
2022-12-05 17:58:23 +00:00
|
|
|
#include "generated/piscsi_interface.pb.h"
|
|
|
|
#include "piscsi/piscsi_response.h"
|
2023-10-15 06:38:15 +00:00
|
|
|
#include <sys/stat.h>
|
2022-10-04 15:23:42 +00:00
|
|
|
|
2022-12-05 17:58:23 +00:00
|
|
|
using namespace piscsi_interface;
|
2022-10-04 15:23:42 +00:00
|
|
|
|
2022-12-05 17:58:23 +00:00
|
|
|
TEST(PiscsiResponseTest, Operation_Count)
|
2022-10-04 15:23:42 +00:00
|
|
|
{
|
2022-12-05 17:58:23 +00:00
|
|
|
PiscsiResponse response;
|
2022-10-04 15:23:42 +00:00
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
PbOperationInfo info;
|
|
|
|
response.GetOperationInfo(info, 0);
|
|
|
|
EXPECT_EQ(PbOperation_ARRAYSIZE - 1, info.operations_size());
|
2022-10-04 15:23:42 +00:00
|
|
|
}
|
|
|
|
|
2022-10-23 19:51:39 +00:00
|
|
|
void TestNonDiskDevice(PbDeviceType type, int default_param_count)
|
2022-10-04 15:23:42 +00:00
|
|
|
{
|
2022-11-02 14:36:19 +00:00
|
|
|
auto bus = make_shared<MockBus>();
|
2023-10-15 06:38:15 +00:00
|
|
|
ControllerManager controller_manager;
|
2022-10-04 15:23:42 +00:00
|
|
|
DeviceFactory device_factory;
|
2022-12-05 17:58:23 +00:00
|
|
|
PiscsiResponse response;
|
2022-10-04 15:23:42 +00:00
|
|
|
|
2022-11-04 07:22:32 +00:00
|
|
|
auto d = device_factory.CreateDevice(type, 0, "");
|
2023-10-15 06:38:15 +00:00
|
|
|
const param_map params;
|
2022-11-02 14:36:19 +00:00
|
|
|
d->Init(params);
|
2023-10-15 06:38:15 +00:00
|
|
|
EXPECT_TRUE(controller_manager.AttachToController(*bus, 0, d));
|
2022-10-04 15:23:42 +00:00
|
|
|
|
2022-10-23 19:51:39 +00:00
|
|
|
PbServerInfo info;
|
2023-10-15 06:38:15 +00:00
|
|
|
response.GetDevices(controller_manager.GetAllDevices(), info, "image_folder");
|
2022-10-04 15:23:42 +00:00
|
|
|
|
2022-10-23 19:51:39 +00:00
|
|
|
EXPECT_EQ(1, info.devices_info().devices().size());
|
2022-11-02 14:36:19 +00:00
|
|
|
|
2022-10-23 19:51:39 +00:00
|
|
|
const auto& device = info.devices_info().devices()[0];
|
2022-10-04 15:23:42 +00:00
|
|
|
EXPECT_FALSE(device.properties().read_only());
|
|
|
|
EXPECT_FALSE(device.properties().protectable());
|
|
|
|
EXPECT_FALSE(device.properties().stoppable());
|
|
|
|
EXPECT_FALSE(device.properties().removable());
|
|
|
|
EXPECT_FALSE(device.properties().lockable());
|
|
|
|
EXPECT_EQ(32, device.properties().luns());
|
|
|
|
EXPECT_EQ(0, device.block_size());
|
|
|
|
EXPECT_EQ(0, device.block_count());
|
|
|
|
EXPECT_EQ(default_param_count, device.properties().default_params().size());
|
|
|
|
EXPECT_FALSE(device.properties().supports_file());
|
|
|
|
if (default_param_count) {
|
|
|
|
EXPECT_TRUE(device.properties().supports_params());
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
EXPECT_FALSE(device.properties().supports_params());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-05 17:58:23 +00:00
|
|
|
TEST(PiscsiResponseTest, GetDevices)
|
2022-10-04 15:23:42 +00:00
|
|
|
{
|
2022-10-23 19:51:39 +00:00
|
|
|
TestNonDiskDevice(SCHS, 0);
|
|
|
|
TestNonDiskDevice(SCLP, 1);
|
2022-10-04 15:23:42 +00:00
|
|
|
}
|
|
|
|
|
2022-12-05 17:58:23 +00:00
|
|
|
TEST(PiscsiResponseTest, GetImageFile)
|
2022-10-04 15:23:42 +00:00
|
|
|
{
|
2022-12-05 17:58:23 +00:00
|
|
|
PiscsiResponse response;
|
2022-10-23 19:51:39 +00:00
|
|
|
PbImageFile image_file;
|
|
|
|
|
|
|
|
EXPECT_FALSE(response.GetImageFile(image_file, "default_folder", ""));
|
|
|
|
|
|
|
|
// Even though the call fails (non-existing file) some properties must be set
|
|
|
|
EXPECT_FALSE(response.GetImageFile(image_file, "default_folder", "filename.hds"));
|
|
|
|
EXPECT_EQ("filename.hds", image_file.name());
|
|
|
|
EXPECT_EQ(SCHD, image_file.type());
|
2022-10-04 15:23:42 +00:00
|
|
|
}
|
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
TEST(PiscsiResponseTest, GetImageFilesInfo)
|
|
|
|
{
|
|
|
|
PiscsiResponse response;
|
|
|
|
|
|
|
|
PbImageFilesInfo info;
|
|
|
|
response.GetImageFilesInfo(info, "default_folder", "", "", 1);
|
|
|
|
EXPECT_TRUE(info.image_files().empty());
|
|
|
|
}
|
|
|
|
|
2022-12-05 17:58:23 +00:00
|
|
|
TEST(PiscsiResponseTest, GetReservedIds)
|
2022-10-04 15:23:42 +00:00
|
|
|
{
|
2022-12-05 17:58:23 +00:00
|
|
|
PiscsiResponse response;
|
2022-10-04 15:23:42 +00:00
|
|
|
unordered_set<int> ids;
|
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
PbReservedIdsInfo info1;
|
|
|
|
response.GetReservedIds(info1, ids);
|
|
|
|
EXPECT_TRUE(info1.ids().empty());
|
2022-10-04 15:23:42 +00:00
|
|
|
|
|
|
|
ids.insert(3);
|
2023-10-15 06:38:15 +00:00
|
|
|
PbReservedIdsInfo info2;
|
|
|
|
response.GetReservedIds(info2, ids);
|
|
|
|
EXPECT_EQ(1, info2.ids().size());
|
|
|
|
EXPECT_EQ(3, info2.ids()[0]);
|
2022-10-23 19:51:39 +00:00
|
|
|
}
|
|
|
|
|
2022-12-05 17:58:23 +00:00
|
|
|
TEST(PiscsiResponseTest, GetDevicesInfo)
|
2022-10-23 19:51:39 +00:00
|
|
|
{
|
|
|
|
const int ID = 2;
|
|
|
|
const int LUN1 = 0;
|
|
|
|
const int LUN2 = 5;
|
|
|
|
const int LUN3 = 6;
|
|
|
|
|
2022-11-04 07:22:32 +00:00
|
|
|
auto bus = make_shared<MockBus>();
|
2023-10-15 06:38:15 +00:00
|
|
|
ControllerManager controller_manager;
|
2022-12-05 17:58:23 +00:00
|
|
|
PiscsiResponse response;
|
2022-10-23 19:51:39 +00:00
|
|
|
PbCommand command;
|
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
PbResult result1;
|
|
|
|
response.GetDevicesInfo(controller_manager.GetAllDevices(), result1, command, "");
|
|
|
|
EXPECT_TRUE(result1.status());
|
|
|
|
EXPECT_TRUE(result1.devices_info().devices().empty());
|
2022-10-23 19:51:39 +00:00
|
|
|
|
2022-11-04 07:22:32 +00:00
|
|
|
auto device1 = make_shared<MockHostServices>(LUN1);
|
2023-10-15 06:38:15 +00:00
|
|
|
EXPECT_TRUE(controller_manager.AttachToController(*bus, ID, device1));
|
2022-10-23 19:51:39 +00:00
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
response.GetDevicesInfo(controller_manager.GetAllDevices(), result1, command, "");
|
|
|
|
EXPECT_TRUE(result1.status());
|
|
|
|
auto& devices1 = result1.devices_info().devices();
|
2022-10-23 19:51:39 +00:00
|
|
|
EXPECT_EQ(1, devices1.size());
|
|
|
|
EXPECT_EQ(SCHS, devices1[0].type());
|
|
|
|
EXPECT_EQ(ID, devices1[0].id());
|
|
|
|
EXPECT_EQ(LUN1, devices1[0].unit());
|
|
|
|
|
|
|
|
auto device2 = make_shared<MockSCSIHD_NEC>(LUN2);
|
2023-10-15 06:38:15 +00:00
|
|
|
EXPECT_TRUE(controller_manager.AttachToController(*bus, ID, device2));
|
2022-10-23 19:51:39 +00:00
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
PbResult result2;
|
|
|
|
response.GetDevicesInfo(controller_manager.GetAllDevices(), result2, command, "");
|
|
|
|
EXPECT_TRUE(result2.status());
|
|
|
|
auto& devices2 = result2.devices_info().devices();
|
|
|
|
EXPECT_EQ(2, devices2.size()) << "Device count mismatch";
|
2022-10-23 19:51:39 +00:00
|
|
|
|
|
|
|
auto requested_device = command.add_devices();
|
|
|
|
requested_device->set_id(ID);
|
|
|
|
requested_device->set_unit(LUN1);
|
2023-10-15 06:38:15 +00:00
|
|
|
PbResult result3;
|
|
|
|
response.GetDevicesInfo(controller_manager.GetAllDevices(), result3, command, "");
|
|
|
|
EXPECT_TRUE(result3.status());
|
|
|
|
auto& devices3 = result3.devices_info().devices();
|
2022-10-23 19:51:39 +00:00
|
|
|
EXPECT_EQ(1, devices3.size()) << "Only data for the specified ID and LUN must be returned";
|
|
|
|
EXPECT_EQ(SCHS, devices3[0].type());
|
|
|
|
EXPECT_EQ(ID, devices3[0].id());
|
|
|
|
EXPECT_EQ(LUN1, devices3[0].unit());
|
|
|
|
|
|
|
|
requested_device->set_id(ID);
|
|
|
|
requested_device->set_unit(LUN3);
|
2023-10-15 06:38:15 +00:00
|
|
|
PbResult result4;
|
|
|
|
response.GetDevicesInfo(controller_manager.GetAllDevices(), result4, command, "");
|
|
|
|
EXPECT_FALSE(result4.status()) << "Only data for the specified ID and LUN must be returned";
|
2022-10-23 19:51:39 +00:00
|
|
|
}
|
|
|
|
|
2022-12-05 17:58:23 +00:00
|
|
|
TEST(PiscsiResponseTest, GetDeviceTypesInfo)
|
2022-10-23 19:51:39 +00:00
|
|
|
{
|
2022-12-05 17:58:23 +00:00
|
|
|
PiscsiResponse response;
|
2022-10-23 19:51:39 +00:00
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
PbDeviceTypesInfo info;
|
|
|
|
response.GetDeviceTypesInfo(info);
|
|
|
|
EXPECT_EQ(8, info.properties().size());
|
2022-10-23 19:51:39 +00:00
|
|
|
}
|
|
|
|
|
2022-12-05 17:58:23 +00:00
|
|
|
TEST(PiscsiResponseTest, GetServerInfo)
|
2022-10-23 19:51:39 +00:00
|
|
|
{
|
2022-11-04 07:22:32 +00:00
|
|
|
auto bus = make_shared<MockBus>();
|
2022-12-05 17:58:23 +00:00
|
|
|
PiscsiResponse response;
|
2022-11-04 07:22:32 +00:00
|
|
|
const unordered_set<shared_ptr<PrimaryDevice>> devices;
|
2022-10-23 19:51:39 +00:00
|
|
|
const unordered_set<int> ids = { 1, 3 };
|
2023-10-15 06:38:15 +00:00
|
|
|
|
|
|
|
PbServerInfo info;
|
|
|
|
response.GetServerInfo(info, devices, ids, "default_folder", "", "", 1234);
|
|
|
|
EXPECT_EQ(piscsi_major_version, info.version_info().major_version());
|
|
|
|
EXPECT_EQ(piscsi_minor_version, info.version_info().minor_version());
|
|
|
|
EXPECT_EQ(piscsi_patch_version, info.version_info().patch_version());
|
|
|
|
EXPECT_EQ(level::level_string_views[get_level()], info.log_level_info().current_log_level());
|
|
|
|
EXPECT_EQ("default_folder", info.image_files_info().default_image_folder());
|
|
|
|
EXPECT_EQ(1234, info.image_files_info().depth());
|
|
|
|
EXPECT_EQ(2, info.reserved_ids_info().ids().size());
|
2022-10-23 19:51:39 +00:00
|
|
|
}
|
|
|
|
|
2022-12-05 17:58:23 +00:00
|
|
|
TEST(PiscsiResponseTest, GetVersionInfo)
|
2022-10-23 19:51:39 +00:00
|
|
|
{
|
2022-12-05 17:58:23 +00:00
|
|
|
PiscsiResponse response;
|
2022-10-23 19:51:39 +00:00
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
PbVersionInfo info;
|
|
|
|
response.GetVersionInfo(info);
|
|
|
|
EXPECT_EQ(piscsi_major_version, info.major_version());
|
|
|
|
EXPECT_EQ(piscsi_minor_version, info.minor_version());
|
|
|
|
EXPECT_EQ(piscsi_patch_version, info.patch_version());
|
2022-10-23 19:51:39 +00:00
|
|
|
}
|
|
|
|
|
2022-12-05 17:58:23 +00:00
|
|
|
TEST(PiscsiResponseTest, GetLogLevelInfo)
|
2022-10-23 19:51:39 +00:00
|
|
|
{
|
2022-12-05 17:58:23 +00:00
|
|
|
PiscsiResponse response;
|
2022-10-23 19:51:39 +00:00
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
PbLogLevelInfo info;
|
|
|
|
response.GetLogLevelInfo(info);
|
|
|
|
EXPECT_EQ(level::level_string_views[get_level()], info.current_log_level());
|
|
|
|
EXPECT_EQ(7, info.log_levels().size());
|
2022-10-23 19:51:39 +00:00
|
|
|
}
|
|
|
|
|
2022-12-05 17:58:23 +00:00
|
|
|
TEST(PiscsiResponseTest, GetNetworkInterfacesInfo)
|
2022-10-23 19:51:39 +00:00
|
|
|
{
|
2022-12-05 17:58:23 +00:00
|
|
|
PiscsiResponse response;
|
2022-10-23 19:51:39 +00:00
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
PbNetworkInterfacesInfo info;
|
|
|
|
response.GetNetworkInterfacesInfo(info);
|
|
|
|
EXPECT_FALSE(info.name().empty());
|
2022-10-23 19:51:39 +00:00
|
|
|
}
|
|
|
|
|
2022-12-05 17:58:23 +00:00
|
|
|
TEST(PiscsiResponseTest, GetMappingInfo)
|
2022-10-23 19:51:39 +00:00
|
|
|
{
|
2022-12-05 17:58:23 +00:00
|
|
|
PiscsiResponse response;
|
2022-10-23 19:51:39 +00:00
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
PbMappingInfo info;
|
|
|
|
response.GetMappingInfo(info);
|
|
|
|
EXPECT_EQ(10, info.mapping().size());
|
2022-10-04 15:23:42 +00:00
|
|
|
}
|