#include <gtest/gtest.h>
#include <iostream>
#include <algorithm>
#include <string>
using std::cout;
using std::endl;
using std::string;
using std::vector;
TEST(interfaces_test, Find_lo)
{
auto ifs = NetIf::all_interfaces();
auto i = std::find_if(ifs.begin(), ifs.end(), [](const auto& x) -> bool
{
return x.name() == "lo";
});
ASSERT_NE(i, ifs.end());
ASSERT_EQ(i->index(), 1);
ASSERT_EQ(i->hw_addr(), "00:00:00:00:00:00");
cout << *i << endl;
}
TEST(interfaces_test, Find_lo_ipv4)
{
auto ifs = NetIf::all_interfaces();
auto i = std::find_if(ifs.begin(), ifs.end(), [](const auto& x) -> bool
{
return x.name() == "lo";
});
ASSERT_NE(i, ifs.end());
auto sa = std::find_if(i->addrs().begin(), i->addrs().end(), [](const NetIfAddr& ad) -> bool
{
return ad.test_flags(InetAddrFlag::ipv4|InetAddrFlag::loopback);
});
ASSERT_NE(sa, i->addrs().end());
cout << *i << endl;
}
TEST(interfaces_test, find_lo_ipv6)
{
auto ifs = NetIf::all_interfaces();
auto i = std::find_if(ifs.begin(), ifs.end(), [](const auto& x) -> bool
{
return x.name() == "lo";
});
ASSERT_NE(i, ifs.end());
auto sa = std::find_if(i->addrs().begin(), i->addrs().end(), [](const NetIfAddr& ad) -> bool
{
return ad.test_flags(InetAddrFlag::ipv6|InetAddrFlag::loopback);
});
ASSERT_NE(sa, i->addrs().end());
cout << *i << endl;
}
TEST(interfaces_test, find_unicast_ipv4)
{
bool found = false;
for (auto& i : NetIf::all_interfaces())
{
for (auto& j : i.addrs())
{
{
found = true;
cout << j << endl;
}
}
}
EXPECT_TRUE(found);
}
TEST(interfaces_test, find_unicast_ipv6)
{
bool found = false;
for (auto& i : NetIf::all_interfaces())
{
for (auto& j : i.addrs())
{
{
found = true;
cout << j << endl;
}
}
}
EXPECT_TRUE(found);
}
TEST(interfaces_test, iterate_addrs_test_local)
{
auto ifs = NetIf::all_interfaces();
ASSERT_TRUE(ifs.size() > 0);
for (auto& i : ifs)
{
cout << i << endl;
for (auto& j : i.addrs())
{
{
cout << "*found link local address: " << j << endl;
ASSERT_EQ(j.scope_id(), i.index());
}
}
}
}
TEST(host_addrs, local_addrs)
{
vector<string> hosts({
"localhost",
"127.0.0.1",
"::1",
"::"
});
for (auto& h : hosts)
{
auto v = NetIf::host_addrs(h);
cout << "host " << h << " for all returned " << v.size() << " entries" << endl;
for (auto& i : v)
{
cout << i << endl;
}
ASSERT_GT(v.size(), 0);
v = NetIf::host_addrs(h, NetIf::SocketType::tcp_stream);
cout << "host " << h << " for tcp returned " << v.size() << " entries" << endl;
for (auto& i : v)
{
cout << i << endl;
}
ASSERT_GT(v.size(), 0);
v = NetIf::host_addrs(h, NetIf::SocketType::udp_datagram);
cout << "host " << h << " for udp returned " << v.size() << " entries" << endl;
for (auto& i : v)
{
cout << i << endl;
}
ASSERT_GT(v.size(), 0);
}
auto ifs = NetIf::all_interfaces();
for (auto& i : ifs)
{
{
cout << i << endl;
for (auto& a : i.addrs())
{
auto v = NetIf::host_addrs(a.host());
ASSERT_GT(v.size(), 0);
}
}
}
}
Named address within an interface.
NetIfFlag
Interface flags.
@ if_running
Resources allocated.
@ if_up
Interface is running.
InetAddrFlag
Internet address flags.
@ unicast
Unicast address.
@ link_local
Traffic is restricted to the local link.
Internet network interface utility.
Low-level tcp and udp sockets.
TEST(inet_example, client_server_stream_test)
[Inet client server]