2022-10-04 17:23:42 +02:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
//
|
2022-12-05 09:58:23 -08:00
|
|
|
// SCSI Target Emulator PiSCSI
|
2022-10-04 17:23:42 +02:00
|
|
|
// for Raspberry Pi
|
|
|
|
//
|
|
|
|
// Copyright (C) 2022 Uwe Seimet
|
|
|
|
//
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#include "mocks.h"
|
2022-11-10 07:44:06 +01:00
|
|
|
#include "shared/protobuf_serializer.h"
|
2022-12-05 09:58:23 -08:00
|
|
|
#include "shared/piscsi_exceptions.h"
|
|
|
|
#include "generated/piscsi_interface.pb.h"
|
2022-10-25 10:29:57 +02:00
|
|
|
#include <filesystem>
|
2022-10-04 17:23:42 +02:00
|
|
|
|
2022-10-25 10:29:57 +02:00
|
|
|
using namespace filesystem;
|
2022-12-05 09:58:23 -08:00
|
|
|
using namespace piscsi_interface;
|
2022-10-04 17:23:42 +02:00
|
|
|
|
|
|
|
TEST(ProtobufSerializerTest, SerializeMessage)
|
|
|
|
{
|
2022-10-08 19:26:04 +02:00
|
|
|
PbResult result;
|
2022-10-04 17:23:42 +02:00
|
|
|
ProtobufSerializer serializer;
|
|
|
|
|
2022-10-23 21:51:39 +02:00
|
|
|
const int fd = open("/dev/null", O_WRONLY);
|
2022-10-08 19:26:04 +02:00
|
|
|
EXPECT_NE(-1, fd);
|
|
|
|
serializer.SerializeMessage(fd, result);
|
|
|
|
EXPECT_THROW(serializer.SerializeMessage(-1, result), io_exception) << "Writing a message must fail";
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(ProtobufSerializerTest, DeserializeMessage)
|
|
|
|
{
|
|
|
|
PbResult result;
|
|
|
|
ProtobufSerializer serializer;
|
|
|
|
vector<byte> buf(1);
|
|
|
|
|
|
|
|
int fd = open("/dev/null", O_RDONLY);
|
|
|
|
EXPECT_NE(-1, fd);
|
|
|
|
EXPECT_THROW(serializer.DeserializeMessage(fd, result), io_exception) << "Reading the message header must fail";
|
|
|
|
close(fd);
|
|
|
|
|
2022-10-25 10:29:57 +02:00
|
|
|
auto [fd1, filename1] = OpenTempFile();
|
2022-10-23 21:51:39 +02:00
|
|
|
// Data size -1
|
|
|
|
buf = { byte{0xff}, byte{0xff}, byte{0xff}, byte{0xff} };
|
2022-10-25 10:29:57 +02:00
|
|
|
EXPECT_EQ(buf.size(), write(fd1, buf.data(), buf.size()));
|
|
|
|
close(fd1);
|
|
|
|
fd1 = open(filename1.c_str(), O_RDONLY);
|
|
|
|
EXPECT_NE(-1, fd1);
|
|
|
|
EXPECT_THROW(serializer.DeserializeMessage(fd1, result), io_exception) << "Invalid header was not rejected";
|
|
|
|
remove(filename1);
|
2022-10-23 21:51:39 +02:00
|
|
|
|
2022-10-25 10:29:57 +02:00
|
|
|
auto [fd2, filename2] = OpenTempFile();
|
2022-10-23 21:51:39 +02:00
|
|
|
// Data size 2
|
|
|
|
buf = { byte{0x02}, byte{0x00}, byte{0x00}, byte{0x00} };
|
2022-10-25 10:29:57 +02:00
|
|
|
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(serializer.DeserializeMessage(fd2, result), io_exception) << "Invalid data were not rejected";
|
|
|
|
remove(filename2);
|
2022-10-23 21:51:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(ProtobufSerializerTest, SerializeDeserializeMessage)
|
|
|
|
{
|
|
|
|
PbResult result;
|
|
|
|
result.set_status(true);
|
|
|
|
ProtobufSerializer serializer;
|
|
|
|
|
2022-10-25 10:29:57 +02:00
|
|
|
auto [fd, filename] = OpenTempFile();
|
2022-10-08 19:26:04 +02:00
|
|
|
EXPECT_NE(-1, fd);
|
2022-10-23 21:51:39 +02:00
|
|
|
serializer.SerializeMessage(fd, result);
|
2022-10-08 19:26:04 +02:00
|
|
|
close(fd);
|
2022-10-23 21:51:39 +02:00
|
|
|
|
|
|
|
result.set_status(false);
|
|
|
|
fd = open(filename.c_str(), O_RDONLY);
|
|
|
|
EXPECT_NE(-1, fd);
|
|
|
|
serializer.DeserializeMessage(fd, result);
|
|
|
|
close(fd);
|
2022-10-25 10:29:57 +02:00
|
|
|
remove(filename);
|
2022-10-23 21:51:39 +02:00
|
|
|
|
|
|
|
EXPECT_TRUE(result.status());
|
2022-10-08 19:26:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(ProtobufSerializerTest, ReadBytes)
|
|
|
|
{
|
|
|
|
ProtobufSerializer serializer;
|
|
|
|
vector<byte> buf(1);
|
|
|
|
|
|
|
|
int fd = open("/dev/null", O_RDONLY);
|
|
|
|
EXPECT_NE(-1, fd);
|
|
|
|
EXPECT_EQ(0, serializer.ReadBytes(fd, buf));
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
fd = open("/dev/zero", O_RDONLY);
|
|
|
|
EXPECT_NE(-1, fd);
|
|
|
|
EXPECT_EQ(1, serializer.ReadBytes(fd, buf));
|
2022-10-06 16:15:19 +02:00
|
|
|
close(fd);
|
2022-10-04 17:23:42 +02:00
|
|
|
}
|