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 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517
|
/*
* SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2014 ownCloud, Inc.
* SPDX-License-Identifier: CC0-1.0
*
* 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 <sqlite3.h>
#include "common/syncjournaldb.h"
#include "common/syncjournalfilerecord.h"
#include "logger.h"
using namespace OCC;
class TestSyncJournalDB : public QObject
{
Q_OBJECT
QTemporaryDir _tempDir;
public:
TestSyncJournalDB()
: _db((_tempDir.path() + "/sync.db"))
{
QVERIFY(_tempDir.isValid());
}
qint64 dropMsecs(QDateTime time)
{
return Utility::qDateTimeToTime_t(time);
}
private slots:
void initTestCase()
{
OCC::Logger::instance()->setLogFlush(true);
OCC::Logger::instance()->setLogDebug(true);
QStandardPaths::setTestModeEnabled(true);
}
void cleanupTestCase()
{
const QString file = _db.databaseFilePath();
QFile::remove(file);
}
void testFileRecord()
{
SyncJournalFileRecord record;
QVERIFY(_db.getFileRecord(QByteArrayLiteral("nonexistent"), &record));
QVERIFY(!record.isValid());
record._path = "foo";
// Use a value that exceeds uint32 and isn't representable by the
// signed int being cast to uint64 either (like uint64::max would be)
record._inode = std::numeric_limits<quint32>::max() + 12ull;
record._modtime = dropMsecs(QDateTime::currentDateTime());
record._type = ItemTypeDirectory;
record._etag = "789789";
record._fileId = "abcd";
record._remotePerm = RemotePermissions::fromDbValue("RW");
record._fileSize = 213089055;
record._checksumHeader = "MD5:mychecksum";
QVERIFY(_db.setFileRecord(record));
SyncJournalFileRecord storedRecord;
QVERIFY(_db.getFileRecord(QByteArrayLiteral("foo"), &storedRecord));
QVERIFY(storedRecord == record);
// Update checksum
record._checksumHeader = "Adler32:newchecksum";
QVERIFY(_db.updateFileRecordChecksum("foo", "newchecksum", "Adler32"));
QVERIFY(_db.getFileRecord(QByteArrayLiteral("foo"), &storedRecord));
QVERIFY(storedRecord == record);
// Update metadata
record._modtime = dropMsecs(QDateTime::currentDateTime().addDays(1));
// try a value that only fits uint64, not int64
record._inode = std::numeric_limits<quint64>::max() - std::numeric_limits<quint32>::max() - 1;
record._type = ItemTypeFile;
record._etag = "789FFF";
record._fileId = "efg";
record._remotePerm = RemotePermissions::fromDbValue("NV");
record._fileSize = 289055;
QVERIFY(_db.setFileRecord(record));
QVERIFY(_db.getFileRecord(QByteArrayLiteral("foo"), &storedRecord));
QVERIFY(storedRecord == record);
QVERIFY(_db.deleteFileRecord("foo"));
QVERIFY(_db.getFileRecord(QByteArrayLiteral("foo"), &record));
QVERIFY(!record.isValid());
}
void testFolderQuota()
{
const auto bigFolderRecord = QByteArray("bigfolder");
SyncJournalFileRecord record;
record._path = bigFolderRecord;
record._inode = std::numeric_limits<quint32>::max() + 12ull;
record._modtime = dropMsecs(QDateTime::currentDateTime());
record._type = ItemTypeDirectory;
record._etag = "123123";
record._fileId = "abcd";
record._fileSize = 213089999;
QVERIFY(_db.setFileRecord(record));
SyncJournalFileRecord storedRecord;
QVERIFY(_db.getFileRecord(bigFolderRecord, &storedRecord));
QVERIFY(storedRecord == record);
// default values
QCOMPARE(storedRecord._folderQuota.bytesAvailable, -1);
QCOMPARE(storedRecord._folderQuota.bytesUsed, -1);
record._folderQuota.bytesUsed = 100;
record._folderQuota.bytesAvailable = 5000;
QVERIFY(_db.setFileRecord(record));
QVERIFY(_db.getFileRecord(bigFolderRecord, &storedRecord));
QVERIFY(storedRecord == record);
QCOMPARE(storedRecord._folderQuota.bytesAvailable, 5000);
QCOMPARE(storedRecord._folderQuota.bytesUsed, 100);
}
void testFolderMigration() {
const auto quotaBytesUsed = QStringLiteral("quotaBytesUsed");
const auto quotaBytesAvailable = QStringLiteral("quotaBytesAvailable");
const auto metadata = QStringLiteral("metadata");
const auto columnsBeforeRemoval = _db.tableColumns(metadata.toLatin1());
QCOMPARE_GT(columnsBeforeRemoval.indexOf(quotaBytesUsed.toLatin1()), -1);
QCOMPARE_GT(columnsBeforeRemoval.indexOf(quotaBytesAvailable.toLatin1()), -1);
QVERIFY(_db.removeColumn(quotaBytesAvailable));
QVERIFY(_db.removeColumn(quotaBytesUsed));
QVERIFY(_db.updateMetadataTableStructure());
const auto columnsAfterConnect = _db.tableColumns(metadata.toLatin1());
QVERIFY(columnsAfterConnect.indexOf(quotaBytesUsed.toLatin1()) > -1);
QVERIFY(columnsAfterConnect.indexOf(quotaBytesAvailable.toLatin1()) > -1);
QVERIFY(_db.hasDefaultValue(quotaBytesUsed));
QVERIFY(_db.hasDefaultValue(quotaBytesAvailable));
}
void testFileRecordChecksum()
{
// Try with and without a checksum
{
SyncJournalFileRecord record;
record._path = "foo-checksum";
record._remotePerm = RemotePermissions::fromDbValue(" ");
record._checksumHeader = "MD5:mychecksum";
record._modtime = Utility::qDateTimeToTime_t(QDateTime::currentDateTimeUtc());
QVERIFY(_db.setFileRecord(record));
SyncJournalFileRecord storedRecord;
QVERIFY(_db.getFileRecord(QByteArrayLiteral("foo-checksum"), &storedRecord));
QVERIFY(storedRecord._path == record._path);
QVERIFY(storedRecord._remotePerm == record._remotePerm);
QVERIFY(storedRecord._checksumHeader == record._checksumHeader);
// Attention: compare time_t types here, as QDateTime seem to maintain
// milliseconds internally, which disappear in sqlite. Go for full seconds here.
QVERIFY(storedRecord._modtime == record._modtime);
QVERIFY(storedRecord == record);
}
{
SyncJournalFileRecord record;
record._path = "foo-nochecksum";
record._remotePerm = RemotePermissions::fromDbValue("RW");
record._modtime = Utility::qDateTimeToTime_t(QDateTime::currentDateTimeUtc());
QVERIFY(_db.setFileRecord(record));
SyncJournalFileRecord storedRecord;
QVERIFY(_db.getFileRecord(QByteArrayLiteral("foo-nochecksum"), &storedRecord));
QVERIFY(storedRecord == record);
}
}
void testDownloadInfo()
{
using Info = SyncJournalDb::DownloadInfo;
Info record = _db.getDownloadInfo("nonexistent");
QVERIFY(!record._valid);
record._errorCount = 5;
record._etag = "ABCDEF";
record._valid = true;
record._tmpfile = "/tmp/foo";
_db.setDownloadInfo("foo", record);
Info storedRecord = _db.getDownloadInfo("foo");
QVERIFY(storedRecord == record);
_db.setDownloadInfo("foo", Info());
Info wipedRecord = _db.getDownloadInfo("foo");
QVERIFY(!wipedRecord._valid);
}
void testUploadInfo()
{
using Info = SyncJournalDb::UploadInfo;
Info record = _db.getUploadInfo("nonexistent");
QVERIFY(!record._valid);
record._errorCount = 5;
record._chunkUploadV1 = 12;
record._transferid = 812974891;
record._size = 12894789147;
record._modtime = dropMsecs(QDateTime::currentDateTime());
record._valid = true;
_db.setUploadInfo("foo", record);
Info storedRecord = _db.getUploadInfo("foo");
QVERIFY(storedRecord == record);
_db.setUploadInfo("foo", Info());
Info wipedRecord = _db.getUploadInfo("foo");
QVERIFY(!wipedRecord._valid);
}
void testNumericId()
{
SyncJournalFileRecord record;
// Typical 8-digit padded id
record._fileId = "00000001abcd";
QCOMPARE(record.numericFileId(), QByteArray("00000001"));
// When the numeric id overflows the 8-digit boundary
record._fileId = "123456789ocidblaabcd";
QCOMPARE(record.numericFileId(), QByteArray("123456789"));
}
void testConflictRecord()
{
ConflictRecord record;
record.path = "abc";
record.baseFileId = "def";
record.baseModtime = 1234;
record.baseEtag = "ghi";
QVERIFY(!_db.conflictRecord(record.path).isValid());
_db.setConflictRecord(record);
auto newRecord = _db.conflictRecord(record.path);
QVERIFY(newRecord.isValid());
QCOMPARE(newRecord.path, record.path);
QCOMPARE(newRecord.baseFileId, record.baseFileId);
QCOMPARE(newRecord.baseModtime, record.baseModtime);
QCOMPARE(newRecord.baseEtag, record.baseEtag);
_db.deleteConflictRecord(record.path);
QVERIFY(!_db.conflictRecord(record.path).isValid());
}
void testAvoidReadFromDbOnNextSync()
{
auto invalidEtag = QByteArray("_invalid_");
auto initialEtag = QByteArray("etag");
auto makeEntry = [&](const QByteArray &path, ItemType type) {
SyncJournalFileRecord record;
record._modtime = QDateTime::currentSecsSinceEpoch();
record._path = path;
record._type = type;
record._etag = initialEtag;
record._remotePerm = RemotePermissions::fromDbValue("RW");
QVERIFY(_db.setFileRecord(record));
};
auto getEtag = [&](const QByteArray &path) {
SyncJournalFileRecord record;
[[maybe_unused]] const auto result = _db.getFileRecord(path, &record);
return record._etag;
};
makeEntry("foodir", ItemTypeDirectory);
makeEntry("otherdir", ItemTypeDirectory);
makeEntry("foo%", ItemTypeDirectory); // wildcards don't apply
makeEntry("foodi_", ItemTypeDirectory); // wildcards don't apply
makeEntry("foodir/file", ItemTypeFile);
makeEntry("foodir/subdir", ItemTypeDirectory);
makeEntry("foodir/subdir/file", ItemTypeFile);
makeEntry("foodir/otherdir", ItemTypeDirectory);
makeEntry("fo", ItemTypeDirectory); // prefix, but does not match
makeEntry("foodir/sub", ItemTypeDirectory); // prefix, but does not match
makeEntry("foodir/subdir/subsubdir", ItemTypeDirectory);
makeEntry("foodir/subdir/subsubdir/file", ItemTypeFile);
makeEntry("foodir/subdir/otherdir", ItemTypeDirectory);
_db.schedulePathForRemoteDiscovery(QByteArray("foodir/subdir"));
// Direct effects of parent directories being set to _invalid_
QCOMPARE(getEtag("foodir"), invalidEtag);
QCOMPARE(getEtag("foodir/subdir"), invalidEtag);
QCOMPARE(getEtag("foodir/subdir/subsubdir"), initialEtag);
QCOMPARE(getEtag("foodir/file"), initialEtag);
QCOMPARE(getEtag("foodir/subdir/file"), initialEtag);
QCOMPARE(getEtag("foodir/subdir/subsubdir/file"), initialEtag);
QCOMPARE(getEtag("fo"), initialEtag);
QCOMPARE(getEtag("foo%"), initialEtag);
QCOMPARE(getEtag("foodi_"), initialEtag);
QCOMPARE(getEtag("otherdir"), initialEtag);
QCOMPARE(getEtag("foodir/otherdir"), initialEtag);
QCOMPARE(getEtag("foodir/sub"), initialEtag);
QCOMPARE(getEtag("foodir/subdir/otherdir"), initialEtag);
// Indirect effects: setFileRecord() calls filter etags
initialEtag = "etag2";
makeEntry("foodir", ItemTypeDirectory);
QCOMPARE(getEtag("foodir"), invalidEtag);
makeEntry("foodir/subdir", ItemTypeDirectory);
QCOMPARE(getEtag("foodir/subdir"), invalidEtag);
makeEntry("foodir/subdir/subsubdir", ItemTypeDirectory);
QCOMPARE(getEtag("foodir/subdir/subsubdir"), initialEtag);
makeEntry("fo", ItemTypeDirectory);
QCOMPARE(getEtag("fo"), initialEtag);
makeEntry("foodir/sub", ItemTypeDirectory);
QCOMPARE(getEtag("foodir/sub"), initialEtag);
}
void testRecursiveDelete()
{
auto makeEntry = [&](const QByteArray &path) {
SyncJournalFileRecord record;
record._path = path;
record._remotePerm = RemotePermissions::fromDbValue("RW");
record._modtime = QDateTime::currentSecsSinceEpoch();
QVERIFY(_db.setFileRecord(record));
};
QByteArrayList elements;
elements
<< "foo"
<< "foo/file"
<< "bar"
<< "moo"
<< "moo/file"
<< "foo%bar"
<< "foo bla bar/file"
<< "fo_"
<< "fo_/file";
for (const auto& elem : std::as_const(elements)) {
makeEntry(elem);
}
auto checkElements = [&]() {
bool ok = true;
for (const auto& elem : std::as_const(elements)) {
SyncJournalFileRecord record;
if (!_db.getFileRecord(elem, &record) || !record.isValid()) {
qWarning() << "Missing record: " << elem;
ok = false;
}
}
return ok;
};
QVERIFY(_db.deleteFileRecord("moo", true));
elements.removeAll("moo");
elements.removeAll("moo/file");
QVERIFY(checkElements());
QVERIFY(_db.deleteFileRecord("fo_", true));
elements.removeAll("fo_");
elements.removeAll("fo_/file");
QVERIFY(checkElements());
QVERIFY(_db.deleteFileRecord("foo%bar", true));
elements.removeAll("foo%bar");
QVERIFY(checkElements());
}
void testPinState()
{
auto make = [&](const QByteArray &path, PinState state) {
_db.internalPinStates().setForPath(path, state);
auto pinState = _db.internalPinStates().rawForPath(path);
QVERIFY(pinState);
QCOMPARE(*pinState, state);
};
auto get = [&](const QByteArray &path) -> PinState {
auto state = _db.internalPinStates().effectiveForPath(path);
if (!state) {
QTest::qFail("couldn't read pin state", __FILE__, __LINE__);
return PinState::Inherited;
}
return *state;
};
auto getRecursive = [&](const QByteArray &path) -> PinState {
auto state = _db.internalPinStates().effectiveForPathRecursive(path);
if (!state) {
QTest::qFail("couldn't read pin state", __FILE__, __LINE__);
return PinState::Inherited;
}
return *state;
};
auto getRaw = [&](const QByteArray &path) -> PinState {
auto state = _db.internalPinStates().rawForPath(path);
if (!state) {
QTest::qFail("couldn't read pin state", __FILE__, __LINE__);
return PinState::Inherited;
}
return *state;
};
_db.internalPinStates().wipeForPathAndBelow("");
auto list = _db.internalPinStates().rawList();
QCOMPARE(list->size(), 0);
// Make a thrice-nested setup
make("", PinState::AlwaysLocal);
make("local", PinState::AlwaysLocal);
make("online", PinState::OnlineOnly);
make("inherit", PinState::Inherited);
for (auto base : {"local/", "online/", "inherit/"}) {
make(QByteArray(base) + "inherit", PinState::Inherited);
make(QByteArray(base) + "local", PinState::AlwaysLocal);
make(QByteArray(base) + "online", PinState::OnlineOnly);
for (auto base2 : {"local/", "online/", "inherit/"}) {
make(QByteArray(base) + base2 + "inherit", PinState::Inherited);
make(QByteArray(base) + base2 + "local", PinState::AlwaysLocal);
make(QByteArray(base) + base2 + "online", PinState::OnlineOnly);
}
}
list = _db.internalPinStates().rawList();
QCOMPARE(list->size(), 4 + 9 + 27);
// Baseline direct checks (the fallback for unset root pinstate is AlwaysLocal)
QCOMPARE(get(""), PinState::AlwaysLocal);
QCOMPARE(get("local"), PinState::AlwaysLocal);
QCOMPARE(get("online"), PinState::OnlineOnly);
QCOMPARE(get("inherit"), PinState::AlwaysLocal);
QCOMPARE(get("nonexistent"), PinState::AlwaysLocal);
QCOMPARE(get("online/local"), PinState::AlwaysLocal);
QCOMPARE(get("local/online"), PinState::OnlineOnly);
QCOMPARE(get("inherit/local"), PinState::AlwaysLocal);
QCOMPARE(get("inherit/online"), PinState::OnlineOnly);
QCOMPARE(get("inherit/inherit"), PinState::AlwaysLocal);
QCOMPARE(get("inherit/nonexistent"), PinState::AlwaysLocal);
// Inheriting checks, level 1
QCOMPARE(get("local/inherit"), PinState::AlwaysLocal);
QCOMPARE(get("local/nonexistent"), PinState::AlwaysLocal);
QCOMPARE(get("online/inherit"), PinState::OnlineOnly);
QCOMPARE(get("online/nonexistent"), PinState::OnlineOnly);
// Inheriting checks, level 2
QCOMPARE(get("local/inherit/inherit"), PinState::AlwaysLocal);
QCOMPARE(get("local/local/inherit"), PinState::AlwaysLocal);
QCOMPARE(get("local/local/nonexistent"), PinState::AlwaysLocal);
QCOMPARE(get("local/online/inherit"), PinState::OnlineOnly);
QCOMPARE(get("local/online/nonexistent"), PinState::OnlineOnly);
QCOMPARE(get("online/inherit/inherit"), PinState::OnlineOnly);
QCOMPARE(get("online/local/inherit"), PinState::AlwaysLocal);
QCOMPARE(get("online/local/nonexistent"), PinState::AlwaysLocal);
QCOMPARE(get("online/online/inherit"), PinState::OnlineOnly);
QCOMPARE(get("online/online/nonexistent"), PinState::OnlineOnly);
// Spot check the recursive variant
QCOMPARE(getRecursive(""), PinState::Inherited);
QCOMPARE(getRecursive("local"), PinState::Inherited);
QCOMPARE(getRecursive("online"), PinState::Inherited);
QCOMPARE(getRecursive("inherit"), PinState::Inherited);
QCOMPARE(getRecursive("online/local"), PinState::Inherited);
QCOMPARE(getRecursive("online/local/inherit"), PinState::AlwaysLocal);
QCOMPARE(getRecursive("inherit/inherit/inherit"), PinState::AlwaysLocal);
QCOMPARE(getRecursive("inherit/online/inherit"), PinState::OnlineOnly);
QCOMPARE(getRecursive("inherit/online/local"), PinState::AlwaysLocal);
make("local/local/local/local", PinState::AlwaysLocal);
QCOMPARE(getRecursive("local/local/local"), PinState::AlwaysLocal);
QCOMPARE(getRecursive("local/local/local/local"), PinState::AlwaysLocal);
// Check changing the root pin state
make("", PinState::OnlineOnly);
QCOMPARE(get("local"), PinState::AlwaysLocal);
QCOMPARE(get("online"), PinState::OnlineOnly);
QCOMPARE(get("inherit"), PinState::OnlineOnly);
QCOMPARE(get("nonexistent"), PinState::OnlineOnly);
make("", PinState::AlwaysLocal);
QCOMPARE(get("local"), PinState::AlwaysLocal);
QCOMPARE(get("online"), PinState::OnlineOnly);
QCOMPARE(get("inherit"), PinState::AlwaysLocal);
QCOMPARE(get("nonexistent"), PinState::AlwaysLocal);
// Wiping
QCOMPARE(getRaw("local/local"), PinState::AlwaysLocal);
_db.internalPinStates().wipeForPathAndBelow("local/local");
QCOMPARE(getRaw("local"), PinState::AlwaysLocal);
QCOMPARE(getRaw("local/local"), PinState::Inherited);
QCOMPARE(getRaw("local/local/local"), PinState::Inherited);
QCOMPARE(getRaw("local/local/online"), PinState::Inherited);
list = _db.internalPinStates().rawList();
QCOMPARE(list->size(), 4 + 9 + 27 - 4);
// Wiping everything
_db.internalPinStates().wipeForPathAndBelow("");
QCOMPARE(getRaw(""), PinState::Inherited);
QCOMPARE(getRaw("local"), PinState::Inherited);
QCOMPARE(getRaw("online"), PinState::Inherited);
list = _db.internalPinStates().rawList();
QCOMPARE(list->size(), 0);
}
private:
SyncJournalDb _db;
};
QTEST_APPLESS_MAIN(TestSyncJournalDB)
#include "testsyncjournaldb.moc"
|