2014-10-23 17:15:47 +04:00
|
|
|
/*
|
|
|
|
* 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>
|
|
|
|
|
2017-09-01 19:11:43 +03:00
|
|
|
#include "common/syncjournaldb.h"
|
|
|
|
#include "common/syncjournalfilerecord.h"
|
2014-10-23 17:15:47 +04:00
|
|
|
|
2014-11-10 00:36:49 +03:00
|
|
|
using namespace OCC;
|
2014-10-23 17:15:47 +04:00
|
|
|
|
|
|
|
class TestSyncJournalDB : public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
|
2017-07-03 15:08:10 +03:00
|
|
|
QTemporaryDir _tempDir;
|
2017-07-12 11:06:04 +03:00
|
|
|
|
2014-10-23 17:15:47 +04:00
|
|
|
public:
|
|
|
|
TestSyncJournalDB()
|
2017-07-12 11:06:04 +03:00
|
|
|
: _db((_tempDir.path() + "/sync.db"))
|
2014-10-23 17:15:47 +04:00
|
|
|
{
|
2017-07-03 15:08:10 +03:00
|
|
|
QVERIFY(_tempDir.isValid());
|
2014-10-23 17:15:47 +04:00
|
|
|
}
|
|
|
|
|
2017-09-14 16:50:13 +03:00
|
|
|
qint64 dropMsecs(QDateTime time)
|
2014-10-23 17:15:47 +04:00
|
|
|
{
|
2017-09-14 16:50:13 +03:00
|
|
|
return Utility::qDateTimeToTime_t(time);
|
2014-10-23 17:15:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
private slots:
|
|
|
|
|
|
|
|
void initTestCase()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void cleanupTestCase()
|
|
|
|
{
|
2016-07-17 22:10:07 +03:00
|
|
|
const QString file = _db.databaseFilePath();
|
|
|
|
QFile::remove(file);
|
2014-10-23 17:15:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void testFileRecord()
|
|
|
|
{
|
2017-09-13 20:02:38 +03:00
|
|
|
SyncJournalFileRecord record;
|
2017-09-14 16:50:13 +03:00
|
|
|
QVERIFY(_db.getFileRecord(QByteArrayLiteral("nonexistant"), &record));
|
2014-10-23 17:15:47 +04:00
|
|
|
QVERIFY(!record.isValid());
|
|
|
|
|
|
|
|
record._path = "foo";
|
2017-12-11 16:47:49 +03:00
|
|
|
// 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;
|
2014-10-23 17:15:47 +04:00
|
|
|
record._modtime = dropMsecs(QDateTime::currentDateTime());
|
2017-12-14 17:08:53 +03:00
|
|
|
record._type = ItemTypeDirectory;
|
2014-10-23 17:15:47 +04:00
|
|
|
record._etag = "789789";
|
|
|
|
record._fileId = "abcd";
|
2017-09-19 11:53:51 +03:00
|
|
|
record._remotePerm = RemotePermissions("RW");
|
2014-09-05 15:03:33 +04:00
|
|
|
record._fileSize = 213089055;
|
2017-06-14 13:14:46 +03:00
|
|
|
record._checksumHeader = "MD5:mychecksum";
|
2014-10-23 17:15:47 +04:00
|
|
|
QVERIFY(_db.setFileRecord(record));
|
|
|
|
|
2017-09-13 20:02:38 +03:00
|
|
|
SyncJournalFileRecord storedRecord;
|
2017-09-14 16:50:13 +03:00
|
|
|
QVERIFY(_db.getFileRecord(QByteArrayLiteral("foo"), &storedRecord));
|
2014-10-23 17:15:47 +04:00
|
|
|
QVERIFY(storedRecord == record);
|
|
|
|
|
2015-10-28 16:42:44 +03:00
|
|
|
// Update checksum
|
2017-06-14 13:14:46 +03:00
|
|
|
record._checksumHeader = "Adler32:newchecksum";
|
|
|
|
_db.updateFileRecordChecksum("foo", "newchecksum", "Adler32");
|
2017-09-14 16:50:13 +03:00
|
|
|
QVERIFY(_db.getFileRecord(QByteArrayLiteral("foo"), &storedRecord));
|
2015-10-28 16:42:44 +03:00
|
|
|
QVERIFY(storedRecord == record);
|
|
|
|
|
|
|
|
// Update metadata
|
|
|
|
record._modtime = dropMsecs(QDateTime::currentDateTime().addDays(1));
|
2017-12-11 16:47:49 +03:00
|
|
|
// try a value that only fits uint64, not int64
|
|
|
|
record._inode = std::numeric_limits<quint64>::max() - std::numeric_limits<quint32>::max() - 1;
|
2017-12-14 17:08:53 +03:00
|
|
|
record._type = ItemTypeFile;
|
2015-10-28 16:42:44 +03:00
|
|
|
record._etag = "789FFF";
|
|
|
|
record._fileId = "efg";
|
2017-09-19 11:53:51 +03:00
|
|
|
record._remotePerm = RemotePermissions("NV");
|
2015-10-28 16:42:44 +03:00
|
|
|
record._fileSize = 289055;
|
2015-11-10 17:05:00 +03:00
|
|
|
_db.setFileRecordMetadata(record);
|
2017-09-14 16:50:13 +03:00
|
|
|
QVERIFY(_db.getFileRecord(QByteArrayLiteral("foo"), &storedRecord));
|
2015-10-28 16:42:44 +03:00
|
|
|
QVERIFY(storedRecord == record);
|
|
|
|
|
2014-10-23 17:15:47 +04:00
|
|
|
QVERIFY(_db.deleteFileRecord("foo"));
|
2017-09-14 16:50:13 +03:00
|
|
|
QVERIFY(_db.getFileRecord(QByteArrayLiteral("foo"), &record));
|
2014-10-23 17:15:47 +04:00
|
|
|
QVERIFY(!record.isValid());
|
|
|
|
}
|
|
|
|
|
2015-10-28 13:00:03 +03:00
|
|
|
void testFileRecordChecksum()
|
|
|
|
{
|
|
|
|
// Try with and without a checksum
|
|
|
|
{
|
|
|
|
SyncJournalFileRecord record;
|
|
|
|
record._path = "foo-checksum";
|
2017-09-19 11:53:51 +03:00
|
|
|
record._remotePerm = RemotePermissions("RW");
|
2017-06-14 13:14:46 +03:00
|
|
|
record._checksumHeader = "MD5:mychecksum";
|
2017-09-14 16:50:13 +03:00
|
|
|
record._modtime = Utility::qDateTimeToTime_t(QDateTime::currentDateTimeUtc());
|
2015-10-28 13:00:03 +03:00
|
|
|
QVERIFY(_db.setFileRecord(record));
|
|
|
|
|
2017-09-13 20:02:38 +03:00
|
|
|
SyncJournalFileRecord storedRecord;
|
2017-09-14 16:50:13 +03:00
|
|
|
QVERIFY(_db.getFileRecord(QByteArrayLiteral("foo-checksum"), &storedRecord));
|
2015-11-25 23:13:50 +03:00
|
|
|
QVERIFY(storedRecord._path == record._path);
|
|
|
|
QVERIFY(storedRecord._remotePerm == record._remotePerm);
|
2017-06-14 13:14:46 +03:00
|
|
|
QVERIFY(storedRecord._checksumHeader == record._checksumHeader);
|
2015-11-25 23:13:50 +03:00
|
|
|
|
|
|
|
// qDebug()<< "OOOOO " << storedRecord._modtime.toTime_t() << record._modtime.toTime_t();
|
|
|
|
|
|
|
|
// Attention: compare time_t types here, as QDateTime seem to maintain
|
|
|
|
// milliseconds internally, which disappear in sqlite. Go for full seconds here.
|
2017-09-14 16:50:13 +03:00
|
|
|
QVERIFY(storedRecord._modtime == record._modtime);
|
2015-10-28 13:00:03 +03:00
|
|
|
QVERIFY(storedRecord == record);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
SyncJournalFileRecord record;
|
|
|
|
record._path = "foo-nochecksum";
|
2017-09-19 11:53:51 +03:00
|
|
|
record._remotePerm = RemotePermissions("RWN");
|
2017-09-14 16:50:13 +03:00
|
|
|
record._modtime = Utility::qDateTimeToTime_t(QDateTime::currentDateTimeUtc());
|
2015-11-26 00:16:29 +03:00
|
|
|
|
2015-10-28 13:00:03 +03:00
|
|
|
QVERIFY(_db.setFileRecord(record));
|
|
|
|
|
2017-09-13 20:02:38 +03:00
|
|
|
SyncJournalFileRecord storedRecord;
|
2017-09-14 16:50:13 +03:00
|
|
|
QVERIFY(_db.getFileRecord(QByteArrayLiteral("foo-nochecksum"), &storedRecord));
|
2015-10-28 13:00:03 +03:00
|
|
|
QVERIFY(storedRecord == record);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-23 17:15:47 +04:00
|
|
|
void testDownloadInfo()
|
|
|
|
{
|
2020-08-12 16:23:11 +03:00
|
|
|
using Info = SyncJournalDb::DownloadInfo;
|
2014-10-23 17:15:47 +04:00
|
|
|
Info record = _db.getDownloadInfo("nonexistant");
|
|
|
|
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()
|
|
|
|
{
|
2020-05-25 21:26:45 +03:00
|
|
|
using Info = SyncJournalDb::UploadInfo;
|
2014-10-23 17:15:47 +04:00
|
|
|
Info record = _db.getUploadInfo("nonexistant");
|
|
|
|
QVERIFY(!record._valid);
|
|
|
|
|
|
|
|
record._errorCount = 5;
|
|
|
|
record._chunk = 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);
|
|
|
|
}
|
|
|
|
|
2017-09-05 14:40:26 +03:00
|
|
|
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"));
|
|
|
|
}
|
|
|
|
|
2017-12-02 13:40:43 +03:00
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
2018-02-07 15:05:41 +03:00
|
|
|
void testAvoidReadFromDbOnNextSync()
|
|
|
|
{
|
|
|
|
auto invalidEtag = QByteArray("_invalid_");
|
|
|
|
auto initialEtag = QByteArray("etag");
|
2018-06-22 14:04:32 +03:00
|
|
|
auto makeEntry = [&](const QByteArray &path, ItemType type) {
|
2018-02-07 15:05:41 +03:00
|
|
|
SyncJournalFileRecord record;
|
|
|
|
record._path = path;
|
|
|
|
record._type = type;
|
|
|
|
record._etag = initialEtag;
|
|
|
|
_db.setFileRecord(record);
|
|
|
|
};
|
|
|
|
auto getEtag = [&](const QByteArray &path) {
|
|
|
|
SyncJournalFileRecord record;
|
|
|
|
_db.getFileRecord(path, &record);
|
|
|
|
return record._etag;
|
|
|
|
};
|
|
|
|
|
2018-06-22 14:04:32 +03:00
|
|
|
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);
|
2018-02-07 15:05:41 +03:00
|
|
|
|
|
|
|
_db.avoidReadFromDbOnNextSync(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";
|
|
|
|
|
2018-06-22 14:04:32 +03:00
|
|
|
makeEntry("foodir", ItemTypeDirectory);
|
2018-02-07 15:05:41 +03:00
|
|
|
QCOMPARE(getEtag("foodir"), invalidEtag);
|
2018-06-22 14:04:32 +03:00
|
|
|
makeEntry("foodir/subdir", ItemTypeDirectory);
|
2018-02-07 15:05:41 +03:00
|
|
|
QCOMPARE(getEtag("foodir/subdir"), invalidEtag);
|
2018-06-22 14:04:32 +03:00
|
|
|
makeEntry("foodir/subdir/subsubdir", ItemTypeDirectory);
|
2018-02-07 15:05:41 +03:00
|
|
|
QCOMPARE(getEtag("foodir/subdir/subsubdir"), initialEtag);
|
2018-06-22 14:04:32 +03:00
|
|
|
makeEntry("fo", ItemTypeDirectory);
|
2018-02-07 15:05:41 +03:00
|
|
|
QCOMPARE(getEtag("fo"), initialEtag);
|
2018-06-22 14:04:32 +03:00
|
|
|
makeEntry("foodir/sub", ItemTypeDirectory);
|
2018-02-07 15:05:41 +03:00
|
|
|
QCOMPARE(getEtag("foodir/sub"), initialEtag);
|
|
|
|
}
|
|
|
|
|
|
|
|
void testRecursiveDelete()
|
|
|
|
{
|
|
|
|
auto makeEntry = [&](const QByteArray &path) {
|
|
|
|
SyncJournalFileRecord record;
|
|
|
|
record._path = path;
|
|
|
|
_db.setFileRecord(record);
|
|
|
|
};
|
|
|
|
|
|
|
|
QByteArrayList elements;
|
|
|
|
elements
|
|
|
|
<< "foo"
|
|
|
|
<< "foo/file"
|
|
|
|
<< "bar"
|
|
|
|
<< "moo"
|
|
|
|
<< "moo/file"
|
|
|
|
<< "foo%bar"
|
|
|
|
<< "foo bla bar/file"
|
|
|
|
<< "fo_"
|
|
|
|
<< "fo_/file";
|
2019-10-07 00:10:26 +03:00
|
|
|
for (const auto& elem : elements)
|
2018-02-07 15:05:41 +03:00
|
|
|
makeEntry(elem);
|
|
|
|
|
|
|
|
auto checkElements = [&]() {
|
|
|
|
bool ok = true;
|
2019-10-07 00:10:26 +03:00
|
|
|
for (const auto& elem : elements) {
|
2018-02-07 15:05:41 +03:00
|
|
|
SyncJournalFileRecord record;
|
|
|
|
_db.getFileRecord(elem, &record);
|
|
|
|
if (!record.isValid()) {
|
|
|
|
qWarning() << "Missing record: " << elem;
|
|
|
|
ok = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ok;
|
|
|
|
};
|
|
|
|
|
|
|
|
_db.deleteFileRecord("moo", true);
|
|
|
|
elements.removeAll("moo");
|
|
|
|
elements.removeAll("moo/file");
|
|
|
|
QVERIFY(checkElements());
|
|
|
|
|
|
|
|
_db.deleteFileRecord("fo_", true);
|
|
|
|
elements.removeAll("fo_");
|
|
|
|
elements.removeAll("fo_/file");
|
|
|
|
QVERIFY(checkElements());
|
|
|
|
|
|
|
|
_db.deleteFileRecord("foo%bar", true);
|
|
|
|
elements.removeAll("foo%bar");
|
|
|
|
QVERIFY(checkElements());
|
|
|
|
}
|
|
|
|
|
2014-10-23 17:15:47 +04:00
|
|
|
private:
|
|
|
|
SyncJournalDb _db;
|
|
|
|
};
|
|
|
|
|
2016-05-11 02:17:16 +03:00
|
|
|
QTEST_APPLESS_MAIN(TestSyncJournalDB)
|
2016-03-30 18:58:15 +03:00
|
|
|
#include "testsyncjournaldb.moc"
|