2 * Copyright (C) 2015 Samsung Electronics Co., Ltd All Rights Reserved
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License version 2.1 as published by the Free Software Foundation.
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 * @author Krzysztof Dynowski (k.dynowski@samsumg.com)
21 * @brief Unit tests of lxcpp network helpers
25 #include "config/manager.hpp"
26 #include "lxcpp/network-config.hpp"
36 using namespace lxcpp;
37 using namespace config;
43 static std::string getUniqueName(const std::string& prefix) {
44 std::vector<std::string> iflist = NetworkInterface::getInterfaces(0);
48 name = prefix + std::to_string(i++);
49 } while (std::find(iflist.begin(), iflist.end(), name) != iflist.end());
57 * NOTE: network inerface unit tests are not finished yet
58 * tests are developed/added together with network interface code
59 * and container code development
62 BOOST_FIXTURE_TEST_SUITE(LxcppNetworkSuite, Fixture)
64 BOOST_AUTO_TEST_CASE(NetworkListInterfaces)
66 std::vector<std::string> iflist;
67 BOOST_CHECK_NO_THROW(iflist = NetworkInterface::getInterfaces(0));
69 for (const auto& i : iflist) {
70 NetworkInterface ni(i);
71 std::cout << i << ": ";
73 std::vector<InetAddr> addrs;
74 BOOST_CHECK_NO_THROW(attrs = ni.getAttrs());
75 for (const Attr& a : attrs) {
76 std::cout << a.name << "=" << a.value;
77 if (a.name == AttrName::FLAGS) {
78 uint32_t f = stoul(a.value);
80 std::cout << ((f & IFF_UP) !=0 ? "UP" : "DONW");
85 std::cout << std::endl;
87 BOOST_CHECK_NO_THROW(addrs = ni.getInetAddressList());
88 for (const InetAddr& a : addrs) {
89 std::cout << " " << toString(a) << std::endl;
94 BOOST_AUTO_TEST_CASE(NetworkConfigSerialization)
96 std::string tmpConfigFile = "/tmp/netconfig.conf";
98 BOOST_CHECK_NO_THROW(config::saveToJsonString(cfg));
100 cfg.addInterfaceConfig("host-veth0", "zone-eth0", InterfaceType::VETH);
101 cfg.addInterfaceConfig("host-veth1", "zone-eth1", InterfaceType::BRIDGE);
102 cfg.addInterfaceConfig("host-veth2", "zone-eth2", InterfaceType::MACVLAN);
104 InetAddr addr(0, 24, "1.2.3.4");
105 cfg.addInetConfig("zone-eth0", addr);
107 config::saveToJsonFile(tmpConfigFile, cfg);
110 config::loadFromJsonFile(tmpConfigFile, cfg2);
112 int ifnum = cfg.getInterfaces().size();
113 for (int i = 0; i < ifnum; ++i) {
114 const NetworkInterfaceConfig& ni1 = cfg.getInterface(i);
115 const NetworkInterfaceConfig& ni2 = cfg2.getInterface(i);
117 BOOST_CHECK_EQUAL(ni1.getHostIf(), ni2.getHostIf());
118 BOOST_CHECK_EQUAL(ni1.getZoneIf(), ni2.getZoneIf());
119 BOOST_CHECK(ni1.getType() == ni2.getType());
120 BOOST_CHECK(ni1.getMode() == ni2.getMode());
123 BOOST_AUTO_TEST_CASE(NetworkBridgeCreateDestroy)
125 std::string name = getUniqueName("lolo");
126 NetworkInterface ni(name);
127 InetAddr myip(0, 32, "10.100.1.1");
129 BOOST_CHECK_NO_THROW(ni.create(InterfaceType::BRIDGE, ""));
130 BOOST_CHECK_NO_THROW(ni.addInetAddr(myip);)
132 std::vector<std::string> iflist = NetworkInterface::getInterfaces(0);
133 BOOST_CHECK(std::find(iflist.begin(), iflist.end(), name) != iflist.end());
135 std::vector<InetAddr> addrs = ni.getInetAddressList();
136 BOOST_CHECK(std::find(addrs.begin(), addrs.end(), myip) != addrs.end());
137 for (const auto& i : iflist) {
138 std::cout << " " << i;
140 std::cout << std::endl;
141 for (const InetAddr& a : addrs) {
142 std::cout << " " << toString(a) << std::endl;
145 BOOST_CHECK_NO_THROW(ni.delInetAddr(myip));
146 BOOST_CHECK_NO_THROW(ni.destroy());
149 BOOST_AUTO_TEST_CASE(NetworkMacVLanCreateDestroy)
151 std::string masterif;
152 std::vector<std::string> iflist = NetworkInterface::getInterfaces(0);
153 for (const auto& ifn : iflist) {
154 if (ifn == "lo") continue;
155 NetworkInterface n(ifn);
156 if (n.status() == NetStatus::UP) {
162 NetworkInterface ni(getUniqueName("lolo"));
163 std::cout << " creating MACVLAN on " << masterif << std::endl;
164 BOOST_CHECK_NO_THROW(ni.create(InterfaceType::MACVLAN, masterif, MacVLanMode::VEPA));
166 iflist = NetworkInterface::getInterfaces(0);
167 BOOST_CHECK(std::find(iflist.begin(), iflist.end(), ni.getName()) != iflist.end());
169 BOOST_CHECK_NO_THROW(ni.destroy());
172 BOOST_AUTO_TEST_SUITE_END()