1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
|
/*
SPDX-FileCopyrightText: 2010 Sebastian Trueg <trueg at kde.org>
SPDX-FileCopyrightText: 2014 Vishesh Handa <vhanda@kde.org>
SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
*/
#include "kinotify.h"
#include <QTemporaryDir>
#include <KRandom>
#include <QTextStream>
#include <QFile>
#include <QSignalSpy>
#include <QDir>
#include <QTest>
#include <stdio.h>
class KInotifyTest : public QObject
{
Q_OBJECT
private Q_SLOTS:
void testDeleteFile();
void testDeleteFolder();
void testCreateFolder();
void testRenameFile();
void testRenameDeleteFile();
void testMoveFile();
void testRenameFolder();
void testMoveFolder();
void testMoveFromUnwatchedFolder();
void testMoveRootFolder();
void testFileClosedAfterWrite();
};
namespace
{
void touchFile(const QString& path)
{
QFile file(path);
file.open(QIODevice::WriteOnly);
QTextStream s(&file);
s << KRandom::randomString(10);
}
void mkdir(const QString& path)
{
QDir().mkpath(path);
QVERIFY(QDir(path).exists());
}
}
void KInotifyTest::testDeleteFile()
{
// create some test files
QTemporaryDir dir;
const QString f1(QStringLiteral("%1/randomJunk1").arg(dir.path()));
touchFile(f1);
// start the inotify watcher
KInotify kn(nullptr /*no config*/);
kn.addWatch(dir.path(), KInotify::EventAll);
// listen to the desired signal
QSignalSpy spy(&kn, SIGNAL(deleted(QString,bool)));
// test removing a file
QFile::remove(f1);
QVERIFY(spy.wait());
QCOMPARE(spy.count(), 1);
QCOMPARE(spy.takeFirst().at(0).toString(), f1);
}
void KInotifyTest::testDeleteFolder()
{
// create some test files
QTemporaryDir dir;
const QString d1(QStringLiteral("%1/randomJunk4/").arg(dir.path()));
mkdir(d1);
// start the inotify watcher
KInotify kn(nullptr /*no config*/);
kn.addWatch(dir.path(), KInotify::EventAll);
// listen to the desired signal
QSignalSpy spy(&kn, SIGNAL(deleted(QString,bool)));
// test removing a folder
QDir().rmdir(d1);
QVERIFY(spy.wait());
QCOMPARE(spy.count(), 1);
QCOMPARE(spy.first().at(0).toString(), d1);
QCOMPARE(spy.first().at(1).toBool(), true);
// make sure we do not watch the removed folder anymore
QVERIFY(!kn.watchingPath(d1));
}
void KInotifyTest::testCreateFolder()
{
QTemporaryDir dir;
// start the inotify watcher
KInotify kn(nullptr /*no config*/);
kn.addWatch(dir.path(), KInotify::EventAll);
// listen to the desired signal
QSignalSpy createdSpy(&kn, SIGNAL(created(QString,bool)));
// create the subdir
const QString d1(QStringLiteral("%1/randomJunk1/").arg(dir.path()));
mkdir(d1);
QVERIFY(createdSpy.wait());
QCOMPARE(createdSpy.count(), 1);
QCOMPARE(createdSpy.takeFirst().at(0).toString(), d1);
QVERIFY(kn.watchingPath(d1));
// lets go one level deeper
const QString d2 = QStringLiteral("%1subdir1/").arg(d1);
mkdir(d2);
QVERIFY(createdSpy.wait());
QCOMPARE(createdSpy.count(), 1);
QCOMPARE(createdSpy.takeFirst().at(0).toString(), d2);
QVERIFY(kn.watchingPath(d2));
// although we are in the folder test lets try creating a file
const QString f1 = QStringLiteral("%1somefile1").arg(d2);
touchFile(f1);
QVERIFY(createdSpy.wait());
QCOMPARE(createdSpy.count(), 1);
QCOMPARE(createdSpy.takeFirst().at(0).toString(), f1);
}
void KInotifyTest::testRenameFile()
{
// create some test files
QTemporaryDir dir;
const QString f1(QStringLiteral("%1/randomJunk1").arg(dir.path()));
touchFile(f1);
// start the inotify watcher
KInotify kn(nullptr /*no config*/);
kn.addWatch(dir.path(), KInotify::EventAll);
// listen to the desired signal
QSignalSpy spy(&kn, SIGNAL(moved(QString,QString)));
// actually move the file
const QString f2(QStringLiteral("%1/randomJunk2").arg(dir.path()));
rename(f1.toLocal8Bit().constData(), f2.toLocal8Bit().constData());
// check the desired signal
QVERIFY(spy.wait());
QCOMPARE(spy.count(), 1);
QList<QVariant> args = spy.takeFirst();
QCOMPARE(args.at(0).toString(), f1);
QCOMPARE(args.at(1).toString(), f2);
// test a subsequent rename
const QString f3(QStringLiteral("%1/randomJunk3").arg(dir.path()));
rename(f2.toLocal8Bit().constData(), f3.toLocal8Bit().constData());
// check the desired signal
QVERIFY(spy.wait());
QCOMPARE(spy.count(), 1);
args = spy.takeFirst();
QCOMPARE(args.at(0).toString(), f2);
QCOMPARE(args.at(1).toString(), f3);
}
void KInotifyTest::testRenameDeleteFile()
{
// create some test files
QTemporaryDir dir;
const QString f1(QStringLiteral("%1/randomJunk1").arg(dir.path()));
touchFile(f1);
// start the inotify watcher
KInotify kn(nullptr /*no config*/);
kn.addWatch(dir.path(), KInotify::EventAll);
// listen to the desired signal
QSignalSpy spy(&kn, SIGNAL(moved(QString,QString)));
// actually move the file
const QString f2(QStringLiteral("%1/randomJunk2").arg(dir.path()));
rename(f1.toLocal8Bit().constData(), f2.toLocal8Bit().constData());
// check the desired signal
QVERIFY(spy.wait());
QCOMPARE(spy.count(), 1);
QList<QVariant> args = spy.takeFirst();
QCOMPARE(args.at(0).toString(), f1);
QCOMPARE(args.at(1).toString(), f2);
// test a subsequent delete
QSignalSpy spy1(&kn, SIGNAL(deleted(QString,bool)));
QFile::remove(f2);
// check the desired signal
QVERIFY(spy1.wait());
QCOMPARE(spy1.count(), 1);
QCOMPARE(spy1.takeFirst().at(0).toString(), f2);
QVERIFY(spy.isEmpty());
}
void KInotifyTest::testMoveFile()
{
// create some test files
QTemporaryDir dir1;
QTemporaryDir dir2;
const QString src(QStringLiteral("%1/randomJunk1").arg(dir1.path()));
const QString dest(QStringLiteral("%1/randomJunk2").arg(dir2.path()));
touchFile(src);
// start the inotify watcher
KInotify kn(nullptr /*no config*/);
kn.addWatch(dir1.path(), KInotify::EventAll);
kn.addWatch(dir2.path(), KInotify::EventAll);
// listen to the desired signal
QSignalSpy spy(&kn, SIGNAL(moved(QString,QString)));
// actually move the file
rename(src.toLocal8Bit().constData(), dest.toLocal8Bit().constData());
// check the desired signal
QVERIFY(spy.wait());
QCOMPARE(spy.count(), 1);
QList<QVariant> args = spy.takeFirst();
QCOMPARE(args.at(0).toString(), src);
QCOMPARE(args.at(1).toString(), dest);
// test a subsequent move (back to the original folder)
const QString dest2(QStringLiteral("%1/randomJunk3").arg(dir1.path()));
rename(dest.toLocal8Bit().constData(), dest2.toLocal8Bit().constData());
// check the desired signal
QVERIFY(spy.wait());
QCOMPARE(spy.count(), 1);
args = spy.takeFirst();
QCOMPARE(args.at(0).toString(), dest);
QCOMPARE(args.at(1).toString(), dest2);
}
void KInotifyTest::testRenameFolder()
{
// create some test files
QTemporaryDir dir;
const QString d1(QStringLiteral("%1/randomJunk1/").arg(dir.path()));
mkdir(d1);
// start the inotify watcher
KInotify kn(nullptr /*no config*/);
kn.addWatch(dir.path(), KInotify::EventAll);
// listen to the desired signal
QSignalSpy spy(&kn, SIGNAL(moved(QString,QString)));
// actually rename the folder
const QString d2(QStringLiteral("%1/randomJunk2/").arg(dir.path()));
rename(d1.toLocal8Bit().constData(), d2.toLocal8Bit().constData());
// check the desired signal
QVERIFY(spy.wait());
QCOMPARE(spy.count(), 1);
QList<QVariant> args = spy.takeFirst();
QCOMPARE(args.at(0).toString(), d1);
QCOMPARE(args.at(1).toString(), d2);
// check the path cache
QVERIFY(!kn.watchingPath(d1));
QVERIFY(kn.watchingPath(d2));
// test a subsequent rename
const QString d3(QStringLiteral("%1/randomJunk3/").arg(dir.path()));
rename(d2.toLocal8Bit().constData(), d3.toLocal8Bit().constData());
// check the desired signal
QVERIFY(spy.wait());
QCOMPARE(spy.count(), 1);
args = spy.takeFirst();
QCOMPARE(args.at(0).toString(), d2);
QCOMPARE(args.at(1).toString(), d3);
// check the path cache
QVERIFY(!kn.watchingPath(d1));
QVERIFY(!kn.watchingPath(d2));
QVERIFY(kn.watchingPath(d3));
// KInotify claims it has updated its data structures, lets see if that is true
// by creating a file in the new folder
// listen to the desired signal
const QString f4(QStringLiteral("%1somefile").arg(d3));
QSignalSpy createdSpy(&kn, SIGNAL(created(QString,bool)));
// test creating a file
touchFile(f4);
QVERIFY(createdSpy.wait());
QCOMPARE(createdSpy.count(), 1);
QCOMPARE(createdSpy.takeFirst().at(0).toString(), f4);
}
void KInotifyTest::testMoveFolder()
{
// create some test files
QTemporaryDir dir1;
QTemporaryDir dir2;
const QString src(QStringLiteral("%1/randomJunk1/").arg(dir1.path()));
const QString dest(QStringLiteral("%1/randomJunk2/").arg(dir2.path()));
mkdir(src);
// start the inotify watcher
KInotify kn(nullptr /*no config*/);
kn.addWatch(dir1.path(), KInotify::EventAll);
kn.addWatch(dir2.path(), KInotify::EventAll);
// listen to the desired signal
QSignalSpy spy(&kn, SIGNAL(moved(QString,QString)));
// actually move the file
rename(src.toLocal8Bit().constData(), dest.toLocal8Bit().constData());
// check the desired signal
QVERIFY(spy.wait());
QCOMPARE(spy.count(), 1);
QList<QVariant> args = spy.takeFirst();
QCOMPARE(args.at(0).toString(), src);
QCOMPARE(args.at(1).toString(), dest);
// check the path cache
QVERIFY(!kn.watchingPath(src));
QVERIFY(kn.watchingPath(dest));
// test a subsequent move
const QString dest2(QStringLiteral("%1/randomJunk3/").arg(dir1.path()));
rename(dest.toLocal8Bit().constData(), dest2.toLocal8Bit().constData());
// check the desired signal
QVERIFY(spy.wait());
QCOMPARE(spy.count(), 1);
args = spy.takeFirst();
QCOMPARE(args.at(0).toString(), dest);
QCOMPARE(args.at(1).toString(), dest2);
// check the path cache
QVERIFY(!kn.watchingPath(src));
QVERIFY(!kn.watchingPath(dest));
QVERIFY(kn.watchingPath(dest2));
// KInotify claims it has updated its data structures, lets see if that is true
// by creating a file in the new folder
// listen to the desired signal
const QString f4(QStringLiteral("%1somefile").arg(dest2));
QSignalSpy createdSpy(&kn, SIGNAL(created(QString,bool)));
// test creating a file
touchFile(f4);
QVERIFY(createdSpy.wait());
QCOMPARE(createdSpy.count(), 1);
QCOMPARE(createdSpy.takeFirst().at(0).toString(), f4);
}
void KInotifyTest::testMoveFromUnwatchedFolder()
{
// create some test folders
QTemporaryDir dir;
const QString src(QStringLiteral("%1/randomJunk1").arg(dir.path()));
const QString dest(QStringLiteral("%1/randomJunk2").arg(dir.path()));
mkdir(src);
mkdir(dest);
// Start watching only for destination
KInotify kn(nullptr);
kn.addWatch(dest, KInotify::EventAll);
QSignalSpy spy(&kn, &KInotify::created);
// Create stuff inside src
mkdir(QStringLiteral("%1/sub").arg(src));
touchFile(QStringLiteral("%1/sub/file1").arg(src));
mkdir(QStringLiteral("%1/sub/sub1").arg(src));
touchFile(QStringLiteral("%1/sub/sub1/file2").arg(src));
// Now move
QFile::rename(QStringLiteral("%1/sub").arg(src),
QStringLiteral("%1/sub").arg(dest));
QVERIFY(spy.wait());
QCOMPARE(spy.count(), 4);
// Checking if watches are installed
QSignalSpy spy1(&kn, &KInotify::deleted);
QDir dstdir(QStringLiteral("%1/sub").arg(dest));
dstdir.removeRecursively();
QVERIFY(spy1.wait());
QCOMPARE(spy1.count(), 4);
}
void KInotifyTest::testMoveRootFolder()
{
// create some test folders
QTemporaryDir dir;
const QString src(QStringLiteral("%1/randomJunk1").arg(dir.path()));
const QString dest(QStringLiteral("%1/randomJunk2").arg(dir.path()));
mkdir(src);
// start watching the new subfolder only
KInotify kn(nullptr /*no config*/);
kn.addWatch(src, KInotify::EventAll);
// listen for the moved signal
QSignalSpy spy(&kn, SIGNAL(moved(QString,QString)));
// actually move the file
QFile::rename(src, dest);
// check the desired signal
QEXPECT_FAIL("", "KInotify cannot handle moving of top-level folders.", Abort);
QVERIFY(spy.wait(500));
QCOMPARE(spy.count(), 1);
QList<QVariant> args = spy.takeFirst();
QCOMPARE(args.at(0).toString(), src);
QCOMPARE(args.at(1).toString(), dest);
// check the path cache
QVERIFY(!kn.watchingPath(src));
QVERIFY(kn.watchingPath(dest));
}
void KInotifyTest::testFileClosedAfterWrite()
{
QTemporaryDir dir;
touchFile(dir.path() + QLatin1String("/file"));
KInotify kn(nullptr /*no config*/);
kn.addWatch(dir.path(), KInotify::EventAll);
QSignalSpy spy(&kn, SIGNAL(closedWrite(QString)));
touchFile(dir.path() + QLatin1String("/file"));
QVERIFY(spy.wait());
QCOMPARE(spy.count(), 1);
QCOMPARE(spy.at(0).first().toString(), QString(dir.path() + QLatin1String("/file")));
}
QTEST_GUILESS_MAIN(KInotifyTest)
#include "kinotifytest.moc"
|