1 #include "util.hpp"
2 
3 #include <arpa/inet.h>
4 #include <netinet/in.h>
5 
6 #include <cstddef>
7 #include <xyz/openbmc_project/Common/error.hpp>
8 
9 #include <gtest/gtest.h>
10 
11 namespace phosphor
12 {
13 namespace network
14 {
15 
16 using InternalFailure =
17     sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
18 class TestUtil : public testing::Test
19 {
20   public:
21     TestUtil()
22     {
23         // Empty
24     }
25 };
26 
27 TEST_F(TestUtil, ToHex)
28 {
29     EXPECT_EQ('E', mac_address::internal::toHex(std::byte(0xfe)));
30     EXPECT_EQ('A', mac_address::internal::toHex(std::byte(10)));
31     EXPECT_EQ('4', mac_address::internal::toHex(std::byte(4)));
32 }
33 
34 TEST_F(TestUtil, MacToString)
35 {
36     MacAddr mac1{
37         std::byte(0x00), std::byte(0xDE), std::byte(0xAD),
38         std::byte(0x00), std::byte(0xBE), std::byte(0xEF),
39     };
40     EXPECT_EQ("00:DE:AD:00:BE:EF", mac_address::toString(mac1));
41     MacAddr mac2{
42         std::byte(0x70), std::byte(0xFF), std::byte(0x84),
43         std::byte(0x09), std::byte(0x35), std::byte(0x09),
44     };
45     EXPECT_EQ("70:FF:84:09:35:09", mac_address::toString(mac2));
46 }
47 
48 TEST_F(TestUtil, IpToString)
49 {
50     struct in_addr ip1;
51     EXPECT_EQ(1, inet_pton(AF_INET, "192.168.10.1", &ip1));
52     EXPECT_EQ("192.168.10.1", toString(InAddrAny(ip1)));
53 
54     struct in6_addr ip2;
55     EXPECT_EQ(1, inet_pton(AF_INET6, "fdd8:b5ad:9d93:94ee::2:1", &ip2));
56     EXPECT_EQ("fdd8:b5ad:9d93:94ee::2:1", toString(InAddrAny(ip2)));
57 
58     InAddrAny ip3;
59     try
60     {
61         struct E
62         {
63             operator struct in6_addr()
64             {
65                 throw 1;
66             }
67         };
68         ip3.emplace<struct in6_addr>(E());
69         EXPECT_TRUE(false);
70     }
71     catch (...)
72     {
73     }
74     EXPECT_THROW(toString(ip3), std::runtime_error);
75 }
76 
77 TEST_F(TestUtil, IpValidation)
78 {
79     std::string ipaddress = "0.0.0.0";
80     EXPECT_EQ(true, isValidIP(AF_INET, ipaddress));
81 
82     ipaddress = "9.3.185.83";
83     EXPECT_EQ(true, isValidIP(AF_INET, ipaddress));
84 
85     ipaddress = "9.3.185.a";
86     EXPECT_EQ(false, isValidIP(AF_INET, ipaddress));
87 
88     ipaddress = "9.3.a.83";
89     EXPECT_EQ(false, isValidIP(AF_INET, ipaddress));
90 
91     ipaddress = "x.x.x.x";
92     EXPECT_EQ(false, isValidIP(AF_INET, ipaddress));
93 
94     ipaddress = "0:0:0:0:0:0:0:0";
95     EXPECT_EQ(true, isValidIP(AF_INET6, ipaddress));
96 
97     ipaddress = "1:0:0:0:0:0:0:8";
98     EXPECT_EQ(true, isValidIP(AF_INET6, ipaddress));
99 
100     ipaddress = "1::8";
101     EXPECT_EQ(true, isValidIP(AF_INET6, ipaddress));
102 
103     ipaddress = "0:0:0:0:0:FFFF:204.152.189.116";
104     EXPECT_EQ(true, isValidIP(AF_INET6, ipaddress));
105 
106     ipaddress = "::ffff:204.152.189.116";
107     EXPECT_EQ(true, isValidIP(AF_INET6, ipaddress));
108 
109     ipaddress = "a:0:0:0:0:FFFF:204.152.189.116";
110     EXPECT_EQ(true, isValidIP(AF_INET6, ipaddress));
111 
112     ipaddress = "1::8";
113     EXPECT_EQ(true, isValidIP(AF_INET6, ipaddress));
114 }
115 
116 TEST_F(TestUtil, PrefixValidation)
117 {
118     uint8_t prefixLength = 1;
119     EXPECT_EQ(true, isValidPrefix(AF_INET, prefixLength));
120 
121     prefixLength = 32;
122     EXPECT_EQ(true, isValidPrefix(AF_INET, prefixLength));
123 
124     prefixLength = 0;
125     EXPECT_EQ(false, isValidPrefix(AF_INET, prefixLength));
126 
127     prefixLength = 33;
128     EXPECT_EQ(false, isValidPrefix(AF_INET, prefixLength));
129 
130     prefixLength = 33;
131     EXPECT_EQ(true, isValidPrefix(AF_INET6, prefixLength));
132 
133     prefixLength = 65;
134     EXPECT_EQ(false, isValidPrefix(AF_INET, prefixLength));
135 }
136 
137 TEST_F(TestUtil, MacValidation)
138 {
139     std::string macaddress = "00:00:00:00:00:00";
140     EXPECT_EQ(false, phosphor::network::mac_address::validate(macaddress));
141 
142     macaddress = "F6:C6:E6:6:B0:D3";
143     EXPECT_EQ(false, phosphor::network::mac_address::validate(macaddress));
144 
145     macaddress = "F6:C6:E6:06:B0:D3";
146     EXPECT_EQ(true, phosphor::network::mac_address::validate(macaddress));
147 
148     macaddress = "hh:HH:HH:hh:HH:yy";
149     EXPECT_EQ(false, phosphor::network::mac_address::validate(macaddress));
150 
151     macaddress = "hhh:GGG:iii:jjj:kkk:lll";
152     EXPECT_EQ(false, phosphor::network::mac_address::validate(macaddress));
153 }
154 
155 TEST_F(TestUtil, ConvertV4MasktoPrefix)
156 {
157     std::string mask = "255.255.255.0";
158     uint8_t prefix = toCidr(AF_INET, mask);
159     EXPECT_EQ(prefix, 24);
160 
161     mask = "255.255.0.0";
162     prefix = toCidr(AF_INET, mask);
163     EXPECT_EQ(prefix, 16);
164 
165     mask = "255.0.0.0";
166     prefix = toCidr(AF_INET, mask);
167     EXPECT_EQ(prefix, 8);
168 
169     mask = "255.224.0.0";
170     prefix = toCidr(AF_INET, mask);
171     EXPECT_EQ(prefix, 11);
172 
173     // Invalid Mask
174     mask = "255.0.255.0";
175     prefix = toCidr(AF_INET, mask);
176     EXPECT_EQ(prefix, 0);
177 }
178 
179 TEST_F(TestUtil, convertV6MasktoPrefix)
180 {
181     std::string mask = "ffff:ffff::";
182     uint8_t prefix = toCidr(AF_INET6, mask);
183     EXPECT_EQ(prefix, 32);
184 
185     mask = "ffff:ffff:ffff::";
186     prefix = toCidr(AF_INET6, mask);
187     EXPECT_EQ(prefix, 48);
188 
189     mask = "ffff:ffff:fc00::";
190     prefix = toCidr(AF_INET6, mask);
191     EXPECT_EQ(prefix, 38);
192 
193     // Invalid Mask
194     mask = "ffff:0fff::";
195     prefix = toCidr(AF_INET6, mask);
196     EXPECT_EQ(prefix, 0);
197 }
198 
199 TEST_F(TestUtil, isLinkLocaladdress)
200 {
201     std::string ipaddress = "fe80:fec0::";
202     EXPECT_TRUE(isLinkLocalIP(ipaddress));
203 
204     ipaddress = "2000:fe80:789::";
205     EXPECT_FALSE(isLinkLocalIP(ipaddress));
206 
207     ipaddress = "2000:fe80::";
208     EXPECT_FALSE(isLinkLocalIP(ipaddress));
209 
210     ipaddress = "169.254.3.3";
211     EXPECT_TRUE(isLinkLocalIP(ipaddress));
212 
213     ipaddress = "3.169.254.3";
214     EXPECT_FALSE(isLinkLocalIP(ipaddress));
215 
216     ipaddress = "3.3.169.254";
217     EXPECT_FALSE(isLinkLocalIP(ipaddress));
218 }
219 
220 TEST_F(TestUtil, convertPrefixToMask)
221 {
222     std::string mask = toMask(AF_INET, 24);
223     EXPECT_EQ(mask, "255.255.255.0");
224 
225     mask = toMask(AF_INET, 8);
226     EXPECT_EQ(mask, "255.0.0.0");
227 
228     mask = toMask(AF_INET, 27);
229     EXPECT_EQ(mask, "255.255.255.224");
230 }
231 
232 TEST_F(TestUtil, getNetworkAddress)
233 {
234     std::string address = getNetworkID(AF_INET, "9.3.23.251", 24);
235     EXPECT_EQ("9.3.23.0", address);
236 
237     address = getNetworkID(AF_INET, "9.3.23.251", 25);
238     EXPECT_EQ("9.3.23.128", address);
239 
240     address = getNetworkID(AF_INET6, "2001:db8:abcd:dd12::0", 64);
241     EXPECT_EQ("2001:db8:abcd:dd12::", address);
242 
243     EXPECT_THROW(getNetworkID(AF_INET, "a.b.c.d", 25), InternalFailure);
244 
245     EXPECT_THROW(getNetworkID(AF_INET6, "2001:db8:gghh:dd12::0", 64),
246                  InternalFailure);
247 
248     address = getNetworkID(AF_INET6, "fe80::201:6cff:fe80:228", 64);
249     EXPECT_EQ("fe80::", address);
250 }
251 
252 } // namespace network
253 } // namespace phosphor
254