Panoptes 1.0.0
Endpoint Detection and Response
Loading...
Searching...
No Matches
Test_PanoLog.cpp
Go to the documentation of this file.
1#include "gtest/gtest.h"
2#include "pano_log.h"
3#include <filesystem>
4#include <fstream>
5
6namespace fs = std::filesystem;
7
8// A fresh empty folder for one test, removed afterwards
9class LogFolder : public ::testing::Test {
10protected:
11 void SetUp() override {
12 m_folder = fs::temp_directory_path() / ("Test_PanoLog_" + std::to_string(GetCurrentProcessId()) +
13 "_" + ::testing::UnitTest::GetInstance()->current_test_info()->name());
14 fs::remove_all(m_folder);
15 fs::create_directories(m_folder);
16 }
17 void TearDown() override {
18 fs::remove_all(m_folder);
19 }
20 void Touch(const wchar_t* name) {
21 std::ofstream(m_folder / name).put('x');
22 }
23 DWORD Newest() {
24 return NewestLogFileNumber(m_folder.wstring() + L"\\", L"pano", L".log");
25 }
26 fs::path m_folder;
27};
28
29TEST_F(LogFolder, EmptyFolderStartsAtZero) {
30 EXPECT_EQ(Newest(), 0u);
31}
32
33TEST_F(LogFolder, MissingFolderStartsAtZero) {
34 EXPECT_EQ(NewestLogFileNumber(m_folder.wstring() + L"\\missing\\", L"pano", L".log"), 0u);
35}
36
37TEST_F(LogFolder, OnlyTheFirstFile) {
38 Touch(L"pano.log");
39 EXPECT_EQ(Newest(), 0u);
40}
41
42TEST_F(LogFolder, HighestNumberNotLastAlphabetically) {
43 Touch(L"pano.log");
44 Touch(L"pano1.log");
45 Touch(L"pano2.log");
46 Touch(L"pano10.log");
47 EXPECT_EQ(Newest(), 10u);
48}
49
50TEST_F(LogFolder, OtherNamesAreIgnored) {
51 Touch(L"pano3.log");
52 Touch(L"pano99.log.bak");
53 Touch(L"panoX7.log");
54 Touch(L"pano.old.log");
55 Touch(L"detection50.log");
56 Touch(L"pano12345678901.log"); // too many digits for a file number
57 EXPECT_EQ(Newest(), 3u);
58}
59
60TEST_F(LogFolder, CaseInsensitive) {
61 Touch(L"PANO4.LOG");
62 EXPECT_EQ(Newest(), 4u);
63}
TEST_F(LogFolder, EmptyFolderStartsAtZero)
DWORD Newest()
void SetUp() override
void Touch(const wchar_t *name)
void TearDown() override
fs::path m_folder
DWORD NewestLogFileNumber(const std::wstring &folder, const std::wstring &base, const std::wstring &ext)
The number of the newest log file in a folder: 0 for base.ext (or when there is none),...
Definition pano_log.cpp:33