2022-10-23 19:51:39 +00:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
//
|
2022-12-05 17:58:23 +00:00
|
|
|
// SCSI Target Emulator PiSCSI
|
2022-10-23 19:51:39 +00:00
|
|
|
// for Raspberry Pi
|
|
|
|
//
|
2023-10-15 06:38:15 +00:00
|
|
|
// Copyright (C) 2022-2023 Uwe Seimet
|
2022-10-23 19:51:39 +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"
|
|
|
|
#include "piscsi/piscsi_image.h"
|
2022-10-23 19:51:39 +00:00
|
|
|
|
2022-12-05 17:58:23 +00:00
|
|
|
using namespace piscsi_interface;
|
2022-10-23 19:51:39 +00:00
|
|
|
using namespace protobuf_util;
|
|
|
|
|
2022-12-05 17:58:23 +00:00
|
|
|
TEST(PiscsiImageTest, SetGetDepth)
|
2022-10-23 19:51:39 +00:00
|
|
|
{
|
2022-12-05 17:58:23 +00:00
|
|
|
PiscsiImage image;
|
2022-10-23 19:51:39 +00:00
|
|
|
|
|
|
|
image.SetDepth(1);
|
|
|
|
EXPECT_EQ(1, image.GetDepth());
|
|
|
|
}
|
|
|
|
|
2022-12-05 17:58:23 +00:00
|
|
|
TEST(PiscsiImageTest, SetGetDefaultFolder)
|
2022-10-23 19:51:39 +00:00
|
|
|
{
|
2022-12-05 17:58:23 +00:00
|
|
|
PiscsiImage image;
|
2022-10-23 19:51:39 +00:00
|
|
|
|
|
|
|
EXPECT_NE(string::npos, image.GetDefaultFolder().find("/images"));
|
|
|
|
|
|
|
|
EXPECT_TRUE(!image.SetDefaultFolder("").empty());
|
|
|
|
EXPECT_TRUE(!image.SetDefaultFolder("/not_in_home").empty());
|
|
|
|
}
|
|
|
|
|
2022-12-05 17:58:23 +00:00
|
|
|
TEST(PiscsiImageTest, CreateImage)
|
2022-10-23 19:51:39 +00:00
|
|
|
{
|
2022-12-05 17:58:23 +00:00
|
|
|
PiscsiImage image;
|
2022-10-23 19:51:39 +00:00
|
|
|
|
2023-03-28 08:21:36 +00:00
|
|
|
StorageDevice::UnreserveAll();
|
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
PbCommand command1;
|
|
|
|
CommandContext context1(command1, "", "");
|
|
|
|
EXPECT_FALSE(image.CreateImage(context1)) << "Filename must be reported as missing";
|
2022-10-23 19:51:39 +00:00
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
PbCommand command2;
|
|
|
|
SetParam(command2, "file", "/a/b/c/filename");
|
|
|
|
CommandContext context2(command2, "", "");
|
|
|
|
EXPECT_FALSE(image.CreateImage(context2)) << "Depth must be reported as invalid";
|
2022-10-23 19:51:39 +00:00
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
PbCommand command3;
|
|
|
|
SetParam(command3, "file", "filename");
|
|
|
|
SetParam(command3, "size", "-1");
|
|
|
|
CommandContext context3(command3, "", "");
|
|
|
|
EXPECT_FALSE(image.CreateImage(context3)) << "Size must be reported as invalid";
|
2022-10-23 19:51:39 +00:00
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
PbCommand command4;
|
|
|
|
SetParam(command4, "size", "1");
|
|
|
|
CommandContext context4(command4, "", "");
|
|
|
|
EXPECT_FALSE(image.CreateImage(context4)) << "Size must be reported as invalid";
|
2022-10-23 19:51:39 +00:00
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
PbCommand command5;
|
|
|
|
SetParam(command5, "size", "513");
|
|
|
|
CommandContext context5(command5, "", "");
|
|
|
|
EXPECT_FALSE(image.CreateImage(context4)) << "Size must be reported as not a multiple of 512";
|
2022-10-23 19:51:39 +00:00
|
|
|
|
|
|
|
// Further tests would modify the filesystem
|
|
|
|
}
|
|
|
|
|
2022-12-05 17:58:23 +00:00
|
|
|
TEST(PiscsiImageTest, DeleteImage)
|
2022-10-23 19:51:39 +00:00
|
|
|
{
|
2022-12-05 17:58:23 +00:00
|
|
|
PiscsiImage image;
|
2022-10-23 19:51:39 +00:00
|
|
|
|
2023-03-28 08:21:36 +00:00
|
|
|
StorageDevice::UnreserveAll();
|
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
PbCommand command1;
|
|
|
|
CommandContext context1(command1, "", "");
|
|
|
|
EXPECT_FALSE(image.DeleteImage(context1)) << "Filename must be reported as missing";
|
2022-10-23 19:51:39 +00:00
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
PbCommand command2;
|
|
|
|
SetParam(command2, "file", "/a/b/c/filename");
|
|
|
|
CommandContext context2(command2, "", "");
|
|
|
|
EXPECT_FALSE(image.DeleteImage(context2)) << "Depth must be reported as invalid";
|
2022-10-23 19:51:39 +00:00
|
|
|
|
2022-10-25 08:29:57 +00:00
|
|
|
MockStorageDevice device;
|
2023-10-15 06:38:15 +00:00
|
|
|
device.SetFilename("filename");
|
|
|
|
device.ReserveFile();
|
|
|
|
PbCommand command3;
|
|
|
|
SetParam(command3, "file", "filename");
|
|
|
|
CommandContext context3(command3, "", "");
|
|
|
|
EXPECT_FALSE(image.DeleteImage(context3)) << "File must be reported as in use";
|
2022-10-25 08:29:57 +00:00
|
|
|
|
2022-10-23 19:51:39 +00:00
|
|
|
// Further testing would modify the filesystem
|
|
|
|
}
|
|
|
|
|
2022-12-05 17:58:23 +00:00
|
|
|
TEST(PiscsiImageTest, RenameImage)
|
2022-10-23 19:51:39 +00:00
|
|
|
{
|
2022-12-05 17:58:23 +00:00
|
|
|
PiscsiImage image;
|
2022-10-23 19:51:39 +00:00
|
|
|
|
2023-03-28 08:21:36 +00:00
|
|
|
StorageDevice::UnreserveAll();
|
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
PbCommand command1;
|
|
|
|
CommandContext context1(command1, "", "");
|
|
|
|
EXPECT_FALSE(image.RenameImage(context1)) << "Source filename must be reported as missing";
|
2022-10-23 19:51:39 +00:00
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
PbCommand command2;
|
|
|
|
SetParam(command2, "from", "/a/b/c/filename_from");
|
|
|
|
CommandContext context2(command2, "", "");
|
|
|
|
EXPECT_FALSE(image.RenameImage(context2)) << "Depth must be reported as invalid";
|
2022-10-23 19:51:39 +00:00
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
PbCommand command3;
|
|
|
|
SetParam(command3, "from", "filename_from");
|
|
|
|
CommandContext context3(command3, "", "");
|
|
|
|
EXPECT_FALSE(image.RenameImage(context3)) << "Source file must be reported as missing";
|
2022-10-25 08:29:57 +00:00
|
|
|
|
2022-10-23 19:51:39 +00:00
|
|
|
// Further testing would modify the filesystem
|
|
|
|
}
|
|
|
|
|
2022-12-05 17:58:23 +00:00
|
|
|
TEST(PiscsiImageTest, CopyImage)
|
2022-10-23 19:51:39 +00:00
|
|
|
{
|
2022-12-05 17:58:23 +00:00
|
|
|
PiscsiImage image;
|
2022-10-23 19:51:39 +00:00
|
|
|
|
2023-03-28 08:21:36 +00:00
|
|
|
StorageDevice::UnreserveAll();
|
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
PbCommand command1;
|
|
|
|
CommandContext context1(command1, "", "");
|
|
|
|
EXPECT_FALSE(image.CopyImage(context1)) << "Source filename must be reported as missing";
|
2022-10-23 19:51:39 +00:00
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
PbCommand command2;
|
|
|
|
SetParam(command2, "from", "/a/b/c/filename_from");
|
|
|
|
CommandContext context2(command2, "", "");
|
|
|
|
EXPECT_FALSE(image.CopyImage(context2)) << "Depth must be reported as invalid";
|
2022-10-23 19:51:39 +00:00
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
PbCommand command3;
|
|
|
|
SetParam(command3, "from", "filename_from");
|
|
|
|
CommandContext context3(command3, "", "");
|
|
|
|
EXPECT_FALSE(image.CopyImage(context3)) << "Source file must be reported as missing";
|
2022-10-25 08:29:57 +00:00
|
|
|
|
2022-10-23 19:51:39 +00:00
|
|
|
// Further testing would modify the filesystem
|
|
|
|
}
|
|
|
|
|
2022-12-05 17:58:23 +00:00
|
|
|
TEST(PiscsiImageTest, SetImagePermissions)
|
2022-10-23 19:51:39 +00:00
|
|
|
{
|
2022-12-05 17:58:23 +00:00
|
|
|
PiscsiImage image;
|
2022-10-23 19:51:39 +00:00
|
|
|
|
2023-03-28 08:21:36 +00:00
|
|
|
StorageDevice::UnreserveAll();
|
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
PbCommand command1;
|
|
|
|
CommandContext context1(command1, "", "");
|
|
|
|
EXPECT_FALSE(image.SetImagePermissions(context1)) << "Filename must be reported as missing";
|
2022-10-23 19:51:39 +00:00
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
PbCommand command2;
|
|
|
|
SetParam(command2, "file", "/a/b/c/filename");
|
|
|
|
CommandContext context2(command2, "", "");
|
|
|
|
EXPECT_FALSE(image.SetImagePermissions(context2)) << "Depth must be reported as invalid";
|
2022-10-23 19:51:39 +00:00
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
PbCommand command3;
|
|
|
|
SetParam(command3, "file", "filename");
|
|
|
|
CommandContext context3(command3, "", "");
|
|
|
|
EXPECT_FALSE(image.CopyImage(context3)) << "Source file must be reported as missing";
|
2022-10-23 19:51:39 +00:00
|
|
|
|
|
|
|
// Further testing would modify the filesystem
|
|
|
|
}
|