Skip to content

Commit

Permalink
Add help context marshaling test
Browse files Browse the repository at this point in the history
  • Loading branch information
huoyaoyuan committed Jun 9, 2022
1 parent ac53393 commit 1c357ef
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 9 deletions.
27 changes: 18 additions & 9 deletions src/tests/Interop/COM/NETClients/Primitives/ErrorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public void Run()
{
this.VerifyExpectedException();
this.VerifyReturnHResult();
this.VerifyHelpLink();
}

private void VerifyExpectedException()
Expand All @@ -40,21 +41,29 @@ private void VerifyReturnHResult()

var hrs = new[]
{
unchecked((int)0x80004001),
unchecked((int)0x80004003),
unchecked((int)0x80070005),
unchecked((int)0x80070057),
unchecked((int)0x8000ffff),
-1,
1,
2
};
unchecked((int)0x80004001),
unchecked((int)0x80004003),
unchecked((int)0x80070005),
unchecked((int)0x80070057),
unchecked((int)0x8000ffff),
-1,
1,
2
};

foreach (var hr in hrs)
{
Assert.Equal(hr, this.server.Return_As_HResult(hr));
Assert.Equal(hr, this.server.Return_As_HResult_Struct(hr).hr);
}
}

private void VerifyHelpLink()
{
string helpLink = "C:\\Windows\\system32\\dummy.hlp";
uint helpContext = 5678;
var ex = Assert.Throws<COMException>(() => { this.server.Throw_HResult_HelpLink(unchecked((int)-1), helpLink, helpContext); });
Assert.Equal($"{helpLink}#{helpContext}", ex.HelpLink);
}
}
}
9 changes: 9 additions & 0 deletions src/tests/Interop/COM/NETServer/ErrorMarshalTesting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,13 @@ public Server.Contract.HResult Return_As_HResult_Struct(int hresultToReturn)
{
return new Server.Contract.HResult { hr = hresultToReturn };
}

public void Throw_HResult_HelpLink(int hresultToReturn, string helpLink, uint helpContext)
{
Marshal.GetExceptionForHR(hresultToReturn);

Exception e = Marshal.GetExceptionForHR(hresultToReturn);
e.HelpLink = $"{helpLink}#{helpContext}";
throw e;
}
}
43 changes: 43 additions & 0 deletions src/tests/Interop/COM/NativeClients/Primitives/ErrorTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,48 @@ namespace
THROW_FAIL_IF_FALSE(hr == hrMaybe);
}
}

void VerifyHelpContext(_In_ IErrorMarshalTesting *et)
{
::printf("Verify expected helplink and context\n");

HRESULT hrs[] =
{
E_NOTIMPL,
E_POINTER,
E_ACCESSDENIED,
E_INVALIDARG,
E_UNEXPECTED,
HRESULT{-1},
S_FALSE,
HRESULT{2}
};

BSTR helpLink = SysAllocString(OLESTR("C:\\Windows\\system32\\dummy.hlp"));

for (int i = 0; i < ARRAY_SIZE(hrs); ++i)
{
HRESULT hr = hrs[i];
DWORD helpContext = (DWORD)(i + 0x1234);
HRESULT hrMaybe = et->Throw_HResult_HelpLink(hr, helpLink, helpContext);
THROW_FAIL_IF_FALSE(hr == hrMaybe);

IErrorInfo* pErrInfo;
THROW_IF_FAILED(GetErrorInfo(0, &pErrInfo));

BSTR helpLinkMaybe;
THROW_IF_FAILED(pErrInfo->GetHelpFile(&helpLinkMaybe));
THROW_FAIL_IF_FALSE(VarBstrCmp(helpLink, helpLinkMaybe, LANG_ENGLISH, 0) == VARCMP_EQ);
SysFreeString(helpLinkMaybe);

DWORD helpContextMaybe;
THROW_IF_FAILED(pErrInfo->GetHelpContext(&helpContextMaybe));
THROW_FAIL_IF_FALSE(helpContext == helpContextMaybe);
pErrInfo->Release();
}

SysFreeString(helpLink);
}
}

void Run_ErrorTests()
Expand All @@ -89,4 +131,5 @@ void Run_ErrorTests()
VerifyExpectedException(errorMarshal);
VerifyReturnHResult(errorMarshal);
VerifyReturnHResultStruct(errorMarshal);
VerifyHelpContext(errorMarshal);
}
19 changes: 19 additions & 0 deletions src/tests/Interop/COM/NativeServer/ErrorMarshalTesting.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,25 @@ class ErrorMarshalTesting : public UnknownImpl, public IErrorMarshalTesting
return hresultToReturn;
}

DEF_FUNC(Throw_HResult_HelpLink)(
/*[in]*/ int hresultToReturn,
/*[in]*/ BSTR helpLink,
/*[in]*/ DWORD helpContext)
{
ICreateErrorInfo* pCreateErrInfo;
CreateErrorInfo(&pCreateErrInfo);
pCreateErrInfo->SetHelpFile(helpLink);
pCreateErrInfo->SetHelpContext(helpContext);

IErrorInfo* pErrInfo;
pCreateErrInfo->QueryInterface(IID_IErrorInfo, (void**)&pErrInfo);
SetErrorInfo(0, pErrInfo);
pErrInfo->Release();
pCreateErrInfo->Release();

return HRESULT{ hresultToReturn };
}

public: // IUnknown
STDMETHOD(QueryInterface)(
/* [in] */ REFIID riid,
Expand Down
2 changes: 2 additions & 0 deletions src/tests/Interop/COM/ServerContracts/Server.Contracts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,8 @@ public interface IErrorMarshalTesting

[PreserveSig]
HResult Return_As_HResult_Struct(int hresultToReturn);

void Throw_HResult_HelpLink(int hresultToReturn, string helpLink, uint helpContext);
}

public enum IDispatchTesting_Exception
Expand Down
4 changes: 4 additions & 0 deletions src/tests/Interop/COM/ServerContracts/Server.Contracts.h
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,10 @@ IErrorMarshalTesting : IUnknown
/*[in]*/ int hresultToReturn ) = 0;
virtual int STDMETHODCALLTYPE Return_As_HResult_Struct (
/*[in]*/ int hresultToReturn ) = 0;
virtual HRESULT STDMETHODCALLTYPE Throw_HResult_HelpLink (
/*[in]*/ int hresultToReturn,
/*[in]*/ BSTR helpLink,
/*[in]*/ DWORD helpContext ) = 0;
};

enum IDispatchTesting_Exception
Expand Down

0 comments on commit 1c357ef

Please sign in to comment.