mirror of
https://github.com/mudita/MuditaOS.git
synced 2026-01-01 18:39:03 -05:00
Fixes and optimizations in logger: * fixed possible buffer overflow when logging logs over line buffer size; * reduced max log line length to 2048; * moved pubsetbuf before file opening; * log file stream buffer created once in logger ctor; * updatet UTs; * additional minor cleanup.
192 lines
7.1 KiB
C++
192 lines
7.1 KiB
C++
// Copyright (c) 2017-2023, Mudita Sp. z.o.o. All rights reserved.
|
|
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md
|
|
|
|
#include <catch2/catch.hpp>
|
|
|
|
#include "RandomStringGenerator.hpp"
|
|
#include "LoggerBuffer.hpp"
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
using namespace std;
|
|
|
|
void TestBuffer(const LoggerBuffer &buffer, size_t capacity, size_t numOfMsgs)
|
|
{
|
|
const bool isEmpty = buffer.isEmpty();
|
|
REQUIRE((numOfMsgs == 0 ? isEmpty : !isEmpty));
|
|
const bool isFull = buffer.isFull();
|
|
REQUIRE((capacity > 0 && capacity == numOfMsgs ? isFull : !isFull));
|
|
REQUIRE(buffer.getCapacity() == capacity);
|
|
REQUIRE(buffer.getSize() == numOfMsgs);
|
|
}
|
|
|
|
size_t GetNumOfBytes(vector<string>::const_iterator startIt, vector<string>::const_iterator endIt)
|
|
{
|
|
size_t numOfBytes = 0;
|
|
for (; startIt != endIt; ++startIt) {
|
|
numOfBytes += startIt->length();
|
|
}
|
|
return numOfBytes;
|
|
}
|
|
|
|
TEST_CASE("LoggerBuffer tests")
|
|
{
|
|
const size_t capacity = 100;
|
|
LoggerBuffer buffer(capacity);
|
|
|
|
RandomStringGenerator randomStringGenerator;
|
|
|
|
auto putMsgFunc = [&](const auto &msg) { buffer.put(msg); };
|
|
auto getMsgFunc = [&](const auto &originalMsg) {
|
|
const auto msg = buffer.get();
|
|
REQUIRE(msg.has_value());
|
|
REQUIRE(msg.value() == originalMsg);
|
|
};
|
|
auto putAllMsgsFunc = [&](const vector<string> &msgs) { for_each(msgs.begin(), msgs.end(), putMsgFunc); };
|
|
auto getAllMsgsFunc = [&](const vector<string> &msgs) { for_each(msgs.begin(), msgs.end(), getMsgFunc); };
|
|
auto checkLostBytes = [&](size_t numOfBytes, const string &originalMsg) {
|
|
const auto msg = buffer.get();
|
|
REQUIRE(msg.has_value());
|
|
REQUIRE(msg.value().find(originalMsg) != string::npos);
|
|
REQUIRE(msg.value().find(to_string(numOfBytes)) != string::npos);
|
|
REQUIRE(msg.value().find(LoggerBuffer::lostBytesMessage) != string::npos);
|
|
};
|
|
|
|
SECTION("calling get on empty buffer should return false")
|
|
{
|
|
const auto msg = buffer.get();
|
|
REQUIRE(!msg.has_value());
|
|
TestBuffer(buffer, capacity, 0);
|
|
}
|
|
|
|
SECTION("after putting one msg in buffer, get should return this msg")
|
|
{
|
|
const auto originalMsg = randomStringGenerator.getRandomString();
|
|
buffer.put(originalMsg);
|
|
TestBuffer(buffer, capacity, 1);
|
|
const auto msg = buffer.get();
|
|
REQUIRE(msg.has_value());
|
|
REQUIRE(msg.value() == originalMsg);
|
|
TestBuffer(buffer, capacity, 0);
|
|
}
|
|
|
|
SECTION("after filling whole buffer with msgs, calling get repeatedly should return all these msgs back")
|
|
{
|
|
const auto msgs = randomStringGenerator.createRandomStringVector(capacity);
|
|
putAllMsgsFunc(msgs);
|
|
TestBuffer(buffer, capacity, msgs.size());
|
|
getAllMsgsFunc(msgs);
|
|
TestBuffer(buffer, capacity, 0);
|
|
}
|
|
|
|
SECTION("buffer should be empty after resetting it")
|
|
{
|
|
const auto msgs = randomStringGenerator.createRandomStringVector(capacity);
|
|
putAllMsgsFunc(msgs);
|
|
TestBuffer(buffer, capacity, msgs.size());
|
|
buffer.reset();
|
|
TestBuffer(buffer, capacity, 0);
|
|
}
|
|
|
|
SECTION("when more msgs are put into buffer than its capacity, only last msgs should be returned with get and "
|
|
"first msg should contain lost bytes message")
|
|
{
|
|
const size_t numOfMsgsAboveCapacity = 13;
|
|
const auto msgs = randomStringGenerator.createRandomStringVector(capacity + numOfMsgsAboveCapacity);
|
|
putAllMsgsFunc(msgs);
|
|
auto firstLostMsgIt = msgs.begin();
|
|
auto firstMsgInBufferIt = firstLostMsgIt + numOfMsgsAboveCapacity;
|
|
const auto numOfLostBytes = GetNumOfBytes(firstLostMsgIt, firstMsgInBufferIt);
|
|
checkLostBytes(numOfLostBytes, *firstMsgInBufferIt);
|
|
for_each(firstMsgInBufferIt + 1, msgs.end(), getMsgFunc);
|
|
TestBuffer(buffer, capacity, 0);
|
|
}
|
|
|
|
SECTION("each get should reduce number of msgs in buffer")
|
|
{
|
|
size_t numOfMsgsInBuffer = capacity;
|
|
const auto msgs = randomStringGenerator.createRandomStringVector(capacity);
|
|
|
|
auto getStartIt = msgs.begin();
|
|
|
|
putAllMsgsFunc(msgs);
|
|
TestBuffer(buffer, capacity, capacity);
|
|
|
|
size_t numOfMsgsToGet = 15;
|
|
for_each(getStartIt, getStartIt + numOfMsgsToGet, getMsgFunc);
|
|
numOfMsgsInBuffer -= numOfMsgsToGet;
|
|
TestBuffer(buffer, capacity, numOfMsgsInBuffer);
|
|
getStartIt += numOfMsgsToGet;
|
|
|
|
numOfMsgsToGet = 34;
|
|
for_each(getStartIt, getStartIt + numOfMsgsToGet, getMsgFunc);
|
|
numOfMsgsInBuffer -= numOfMsgsToGet;
|
|
TestBuffer(buffer, capacity, numOfMsgsInBuffer);
|
|
getStartIt += numOfMsgsToGet;
|
|
|
|
for_each(getStartIt, msgs.end(), getMsgFunc);
|
|
TestBuffer(buffer, capacity, 0);
|
|
}
|
|
|
|
SECTION("put get put get")
|
|
{
|
|
const auto msgs = randomStringGenerator.createRandomStringVector(capacity);
|
|
|
|
auto putStartIt = msgs.begin();
|
|
auto getStartIt = msgs.begin();
|
|
|
|
size_t numOfMsgsToPut = 37;
|
|
for_each(putStartIt, putStartIt + numOfMsgsToPut, putMsgFunc);
|
|
putStartIt += numOfMsgsToPut;
|
|
size_t numOfMsgsInBuffer = numOfMsgsToPut;
|
|
TestBuffer(buffer, capacity, numOfMsgsInBuffer);
|
|
|
|
size_t numOfMsgsToGet = 25;
|
|
for_each(getStartIt, getStartIt + numOfMsgsToGet, getMsgFunc);
|
|
getStartIt += numOfMsgsToGet;
|
|
numOfMsgsInBuffer -= numOfMsgsToGet;
|
|
TestBuffer(buffer, capacity, numOfMsgsInBuffer);
|
|
|
|
numOfMsgsToPut = msgs.end() - putStartIt;
|
|
for_each(putStartIt, msgs.end(), putMsgFunc);
|
|
numOfMsgsInBuffer += numOfMsgsToPut;
|
|
TestBuffer(buffer, capacity, numOfMsgsInBuffer);
|
|
|
|
for_each(getStartIt, msgs.end(), getMsgFunc);
|
|
TestBuffer(buffer, capacity, 0);
|
|
}
|
|
|
|
SECTION("put get put get - with buffer overflow")
|
|
{
|
|
const size_t numOfMsgsAboveCapacity = 43;
|
|
const auto msgs = randomStringGenerator.createRandomStringVector(capacity + numOfMsgsAboveCapacity);
|
|
|
|
auto putStartIt = msgs.begin();
|
|
auto getStartIt = msgs.begin();
|
|
|
|
size_t numOfMsgsToPut = 77;
|
|
for_each(putStartIt, putStartIt + numOfMsgsToPut, putMsgFunc);
|
|
putStartIt += numOfMsgsToPut;
|
|
size_t numOfMsgsInBuffer = numOfMsgsToPut;
|
|
TestBuffer(buffer, capacity, numOfMsgsInBuffer);
|
|
|
|
size_t numOfMsgsToGet = 15;
|
|
for_each(getStartIt, getStartIt + numOfMsgsToGet, getMsgFunc);
|
|
getStartIt += numOfMsgsToGet;
|
|
numOfMsgsInBuffer -= numOfMsgsToGet;
|
|
TestBuffer(buffer, capacity, numOfMsgsInBuffer);
|
|
|
|
numOfMsgsToPut = msgs.end() - putStartIt; // put rest of msgs - more than buffer can hold
|
|
for_each(putStartIt, msgs.end(), putMsgFunc);
|
|
numOfMsgsInBuffer = capacity;
|
|
TestBuffer(buffer, capacity, numOfMsgsInBuffer);
|
|
|
|
auto firstLostMsgIt = getStartIt;
|
|
auto firstMsgInBufferIt = firstLostMsgIt + numOfMsgsAboveCapacity - numOfMsgsToGet;
|
|
const auto numOfLostBytes = GetNumOfBytes(firstLostMsgIt, firstMsgInBufferIt);
|
|
checkLostBytes(numOfLostBytes, *firstMsgInBufferIt);
|
|
for_each(firstMsgInBufferIt + 1, msgs.end(), getMsgFunc);
|
|
TestBuffer(buffer, capacity, 0);
|
|
}
|
|
}
|