blob: 7179212d62a241e7ed7ecdaa373ead50683501a7 [file] [log] [blame]
Ratan Gupta2aca45c2017-07-26 01:16:27 +05301#include "config_parser.hpp"
Ratan Gupta2aca45c2017-07-26 01:16:27 +05302#include "ipaddress.hpp"
Ratan Gupta35297172018-11-28 18:40:16 +05303#include "mock_network_manager.hpp"
Gunnar Mills57d9c502018-09-14 14:42:34 -05004#include "mock_syscall.hpp"
Gunnar Mills57d9c502018-09-14 14:42:34 -05005#include "vlan_interface.hpp"
Ratan Gupta2aca45c2017-07-26 01:16:27 +05306
Gunnar Mills57d9c502018-09-14 14:42:34 -05007#include <arpa/inet.h>
Ratan Gupta2aca45c2017-07-26 01:16:27 +05308#include <net/if.h>
9#include <netinet/in.h>
Gunnar Mills57d9c502018-09-14 14:42:34 -050010
Ratan Gupta2aca45c2017-07-26 01:16:27 +053011#include <exception>
12#include <experimental/filesystem>
Gunnar Mills57d9c502018-09-14 14:42:34 -050013#include <sdbusplus/bus.hpp>
14
15#include <gtest/gtest.h>
Ratan Gupta2aca45c2017-07-26 01:16:27 +053016
17namespace phosphor
18{
19namespace network
20{
21
22namespace fs = std::experimental::filesystem;
23
24class TestVlanInterface : public testing::Test
25{
Gunnar Mills57d9c502018-09-14 14:42:34 -050026 public:
27 sdbusplus::bus::bus bus;
Ratan Gupta35297172018-11-28 18:40:16 +053028 MockManager manager;
Gunnar Mills57d9c502018-09-14 14:42:34 -050029 EthernetInterface interface;
30 std::string confDir;
31 TestVlanInterface() :
32 bus(sdbusplus::bus::new_default()),
33 manager(bus, "/xyz/openbmc_test/network", "/tmp"),
34 interface(bus, "/xyz/openbmc_test/network/test0", false, manager)
Ratan Gupta2aca45c2017-07-26 01:16:27 +053035
Gunnar Mills57d9c502018-09-14 14:42:34 -050036 {
37 setConfDir();
38 }
Ratan Gupta2aca45c2017-07-26 01:16:27 +053039
Gunnar Mills57d9c502018-09-14 14:42:34 -050040 ~TestVlanInterface()
41 {
42 if (confDir != "")
Ratan Gupta2aca45c2017-07-26 01:16:27 +053043 {
Gunnar Mills57d9c502018-09-14 14:42:34 -050044 fs::remove_all(confDir);
Ratan Gupta2aca45c2017-07-26 01:16:27 +053045 }
Gunnar Mills57d9c502018-09-14 14:42:34 -050046 }
Ratan Gupta2aca45c2017-07-26 01:16:27 +053047
Gunnar Mills57d9c502018-09-14 14:42:34 -050048 void setConfDir()
49 {
50 char tmp[] = "/tmp/VlanInterface.XXXXXX";
51 confDir = mkdtemp(tmp);
52 manager.setConfDir(confDir);
53 }
54
55 void createVlan(VlanId id)
56 {
57 interface.createVLAN(id);
58 }
59
60 void deleteVlan(const std::string& interfaceName)
61 {
62 interface.deleteVLANObject(interfaceName);
63 }
64
65 int countIPObjects()
66 {
67 return interface.getAddresses().size();
68 }
69
70 bool isIPObjectExist(const std::string& ipaddress)
71 {
72 auto address = interface.getAddresses().find(ipaddress);
73 if (address == interface.getAddresses().end())
Ratan Gupta2aca45c2017-07-26 01:16:27 +053074 {
Ratan Gupta2aca45c2017-07-26 01:16:27 +053075 return false;
76 }
Gunnar Mills57d9c502018-09-14 14:42:34 -050077 return true;
78 }
79
80 bool deleteIPObject(const std::string& ipaddress)
81 {
82 auto address = interface.getAddresses().find(ipaddress);
83 if (address == interface.getAddresses().end())
84 {
85 return false;
86 }
87 address->second->delete_();
88 return true;
89 }
90
91 void createIPObject(IP::Protocol addressType, const std::string& ipaddress,
92 uint8_t subnetMask, const std::string& gateway)
93 {
94 interface.iP(addressType, ipaddress, subnetMask, gateway);
95 }
96
97 bool isValueFound(const std::vector<std::string>& values,
98 const std::string& expectedValue)
99 {
100 for (const auto& value : values)
101 {
102 if (expectedValue == value)
103 {
104 return true;
105 }
106 }
107 return false;
108 }
Ratan Gupta2aca45c2017-07-26 01:16:27 +0530109};
110
111TEST_F(TestVlanInterface, createVLAN)
112{
113 createVlan(50);
114 fs::path filePath = confDir;
115 filePath /= "test0.50.netdev";
116
117 config::Parser parser(filePath.string());
Ratan Guptac27170a2017-11-22 15:44:42 +0530118 config::ReturnCode rc = config::ReturnCode::SUCCESS;
119 config::ValueList values;
120
121 std::tie(rc, values) = parser.getValues("NetDev", "Name");
Ratan Gupta2aca45c2017-07-26 01:16:27 +0530122 std::string expectedValue = "test0.50";
123 bool found = isValueFound(values, expectedValue);
124 EXPECT_EQ(found, true);
125
Ratan Guptac27170a2017-11-22 15:44:42 +0530126 std::tie(rc, values) = parser.getValues("NetDev", "Kind");
Ratan Gupta2aca45c2017-07-26 01:16:27 +0530127 expectedValue = "vlan";
128 found = isValueFound(values, expectedValue);
129 EXPECT_EQ(found, true);
130
Ratan Guptac27170a2017-11-22 15:44:42 +0530131 std::tie(rc, values) = parser.getValues("VLAN", "Id");
Ratan Gupta2aca45c2017-07-26 01:16:27 +0530132 expectedValue = "50";
133 found = isValueFound(values, expectedValue);
134 EXPECT_EQ(found, true);
Ratan Gupta2aca45c2017-07-26 01:16:27 +0530135}
136
137TEST_F(TestVlanInterface, deleteVLAN)
138{
139 createVlan(50);
140 deleteVlan("test0.50");
141 bool fileFound = false;
142
143 fs::path filePath = confDir;
144 filePath /= "test0.50.netdev";
145 if (fs::is_regular_file(filePath.string()))
146 {
Gunnar Mills57d9c502018-09-14 14:42:34 -0500147 fileFound = true;
Ratan Gupta2aca45c2017-07-26 01:16:27 +0530148 }
149 EXPECT_EQ(fileFound, false);
150}
151
152TEST_F(TestVlanInterface, createMultipleVLAN)
153{
154 createVlan(50);
155 createVlan(60);
156
157 fs::path filePath = confDir;
158 filePath /= "test0.50.netdev";
159 config::Parser parser(filePath.string());
Ratan Guptac27170a2017-11-22 15:44:42 +0530160 config::ReturnCode rc = config::ReturnCode::SUCCESS;
161 config::ValueList values;
162
163 std::tie(rc, values) = parser.getValues("NetDev", "Name");
Ratan Gupta2aca45c2017-07-26 01:16:27 +0530164 std::string expectedValue = "test0.50";
165 bool found = isValueFound(values, expectedValue);
166 EXPECT_EQ(found, true);
167
Ratan Guptac27170a2017-11-22 15:44:42 +0530168 std::tie(rc, values) = parser.getValues("NetDev", "Kind");
Ratan Gupta2aca45c2017-07-26 01:16:27 +0530169 expectedValue = "vlan";
170 found = isValueFound(values, expectedValue);
171 EXPECT_EQ(found, true);
172
Ratan Guptac27170a2017-11-22 15:44:42 +0530173 std::tie(rc, values) = parser.getValues("VLAN", "Id");
Ratan Gupta2aca45c2017-07-26 01:16:27 +0530174 expectedValue = "50";
175 found = isValueFound(values, expectedValue);
176 EXPECT_EQ(found, true);
177
178 filePath = confDir;
179 filePath /= "test0.60.netdev";
180 parser.setFile(filePath.string());
Ratan Guptac27170a2017-11-22 15:44:42 +0530181 std::tie(rc, values) = parser.getValues("NetDev", "Name");
Ratan Gupta2aca45c2017-07-26 01:16:27 +0530182 expectedValue = "test0.60";
183 found = isValueFound(values, expectedValue);
184 EXPECT_EQ(found, true);
185
Ratan Guptac27170a2017-11-22 15:44:42 +0530186 std::tie(rc, values) = parser.getValues("VLAN", "Id");
Ratan Gupta2aca45c2017-07-26 01:16:27 +0530187 expectedValue = "60";
188 found = isValueFound(values, expectedValue);
189 EXPECT_EQ(found, true);
190
191 deleteVlan("test0.50");
192 deleteVlan("test0.60");
193}
194
Gunnar Mills57d9c502018-09-14 14:42:34 -0500195} // namespace network
196} // namespace phosphor