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 | */ |
Yukai Tu | 73287f5 | 2017-02-06 15:46:40 -0800 | [diff] [blame] | 20 | #define BOOST_TEST_MAIN 1 |
| 21 | #define BOOST_TEST_DYN_LINK 1 |
| 22 | #define BOOST_TEST_MODULE ChronoShare Integrated Tests (FsWatcher) |
Lijing Wang | e0dd63e | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 23 | |
Alexander Afanasyev | f4cde4e | 2016-12-25 13:42:57 -0800 | [diff] [blame] | 24 | #include "fs-watcher.hpp" |
Lijing Wang | e0dd63e | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 25 | #include "test-common.hpp" |
| 26 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 27 | #include <boost/bind.hpp> |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 28 | #include <boost/filesystem.hpp> |
Lijing Wang | e0dd63e | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 29 | #include <boost/filesystem/fstream.hpp> |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 30 | #include <boost/lexical_cast.hpp> |
| 31 | #include <boost/make_shared.hpp> |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 32 | #include <boost/test/unit_test.hpp> |
| 33 | #include <boost/thread/thread.hpp> |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 34 | #include <fstream> |
Lijing Wang | e0dd63e | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 35 | #include <iostream> |
| 36 | #include <thread> |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 37 | #include <set> |
Yukai Tu | 73287f5 | 2017-02-06 15:46:40 -0800 | [diff] [blame] | 38 | #include <QtWidgets> |
Lijing Wang | e0dd63e | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 39 | |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 40 | using namespace std; |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 41 | namespace fs = boost::filesystem; |
| 42 | |
Lijing Wang | e0dd63e | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 43 | _LOG_INIT(Test.FSWatcher); |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 44 | |
Lijing Wang | e0dd63e | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 45 | namespace ndn { |
| 46 | namespace chronoshare { |
| 47 | namespace tests { |
| 48 | |
Lijing Wang | e0dd63e | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 49 | class TestFSWatcherFixture : public IdentityManagementFixture |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 50 | { |
Lijing Wang | e0dd63e | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 51 | public: |
| 52 | TestFSWatcherFixture() |
| 53 | : dir(fs::path(UNIT_TEST_CONFIG_PATH) / "TestFsWatcher") |
| 54 | , argc(0) |
| 55 | { |
| 56 | if (fs::exists(dir)) { |
| 57 | fs::remove_all(dir); |
| 58 | } |
| 59 | |
| 60 | fs::create_directory(dir); |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 61 | } |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 62 | |
Lijing Wang | e0dd63e | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 63 | ~TestFSWatcherFixture(){ |
| 64 | // cleanup |
| 65 | if (fs::exists(dir)) { |
| 66 | _LOG_DEBUG("clean all"); |
| 67 | fs::remove_all(dir); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | void |
| 72 | advanceClocks(std::chrono::seconds delay) |
| 73 | { |
| 74 | std::chrono::milliseconds step = delay; |
| 75 | step /= 50; |
| 76 | for (int i = 0; i < 50; ++i) { |
| 77 | std::this_thread::sleep_for(step); |
| 78 | m_io.poll(); |
| 79 | m_io.reset(); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | void |
| 84 | onChange(set<string>& files, const fs::path& file) |
| 85 | { |
| 86 | _LOG_DEBUG("on change, file: " << file); |
| 87 | files.insert(file.string()); |
| 88 | } |
| 89 | |
| 90 | void |
| 91 | onDelete(set<string>& files, const fs::path& file) |
| 92 | { |
| 93 | _LOG_DEBUG("on delete, file: " << file); |
| 94 | files.erase(file.string()); |
| 95 | } |
| 96 | |
| 97 | void |
| 98 | create_file(const fs::path& ph, const std::string& contents) |
| 99 | { |
| 100 | std::ofstream f(ph.string().c_str()); |
| 101 | if (!f) { |
| 102 | abort(); |
| 103 | } |
| 104 | if (!contents.empty()) { |
| 105 | f << contents; |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | void |
| 110 | run() |
| 111 | { |
Yukai Tu | 73287f5 | 2017-02-06 15:46:40 -0800 | [diff] [blame] | 112 | QApplication app(argc, nullptr); |
Lijing Wang | e0dd63e | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 113 | new FsWatcher(m_io, dir.string().c_str(), |
| 114 | std::bind(&TestFSWatcherFixture::onChange, this, std::ref(files), _1), |
| 115 | std::bind(&TestFSWatcherFixture::onDelete, this, std::ref(files), _1), |
Yukai Tu | 73287f5 | 2017-02-06 15:46:40 -0800 | [diff] [blame] | 116 | &app); |
| 117 | |
| 118 | QTimer::singleShot(21000, &app, SLOT(quit())); |
| 119 | app.exec(); |
Lijing Wang | e0dd63e | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | public: |
| 123 | fs::path dir; |
| 124 | set<string> files; |
| 125 | int argc; |
Yukai Tu | 73287f5 | 2017-02-06 15:46:40 -0800 | [diff] [blame] | 126 | //fsWatcherApp* app; |
Lijing Wang | e0dd63e | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 127 | }; |
| 128 | |
| 129 | BOOST_FIXTURE_TEST_SUITE(TestFsWatcher, TestFSWatcherFixture) |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 130 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 131 | BOOST_AUTO_TEST_CASE(TestFsWatcher) |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 132 | { |
Lijing Wang | e0dd63e | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 133 | std::thread workThread(boost::bind(&TestFSWatcherFixture::run, this)); |
| 134 | this->advanceClocks(std::chrono::seconds(2)); |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 135 | |
| 136 | // ============ check create file detection ================ |
| 137 | create_file(dir / "test.txt", "hello"); |
Lijing Wang | e0dd63e | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 138 | this->advanceClocks(std::chrono::seconds(2)); |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 139 | // test.txt |
| 140 | BOOST_CHECK_EQUAL(files.size(), 1); |
| 141 | BOOST_CHECK(files.find("test.txt") != files.end()); |
| 142 | |
| 143 | // =========== check create a bunch of files in sub dir ============= |
| 144 | fs::path subdir = dir / "sub"; |
| 145 | fs::create_directory(subdir); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 146 | for (int i = 0; i < 10; i++) { |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 147 | string filename = boost::lexical_cast<string>(i); |
| 148 | create_file(subdir / filename.c_str(), boost::lexical_cast<string>(i)); |
| 149 | } |
Lijing Wang | e0dd63e | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 150 | this->advanceClocks(std::chrono::seconds(2)); |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 151 | // test.txt |
| 152 | // sub/0..9 |
| 153 | BOOST_CHECK_EQUAL(files.size(), 11); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 154 | for (int i = 0; i < 10; i++) { |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 155 | string filename = boost::lexical_cast<string>(i); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 156 | BOOST_CHECK(files.find("sub/" + filename) != files.end()); |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | // ============== check copy directory with files to two levels of sub dirs ================= |
| 160 | fs::create_directory(dir / "sub1"); |
| 161 | fs::path subdir1 = dir / "sub1" / "sub2"; |
| 162 | fs::copy_directory(subdir, subdir1); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 163 | for (int i = 0; i < 5; i++) { |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 164 | string filename = boost::lexical_cast<string>(i); |
Lijing Wang | e0dd63e | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 165 | fs::copy(subdir / filename.c_str(), subdir1 / filename.c_str()); |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 166 | } |
Lijing Wang | e0dd63e | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 167 | this->advanceClocks(std::chrono::seconds(2)); |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 168 | // test.txt |
| 169 | // sub/0..9 |
| 170 | // sub1/sub2/0..4 |
| 171 | BOOST_CHECK_EQUAL(files.size(), 16); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 172 | for (int i = 0; i < 5; i++) { |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 173 | string filename = boost::lexical_cast<string>(i); |
| 174 | BOOST_CHECK(files.find("sub1/sub2/" + filename) != files.end()); |
| 175 | } |
| 176 | |
| 177 | // =============== check remove files ========================= |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 178 | for (int i = 0; i < 7; i++) { |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 179 | string filename = boost::lexical_cast<string>(i); |
| 180 | fs::remove(subdir / filename.c_str()); |
| 181 | } |
Lijing Wang | e0dd63e | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 182 | this->advanceClocks(std::chrono::seconds(2)); |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 183 | // test.txt |
| 184 | // sub/7..9 |
| 185 | // sub1/sub2/0..4 |
| 186 | BOOST_CHECK_EQUAL(files.size(), 9); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 187 | for (int i = 0; i < 10; i++) { |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 188 | string filename = boost::lexical_cast<string>(i); |
| 189 | if (i < 7) |
| 190 | BOOST_CHECK(files.find("sub/" + filename) == files.end()); |
| 191 | else |
| 192 | BOOST_CHECK(files.find("sub/" + filename) != files.end()); |
| 193 | } |
| 194 | |
Lijing Wang | e0dd63e | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 195 | // =================== check remove files again, remove the whole dir this time |
| 196 | // =================== |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 197 | // before remove check |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 198 | for (int i = 0; i < 5; i++) { |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 199 | string filename = boost::lexical_cast<string>(i); |
| 200 | BOOST_CHECK(files.find("sub1/sub2/" + filename) != files.end()); |
| 201 | } |
| 202 | fs::remove_all(subdir1); |
Lijing Wang | e0dd63e | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 203 | this->advanceClocks(std::chrono::seconds(2)); |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 204 | BOOST_CHECK_EQUAL(files.size(), 4); |
| 205 | // test.txt |
| 206 | // sub/7..9 |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 207 | for (int i = 0; i < 5; i++) { |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 208 | string filename = boost::lexical_cast<string>(i); |
| 209 | BOOST_CHECK(files.find("sub1/sub2/" + filename) == files.end()); |
| 210 | } |
| 211 | |
| 212 | // =================== check rename files ======================= |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 213 | for (int i = 7; i < 10; i++) { |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 214 | string filename = boost::lexical_cast<string>(i); |
| 215 | fs::rename(subdir / filename.c_str(), dir / filename.c_str()); |
| 216 | } |
Lijing Wang | e0dd63e | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 217 | this->advanceClocks(std::chrono::seconds(2)); |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 218 | // test.txt |
| 219 | // 7 |
| 220 | // 8 |
| 221 | // 9 |
| 222 | // sub |
| 223 | BOOST_CHECK_EQUAL(files.size(), 4); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 224 | for (int i = 7; i < 10; i++) { |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 225 | string filename = boost::lexical_cast<string>(i); |
| 226 | BOOST_CHECK(files.find("sub/" + filename) == files.end()); |
| 227 | BOOST_CHECK(files.find(filename) != files.end()); |
| 228 | } |
| 229 | |
Alexander Afanasyev | a323063 | 2013-02-07 16:24:30 -0800 | [diff] [blame] | 230 | create_file(dir / "add-removal-check.txt", "add-removal-check"); |
Lijing Wang | e0dd63e | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 231 | this->advanceClocks(std::chrono::seconds(2)); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 232 | BOOST_CHECK(files.find("add-removal-check.txt") != files.end()); |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 233 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 234 | fs::remove(dir / "add-removal-check.txt"); |
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()); |
Alexander Afanasyev | a323063 | 2013-02-07 16:24:30 -0800 | [diff] [blame] | 237 | |
| 238 | create_file(dir / "add-removal-check.txt", "add-removal-check"); |
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 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 242 | fs::remove(dir / "add-removal-check.txt"); |
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 | |
| 246 | create_file(dir / "add-removal-check.txt", "add-removal-check"); |
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 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 250 | fs::remove(dir / "add-removal-check.txt"); |
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 | |
Yukai Tu | 73287f5 | 2017-02-06 15:46:40 -0800 | [diff] [blame] | 254 | //emit app->stopApp(); |
Lijing Wang | e0dd63e | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 255 | |
| 256 | workThread.join(); |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | BOOST_AUTO_TEST_SUITE_END() |
Lijing Wang | e0dd63e | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 260 | |
| 261 | } // namespace tests |
| 262 | } // namespace chronoshare |
| 263 | } // namespace ndn |