forked from microsoft/wil
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWatcherTests.cpp
544 lines (462 loc) · 20.2 KB
/
WatcherTests.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
#include "pch.h"
#include <wil/filesystem.h>
#include <wil/registry.h>
#include <wil/resource.h>
#include <memory> // For shared_event_watcher
#include <wil/resource.h>
#include "common.h"
TEST_CASE("EventWatcherTests::Construction", "[resource][event_watcher]")
{
SECTION("Create unique_event_watcher_nothrow without event")
{
auto watcher = wil::make_event_watcher_nothrow([] {});
REQUIRE(watcher != nullptr);
}
SECTION("Create unique_event_watcher_nothrow with unique_event_nothrow")
{
wil::unique_event_nothrow eventToPass;
FAIL_FAST_IF_FAILED(eventToPass.create(wil::EventOptions::None));
auto watcher = wil::make_event_watcher_nothrow(wistd::move(eventToPass), [] {});
REQUIRE(watcher != nullptr);
REQUIRE(eventToPass.get() == nullptr); // move construction must take it
}
SECTION("Create unique_event_watcher_nothrow with handle")
{
wil::unique_event_nothrow eventToDupe;
FAIL_FAST_IF_FAILED(eventToDupe.create(wil::EventOptions::None));
auto watcher = wil::make_event_watcher_nothrow(eventToDupe.get(), [] {});
REQUIRE(watcher != nullptr);
REQUIRE(eventToDupe.get() != nullptr); // handle duped in this case
}
#ifdef WIL_ENABLE_EXCEPTIONS
SECTION("Create unique_event_watcher_nothrow with unique_event")
{
wil::unique_event eventToPass(wil::EventOptions::None);
auto watcher = wil::make_event_watcher_nothrow(wistd::move(eventToPass), [] {});
REQUIRE(watcher != nullptr);
REQUIRE(eventToPass.get() == nullptr); // move construction must take it
}
SECTION("Create unique_event_watcher without event")
{
auto watcher = wil::make_event_watcher([] {});
}
SECTION("Create unique_event_watcher with unique_event_nothrow")
{
wil::unique_event_nothrow eventToPass;
THROW_IF_FAILED(eventToPass.create(wil::EventOptions::None));
auto watcher = wil::make_event_watcher(wistd::move(eventToPass), [] {});
REQUIRE(eventToPass.get() == nullptr); // move construction must take it
}
SECTION("Create unique_event_watcher with unique_event")
{
wil::unique_event eventToPass(wil::EventOptions::None);
auto watcher = wil::make_event_watcher(wistd::move(eventToPass), [] {});
REQUIRE(eventToPass.get() == nullptr); // move construction must take it
}
SECTION("Create unique_event_watcher with handle")
{
wil::unique_event eventToDupe(wil::EventOptions::None);
auto watcher = wil::make_event_watcher(eventToDupe.get(), [] {});
REQUIRE(eventToDupe.get() != nullptr); // handle duped in this case
}
SECTION("Create unique_event_watcher shared watcher")
{
wil::shared_event_watcher sharedWatcher = wil::make_event_watcher([] {});
}
#endif
}
static auto make_event(wil::EventOptions options = wil::EventOptions::None)
{
wil::unique_event_nothrow result;
FAIL_FAST_IF_FAILED(result.create(options));
return result;
}
TEST_CASE("EventWatcherTests::VerifyDelivery", "[resource][event_watcher]")
{
auto notificationReceived = make_event();
int volatile countObserved = 0;
auto watcher = wil::make_event_watcher_nothrow([&] {
countObserved = countObserved + 1;
notificationReceived.SetEvent();
});
REQUIRE(watcher != nullptr);
watcher.SetEvent();
REQUIRE(notificationReceived.wait(5000)); // 5 second max wait
watcher.SetEvent();
REQUIRE(notificationReceived.wait(5000)); // 5 second max wait
REQUIRE(countObserved == 2);
}
TEST_CASE("EventWatcherTests::VerifyLastChangeObserved", "[resource][event_watcher]")
{
wil::EventOptions const eventOptions[] = {
wil::EventOptions::None,
wil::EventOptions::ManualReset,
wil::EventOptions::Signaled,
wil::EventOptions::ManualReset | wil::EventOptions::Signaled,
};
for (auto const& eventOption : eventOptions)
{
auto allChangesMade = make_event(
wil::EventOptions::ManualReset); // ManualReset to avoid hang in case where 2 callbacks are generated (a test failure).
auto processedChange = make_event();
DWORD volatile stateToObserve = 0;
DWORD volatile lastObservedState = 0;
int volatile countObserved = 0;
auto watcher = wil::make_event_watcher_nothrow(make_event(eventOption), [&] {
allChangesMade.wait();
countObserved = countObserved + 1;
lastObservedState = stateToObserve;
processedChange.SetEvent();
});
REQUIRE(watcher != nullptr);
stateToObserve = 1;
watcher.SetEvent();
stateToObserve = 2;
watcher.SetEvent();
allChangesMade.SetEvent();
REQUIRE(processedChange.wait(5000));
REQUIRE((countObserved == 1 || countObserved == 2)); // ensure the race worked how we wanted it to
REQUIRE(lastObservedState == stateToObserve);
}
}
#define ROOT_KEY_PAIR HKEY_CURRENT_USER, L"Software\\Microsoft\\RegistryWatcherTest"
TEST_CASE("RegistryWatcherTests::Construction", "[registry][registry_watcher]")
{
SECTION("Create unique_registry_watcher_nothrow with string")
{
auto watcher = wil::make_registry_watcher_nothrow(ROOT_KEY_PAIR, true, [&](wil::RegistryChangeKind) {});
REQUIRE(watcher);
}
SECTION("Create unique_registry_watcher_nothrow with unique_hkey")
{
wil::unique_hkey keyToMove;
REQUIRE_SUCCEEDED(HRESULT_FROM_WIN32(RegCreateKeyExW(ROOT_KEY_PAIR, 0, nullptr, 0, KEY_NOTIFY, nullptr, &keyToMove, nullptr)));
auto watcher = wil::make_registry_watcher_nothrow(wistd::move(keyToMove), true, [&](wil::RegistryChangeKind) {});
REQUIRE(watcher);
REQUIRE(keyToMove.get() == nullptr); // ownership is transferred
}
SECTION("Create unique_registry_watcher_nothrow with handle")
{
// construct with just an open registry key
wil::unique_hkey rootKey;
REQUIRE_SUCCEEDED(HRESULT_FROM_WIN32(RegCreateKeyExW(ROOT_KEY_PAIR, 0, nullptr, 0, KEY_NOTIFY, nullptr, &rootKey, nullptr)));
auto watcher = wil::make_registry_watcher_nothrow(rootKey.get(), L"", true, [&](wil::RegistryChangeKind) {});
REQUIRE(watcher);
}
#ifdef WIL_ENABLE_EXCEPTIONS
SECTION("Create unique_registry_watcher with string")
{
REQUIRE_NOTHROW(wil::make_registry_watcher(ROOT_KEY_PAIR, true, [&](wil::RegistryChangeKind) {}));
}
SECTION("Create unique_registry_watcher with unique_hkey")
{
wil::unique_hkey keyToMove;
THROW_IF_FAILED(HRESULT_FROM_WIN32(RegCreateKeyExW(ROOT_KEY_PAIR, 0, nullptr, 0, KEY_NOTIFY, nullptr, &keyToMove, nullptr)));
REQUIRE_NOTHROW(wil::make_registry_watcher(wistd::move(keyToMove), true, [&](wil::RegistryChangeKind) {}));
REQUIRE(keyToMove.get() == nullptr); // ownership is transferred
}
#endif
}
void SetRegistryValue(
_In_ HKEY hKey,
_In_opt_ LPCWSTR lpSubKey,
_In_opt_ LPCWSTR lpValueName,
_In_ DWORD dwType,
_In_reads_bytes_opt_(cbData) LPCVOID lpData,
_In_ DWORD cbData)
{
wil::unique_hkey key;
REQUIRE(RegOpenKeyExW(hKey, lpSubKey, 0, KEY_WRITE, &key) == ERROR_SUCCESS);
REQUIRE(RegSetValueExW(key.get(), lpValueName, 0, dwType, static_cast<BYTE const*>(lpData), cbData) == ERROR_SUCCESS);
}
TEST_CASE("RegistryWatcherTests::VerifyDelivery", "[registry][registry_watcher]")
{
RegDeleteTreeW(ROOT_KEY_PAIR); // So that we get the 'Modify' event
auto notificationReceived = make_event();
int volatile countObserved = 0;
auto volatile observedChangeType = wil::RegistryChangeKind::Delete;
auto watcher = wil::make_registry_watcher_nothrow(ROOT_KEY_PAIR, true, [&](wil::RegistryChangeKind changeType) {
countObserved = countObserved + 1;
observedChangeType = changeType;
notificationReceived.SetEvent();
});
REQUIRE(watcher);
DWORD value = 1;
SetRegistryValue(ROOT_KEY_PAIR, L"value", REG_DWORD, &value, sizeof(value));
REQUIRE(notificationReceived.wait(5000));
REQUIRE(observedChangeType == wil::RegistryChangeKind::Modify);
value++;
SetRegistryValue(ROOT_KEY_PAIR, L"value", REG_DWORD, &value, sizeof(value));
REQUIRE(notificationReceived.wait(5000));
REQUIRE(countObserved == 2);
REQUIRE(observedChangeType == wil::RegistryChangeKind::Modify);
}
TEST_CASE("RegistryWatcherTests::VerifyLastChangeObserved", "[registry][registry_watcher]")
{
RegDeleteTreeW(ROOT_KEY_PAIR);
auto allChangesMade =
make_event(wil::EventOptions::ManualReset); // ManualReset for the case where both registry operations result in a callback.
auto processedChange = make_event();
DWORD volatile stateToObserve = 0;
DWORD volatile lastObservedState = 0;
DWORD volatile lastObservedValue = 0;
int volatile countObserved = 0;
auto watcher = wil::make_registry_watcher_nothrow(ROOT_KEY_PAIR, true, [&, called = false](wil::RegistryChangeKind) mutable {
// This callback may be called more than once (since we modify the key twice), but we're holding references to
// local variables. Therefore, bail out if this is not the first time we're called
if (called)
{
return;
}
called = true;
allChangesMade.wait();
countObserved = countObserved + 1;
lastObservedState = stateToObserve;
DWORD value, cbValue = sizeof(value);
RegGetValueW(ROOT_KEY_PAIR, L"value", RRF_RT_REG_DWORD, nullptr, &value, &cbValue);
lastObservedValue = value;
processedChange.SetEvent();
});
REQUIRE(watcher);
DWORD value;
// make 2 changes and verify that only the last gets observed
stateToObserve = 1;
value = 0;
SetRegistryValue(ROOT_KEY_PAIR, L"value", REG_DWORD, &value, sizeof(value));
stateToObserve = 2;
value = 1;
SetRegistryValue(ROOT_KEY_PAIR, L"value", REG_DWORD, &value, sizeof(value));
allChangesMade.SetEvent();
REQUIRE(processedChange.wait(5000));
REQUIRE(countObserved >= 1); // Sometimes 2 events are observed, see if this can be eliminated.
REQUIRE(lastObservedState == stateToObserve);
REQUIRE(lastObservedValue == 1);
}
TEST_CASE("RegistryWatcherTests::VerifyDeleteBehavior", "[registry][registry_watcher]")
{
auto notificationReceived = make_event();
int volatile countObserved = 0;
auto volatile observedChangeType = wil::RegistryChangeKind::Modify;
auto watcher = wil::make_registry_watcher_nothrow(ROOT_KEY_PAIR, true, [&](wil::RegistryChangeKind changeType) {
countObserved = countObserved + 1;
observedChangeType = changeType;
notificationReceived.SetEvent();
});
REQUIRE(watcher);
RegDeleteTreeW(ROOT_KEY_PAIR); // delete the key to signal the watcher with the special error case
REQUIRE(notificationReceived.wait(5000));
REQUIRE(countObserved == 1);
REQUIRE(observedChangeType == wil::RegistryChangeKind::Delete);
}
TEST_CASE("RegistryWatcherTests::VerifyResetInCallback", "[registry][registry_watcher]")
{
auto notificationReceived = make_event();
wil::unique_registry_watcher_nothrow watcher = wil::make_registry_watcher_nothrow(ROOT_KEY_PAIR, TRUE, [&](wil::RegistryChangeKind) {
watcher.reset();
DWORD value = 2;
SetRegistryValue(ROOT_KEY_PAIR, L"value", REG_DWORD, &value, sizeof(value));
notificationReceived.SetEvent();
});
REQUIRE(watcher);
DWORD value = 1;
SetRegistryValue(ROOT_KEY_PAIR, L"value", REG_DWORD, &value, sizeof(value));
REQUIRE(notificationReceived.wait(5000));
}
// Stress test, disabled by default
TEST_CASE("RegistryWatcherTests::VerifyResetInCallbackStress", "[LocalOnly][registry][registry_watcher][stress]")
{
for (DWORD value = 0; value < 10000; ++value)
{
wil::srwlock lock;
auto notificationReceived = make_event();
wil::unique_registry_watcher_nothrow watcher =
wil::make_registry_watcher_nothrow(ROOT_KEY_PAIR, TRUE, [&](wil::RegistryChangeKind) {
{
auto al = lock.lock_exclusive();
watcher.reset(); // get m_refCount to 1 to ensure the Release happens on the background thread
}
++value;
SetRegistryValue(ROOT_KEY_PAIR, L"value", REG_DWORD, &value, sizeof(value));
notificationReceived.SetEvent();
});
REQUIRE(watcher);
SetRegistryValue(ROOT_KEY_PAIR, L"value", REG_DWORD, &value, sizeof(value));
notificationReceived.wait();
{
auto al = lock.lock_exclusive();
watcher.reset();
}
}
}
TEST_CASE("RegistryWatcherTests::VerifyResetAfterDelete", "[registry][registry_watcher]")
{
auto notificationReceived = make_event();
int volatile countObserved = 0;
auto volatile observedChangeType = wil::RegistryChangeKind::Modify;
wil::unique_registry_watcher_nothrow watcher =
wil::make_registry_watcher_nothrow(ROOT_KEY_PAIR, true, [&](wil::RegistryChangeKind changeType) {
countObserved = countObserved + 1;
observedChangeType = changeType;
notificationReceived.SetEvent();
watcher = wil::make_registry_watcher_nothrow(ROOT_KEY_PAIR, true, [&](wil::RegistryChangeKind changeType) {
countObserved = countObserved + 1;
observedChangeType = changeType;
notificationReceived.SetEvent();
});
REQUIRE(watcher);
});
REQUIRE(watcher);
RegDeleteTreeW(ROOT_KEY_PAIR); // delete the key to signal the watcher with the special error case
notificationReceived.wait();
REQUIRE(countObserved == 1);
REQUIRE(observedChangeType == wil::RegistryChangeKind::Delete);
// wait for the reset to finish. The constructor creates the registry key
notificationReceived.wait(300);
DWORD value = 1;
SetRegistryValue(ROOT_KEY_PAIR, L"value", REG_DWORD, &value, sizeof(value));
notificationReceived.wait();
REQUIRE(countObserved == 2);
REQUIRE(observedChangeType == wil::RegistryChangeKind::Modify);
}
TEST_CASE("RegistryWatcherTests::VerifyCallbackFinishesBeforeFreed", "[registry][registry_watcher]")
{
auto notificationReceived = make_event();
auto deleteNotification = make_event();
int volatile deleteObserved = 0;
auto watcher = wil::make_registry_watcher_nothrow(ROOT_KEY_PAIR, true, [&](wil::RegistryChangeKind) {
notificationReceived.SetEvent();
// ensure that the callback is still being executed while the watcher is reset().
deleteNotification.wait(200);
deleteObserved = deleteObserved + 1;
notificationReceived.SetEvent();
});
RegDeleteTreeW(ROOT_KEY_PAIR); // delete the key to signal the watcher with the special error case
REQUIRE(notificationReceived.wait(5000));
watcher.reset();
deleteNotification.SetEvent();
REQUIRE(notificationReceived.wait(5000));
REQUIRE(deleteObserved == 1);
}
#ifdef WIL_ENABLE_EXCEPTIONS
TEST_CASE("RegistryWatcherTests::VerifyDeleteDuringNotification", "[registry][registry_watcher]")
{
wil::unique_event notificationReceived(wil::EventOptions::None);
wil::unique_event deleteNotification(wil::EventOptions::None);
int mockObserved = 0;
witest::detoured_global_function<&RegNotifyChangeKeyValue> mockRegNotifyChangeKeyValue;
REQUIRE_SUCCEEDED(mockRegNotifyChangeKeyValue.reset([&](HKEY, BOOL, DWORD, HANDLE event, BOOL) -> LSTATUS {
if (!mockObserved)
{
mockObserved++;
SetEvent(event);
// on watcher create just return
return ERROR_SUCCESS;
}
else
{
mockObserved++;
notificationReceived.SetEvent();
deleteNotification.wait();
return ERROR_SUCCESS;
}
}));
auto watcher = wil::make_registry_watcher(ROOT_KEY_PAIR, true, [&](wil::RegistryChangeKind) {});
notificationReceived.wait();
REQUIRE(mockObserved == 2);
deleteNotification.SetEvent();
watcher.reset();
}
#endif
TEST_CASE("FileSystemWatcherTests::Construction", "[resource][folder_watcher]")
{
SECTION("Create unique_folder_watcher_nothrow with valid path")
{
auto watcher = wil::make_folder_watcher_nothrow(L"C:\\Windows\\System32", true, wil::FolderChangeEvents::All, [] {});
REQUIRE(watcher);
}
SECTION("Create unique_folder_watcher_nothrow with invalid path")
{
auto watcher = wil::make_folder_watcher_nothrow(L"X:\\invalid path", true, wil::FolderChangeEvents::All, [] {});
REQUIRE(!watcher);
}
#ifdef WIL_ENABLE_EXCEPTIONS
SECTION("Create unique_folder_watcher with valid path")
{
REQUIRE_NOTHROW(wil::make_folder_watcher(L"C:\\Windows\\System32", true, wil::FolderChangeEvents::All, [] {}));
}
SECTION("Create unique_folder_watcher with invalid path")
{
REQUIRE_THROWS(wil::make_folder_watcher(L"X:\\invalid path", true, wil::FolderChangeEvents::All, [] {}));
}
#endif
}
TEST_CASE("FileSystemWatcherTests::VerifyDelivery", "[resource][folder_watcher]")
{
witest::TestFolder folder;
REQUIRE(folder);
auto notificationEvent = make_event();
int observedCount = 0;
auto watcher = wil::make_folder_watcher_nothrow(folder.Path(), true, wil::FolderChangeEvents::All, [&] {
++observedCount;
notificationEvent.SetEvent();
});
REQUIRE(watcher);
witest::TestFile file(folder.Path(), L"file.txt");
REQUIRE(file);
REQUIRE(notificationEvent.wait(5000));
REQUIRE(observedCount == 1);
witest::TestFile file2(folder.Path(), L"file2.txt");
REQUIRE(file2);
REQUIRE(notificationEvent.wait(5000));
REQUIRE(observedCount == 2);
}
TEST_CASE("FolderChangeReaderTests::Construction", "[resource][folder_change_reader]")
{
SECTION("Create folder_change_reader_nothrow with valid path")
{
auto reader =
wil::make_folder_change_reader_nothrow(L"C:\\Windows\\System32", true, wil::FolderChangeEvents::All, [](auto, auto) {});
REQUIRE(reader);
}
SECTION("Create folder_change_reader_nothrow with invalid path")
{
auto reader =
wil::make_folder_change_reader_nothrow(L"X:\\invalid path", true, wil::FolderChangeEvents::All, [](auto, auto) {});
REQUIRE(!reader);
}
#ifdef WIL_ENABLE_EXCEPTIONS
SECTION("Create folder_change_reader with valid path")
{
REQUIRE_NOTHROW(wil::make_folder_change_reader(L"C:\\Windows\\System32", true, wil::FolderChangeEvents::All, [](auto, auto) {}));
}
SECTION("Create folder_change_reader with invalid path")
{
REQUIRE_THROWS(wil::make_folder_change_reader(L"X:\\invalid path", true, wil::FolderChangeEvents::All, [](auto, auto) {}));
}
#endif
}
TEST_CASE("FolderChangeReaderTests::VerifyDelivery", "[resource][folder_change_reader]")
{
witest::TestFolder folder;
REQUIRE(folder);
auto notificationEvent = make_event();
wil::FolderChangeEvent observedEvent;
wchar_t observedFileName[MAX_PATH] = L"";
auto reader = wil::make_folder_change_reader_nothrow(
folder.Path(), true, wil::FolderChangeEvents::All, [&](wil::FolderChangeEvent event, PCWSTR fileName) {
observedEvent = event;
StringCchCopyW(observedFileName, ARRAYSIZE(observedFileName), fileName);
notificationEvent.SetEvent();
});
REQUIRE(reader);
witest::TestFile testFile(folder.Path(), L"file.txt");
REQUIRE(testFile);
REQUIRE(notificationEvent.wait(5000));
REQUIRE(observedEvent == wil::FolderChangeEvent::Added);
REQUIRE(wcscmp(observedFileName, L"file.txt") == 0);
witest::TestFile testFile2(folder.Path(), L"file2.txt");
REQUIRE(testFile2);
REQUIRE(notificationEvent.wait(5000));
REQUIRE(observedEvent == wil::FolderChangeEvent::Added);
REQUIRE(wcscmp(observedFileName, L"file2.txt") == 0);
}