2 * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
4 * Contact: Lukasz Pawelczyk <l.pawelczyk@partner.samsung.com>
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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
22 * @author Lukasz Pawelczyk (l.pawelczyk@partner.samsung.com)
23 * @brief Unit tests of utils
29 #include "utils/fs.hpp"
30 #include "utils/exception.hpp"
33 #include <sys/mount.h>
34 #include <boost/filesystem.hpp>
36 BOOST_AUTO_TEST_SUITE(UtilsFSSuite)
38 using namespace security_containers;
39 using namespace security_containers::utils;
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"
47 const std::string FILE_CONTENT_2 = "Some other content\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();
74 BOOST_AUTO_TEST_CASE(ReadFileContentTest)
76 BOOST_CHECK_EQUAL(FILE_CONTENT, readFileContent(FILE_PATH));
77 BOOST_CHECK_THROW(readFileContent(BUGGY_FILE_PATH), UtilsException);
80 BOOST_AUTO_TEST_CASE(SaveFileContentTest)
82 BOOST_REQUIRE(saveFileContent(FILE_PATH_RANDOM, FILE_CONTENT));
83 BOOST_CHECK_EQUAL(FILE_CONTENT, readFileContent(FILE_PATH));
85 boost::system::error_code ec;
86 boost::filesystem::remove(FILE_PATH_RANDOM, ec);
89 BOOST_AUTO_TEST_CASE(MountPointTest)
92 namespace fs = boost::filesystem;
93 boost::system::error_code ec;
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);
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);
107 BOOST_REQUIRE(umount(MOUNT_POINT_RANDOM_1));
108 BOOST_REQUIRE(fs::remove(MOUNT_POINT_RANDOM_1, ec));
111 BOOST_AUTO_TEST_CASE(MoveFileTest)
113 namespace fs = boost::filesystem;
114 boost::system::error_code ec;
115 std::string src, dst;
118 src = TMP_PATH + "/" + FILE_NAME_RANDOM_1;
119 dst = TMP_PATH + "/" + FILE_NAME_RANDOM_2;
121 BOOST_REQUIRE(saveFileContent(src, FILE_CONTENT));
123 BOOST_CHECK(moveFile(src, dst));
124 BOOST_CHECK(!fs::exists(src));
125 BOOST_CHECK_EQUAL(readFileContent(dst), FILE_CONTENT);
127 BOOST_REQUIRE(fs::remove(dst));
129 // different mount point
130 src = TMP_PATH + "/" + FILE_NAME_RANDOM_1;
131 dst = MOUNT_POINT_RANDOM_2 + "/" + FILE_NAME_RANDOM_2;
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));
137 BOOST_CHECK(moveFile(src, dst));
138 BOOST_CHECK(!fs::exists(src));
139 BOOST_CHECK_EQUAL(readFileContent(dst), FILE_CONTENT);
141 BOOST_REQUIRE(fs::remove(dst));
142 BOOST_REQUIRE(umount(MOUNT_POINT_RANDOM_2));
143 BOOST_REQUIRE(fs::remove(MOUNT_POINT_RANDOM_2, ec));
146 BOOST_AUTO_TEST_CASE(CopyDirContentsTest)
148 namespace fs = boost::filesystem;
149 std::string src, src_inner, dst, dst_inner;
150 boost::system::error_code ec;
152 src = TMP_PATH + "/" + FILE_DIR_RANDOM_1;
153 src_inner = src + "/" + FILE_DIR_RANDOM_3;
155 dst = TMP_PATH + "/" + FILE_DIR_RANDOM_2;
156 dst_inner = dst + "/" + FILE_DIR_RANDOM_3;
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);
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));
168 BOOST_REQUIRE(fs::create_directory(dst, ec));
169 BOOST_REQUIRE(ec.value() == 0);
172 BOOST_CHECK(copyDirContents(src, dst));
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));
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);
185 BOOST_AUTO_TEST_SUITE_END()