blob: 9b1d34a2d6293ca3dceecaeda8a84d49c871b232 [file] [log] [blame]
Ratan Gupta594123d2017-05-16 22:48:35 +05301#include <gtest/gtest.h>
2#include <netinet/in.h>
3#include "util.hpp"
4
5namespace phosphor
6{
7namespace network
8{
9
10class TestUtil : public testing::Test
11{
12 public:
13 TestUtil()
14 {
15 // Empty
16 }
17
18};
19
20TEST_F(TestUtil, ConvertV4MasktoPrefix)
21{
22 std::string mask = "255.255.255.0";
23 uint8_t prefix = toCidr(AF_INET, mask);
24 EXPECT_EQ(prefix, 24);
25
26 mask = "255.255.0.0";
27 prefix = toCidr(AF_INET, mask);
28 EXPECT_EQ(prefix, 16);
29
30 mask = "255.0.0.0";
31 prefix = toCidr(AF_INET, mask);
32 EXPECT_EQ(prefix, 8);
33
34 mask = "255.224.0.0";
35 prefix = toCidr(AF_INET, mask);
36 EXPECT_EQ(prefix, 11);
37
38 //Invalid Mask
39 mask = "255.0.255.0";
40 prefix = toCidr(AF_INET, mask);
41 EXPECT_EQ(prefix, 0);
42}
43
44TEST_F(TestUtil, convertV6MasktoPrefix)
45{
46 std::string mask = "ffff:ffff::";
47 uint8_t prefix = toCidr(AF_INET6, mask);
48 EXPECT_EQ(prefix, 32);
49
50 mask = "ffff:ffff:ffff::";
51 prefix = toCidr(AF_INET6, mask);
52 EXPECT_EQ(prefix, 48);
53
54 mask = "ffff:ffff:fc00::";
55 prefix = toCidr(AF_INET6, mask);
56 EXPECT_EQ(prefix, 38);
57
58 //Invalid Mask
59 mask = "ffff:0fff::";
60 prefix = toCidr(AF_INET6, mask);
61 EXPECT_EQ(prefix, 0);
62}
63
Ratan Gupta11cef802017-05-29 08:41:48 +053064TEST_F(TestUtil, isLinkLocaladdress)
Ratan Gupta594123d2017-05-16 22:48:35 +053065{
66 std::string ipaddress = "fe80:fec0::";
67 EXPECT_TRUE(isLinkLocal(ipaddress));
68
69 ipaddress = "2000:4567:789::";
70 EXPECT_FALSE(isLinkLocal(ipaddress));
71
72 ipaddress = "2000:fe80::";
73 EXPECT_FALSE(isLinkLocal(ipaddress));
74}
75
76TEST_F(TestUtil, convertPrefixToMask)
77{
78 std::string mask = toMask(AF_INET, 24);
79 EXPECT_EQ(mask, "255.255.255.0");
80
81 mask = toMask(AF_INET, 8);
82 EXPECT_EQ(mask, "255.0.0.0");
83
84 mask = toMask(AF_INET, 27);
85 EXPECT_EQ(mask, "255.255.255.224");
86}
87
Ratan Gupta11cef802017-05-29 08:41:48 +053088TEST_F(TestUtil, getNetworkAddress)
89{
90 std::string address = getNetworkID(AF_INET,"9.3.23.251",24);
91 EXPECT_EQ("9.3.23.0",address);
92
93 address = getNetworkID(AF_INET,"9.3.23.251",25);
94 EXPECT_EQ("9.3.23.128",address);
95
96 address = getNetworkID(AF_INET6,"2001:db8:abcd:dd12::0",64);
97 EXPECT_EQ("2001:db8:abcd:dd12::",address);
98
99 address = getNetworkID(AF_INET,"a.b.c.d",25);
100 EXPECT_EQ("",address);
101
102 address = getNetworkID(AF_INET6,"2001:db8:gghh:dd12::0",64);
103 EXPECT_EQ("",address);
104
105
106 address = getNetworkID(AF_INET6,"fe80::201:6cff:fe80:228",64);
107 EXPECT_EQ("fe80::",address);
108}
109
Ratan Gupta594123d2017-05-16 22:48:35 +0530110}// namespce network
111}// namespace phosphor