diff --git a/libguh-core/guhcore.cpp b/libguh-core/guhcore.cpp index 967ca75d..38eabfd8 100644 --- a/libguh-core/guhcore.cpp +++ b/libguh-core/guhcore.cpp @@ -424,7 +424,7 @@ void GuhCore::init() { m_timeManager = new TimeManager(QTimeZone::systemTimeZoneId(), this); qCDebug(dcApplication) << "Creating Log Engine"; - m_logger = new LogEngine(this); + m_logger = new LogEngine(GuhSettings::logPath(), this); qCDebug(dcApplication) << "Creating Device Manager (locale:" << m_configuration->locale() << ")"; m_deviceManager = new DeviceManager(m_configuration->locale(), this); diff --git a/libguh-core/logging/logengine.cpp b/libguh-core/logging/logengine.cpp index 2c81008b..a0e2a9bf 100644 --- a/libguh-core/logging/logengine.cpp +++ b/libguh-core/logging/logengine.cpp @@ -132,11 +132,11 @@ namespace guhserver { /*! Constructs the log engine with the given \a parent. */ -LogEngine::LogEngine(QObject *parent): +LogEngine::LogEngine(const QString &logPath, QObject *parent): QObject(parent) { m_db = QSqlDatabase::addDatabase("QSQLITE", "logs"); - m_db.setDatabaseName(GuhSettings::logPath()); + m_db.setDatabaseName(logPath); m_dbMaxSize = 50000; m_overflow = 100; diff --git a/libguh-core/logging/logengine.h b/libguh-core/logging/logengine.h index dc02f18d..38441fc3 100644 --- a/libguh-core/logging/logengine.h +++ b/libguh-core/logging/logengine.h @@ -38,7 +38,7 @@ class LogEngine: public QObject { Q_OBJECT public: - LogEngine(QObject *parent = 0); + LogEngine(const QString &logPath = GuhSettings::logPath(), QObject *parent = 0); ~LogEngine(); QList logEntries(const LogFilter &filter = LogFilter()) const; diff --git a/tests/auto/auto.pro b/tests/auto/auto.pro index 23e92a51..41d560bd 100644 --- a/tests/auto/auto.pro +++ b/tests/auto/auto.pro @@ -16,6 +16,7 @@ SUBDIRS = versioning \ websocketserver \ logging \ loggingdirect \ + loggingloading \ restlogging \ #coap \ # temporary removed until fixed configurations \ diff --git a/tests/auto/logging/testlogging.cpp b/tests/auto/logging/testlogging.cpp index 4d59f2b1..b0ca6ce6 100644 --- a/tests/auto/logging/testlogging.cpp +++ b/tests/auto/logging/testlogging.cpp @@ -64,6 +64,7 @@ private slots: void testHouseKeeping(); + // this has to be the last test void removeDevice(); }; @@ -326,8 +327,8 @@ void TestLogging::actionLog() clientSpy.wait(200); - QVariant notification = checkNotification(clientSpy, "Logging.LogEntryAdded"); - QVERIFY(!notification.isNull()); + loggEntryAddedVariants = checkNotifications(clientSpy, "Logging.LogEntryAdded"); + QVERIFY(!loggEntryAddedVariants.isEmpty()); // get this logentry with filter params.clear(); diff --git a/tests/auto/loggingloading/guhd-broken.sqlite b/tests/auto/loggingloading/guhd-broken.sqlite new file mode 100755 index 00000000..43329a10 Binary files /dev/null and b/tests/auto/loggingloading/guhd-broken.sqlite differ diff --git a/tests/auto/loggingloading/guhd-v2.sqlite b/tests/auto/loggingloading/guhd-v2.sqlite new file mode 100755 index 00000000..c23e0f2b Binary files /dev/null and b/tests/auto/loggingloading/guhd-v2.sqlite differ diff --git a/tests/auto/loggingloading/loggingloading.pro b/tests/auto/loggingloading/loggingloading.pro new file mode 100644 index 00000000..aaef3966 --- /dev/null +++ b/tests/auto/loggingloading/loggingloading.pro @@ -0,0 +1,6 @@ +include(../../../guh.pri) +include(../autotests.pri) + +RESOURCES += loggingloading.qrc +TARGET = loggingloading +SOURCES += testloggingloading.cpp diff --git a/tests/auto/loggingloading/loggingloading.qrc b/tests/auto/loggingloading/loggingloading.qrc new file mode 100644 index 00000000..c38aebda --- /dev/null +++ b/tests/auto/loggingloading/loggingloading.qrc @@ -0,0 +1,6 @@ + + + guhd-v2.sqlite + guhd-broken.sqlite + + diff --git a/tests/auto/loggingloading/testloggingloading.cpp b/tests/auto/loggingloading/testloggingloading.cpp new file mode 100644 index 00000000..d842831d --- /dev/null +++ b/tests/auto/loggingloading/testloggingloading.cpp @@ -0,0 +1,139 @@ +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + * * + * Copyright (C) 2017 Simon Stürz * + * * + * This file is part of guh. * + * * + * Guh is free software: you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation, version 2 of the License. * + * * + * Guh is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with guh. If not, see . * + * * + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +#include + +#include "logging/logengine.h" +#include "logging/logvaluetool.h" + +using namespace guhserver; + +class TestLoggingLoading: public QObject +{ + Q_OBJECT +public: + TestLoggingLoading(QObject* parent = nullptr); + +private slots: + void testLogMigration(); + void testLogfileRotation(); + + void databaseSerializationTest_data(); + void databaseSerializationTest(); +}; + +TestLoggingLoading::TestLoggingLoading(QObject *parent): QObject(parent) +{ + +} + +void TestLoggingLoading::testLogMigration() +{ + Q_INIT_RESOURCE(loggingloading); + + // Create LogEngine with log db from resource file + QString temporaryDbName = GuhSettings::settingsPath() + "/guhd-v2.sqlite"; + + if (QFile::exists(temporaryDbName)) + QVERIFY(QFile(temporaryDbName).remove()); + + QVERIFY(QFile::copy(":/guhd-v2.sqlite", temporaryDbName)); + QVERIFY(QFile::setPermissions(temporaryDbName, QFile::ReadOwner | QFile::WriteOwner | QFile::ReadGroup | QFile::ReadOther)); + + LogEngine *logEngine = new LogEngine(temporaryDbName, this); + // Check there is no rotated logfile + QVERIFY(!QFile::exists(temporaryDbName + ".1")); + + delete logEngine; + + QVERIFY(QFile(temporaryDbName).remove()); +} + +void TestLoggingLoading::testLogfileRotation() +{ + Q_INIT_RESOURCE(loggingloading); + + // Create LogEngine with log db from resource file + QString temporaryDbName = GuhSettings::settingsPath() + "/guhd-broken.sqlite"; + QString rotatedDbName = GuhSettings::settingsPath() + "/guhd-broken.sqlite.1"; + + // Remove the files if there are some left + if (QFile::exists(temporaryDbName)) + QVERIFY(QFile(temporaryDbName).remove()); + + if (QFile::exists(rotatedDbName)) + QVERIFY(QFile(rotatedDbName).remove()); + + QVERIFY(QFile::copy(":/guhd-broken.sqlite", temporaryDbName)); + QVERIFY(QFile::setPermissions(temporaryDbName, QFile::ReadOwner | QFile::WriteOwner | QFile::ReadGroup | QFile::ReadOther)); + + QVERIFY(!QFile::exists(rotatedDbName)); + LogEngine *logEngine = new LogEngine(temporaryDbName, this); + QVERIFY(QFile::exists(rotatedDbName)); + + delete logEngine; + + QVERIFY(QFile(temporaryDbName).remove()); + QVERIFY(QFile(rotatedDbName).remove()); +} + +void TestLoggingLoading::databaseSerializationTest_data() +{ + QUuid uuid = QUuid("3782732b-61b4-48e8-8d6d-b5205159d7cd"); + + QVariantMap variantMap; + variantMap.insert("string", "value"); + variantMap.insert("int", 5); + variantMap.insert("double", 3.14); + variantMap.insert("uuid", uuid); + + QVariantList variantList; + variantList.append(variantMap); + variantList.append("String"); + variantList.append(3.14); + variantList.append(uuid); + + QTest::addColumn("value"); + + QTest::newRow("QString") << QVariant(QString("Hello")); + QTest::newRow("Integer") << QVariant((int)2); + QTest::newRow("Double") << QVariant((double)2.34); + QTest::newRow("Float") << QVariant((float)2.34); + QTest::newRow("QColor") << QVariant(QColor(0,255,128)); + QTest::newRow("QByteArray") << QVariant(QByteArray("\nthisisatestarray\n")); + QTest::newRow("QUuid") << QVariant(uuid); + QTest::newRow("QVariantMap") << QVariant(variantMap); + QTest::newRow("QVariantList") << QVariant(variantList); +} + +void TestLoggingLoading::databaseSerializationTest() +{ + QFETCH(QVariant, value); + + QString serializedValue = LogValueTool::serializeValue(value); + QVariant deserializedValue = LogValueTool::deserializeValue(serializedValue); + + qDebug() << "Stored:" << value; + qDebug() << "Loaded:" << deserializedValue; + QCOMPARE(deserializedValue, value); +} + +#include "testloggingloading.moc" +QTEST_MAIN(TestLoggingLoading)