1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
#include <gtest/gtest.h>
#include <string>
#include <vector>
#include <tins/network_interface.h>
#include <tins/macros.h>
using namespace Tins;
using namespace std;
class NetworkInterfaceTest : public ::testing::Test {
public:
static const std::string iface_name, iface_addr;
};
#ifdef BSD
const string NetworkInterfaceTest::iface_name("lo0"),
NetworkInterfaceTest::iface_addr("");
#else
const string NetworkInterfaceTest::iface_name("lo"),
NetworkInterfaceTest::iface_addr("");
#endif
#ifndef _WIN32
TEST_F(NetworkInterfaceTest, ConstructorFromString) {
// just test this doesn't throw
NetworkInterface iface(iface_name);
try {
NetworkInterface iface("ishallnotexist");
ASSERT_TRUE(false);
}
catch(...) {
}
}
TEST_F(NetworkInterfaceTest, ConstructorFromIp) {
NetworkInterface iface(IPv4Address("127.0.0.1"));
EXPECT_EQ(iface.name(), iface_name);
NetworkInterface i6face(IPv6Address("::1"));
EXPECT_EQ(i6face.name(), iface_name);
}
TEST_F(NetworkInterfaceTest, Id) {
NetworkInterface iface(iface_name);
EXPECT_TRUE(iface.id() != 0);
}
TEST_F(NetworkInterfaceTest, Info) {
NetworkInterface iface(iface_name);
NetworkInterface::Info info(iface.addresses());
// assuming it's like this
EXPECT_EQ(info.ip_addr, "127.0.0.1");
EXPECT_EQ(info.netmask, "255.0.0.0");
}
TEST_F(NetworkInterfaceTest, EqualsOperator) {
NetworkInterface iface1(iface_name), iface2(iface_name);
EXPECT_EQ(iface1, iface2);
}
TEST_F(NetworkInterfaceTest, DistinctOperator) {
NetworkInterface iface1(iface_name), iface2;
EXPECT_NE(iface1, iface2);
}
#endif // _WIN32
TEST_F(NetworkInterfaceTest, IterateOverInterfaces) {
vector<NetworkInterface> interfaces = NetworkInterface::all();
for (size_t i = 0; i < interfaces.size(); ++i) {
// Expect this interface to be equal to itself
EXPECT_EQ(interfaces[i], interfaces[i]);
// We expect to be able to construct the interface from a name
// and they should still be equal
NetworkInterface iface(interfaces[i].name());
EXPECT_EQ(interfaces[i], iface);
}
}
|