lxcpp: provisioning implementation (part 1)
[platform/core/security/vasum.git] / tests / unit_tests / lxcpp / ut-provisioning.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  Maciej Karpiuk (m.karpiuk2@samsung.com)
21  * @brief   Unit tests of lxcpp provisioning
22  */
23
24 #include "config.hpp"
25 #include "config/manager.hpp"
26 #include "lxcpp/lxcpp.hpp"
27 #include "lxcpp/container.hpp"
28 #include "lxcpp/container-config.hpp"
29 #include "lxcpp/provision-config.hpp"
30 #include "ut.hpp"
31 #include "utils/scoped-dir.hpp"
32 #include <iostream>
33
34 namespace {
35
36 using namespace lxcpp;
37 using namespace config;
38 using namespace provision;
39
40 const std::string TEST_DIR            = "/tmp/ut-provisioning";
41 const std::string ROOT_DIR            = TEST_DIR + "/root";
42
43 struct Fixture {
44     Fixture() : mTestPath(ROOT_DIR) {
45         container = std::unique_ptr<Container>(createContainer("ProvisioningTester", ROOT_DIR));
46     }
47     ~Fixture() {}
48
49     std::unique_ptr<Container> container;
50     utils::ScopedDir mTestPath;
51 };
52
53 } // namespace
54
55
56 BOOST_FIXTURE_TEST_SUITE(LxcppProvisioningSuite, Fixture)
57
58 BOOST_AUTO_TEST_CASE(ListProvisionsEmptyContainer)
59 {
60     BOOST_REQUIRE(container->getFiles().size() == 0);
61     BOOST_REQUIRE(container->getMounts().size() == 0);
62     BOOST_REQUIRE(container->getLinks().size() == 0);
63 }
64
65 BOOST_AUTO_TEST_CASE(AddDeclareFile)
66 {
67     container->declareFile(File::Type::FIFO, "path", 0747, 0777);
68     container->declareFile(File::Type::REGULAR, "path", 0747, 0777);
69
70     std::vector<File> fileList = container->getFiles();
71     BOOST_REQUIRE_EQUAL(fileList.size(), 2);
72
73     BOOST_REQUIRE(fileList[0].type == File::Type::FIFO);
74     BOOST_REQUIRE(fileList[0].path == "path");
75     BOOST_REQUIRE(fileList[0].flags == 0747);
76     BOOST_REQUIRE(fileList[0].mode == 0777);
77     BOOST_REQUIRE(fileList[1].type == File::Type::REGULAR);
78
79     BOOST_REQUIRE_NO_THROW(container->removeFile(fileList[0]));
80     BOOST_REQUIRE_EQUAL(container->getFiles().size(), 1);
81     File dummyFile({File::Type::FIFO, "dummy", 1, 2});
82     BOOST_REQUIRE_THROW(container->removeFile(dummyFile), ProvisionException);
83     BOOST_REQUIRE_NO_THROW(container->removeFile(fileList[1]));
84     BOOST_REQUIRE_EQUAL(container->getFiles().size(), 0);
85 }
86
87 BOOST_AUTO_TEST_CASE(AddDeclareMount)
88 {
89     container->declareMount("/fake/path1", "/fake/path2", "tmpfs", 077, "fake");
90     container->declareMount("/fake/path2", "/fake/path2", "tmpfs", 077, "fake");
91     BOOST_CHECK_THROW(container->declareMount("/fake/path2", "/fake/path2", "tmpfs", 077, "fake"),
92                       ProvisionException);
93
94     std::vector<Mount> mountList = container->getMounts();
95     BOOST_REQUIRE_EQUAL(mountList.size(), 2);
96
97     BOOST_REQUIRE(mountList[0].source == "/fake/path1");
98     BOOST_REQUIRE(mountList[0].target == "/fake/path2");
99     BOOST_REQUIRE(mountList[0].type == "tmpfs");
100     BOOST_REQUIRE(mountList[0].flags == 077);
101     BOOST_REQUIRE(mountList[0].data == "fake");
102
103     BOOST_REQUIRE(mountList[1].source == "/fake/path2");
104     BOOST_REQUIRE(mountList[1].target == "/fake/path2");
105     BOOST_REQUIRE(mountList[1].type == "tmpfs");
106     BOOST_REQUIRE(mountList[1].flags == 077);
107     BOOST_REQUIRE(mountList[1].data == "fake");
108
109     BOOST_REQUIRE_NO_THROW(container->removeMount(mountList[0]));
110     BOOST_REQUIRE_EQUAL(container->getMounts().size(), 1);
111     Mount dummyMount({"a", "b", "c", 1, "d"});
112     BOOST_REQUIRE_THROW(container->removeMount(dummyMount), ProvisionException);
113     BOOST_REQUIRE_NO_THROW(container->removeMount(mountList[1]));
114     BOOST_REQUIRE_EQUAL(container->getMounts().size(), 0);
115 }
116
117 BOOST_AUTO_TEST_CASE(AddDeclareLink)
118 {
119     container->declareLink("/fake/path1", "/fake/path2");
120     container->declareLink("/fake/path2", "/fake/path2");
121     BOOST_CHECK_THROW(container->declareLink("/fake/path2", "/fake/path2"),
122                       ProvisionException);
123
124     std::vector<Link> linkList = container->getLinks();
125     BOOST_REQUIRE_EQUAL(linkList.size(), 2);
126
127     BOOST_REQUIRE(linkList[0].source == "/fake/path1");
128     BOOST_REQUIRE(linkList[0].target == "/fake/path2");
129     BOOST_REQUIRE(linkList[1].source == "/fake/path2");
130     BOOST_REQUIRE(linkList[1].target == "/fake/path2");
131
132     BOOST_REQUIRE_NO_THROW(container->removeLink(linkList[0]));
133     BOOST_REQUIRE_EQUAL(container->getLinks().size(), 1);
134     Link dummyLink({"a", "b"});
135     BOOST_REQUIRE_THROW(container->removeLink(dummyLink), ProvisionException);
136     BOOST_REQUIRE_NO_THROW(container->removeLink(linkList[1]));
137     BOOST_REQUIRE_EQUAL(container->getLinks().size(), 0);
138 }
139
140 BOOST_AUTO_TEST_CASE(ProvisioningConfigSerialization)
141 {
142     std::string tmpConfigFile = "/tmp/fileconfig.conf";
143     std::string tmpConfigMount = "/tmp/mountconfig.conf";
144     std::string tmpConfigLink = "/tmp/linkconfig.conf";
145
146     File  saved_file ({File::Type::REGULAR, "path", 0747, 0777});
147     Mount saved_mount({"/fake/path1", "/fake/path2", "tmpfs", 077, "fake"});
148     Link  saved_link ({"/fake/path1", "/fake/path2"});
149     config::saveToJsonFile(tmpConfigFile, saved_file);
150     config::saveToJsonFile(tmpConfigMount, saved_mount);
151     config::saveToJsonFile(tmpConfigLink, saved_link);
152
153     File loaded_file;
154     Mount loaded_mount;
155     Link loaded_link;
156     config::loadFromJsonFile(tmpConfigFile, loaded_file);
157     config::loadFromJsonFile(tmpConfigMount, loaded_mount);
158     config::loadFromJsonFile(tmpConfigLink, loaded_link);
159     BOOST_REQUIRE(saved_file == loaded_file);
160     BOOST_REQUIRE(saved_mount == loaded_mount);
161     BOOST_REQUIRE(saved_link == loaded_link);
162 }
163
164 BOOST_AUTO_TEST_SUITE_END()