2022-10-04 15:23:42 +00:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
//
|
2022-12-05 17:58:23 +00:00
|
|
|
// SCSI Target Emulator PiSCSI
|
2022-10-04 15:23:42 +00:00
|
|
|
// for Raspberry Pi
|
|
|
|
//
|
2023-10-15 06:38:15 +00:00
|
|
|
// Copyright (C) 2022-2023 Uwe Seimet
|
2022-10-04 15:23:42 +00:00
|
|
|
//
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2022-10-23 19:51:39 +00:00
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
2022-12-05 17:58:23 +00:00
|
|
|
#include "shared/piscsi_util.h"
|
2022-10-23 19:51:39 +00:00
|
|
|
#ifdef __linux__
|
|
|
|
#include <sched.h>
|
|
|
|
#endif
|
2022-10-04 15:23:42 +00:00
|
|
|
|
2022-10-23 19:51:39 +00:00
|
|
|
using namespace std;
|
2022-12-05 17:58:23 +00:00
|
|
|
using namespace piscsi_util;
|
2022-10-04 15:23:42 +00:00
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
TEST(PiscsiUtilTest, Split)
|
|
|
|
{
|
|
|
|
auto v = Split("this_is_a_test", '_');
|
|
|
|
EXPECT_EQ(4, v.size());
|
|
|
|
EXPECT_EQ("this", v[0]);
|
|
|
|
EXPECT_EQ("is", v[1]);
|
|
|
|
EXPECT_EQ("a", v[2]);
|
|
|
|
EXPECT_EQ("test", v[3]);
|
|
|
|
v = Split("test", ':');
|
|
|
|
EXPECT_EQ(1, v.size());
|
|
|
|
EXPECT_EQ("test", v[0]);
|
|
|
|
v = Split(":test", ':');
|
|
|
|
EXPECT_EQ(2, v.size());
|
|
|
|
EXPECT_EQ("", v[0]);
|
|
|
|
EXPECT_EQ("test", v[1]);
|
|
|
|
v = Split("test:", ':');
|
|
|
|
EXPECT_EQ(1, v.size());
|
|
|
|
EXPECT_EQ("test", v[0]);
|
|
|
|
v = Split(":", ':');
|
|
|
|
EXPECT_EQ(1, v.size());
|
|
|
|
EXPECT_EQ("", v[0]);
|
|
|
|
v = Split("", ':');
|
|
|
|
EXPECT_EQ(0, v.size());
|
|
|
|
|
|
|
|
v = Split("this:is:a:test", ':', 1);
|
|
|
|
EXPECT_EQ(1, v.size());
|
|
|
|
EXPECT_EQ("this:is:a:test", v[0]);
|
|
|
|
v = Split("this:is:a:test", ':', 2);
|
|
|
|
EXPECT_EQ(2, v.size());
|
|
|
|
EXPECT_EQ("this", v[0]);
|
|
|
|
EXPECT_EQ("is:a:test", v[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(PiscsiUtilTest, GetLocale)
|
|
|
|
{
|
|
|
|
EXPECT_LE(2, GetLocale().size());
|
|
|
|
}
|
|
|
|
|
2022-12-05 17:58:23 +00:00
|
|
|
TEST(PiscsiUtilTest, ProcessId)
|
2022-11-11 20:08:48 +00:00
|
|
|
{
|
|
|
|
int id = -1;
|
|
|
|
int lun = -1;
|
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
string error = ProcessId("", id, lun);
|
2022-11-11 20:08:48 +00:00
|
|
|
EXPECT_FALSE(error.empty());
|
|
|
|
EXPECT_EQ(-1, id);
|
|
|
|
EXPECT_EQ(-1, lun);
|
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
error = ProcessId("8", id, lun);
|
2022-11-11 20:08:48 +00:00
|
|
|
EXPECT_FALSE(error.empty());
|
|
|
|
EXPECT_EQ(-1, id);
|
|
|
|
EXPECT_EQ(-1, lun);
|
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
error = ProcessId("0:32", id, lun);
|
2022-11-11 20:08:48 +00:00
|
|
|
EXPECT_FALSE(error.empty());
|
|
|
|
EXPECT_EQ(-1, id);
|
|
|
|
EXPECT_EQ(-1, lun);
|
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
error = ProcessId("-1:", id, lun);
|
2022-11-11 20:08:48 +00:00
|
|
|
EXPECT_FALSE(error.empty());
|
|
|
|
EXPECT_EQ(-1, id);
|
|
|
|
EXPECT_EQ(-1, lun);
|
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
error = ProcessId("0:-1", id, lun);
|
|
|
|
EXPECT_FALSE(error.empty());
|
|
|
|
EXPECT_EQ(-1, id);
|
|
|
|
EXPECT_EQ(-1, lun);
|
|
|
|
|
|
|
|
error = ProcessId("a", id, lun);
|
|
|
|
EXPECT_FALSE(error.empty());
|
|
|
|
EXPECT_EQ(-1, id);
|
|
|
|
EXPECT_EQ(-1, lun);
|
|
|
|
|
|
|
|
error = ProcessId("a:0", id, lun);
|
|
|
|
EXPECT_FALSE(error.empty());
|
|
|
|
EXPECT_EQ(-1, id);
|
|
|
|
EXPECT_EQ(-1, lun);
|
|
|
|
|
|
|
|
error = ProcessId("0:a", id, lun);
|
|
|
|
EXPECT_FALSE(error.empty());
|
|
|
|
EXPECT_EQ(-1, id);
|
|
|
|
EXPECT_EQ(-1, lun);
|
|
|
|
|
|
|
|
error = ProcessId("0", id, lun);
|
2022-11-11 20:08:48 +00:00
|
|
|
EXPECT_TRUE(error.empty());
|
|
|
|
EXPECT_EQ(0, id);
|
2023-10-15 06:38:15 +00:00
|
|
|
EXPECT_EQ(-1, lun);
|
2022-11-11 20:08:48 +00:00
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
error = ProcessId("7:31", id, lun);
|
2022-11-11 20:08:48 +00:00
|
|
|
EXPECT_TRUE(error.empty());
|
|
|
|
EXPECT_EQ(7, id);
|
|
|
|
EXPECT_EQ(31, lun);
|
|
|
|
}
|
|
|
|
|
2022-12-05 17:58:23 +00:00
|
|
|
TEST(PiscsiUtilTest, GetAsUnsignedInt)
|
2022-10-04 15:23:42 +00:00
|
|
|
{
|
|
|
|
int result;
|
|
|
|
|
2022-11-10 06:44:06 +00:00
|
|
|
EXPECT_FALSE(GetAsUnsignedInt("", result));
|
|
|
|
EXPECT_FALSE(GetAsUnsignedInt("xyz", result));
|
|
|
|
EXPECT_FALSE(GetAsUnsignedInt("-1", result));
|
|
|
|
EXPECT_FALSE(GetAsUnsignedInt("1234567898765432112345678987654321", result)) << "Value is out of range";
|
|
|
|
EXPECT_TRUE(GetAsUnsignedInt("0", result));
|
2022-10-04 15:23:42 +00:00
|
|
|
EXPECT_EQ(0, result);
|
2022-11-10 06:44:06 +00:00
|
|
|
EXPECT_TRUE(GetAsUnsignedInt("1234", result));
|
2022-10-04 15:23:42 +00:00
|
|
|
EXPECT_EQ(1234, result);
|
|
|
|
}
|
2022-10-08 17:26:04 +00:00
|
|
|
|
2022-12-05 17:58:23 +00:00
|
|
|
TEST(PiscsiUtilTest, Banner)
|
2022-10-08 17:26:04 +00:00
|
|
|
{
|
|
|
|
EXPECT_FALSE(Banner("Test").empty());
|
|
|
|
}
|
|
|
|
|
2022-12-05 17:58:23 +00:00
|
|
|
TEST(PiscsiUtilTest, GetExtensionLowerCase)
|
2022-10-23 19:51:39 +00:00
|
|
|
{
|
|
|
|
EXPECT_EQ("", GetExtensionLowerCase(""));
|
|
|
|
EXPECT_EQ("", GetExtensionLowerCase("."));
|
2023-10-15 06:38:15 +00:00
|
|
|
EXPECT_EQ("", GetExtensionLowerCase(".ext"));
|
|
|
|
EXPECT_EQ("", GetExtensionLowerCase(".ext_long"));
|
2022-10-23 19:51:39 +00:00
|
|
|
EXPECT_EQ("ext", GetExtensionLowerCase("file.ext"));
|
|
|
|
EXPECT_EQ("ext", GetExtensionLowerCase("FILE.EXT"));
|
|
|
|
EXPECT_EQ("ext", GetExtensionLowerCase(".XYZ.EXT"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef __linux__
|
2022-12-05 17:58:23 +00:00
|
|
|
TEST(PiscsiUtilTest, FixCpu)
|
2022-10-23 19:51:39 +00:00
|
|
|
{
|
|
|
|
cpu_set_t mask;
|
|
|
|
|
|
|
|
FixCpu(0);
|
|
|
|
CPU_ZERO(&mask);
|
|
|
|
const int cpu = sched_getaffinity(0, sizeof(cpu_set_t), &mask);
|
|
|
|
const int cpus = CPU_COUNT(&mask);
|
|
|
|
|
|
|
|
FixCpu(cpus);
|
|
|
|
CPU_ZERO(&mask);
|
|
|
|
EXPECT_EQ(cpu, sched_getaffinity(0, sizeof(cpu_set_t), &mask));
|
|
|
|
|
|
|
|
FixCpu(cpus - 1);
|
|
|
|
CPU_ZERO(&mask);
|
|
|
|
EXPECT_EQ(cpus - 1, sched_getaffinity(0, sizeof(cpu_set_t), &mask));
|
|
|
|
FixCpu(0);
|
|
|
|
|
|
|
|
// Reset affinity
|
|
|
|
CPU_ZERO(&mask);
|
|
|
|
CPU_SET(cpu, &mask);
|
|
|
|
sched_setaffinity(0, sizeof(cpu_set_t), &mask);
|
|
|
|
|
|
|
|
EXPECT_EQ(0, cpu);
|
|
|
|
}
|
|
|
|
#endif
|