Skip to content

Commit

Permalink
[release/2.1rc]] Cherry-pick release 2.0 fixes into 2.1.0rc (#166)
Browse files Browse the repository at this point in the history
* identify the ProviderGuid failing with 1168

* Onboard repo to Secure Development Tools Azure DevOps (#152)

* resolve c:// monitor fix merge conflicts

* Fix 30 seconds delay issue (#156)

* reduce file monitor wait time

* resolve event log failure fix merge conflicts

---------

Co-authored-by: Bob Sira <sbobfitz2@gmail.com>
Co-authored-by: Tina Murimi <christine.murimi@gmail.com>
Co-authored-by: Charity Kathure <ckathure@microsoft.com>
  • Loading branch information
4 people authored Dec 7, 2023
1 parent 03169f9 commit 98be0dd
Show file tree
Hide file tree
Showing 27 changed files with 1,027 additions and 259 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ bld/

# Visual Studio 2015/2017 cache/options directory
.vs/
.vscode/
# Uncomment if you have tasks that create the project's static files in wwwroot
#wwwroot/

Expand Down
169 changes: 169 additions & 0 deletions LogMonitor/LogMonitorTests/ConfigFileParserTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,7 @@ namespace LogMonitorTests
Assert::AreEqual(directory.c_str(), sourceFile->Directory.c_str());
Assert::AreEqual(L"", sourceFile->Filter.c_str());
Assert::AreEqual(false, sourceFile->IncludeSubdirectories);
Assert::AreEqual(300.0, sourceFile->WaitInSeconds);
}
}

Expand Down Expand Up @@ -1336,6 +1337,65 @@ namespace LogMonitorTests
}
}

TEST_METHOD(TestRootDirectoryConfigurations)
{

const std::wstring directory = L"C:\\";
bool includeSubdirectories = false;

std::wstring configFileStr;
std::wstring configFileStrFormat =
L"{ \
\"LogConfig\": { \
\"sources\": [ \
{\
\"type\": \"File\",\
\"directory\": \"%s\",\
\"includeSubdirectories\": %s\
}\
]\
}\
}";

// Valid: Root dir and includeSubdirectories = false
{
configFileStr = Utility::FormatString(
configFileStrFormat.c_str(),
Utility::ReplaceAll(directory, L"\\", L"\\\\").c_str(),
includeSubdirectories ? L"true" : L"false");

JsonFileParser jsonParser(configFileStr);
LoggerSettings settings;

bool success = ReadConfigFile(jsonParser, settings);
Assert::IsTrue(success);

std::wstring output = RecoverOuput();
Assert::AreEqual(L"", output.c_str());
}

// Invalid: Root dir and includeSubdirectories = true
{
includeSubdirectories = true;
configFileStr = Utility::FormatString(
configFileStrFormat.c_str(),
Utility::ReplaceAll(directory, L"\\", L"\\\\").c_str(),
includeSubdirectories ? L"true" : L"false");

fflush(stdout);
ZeroMemory(bigOutBuf, sizeof(bigOutBuf));

JsonFileParser jsonParser(configFileStr);
LoggerSettings settings;

bool success = ReadConfigFile(jsonParser, settings);
Assert::IsTrue(success);

std::wstring output = RecoverOuput();
Assert::IsTrue(output.find(L"WARNING") != std::wstring::npos);
}
}

///
/// Check that invalid ETW sources are not returned by ReadConfigFile.
///
Expand Down Expand Up @@ -1543,5 +1603,114 @@ namespace LogMonitorTests
Assert::AreEqual(succcess, true);
}

TEST_METHOD(TestWaitInSeconds){
// Test WaitInSeconds input as value
TestWaitInSecondsValues(L"242", false);

// Test WaitInSeconds input as string
TestWaitInSecondsValues(L"359", true);

// Test WaitInSeconds input is Infinity
TestWaitInSecondsValues(L"INFINITY", true);
}

