Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2013-2016, Regents of the University of California. |
| 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 | */ |
Alexander Afanasyev | f4cde4e | 2016-12-25 13:42:57 -0800 | [diff] [blame] | 20 | #include "fs-watcher.hpp" |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 21 | #include <boost/bind.hpp> |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 22 | #include <boost/filesystem.hpp> |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 23 | #include <boost/lexical_cast.hpp> |
| 24 | #include <boost/make_shared.hpp> |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 25 | #include <boost/test/unit_test.hpp> |
| 26 | #include <boost/thread/thread.hpp> |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 27 | #include <QtGui> |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 28 | #include <fstream> |
| 29 | #include <set> |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 30 | |
| 31 | using namespace std; |
| 32 | using namespace boost; |
| 33 | namespace fs = boost::filesystem; |
| 34 | |
Alexander Afanasyev | a323063 | 2013-02-07 16:24:30 -0800 | [diff] [blame] | 35 | BOOST_AUTO_TEST_SUITE(TestFsWatcher) |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 36 | |
| 37 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 38 | onChange(set<string>& files, const fs::path& file) |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 39 | { |
Yingdi Yu | a264b87 | 2013-07-10 18:07:23 -0700 | [diff] [blame] | 40 | cerr << "onChange called" << endl; |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 41 | files.insert(file.string()); |
| 42 | } |
| 43 | |
| 44 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 45 | onDelete(set<string>& files, const fs::path& file) |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 46 | { |
| 47 | files.erase(file.string()); |
| 48 | } |
| 49 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 50 | void |
| 51 | create_file(const fs::path& ph, const std::string& contents) |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 52 | { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 53 | std::ofstream f(ph.string().c_str()); |
| 54 | if (!f) { |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 55 | abort(); |
| 56 | } |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 57 | if (!contents.empty()) { |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 58 | f << contents; |
| 59 | } |
| 60 | } |
| 61 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 62 | void |
| 63 | run(fs::path dir, FsWatcher::LocalFile_Change_Callback c, FsWatcher::LocalFile_Change_Callback d) |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 64 | { |
| 65 | int x = 0; |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 66 | QCoreApplication app(x, 0); |
| 67 | FsWatcher watcher(dir.string().c_str(), c, d); |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 68 | app.exec(); |
| 69 | sleep(100); |
| 70 | } |
| 71 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 72 | BOOST_AUTO_TEST_CASE(TestFsWatcher) |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 73 | { |
| 74 | fs::path dir = fs::absolute(fs::path("TestFsWatcher")); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 75 | if (fs::exists(dir)) { |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 76 | fs::remove_all(dir); |
| 77 | } |
| 78 | |
| 79 | fs::create_directory(dir); |
| 80 | |
| 81 | set<string> files; |
| 82 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 83 | FsWatcher::LocalFile_Change_Callback fileChange = boost::bind(onChange, ref(files), _1); |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 84 | FsWatcher::LocalFile_Change_Callback fileDelete = boost::bind(onDelete, ref(files), _1); |
| 85 | |
| 86 | thread workThread(run, dir, fileChange, fileDelete); |
| 87 | //FsWatcher watcher (dir.string().c_str(), fileChange, fileDelete); |
| 88 | |
| 89 | // ============ check create file detection ================ |
| 90 | create_file(dir / "test.txt", "hello"); |
| 91 | // have to at least wait 0.5 seconds |
Yingdi Yu | a264b87 | 2013-07-10 18:07:23 -0700 | [diff] [blame] | 92 | usleep(1000000); |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 93 | // test.txt |
| 94 | BOOST_CHECK_EQUAL(files.size(), 1); |
| 95 | BOOST_CHECK(files.find("test.txt") != files.end()); |
| 96 | |
| 97 | // =========== check create a bunch of files in sub dir ============= |
| 98 | fs::path subdir = dir / "sub"; |
| 99 | fs::create_directory(subdir); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 100 | for (int i = 0; i < 10; i++) { |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 101 | string filename = boost::lexical_cast<string>(i); |
| 102 | create_file(subdir / filename.c_str(), boost::lexical_cast<string>(i)); |
| 103 | } |
| 104 | // have to at least wait 0.5 * 2 seconds |
| 105 | usleep(1100000); |
| 106 | // test.txt |
| 107 | // sub/0..9 |
| 108 | BOOST_CHECK_EQUAL(files.size(), 11); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 109 | for (int i = 0; i < 10; i++) { |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 110 | string filename = boost::lexical_cast<string>(i); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 111 | BOOST_CHECK(files.find("sub/" + filename) != files.end()); |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | // ============== check copy directory with files to two levels of sub dirs ================= |
| 115 | fs::create_directory(dir / "sub1"); |
| 116 | fs::path subdir1 = dir / "sub1" / "sub2"; |
| 117 | fs::copy_directory(subdir, subdir1); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 118 | for (int i = 0; i < 5; i++) { |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 119 | string filename = boost::lexical_cast<string>(i); |
| 120 | fs::copy_file(subdir / filename.c_str(), subdir1 / filename.c_str()); |
| 121 | } |
| 122 | // have to at least wait 0.5 * 2 seconds |
| 123 | usleep(1100000); |
| 124 | // test.txt |
| 125 | // sub/0..9 |
| 126 | // sub1/sub2/0..4 |
| 127 | BOOST_CHECK_EQUAL(files.size(), 16); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 128 | for (int i = 0; i < 5; i++) { |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 129 | string filename = boost::lexical_cast<string>(i); |
| 130 | BOOST_CHECK(files.find("sub1/sub2/" + filename) != files.end()); |
| 131 | } |
| 132 | |
| 133 | // =============== check remove files ========================= |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 134 | for (int i = 0; i < 7; i++) { |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 135 | string filename = boost::lexical_cast<string>(i); |
| 136 | fs::remove(subdir / filename.c_str()); |
| 137 | } |
| 138 | usleep(1100000); |
| 139 | // test.txt |
| 140 | // sub/7..9 |
| 141 | // sub1/sub2/0..4 |
| 142 | BOOST_CHECK_EQUAL(files.size(), 9); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 143 | for (int i = 0; i < 10; i++) { |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 144 | string filename = boost::lexical_cast<string>(i); |
| 145 | if (i < 7) |
| 146 | BOOST_CHECK(files.find("sub/" + filename) == files.end()); |
| 147 | else |
| 148 | BOOST_CHECK(files.find("sub/" + filename) != files.end()); |
| 149 | } |
| 150 | |
| 151 | // =================== check remove files again, remove the whole dir this time =================== |
| 152 | // before remove check |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 153 | for (int i = 0; i < 5; i++) { |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 154 | string filename = boost::lexical_cast<string>(i); |
| 155 | BOOST_CHECK(files.find("sub1/sub2/" + filename) != files.end()); |
| 156 | } |
| 157 | fs::remove_all(subdir1); |
| 158 | usleep(1100000); |
| 159 | BOOST_CHECK_EQUAL(files.size(), 4); |
| 160 | // test.txt |
| 161 | // sub/7..9 |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 162 | for (int i = 0; i < 5; i++) { |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 163 | string filename = boost::lexical_cast<string>(i); |
| 164 | BOOST_CHECK(files.find("sub1/sub2/" + filename) == files.end()); |
| 165 | } |
| 166 | |
| 167 | // =================== check rename files ======================= |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 168 | for (int i = 7; i < 10; i++) { |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 169 | string filename = boost::lexical_cast<string>(i); |
| 170 | fs::rename(subdir / filename.c_str(), dir / filename.c_str()); |
| 171 | } |
| 172 | usleep(1100000); |
| 173 | // test.txt |
| 174 | // 7 |
| 175 | // 8 |
| 176 | // 9 |
| 177 | // sub |
| 178 | BOOST_CHECK_EQUAL(files.size(), 4); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 179 | for (int i = 7; i < 10; i++) { |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 180 | string filename = boost::lexical_cast<string>(i); |
| 181 | BOOST_CHECK(files.find("sub/" + filename) == files.end()); |
| 182 | BOOST_CHECK(files.find(filename) != files.end()); |
| 183 | } |
| 184 | |
Alexander Afanasyev | a323063 | 2013-02-07 16:24:30 -0800 | [diff] [blame] | 185 | create_file(dir / "add-removal-check.txt", "add-removal-check"); |
| 186 | usleep(1200000); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 187 | BOOST_CHECK(files.find("add-removal-check.txt") != files.end()); |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 188 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 189 | fs::remove(dir / "add-removal-check.txt"); |
Alexander Afanasyev | a323063 | 2013-02-07 16:24:30 -0800 | [diff] [blame] | 190 | usleep(1200000); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 191 | BOOST_CHECK(files.find("add-removal-check.txt") == files.end()); |
Alexander Afanasyev | a323063 | 2013-02-07 16:24:30 -0800 | [diff] [blame] | 192 | |
| 193 | create_file(dir / "add-removal-check.txt", "add-removal-check"); |
| 194 | usleep(1200000); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 195 | BOOST_CHECK(files.find("add-removal-check.txt") != files.end()); |
Alexander Afanasyev | a323063 | 2013-02-07 16:24:30 -0800 | [diff] [blame] | 196 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 197 | fs::remove(dir / "add-removal-check.txt"); |
Alexander Afanasyev | a323063 | 2013-02-07 16:24:30 -0800 | [diff] [blame] | 198 | usleep(1200000); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 199 | BOOST_CHECK(files.find("add-removal-check.txt") == files.end()); |
Alexander Afanasyev | a323063 | 2013-02-07 16:24:30 -0800 | [diff] [blame] | 200 | |
| 201 | create_file(dir / "add-removal-check.txt", "add-removal-check"); |
| 202 | usleep(1200000); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 203 | BOOST_CHECK(files.find("add-removal-check.txt") != files.end()); |
Alexander Afanasyev | a323063 | 2013-02-07 16:24:30 -0800 | [diff] [blame] | 204 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 205 | fs::remove(dir / "add-removal-check.txt"); |
Alexander Afanasyev | a323063 | 2013-02-07 16:24:30 -0800 | [diff] [blame] | 206 | usleep(1200000); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 207 | BOOST_CHECK(files.find("add-removal-check.txt") == files.end()); |
Alexander Afanasyev | a323063 | 2013-02-07 16:24:30 -0800 | [diff] [blame] | 208 | |
| 209 | // cleanup |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 210 | if (fs::exists(dir)) { |
Alexander Afanasyev | a323063 | 2013-02-07 16:24:30 -0800 | [diff] [blame] | 211 | fs::remove_all(dir); |
| 212 | } |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | BOOST_AUTO_TEST_SUITE_END() |