From 9dc617efe6dd29b0bca5440519ab3e415f6067cb Mon Sep 17 00:00:00 2001 From: Nodir Temirkhodjaev Date: Fri, 25 Aug 2017 21:51:35 +0500 Subject: [PATCH] Add test for LogBuffer. --- src/tests/FortFirewallTest.pro | 2 +- src/tests/fortmanager/FortManager.pro | 7 ---- src/tests/fortmanager/test.cpp | 10 ------ src/tests/logbuffer/LogBuffer.pro | 11 ++++++ src/tests/logbuffer/test.cpp | 39 +++++++++++++++++++++ src/tests/{fortmanager => logbuffer}/test.h | 2 +- src/ui/FortFirewall.pro | 8 ++--- src/ui/firewallLog/logbuffer.cpp | 14 ++++---- src/ui/firewallLog/logbuffer.h | 7 ++-- src/ui/firewallLog/logentry.cpp | 9 +++-- src/ui/firewallLog/logentry.h | 4 ++- 11 files changed, 78 insertions(+), 35 deletions(-) delete mode 100644 src/tests/fortmanager/FortManager.pro delete mode 100644 src/tests/fortmanager/test.cpp create mode 100644 src/tests/logbuffer/LogBuffer.pro create mode 100644 src/tests/logbuffer/test.cpp rename src/tests/{fortmanager => logbuffer}/test.h (83%) diff --git a/src/tests/FortFirewallTest.pro b/src/tests/FortFirewallTest.pro index 140c6a7b..1963616f 100644 --- a/src/tests/FortFirewallTest.pro +++ b/src/tests/FortFirewallTest.pro @@ -1,4 +1,4 @@ TEMPLATE = subdirs SUBDIRS = \ - fortmanager + logbuffer diff --git a/src/tests/fortmanager/FortManager.pro b/src/tests/fortmanager/FortManager.pro deleted file mode 100644 index ad841b89..00000000 --- a/src/tests/fortmanager/FortManager.pro +++ /dev/null @@ -1,7 +0,0 @@ -include(../common/Test.pri) - -SOURCES += \ - $$UIPATH/fortmanager.cpp - -HEADERS += \ - $$UIPATH/fortmanager.h diff --git a/src/tests/fortmanager/test.cpp b/src/tests/fortmanager/test.cpp deleted file mode 100644 index e6271cd4..00000000 --- a/src/tests/fortmanager/test.cpp +++ /dev/null @@ -1,10 +0,0 @@ -#include "test.h" - -#include - -#include "fortmanager.h" - -void Test::dummy() -{ - QVERIFY(false); -} diff --git a/src/tests/logbuffer/LogBuffer.pro b/src/tests/logbuffer/LogBuffer.pro new file mode 100644 index 00000000..a48df739 --- /dev/null +++ b/src/tests/logbuffer/LogBuffer.pro @@ -0,0 +1,11 @@ +include(../common/Test.pri) + +SOURCES += \ + $$UIPATH/firewallLog/logbuffer.cpp \ + $$UIPATH/firewallLog/logentry.cpp \ + $$UIPATH/fortcommon.cpp + +HEADERS += \ + $$UIPATH/firewallLog/logbuffer.h \ + $$UIPATH/firewallLog/logentry.h \ + $$UIPATH/fortcommon.h diff --git a/src/tests/logbuffer/test.cpp b/src/tests/logbuffer/test.cpp new file mode 100644 index 00000000..4ed5019e --- /dev/null +++ b/src/tests/logbuffer/test.cpp @@ -0,0 +1,39 @@ +#include "test.h" + +#include + +#include "firewallLog/logbuffer.h" +#include "firewallLog/logentry.h" +#include "fortcommon.h" + +void Test::logWriteRead() +{ + const QString path("C:\\test\\"); + + const int pathSize = path.size(); + QCOMPARE(pathSize, 8); + + const int entrySize = FortCommon::logHeaderSize() + + pathSize * sizeof(wchar_t); + + LogBuffer buf(entrySize); + + const quint32 ip = 1, pid = 2; + LogEntry entry(ip, pid, path); + + const int testCount = 3; + + // Write + for (int n = testCount; --n >= 0; ) { + QCOMPARE(buf.write(entry), entrySize); + } + + // Read + for (int n = testCount; --n >= 0; ) { + QCOMPARE(buf.read(entry), entrySize); + QCOMPARE(entry.ip(), ip); + QCOMPARE(entry.pid(), pid); + QCOMPARE(entry.path(), path); + } + QCOMPARE(buf.read(entry), 0); +} diff --git a/src/tests/fortmanager/test.h b/src/tests/logbuffer/test.h similarity index 83% rename from src/tests/fortmanager/test.h rename to src/tests/logbuffer/test.h index 5ca83291..24392d8a 100644 --- a/src/tests/fortmanager/test.h +++ b/src/tests/logbuffer/test.h @@ -8,7 +8,7 @@ class Test : public QObject Q_OBJECT private slots: - void dummy(); + void logWriteRead(); }; #endif // TEST_H diff --git a/src/ui/FortFirewall.pro b/src/ui/FortFirewall.pro index f340f0ab..a2aa0a8a 100644 --- a/src/ui/FortFirewall.pro +++ b/src/ui/FortFirewall.pro @@ -9,14 +9,14 @@ TEMPLATE = app SOURCES += \ main.cpp \ - fortcommon.cpp \ firewallLog/logbuffer.cpp \ - firewallLog/logentry.cpp + firewallLog/logentry.cpp \ + fortcommon.cpp HEADERS += \ - fortcommon.h \ firewallLog/logbuffer.h \ - firewallLog/logentry.h + firewallLog/logentry.h \ + fortcommon.h QML_FILES += \ qml/*.qml diff --git a/src/ui/firewallLog/logbuffer.cpp b/src/ui/firewallLog/logbuffer.cpp index bfd97cfb..67cf7ddc 100644 --- a/src/ui/firewallLog/logbuffer.cpp +++ b/src/ui/firewallLog/logbuffer.cpp @@ -3,11 +3,11 @@ #include "fortcommon.h" #include "logentry.h" -LogBuffer::LogBuffer(QObject *parent) : +LogBuffer::LogBuffer(int bufferSize, QObject *parent) : QObject(parent), m_top(0), m_offset(0), - m_array(0, Qt::Uninitialized) + m_array(bufferSize, Qt::Uninitialized) { } @@ -20,7 +20,7 @@ void LogBuffer::prepareFor(int len) } } -void LogBuffer::write(const LogEntry &logEntry) +int LogBuffer::write(const LogEntry &logEntry) { const QString path = logEntry.path(); const int pathLen = path.size() * sizeof(wchar_t); @@ -38,12 +38,14 @@ void LogBuffer::write(const LogEntry &logEntry) } m_top += entrySize; + + return entrySize; } -bool LogBuffer::read(LogEntry &logEntry) +int LogBuffer::read(LogEntry &logEntry) { if (m_offset >= m_top) - return false; + return 0; const char *input = m_array.constData() + m_offset; @@ -64,5 +66,5 @@ bool LogBuffer::read(LogEntry &logEntry) const int entrySize = FortCommon::logSize(pathLen); m_offset += entrySize; - return true; + return entrySize; } diff --git a/src/ui/firewallLog/logbuffer.h b/src/ui/firewallLog/logbuffer.h index 2e00caad..d627854c 100644 --- a/src/ui/firewallLog/logbuffer.h +++ b/src/ui/firewallLog/logbuffer.h @@ -11,13 +11,14 @@ class LogBuffer : public QObject Q_OBJECT public: - explicit LogBuffer(QObject *parent = nullptr); + explicit LogBuffer(int bufferSize = 0, + QObject *parent = nullptr); signals: public slots: - void write(const LogEntry &logEntry); - bool read(LogEntry &logEntry); + int write(const LogEntry &logEntry); + int read(LogEntry &logEntry); private: void prepareFor(int len); diff --git a/src/ui/firewallLog/logentry.cpp b/src/ui/firewallLog/logentry.cpp index 4af017a6..3150e529 100644 --- a/src/ui/firewallLog/logentry.cpp +++ b/src/ui/firewallLog/logentry.cpp @@ -1,7 +1,12 @@ #include "logentry.h" -LogEntry::LogEntry(QObject *parent) : - QObject(parent) +LogEntry::LogEntry(quint32 ip, quint32 pid, + const QString &path, + QObject *parent) : + QObject(parent), + m_ip(ip), + m_pid(pid), + m_path(path) { } diff --git a/src/ui/firewallLog/logentry.h b/src/ui/firewallLog/logentry.h index aa328591..72ad2c2f 100644 --- a/src/ui/firewallLog/logentry.h +++ b/src/ui/firewallLog/logentry.h @@ -11,7 +11,9 @@ class LogEntry : public QObject Q_PROPERTY(QString path READ path WRITE setPath NOTIFY pathChanged) public: - explicit LogEntry(QObject *parent = nullptr); + explicit LogEntry(quint32 ip = 0, quint32 pid = 0, + const QString &path = QString(), + QObject *parent = nullptr); quint32 ip() const { return m_ip; } void setIp(quint32 ip);