Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Increase profiler test coverage #2027

Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace NewRelic { namespace Profiler { namespace Configuration { namespace Te
class MockTokenResolver : public SignatureParser::ITokenResolver
{
public:
MockTokenResolver(const std::wstring& typeString = L"MyNamespace.MyClass") : _typeString(typeString) {}
MockTokenResolver(const std::wstring& typeString = L"MyNamespace.MyClass") : _typeString(typeString), _typeGenericArgumentCount(0) {}

virtual std::wstring GetTypeStringsFromTypeDefOrRefOrSpecToken(uint32_t /*typeDefOrRefOrSPecToken*/) override
{
Expand Down Expand Up @@ -926,5 +926,49 @@ namespace NewRelic { namespace Profiler { namespace Configuration { namespace Te
Assert::IsFalse(instrumentationPoint == nullptr);
}

TEST_METHOD(does_not_match_when_assembly_is_mscorlib)
{
InstrumentationXmlSetPtr xmlSet(new InstrumentationXmlSet());
xmlSet->emplace(L"filename", L"\
<?xml version=\"1.0\" encoding=\"utf-8\"?>\
<extension>\
<instrumentation>\
<tracerFactory>\
<match assemblyName=\"mscorlib\" className=\"MyNamespace.MyClass\">\
<exactMethodMatcher methodName=\"MyMethod\"/>\
</match>\
</tracerFactory>\
</instrumentation>\
</extension>\
");
InstrumentationConfiguration instrumentation(xmlSet);
auto function = std::make_shared<MethodRewriter::Test::MockFunction>();
function->_assemblyName = _X("mscorlib");
auto instrumentationPoint = instrumentation.TryGetInstrumentationPoint(function);
Assert::IsTrue(instrumentationPoint == nullptr);
}

TEST_METHOD(does_not_match_when_assembly_is_SystemPrivateCoreLib)
{
InstrumentationXmlSetPtr xmlSet(new InstrumentationXmlSet());
xmlSet->emplace(L"filename", L"\
<?xml version=\"1.0\" encoding=\"utf-8\"?>\
<extension>\
<instrumentation>\
<tracerFactory>\
<match assemblyName=\"System.Private.CoreLib\" className=\"MyNamespace.MyClass\">\
<exactMethodMatcher methodName=\"MyMethod\"/>\
</match>\
</tracerFactory>\
</instrumentation>\
</extension>\
");
InstrumentationConfiguration instrumentation(xmlSet);
auto function = std::make_shared<MethodRewriter::Test::MockFunction>();
function->_assemblyName = _X("System.Private.CoreLib");
auto instrumentationPoint = instrumentation.TryGetInstrumentationPoint(function);
Assert::IsTrue(instrumentationPoint == nullptr);
}

};
}}}}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,26 @@ namespace NewRelic { namespace Profiler { namespace MethodRewriter{ namespace Te
RunTest(_X("true"), _X("true"), AgentCallStyle::Strategy::Reflection);
}

TEST_METHOD(AgentCallStrategy_EnvironmentVariable_0)
{
RunTest(_X("0"), _X("false"), AgentCallStyle::Strategy::InAgentCache);
}

TEST_METHOD(AgentCallStrategy_EnvironmentVariable_1)
{
RunTest(_X("1"), _X("false"), AgentCallStyle::Strategy::AppDomainCache);
}

TEST_METHOD(AgentCallStrategy_EnvironmentVariable_Empty)
{
RunTest(_X(""), _X("false"), AgentCallStyle::Strategy::InAgentCache);
}

TEST_METHOD(AgentCallStrategy_EnvironmentVariable_NotSet)
{
RunTestNoEnvironmentVariablesSet(AgentCallStyle::Strategy::InAgentCache);
}

TEST_METHOD(AgentCallStrategy_ToString_InAgentCache)
{
const xstring_t expectedValue = _X("In Agent Cache");
Expand All @@ -59,22 +79,19 @@ namespace NewRelic { namespace Profiler { namespace MethodRewriter{ namespace Te
void RunTest(const xstring_t& legacyCachingEnabled, const xstring_t& disableAppDomainCache, const AgentCallStyle::Strategy expectedStrategy)
{
auto systemCalls = std::make_shared<MockSystemCalls>();
systemCalls->EnvironmentVariableResult = [&legacyCachingEnabled, &disableAppDomainCache](const xstring_t& variableName)
{
if (variableName == _X("NEW_RELIC_ENABLE_LEGACY_CACHING"))
{
return std::make_unique<xstring_t>(legacyCachingEnabled);
}
else if (variableName == _X("NEW_RELIC_DISABLE_APPDOMAIN_CACHING"))
{
return std::make_unique<xstring_t>(disableAppDomainCache);
}
else
{
return std::make_unique<xstring_t>(_X(""));
}
};
systemCalls->SetEnvironmentVariable(_X("NEW_RELIC_ENABLE_LEGACY_CACHING"), legacyCachingEnabled);
systemCalls->SetEnvironmentVariable(_X("NEW_RELIC_DISABLE_APPDOMAIN_CACHING"), disableAppDomainCache);

AgentCallStyle agentCallStyle(systemCalls);

const auto callStrategy = agentCallStyle.GetConfiguredCallingStrategy();

Assert::AreEqual(static_cast<int>(expectedStrategy), static_cast<int>(callStrategy));
}

void RunTestNoEnvironmentVariablesSet(const AgentCallStyle::Strategy expectedStrategy)
{
auto systemCalls = std::make_shared<MockSystemCalls>();
AgentCallStyle agentCallStyle(systemCalls);

const auto callStrategy = agentCallStyle.GetConfiguredCallingStrategy();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
#include "MockSystemCalls.h"
#include "../MethodRewriter/FunctionManipulator.h"
#include "../MethodRewriter/InstrumentFunctionManipulator.h"
#include "../MethodRewriter/ApiFunctionManipulator.h"
#include "../MethodRewriter/HelperFunctionManipulator.h"

using namespace Microsoft::VisualStudio::CppUnitTestFramework;

Expand All @@ -25,15 +27,201 @@ namespace NewRelic { namespace Profiler { namespace MethodRewriter { namespace T
FunctionManipulator manipulator(function, false, AgentCallStyle::Strategy::InAgentCache);
}

TEST_METHOD(instrument_minimal_method)
TEST_METHOD(instrument_api_method_netframework_inagentcache)
{
auto function = std::make_shared<MockFunction>();
InstrumentFunctionManipulator manipulator(function, std::make_shared<InstrumentationSettings>(nullptr, L""), false, AgentCallStyle::Strategy::InAgentCache);
ApiFunctionManipulator manipulator(function, std::make_shared<InstrumentationSettings>(nullptr, _X("")), false, AgentCallStyle::Strategy::InAgentCache);

manipulator.InstrumentApi();
}

TEST_METHOD(instrument_api_method_netframework_appdomaincache)
{
auto function = std::make_shared<MockFunction>();
ApiFunctionManipulator manipulator(function, std::make_shared<InstrumentationSettings>(nullptr, _X("")), false, AgentCallStyle::Strategy::AppDomainCache);

manipulator.InstrumentApi();
}

TEST_METHOD(instrument_api_method_netframework_reflection)
{
auto function = std::make_shared<MockFunction>();
ApiFunctionManipulator manipulator(function, std::make_shared<InstrumentationSettings>(nullptr, _X("")), false, AgentCallStyle::Strategy::Reflection);

manipulator.InstrumentApi();
}

TEST_METHOD(instrument_api_method_coreclr_inagentcache)
{
auto function = std::make_shared<MockFunction>();
function->_isCoreClr = true;
ApiFunctionManipulator manipulator(function, std::make_shared<InstrumentationSettings>(nullptr, _X("")), true, AgentCallStyle::Strategy::InAgentCache);

manipulator.InstrumentApi();
}

TEST_METHOD(instrument_api_method_coreclr_appdomaincache)
{
auto function = std::make_shared<MockFunction>();
function->_isCoreClr = true;
ApiFunctionManipulator manipulator(function, std::make_shared<InstrumentationSettings>(nullptr, _X("")), true, AgentCallStyle::Strategy::AppDomainCache);

manipulator.InstrumentApi();
}

TEST_METHOD(instrument_api_method_coreclr_reflection)
{
auto function = std::make_shared<MockFunction>();
function->_isCoreClr = true;
ApiFunctionManipulator manipulator(function, std::make_shared<InstrumentationSettings>(nullptr, _X("")), true, AgentCallStyle::Strategy::Reflection);

manipulator.InstrumentApi();
}

TEST_METHOD(instrument_minimal_method_netframework_inagentcache)
{
auto function = std::make_shared<MockFunction>();
InstrumentFunctionManipulator manipulator(function, std::make_shared<InstrumentationSettings>(nullptr, _X("")), false, AgentCallStyle::Strategy::InAgentCache);

auto instrumentationPoint = CreateInstrumentationPointThatMatchesFunction(function);
manipulator.InstrumentDefault(instrumentationPoint);
}

TEST_METHOD(instrument_minimal_method_netframework_appdomaincache)
{
auto function = std::make_shared<MockFunction>();
InstrumentFunctionManipulator manipulator(function, std::make_shared<InstrumentationSettings>(nullptr, _X("")), false, AgentCallStyle::Strategy::AppDomainCache);

auto instrumentationPoint = CreateInstrumentationPointThatMatchesFunction(function);
manipulator.InstrumentDefault(instrumentationPoint);
}

TEST_METHOD(instrument_minimal_method_netframework_reflection)
{
auto function = std::make_shared<MockFunction>();
InstrumentFunctionManipulator manipulator(function, std::make_shared<InstrumentationSettings>(nullptr, _X("")), false, AgentCallStyle::Strategy::Reflection);

auto instrumentationPoint = CreateInstrumentationPointThatMatchesFunction(function);
manipulator.InstrumentDefault(instrumentationPoint);
}

TEST_METHOD(instrument_minimal_method_coreclr_inagentcache)
{
auto function = std::make_shared<MockFunction>();
InstrumentFunctionManipulator manipulator(function, std::make_shared<InstrumentationSettings>(nullptr, _X("")), true, AgentCallStyle::Strategy::InAgentCache);

auto instrumentationPoint = CreateInstrumentationPointThatMatchesFunction(function);
manipulator.InstrumentDefault(instrumentationPoint);
}

TEST_METHOD(instrument_minimal_method_coreclr_appdomaincache)
{
auto function = std::make_shared<MockFunction>();
InstrumentFunctionManipulator manipulator(function, std::make_shared<InstrumentationSettings>(nullptr, _X("")), true, AgentCallStyle::Strategy::AppDomainCache);

auto instrumentationPoint = CreateInstrumentationPointThatMatchesFunction(function);
manipulator.InstrumentDefault(instrumentationPoint);
}

TEST_METHOD(instrument_minimal_method_coreclr_reflection)
{
auto function = std::make_shared<MockFunction>();
InstrumentFunctionManipulator manipulator(function, std::make_shared<InstrumentationSettings>(nullptr, _X("")), true, AgentCallStyle::Strategy::Reflection);

auto instrumentationPoint = CreateInstrumentationPointThatMatchesFunction(function);
manipulator.InstrumentDefault(instrumentationPoint);
}

TEST_METHOD(helper_method_unsupported_method)
{
auto function = std::make_shared<MockFunction>();
function->_functionName = _X("ThisMethodShouldNotExist");
HelperFunctionManipulator manipulator(function, true, AgentCallStyle::Strategy::InAgentCache);

manipulator.InstrumentHelper();
}

TEST_METHOD(helper_method_LoadAssemblyOrThrow)
{
auto function = std::make_shared<MockFunction>();
function->_functionName = _X("LoadAssemblyOrThrow");
HelperFunctionManipulator manipulator(function, true, AgentCallStyle::Strategy::InAgentCache);

manipulator.InstrumentHelper();
}

TEST_METHOD(helper_method_GetTypeViaReflectionOrThrow)
{
auto function = std::make_shared<MockFunction>();
function->_functionName = _X("GetTypeViaReflectionOrThrow");
HelperFunctionManipulator manipulator(function, true, AgentCallStyle::Strategy::InAgentCache);

manipulator.InstrumentHelper();
}

TEST_METHOD(helper_method_GetMethodViaReflectionOrThrow)
{
auto function = std::make_shared<MockFunction>();
function->_functionName = _X("GetMethodViaReflectionOrThrow");
HelperFunctionManipulator manipulator(function, true, AgentCallStyle::Strategy::InAgentCache);

manipulator.InstrumentHelper();
}

TEST_METHOD(helper_method_StoreMethodInAppDomainStorageOrThrow)
{
auto function = std::make_shared<MockFunction>();
function->_functionName = _X("StoreMethodInAppDomainStorageOrThrow");
HelperFunctionManipulator manipulator(function, true, AgentCallStyle::Strategy::InAgentCache);

manipulator.InstrumentHelper();
}

TEST_METHOD(helper_method_GetMethodFromAppDomainStorage)
{
auto function = std::make_shared<MockFunction>();
function->_functionName = _X("GetMethodFromAppDomainStorage");
HelperFunctionManipulator manipulator(function, true, AgentCallStyle::Strategy::InAgentCache);

manipulator.InstrumentHelper();
}

TEST_METHOD(helper_method_GetMethodFromAppDomainStorageOrReflectionOrThrow)
{
auto function = std::make_shared<MockFunction>();
function->_functionName = _X("GetMethodFromAppDomainStorageOrReflectionOrThrow");
HelperFunctionManipulator manipulator(function, true, AgentCallStyle::Strategy::InAgentCache);

manipulator.InstrumentHelper();
}

TEST_METHOD(helper_method_EnsureInitialized)
{
auto function = std::make_shared<MockFunction>();
function->_functionName = _X("EnsureInitialized");
HelperFunctionManipulator manipulator(function, true, AgentCallStyle::Strategy::InAgentCache);

manipulator.InstrumentHelper();
}

TEST_METHOD(helper_method_GetMethodCacheLookupMethod)
{
auto function = std::make_shared<MockFunction>();
function->_functionName = _X("GetMethodCacheLookupMethod");
HelperFunctionManipulator manipulator(function, true, AgentCallStyle::Strategy::InAgentCache);

manipulator.InstrumentHelper();
}

TEST_METHOD(helper_method_GetMethodInfoFromAgentCache)
{
auto function = std::make_shared<MockFunction>();
function->_functionName = _X("GetMethodInfoFromAgentCache");
HelperFunctionManipulator manipulator(function, true, AgentCallStyle::Strategy::InAgentCache);

manipulator.InstrumentHelper();
}

//TEST_METHOD(test_method_with_no_code)
//{
// Assert::Fail(L"Test not implemented.");
Expand Down
Loading
Loading