lxcpp: network implementation (part 2)
[platform/core/security/vasum.git] / tests / unit_tests / lxcpp / ut-network.cpp
1 /*
2  *  Copyright (C) 2015 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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.
7  *
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.
12  *
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
16  */
17
18 /**
19  * @file
20  * @author  Krzysztof Dynowski (k.dynowski@samsumg.com)
21  * @brief   Unit tests of lxcpp network helpers
22  */
23
24 #include "config.hpp"
25 #include "config/manager.hpp"
26 #include "lxcpp/network-config.hpp"
27 #include "ut.hpp"
28
29 #include <iostream>
30 #include <sched.h>
31
32 #include <net/if.h>
33
34 namespace {
35
36 using namespace lxcpp;
37 using namespace config;
38
39 struct Fixture {
40     Fixture() {}
41     ~Fixture() {}
42
43     static std::string getUniqueName(const std::string& prefix) {
44         std::vector<std::string> iflist = NetworkInterface::getInterfaces(0);
45         std::string name;
46         unsigned i = 0;
47         do {
48             name = prefix + std::to_string(i++);
49         } while (std::find(iflist.begin(), iflist.end(), name) != iflist.end());
50         return name;
51     }
52 };
53
54 } // namespace
55
56 /*
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
60  */
61
62 BOOST_FIXTURE_TEST_SUITE(LxcppNetworkSuite, Fixture)
63
64 BOOST_AUTO_TEST_CASE(NetworkListInterfaces)
65 {
66     std::vector<std::string> iflist;
67     BOOST_CHECK_NO_THROW(iflist = NetworkInterface::getInterfaces(0));
68
69     for (const auto& i : iflist) {
70         NetworkInterface ni(i);
71         std::cout << i << ": ";
72         Attrs attrs;
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);
79                 std::cout << "(";
80                 std::cout << ((f & IFF_UP) !=0 ? "UP" : "DONW");
81                 std::cout << ")";
82             }
83             std::cout << "; ";
84         }
85         std::cout << std::endl;
86
87         BOOST_CHECK_NO_THROW(addrs = ni.getInetAddressList());
88         for (const InetAddr& a : addrs) {
89             std::cout << "   " << toString(a) << std::endl;
90         }
91     }
92 }
93
94 BOOST_AUTO_TEST_CASE(NetworkConfigSerialization)
95 {
96     std::string tmpConfigFile = "/tmp/netconfig.conf";
97     NetworkConfig cfg;
98     BOOST_CHECK_NO_THROW(config::saveToJsonString(cfg));
99
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);
103
104     InetAddr addr(0, 24, "1.2.3.4");
105     cfg.addInetConfig("zone-eth0", addr);
106
107     config::saveToJsonFile(tmpConfigFile, cfg);
108
109     NetworkConfig cfg2;
110     config::loadFromJsonFile(tmpConfigFile, cfg2);
111
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);
116
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());
121     }
122 }
123 BOOST_AUTO_TEST_CASE(NetworkBridgeCreateDestroy)
124 {
125     std::string name = getUniqueName("lolo");
126     NetworkInterface ni(name);
127     InetAddr myip(0, 32, "10.100.1.1");
128
129     BOOST_CHECK_NO_THROW(ni.create(InterfaceType::BRIDGE, ""));
130     BOOST_CHECK_NO_THROW(ni.addInetAddr(myip);)
131
132     std::vector<std::string> iflist = NetworkInterface::getInterfaces(0);
133     BOOST_CHECK(std::find(iflist.begin(), iflist.end(), name) != iflist.end());
134
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;
139     }
140     std::cout << std::endl;
141     for (const InetAddr& a : addrs) {
142         std::cout << "   " << toString(a) << std::endl;
143     }
144
145     BOOST_CHECK_NO_THROW(ni.delInetAddr(myip));
146     BOOST_CHECK_NO_THROW(ni.destroy());
147 }
148
149 BOOST_AUTO_TEST_CASE(NetworkMacVLanCreateDestroy)
150 {
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) {
157             masterif = ifn;
158             break;
159         }
160     }
161
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));
165
166     iflist = NetworkInterface::getInterfaces(0);
167     BOOST_CHECK(std::find(iflist.begin(), iflist.end(), ni.getName()) != iflist.end());
168
169     BOOST_CHECK_NO_THROW(ni.destroy());
170 }
171
172 BOOST_AUTO_TEST_SUITE_END()