Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/inputs/pcap/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ set(VISOR_STATIC_PLUGINS ${VISOR_STATIC_PLUGINS} Visor::Input::Pcap PARENT_SCOPE

## TEST SUITE
add_executable(unit-tests-input-pcap
tests/test_iface.cpp
tests/test_mock_traffic.cpp
tests/test_parse_pcap.cpp)

Expand Down
8 changes: 6 additions & 2 deletions src/inputs/pcap/PcapInputStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,12 @@ void PcapInputStream::start()
}
config_set("iface", TARGET);
}
interfaceIP4 = TARGET;
interfaceIP6 = TARGET;
// iface is an interface name or an address; parsing a name as an address throws
if (pcpp::IPv4Address::isValidIPv4Address(TARGET)) {
interfaceIP4 = pcpp::IPv4Address(TARGET);
} else if (pcpp::IPv6Address::isValidIPv6Address(TARGET)) {
interfaceIP6 = pcpp::IPv6Address(TARGET);
}
}
std::string ifNameList = _get_interface_list();

Expand Down
39 changes: 39 additions & 0 deletions src/inputs/pcap/tests/test_iface.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include "PcapInputStream.h"
#include <catch2/catch_test_macros.hpp>
#include <catch2/matchers/catch_matchers_string.hpp>

using namespace visor::input::pcap;
using Catch::Matchers::StartsWith;

// iface may be an interface name or an address, and only an address may be parsed as one. The addresses
// are reserved for documentation (RFC 5737, RFC 3849), so the lookup fails before any capture opens.
TEST_CASE("iface accepts a name, an IPv4 address or an IPv6 address", "[pcap][iface]")
{
SECTION("name")
{
PcapInputStream stream{"pcap-test"};
stream.config_set("iface", "pktvisor-test0");
CHECK_THROWS_WITH(stream.start(), StartsWith("Couldn't find interface by provided name: \"pktvisor-test0\""));
}

SECTION("IPv4 address")
{
PcapInputStream stream{"pcap-test"};
stream.config_set("iface", "192.0.2.1");
CHECK_THROWS_WITH(stream.start(), "Couldn't find interface by provided IP: 192.0.2.1");
}

SECTION("IPv6 address")
{
PcapInputStream stream{"pcap-test"};
stream.config_set("iface", "2001:db8::1");
CHECK_THROWS_WITH(stream.start(), "Couldn't find interface by provided IP: 2001:db8::1");
}

SECTION("the zero address is looked up as a name")
{
PcapInputStream stream{"pcap-test"};
stream.config_set("iface", "0.0.0.0");
CHECK_THROWS_WITH(stream.start(), StartsWith("Couldn't find interface by provided name: \"0.0.0.0\""));
}
}
Loading