Files
RASCSI/cpp/test/network_util_test.cpp
T
Daniel Markstedt e2735813a3 parprouted bridge for SCDP adapter
Refactor the DaynaPort TAP driver around explicit bridge and proxy-ARP profiles. Validate the selected interface and profile before creating piscsi0, attach it only to the preconfigured bridge when appropriate, derive a stable DaynaPort MAC address from the uplink, and make TAP setup and cleanup robust.

Add managed host networking profiles for wired bridging and Wi-Fi proxy ARP. Install the PiSCSI NetworkManager templates, profile-management command, parprouted and DHCP relay system services, udev/sysctl hooks, and configuration file so a selected Wi-Fi uplink can provide proxy-ARP networking without altering connectivity during package installation.

Expose DaynaPort profiles consistently through the protocol, daemon, scsictl, and web UI. Report interface type, state, and supported modes; require or default explicit mode/interface pairs on attach; present eligible wired-bridge and Wi-Fi proxy-ARP choices; and validate selections before attaching an SCDP device.

Package and document the new networking workflow. Add the profile utility and required runtime assets to Debian packages, expand the manual-page set and web manual index, update DaynaPort command help, and extend unit and handler tests for the profile-aware behavior.
2026-08-23 12:15:13 +02:00

90 lines
3.0 KiB
C++

//---------------------------------------------------------------------------
//
// SCSI Target Emulator PiSCSI
// for Raspberry Pi
//
// Copyright (C) 2023 Uwe Seimet
//
//---------------------------------------------------------------------------
#include <gtest/gtest.h>
#include <netdb.h>
#include <netinet/in.h>
#include <fstream>
#include "shared/network_util.h"
#include "test_shared.h"
using namespace network_util;
TEST(NetworkUtilTest, IsInterfaceUp)
{
EXPECT_FALSE(IsInterfaceUp("foo_bar"));
}
TEST(NetworkUtilTest, IsValidInterfaceName)
{
EXPECT_TRUE(IsValidInterfaceName("enp0s31f6"));
EXPECT_TRUE(IsValidInterfaceName("wlx001122334455"));
EXPECT_FALSE(IsValidInterfaceName(""));
EXPECT_FALSE(IsValidInterfaceName("wlan0,wlan1"));
EXPECT_FALSE(IsValidInterfaceName("../wlan0"));
}
TEST(NetworkUtilTest, ParseProxyArpUplink)
{
EXPECT_EQ("wlan0", ParseProxyArpUplink("PISCSI_PROXYARP_UPLINK=wlan0\n").value());
EXPECT_EQ("wlx001122334455", ParseProxyArpUplink("# comment\nPISCSI_PROXYARP_UPLINK=wlx001122334455\n").value());
EXPECT_FALSE(ParseProxyArpUplink("PISCSI_PROXYARP_UPLINK=wlan0\nPISCSI_PROXYARP_UPLINK=wlan1\n").has_value());
EXPECT_FALSE(ParseProxyArpUplink("PISCSI_PROXYARP_UPLINK=wlan0,wlan1\n").has_value());
EXPECT_FALSE(ParseProxyArpUplink("PISCSI_PROXYARP_UPLINK=\n").has_value());
EXPECT_FALSE(ParseProxyArpUplink("PISCSI_PROXYARP_PROMISC=true\n").has_value());
}
TEST(NetworkUtilTest, ReadProxyArpUplinkFile)
{
const auto configuration = test_data_temp_path / "proxyarp-network.conf";
const auto directory = test_data_temp_path / "proxyarp-network.conf-directory";
filesystem::remove(configuration);
filesystem::remove_all(directory);
ASSERT_TRUE(filesystem::create_directory(directory));
EXPECT_FALSE(ReadProxyArpUplinkFile(directory).has_value());
{
ofstream output(configuration);
output << "PISCSI_PROXYARP_UPLINK=wlan0\n";
}
EXPECT_EQ("wlan0", ReadProxyArpUplinkFile(configuration).value());
filesystem::remove(configuration);
filesystem::remove_all(directory);
}
TEST(NetworkUtilTest, ClassifyInterface)
{
EXPECT_EQ(NetworkInterfaceType::LOOPBACK, ClassifyInterface(false, false, false, false, "lo"));
EXPECT_EQ(NetworkInterfaceType::BRIDGE, ClassifyInterface(false, true, false, true, "piscsi_bridge"));
EXPECT_EQ(NetworkInterfaceType::WIFI, ClassifyInterface(true, false, false, false, "wlan0"));
EXPECT_EQ(NetworkInterfaceType::TAP, ClassifyInterface(false, false, true, true, "piscsi0"));
EXPECT_EQ(NetworkInterfaceType::VIRTUAL, ClassifyInterface(false, false, false, true, "veth0"));
EXPECT_EQ(NetworkInterfaceType::ETHERNET, ClassifyInterface(false, false, false, false, "enp1s0"));
}
TEST(NetworkUtilTest, GetNetworkInterfaces)
{
#ifdef __linux__
for (const auto& [name, interface] : GetNetworkInterfaceInfo()) {
EXPECT_EQ(name, interface.name);
}
#else
GetNetworkInterfaces();
#endif
}
TEST(NetworkUtilTest, ResolveHostName)
{
sockaddr_in server_addr = {};
EXPECT_FALSE(ResolveHostName("foo.foobar", &server_addr));
EXPECT_TRUE(ResolveHostName("127.0.0.1", &server_addr));
}