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
|
/*
* This software is in the public domain, furnished "as is", without technical
* support, and with no warranty, express or implied, as to its usefulness for
* any purpose.
*
*/
#include <QtTest>
#include "testutils/syncenginetestutils.h"
#include <syncengine.h>
using namespace OCC;
SyncJournalFileRecord journalRecord(FakeFolder &folder, const QByteArray &path)
{
SyncJournalFileRecord rec;
folder.syncJournal().getFileRecord(path, &rec);
return rec;
}
class TestBlacklist : public QObject
{
Q_OBJECT
private Q_SLOTS:
void initTestCase_data()
{
QTest::addColumn<Vfs::Mode>("vfsMode");
QTest::addColumn<bool>("filesAreDehydrated");
QTest::newRow("Vfs::Off") << Vfs::Off << false;
if (VfsPluginManager::instance().isVfsPluginAvailable(Vfs::WindowsCfApi)) {
QTest::newRow("Vfs::WindowsCfApi dehydrated") << Vfs::WindowsCfApi << true;
// TODO: the hydrated version will fail due to an issue in the winvfs plugin, so leave it disabled for now.
// QTest::newRow("Vfs::WindowsCfApi hydrated") << Vfs::WindowsCfApi << false;
} else if (Utility::isWindows()) {
qWarning("Skipping Vfs::WindowsCfApi");
}
}
void testBlacklistBasic_data()
{
QTest::addColumn<bool>("remote");
QTest::newRow("remote") << true;
QTest::newRow("local") << false;
}
void testBlacklistBasic()
{
QFETCH_GLOBAL(Vfs::Mode, vfsMode);
QFETCH_GLOBAL(bool, filesAreDehydrated);
QFETCH(bool, remote);
FakeFolder fakeFolder(FileInfo::A12_B12_C12_S12(), vfsMode, filesAreDehydrated);
QCOMPARE(fakeFolder.currentLocalState(), fakeFolder.currentRemoteState());
ItemCompletedSpy completeSpy(fakeFolder);
auto &modifier = remote ? fakeFolder.remoteModifier() : fakeFolder.localModifier();
int counter = 0;
const QString testFileName = QStringLiteral("A/new");
QByteArray reqId;
fakeFolder.setServerOverride([&](QNetworkAccessManager::Operation op, const QNetworkRequest &req, QIODevice *) -> QNetworkReply * {
if (req.url().path().endsWith(testFileName)) {
reqId = req.rawHeader("X-Request-ID");
}
if (!remote && op == QNetworkAccessManager::PutOperation)
++counter;
if (remote && op == QNetworkAccessManager::GetOperation)
++counter;
return nullptr;
});
auto cleanup = [&]() {
completeSpy.clear();
};
auto initialEtag = journalRecord(fakeFolder, "A")._etag;
QVERIFY(!initialEtag.isEmpty());
// The first sync and the download will fail - the item will be blacklisted
modifier.insert(testFileName);
fakeFolder.serverErrorPaths().append(testFileName, 500); // will be blacklisted
const bool syncResult = fakeFolder.applyLocalModificationsAndSync();
if (vfsMode == Vfs::WindowsCfApi && filesAreDehydrated && remote) {
// With dehydrated files, only a PROPFIND is done, but not a GET request.
// And it is the GET request that fails, and causes a blacklist entry, all "syncs" will succeed.
QVERIFY(syncResult);
QSKIP("The remainder of this test is not applicable for dehydrated files");
}
QVERIFY(!syncResult);
{
auto it = completeSpy.findItem(testFileName);
QVERIFY(it);
QCOMPARE(it->_status, SyncFileItem::NormalError); // initial error visible
QCOMPARE(it->instruction(), CSYNC_INSTRUCTION_NEW);
auto entry = fakeFolder.syncJournal().errorBlacklistEntry(testFileName);
QVERIFY(entry.isValid());
QCOMPARE(entry._errorCategory, SyncJournalErrorBlacklistRecord::Category::Normal);
QCOMPARE(entry._retryCount, 1);
QCOMPARE(counter, 1);
QVERIFY(entry._ignoreDuration > 0);
QCOMPARE(entry._requestId, reqId);
if (remote) {
QCOMPARE(journalRecord(fakeFolder, "A")._etag, initialEtag);
}
}
cleanup();
// Ignored during the second run - but soft errors are also errors
QVERIFY(!fakeFolder.applyLocalModificationsAndSync());
{
auto it = completeSpy.findItem(testFileName);
QVERIFY(it);
QCOMPARE(it->_status, SyncFileItem::BlacklistedError);
QCOMPARE(it->instruction(), CSYNC_INSTRUCTION_IGNORE); // no retry happened!
auto entry = fakeFolder.syncJournal().errorBlacklistEntry(testFileName);
QVERIFY(entry.isValid());
QCOMPARE(entry._errorCategory, SyncJournalErrorBlacklistRecord::Category::Normal);
QCOMPARE(entry._retryCount, 1);
QCOMPARE(counter, 1);
QVERIFY(entry._ignoreDuration > 0);
QCOMPARE(entry._requestId, reqId);
if (remote)
QCOMPARE(journalRecord(fakeFolder, "A")._etag, initialEtag);
}
cleanup();
// Let's expire the blacklist entry to verify it gets retried
{
auto entry = fakeFolder.syncJournal().errorBlacklistEntry(testFileName);
entry._ignoreDuration = 1;
entry._lastTryTime -= 1;
fakeFolder.syncJournal().setErrorBlacklistEntry(entry);
}
QVERIFY(!fakeFolder.applyLocalModificationsAndSync());
{
auto it = completeSpy.findItem(testFileName);
QVERIFY(it);
QCOMPARE(it->_status, SyncFileItem::BlacklistedError); // blacklisted as it's just a retry
QCOMPARE(it->instruction(), CSYNC_INSTRUCTION_NEW); // retry!
auto entry = fakeFolder.syncJournal().errorBlacklistEntry(testFileName);
QVERIFY(entry.isValid());
QCOMPARE(entry._errorCategory, SyncJournalErrorBlacklistRecord::Category::Normal);
QCOMPARE(entry._retryCount, 2);
QCOMPARE(counter, 2);
QVERIFY(entry._ignoreDuration > 0);
QCOMPARE(entry._requestId, reqId);
if (remote)
QCOMPARE(journalRecord(fakeFolder, "A")._etag, initialEtag);
}
cleanup();
// Try to make sure that the modifications below do are not within the same second as the
// previous sync attempt was done, otherwise the changes go undetected.
QThread::sleep(1);
// When the file changes a retry happens immediately
modifier.appendByte(testFileName);
QVERIFY(!fakeFolder.applyLocalModificationsAndSync());
{
auto it = completeSpy.findItem(testFileName);
QVERIFY(it);
QCOMPARE(it->_status, SyncFileItem::BlacklistedError);
QCOMPARE(it->instruction(), CSYNC_INSTRUCTION_NEW); // retry!
auto entry = fakeFolder.syncJournal().errorBlacklistEntry(testFileName);
QVERIFY(entry.isValid());
QCOMPARE(entry._errorCategory, SyncJournalErrorBlacklistRecord::Category::Normal);
QCOMPARE(entry._retryCount, 3);
QCOMPARE(counter, 3);
QVERIFY(entry._ignoreDuration > 0);
QCOMPARE(entry._requestId, reqId);
if (remote)
QCOMPARE(journalRecord(fakeFolder, "A")._etag, initialEtag);
}
cleanup();
// When the error goes away and the item is retried, the sync succeeds
fakeFolder.serverErrorPaths().clear();
{
auto entry = fakeFolder.syncJournal().errorBlacklistEntry(testFileName);
entry._ignoreDuration = 1;
entry._lastTryTime -= 1;
fakeFolder.syncJournal().setErrorBlacklistEntry(entry);
}
QVERIFY(fakeFolder.applyLocalModificationsAndSync());
{
auto it = completeSpy.findItem(testFileName);
QVERIFY(it);
QCOMPARE(it->_status, SyncFileItem::Success);
QCOMPARE(it->instruction(), CSYNC_INSTRUCTION_NEW);
auto entry = fakeFolder.syncJournal().errorBlacklistEntry(testFileName);
QVERIFY(!entry.isValid());
QCOMPARE(counter, 4);
if (remote)
QCOMPARE(journalRecord(fakeFolder, "A")._etag, fakeFolder.currentRemoteState().find(QStringLiteral("A"))->etag);
}
cleanup();
QCOMPARE(fakeFolder.currentLocalState(), fakeFolder.currentRemoteState());
}
};
QTEST_GUILESS_MAIN(TestBlacklist)
#include "testblacklist.moc"
|