TEST_METHOD(TestInvalidWaitInSeconds) {
std::wstring directory = L"C:\\LogMonitor\\logs";
TestInvalidWaitInSecondsValues(L"-10", false);
TestInvalidWaitInSecondsValues(L"-Inf", true);
}

private:
void TestWaitInSecondsValues(std::wstring waitInSeconds, bool asString = false) {
std::wstring directory = L"C:\\LogMonitor\\logs";
std::wstring configFileStr = GetConfigFileStrFormat(directory, waitInSeconds, asString);

JsonFileParser jsonParser(configFileStr);
LoggerSettings settings;

bool success = ReadConfigFile(jsonParser, settings);

//
// The config string was valid
//
Assert::IsTrue(success);

//
// The source Event Log is valid
//
Assert::AreEqual((size_t)1, settings.Sources.size());
Assert::AreEqual((int)LogSourceType::File, (int)settings.Sources[0]->Type);

std::shared_ptr<SourceFile> sourceFile = std::reinterpret_pointer_cast<SourceFile>(settings.Sources[0]);

if (isinf(std::stod(waitInSeconds))) {
Assert::IsTrue(isinf(sourceFile->WaitInSeconds));
}
else {
double precision = 1e-6;
Assert::AreEqual(std::stod(waitInSeconds), sourceFile->WaitInSeconds, precision);
}
}

void TestInvalidWaitInSecondsValues(std::wstring waitInSeconds, bool asString = false) {
std::wstring directory = L"C:\\LogMonitor\\logs";
std::wstring configFileStr = GetConfigFileStrFormat(directory, waitInSeconds, asString);

JsonFileParser jsonParser(configFileStr);
LoggerSettings settings;

bool success = ReadConfigFile(jsonParser, settings);

std::wstring output = RecoverOuput();

Assert::IsTrue(success);
Assert::IsTrue(output.find(L"WARNING") != std::wstring::npos);

Assert::IsTrue(success);
Assert::IsTrue(output.find(L"WARNING") != std::wstring::npos);
}

std::wstring GetConfigFileStrFormat(std::wstring directory, std::wstring waitInSeconds, bool asString) {
std::wstring configFileStrFormat;
if (asString) {
configFileStrFormat =
L"{ \
\"LogConfig\": { \
\"sources\": [ \
{\
\"type\": \"File\",\
\"directory\": \"%s\",\
\"waitInSeconds\": \"%s\"\
}\
]\
}\
}";

return Utility::FormatString(
configFileStrFormat.c_str(),
Utility::ReplaceAll(directory, L"\\", L"\\\\").c_str(),
waitInSeconds.c_str()
);
}
else {
configFileStrFormat =
L"{ \
\"LogConfig\": { \
\"sources\": [ \
{\
\"type\": \"File\",\
\"directory\": \"%s\",\
\"waitInSeconds\": %f\
}\
]\
}\
}";
return Utility::FormatString(
configFileStrFormat.c_str(),
Utility::ReplaceAll(directory, L"\\", L"\\\\").c_str(),
std::stod(waitInSeconds)
);
}
}
};
}
23 changes: 14 additions & 9 deletions LogMonitor/LogMonitorTests/LogFileMonitorTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ namespace LogMonitorTests
fflush(stdout);
ZeroMemory(bigOutBuf, sizeof(bigOutBuf));

std::shared_ptr<LogFileMonitor> logfileMon = std::make_shared<LogFileMonitor>(sourceFile.Directory, sourceFile.Filter, sourceFile.IncludeSubdirectories, L"json", L"");
std::shared_ptr<LogFileMonitor> logfileMon = std::make_shared<LogFileMonitor>(sourceFile.Directory, sourceFile.Filter, sourceFile.IncludeSubdirectories, sourceFile.WaitInSeconds, L"json", L"");
Sleep(WAIT_TIME_LOGFILEMONITOR_START);

