-
Notifications
You must be signed in to change notification settings - Fork 230
/
CppWinRTComServerTests.cpp
160 lines (132 loc) · 4.28 KB
/
CppWinRTComServerTests.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include "pch.h"
#include "common.h"
#undef GetCurrentTime
#define WINRT_CUSTOM_MODULE_LOCK
#include <wil/cppwinrt_notifiable_module_lock.h>
#include <winrt/Windows.Foundation.h>
#include <wil/cppwinrt_register_com_server.h>
#include <wil/resource.h>
#include <string_view>
#include <thread>
#include <chrono>
using namespace std::string_view_literals;
wil::unique_event _comExit;
void notifier()
{
_comExit.SetEvent();
}
struct MyServer : winrt::implements<MyServer, winrt::Windows::Foundation::IStringable>
{
winrt::hstring ToString()
{
return L"MyServer from Server";
}
};
struct BuggyServer : winrt::implements<BuggyServer, winrt::Windows::Foundation::IStringable>
{
BuggyServer()
{
throw winrt::hresult_access_denied{};
}
winrt::hstring ToString()
{
return L"BuggyServer from Server";
}
};
auto create_my_server_instance()
{
return winrt::create_instance<winrt::Windows::Foundation::IStringable>(winrt::guid_of<MyServer>(), CLSCTX_LOCAL_SERVER);
}
TEST_CASE("CppWinRTComServerTests::DefaultNotifiableModuleLock", "[cppwinrt_com_server]")
{
_comExit.create();
wil::notifiable_module_lock::instance().set_notifier(notifier);
winrt::init_apartment();
{
auto server{winrt::make<MyServer>()};
REQUIRE(winrt::get_module_lock() == 1);
}
_comExit.wait();
REQUIRE(!winrt::get_module_lock());
}
TEST_CASE("CppWinRTComServerTests::RegisterComServer", "[cppwinrt_com_server]")
{
winrt::init_apartment();
{
auto revoker = wil::register_com_server<MyServer>();
auto instance = create_my_server_instance();
REQUIRE(winrt::get_module_lock() == 1);
}
REQUIRE(!winrt::get_module_lock());
try
{
auto instance = create_my_server_instance();
}
catch (winrt::hresult_error const& e)
{
REQUIRE(e.code() == REGDB_E_CLASSNOTREG);
}
}
TEST_CASE("CppWinRTComServerTests::RegisterComServerThrowIsSafe", "[cppwinrt_com_server]")
{
winrt::init_apartment();
{
auto revoker = wil::register_com_server<BuggyServer>();
try
{
auto instance =
winrt::create_instance<winrt::Windows::Foundation::IStringable>(winrt::guid_of<BuggyServer>(), CLSCTX_LOCAL_SERVER);
REQUIRE(false);
}
catch (winrt::hresult_error const& e)
{
REQUIRE(e.code() == E_ACCESSDENIED);
}
}
}
TEST_CASE("CppWinRTComServerTests::AnyRegisterFailureClearAllRegistrations", "[cppwinrt_com_server]")
{
winrt::init_apartment();
witest::detoured_thread_function<&::CoRegisterClassObject> detour(
[](REFCLSID clsid, LPUNKNOWN obj, DWORD ctxt, DWORD flags, LPDWORD cookie) mutable {
if (winrt::guid{clsid} == winrt::guid_of<BuggyServer>())
{
*cookie = 0;
return E_UNEXPECTED;
}
return ::CoRegisterClassObject(clsid, obj, ctxt, flags, cookie);
});
try
{
auto revoker = wil::register_com_server<MyServer, BuggyServer>();
REQUIRE(false);
}
catch (winrt::hresult_error const& e)
{
REQUIRE(e.code() == E_UNEXPECTED);
}
REQUIRE(!winrt::get_module_lock());
}
TEST_CASE("CppWinRTComServerTests::NotifierAndRegistration", "[cppwinrt_com_server]")
{
wil::unique_event moduleEvent(wil::EventOptions::None);
wil::unique_event coroutineRunning(wil::EventOptions::None);
wil::unique_event coroutineContinue(wil::EventOptions::None);
wil::notifiable_module_lock::instance().set_notifier([&]() {
moduleEvent.SetEvent();
});
winrt::init_apartment();
auto revoker = wil::register_com_server<MyServer>();
[&]() -> winrt::Windows::Foundation::IAsyncAction {
co_await winrt::resume_background();
coroutineRunning.SetEvent();
coroutineContinue.wait();
auto instance = create_my_server_instance();
REQUIRE(winrt::get_module_lock() == 2);
}();
coroutineRunning.wait();
REQUIRE(winrt::get_module_lock() == 1); // Coroutine bumped count
coroutineContinue.SetEvent();
moduleEvent.wait();
REQUIRE(!winrt::get_module_lock());
}