blob: c73306b42690660dd076fa1e98f28bd2030e6842 [file] [log] [blame] [edit]
#include <stdplus/print.hpp>
#include <util/mctp_setup.hpp>
#include <unordered_set>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
using ::testing::HasSubstr;
TEST(GetActiveMctpPortsFound, TestMctpSetup)
{
std::string commandLineOutput =
"dev mctpserial0 index 11 address 0x(no-addr) net 4 mtu 68 up\n"
"dev mctpserial1 index 11 address 0x(no-addr) net 7 mtu 68 up\n";
std::optional<std::unordered_set<std::string>> ports =
getActiveMctpPorts(commandLineOutput);
ASSERT_THAT(ports.has_value(), true);
ASSERT_THAT(ports.value().size(), 2);
for (const std::string& port : ports.value())
{
ASSERT_THAT(port, HasSubstr("mctpserial"));
}
}
TEST(GetActiveMctpPortsNotFound, TestMctpSetup)
{
std::string commandLineOutput =
"dev lo index 1 address 0x00:00:00:00:00:00 net 1 mtu 65536 up\n"
"dev mctpi2c6 index 5 address 0x10 net 1 mtu 254 up\n";
std::optional<std::unordered_set<std::string>> ports =
getActiveMctpPorts(commandLineOutput);
ASSERT_THAT(ports.has_value(), false);
}
TEST(ExtractNewNameWithNoPreMctpPort, TestMctpSetup)
{
std::string preSetupOutput =
"dev lo index 1 address 0x00:00:00:00:00:00 net 1 mtu 65536 up\n"
"dev mctpi2c6 index 5 address 0x10 net 1 mtu 254 up\n";
std::string postSetupOutput =
"dev lo index 1 address 0x00:00:00:00:00:00 net 1 mtu 65536 up\n"
"dev mctpi2c6 index 5 address 0x10 net 1 mtu 254 up\n"
"dev mctpserial0 index 11 address 0x(no-addr) net 4 mtu 68 up\n";
std::optional<std::string> newPort =
extractNewName(preSetupOutput, postSetupOutput);
ASSERT_THAT(newPort.has_value(), true);
ASSERT_THAT(newPort.value(), "mctpserial0");
}
TEST(ExtractNewNameWithPreMctpPortPresent, TestMctpSetup)
{
std::string preSetupOutput =
"dev lo index 1 address 0x00:00:00:00:00:00 net 1 mtu 65536 up\n"
"dev mctpi2c6 index 5 address 0x10 net 1 mtu 254 up\n"
"dev mctpserial0 index 11 address 0x(no-addr) net 7 mtu 68 up\n";
// Irrespective of the order- new port should be found
std::string postSetupOutput =
"dev lo index 1 address 0x00:00:00:00:00:00 net 1 mtu 65536 up\n"
"dev mctpi2c6 index 5 address 0x10 net 1 mtu 254 up\n"
"dev mctpserial1 index 11 address 0x(no-addr) net 4 mtu 68 up\n"
"dev mctpserial0 index 11 address 0x(no-addr) net 7 mtu 68 up\n";
std::optional<std::string> newPort =
extractNewName(preSetupOutput, postSetupOutput);
ASSERT_THAT(newPort.has_value(), true);
ASSERT_THAT(newPort.value(), "mctpserial1");
}
TEST(ExtractNewNameNotFound, TestMctpSetup)
{
std::string preSetupOutput =
"dev lo index 1 address 0x00:00:00:00:00:00 net 1 mtu 65536 up\n"
"dev mctpi2c6 index 5 address 0x10 net 1 mtu 254 up\n";
std::optional<std::string> newPort =
extractNewName(preSetupOutput, preSetupOutput);
ASSERT_THAT(newPort.has_value(), false);
}
TEST(ExtractNewNameEmpty, TestMctpSetup)
{
std::optional<std::string> newPort = extractNewName("", "");
ASSERT_THAT(newPort.has_value(), false);
}
TEST(CreateNetIdSuccess, TestMctpSetup)
{
std::string udevId = "1_1_4_1";
int netId = createNetIdFromUdevId(udevId);
// net id should be sum of the digits in udev id
ASSERT_EQ(netId, 7);
}