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";
|
|
|
|
record._inode = 1234;
|
|
|
|
record._modtime = dropMsecs(QDateTime::currentDateTime());
|
|
|
|
record._type = 5;
|
|
|
|
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._inode = 12345;
|
|
|
|
record._modtime = dropMsecs(QDateTime::currentDateTime().addDays(1));
|
|
|
|
record._type = 7;
|
|
|
|
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()
|
|
|
|
{
|
|
|
|
typedef SyncJournalDb::DownloadInfo Info;
|
|
|
|
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()
|
|
|
|
{
|
|
|
|
typedef SyncJournalDb::UploadInfo Info;
|
|
|
|
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"));
|
|
|
|
}
|
|
|
|
|
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"
|