//
Expand Down Expand Up @@ -224,7 +224,7 @@ namespace LogMonitorTests
fflush(stdout);
ZeroMemory(bigOutBuf, sizeof(bigOutBuf));

std::shared_ptr<LogFileMonitor> logfileMon = std::make_shared<LogFileMonitor>(sourceFile.Directory, sourceFile.Filter, sourceFile.IncludeSubdirectories, L"json", L"");
std::shared_ptr<LogFileMonitor> logfileMon = std::make_shared<LogFileMonitor>(sourceFile.Directory, sourceFile.Filter, sourceFile.IncludeSubdirectories, sourceFile.WaitInSeconds, L"json", L"");
Sleep(WAIT_TIME_LOGFILEMONITOR_START);

//
Expand All @@ -241,7 +241,7 @@ namespace LogMonitorTests
// Check that LogFileMonitor started successfully.
//
output = RecoverOuput();
Assert::AreEqual(L"", output.c_str());
Assert::IsTrue(output.find(L"INFO") != std::wstring::npos);
{
std::wstring filename = sourceFile.Directory + L"\\testfile.txt";
std::string content = "Hello World!";
Expand Down Expand Up @@ -288,11 +288,12 @@ namespace LogMonitorTests
sourceFile.Directory = tempDirectory;
sourceFile.Filter = L"*.log";
sourceFile.IncludeSubdirectories = true;
sourceFile.WaitInSeconds = 10;

fflush(stdout);
ZeroMemory(bigOutBuf, sizeof(bigOutBuf));

std::shared_ptr<LogFileMonitor> logfileMon = std::make_shared<LogFileMonitor>(sourceFile.Directory, sourceFile.Filter, sourceFile.IncludeSubdirectories, L"json", L"");
std::shared_ptr<LogFileMonitor> logfileMon = std::make_shared<LogFileMonitor>(sourceFile.Directory, sourceFile.Filter, sourceFile.IncludeSubdirectories, sourceFile.WaitInSeconds, L"json", L"");
Sleep(WAIT_TIME_LOGFILEMONITOR_START);

//
Expand Down Expand Up @@ -395,7 +396,7 @@ namespace LogMonitorTests
fflush(stdout);
ZeroMemory(bigOutBuf, sizeof(bigOutBuf));

std::shared_ptr<LogFileMonitor> logfileMon = std::make_shared<LogFileMonitor>(sourceFile.Directory, sourceFile.Filter, sourceFile.IncludeSubdirectories, L"json", L"");
std::shared_ptr<LogFileMonitor> logfileMon = std::make_shared<LogFileMonitor>(sourceFile.Directory, sourceFile.Filter, sourceFile.IncludeSubdirectories, sourceFile.WaitInSeconds, L"json", L"");
Sleep(WAIT_TIME_LOGFILEMONITOR_START);

//
Expand Down Expand Up @@ -566,11 +567,12 @@ namespace LogMonitorTests
sourceFile.Directory = tempDirectory;
sourceFile.Filter = L"*.log";
sourceFile.IncludeSubdirectories = true;
sourceFile.WaitInSeconds = 10;

fflush(stdout);
ZeroMemory(bigOutBuf, sizeof(bigOutBuf));

std::shared_ptr<LogFileMonitor> logfileMon = std::make_shared<LogFileMonitor>(sourceFile.Directory, sourceFile.Filter, sourceFile.IncludeSubdirectories, L"json", L"");
std::shared_ptr<LogFileMonitor> logfileMon = std::make_shared<LogFileMonitor>(sourceFile.Directory, sourceFile.Filter, sourceFile.IncludeSubdirectories, sourceFile.WaitInSeconds, L"json", L"");
Sleep(WAIT_TIME_LOGFILEMONITOR_START);

//
Expand Down Expand Up @@ -671,11 +673,12 @@ namespace LogMonitorTests
sourceFile.Directory = tempDirectory;
sourceFile.Filter = L"*.log";
sourceFile.IncludeSubdirectories = true;
sourceFile.WaitInSeconds = 10;

fflush(stdout);
ZeroMemory(bigOutBuf, sizeof(bigOutBuf));

std::shared_ptr<LogFileMonitor> logfileMon = std::make_shared<LogFileMonitor>(sourceFile.Directory, sourceFile.Filter, sourceFile.IncludeSubdirectories, L"json", L"");
std::shared_ptr<LogFileMonitor> logfileMon = std::make_shared<LogFileMonitor>(sourceFile.Directory, sourceFile.Filter, sourceFile.IncludeSubdirectories, sourceFile.WaitInSeconds, L"json", L"");
Sleep(WAIT_TIME_LOGFILEMONITOR_START);

//
Expand Down Expand Up @@ -790,11 +793,12 @@ namespace LogMonitorTests
sourceFile.Directory = tempDirectory;
sourceFile.Filter = L"*.log";
sourceFile.IncludeSubdirectories = true;
sourceFile.WaitInSeconds = 10;

fflush(stdout);
ZeroMemory(bigOutBuf, sizeof(bigOutBuf));

std::shared_ptr<LogFileMonitor> logfileMon = std::make_shared<LogFileMonitor>(sourceFile.Directory, sourceFile.Filter, sourceFile.IncludeSubdirectories, L"json", L"");
std::shared_ptr<LogFileMonitor> logfileMon = std::make_shared<LogFileMonitor>(sourceFile.Directory, sourceFile.Filter, sourceFile.IncludeSubdirectories, sourceFile.WaitInSeconds, L"json", L"");
Sleep(WAIT_TIME_LOGFILEMONITOR_START);

//
Expand Down Expand Up @@ -980,11 +984,12 @@ namespace LogMonitorTests
sourceFile.Directory = tempDirectory;
sourceFile.Filter = L"*.log";
sourceFile.IncludeSubdirectories = true;
sourceFile.WaitInSeconds = 10;

fflush(stdout);
ZeroMemory(bigOutBuf, sizeof(bigOutBuf));

std::shared_ptr<LogFileMonitor> logfileMon = std::make_shared<LogFileMonitor>(sourceFile.Directory, sourceFile.Filter, sourceFile.IncludeSubdirectories, L"json", L"");
std::shared_ptr<LogFileMonitor> logfileMon = std::make_shared<LogFileMonitor>(sourceFile.Directory, sourceFile.Filter, sourceFile.IncludeSubdirectories, sourceFile.WaitInSeconds, L"json", L"");
Sleep(WAIT_TIME_LOGFILEMONITOR_START);

//
Expand Down
2 changes: 1 addition & 1 deletion LogMonitor/LogMonitorTests/LogMonitorTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include "../src/LogMonitor/EtwMonitor.cpp"
#include "../src/LogMonitor/EventMonitor.cpp"
#include "../src/LogMonitor/JsonFileParser.cpp"
#include "../src/LogMonitor/FileMonitor/Utilities.cpp"
#include "../src/LogMonitor/FileMonitor/FileMonitorUtilities.cpp"
#include "../src/LogMonitor/LogFileMonitor.cpp"
#include "../src/LogMonitor/ProcessMonitor.cpp"
#include "../src/LogMonitor/Utility.cpp"
Expand Down
2 changes: 1 addition & 1 deletion LogMonitor/LogMonitorTests/pch.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
#include "../src/LogMonitor/LogWriter.h"
#include "../src/LogMonitor/EtwMonitor.h"
#include "../src/LogMonitor/EventMonitor.h"
#include "../src/LogMonitor/FileMonitor/Utilities.h"
#include "../src/LogMonitor/FileMonitor/FileMonitorUtilities.h"
#include "../src/LogMonitor/LogFileMonitor.h"
#include "../src/LogMonitor/ProcessMonitor.h"
#include "Utility.h"
Expand Down
Loading

0 comments on commit 98be0dd

Please sign in to comment.