1*6e1a52faSEd Tanous #include "ethernet.hpp"
2*6e1a52faSEd Tanous #include "http_response.hpp"
3*6e1a52faSEd Tanous
4*6e1a52faSEd Tanous #include <nlohmann/json.hpp>
5*6e1a52faSEd Tanous
6*6e1a52faSEd Tanous #include <cstddef>
7*6e1a52faSEd Tanous #include <string>
8*6e1a52faSEd Tanous #include <variant>
9*6e1a52faSEd Tanous #include <vector>
10*6e1a52faSEd Tanous
11*6e1a52faSEd Tanous #include <gmock/gmock.h>
12*6e1a52faSEd Tanous #include <gtest/gtest.h>
13*6e1a52faSEd Tanous
14*6e1a52faSEd Tanous namespace redfish
15*6e1a52faSEd Tanous {
16*6e1a52faSEd Tanous namespace
17*6e1a52faSEd Tanous {
18*6e1a52faSEd Tanous
19*6e1a52faSEd Tanous using ::testing::IsEmpty;
20*6e1a52faSEd Tanous
TEST(Ethernet,parseAddressesEmpty)21*6e1a52faSEd Tanous TEST(Ethernet, parseAddressesEmpty)
22*6e1a52faSEd Tanous {
23*6e1a52faSEd Tanous std::vector<std::variant<nlohmann::json::object_t, std::nullptr_t>> addr;
24*6e1a52faSEd Tanous std::vector<IPv4AddressData> existingAddr;
25*6e1a52faSEd Tanous
26*6e1a52faSEd Tanous std::vector<AddressPatch> addrOut;
27*6e1a52faSEd Tanous std::string gatewayOut;
28*6e1a52faSEd Tanous
29*6e1a52faSEd Tanous crow::Response res;
30*6e1a52faSEd Tanous
31*6e1a52faSEd Tanous EXPECT_TRUE(parseAddresses(addr, existingAddr, res, addrOut, gatewayOut));
32*6e1a52faSEd Tanous EXPECT_THAT(addrOut, IsEmpty());
33*6e1a52faSEd Tanous EXPECT_THAT(gatewayOut, IsEmpty());
34*6e1a52faSEd Tanous }
35*6e1a52faSEd Tanous
36*6e1a52faSEd Tanous // Create full entry with all fields
TEST(Ethernet,parseAddressesCreateOne)37*6e1a52faSEd Tanous TEST(Ethernet, parseAddressesCreateOne)
38*6e1a52faSEd Tanous {
39*6e1a52faSEd Tanous std::vector<std::variant<nlohmann::json::object_t, std::nullptr_t>> addr;
40*6e1a52faSEd Tanous nlohmann::json::object_t eth;
41*6e1a52faSEd Tanous eth["Address"] = "1.1.1.2";
42*6e1a52faSEd Tanous eth["Gateway"] = "1.1.1.1";
43*6e1a52faSEd Tanous eth["SubnetMask"] = "255.255.255.0";
44*6e1a52faSEd Tanous addr.emplace_back(eth);
45*6e1a52faSEd Tanous std::vector<IPv4AddressData> existingAddr;
46*6e1a52faSEd Tanous
47*6e1a52faSEd Tanous std::vector<AddressPatch> addrOut;
48*6e1a52faSEd Tanous std::string gatewayOut;
49*6e1a52faSEd Tanous
50*6e1a52faSEd Tanous crow::Response res;
51*6e1a52faSEd Tanous
52*6e1a52faSEd Tanous EXPECT_TRUE(parseAddresses(addr, existingAddr, res, addrOut, gatewayOut));
53*6e1a52faSEd Tanous EXPECT_EQ(addrOut.size(), 1);
54*6e1a52faSEd Tanous EXPECT_EQ(addrOut[0].address, "1.1.1.2");
55*6e1a52faSEd Tanous EXPECT_EQ(addrOut[0].gateway, "1.1.1.1");
56*6e1a52faSEd Tanous EXPECT_EQ(addrOut[0].prefixLength, 24);
57*6e1a52faSEd Tanous EXPECT_EQ(addrOut[0].existingDbusId, "");
58*6e1a52faSEd Tanous EXPECT_EQ(addrOut[0].operation, AddrChange::Update);
59*6e1a52faSEd Tanous EXPECT_EQ(gatewayOut, "1.1.1.1");
60*6e1a52faSEd Tanous }
61*6e1a52faSEd Tanous
62*6e1a52faSEd Tanous // Missing gateway should default to no gateway
TEST(Ethernet,parseAddressesCreateOneNoGateway)63*6e1a52faSEd Tanous TEST(Ethernet, parseAddressesCreateOneNoGateway)
64*6e1a52faSEd Tanous {
65*6e1a52faSEd Tanous std::vector<std::variant<nlohmann::json::object_t, std::nullptr_t>> addr;
66*6e1a52faSEd Tanous nlohmann::json::object_t eth;
67*6e1a52faSEd Tanous eth["Address"] = "1.1.1.2";
68*6e1a52faSEd Tanous eth["SubnetMask"] = "255.255.255.0";
69*6e1a52faSEd Tanous addr.emplace_back(eth);
70*6e1a52faSEd Tanous std::vector<IPv4AddressData> existingAddr;
71*6e1a52faSEd Tanous
72*6e1a52faSEd Tanous std::vector<AddressPatch> addrOut;
73*6e1a52faSEd Tanous std::string gatewayOut;
74*6e1a52faSEd Tanous
75*6e1a52faSEd Tanous crow::Response res;
76*6e1a52faSEd Tanous
77*6e1a52faSEd Tanous EXPECT_TRUE(parseAddresses(addr, existingAddr, res, addrOut, gatewayOut));
78*6e1a52faSEd Tanous EXPECT_EQ(addrOut.size(), 1);
79*6e1a52faSEd Tanous EXPECT_EQ(addrOut[0].address, "1.1.1.2");
80*6e1a52faSEd Tanous EXPECT_EQ(addrOut[0].gateway, "");
81*6e1a52faSEd Tanous EXPECT_EQ(addrOut[0].prefixLength, 24);
82*6e1a52faSEd Tanous EXPECT_EQ(addrOut[0].existingDbusId, "");
83*6e1a52faSEd Tanous EXPECT_EQ(addrOut[0].operation, AddrChange::Update);
84*6e1a52faSEd Tanous EXPECT_EQ(gatewayOut, "");
85*6e1a52faSEd Tanous }
86*6e1a52faSEd Tanous
87*6e1a52faSEd Tanous // Create two entries with conflicting gateways.
TEST(Ethernet,conflictingGatewaysNew)88*6e1a52faSEd Tanous TEST(Ethernet, conflictingGatewaysNew)
89*6e1a52faSEd Tanous {
90*6e1a52faSEd Tanous std::vector<std::variant<nlohmann::json::object_t, std::nullptr_t>> addr;
91*6e1a52faSEd Tanous nlohmann::json::object_t eth;
92*6e1a52faSEd Tanous eth["Address"] = "1.1.1.2";
93*6e1a52faSEd Tanous eth["Gateway"] = "1.1.1.1";
94*6e1a52faSEd Tanous eth["SubnetMask"] = "255.255.255.0";
95*6e1a52faSEd Tanous addr.emplace_back(eth);
96*6e1a52faSEd Tanous eth["Gateway"] = "1.1.1.5";
97*6e1a52faSEd Tanous addr.emplace_back(eth);
98*6e1a52faSEd Tanous std::vector<IPv4AddressData> existingAddr;
99*6e1a52faSEd Tanous
100*6e1a52faSEd Tanous std::vector<AddressPatch> addrOut;
101*6e1a52faSEd Tanous std::string gatewayOut;
102*6e1a52faSEd Tanous
103*6e1a52faSEd Tanous crow::Response res;
104*6e1a52faSEd Tanous
105*6e1a52faSEd Tanous EXPECT_FALSE(parseAddresses(addr, existingAddr, res, addrOut, gatewayOut));
106*6e1a52faSEd Tanous }
107*6e1a52faSEd Tanous
108*6e1a52faSEd Tanous // Create full entry with all fields
TEST(Ethernet,conflictingGatewaysExisting)109*6e1a52faSEd Tanous TEST(Ethernet, conflictingGatewaysExisting)
110*6e1a52faSEd Tanous {
111*6e1a52faSEd Tanous std::vector<std::variant<nlohmann::json::object_t, std::nullptr_t>> addr;
112*6e1a52faSEd Tanous nlohmann::json::object_t eth;
113*6e1a52faSEd Tanous addr.emplace_back(eth);
114*6e1a52faSEd Tanous eth["Address"] = "1.1.1.2";
115*6e1a52faSEd Tanous eth["Gateway"] = "1.1.1.1";
116*6e1a52faSEd Tanous eth["SubnetMask"] = "255.255.255.0";
117*6e1a52faSEd Tanous addr.emplace_back(eth);
118*6e1a52faSEd Tanous std::vector<IPv4AddressData> existingAddr;
119*6e1a52faSEd Tanous IPv4AddressData& existing = existingAddr.emplace_back();
120*6e1a52faSEd Tanous existing.id = "my_ip_id";
121*6e1a52faSEd Tanous existing.origin = "Static";
122*6e1a52faSEd Tanous existing.gateway = "192.168.1.1";
123*6e1a52faSEd Tanous
124*6e1a52faSEd Tanous std::vector<AddressPatch> addrOut;
125*6e1a52faSEd Tanous std::string gatewayOut;
126*6e1a52faSEd Tanous
127*6e1a52faSEd Tanous crow::Response res;
128*6e1a52faSEd Tanous
129*6e1a52faSEd Tanous EXPECT_FALSE(parseAddresses(addr, existingAddr, res, addrOut, gatewayOut));
130*6e1a52faSEd Tanous }
131*6e1a52faSEd Tanous
132*6e1a52faSEd Tanous // Missing address should fail
TEST(Ethernet,parseMissingAddress)133*6e1a52faSEd Tanous TEST(Ethernet, parseMissingAddress)
134*6e1a52faSEd Tanous {
135*6e1a52faSEd Tanous std::vector<std::variant<nlohmann::json::object_t, std::nullptr_t>> addr;
136*6e1a52faSEd Tanous nlohmann::json::object_t eth;
137*6e1a52faSEd Tanous eth["SubnetMask"] = "255.255.255.0";
138*6e1a52faSEd Tanous addr.emplace_back(eth);
139*6e1a52faSEd Tanous std::vector<IPv4AddressData> existingAddr;
140*6e1a52faSEd Tanous
141*6e1a52faSEd Tanous std::vector<AddressPatch> addrOut;
142*6e1a52faSEd Tanous std::string gatewayOut;
143*6e1a52faSEd Tanous
144*6e1a52faSEd Tanous crow::Response res;
145*6e1a52faSEd Tanous
146*6e1a52faSEd Tanous EXPECT_FALSE(parseAddresses(addr, existingAddr, res, addrOut, gatewayOut));
147*6e1a52faSEd Tanous }
148*6e1a52faSEd Tanous
149*6e1a52faSEd Tanous // Missing subnet should fail
TEST(Ethernet,parseAddressesMissingSubnet)150*6e1a52faSEd Tanous TEST(Ethernet, parseAddressesMissingSubnet)
151*6e1a52faSEd Tanous {
152*6e1a52faSEd Tanous std::vector<std::variant<nlohmann::json::object_t, std::nullptr_t>> addr;
153*6e1a52faSEd Tanous nlohmann::json::object_t eth;
154*6e1a52faSEd Tanous eth["Address"] = "1.1.1.2";
155*6e1a52faSEd Tanous addr.emplace_back(eth);
156*6e1a52faSEd Tanous std::vector<IPv4AddressData> existingAddr;
157*6e1a52faSEd Tanous
158*6e1a52faSEd Tanous std::vector<AddressPatch> addrOut;
159*6e1a52faSEd Tanous std::string gatewayOut;
160*6e1a52faSEd Tanous
161*6e1a52faSEd Tanous crow::Response res;
162*6e1a52faSEd Tanous
163*6e1a52faSEd Tanous EXPECT_FALSE(parseAddresses(addr, existingAddr, res, addrOut, gatewayOut));
164*6e1a52faSEd Tanous }
165*6e1a52faSEd Tanous
166*6e1a52faSEd Tanous // With one existing address, and a null, it should request deletion
167*6e1a52faSEd Tanous // and clear gateway
TEST(Ethernet,parseAddressesDeleteExistingOnNull)168*6e1a52faSEd Tanous TEST(Ethernet, parseAddressesDeleteExistingOnNull)
169*6e1a52faSEd Tanous {
170*6e1a52faSEd Tanous std::vector<std::variant<nlohmann::json::object_t, std::nullptr_t>> addr;
171*6e1a52faSEd Tanous addr.emplace_back(nullptr);
172*6e1a52faSEd Tanous std::vector<IPv4AddressData> existingAddr;
173*6e1a52faSEd Tanous IPv4AddressData& existing = existingAddr.emplace_back();
174*6e1a52faSEd Tanous existing.id = "my_ip_id";
175*6e1a52faSEd Tanous existing.origin = "Static";
176*6e1a52faSEd Tanous
177*6e1a52faSEd Tanous std::vector<AddressPatch> addrOut;
178*6e1a52faSEd Tanous std::string gatewayOut;
179*6e1a52faSEd Tanous
180*6e1a52faSEd Tanous crow::Response res;
181*6e1a52faSEd Tanous
182*6e1a52faSEd Tanous EXPECT_TRUE(parseAddresses(addr, existingAddr, res, addrOut, gatewayOut));
183*6e1a52faSEd Tanous EXPECT_EQ(addrOut.size(), 1);
184*6e1a52faSEd Tanous EXPECT_EQ(addrOut[0].existingDbusId, "my_ip_id");
185*6e1a52faSEd Tanous EXPECT_EQ(addrOut[0].operation, AddrChange::Delete);
186*6e1a52faSEd Tanous EXPECT_EQ(gatewayOut, "");
187*6e1a52faSEd Tanous }
188*6e1a52faSEd Tanous
TEST(Ethernet,parseAddressesDeleteExistingOnShortLength)189*6e1a52faSEd Tanous TEST(Ethernet, parseAddressesDeleteExistingOnShortLength)
190*6e1a52faSEd Tanous {
191*6e1a52faSEd Tanous // With one existing address, and an input of len(0) it should request
192*6e1a52faSEd Tanous // deletion and clear gateway
193*6e1a52faSEd Tanous std::vector<std::variant<nlohmann::json::object_t, std::nullptr_t>> addr;
194*6e1a52faSEd Tanous std::vector<IPv4AddressData> existingAddr;
195*6e1a52faSEd Tanous IPv4AddressData& existing = existingAddr.emplace_back();
196*6e1a52faSEd Tanous existing.id = "my_ip_id";
197*6e1a52faSEd Tanous existing.origin = "Static";
198*6e1a52faSEd Tanous
199*6e1a52faSEd Tanous std::vector<AddressPatch> addrOut;
200*6e1a52faSEd Tanous std::string gatewayOut;
201*6e1a52faSEd Tanous
202*6e1a52faSEd Tanous crow::Response res;
203*6e1a52faSEd Tanous
204*6e1a52faSEd Tanous EXPECT_TRUE(parseAddresses(addr, existingAddr, res, addrOut, gatewayOut));
205*6e1a52faSEd Tanous EXPECT_EQ(addrOut.size(), 1);
206*6e1a52faSEd Tanous EXPECT_EQ(addrOut[0].existingDbusId, "my_ip_id");
207*6e1a52faSEd Tanous EXPECT_EQ(addrOut[0].operation, AddrChange::Delete);
208*6e1a52faSEd Tanous EXPECT_EQ(gatewayOut, "");
209*6e1a52faSEd Tanous }
210*6e1a52faSEd Tanous
211*6e1a52faSEd Tanous } // namespace
212*6e1a52faSEd Tanous } // namespace redfish
213