2022-09-25 21:49:24 +00:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
//
|
2022-12-05 17:58:23 +00:00
|
|
|
// SCSI Target Emulator PiSCSI
|
2022-09-25 21:49:24 +00:00
|
|
|
// for Raspberry Pi
|
|
|
|
//
|
|
|
|
// Copyright (C) 2022 Uwe Seimet
|
|
|
|
//
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2022-10-04 15:23:42 +00:00
|
|
|
#include "mocks.h"
|
2022-11-10 06:44:06 +00:00
|
|
|
#include "shared/protobuf_util.h"
|
2022-12-05 17:58:23 +00:00
|
|
|
#include "generated/piscsi_interface.pb.h"
|
2022-09-25 21:49:24 +00:00
|
|
|
|
2022-12-05 17:58:23 +00:00
|
|
|
using namespace piscsi_interface;
|
2022-10-08 17:26:04 +00:00
|
|
|
using namespace protobuf_util;
|
2022-09-25 21:49:24 +00:00
|
|
|
|
|
|
|
void TestSpecialDevice(const string& name)
|
|
|
|
{
|
|
|
|
PbDeviceDefinition device;
|
|
|
|
ParseParameters(device, name);
|
|
|
|
EXPECT_EQ(name, GetParam(device, "file"));
|
|
|
|
EXPECT_EQ("", GetParam(device, "interfaces"));
|
|
|
|
}
|
|
|
|
|
2022-11-02 22:41:45 +00:00
|
|
|
TEST(ProtobufUtil, AddGetParam)
|
2022-09-25 21:49:24 +00:00
|
|
|
{
|
|
|
|
PbCommand command;
|
2022-10-23 19:51:39 +00:00
|
|
|
SetParam(command, "key", "value");
|
2022-09-25 21:49:24 +00:00
|
|
|
EXPECT_EQ("value", GetParam(command, "key"));
|
|
|
|
EXPECT_EQ("", GetParam(command, "xyz"));
|
|
|
|
|
|
|
|
PbDeviceDefinition definition;
|
2022-10-23 19:51:39 +00:00
|
|
|
SetParam(definition, "key", "value");
|
2022-09-25 21:49:24 +00:00
|
|
|
EXPECT_EQ("value", GetParam(definition, "key"));
|
|
|
|
EXPECT_EQ("", GetParam(definition, "xyz"));
|
|
|
|
|
|
|
|
PbDevice device;
|
2022-10-23 19:51:39 +00:00
|
|
|
SetParam(device, "key", "value");
|
2022-09-25 21:49:24 +00:00
|
|
|
const auto& it = device.params().find("key");
|
|
|
|
EXPECT_EQ("value", it->second);
|
|
|
|
}
|
|
|
|
|
2022-11-02 22:41:45 +00:00
|
|
|
TEST(ProtobufUtil, ParseParameters)
|
2022-09-25 21:49:24 +00:00
|
|
|
{
|
|
|
|
PbDeviceDefinition device1;
|
|
|
|
ParseParameters(device1, "a=b:c=d:e");
|
|
|
|
EXPECT_EQ("b", GetParam(device1, "a"));
|
|
|
|
EXPECT_EQ("d", GetParam(device1, "c"));
|
|
|
|
EXPECT_EQ("", GetParam(device1, "e"));
|
|
|
|
|
2022-11-02 22:41:45 +00:00
|
|
|
// Old style parameter
|
2022-09-25 21:49:24 +00:00
|
|
|
PbDeviceDefinition device2;
|
|
|
|
ParseParameters(device2, "a");
|
|
|
|
EXPECT_EQ("a", GetParam(device2, "file"));
|
|
|
|
|
|
|
|
TestSpecialDevice("bridge");
|
|
|
|
TestSpecialDevice("daynaport");
|
|
|
|
TestSpecialDevice("printer");
|
|
|
|
TestSpecialDevice("services");
|
|
|
|
}
|
2022-10-08 17:26:04 +00:00
|
|
|
|
2022-11-02 22:41:45 +00:00
|
|
|
TEST(ProtobufUtil, SetPatternParams)
|
2022-10-08 17:26:04 +00:00
|
|
|
{
|
|
|
|
PbCommand command1;
|
|
|
|
SetPatternParams(command1, "file");
|
|
|
|
EXPECT_EQ("", GetParam(command1, "folder_pattern"));
|
|
|
|
EXPECT_EQ("file", GetParam(command1, "file_pattern"));
|
|
|
|
|
|
|
|
PbCommand command2;
|
|
|
|
SetPatternParams(command2, ":file");
|
|
|
|
EXPECT_EQ("", GetParam(command2, "folder_pattern"));
|
|
|
|
EXPECT_EQ("file", GetParam(command2, "file_pattern"));
|
|
|
|
|
|
|
|
PbCommand command3;
|
|
|
|
SetPatternParams(command3, "folder:");
|
|
|
|
EXPECT_EQ("folder", GetParam(command3, "folder_pattern"));
|
|
|
|
EXPECT_EQ("", GetParam(command3, "file_pattern"));
|
|
|
|
|
|
|
|
PbCommand command4;
|
|
|
|
SetPatternParams(command4, "folder:file");
|
|
|
|
EXPECT_EQ("folder", GetParam(command4, "folder_pattern"));
|
|
|
|
EXPECT_EQ("file", GetParam(command4, "file_pattern"));
|
|
|
|
}
|
2022-11-10 06:44:06 +00:00
|
|
|
|
|
|
|
TEST(ProtobufUtil, ListDevices)
|
|
|
|
{
|
|
|
|
list<PbDevice> devices;
|
|
|
|
|
|
|
|
EXPECT_FALSE(ListDevices(devices).empty());
|
|
|
|
|
|
|
|
PbDevice device;
|
|
|
|
device.set_type(SCHD);
|
|
|
|
devices.push_back(device);
|
|
|
|
device.set_type(SCBR);
|
|
|
|
devices.push_back(device);
|
|
|
|
device.set_type(SCDP);
|
|
|
|
devices.push_back(device);
|
|
|
|
device.set_type(SCHS);
|
|
|
|
devices.push_back(device);
|
|
|
|
device.set_type(SCLP);
|
|
|
|
devices.push_back(device);
|
|
|
|
const string device_list = ListDevices(devices);
|
|
|
|
EXPECT_FALSE(device_list.empty());
|
|
|
|
EXPECT_NE(string::npos, device_list.find("X68000 HOST BRIDGE"));
|
|
|
|
EXPECT_NE(string::npos, device_list.find("DaynaPort SCSI/Link"));
|
|
|
|
EXPECT_NE(string::npos, device_list.find("Host Services"));
|
|
|
|
EXPECT_NE(string::npos, device_list.find("SCSI Printer"));
|
|
|
|
}
|
2022-11-11 20:08:48 +00:00
|
|
|
|
|
|
|
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());
|
|
|
|
}
|