blob: 437b3cf4ea9cb6c83948926df94a01aaacddd784 [file] [log] [blame]
Ratan Gupta594123d2017-05-16 22:48:35 +05301#include "util.hpp"
2
Gunnar Mills57d9c502018-09-14 14:42:34 -05003#include <netinet/in.h>
4
5#include <gtest/gtest.h>
6
Ratan Gupta594123d2017-05-16 22:48:35 +05307namespace phosphor
8{
9namespace network
10{
11
12class TestUtil : public testing::Test
13{
Gunnar Mills57d9c502018-09-14 14:42:34 -050014 public:
15 TestUtil()
16 {
17 // Empty
18 }
Ratan Gupta594123d2017-05-16 22:48:35 +053019};
20
Nagaraju Goruganti067ca2d2018-01-17 01:12:00 -060021TEST_F(TestUtil, IpValidation)
22{
23 std::string ipaddress = "0.0.0.0";
24 EXPECT_EQ(true, isValidIP(AF_INET, ipaddress));
25
26 ipaddress = "9.3.185.83";
27 EXPECT_EQ(true, isValidIP(AF_INET, ipaddress));
28
29 ipaddress = "9.3.185.a";
30 EXPECT_EQ(false, isValidIP(AF_INET, ipaddress));
31
32 ipaddress = "9.3.a.83";
33 EXPECT_EQ(false, isValidIP(AF_INET, ipaddress));
34
35 ipaddress = "x.x.x.x";
36 EXPECT_EQ(false, isValidIP(AF_INET, ipaddress));
37
38 ipaddress = "0:0:0:0:0:0:0:0";
39 EXPECT_EQ(true, isValidIP(AF_INET6, ipaddress));
40
41 ipaddress = "1:0:0:0:0:0:0:8";
42 EXPECT_EQ(true, isValidIP(AF_INET6, ipaddress));
43
44 ipaddress = "1::8";
45 EXPECT_EQ(true, isValidIP(AF_INET6, ipaddress));
46
47 ipaddress = "0:0:0:0:0:FFFF:204.152.189.116";
48 EXPECT_EQ(true, isValidIP(AF_INET6, ipaddress));
49
50 ipaddress = "::ffff:204.152.189.116";
51 EXPECT_EQ(true, isValidIP(AF_INET6, ipaddress));
52
53 ipaddress = "a:0:0:0:0:FFFF:204.152.189.116";
54 EXPECT_EQ(true, isValidIP(AF_INET6, ipaddress));
55
56 ipaddress = "1::8";
57 EXPECT_EQ(true, isValidIP(AF_INET6, ipaddress));
Nagaraju Goruganti067ca2d2018-01-17 01:12:00 -060058}
59
60TEST_F(TestUtil, PrefixValidation)
61{
62 uint8_t prefixLength = 1;
63 EXPECT_EQ(true, isValidPrefix(AF_INET, prefixLength));
64
65 prefixLength = 32;
66 EXPECT_EQ(true, isValidPrefix(AF_INET, prefixLength));
67
68 prefixLength = 0;
69 EXPECT_EQ(false, isValidPrefix(AF_INET, prefixLength));
70
71 prefixLength = 33;
72 EXPECT_EQ(false, isValidPrefix(AF_INET, prefixLength));
73
74 prefixLength = 33;
75 EXPECT_EQ(true, isValidPrefix(AF_INET6, prefixLength));
76
77 prefixLength = 65;
78 EXPECT_EQ(false, isValidPrefix(AF_INET, prefixLength));
Nagaraju Goruganti067ca2d2018-01-17 01:12:00 -060079}
80
Nagaraju Goruganti067ca2d2018-01-17 01:12:00 -060081TEST_F(TestUtil, MacValidation)
82{
83 std::string macaddress = "00:00:00:00:00:00";
84 EXPECT_EQ(false, phosphor::network::mac_address::validate(macaddress));
85
86 macaddress = "F6:C6:E6:6:B0:D3";
87 EXPECT_EQ(false, phosphor::network::mac_address::validate(macaddress));
88
89 macaddress = "F6:C6:E6:06:B0:D3";
90 EXPECT_EQ(true, phosphor::network::mac_address::validate(macaddress));
91
92 macaddress = "hh:HH:HH:hh:HH:yy";
93 EXPECT_EQ(false, phosphor::network::mac_address::validate(macaddress));
94
95 macaddress = "hhh:GGG:iii:jjj:kkk:lll";
96 EXPECT_EQ(false, phosphor::network::mac_address::validate(macaddress));
Nagaraju Goruganti067ca2d2018-01-17 01:12:00 -060097}
98
Ratan Gupta594123d2017-05-16 22:48:35 +053099TEST_F(TestUtil, ConvertV4MasktoPrefix)
100{
101 std::string mask = "255.255.255.0";
102 uint8_t prefix = toCidr(AF_INET, mask);
103 EXPECT_EQ(prefix, 24);
104
105 mask = "255.255.0.0";
106 prefix = toCidr(AF_INET, mask);
107 EXPECT_EQ(prefix, 16);
108
109 mask = "255.0.0.0";
110 prefix = toCidr(AF_INET, mask);
111 EXPECT_EQ(prefix, 8);
112
113 mask = "255.224.0.0";
114 prefix = toCidr(AF_INET, mask);
115 EXPECT_EQ(prefix, 11);
116
Gunnar Mills57d9c502018-09-14 14:42:34 -0500117 // Invalid Mask
Ratan Gupta594123d2017-05-16 22:48:35 +0530118 mask = "255.0.255.0";
119 prefix = toCidr(AF_INET, mask);
120 EXPECT_EQ(prefix, 0);
121}
122
123TEST_F(TestUtil, convertV6MasktoPrefix)
124{
125 std::string mask = "ffff:ffff::";
126 uint8_t prefix = toCidr(AF_INET6, mask);
127 EXPECT_EQ(prefix, 32);
128
129 mask = "ffff:ffff:ffff::";
130 prefix = toCidr(AF_INET6, mask);
131 EXPECT_EQ(prefix, 48);
132
133 mask = "ffff:ffff:fc00::";
134 prefix = toCidr(AF_INET6, mask);
135 EXPECT_EQ(prefix, 38);
136
Gunnar Mills57d9c502018-09-14 14:42:34 -0500137 // Invalid Mask
Ratan Gupta594123d2017-05-16 22:48:35 +0530138 mask = "ffff:0fff::";
139 prefix = toCidr(AF_INET6, mask);
140 EXPECT_EQ(prefix, 0);
141}
142
Ratan Gupta11cef802017-05-29 08:41:48 +0530143TEST_F(TestUtil, isLinkLocaladdress)
Ratan Gupta594123d2017-05-16 22:48:35 +0530144{
145 std::string ipaddress = "fe80:fec0::";
Nagaraju Goruganti66b974d2017-10-03 08:43:08 -0500146 EXPECT_TRUE(isLinkLocalIP(ipaddress));
Ratan Gupta594123d2017-05-16 22:48:35 +0530147
Nagaraju Goruganti66b974d2017-10-03 08:43:08 -0500148 ipaddress = "2000:fe80:789::";
149 EXPECT_FALSE(isLinkLocalIP(ipaddress));
Ratan Gupta594123d2017-05-16 22:48:35 +0530150
151 ipaddress = "2000:fe80::";
Nagaraju Goruganti66b974d2017-10-03 08:43:08 -0500152 EXPECT_FALSE(isLinkLocalIP(ipaddress));
153
154 ipaddress = "169.254.3.3";
155 EXPECT_TRUE(isLinkLocalIP(ipaddress));
156
157 ipaddress = "3.169.254.3";
158 EXPECT_FALSE(isLinkLocalIP(ipaddress));
159
160 ipaddress = "3.3.169.254";
161 EXPECT_FALSE(isLinkLocalIP(ipaddress));
Ratan Gupta594123d2017-05-16 22:48:35 +0530162}
163
164TEST_F(TestUtil, convertPrefixToMask)
165{
166 std::string mask = toMask(AF_INET, 24);
167 EXPECT_EQ(mask, "255.255.255.0");
168
169 mask = toMask(AF_INET, 8);
170 EXPECT_EQ(mask, "255.0.0.0");
171
172 mask = toMask(AF_INET, 27);
173 EXPECT_EQ(mask, "255.255.255.224");
174}
175
Ratan Gupta11cef802017-05-29 08:41:48 +0530176TEST_F(TestUtil, getNetworkAddress)
177{
Gunnar Mills57d9c502018-09-14 14:42:34 -0500178 std::string address = getNetworkID(AF_INET, "9.3.23.251", 24);
179 EXPECT_EQ("9.3.23.0", address);
Ratan Gupta11cef802017-05-29 08:41:48 +0530180
Gunnar Mills57d9c502018-09-14 14:42:34 -0500181 address = getNetworkID(AF_INET, "9.3.23.251", 25);
182 EXPECT_EQ("9.3.23.128", address);
Ratan Gupta11cef802017-05-29 08:41:48 +0530183
Gunnar Mills57d9c502018-09-14 14:42:34 -0500184 address = getNetworkID(AF_INET6, "2001:db8:abcd:dd12::0", 64);
185 EXPECT_EQ("2001:db8:abcd:dd12::", address);
Ratan Gupta11cef802017-05-29 08:41:48 +0530186
Gunnar Mills57d9c502018-09-14 14:42:34 -0500187 address = getNetworkID(AF_INET, "a.b.c.d", 25);
188 EXPECT_EQ("", address);
Ratan Gupta11cef802017-05-29 08:41:48 +0530189
Gunnar Mills57d9c502018-09-14 14:42:34 -0500190 address = getNetworkID(AF_INET6, "2001:db8:gghh:dd12::0", 64);
191 EXPECT_EQ("", address);
Ratan Gupta11cef802017-05-29 08:41:48 +0530192
Gunnar Mills57d9c502018-09-14 14:42:34 -0500193 address = getNetworkID(AF_INET6, "fe80::201:6cff:fe80:228", 64);
194 EXPECT_EQ("fe80::", address);
Ratan Gupta11cef802017-05-29 08:41:48 +0530195}
196
Gunnar Mills57d9c502018-09-14 14:42:34 -0500197} // namespace network
198} // namespace phosphor