blob: bc3ec641cefe2738d7ddada15da465dc89b71f2c [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
Nagaraju Goruganti067ca2d2018-01-17 01:12:00 -060020TEST_F(TestUtil, IpValidation)
21{
22 std::string ipaddress = "0.0.0.0";
23 EXPECT_EQ(true, isValidIP(AF_INET, ipaddress));
24
25 ipaddress = "9.3.185.83";
26 EXPECT_EQ(true, isValidIP(AF_INET, ipaddress));
27
28 ipaddress = "9.3.185.a";
29 EXPECT_EQ(false, isValidIP(AF_INET, ipaddress));
30
31 ipaddress = "9.3.a.83";
32 EXPECT_EQ(false, isValidIP(AF_INET, ipaddress));
33
34 ipaddress = "x.x.x.x";
35 EXPECT_EQ(false, isValidIP(AF_INET, ipaddress));
36
37 ipaddress = "0:0:0:0:0:0:0:0";
38 EXPECT_EQ(true, isValidIP(AF_INET6, ipaddress));
39
40 ipaddress = "1:0:0:0:0:0:0:8";
41 EXPECT_EQ(true, isValidIP(AF_INET6, ipaddress));
42
43 ipaddress = "1::8";
44 EXPECT_EQ(true, isValidIP(AF_INET6, ipaddress));
45
46 ipaddress = "0:0:0:0:0:FFFF:204.152.189.116";
47 EXPECT_EQ(true, isValidIP(AF_INET6, ipaddress));
48
49 ipaddress = "::ffff:204.152.189.116";
50 EXPECT_EQ(true, isValidIP(AF_INET6, ipaddress));
51
52 ipaddress = "a:0:0:0:0:FFFF:204.152.189.116";
53 EXPECT_EQ(true, isValidIP(AF_INET6, ipaddress));
54
55 ipaddress = "1::8";
56 EXPECT_EQ(true, isValidIP(AF_INET6, ipaddress));
57
58
59}
60
61TEST_F(TestUtil, PrefixValidation)
62{
63 uint8_t prefixLength = 1;
64 EXPECT_EQ(true, isValidPrefix(AF_INET, prefixLength));
65
66 prefixLength = 32;
67 EXPECT_EQ(true, isValidPrefix(AF_INET, prefixLength));
68
69 prefixLength = 0;
70 EXPECT_EQ(false, isValidPrefix(AF_INET, prefixLength));
71
72 prefixLength = 33;
73 EXPECT_EQ(false, isValidPrefix(AF_INET, prefixLength));
74
75 prefixLength = 33;
76 EXPECT_EQ(true, isValidPrefix(AF_INET6, prefixLength));
77
78 prefixLength = 65;
79 EXPECT_EQ(false, isValidPrefix(AF_INET, prefixLength));
80
81}
82
83
84TEST_F(TestUtil, MacValidation)
85{
86 std::string macaddress = "00:00:00:00:00:00";
87 EXPECT_EQ(false, phosphor::network::mac_address::validate(macaddress));
88
89 macaddress = "F6:C6:E6:6:B0:D3";
90 EXPECT_EQ(false, phosphor::network::mac_address::validate(macaddress));
91
92 macaddress = "F6:C6:E6:06:B0:D3";
93 EXPECT_EQ(true, phosphor::network::mac_address::validate(macaddress));
94
95 macaddress = "hh:HH:HH:hh:HH:yy";
96 EXPECT_EQ(false, phosphor::network::mac_address::validate(macaddress));
97
98 macaddress = "hhh:GGG:iii:jjj:kkk:lll";
99 EXPECT_EQ(false, phosphor::network::mac_address::validate(macaddress));
100
101}
102
Ratan Gupta594123d2017-05-16 22:48:35 +0530103TEST_F(TestUtil, ConvertV4MasktoPrefix)
104{
105 std::string mask = "255.255.255.0";
106 uint8_t prefix = toCidr(AF_INET, mask);
107 EXPECT_EQ(prefix, 24);
108
109 mask = "255.255.0.0";
110 prefix = toCidr(AF_INET, mask);
111 EXPECT_EQ(prefix, 16);
112
113 mask = "255.0.0.0";
114 prefix = toCidr(AF_INET, mask);
115 EXPECT_EQ(prefix, 8);
116
117 mask = "255.224.0.0";
118 prefix = toCidr(AF_INET, mask);
119 EXPECT_EQ(prefix, 11);
120
121 //Invalid Mask
122 mask = "255.0.255.0";
123 prefix = toCidr(AF_INET, mask);
124 EXPECT_EQ(prefix, 0);
125}
126
127TEST_F(TestUtil, convertV6MasktoPrefix)
128{
129 std::string mask = "ffff:ffff::";
130 uint8_t prefix = toCidr(AF_INET6, mask);
131 EXPECT_EQ(prefix, 32);
132
133 mask = "ffff:ffff:ffff::";
134 prefix = toCidr(AF_INET6, mask);
135 EXPECT_EQ(prefix, 48);
136
137 mask = "ffff:ffff:fc00::";
138 prefix = toCidr(AF_INET6, mask);
139 EXPECT_EQ(prefix, 38);
140
141 //Invalid Mask
142 mask = "ffff:0fff::";
143 prefix = toCidr(AF_INET6, mask);
144 EXPECT_EQ(prefix, 0);
145}
146
Ratan Gupta11cef802017-05-29 08:41:48 +0530147TEST_F(TestUtil, isLinkLocaladdress)
Ratan Gupta594123d2017-05-16 22:48:35 +0530148{
149 std::string ipaddress = "fe80:fec0::";
Nagaraju Goruganti66b974d2017-10-03 08:43:08 -0500150 EXPECT_TRUE(isLinkLocalIP(ipaddress));
Ratan Gupta594123d2017-05-16 22:48:35 +0530151
Nagaraju Goruganti66b974d2017-10-03 08:43:08 -0500152 ipaddress = "2000:fe80:789::";
153 EXPECT_FALSE(isLinkLocalIP(ipaddress));
Ratan Gupta594123d2017-05-16 22:48:35 +0530154
155 ipaddress = "2000:fe80::";
Nagaraju Goruganti66b974d2017-10-03 08:43:08 -0500156 EXPECT_FALSE(isLinkLocalIP(ipaddress));
157
158 ipaddress = "169.254.3.3";
159 EXPECT_TRUE(isLinkLocalIP(ipaddress));
160
161 ipaddress = "3.169.254.3";
162 EXPECT_FALSE(isLinkLocalIP(ipaddress));
163
164 ipaddress = "3.3.169.254";
165 EXPECT_FALSE(isLinkLocalIP(ipaddress));
Ratan Gupta594123d2017-05-16 22:48:35 +0530166}
167
168TEST_F(TestUtil, convertPrefixToMask)
169{
170 std::string mask = toMask(AF_INET, 24);
171 EXPECT_EQ(mask, "255.255.255.0");
172
173 mask = toMask(AF_INET, 8);
174 EXPECT_EQ(mask, "255.0.0.0");
175
176 mask = toMask(AF_INET, 27);
177 EXPECT_EQ(mask, "255.255.255.224");
178}
179
Ratan Gupta11cef802017-05-29 08:41:48 +0530180TEST_F(TestUtil, getNetworkAddress)
181{
182 std::string address = getNetworkID(AF_INET,"9.3.23.251",24);
183 EXPECT_EQ("9.3.23.0",address);
184
185 address = getNetworkID(AF_INET,"9.3.23.251",25);
186 EXPECT_EQ("9.3.23.128",address);
187
188 address = getNetworkID(AF_INET6,"2001:db8:abcd:dd12::0",64);
189 EXPECT_EQ("2001:db8:abcd:dd12::",address);
190
191 address = getNetworkID(AF_INET,"a.b.c.d",25);
192 EXPECT_EQ("",address);
193
194 address = getNetworkID(AF_INET6,"2001:db8:gghh:dd12::0",64);
195 EXPECT_EQ("",address);
196
Ratan Gupta11cef802017-05-29 08:41:48 +0530197 address = getNetworkID(AF_INET6,"fe80::201:6cff:fe80:228",64);
198 EXPECT_EQ("fe80::",address);
199}
200
Ratan Gupta594123d2017-05-16 22:48:35 +0530201}// namespce network
202}// namespace phosphor