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