Improve syslog parsing; Implement basic main loop.

This commit is contained in:
Marko Zivanovic
2015-09-02 17:01:01 +02:00
parent 13d5266c37
commit a7ee585a9e
11 changed files with 107 additions and 30 deletions
+1 -3
View File
@@ -32,9 +32,7 @@ const std::string Facility::readFromStream(std::istream& src) {
if (c == '.') {
break;
} else {
if (c != ' ' && c != '\t') {
ret.push_back(c);
}
ret.push_back(c);
}
}
return ret;
+3 -8
View File
@@ -28,16 +28,11 @@ SOFTWARE.
#include <memory>
#include <boost/noncopyable.hpp>
class Reader : boost::noncopyable {
class SyslogMessage;
class Reader : private boost::noncopyable {
public:
virtual ~Reader() {
};
virtual std::shared_ptr<SyslogMessage> nextMessage() = 0;
private:
};
#endif /* READER_H */
+2 -2
View File
@@ -30,10 +30,10 @@ SOFTWARE.
class Severity {
public:
Severity(const char* src) : Severity(std::string(src)) {
Severity(const std::string& src) : _value(readFromString(src)) {
};
Severity(const std::string& src) : _value(readFromString(src)) {
Severity(const char* src) : Severity(std::string(src)) {
};
Severity(std::istream& source) : Severity(readFromStream(source)) {
+6 -3
View File
@@ -23,8 +23,11 @@ SOFTWARE.
*/
#include "SyslogBulkUploader.h"
#include "Reader.h"
#include "Writer.h"
SyslogBulkUploader::SyslogBulkUploader() {
void SyslogBulkUploader::run() {
while (auto msg = _reader.nextMessage()) {
_writer.sendMessage(msg);
}
}
+10 -2
View File
@@ -27,11 +27,19 @@ SOFTWARE.
#include <boost/noncopyable.hpp>
class Reader;
class Writer;
class SyslogBulkUploader : boost::noncopyable {
public:
SyslogBulkUploader();
private:
SyslogBulkUploader(Reader& reader, Writer& writer) : _reader(reader), _writer(writer) {
};
void run();
private:
Reader& _reader;
Writer& _writer;
};
#endif /* SYSLOGBULKUPLOADER_H */
+3 -3
View File
@@ -25,14 +25,13 @@ SOFTWARE.
#include "SyslogMessage.h"
void skipWhitespace(std::istream& src) {
bool whitespace = true;
while (src && whitespace) {
while (src) {
std::istream::char_type c = src.peek();
if (c == ' ' || c == '\t') {
src.get();
continue;
} else {
whitespace = false;
break;
}
}
}
@@ -55,6 +54,7 @@ const boost::posix_time::ptime SyslogMessage::readTimestamp(std::istream& src) {
};
const Facility SyslogMessage::readFacility(std::istream& src) {
skipWhitespace(src);
return Facility(src);
};
-4
View File
@@ -45,22 +45,18 @@ public:
};
const Facility facility() const {
return _facility;
}
const std::string message() const {
return _message;
}
const Severity severity() const {
return _severity;
}
const std::string source() const {
return _source;
}
+37
View File
@@ -0,0 +1,37 @@
/*
The MIT License (MIT)
Copyright (c) 2015 Marko Živanović
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#ifndef WRITER_H
#define WRITER_H
#include "SyslogMessage.h"
class Writer : private boost::noncopyable {
public:
virtual void sendMessage(std::shared_ptr<SyslogMessage>) = 0;
};
#endif /* WRITER_H */
+1
View File
@@ -9,6 +9,7 @@ add_executable(SyslogBulkUploaderTests SyslogBulkUploaderTests.cpp)
target_link_libraries(SyslogBulkUploaderTests
slbu-lib
${Boost_UNIT_TEST_FRAMEWORK_LIBRARY}
${Boost_DATE_TIME_LIBRARY}
)
add_executable(SyslogMessageTests SyslogMessageTests.cpp)
+41 -2
View File
@@ -23,9 +23,48 @@ SOFTWARE.
*/
#include "../src/SyslogBulkUploader.h"
#include "../src/Reader.h"
#include "../src/Writer.h"
#define BOOST_TEST_MODULE SyslogBulkUploaderTests
#include <boost/test/unit_test.hpp>
#include <memory>
BOOST_AUTO_TEST_CASE(fakeTest) {
BOOST_CHECK(true);
class MockReader : public Reader {
public:
virtual std::shared_ptr<SyslogMessage> nextMessage() {
if (_pos >= _messages.size()) {
return std::shared_ptr<SyslogMessage>();
} else {
std::stringstream ss(_messages[_pos++]);
return std::shared_ptr<SyslogMessage>(new SyslogMessage(dynamic_cast<std::istream&> (ss)));
}
}
private:
std::vector<std::string> _messages = {
"2015-09-02 13:33:11 Local4.Critical 192.168.0.1 Kiwi_Syslog_Server %ASA-2-106007: Deny inbound UDP from 1.2.3.4/22084 to 4.3.2.1/53 due to DNS Query",
"2015-09-02 13:33:11 Local4.Critical 192.168.0.1 Kiwi_Syslog_Server %ASA-2-106007: Deny inbound UDP from 1.2.3.4/22084 to 4.3.2.1/53 due to DNS Query",
"2015-09-02 13:33:11 Local4.Critical 192.168.0.1 Kiwi_Syslog_Server %ASA-2-106007: Deny inbound UDP from 1.2.3.4/22084 to 4.3.2.1/53 due to DNS Query"
};
size_t _pos;
};
class MockWriter : public Writer {
public:
virtual void sendMessage(std::shared_ptr<SyslogMessage> msg) {
_messages.push_back(msg);
};
std::vector<std::shared_ptr<SyslogMessage>> _messages;
};
BOOST_AUTO_TEST_CASE(test_run) {
MockReader r;
MockWriter w;
SyslogBulkUploader ul(r, w);
ul.run();
BOOST_CHECK_EQUAL(w._messages.size(), 3);
for (size_t i = 0; i < w._messages.size(); i++) {
std::cout << *(w._messages[i].get()) << std::endl;
}
}
+3 -3
View File
@@ -27,15 +27,15 @@ SOFTWARE.
#include <boost/test/unit_test.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
BOOST_AUTO_TEST_CASE(constructor_and_getters) {
std::stringstream source("2015-09-02 13:33:11 Local4.Critical 192.168.0.1 Kiwi_Syslog_Server %ASA-2-106007: Deny inbound UDP from 138.28.4.18/22084 to 9.28.3.28/53 due to DNS Query");
BOOST_AUTO_TEST_CASE(parsing) {
std::stringstream source("2015-09-02 13:33:11 Local4.Critical 192.168.0.1 Kiwi_Syslog_Server %ASA-2-106007: Deny inbound UDP from 1.2.3.4/22084 to 4.3.2.1/53 due to DNS Query");
boost::posix_time::ptime timestamp(boost::posix_time::time_from_string("2015-09-02 13:33:11"));
SyslogMessage m(source);
BOOST_CHECK_EQUAL(m.timestamp(), timestamp);
BOOST_CHECK_EQUAL(m.facility(), Facility("Local4"));
BOOST_CHECK_EQUAL(m.severity(), Severity("Critical"));
BOOST_CHECK_EQUAL(m.source(), "192.168.0.1");
BOOST_CHECK_EQUAL(m.message(), "Kiwi_Syslog_Server %ASA-2-106007: Deny inbound UDP from 138.28.4.18/22084 to 9.28.3.28/53 due to DNS Query");
BOOST_CHECK_EQUAL(m.message(), "Kiwi_Syslog_Server %ASA-2-106007: Deny inbound UDP from 1.2.3.4/22084 to 4.3.2.1/53 due to DNS Query");
}
BOOST_AUTO_TEST_CASE(invalid_params) {