Remove wrapper. Old API is not supported anymore.
[platform/core/security/vasum.git] / tests / unit_tests / lxcpp / ut-cgroups.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@samsung.com)
21  * @brief   Unit tests of lxcpp cgroups managment
22  */
23
24 #include "config.hpp"
25 #include "ut.hpp"
26 #include "logger/logger.hpp"
27
28 #include "lxcpp/cgroups/devices.hpp"
29 #include "lxcpp/exception.hpp"
30 #include "utils/text.hpp"
31
32 namespace {
33
34 struct Fixture {
35     Fixture() {}
36     ~Fixture() {}
37 };
38
39 } // namespace
40
41 BOOST_FIXTURE_TEST_SUITE(LxcppCGroupsSuite, Fixture)
42
43 using namespace lxcpp;
44
45 // assume cgroups are supprted by the system
46 BOOST_AUTO_TEST_CASE(GetAvailable)
47 {
48     std::vector<std::string> subs;
49     BOOST_CHECK_NO_THROW(subs = Subsystem::availableSubsystems());
50     BOOST_CHECK_MESSAGE(subs.size() > 0, "Control groups not supported");
51     for (auto n : subs){
52         Subsystem s(n);
53         LOGD(s.getName() << ": " << (s.isAttached() ? s.getMountPoint() : "[not attached]"));
54     }
55 }
56
57 BOOST_AUTO_TEST_CASE(GetCGroupsByPid)
58 {
59     std::vector<std::string> cg;
60     BOOST_CHECK_NO_THROW(cg=Subsystem::getCGroups(::getpid()));
61     BOOST_CHECK(cg.size() > 0);
62 }
63
64 BOOST_AUTO_TEST_CASE(GetPidsByCGroup)
65 {
66     CGroup cg = CGroup::getCGroup("memory", ::getpid());
67     std::vector<pid_t> pids;
68     BOOST_CHECK_NO_THROW(pids = cg.getPids());
69     BOOST_CHECK(pids.size() > 0);
70 }
71
72 BOOST_AUTO_TEST_CASE(SubsysAttach)
73 {
74     Subsystem sub("freezer");
75     BOOST_CHECK_MESSAGE(sub.isAvailable(), "freezer not found");
76
77     if (!sub.isAvailable()) return ;
78
79     if (sub.isAttached()) {
80         std::string mp = sub.getMountPoint();
81         BOOST_CHECK_NO_THROW(Subsystem::detach(mp));
82         BOOST_CHECK(Subsystem(sub.getName()).isAttached()==false);
83         BOOST_CHECK_NO_THROW(Subsystem::attach(mp, {sub.getName()}));
84         BOOST_CHECK(Subsystem(sub.getName()).isAttached()==true);
85     }
86     else {
87         std::string mp = "/sys/fs/cgroup/" + sub.getName();
88         BOOST_CHECK_NO_THROW(Subsystem::attach(mp, {sub.getName()}));
89         BOOST_CHECK(Subsystem(sub.getName()).isAttached()==true);
90         BOOST_CHECK_NO_THROW(Subsystem::detach(mp));
91         BOOST_CHECK(Subsystem(sub.getName()).isAttached()==false);
92     }
93 }
94
95 BOOST_AUTO_TEST_CASE(ControlGroupParams)
96 {
97     CGroup memg("memory:/ut-params");
98     BOOST_CHECK(memg.exists() == false);
99     BOOST_CHECK_NO_THROW(memg.create());
100     BOOST_CHECK(memg.exists() == true);
101
102     if (!memg.exists()) return ;
103
104     memg.assignPid(::getpid());
105     memg.setValue("limit_in_bytes", "10k");
106     memg.setValue("soft_limit_in_bytes", "10k");
107     BOOST_CHECK_THROW(memg.setValue("non-existing-name", "xxx"), CGroupException);
108
109     LOGD("limit_in_bytes: " << memg.getValue("limit_in_bytes"));
110     LOGD("soft_limit_in_bytes: " << memg.getValue("soft_limit_in_bytes"));
111     LOGD("max_usage_in_bytes: " << memg.getValue("max_usage_in_bytes"));
112
113     CGroup("memory:/").assignPid(::getpid());
114     BOOST_CHECK_NO_THROW(memg.destroy());
115 }
116
117 BOOST_AUTO_TEST_CASE(DevicesParams)
118 {
119     DevicesCGroup devcg("/");
120     std::vector<DevicePermission> list = devcg.list();
121     for (__attribute__((unused)) const auto& i : list) {
122         LOGD(std::string("perm = ") + i.type + " " +
123              std::to_string(i.major) + ":" + std::to_string(i.minor) + " " + i.permission);
124     }
125 }
126
127 BOOST_AUTO_TEST_SUITE_END()