Add API to create new containers
[platform/core/security/vasum.git] / tests / unit_tests / utils / ut-fs.cpp
1 /*
2  *  Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *  Contact: Lukasz Pawelczyk <l.pawelczyk@partner.samsung.com>
5  *
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License
17  */
18
19
20 /**
21  * @file
22  * @author  Lukasz Pawelczyk (l.pawelczyk@partner.samsung.com)
23  * @brief   Unit tests of utils
24  */
25
26 #include "config.hpp"
27 #include "ut.hpp"
28
29 #include "utils/fs.hpp"
30 #include "utils/exception.hpp"
31
32 #include <memory>
33 #include <sys/mount.h>
34 #include <boost/filesystem.hpp>
35
36 BOOST_AUTO_TEST_SUITE(UtilsFSSuite)
37
38 using namespace security_containers;
39 using namespace security_containers::utils;
40
41 namespace {
42
43 const std::string FILE_PATH = SC_TEST_CONFIG_INSTALL_DIR "/utils/ut-fs/file.txt";
44 const std::string FILE_CONTENT = "File content\n"
45                                  "Line 1\n"
46                                  "Line 2\n";
47 const std::string FILE_CONTENT_2 = "Some other content\n"
48                                    "Just to see if\n"
49                                    "everything is copied correctly\n";
50 const std::string FILE_CONTENT_3 = "More content\n"
51                                    "More and more content\n"
52                                    "That's a lot of data to test\n";
53 const std::string BUGGY_FILE_PATH = "/some/missing/file/path/file.txt";
54 const std::string TMP_PATH = "/tmp";
55 const std::string FILE_PATH_RANDOM =
56     boost::filesystem::unique_path("/tmp/testFile-%%%%").string();
57 const std::string MOUNT_POINT_RANDOM_1 =
58     boost::filesystem::unique_path("/tmp/mountPoint-%%%%").string();
59 const std::string MOUNT_POINT_RANDOM_2 =
60     boost::filesystem::unique_path("/tmp/mountPoint-%%%%").string();
61 const std::string FILE_DIR_RANDOM_1 =
62     boost::filesystem::unique_path("testDir-%%%%").string();
63 const std::string FILE_DIR_RANDOM_2 =
64     boost::filesystem::unique_path("testDir-%%%%").string();
65 const std::string FILE_DIR_RANDOM_3 =
66     boost::filesystem::unique_path("testDir-%%%%").string();
67 const std::string FILE_NAME_RANDOM_1 =
68     boost::filesystem::unique_path("testFile-%%%%").string();
69 const std::string FILE_NAME_RANDOM_2 =
70     boost::filesystem::unique_path("testFile-%%%%").string();
71
72 } // namespace
73
74 BOOST_AUTO_TEST_CASE(ReadFileContentTest)
75 {
76     BOOST_CHECK_EQUAL(FILE_CONTENT, readFileContent(FILE_PATH));
77     BOOST_CHECK_THROW(readFileContent(BUGGY_FILE_PATH), UtilsException);
78 }
79
80 BOOST_AUTO_TEST_CASE(SaveFileContentTest)
81 {
82     BOOST_REQUIRE(saveFileContent(FILE_PATH_RANDOM, FILE_CONTENT));
83     BOOST_CHECK_EQUAL(FILE_CONTENT, readFileContent(FILE_PATH));
84
85     boost::system::error_code ec;
86     boost::filesystem::remove(FILE_PATH_RANDOM, ec);
87 }
88
89 BOOST_AUTO_TEST_CASE(MountPointTest)
90 {
91     bool result;
92     namespace fs = boost::filesystem;
93     boost::system::error_code ec;
94
95     BOOST_REQUIRE(fs::create_directory(MOUNT_POINT_RANDOM_1, ec));
96     BOOST_REQUIRE(isMountPoint(MOUNT_POINT_RANDOM_1, result));
97     BOOST_CHECK_EQUAL(result, false);
98     BOOST_REQUIRE(hasSameMountPoint(TMP_PATH, MOUNT_POINT_RANDOM_1, result));
99     BOOST_CHECK_EQUAL(result, true);
100
101     BOOST_REQUIRE(mountRun(MOUNT_POINT_RANDOM_1));
102     BOOST_REQUIRE(isMountPoint(MOUNT_POINT_RANDOM_1, result));
103     BOOST_CHECK_EQUAL(result, true);
104     BOOST_REQUIRE(hasSameMountPoint(TMP_PATH, MOUNT_POINT_RANDOM_1, result));
105     BOOST_CHECK_EQUAL(result, false);
106
107     BOOST_REQUIRE(umount(MOUNT_POINT_RANDOM_1));
108     BOOST_REQUIRE(fs::remove(MOUNT_POINT_RANDOM_1, ec));
109 }
110
111 BOOST_AUTO_TEST_CASE(MoveFileTest)
112 {
113     namespace fs = boost::filesystem;
114     boost::system::error_code ec;
115     std::string src, dst;
116
117     // same mount point
118     src = TMP_PATH + "/" + FILE_NAME_RANDOM_1;
119     dst = TMP_PATH + "/" + FILE_NAME_RANDOM_2;
120
121     BOOST_REQUIRE(saveFileContent(src, FILE_CONTENT));
122
123     BOOST_CHECK(moveFile(src, dst));
124     BOOST_CHECK(!fs::exists(src));
125     BOOST_CHECK_EQUAL(readFileContent(dst), FILE_CONTENT);
126
127     BOOST_REQUIRE(fs::remove(dst));
128
129     // different mount point
130     src = TMP_PATH + "/" + FILE_NAME_RANDOM_1;
131     dst = MOUNT_POINT_RANDOM_2 + "/" + FILE_NAME_RANDOM_2;
132
133     BOOST_REQUIRE(fs::create_directory(MOUNT_POINT_RANDOM_2, ec));
134     BOOST_REQUIRE(mountRun(MOUNT_POINT_RANDOM_2));
135     BOOST_REQUIRE(saveFileContent(src, FILE_CONTENT));
136
137     BOOST_CHECK(moveFile(src, dst));
138     BOOST_CHECK(!fs::exists(src));
139     BOOST_CHECK_EQUAL(readFileContent(dst), FILE_CONTENT);
140
141     BOOST_REQUIRE(fs::remove(dst));
142     BOOST_REQUIRE(umount(MOUNT_POINT_RANDOM_2));
143     BOOST_REQUIRE(fs::remove(MOUNT_POINT_RANDOM_2, ec));
144 }
145
146 BOOST_AUTO_TEST_CASE(CopyDirContentsTest)
147 {
148     namespace fs = boost::filesystem;
149     std::string src, src_inner, dst, dst_inner;
150     boost::system::error_code ec;
151
152     src = TMP_PATH + "/" + FILE_DIR_RANDOM_1;
153     src_inner = src + "/" + FILE_DIR_RANDOM_3;
154
155     dst = TMP_PATH + "/" + FILE_DIR_RANDOM_2;
156     dst_inner = dst + "/" + FILE_DIR_RANDOM_3;
157
158     // create entire structure with files
159     BOOST_REQUIRE(fs::create_directory(src, ec));
160     BOOST_REQUIRE(ec.value() == 0);
161     BOOST_REQUIRE(fs::create_directory(src_inner, ec));
162     BOOST_REQUIRE(ec.value() == 0);
163
164     BOOST_REQUIRE(saveFileContent(src + "/" + FILE_NAME_RANDOM_1, FILE_CONTENT));
165     BOOST_REQUIRE(saveFileContent(src + "/" + FILE_NAME_RANDOM_2, FILE_CONTENT_2));
166     BOOST_REQUIRE(saveFileContent(src_inner + "/" + FILE_NAME_RANDOM_1, FILE_CONTENT_3));
167
168     BOOST_REQUIRE(fs::create_directory(dst, ec));
169     BOOST_REQUIRE(ec.value() == 0);
170
171     // copy data
172     BOOST_CHECK(copyDirContents(src, dst));
173
174     // check if copy is successful
175     BOOST_CHECK(fs::exists(dst + "/" + FILE_NAME_RANDOM_1));
176     BOOST_CHECK(fs::exists(dst + "/" + FILE_NAME_RANDOM_2));
177     BOOST_CHECK(fs::exists(dst_inner));
178     BOOST_CHECK(fs::exists(dst_inner + "/" + FILE_NAME_RANDOM_1));
179
180     BOOST_CHECK_EQUAL(readFileContent(dst + "/" + FILE_NAME_RANDOM_1), FILE_CONTENT);
181     BOOST_CHECK_EQUAL(readFileContent(dst + "/" + FILE_NAME_RANDOM_2), FILE_CONTENT_2);
182     BOOST_CHECK_EQUAL(readFileContent(dst_inner + "/" + FILE_NAME_RANDOM_1), FILE_CONTENT_3);
183 }
184
185 BOOST_AUTO_TEST_SUITE_END()