Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nrcventura committed Nov 18, 2023
1 parent 4595082 commit e7f84a3
Show file tree
Hide file tree
Showing 3 changed files with 176 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ namespace NewRelic { namespace Profiler { namespace MethodRewriter { namespace T
manipulator.InstrumentApi();
}

TEST_METHOD(instrument_api_method_netframework_inagentcache_ParameterlessWithObjectReturn)
{
auto function = std::make_shared<MockFunction>();
MakeFunctionParameterlessWithObjectReturnType(function);
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>();
Expand All @@ -43,6 +52,15 @@ namespace NewRelic { namespace Profiler { namespace MethodRewriter { namespace T
manipulator.InstrumentApi();
}

TEST_METHOD(instrument_api_method_netframework_appdomaincache_ParameterlessWithObjectReturn)
{
auto function = std::make_shared<MockFunction>();
MakeFunctionParameterlessWithObjectReturnType(function);
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>();
Expand All @@ -51,6 +69,15 @@ namespace NewRelic { namespace Profiler { namespace MethodRewriter { namespace T
manipulator.InstrumentApi();
}

TEST_METHOD(instrument_api_method_netframework_reflection_ParameterlessWithObjectReturn)
{
auto function = std::make_shared<MockFunction>();
MakeFunctionParameterlessWithObjectReturnType(function);
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>();
Expand All @@ -60,6 +87,16 @@ namespace NewRelic { namespace Profiler { namespace MethodRewriter { namespace T
manipulator.InstrumentApi();
}

TEST_METHOD(instrument_api_method_coreclr_inagentcache_ParameterlessWithObjectReturn)
{
auto function = std::make_shared<MockFunction>();
function->_isCoreClr = true;
MakeFunctionParameterlessWithObjectReturnType(function);
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>();
Expand All @@ -69,6 +106,16 @@ namespace NewRelic { namespace Profiler { namespace MethodRewriter { namespace T
manipulator.InstrumentApi();
}

TEST_METHOD(instrument_api_method_coreclr_appdomaincache_ParameterlessWithObjectReturn)
{
auto function = std::make_shared<MockFunction>();
function->_isCoreClr = true;
MakeFunctionParameterlessWithObjectReturnType(function);
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>();
Expand All @@ -78,6 +125,16 @@ namespace NewRelic { namespace Profiler { namespace MethodRewriter { namespace T
manipulator.InstrumentApi();
}

TEST_METHOD(instrument_api_method_coreclr_reflection_ParameterlessWithObjectReturn)
{
auto function = std::make_shared<MockFunction>();
function->_isCoreClr = true;
MakeFunctionParameterlessWithObjectReturnType(function);
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>();
Expand Down Expand Up @@ -222,6 +279,45 @@ namespace NewRelic { namespace Profiler { namespace MethodRewriter { namespace T
manipulator.InstrumentHelper();
}

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

manipulator.InstrumentHelper();
}

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

manipulator.InstrumentHelper();
}

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

manipulator.InstrumentHelper();
}

TEST_METHOD(functionManipulator_ThrowsWhenLoadMethodInfoCalledWithUnsupportedCallStrategy)
{
std::function<void(void)> test = []() {
auto function = std::make_shared<MockFunction>();
TestFunctionManipulator manipulator(function, true, AgentCallStyle::Strategy::InAgentCache);

manipulator.TestLoadMethodInfo(_X(""), _X(""), _X(""), function->GetFunctionId(), []() {});
};

Assert::ExpectException<FunctionManipulatorException>(test, _X("Should throw exception for unsupported agent call style."));
}

//TEST_METHOD(test_method_with_no_code)
//{
// Assert::Fail(L"Test not implemented.");
Expand Down Expand Up @@ -326,5 +422,28 @@ namespace NewRelic { namespace Profiler { namespace MethodRewriter { namespace T
instrumentationPoint->MethodName = function->GetFunctionName();
return instrumentationPoint;
}

void MakeFunctionParameterlessWithObjectReturnType(std::shared_ptr<MockFunction>& function)
{
function->_signature->clear();
function->_signature->push_back(0x00); // default calling convention
function->_signature->push_back(0x00); // 0 params
function->_signature->push_back(0x1c); // object return
}

class TestFunctionManipulator : FunctionManipulator
{
public:
TestFunctionManipulator(IFunctionPtr function, const bool isCoreClr, const AgentCallStyle::Strategy agentCallStrategy) :
FunctionManipulator(function, isCoreClr, agentCallStrategy)
{
Initialize();
}

void TestLoadMethodInfo(xstring_t assemblyPath, xstring_t className, xstring_t methodName, uintptr_t functionId, std::function<void()> argumentTypesLambda)
{
LoadMethodInfo(assemblyPath, className, methodName, functionId, argumentTypesLambda);
}
};
};
}}}}
Original file line number Diff line number Diff line change
Expand Up @@ -321,11 +321,26 @@ namespace NewRelic { namespace Profiler { namespace MethodRewriter { namespace T
AssertHelperMethodIsInstrumented(true, _X("InvokeAgentMethodInvokerFunc"));
}

TEST_METHOD(HelperInstrumentor_CoreClr_ShouldInstrument_GetAgentShimFinishTracerDelegateFunc)
{
AssertHelperMethodIsInstrumented(true, _X("GetAgentShimFinishTracerDelegateFunc"));
}

TEST_METHOD(HelperInstrumentor_CoreClr_ShouldInstrument_GetAgentMethodInvokerObject)
{
AssertHelperMethodIsInstrumented(true, _X("GetAgentMethodInvokerObject"));
}

TEST_METHOD(HelperInstrumentor_CoreClr_ShouldInstrument_StoreAgentMethodInvokerFunc)
{
AssertHelperMethodIsInstrumented(true, _X("StoreAgentMethodInvokerFunc"));
}

TEST_METHOD(HelperInstrumentor_CoreClr_ShouldInstrument_StoreAgentShimFinishTracerDelegateFunc)
{
AssertHelperMethodIsInstrumented(true, _X("StoreAgentShimFinishTracerDelegateFunc"));
}

TEST_METHOD(HelperInstrumentor_CoreClr_ShouldInstrument_EnsureInitialized)
{
AssertHelperMethodIsInstrumented(true, _X("EnsureInitialized"));
Expand Down Expand Up @@ -386,11 +401,26 @@ namespace NewRelic { namespace Profiler { namespace MethodRewriter { namespace T
AssertHelperMethodIsInstrumented(false, _X("InvokeAgentMethodInvokerFunc"));
}

TEST_METHOD(HelperInstrumentor_NetFramework_ShouldInstrument_GetAgentShimFinishTracerDelegateFunc)
{
AssertHelperMethodIsInstrumented(false, _X("GetAgentShimFinishTracerDelegateFunc"));
}

TEST_METHOD(HelperInstrumentor_NetFramework_ShouldInstrument_GetAgentMethodInvokerObject)
{
AssertHelperMethodIsInstrumented(false, _X("GetAgentMethodInvokerObject"));
}

TEST_METHOD(HelperInstrumentor_NetFramework_ShouldInstrument_StoreAgentMethodInvokerFunc)
{
AssertHelperMethodIsInstrumented(false, _X("StoreAgentMethodInvokerFunc"));
}

TEST_METHOD(HelperInstrumentor_NetFramework_ShouldInstrument_StoreAgentShimFinishTracerDelegateFunc)
{
AssertHelperMethodIsInstrumented(false, _X("StoreAgentShimFinishTracerDelegateFunc"));
}

TEST_METHOD(HelperInstrumentor_NetFramework_ShouldInstrument_EnsureInitialized)
{
AssertHelperMethodIsInstrumented(false, _X("EnsureInitialized"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,33 @@ namespace NewRelic { namespace Profiler { namespace MethodRewriter { namespace T
Assert::IsTrue(methodRewriter->ShouldInstrumentFunction(_X("GetAgentMethodInvokerObject")));
}

TEST_METHOD(ShouldInstrumentFunction_StoreAgentMethodInvokerObject)
{
auto function = std::make_shared<MockFunction>();

auto methodRewriter = GetMethodRewriterWithConfigurationForFunction(function);

Assert::IsTrue(methodRewriter->ShouldInstrumentFunction(_X("StoreAgentMethodInvokerObject")));
}

TEST_METHOD(ShouldInstrumentFunction_StoreAgentShimFinishTracerDelegateFunc)
{
auto function = std::make_shared<MockFunction>();

auto methodRewriter = GetMethodRewriterWithConfigurationForFunction(function);

Assert::IsTrue(methodRewriter->ShouldInstrumentFunction(_X("StoreAgentShimFinishTracerDelegateFunc")));
}

TEST_METHOD(ShouldInstrumentFunction_GetAgentShimFinishTracerDelegateFunc)
{
auto function = std::make_shared<MockFunction>();

auto methodRewriter = GetMethodRewriterWithConfigurationForFunction(function);

Assert::IsTrue(methodRewriter->ShouldInstrumentFunction(_X("GetAgentShimFinishTracerDelegateFunc")));
}

private:
Configuration::InstrumentationConfigurationPtr GetInstrumentationConfigurationForFunction(std::shared_ptr<MockFunction> function)
{
Expand Down

0 comments on commit e7f84a3

Please sign in to comment.