Skip to content

Commit

Permalink
Additional test coverage for restricted error info (#341)
Browse files Browse the repository at this point in the history
* Additional test coverage for restricted error info

* Ensure restricted error flows from other exception types, too

* Oops, no longer needed there.
  • Loading branch information
jonwis authored Jun 26, 2023
1 parent d784315 commit c5e995e
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 11 deletions.
28 changes: 28 additions & 0 deletions tests/CppWinRTTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
using namespace winrt::Windows::ApplicationModel::Activation;

#include "catch.hpp"
#include <roerrorapi.h>
#include "common.h"

// HRESULT values that C++/WinRT throws as something other than winrt::hresult_error - e.g. a type derived from
// winrt::hresult_error, std::*, etc.
Expand Down Expand Up @@ -646,3 +648,29 @@ TEST_CASE("CppWinRTTests::ResumeForegroundTests", "[cppwinrt]")
}().get();
}
#endif // coroutines

TEST_CASE("CppWinRTTests::ThrownExceptionWithMessage", "[cppwinrt]")
{
SetRestrictedErrorInfo(nullptr);

[]()
{
try
{
throw winrt::hresult_access_denied(L"Puppies not allowed");
}
CATCH_RETURN();
}();
witest::RequireRestrictedErrorInfo(E_ACCESSDENIED, L"Puppies not allowed");

[]()
{
try
{
winrt::check_hresult(E_INVALIDARG);
return S_OK;
}
CATCH_RETURN();
}();
witest::RequireRestrictedErrorInfo(E_INVALIDARG, L"The parameter is incorrect.\r\n");
}
31 changes: 20 additions & 11 deletions tests/ResultTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -580,30 +580,38 @@ TEST_CASE("ResultTests::AutomaticOriginationOnFailure", "[result]")

TEST_CASE("ResultTests::OriginatedWithMessagePreserved", "[result]")
{
SetRestrictedErrorInfo(nullptr);

#ifdef WIL_ENABLE_EXCEPTIONS
try
{
THROW_HR_MSG(E_FAIL, "Puppies not allowed");
}
catch (...) {}
#else
witest::RequireRestrictedErrorInfo(E_FAIL, L"Puppies not allowed");

[]()
{
RETURN_HR_MSG(E_FAIL, "Puppies not allowed");
try
{
throw std::exception("Puppies not allowed");
}
CATCH_RETURN();
}();
witest::RequireRestrictedErrorInfo(HRESULT_FROM_WIN32(ERROR_UNHANDLED_EXCEPTION), L"std::exception: Puppies not allowed");

#endif
wil::com_ptr_nothrow<IRestrictedErrorInfo> errorInfo;
REQUIRE_SUCCEEDED(GetRestrictedErrorInfo(&errorInfo));
wil::unique_bstr description;
wil::unique_bstr restrictedDescription;
wil::unique_bstr capabilitySid;
HRESULT errorCode;
REQUIRE_SUCCEEDED(errorInfo->GetErrorDetails(&description, &errorCode, &restrictedDescription, &capabilitySid));
REQUIRE(E_FAIL == errorCode);
REQUIRE(wcscmp(restrictedDescription.get(), L"Puppies not allowed") == 0);

[]()
{
RETURN_HR_MSG(E_FAIL, "Puppies not allowed");
}();
witest::RequireRestrictedErrorInfo(E_FAIL, L"Puppies not allowed");
}

#endif


TEST_CASE("ResultTests::ReportDoesNotChangeLastError", "[result]")
{
decltype(wil::details::g_pfnLoggingCallback) oopsie = [](wil::FailureInfo const&) noexcept
Expand All @@ -616,3 +624,4 @@ TEST_CASE("ResultTests::ReportDoesNotChangeLastError", "[result]")
LOG_IF_WIN32_BOOL_FALSE(FALSE);
REQUIRE(::GetLastError() == ERROR_ABIOS_ERROR);
}

18 changes: 18 additions & 0 deletions tests/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

#include <wil/filesystem.h>
#include <wil/result.h>
#include <roerrorapi.h>
#include <wil/com.h>

#define REPORTS_ERROR(expr) witest::ReportsError(wistd::is_same<HRESULT, decltype(expr)>{}, [&]() { return expr; })
#define REQUIRE_ERROR(expr) REQUIRE(REPORTS_ERROR(expr))
Expand Down Expand Up @@ -345,6 +347,22 @@ namespace witest
return S_OK;
}

#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_APP | WINAPI_PARTITION_SYSTEM) && (_WIN32_WINNT >= _WIN32_WINNT_WIN8)
inline void RequireRestrictedErrorInfo(HRESULT error, wchar_t const* message)
{
wil::com_ptr_nothrow<IRestrictedErrorInfo> errorInfo;
REQUIRE_SUCCEEDED(GetRestrictedErrorInfo(&errorInfo));
REQUIRE(errorInfo != nullptr);
wil::unique_bstr description;
wil::unique_bstr restrictedDescription;
wil::unique_bstr capabilitySid;
HRESULT errorCode;
REQUIRE_SUCCEEDED(errorInfo->GetErrorDetails(&description, &errorCode, &restrictedDescription, &capabilitySid));
REQUIRE(errorCode == error);
REQUIRE(wcscmp(restrictedDescription.get(), message) == 0);
}
#endif

#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP) && (_WIN32_WINNT >= _WIN32_WINNT_WIN7)

struct TestFolder
Expand Down

0 comments on commit c5e995e

Please sign in to comment.