Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Lijing Wang | e0dd63e | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 3 | * Copyright (c) 2013-2017, Regents of the University of California. |
Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ChronoShare, a decentralized file sharing application over NDN. |
| 6 | * |
| 7 | * ChronoShare is free software: you can redistribute it and/or modify it under the terms |
| 8 | * of the GNU General Public License as published by the Free Software Foundation, either |
| 9 | * version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ChronoShare is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License along with |
| 16 | * ChronoShare, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 17 | * |
| 18 | * See AUTHORS.md for complete list of ChronoShare authors and contributors. |
| 19 | */ |
Lijing Wang | e0dd63e | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 20 | |
Alexander Afanasyev | f4cde4e | 2016-12-25 13:42:57 -0800 | [diff] [blame] | 21 | #include "fs-watcher.hpp" |
Lijing Wang | e0dd63e | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 22 | #include "test-common.hpp" |
| 23 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 24 | #include <boost/bind.hpp> |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 25 | #include <boost/filesystem.hpp> |
Lijing Wang | e0dd63e | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 26 | #include <boost/filesystem/fstream.hpp> |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 27 | #include <boost/lexical_cast.hpp> |
| 28 | #include <boost/make_shared.hpp> |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 29 | #include <boost/test/unit_test.hpp> |
| 30 | #include <boost/thread/thread.hpp> |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 31 | #include <fstream> |
Lijing Wang | e0dd63e | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 32 | #include <iostream> |
| 33 | #include <thread> |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 34 | #include <set> |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 35 | |
Lijing Wang | e0dd63e | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 36 | #include "fs-watcher.t.hpp" |
| 37 | |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 38 | using namespace std; |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 39 | namespace fs = boost::filesystem; |
| 40 | |
Lijing Wang | e0dd63e | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 41 | _LOG_INIT(Test.FSWatcher); |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 42 | |
Lijing Wang | e0dd63e | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 43 | namespace ndn { |
| 44 | namespace chronoshare { |
| 45 | namespace tests { |
| 46 | |
| 47 | fsWatcherApp::fsWatcherApp(int& argc, char** argv) |
| 48 | : QCoreApplication(argc, argv) |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 49 | { |
Lijing Wang | e0dd63e | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 50 | connect(this, SIGNAL(stopApp()), this, SLOT(quit()), Qt::QueuedConnection); |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 51 | } |
| 52 | |
Lijing Wang | e0dd63e | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 53 | fsWatcherApp::~fsWatcherApp() = default; |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 54 | |
Lijing Wang | e0dd63e | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 55 | class TestFSWatcherFixture : public IdentityManagementFixture |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 56 | { |
Lijing Wang | e0dd63e | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 57 | public: |
| 58 | TestFSWatcherFixture() |
| 59 | : dir(fs::path(UNIT_TEST_CONFIG_PATH) / "TestFsWatcher") |
| 60 | , argc(0) |
| 61 | { |
| 62 | if (fs::exists(dir)) { |
| 63 | fs::remove_all(dir); |
| 64 | } |
| 65 | |
| 66 | fs::create_directory(dir); |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 67 | } |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 68 | |
Lijing Wang | e0dd63e | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 69 | ~TestFSWatcherFixture(){ |
| 70 | // cleanup |
| 71 | if (fs::exists(dir)) { |
| 72 | _LOG_DEBUG("clean all"); |
| 73 | fs::remove_all(dir); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | void |
| 78 | advanceClocks(std::chrono::seconds delay) |
| 79 | { |
| 80 | std::chrono::milliseconds step = delay; |
| 81 | step /= 50; |
| 82 | for (int i = 0; i < 50; ++i) { |
| 83 | std::this_thread::sleep_for(step); |
| 84 | m_io.poll(); |
| 85 | m_io.reset(); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | void |
| 90 | onChange(set<string>& files, const fs::path& file) |
| 91 | { |
| 92 | _LOG_DEBUG("on change, file: " << file); |
| 93 | files.insert(file.string()); |
| 94 | } |
| 95 | |
| 96 | void |
| 97 | onDelete(set<string>& files, const fs::path& file) |
| 98 | { |
| 99 | _LOG_DEBUG("on delete, file: " << file); |
| 100 | files.erase(file.string()); |
| 101 | } |
| 102 | |
| 103 | void |
| 104 | create_file(const fs::path& ph, const std::string& contents) |
| 105 | { |
| 106 | std::ofstream f(ph.string().c_str()); |
| 107 | if (!f) { |
| 108 | abort(); |
| 109 | } |
| 110 | if (!contents.empty()) { |
| 111 | f << contents; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | void |
| 116 | run() |
| 117 | { |
| 118 | app = new fsWatcherApp(argc, nullptr); |
| 119 | new FsWatcher(m_io, dir.string().c_str(), |
| 120 | std::bind(&TestFSWatcherFixture::onChange, this, std::ref(files), _1), |
| 121 | std::bind(&TestFSWatcherFixture::onDelete, this, std::ref(files), _1), |
| 122 | app); |
| 123 | app->exec(); |
| 124 | } |
| 125 | |
| 126 | public: |
| 127 | fs::path dir; |
| 128 | set<string> files; |
| 129 | int argc; |
| 130 | fsWatcherApp* app; |
| 131 | }; |
| 132 | |
| 133 | BOOST_FIXTURE_TEST_SUITE(TestFsWatcher, TestFSWatcherFixture) |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 134 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 135 | BOOST_AUTO_TEST_CASE(TestFsWatcher) |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 136 | { |
Lijing Wang | e0dd63e | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 137 | std::thread workThread(boost::bind(&TestFSWatcherFixture::run, this)); |
| 138 | this->advanceClocks(std::chrono::seconds(2)); |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 139 | |
| 140 | // ============ check create file detection ================ |
| 141 | create_file(dir / "test.txt", "hello"); |
Lijing Wang | e0dd63e | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 142 | this->advanceClocks(std::chrono::seconds(2)); |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 143 | // test.txt |
| 144 | BOOST_CHECK_EQUAL(files.size(), 1); |
| 145 | BOOST_CHECK(files.find("test.txt") != files.end()); |
| 146 | |
| 147 | // =========== check create a bunch of files in sub dir ============= |
| 148 | fs::path subdir = dir / "sub"; |
| 149 | fs::create_directory(subdir); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 150 | for (int i = 0; i < 10; i++) { |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 151 | string filename = boost::lexical_cast<string>(i); |
| 152 | create_file(subdir / filename.c_str(), boost::lexical_cast<string>(i)); |
| 153 | } |
Lijing Wang | e0dd63e | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 154 | this->advanceClocks(std::chrono::seconds(2)); |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 155 | // test.txt |
| 156 | // sub/0..9 |
| 157 | BOOST_CHECK_EQUAL(files.size(), 11); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 158 | for (int i = 0; i < 10; i++) { |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 159 | string filename = boost::lexical_cast<string>(i); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 160 | BOOST_CHECK(files.find("sub/" + filename) != files.end()); |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | // ============== check copy directory with files to two levels of sub dirs ================= |
| 164 | fs::create_directory(dir / "sub1"); |
| 165 | fs::path subdir1 = dir / "sub1" / "sub2"; |
| 166 | fs::copy_directory(subdir, subdir1); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 167 | for (int i = 0; i < 5; i++) { |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 168 | string filename = boost::lexical_cast<string>(i); |
Lijing Wang | e0dd63e | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 169 | fs::copy(subdir / filename.c_str(), subdir1 / filename.c_str()); |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 170 | } |
Lijing Wang | e0dd63e | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 171 | this->advanceClocks(std::chrono::seconds(2)); |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 172 | // test.txt |
| 173 | // sub/0..9 |
| 174 | // sub1/sub2/0..4 |
| 175 | BOOST_CHECK_EQUAL(files.size(), 16); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 176 | for (int i = 0; i < 5; i++) { |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 177 | string filename = boost::lexical_cast<string>(i); |
| 178 | BOOST_CHECK(files.find("sub1/sub2/" + filename) != files.end()); |
| 179 | } |
| 180 | |
| 181 | // =============== check remove files ========================= |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 182 | for (int i = 0; i < 7; i++) { |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 183 | string filename = boost::lexical_cast<string>(i); |
| 184 | fs::remove(subdir / filename.c_str()); |
| 185 | } |
Lijing Wang | e0dd63e | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 186 | this->advanceClocks(std::chrono::seconds(2)); |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 187 | // test.txt |
| 188 | // sub/7..9 |
| 189 | // sub1/sub2/0..4 |
| 190 | BOOST_CHECK_EQUAL(files.size(), 9); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 191 | for (int i = 0; i < 10; i++) { |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 192 | string filename = boost::lexical_cast<string>(i); |
| 193 | if (i < 7) |
| 194 | BOOST_CHECK(files.find("sub/" + filename) == files.end()); |
| 195 | else |
| 196 | BOOST_CHECK(files.find("sub/" + filename) != files.end()); |
| 197 | } |
| 198 | |
Lijing Wang | e0dd63e | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 199 | // =================== check remove files again, remove the whole dir this time |
| 200 | // =================== |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 201 | // before remove check |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 202 | for (int i = 0; i < 5; i++) { |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 203 | string filename = boost::lexical_cast<string>(i); |
| 204 | BOOST_CHECK(files.find("sub1/sub2/" + filename) != files.end()); |
| 205 | } |
| 206 | fs::remove_all(subdir1); |
Lijing Wang | e0dd63e | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 207 | this->advanceClocks(std::chrono::seconds(2)); |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 208 | BOOST_CHECK_EQUAL(files.size(), 4); |
| 209 | // test.txt |
| 210 | // sub/7..9 |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 211 | for (int i = 0; i < 5; i++) { |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 212 | string filename = boost::lexical_cast<string>(i); |
| 213 | BOOST_CHECK(files.find("sub1/sub2/" + filename) == files.end()); |
| 214 | } |
| 215 | |
| 216 | // =================== check rename files ======================= |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 217 | for (int i = 7; i < 10; i++) { |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 218 | string filename = boost::lexical_cast<string>(i); |
| 219 | fs::rename(subdir / filename.c_str(), dir / filename.c_str()); |
| 220 | } |
Lijing Wang | e0dd63e | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 221 | this->advanceClocks(std::chrono::seconds(2)); |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 222 | // test.txt |
| 223 | // 7 |
| 224 | // 8 |
| 225 | // 9 |
| 226 | // sub |
| 227 | BOOST_CHECK_EQUAL(files.size(), 4); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 228 | for (int i = 7; i < 10; i++) { |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 229 | string filename = boost::lexical_cast<string>(i); |
| 230 | BOOST_CHECK(files.find("sub/" + filename) == files.end()); |
| 231 | BOOST_CHECK(files.find(filename) != files.end()); |
| 232 | } |
| 233 | |
Alexander Afanasyev | a323063 | 2013-02-07 16:24:30 -0800 | [diff] [blame] | 234 | create_file(dir / "add-removal-check.txt", "add-removal-check"); |
Lijing Wang | e0dd63e | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 235 | this->advanceClocks(std::chrono::seconds(2)); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 236 | BOOST_CHECK(files.find("add-removal-check.txt") != files.end()); |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 237 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 238 | fs::remove(dir / "add-removal-check.txt"); |
Lijing Wang | e0dd63e | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 239 | this->advanceClocks(std::chrono::seconds(2)); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 240 | BOOST_CHECK(files.find("add-removal-check.txt") == files.end()); |
Alexander Afanasyev | a323063 | 2013-02-07 16:24:30 -0800 | [diff] [blame] | 241 | |
| 242 | create_file(dir / "add-removal-check.txt", "add-removal-check"); |
Lijing Wang | e0dd63e | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 243 | this->advanceClocks(std::chrono::seconds(2)); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 244 | BOOST_CHECK(files.find("add-removal-check.txt") != files.end()); |
Alexander Afanasyev | a323063 | 2013-02-07 16:24:30 -0800 | [diff] [blame] | 245 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 246 | fs::remove(dir / "add-removal-check.txt"); |
Lijing Wang | e0dd63e | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 247 | this->advanceClocks(std::chrono::seconds(2)); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 248 | BOOST_CHECK(files.find("add-removal-check.txt") == files.end()); |
Alexander Afanasyev | a323063 | 2013-02-07 16:24:30 -0800 | [diff] [blame] | 249 | |
| 250 | create_file(dir / "add-removal-check.txt", "add-removal-check"); |
Lijing Wang | e0dd63e | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 251 | this->advanceClocks(std::chrono::seconds(2)); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 252 | BOOST_CHECK(files.find("add-removal-check.txt") != files.end()); |
Alexander Afanasyev | a323063 | 2013-02-07 16:24:30 -0800 | [diff] [blame] | 253 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 254 | fs::remove(dir / "add-removal-check.txt"); |
Lijing Wang | e0dd63e | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 255 | this->advanceClocks(std::chrono::seconds(2)); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 256 | BOOST_CHECK(files.find("add-removal-check.txt") == files.end()); |
Alexander Afanasyev | a323063 | 2013-02-07 16:24:30 -0800 | [diff] [blame] | 257 | |
Lijing Wang | e0dd63e | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 258 | emit app->stopApp(); |
| 259 | |
| 260 | workThread.join(); |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | BOOST_AUTO_TEST_SUITE_END() |
Lijing Wang | e0dd63e | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 264 | |
| 265 | } // namespace tests |
| 266 | } // namespace chronoshare |
| 267 | } // namespace ndn |