RASCSI/cpp/test/piscsi_util_test.cpp
Uwe Seimet 41bdcd4aed
Issues 1179 and 1182 (#1232)
* Update logging

* Remove duplicate code

* Update unit tests

* Clean up includes

* Merge ProtobufSerializer into protobuf_util namespace

* Precompile regex

* Add const

* Add Split() convenience method, update log level/ID parsing

* Move log.h to legacy folder

* Elimininate gotos

* Fixes for gcc 13

* Update compiler flags

* Update default folder handling

* Use references instead of pointers

* Move code for better encapsulation

* Move code

* Remove unused method argument

* Move device logger

* Remove redundant to_string

* Rename for consistency

* Update handling of protobuf pointers

* Simplify protobuf usage

* Memory handling update

* Add hasher
2023-10-15 08:38:15 +02:00

171 lines
3.8 KiB
C++

//---------------------------------------------------------------------------
//
// SCSI Target Emulator PiSCSI
// for Raspberry Pi
//
// Copyright (C) 2022-2023 Uwe Seimet
//
//---------------------------------------------------------------------------
#include <gtest/gtest.h>
#include "shared/piscsi_util.h"
#ifdef __linux__
#include <sched.h>
#endif
using namespace std;
using namespace piscsi_util;
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());
}
TEST(PiscsiUtilTest, ProcessId)
{
int id = -1;
int lun = -1;
string error = ProcessId("", id, lun);
EXPECT_FALSE(error.empty());
EXPECT_EQ(-1, id);
EXPECT_EQ(-1, lun);
error = ProcessId("8", id, lun);
EXPECT_FALSE(error.empty());
EXPECT_EQ(-1, id);
EXPECT_EQ(-1, lun);
error = ProcessId("0:32", id, lun);
EXPECT_FALSE(error.empty());
EXPECT_EQ(-1, id);
EXPECT_EQ(-1, lun);
error = ProcessId("-1:", id, lun);
EXPECT_FALSE(error.empty());
EXPECT_EQ(-1, id);
EXPECT_EQ(-1, lun);
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);
EXPECT_TRUE(error.empty());
EXPECT_EQ(0, id);
EXPECT_EQ(-1, lun);
error = ProcessId("7:31", id, lun);
EXPECT_TRUE(error.empty());
EXPECT_EQ(7, id);
EXPECT_EQ(31, lun);
}
TEST(PiscsiUtilTest, GetAsUnsignedInt)
{
int result;
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));
EXPECT_EQ(0, result);
EXPECT_TRUE(GetAsUnsignedInt("1234", result));
EXPECT_EQ(1234, result);
}
TEST(PiscsiUtilTest, Banner)
{
EXPECT_FALSE(Banner("Test").empty());
}
TEST(PiscsiUtilTest, GetExtensionLowerCase)
{
EXPECT_EQ("", GetExtensionLowerCase(""));
EXPECT_EQ("", GetExtensionLowerCase("."));
EXPECT_EQ("", GetExtensionLowerCase(".ext"));
EXPECT_EQ("", GetExtensionLowerCase(".ext_long"));
EXPECT_EQ("ext", GetExtensionLowerCase("file.ext"));
EXPECT_EQ("ext", GetExtensionLowerCase("FILE.EXT"));
EXPECT_EQ("ext", GetExtensionLowerCase(".XYZ.EXT"));
}
#ifdef __linux__
TEST(PiscsiUtilTest, FixCpu)
{
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