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
|
|
|
|
//
|
2023-10-15 06:38:15 +00:00
|
|
|
// Copyright (C) 2022-2023 Uwe Seimet
|
2022-09-25 21:49:24 +00:00
|
|
|
//
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2022-10-04 15:23:42 +00:00
|
|
|
#include "mocks.h"
|
2022-11-10 06:44:06 +00:00
|
|
|
#include "shared/protobuf_util.h"
|
2023-10-15 06:38:15 +00:00
|
|
|
#include "shared/piscsi_exceptions.h"
|
2022-12-05 17:58:23 +00:00
|
|
|
#include "generated/piscsi_interface.pb.h"
|
2023-10-15 06:38:15 +00:00
|
|
|
#include <filesystem>
|
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"));
|
|
|
|
}
|
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
TEST(ProtobufUtil, GetSetParam)
|
2022-09-25 21:49:24 +00:00
|
|
|
{
|
2023-10-15 06:38:15 +00:00
|
|
|
// The implementation is a function template, testing one possible T is sufficient
|
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"));
|
2023-10-15 06:38:15 +00:00
|
|
|
EXPECT_EQ("", GetParam(command, ""));
|
2022-09-25 21:49:24 +00:00
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
2023-10-15 06:38:15 +00:00
|
|
|
vector<PbDevice> devices;
|
2022-11-10 06:44:06 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
EXPECT_NE("", SetIdAndLun(device, ""));
|
|
|
|
EXPECT_EQ("", SetIdAndLun(device, "1"));
|
2022-11-11 20:08:48 +00:00
|
|
|
EXPECT_EQ(1, device.id());
|
2023-10-15 06:38:15 +00:00
|
|
|
EXPECT_EQ("", SetIdAndLun(device, "2:0"));
|
2022-11-11 20:08:48 +00:00
|
|
|
EXPECT_EQ(2, device.id());
|
|
|
|
EXPECT_EQ(0, device.unit());
|
|
|
|
}
|
2023-10-15 06:38:15 +00:00
|
|
|
|
|
|
|
TEST(ProtobufUtil, SerializeMessage)
|
|
|
|
{
|
|
|
|
PbResult result;
|
|
|
|
|
|
|
|
const int fd = open("/dev/null", O_WRONLY);
|
|
|
|
ASSERT_NE(-1, fd);
|
|
|
|
SerializeMessage(fd, result);
|
|
|
|
close(fd);
|
|
|
|
EXPECT_THROW(SerializeMessage(-1, result), io_exception) << "Writing a message must fail";
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(ProtobufUtil, DeserializeMessage)
|
|
|
|
{
|
|
|
|
PbResult result;
|
|
|
|
vector<byte> buf(1);
|
|
|
|
|
|
|
|
int fd = open("/dev/null", O_RDONLY);
|
|
|
|
ASSERT_NE(-1, fd);
|
|
|
|
EXPECT_THROW(DeserializeMessage(fd, result), io_exception) << "Reading the message header must fail";
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
auto [fd1, filename1] = OpenTempFile();
|
|
|
|
// Data size -1
|
|
|
|
buf = { byte{0xff}, byte{0xff}, byte{0xff}, byte{0xff} };
|
|
|
|
EXPECT_EQ(buf.size(), write(fd1, buf.data(), buf.size()));
|
|
|
|
close(fd1);
|
|
|
|
fd1 = open(filename1.c_str(), O_RDONLY);
|
|
|
|
ASSERT_NE(-1, fd1);
|
|
|
|
EXPECT_THROW(DeserializeMessage(fd1, result), io_exception) << "Invalid header was not rejected";
|
|
|
|
remove(filename1);
|
|
|
|
|
|
|
|
auto [fd2, filename2] = OpenTempFile();
|
|
|
|
// Data size 2
|
|
|
|
buf = { byte{0x02}, byte{0x00}, byte{0x00}, byte{0x00} };
|
|
|
|
EXPECT_EQ(buf.size(), write(fd2, buf.data(), buf.size()));
|
|
|
|
close(fd2);
|
|
|
|
fd2 = open(filename2.c_str(), O_RDONLY);
|
|
|
|
EXPECT_NE(-1, fd2);
|
|
|
|
EXPECT_THROW(DeserializeMessage(fd2, result), io_exception) << "Invalid data were not rejected";
|
|
|
|
remove(filename2);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(ProtobufUtil, SerializeDeserializeMessage)
|
|
|
|
{
|
|
|
|
PbResult result;
|
|
|
|
result.set_status(true);
|
|
|
|
|
|
|
|
auto [fd, filename] = OpenTempFile();
|
|
|
|
ASSERT_NE(-1, fd);
|
|
|
|
SerializeMessage(fd, result);
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
result.set_status(false);
|
|
|
|
fd = open(filename.c_str(), O_RDONLY);
|
|
|
|
ASSERT_NE(-1, fd);
|
|
|
|
DeserializeMessage(fd, result);
|
|
|
|
close(fd);
|
|
|
|
remove(filename);
|
|
|
|
|
|
|
|
EXPECT_TRUE(result.status());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(ProtobufUtil, ReadBytes)
|
|
|
|
{
|
|
|
|
vector<byte> buf1(1);
|
|
|
|
vector<byte> buf2;
|
|
|
|
|
|
|
|
int fd = open("/dev/null", O_RDONLY);
|
|
|
|
ASSERT_NE(-1, fd);
|
|
|
|
EXPECT_EQ(0, ReadBytes(fd, buf1));
|
|
|
|
EXPECT_EQ(0, ReadBytes(fd, buf2));
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
fd = open("/dev/zero", O_RDONLY);
|
|
|
|
ASSERT_NE(-1, fd);
|
|
|
|
EXPECT_EQ(1, ReadBytes(fd, buf1));
|
|
|
|
EXPECT_EQ(0, ReadBytes(fd, buf2));
|
|
|
|
close(fd);
|
|
|
|
}
|