1 // SPDX-License-Identifier: Apache-2.0
2 // SPDX-FileCopyrightText: Copyright OpenBMC Authors
3 #include "utils/ip_utils.hpp"
4
5 #include <boost/asio/ip/address.hpp>
6
7 #include <cstdint>
8
9 #include <gtest/gtest.h>
10
11 namespace redfish::ip_util
12 {
13 namespace
14 {
15
16 using ::boost::asio::ip::make_address;
17
TEST(IpToString,ReturnsCorrectIpStringForIpv4Addresses)18 TEST(IpToString, ReturnsCorrectIpStringForIpv4Addresses)
19 {
20 EXPECT_EQ(toString(make_address("127.0.0.1")), "127.0.0.1");
21 EXPECT_EQ(toString(make_address("192.168.1.1")), "192.168.1.1");
22 EXPECT_EQ(toString(make_address("::1")), "::1");
23 }
24
TEST(IpToString,ReturnsCorrectIpStringForIpv6Addresses)25 TEST(IpToString, ReturnsCorrectIpStringForIpv6Addresses)
26 {
27 EXPECT_EQ(toString(make_address("fd03:f9ab:25de:89ec::0001")),
28 "fd03:f9ab:25de:89ec::1");
29 EXPECT_EQ(toString(make_address("fd03:f9ab:25de:89ec::1234:abcd")),
30 "fd03:f9ab:25de:89ec::1234:abcd");
31 EXPECT_EQ(toString(make_address("fd03:f9ab:25de:89ec:1234:5678:90ab:cdef")),
32 "fd03:f9ab:25de:89ec:1234:5678:90ab:cdef");
33 }
34
TEST(IpToString,ReturnsCorrectIpStringForIpv4MappedIpv6Addresses)35 TEST(IpToString, ReturnsCorrectIpStringForIpv4MappedIpv6Addresses)
36 {
37 EXPECT_EQ(toString(make_address("::ffff:127.0.0.1")), "127.0.0.1");
38 }
39
TEST(ipv4VerifyIpAndGetBitcount,PositiveTests)40 TEST(ipv4VerifyIpAndGetBitcount, PositiveTests)
41 {
42 EXPECT_TRUE(ipv4VerifyIpAndGetBitcount("192.168.1.1", nullptr));
43
44 uint8_t bits = 0;
45 EXPECT_TRUE(ipv4VerifyIpAndGetBitcount("128.0.0.0", &bits));
46 EXPECT_EQ(bits, 1);
47 EXPECT_TRUE(ipv4VerifyIpAndGetBitcount("192.0.0.0", &bits));
48 EXPECT_EQ(bits, 2);
49 EXPECT_TRUE(ipv4VerifyIpAndGetBitcount("224.0.0.0", &bits));
50 EXPECT_EQ(bits, 3);
51 EXPECT_TRUE(ipv4VerifyIpAndGetBitcount("240.0.0.0", &bits));
52 EXPECT_EQ(bits, 4);
53 EXPECT_TRUE(ipv4VerifyIpAndGetBitcount("248.0.0.0", &bits));
54 EXPECT_EQ(bits, 5);
55 EXPECT_TRUE(ipv4VerifyIpAndGetBitcount("252.0.0.0", &bits));
56 EXPECT_EQ(bits, 6);
57 EXPECT_TRUE(ipv4VerifyIpAndGetBitcount("254.0.0.0", &bits));
58 EXPECT_EQ(bits, 7);
59 EXPECT_TRUE(ipv4VerifyIpAndGetBitcount("255.0.0.0", &bits));
60 EXPECT_EQ(bits, 8);
61 EXPECT_TRUE(ipv4VerifyIpAndGetBitcount("255.128.0.0", &bits));
62 EXPECT_EQ(bits, 9);
63 EXPECT_TRUE(ipv4VerifyIpAndGetBitcount("255.192.0.0", &bits));
64 EXPECT_EQ(bits, 10);
65 EXPECT_TRUE(ipv4VerifyIpAndGetBitcount("255.224.0.0", &bits));
66 EXPECT_EQ(bits, 11);
67 EXPECT_TRUE(ipv4VerifyIpAndGetBitcount("255.240.0.0", &bits));
68 EXPECT_EQ(bits, 12);
69 EXPECT_TRUE(ipv4VerifyIpAndGetBitcount("255.248.0.0", &bits));
70 EXPECT_EQ(bits, 13);
71 EXPECT_TRUE(ipv4VerifyIpAndGetBitcount("255.252.0.0", &bits));
72 EXPECT_EQ(bits, 14);
73 EXPECT_TRUE(ipv4VerifyIpAndGetBitcount("255.254.0.0", &bits));
74 EXPECT_EQ(bits, 15);
75 EXPECT_TRUE(ipv4VerifyIpAndGetBitcount("255.255.0.0", &bits));
76 EXPECT_EQ(bits, 16);
77 EXPECT_TRUE(ipv4VerifyIpAndGetBitcount("255.255.128.0", &bits));
78 EXPECT_EQ(bits, 17);
79 EXPECT_TRUE(ipv4VerifyIpAndGetBitcount("255.255.192.0", &bits));
80 EXPECT_EQ(bits, 18);
81 EXPECT_TRUE(ipv4VerifyIpAndGetBitcount("255.255.224.0", &bits));
82 EXPECT_EQ(bits, 19);
83 EXPECT_TRUE(ipv4VerifyIpAndGetBitcount("255.255.240.0", &bits));
84 EXPECT_EQ(bits, 20);
85 EXPECT_TRUE(ipv4VerifyIpAndGetBitcount("255.255.248.0", &bits));
86 EXPECT_EQ(bits, 21);
87 EXPECT_TRUE(ipv4VerifyIpAndGetBitcount("255.255.252.0", &bits));
88 EXPECT_EQ(bits, 22);
89 EXPECT_TRUE(ipv4VerifyIpAndGetBitcount("255.255.254.0", &bits));
90 EXPECT_EQ(bits, 23);
91 EXPECT_TRUE(ipv4VerifyIpAndGetBitcount("255.255.255.0", &bits));
92 EXPECT_EQ(bits, 24);
93 EXPECT_TRUE(ipv4VerifyIpAndGetBitcount("255.255.255.128", &bits));
94 EXPECT_EQ(bits, 25);
95 EXPECT_TRUE(ipv4VerifyIpAndGetBitcount("255.255.255.192", &bits));
96 EXPECT_EQ(bits, 26);
97 EXPECT_TRUE(ipv4VerifyIpAndGetBitcount("255.255.255.224", &bits));
98 EXPECT_EQ(bits, 27);
99 EXPECT_TRUE(ipv4VerifyIpAndGetBitcount("255.255.255.240", &bits));
100 EXPECT_EQ(bits, 28);
101 EXPECT_TRUE(ipv4VerifyIpAndGetBitcount("255.255.255.248", &bits));
102 EXPECT_EQ(bits, 29);
103 EXPECT_TRUE(ipv4VerifyIpAndGetBitcount("255.255.255.252", &bits));
104 EXPECT_EQ(bits, 30);
105 }
106
TEST(ipv4VerifyIpAndGetBitcount,NegativeTests)107 TEST(ipv4VerifyIpAndGetBitcount, NegativeTests)
108 {
109 uint8_t bits = 0;
110 // > 256 in any field
111 EXPECT_FALSE(ipv4VerifyIpAndGetBitcount("256.0.0.0", &bits));
112 EXPECT_FALSE(ipv4VerifyIpAndGetBitcount("1.256.0.0", &bits));
113 EXPECT_FALSE(ipv4VerifyIpAndGetBitcount("1.1.256.0", &bits));
114 EXPECT_FALSE(ipv4VerifyIpAndGetBitcount("1.1.1.256", &bits));
115
116 // Non contiguous mask
117 EXPECT_FALSE(ipv4VerifyIpAndGetBitcount("255.0.255.0", &bits));
118
119 // Too many fields
120 EXPECT_FALSE(ipv4VerifyIpAndGetBitcount("1.1.1.1.1", &bits));
121 // Not enough fields
122 EXPECT_FALSE(ipv4VerifyIpAndGetBitcount("1.1.1", &bits));
123
124 // Empty string
125 EXPECT_FALSE(ipv4VerifyIpAndGetBitcount("", &bits));
126 }
127 } // namespace
128 } // namespace redfish::ip_util
129