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"
|
2023-10-15 06:38:15 +00:00
|
|
|
#include "shared/piscsi_util.h"
|
2022-12-05 17:58:23 +00:00
|
|
|
#include "shared/piscsi_exceptions.h"
|
2022-10-23 19:51:39 +00:00
|
|
|
#include "devices/storage_device.h"
|
2022-10-25 08:29:57 +00:00
|
|
|
#include <filesystem>
|
|
|
|
|
|
|
|
using namespace filesystem;
|
2022-10-23 19:51:39 +00:00
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
TEST(StorageDeviceTest, SetGetFilename)
|
2022-10-23 19:51:39 +00:00
|
|
|
{
|
|
|
|
MockStorageDevice device;
|
|
|
|
|
|
|
|
device.SetFilename("filename");
|
|
|
|
EXPECT_EQ("filename", device.GetFilename());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(StorageDeviceTest, ValidateFile)
|
|
|
|
{
|
|
|
|
MockStorageDevice device;
|
|
|
|
|
|
|
|
device.SetBlockCount(0);
|
2022-10-25 08:29:57 +00:00
|
|
|
device.SetFilename("/non_existing_file");
|
|
|
|
EXPECT_THROW(device.ValidateFile(), io_exception);
|
|
|
|
|
|
|
|
device.SetBlockCount(1);
|
|
|
|
EXPECT_THROW(device.ValidateFile(), io_exception);
|
2022-10-23 19:51:39 +00:00
|
|
|
|
2022-10-25 08:29:57 +00:00
|
|
|
const path filename = CreateTempFile(1);
|
|
|
|
device.SetFilename(string(filename));
|
2022-10-23 19:51:39 +00:00
|
|
|
device.SetReadOnly(false);
|
|
|
|
device.SetProtectable(true);
|
2022-10-25 08:29:57 +00:00
|
|
|
device.ValidateFile();
|
|
|
|
EXPECT_FALSE(device.IsReadOnly());
|
|
|
|
EXPECT_TRUE(device.IsProtectable());
|
2022-10-23 19:51:39 +00:00
|
|
|
EXPECT_FALSE(device.IsStopped());
|
|
|
|
EXPECT_FALSE(device.IsRemoved());
|
|
|
|
EXPECT_FALSE(device.IsLocked());
|
|
|
|
|
2022-10-25 08:29:57 +00:00
|
|
|
permissions(filename, perms::owner_read);
|
2022-10-23 19:51:39 +00:00
|
|
|
device.SetReadOnly(false);
|
|
|
|
device.SetProtectable(true);
|
2022-10-25 08:29:57 +00:00
|
|
|
device.ValidateFile();
|
|
|
|
EXPECT_TRUE(device.IsReadOnly());
|
|
|
|
EXPECT_FALSE(device.IsProtectable());
|
|
|
|
EXPECT_FALSE(device.IsProtected());
|
2022-10-23 19:51:39 +00:00
|
|
|
EXPECT_FALSE(device.IsStopped());
|
|
|
|
EXPECT_FALSE(device.IsRemoved());
|
|
|
|
EXPECT_FALSE(device.IsLocked());
|
2022-10-25 08:29:57 +00:00
|
|
|
|
|
|
|
remove(filename);
|
2022-10-23 19:51:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(StorageDeviceTest, MediumChanged)
|
|
|
|
{
|
|
|
|
MockStorageDevice device;
|
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
EXPECT_FALSE(device.IsMediumChanged());
|
|
|
|
|
2022-10-23 19:51:39 +00:00
|
|
|
device.SetMediumChanged(true);
|
|
|
|
EXPECT_TRUE(device.IsMediumChanged());
|
|
|
|
|
|
|
|
device.SetMediumChanged(false);
|
|
|
|
EXPECT_FALSE(device.IsMediumChanged());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(StorageDeviceTest, GetIdsForReservedFile)
|
|
|
|
{
|
|
|
|
const int ID = 1;
|
2023-10-15 06:38:15 +00:00
|
|
|
const int LUN = 0;
|
|
|
|
auto bus = make_shared<MockBus>();
|
|
|
|
ControllerManager controller_manager;
|
|
|
|
MockAbstractController controller(bus, ID);
|
|
|
|
auto device = make_shared<MockSCSIHD_NEC>(LUN);
|
|
|
|
device->SetFilename("filename");
|
2022-10-25 08:29:57 +00:00
|
|
|
StorageDevice::UnreserveAll();
|
2022-10-23 19:51:39 +00:00
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
EXPECT_TRUE(controller_manager.AttachToController(*bus, ID, device));
|
2022-10-23 19:51:39 +00:00
|
|
|
|
2022-10-25 08:29:57 +00:00
|
|
|
const auto [id1, lun1] = StorageDevice::GetIdsForReservedFile("filename");
|
|
|
|
EXPECT_EQ(-1, id1);
|
|
|
|
EXPECT_EQ(-1, lun1);
|
2022-10-23 19:51:39 +00:00
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
device->ReserveFile();
|
2022-10-25 08:29:57 +00:00
|
|
|
const auto [id2, lun2] = StorageDevice::GetIdsForReservedFile("filename");
|
|
|
|
EXPECT_EQ(ID, id2);
|
|
|
|
EXPECT_EQ(LUN, lun2);
|
2022-10-23 19:51:39 +00:00
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
device->UnreserveFile();
|
2022-10-25 08:29:57 +00:00
|
|
|
const auto [id3, lun3] = StorageDevice::GetIdsForReservedFile("filename");
|
|
|
|
EXPECT_EQ(-1, id3);
|
|
|
|
EXPECT_EQ(-1, lun3);
|
2022-10-23 19:51:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(StorageDeviceTest, UnreserveAll)
|
|
|
|
{
|
2023-10-15 06:38:15 +00:00
|
|
|
const int ID = 1;
|
|
|
|
const int LUN = 0;
|
|
|
|
auto bus = make_shared<MockBus>();
|
|
|
|
ControllerManager controller_manager;
|
|
|
|
MockAbstractController controller(bus, ID);
|
|
|
|
auto device = make_shared<MockSCSIHD_NEC>(LUN);
|
|
|
|
device->SetFilename("filename");
|
2022-10-23 19:51:39 +00:00
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
EXPECT_TRUE(controller_manager.AttachToController(*bus, ID, device));
|
|
|
|
|
|
|
|
device->ReserveFile();
|
2022-10-23 19:51:39 +00:00
|
|
|
StorageDevice::UnreserveAll();
|
2022-10-25 08:29:57 +00:00
|
|
|
const auto [id, lun] = StorageDevice::GetIdsForReservedFile("filename");
|
|
|
|
EXPECT_EQ(-1, id);
|
|
|
|
EXPECT_EQ(-1, lun);
|
2022-10-23 19:51:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(StorageDeviceTest, GetSetReservedFiles)
|
|
|
|
{
|
|
|
|
const int ID = 1;
|
2023-10-15 06:38:15 +00:00
|
|
|
const int LUN = 0;
|
|
|
|
auto bus = make_shared<MockBus>();
|
|
|
|
ControllerManager controller_manager;
|
|
|
|
MockAbstractController controller(bus, ID);
|
|
|
|
auto device = make_shared<MockSCSIHD_NEC>(LUN);
|
|
|
|
device->SetFilename("filename");
|
2022-10-23 19:51:39 +00:00
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
EXPECT_TRUE(controller_manager.AttachToController(*bus, ID, device));
|
2022-10-23 19:51:39 +00:00
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
device->ReserveFile();
|
|
|
|
|
|
|
|
const auto& reserved_files = StorageDevice::GetReservedFiles();
|
2022-10-23 19:51:39 +00:00
|
|
|
EXPECT_EQ(1, reserved_files.size());
|
2023-10-15 06:38:15 +00:00
|
|
|
EXPECT_TRUE(reserved_files.contains("filename"));
|
2022-10-23 19:51:39 +00:00
|
|
|
|
|
|
|
StorageDevice::SetReservedFiles(reserved_files);
|
|
|
|
EXPECT_EQ(1, reserved_files.size());
|
2023-10-15 06:38:15 +00:00
|
|
|
EXPECT_TRUE(reserved_files.contains("filename"));
|
2022-10-23 19:51:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(StorageDeviceTest, FileExists)
|
|
|
|
{
|
|
|
|
EXPECT_FALSE(StorageDevice::FileExists("/non_existing_file"));
|
|
|
|
EXPECT_TRUE(StorageDevice::FileExists("/dev/null"));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(StorageDeviceTest, GetFileSize)
|
|
|
|
{
|
|
|
|
MockStorageDevice device;
|
|
|
|
|
2022-10-25 08:29:57 +00:00
|
|
|
const path filename = CreateTempFile(512);
|
|
|
|
device.SetFilename(filename.c_str());
|
2022-10-23 19:51:39 +00:00
|
|
|
const off_t size = device.GetFileSize();
|
2022-10-25 08:29:57 +00:00
|
|
|
remove(filename);
|
2022-10-23 19:51:39 +00:00
|
|
|
EXPECT_EQ(512, size);
|
|
|
|
|
|
|
|
device.SetFilename("/non_existing_file");
|
|
|
|
EXPECT_THROW(device.GetFileSize(), io_exception);
|
|
|
|
}
|