File: nt_registry_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (571 lines) | stat: -rw-r--r-- 23,163 bytes parent folder | download | duplicates (6)
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
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/40285824): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif

#include "chrome/chrome_elf/nt_registry/nt_registry.h"

#include <windows.h>

#include <rpc.h>
#include <stddef.h>

#include "base/test/test_reg_util_win.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/abseil-cpp/absl/cleanup/cleanup.h"

namespace {

//------------------------------------------------------------------------------
// WOW64 redirection tests
//
// - Only HKCU will be tested on the auto (try) bots.
//   HKLM will be kept separate (and manual) for local testing only.
//
// NOTE: Currently no real WOW64 context testing, as building x86 projects
//       during x64 builds is not currently supported for performance reasons.
// https://cs.chromium.org/chromium/src/build/toolchain/win/BUILD.gn?sq%3Dpackage:chromium&l=314
//------------------------------------------------------------------------------

// Utility function for the WOW64 redirection test suites.
// Note: Testing redirection through ADVAPI32 here as well, to get notice if
//       expected behaviour changes!
// If |redirected_path| == nullptr, no redirection is expected in any case.
void DoRedirectTest(nt::ROOT_KEY nt_root_key,
                    const wchar_t* path,
                    const wchar_t* redirected_path OPTIONAL) {
  HANDLE handle = INVALID_HANDLE_VALUE;
  HKEY key_handle = nullptr;
  constexpr ACCESS_MASK kAccess = KEY_WRITE | DELETE;
  const HKEY root_key =
      (nt_root_key == nt::HKCU) ? HKEY_CURRENT_USER : HKEY_LOCAL_MACHINE;

  // Make sure clean before starting.
  nt::DeleteRegKey(nt_root_key, nt::NONE, path);
  if (redirected_path)
    nt::DeleteRegKey(nt_root_key, nt::NONE, redirected_path);

  //----------------------------------------------------------------------------
  // No redirection through ADVAPI32 on straight x86 or x64.
  ASSERT_EQ(ERROR_SUCCESS,
            RegCreateKeyExW(root_key, path, 0, nullptr, REG_OPTION_NON_VOLATILE,
                            kAccess, nullptr, &key_handle, nullptr));
  ASSERT_EQ(ERROR_SUCCESS, RegCloseKey(key_handle));
  ASSERT_TRUE(nt::OpenRegKey(nt_root_key, path, kAccess, &handle, nullptr));
  ASSERT_TRUE(nt::DeleteRegKey(handle));
  nt::CloseRegKey(handle);

#ifdef _WIN64
  //----------------------------------------------------------------------------
  // Try forcing WOW64 redirection on x64 through ADVAPI32.
  ASSERT_EQ(ERROR_SUCCESS,
            RegCreateKeyExW(root_key, path, 0, nullptr, REG_OPTION_NON_VOLATILE,
                            kAccess | KEY_WOW64_32KEY, nullptr, &key_handle,
                            nullptr));
  ASSERT_EQ(ERROR_SUCCESS, RegCloseKey(key_handle));
  // Check path:
  if (nt::OpenRegKey(nt_root_key, path, kAccess, &handle, nullptr)) {
    if (redirected_path)
      ADD_FAILURE();
    ASSERT_TRUE(nt::DeleteRegKey(handle));
    nt::CloseRegKey(handle);
  } else if (!redirected_path) {
    // Should have succeeded.
    ADD_FAILURE();
  }
  if (redirected_path) {
    // Check redirected path:
    if (nt::OpenRegKey(nt_root_key, redirected_path, kAccess, &handle,
                       nullptr)) {
      if (!redirected_path)
        ADD_FAILURE();
      ASSERT_TRUE(nt::DeleteRegKey(handle));
      nt::CloseRegKey(handle);
    } else {
      // Should have succeeded.
      ADD_FAILURE();
    }
  }

  //----------------------------------------------------------------------------
  // Try forcing WOW64 redirection on x64 through NTDLL.
  ASSERT_TRUE(
      nt::CreateRegKey(nt_root_key, path, kAccess | KEY_WOW64_32KEY, nullptr));
  // Check path:
  if (nt::OpenRegKey(nt_root_key, path, kAccess, &handle, nullptr)) {
    if (redirected_path)
      ADD_FAILURE();
    ASSERT_TRUE(nt::DeleteRegKey(handle));
    nt::CloseRegKey(handle);
  } else if (!redirected_path) {
    // Should have succeeded.
    ADD_FAILURE();
  }
  if (redirected_path) {
    // Check redirected path:
    if (nt::OpenRegKey(nt_root_key, redirected_path, kAccess, &handle,
                       nullptr)) {
      if (!redirected_path)
        ADD_FAILURE();
      ASSERT_TRUE(nt::DeleteRegKey(handle));
      nt::CloseRegKey(handle);
    } else {
      // Should have succeeded.
      ADD_FAILURE();
    }
  }
#endif  // _WIN64
}

// These test reg paths match |kClassesSubtree| in nt_registry.cc.
constexpr const wchar_t* kClassesRedirects[] = {
    L"SOFTWARE\\Classes\\CLSID\\chrome_testing",
    L"SOFTWARE\\Classes\\WOW6432Node\\CLSID\\chrome_testing",
    L"SOFTWARE\\Classes\\DirectShow\\chrome_testing",
    L"SOFTWARE\\Classes\\WOW6432Node\\DirectShow\\chrome_testing",
    L"SOFTWARE\\Classes\\Interface\\chrome_testing",
    L"SOFTWARE\\Classes\\WOW6432Node\\Interface\\chrome_testing",
    L"SOFTWARE\\Classes\\Media Type\\chrome_testing",
    L"SOFTWARE\\Classes\\WOW6432Node\\Media Type\\chrome_testing",
    L"SOFTWARE\\Classes\\MediaFoundation\\chrome_testing",
    L"SOFTWARE\\Classes\\WOW6432Node\\MediaFoundation\\chrome_testing"};

static_assert((_countof(kClassesRedirects) & 0x01) == 0,
              "Must have an even number of kClassesRedirects.");

// This test does NOT use NtRegistryTest class.  It requires Windows WOW64
// redirection to take place, which would not happen with a testing redirection
// layer.
TEST(NtRegistryTestRedirection, Wow64RedirectionHKCU) {
  // Using two elements for each loop.
  for (size_t index = 0; index < _countof(kClassesRedirects); index += 2) {
    DoRedirectTest(nt::HKCU, kClassesRedirects[index],
                   kClassesRedirects[index + 1]);
  }
}

// These test reg paths match |kHklmSoftwareSubtree| in nt_registry.cc.
constexpr const wchar_t* kHKLMNoRedirects[] = {
    L"SOFTWARE\\Classes\\chrome_testing",
    L"SOFTWARE\\Clients\\chrome_testing",
    L"SOFTWARE\\Microsoft\\COM3\\chrome_testing",
    L"SOFTWARE\\Microsoft\\Cryptography\\Calais\\Current\\chrome_testing",
    L"SOFTWARE\\Microsoft\\Cryptography\\Calais\\Readers\\chrome_testing",
    L"SOFTWARE\\Microsoft\\Cryptography\\Services\\chrome_testing",
    L"SOFTWARE\\Microsoft\\CTF\\SystemShared\\chrome_testing",
    L"SOFTWARE\\Microsoft\\CTF\\TIP\\chrome_testing",
    L"SOFTWARE\\Microsoft\\DFS\\chrome_testing",
    L"SOFTWARE\\Microsoft\\Driver Signing\\chrome_testing",
    L"SOFTWARE\\Microsoft\\EnterpriseCertificates\\chrome_testing",
    L"SOFTWARE\\Microsoft\\EventSystem\\chrome_testing",
    L"SOFTWARE\\Microsoft\\MSMQ\\chrome_testing",
    L"SOFTWARE\\Microsoft\\Non-Driver Signing\\chrome_testing",
    L"SOFTWARE\\Microsoft\\Notepad\\DefaultFonts\\chrome_testing",
    L"SOFTWARE\\Microsoft\\OLE\\chrome_testing",
    L"SOFTWARE\\Microsoft\\RAS\\chrome_testing",
    L"SOFTWARE\\Microsoft\\RPC\\chrome_testing",
    L"SOFTWARE\\Microsoft\\SOFTWARE\\Microsoft\\Shared "
    L"Tools\\MSInfo\\chrome_testing",
    L"SOFTWARE\\Microsoft\\SystemCertificates\\chrome_testing",
    L"SOFTWARE\\Microsoft\\TermServLicensing\\chrome_testing",
    L"SOFTWARE\\Microsoft\\Transaction Server\\chrome_testing",
    L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App "
    L"Paths\\chrome_testing",
    L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Control "
    L"Panel\\Cursors\\Schemes\\chrome_testing",
    L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\AutoplayHandlers"
    L"\\chrome_testing",
    L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\DriveIcons"
    L"\\chrome_testing",
    L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\KindMap"
    L"\\chrome_testing",
    L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Group "
    L"Policy\\chrome_testing",
    L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Policies\\chrome_testing",
    L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\PreviewHandlers"
    L"\\chrome_testing",
    L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Setup\\chrome_testing",
    L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Telephony\\Locations"
    L"\\chrome_testing",
    L"SOFTWARE\\Microsoft\\Windows "
    L"NT\\CurrentVersion\\Console\\chrome_testing",
    L"SOFTWARE\\Microsoft\\Windows "
    L"NT\\CurrentVersion\\FontDpi\\chrome_testing",
    L"SOFTWARE\\Microsoft\\Windows "
    L"NT\\CurrentVersion\\FontLink\\chrome_testing",
    L"SOFTWARE\\Microsoft\\Windows "
    L"NT\\CurrentVersion\\FontMapper\\chrome_testing",
    L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Fonts\\chrome_testing",
    L"SOFTWARE\\Microsoft\\Windows "
    L"NT\\CurrentVersion\\FontSubstitutes\\chrome_testing",
    L"SOFTWARE\\Microsoft\\Windows "
    L"NT\\CurrentVersion\\Gre_initialize\\chrome_testing",
    L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Image File Execution "
    L"Options\\chrome_testing",
    L"SOFTWARE\\Microsoft\\Windows "
    L"NT\\CurrentVersion\\LanguagePack\\chrome_testing",
    L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\NetworkCards"
    L"\\chrome_testing",
    L"SOFTWARE\\Microsoft\\Windows "
    L"NT\\CurrentVersion\\Perflib\\chrome_testing",
    L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Ports\\chrome_testing",
    L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Print\\chrome_testing",
    L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\ProfileList"
    L"\\chrome_testing",
    L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Time "
    L"Zones\\chrome_testing",
    L"SOFTWARE\\Policies\\chrome_testing",
    L"SOFTWARE\\RegisteredApplications\\chrome_testing"};

// Run from administrator command prompt!
// Note: Disabled for automated testing (HKLM protection).  Local testing
// only.
//
// This test does NOT use NtRegistryTest class.  It requires Windows WOW64
// redirection to take place, which would not happen with a testing redirection
// layer.
TEST(NtRegistryTestRedirection, DISABLED_Wow64RedirectionHKLM) {
  // 1) SOFTWARE is redirected.
  DoRedirectTest(nt::HKLM, L"SOFTWARE\\chrome_testing",
                 L"SOFTWARE\\WOW6432Node\\chrome_testing");

  // 2) Except some subkeys are not.
  for (size_t index = 0; index < _countof(kHKLMNoRedirects); ++index) {
    DoRedirectTest(nt::HKLM, kHKLMNoRedirects[index], nullptr);
  }

  // 3) But then some Classes subkeys are redirected.
  // Using two elements for each loop.
  for (size_t index = 0; index < _countof(kClassesRedirects); index += 2) {
    DoRedirectTest(nt::HKLM, kClassesRedirects[index],
                   kClassesRedirects[index + 1]);
  }

  // 4) And just make sure other Classes subkeys are shared.
  DoRedirectTest(nt::HKLM, L"SOFTWARE\\Classes\\chrome_testing", nullptr);
}

TEST(NtRegistryTestMisc, SanitizeSubkeyPaths) {
  std::wstring new_path;
  EXPECT_TRUE(nt::SetTestingOverride(nt::HKCU, new_path));
  std::wstring sani_path = nt::GetTestingOverride(nt::HKCU);
  EXPECT_STREQ(L"", sani_path.c_str());

  new_path = L"boo";
  EXPECT_TRUE(nt::SetTestingOverride(nt::HKCU, new_path));
  sani_path = nt::GetTestingOverride(nt::HKCU);
  EXPECT_STREQ(L"boo", sani_path.c_str());

  new_path = L"\\boo";
  EXPECT_TRUE(nt::SetTestingOverride(nt::HKCU, new_path));
  sani_path = nt::GetTestingOverride(nt::HKCU);
  EXPECT_STREQ(L"boo", sani_path.c_str());

  new_path = L"boo\\";
  EXPECT_TRUE(nt::SetTestingOverride(nt::HKCU, new_path));
  sani_path = nt::GetTestingOverride(nt::HKCU);
  EXPECT_STREQ(L"boo", sani_path.c_str());

  new_path = L"\\\\\\";
  EXPECT_TRUE(nt::SetTestingOverride(nt::HKCU, new_path));
  sani_path = nt::GetTestingOverride(nt::HKCU);
  EXPECT_STREQ(L"", sani_path.c_str());

  new_path = L"boo\\\\\\ya";
  EXPECT_TRUE(nt::SetTestingOverride(nt::HKCU, new_path));
  sani_path = nt::GetTestingOverride(nt::HKCU);
  EXPECT_STREQ(L"boo\\ya", sani_path.c_str());

  new_path = L"\\\\\\boo\\ya\\\\";
  EXPECT_TRUE(nt::SetTestingOverride(nt::HKCU, new_path));
  sani_path = nt::GetTestingOverride(nt::HKCU);
  EXPECT_STREQ(L"boo\\ya", sani_path.c_str());

  // Be sure to leave the environment clean.
  new_path.clear();
  EXPECT_TRUE(nt::SetTestingOverride(nt::HKCU, new_path));
  sani_path = nt::GetTestingOverride(nt::HKCU);
  EXPECT_STREQ(L"", sani_path.c_str());
}

//------------------------------------------------------------------------------
// NtRegistryTest class
//
// Only use this class for tests that need testing registry redirection.
//------------------------------------------------------------------------------

class NtRegistryTest : public testing::Test {
 protected:
  void SetUp() override {
    std::wstring temp;
    ASSERT_NO_FATAL_FAILURE(
        override_manager_.OverrideRegistry(HKEY_CURRENT_USER, &temp));
    ASSERT_TRUE(nt::SetTestingOverride(nt::HKCU, temp));
    ASSERT_NO_FATAL_FAILURE(
        override_manager_.OverrideRegistry(HKEY_LOCAL_MACHINE, &temp));
    ASSERT_TRUE(nt::SetTestingOverride(nt::HKLM, temp));
  }

  void TearDown() override {
    std::wstring temp;
    ASSERT_TRUE(nt::SetTestingOverride(nt::HKCU, temp));
    ASSERT_TRUE(nt::SetTestingOverride(nt::HKLM, temp));
  }

 private:
  registry_util::RegistryOverrideManager override_manager_;
};

//------------------------------------------------------------------------------
// NT registry API tests
//------------------------------------------------------------------------------

TEST_F(NtRegistryTest, ApiDword) {
  HANDLE key_handle;
  const wchar_t* dword_val_name = L"DwordTestValue";
  DWORD dword_val = 1234;

  // Create a subkey to play under.
  ASSERT_TRUE(nt::CreateRegKey(nt::HKCU, L"NTRegistry\\dword", KEY_ALL_ACCESS,
                               &key_handle));
  ASSERT_NE(key_handle, INVALID_HANDLE_VALUE);
  ASSERT_NE(key_handle, nullptr);
  absl::Cleanup key_closer = [key_handle] { nt::CloseRegKey(key_handle); };

  DWORD get_dword = 0;
  EXPECT_FALSE(nt::QueryRegValueDWORD(key_handle, dword_val_name, &get_dword));

  // Set
  EXPECT_TRUE(nt::SetRegValueDWORD(key_handle, dword_val_name, dword_val));

  // Get
  EXPECT_TRUE(nt::QueryRegValueDWORD(key_handle, dword_val_name, &get_dword));
  EXPECT_EQ(get_dword, dword_val);

  // Clean up done by NtRegistryTest.
}

TEST_F(NtRegistryTest, ApiSz) {
  HANDLE key_handle;
  const wchar_t* sz_val_name = L"SzTestValue";
  std::wstring sz_val = L"blah de blah de blahhhhh.";
  const wchar_t* sz_val_name2 = L"SzTestValueEmpty";
  std::wstring sz_val2;
  const wchar_t* sz_val_name3 = L"SzTestValueMalformed";
  const wchar_t* sz_val_name4 = L"SzTestValueMalformed2";
  std::wstring sz_val3 = L"malformed";
  BYTE* sz_val3_byte = reinterpret_cast<BYTE*>(&sz_val3[0]);
  std::vector<BYTE> malform;
  for (size_t i = 0; i < (sz_val3.size() * sizeof(wchar_t)); i++)
    malform.push_back(sz_val3_byte[i]);
  const wchar_t* sz_val_name5 = L"SzTestValueSize0";

  // Create a subkey to play under.
  // ------------------------------
  ASSERT_TRUE(nt::CreateRegKey(nt::HKCU, L"NTRegistry\\sz", KEY_ALL_ACCESS,
                               &key_handle));
  ASSERT_NE(key_handle, INVALID_HANDLE_VALUE);
  ASSERT_NE(key_handle, nullptr);
  absl::Cleanup key_closer = [key_handle] { nt::CloseRegKey(key_handle); };

  std::wstring get_sz;
  EXPECT_FALSE(nt::QueryRegValueSZ(key_handle, sz_val_name, &get_sz));

  // Set
  // ------------------------------
  EXPECT_TRUE(nt::SetRegValueSZ(key_handle, sz_val_name, sz_val));
  EXPECT_TRUE(nt::SetRegValueSZ(key_handle, sz_val_name2, sz_val2));
  // No null terminator.
  EXPECT_TRUE(nt::SetRegKeyValue(key_handle, sz_val_name3, REG_SZ,
                                 malform.data(),
                                 static_cast<DWORD>(malform.size())));
  malform.push_back(0);
  // Single trailing 0 byte.
  EXPECT_TRUE(nt::SetRegKeyValue(key_handle, sz_val_name4, REG_SZ,
                                 malform.data(),
                                 static_cast<DWORD>(malform.size())));
  // Size 0 value.
  EXPECT_TRUE(nt::SetRegKeyValue(key_handle, sz_val_name5, REG_SZ, nullptr, 0));

  // Get
  // ------------------------------
  EXPECT_TRUE(nt::QueryRegValueSZ(key_handle, sz_val_name, &get_sz));
  EXPECT_EQ(get_sz, sz_val);
  EXPECT_TRUE(nt::QueryRegValueSZ(key_handle, sz_val_name2, &get_sz));
  EXPECT_EQ(get_sz, sz_val2);
  EXPECT_TRUE(nt::QueryRegValueSZ(key_handle, sz_val_name3, &get_sz));
  // Should be adjusted under the hood to equal sz_val3.
  EXPECT_EQ(get_sz, sz_val3);
  EXPECT_TRUE(nt::QueryRegValueSZ(key_handle, sz_val_name4, &get_sz));
  // Should be adjusted under the hood to equal sz_val3.
  EXPECT_EQ(get_sz, sz_val3);
  EXPECT_TRUE(nt::QueryRegValueSZ(key_handle, sz_val_name5, &get_sz));
  // Should be adjusted under the hood to an empty string.
  EXPECT_EQ(get_sz, sz_val2);

  // Clean up done by NtRegistryTest.
}

TEST_F(NtRegistryTest, ApiMultiSz) {
  HANDLE key_handle;
  std::vector<std::wstring> multisz_val_set;
  std::vector<std::wstring> multisz_val_get;

  // Create a subkey to play under.
  // ------------------------------
  ASSERT_TRUE(nt::CreateRegKey(nt::HKCU, L"NTRegistry\\multisz", KEY_ALL_ACCESS,
                               &key_handle));
  ASSERT_NE(key_handle, INVALID_HANDLE_VALUE);
  ASSERT_NE(key_handle, nullptr);
  absl::Cleanup key_closer = [key_handle] { nt::CloseRegKey(key_handle); };

  // Test 1 - Success
  // ------------------------------
  const wchar_t* multisz_val_name = L"SzmultiTestValue";
  std::wstring multi1 = L"one";
  std::wstring multi2 = L"two";
  std::wstring multi3 = L"three";

  multisz_val_set.push_back(multi1);
  multisz_val_set.push_back(multi2);
  multisz_val_set.push_back(multi3);
  EXPECT_TRUE(
      nt::SetRegValueMULTISZ(key_handle, multisz_val_name, multisz_val_set));

  EXPECT_TRUE(
      nt::QueryRegValueMULTISZ(key_handle, multisz_val_name, &multisz_val_get));
  EXPECT_EQ(multisz_val_get, multisz_val_set);
  multisz_val_set.clear();
  multisz_val_get.clear();

  // Test 2 - Bad value
  // ------------------------------
  const wchar_t* multisz_val_name2 = L"SzmultiTestValueBad";
  std::wstring multi_empty;

  multisz_val_set.push_back(multi_empty);
  EXPECT_TRUE(
      nt::SetRegValueMULTISZ(key_handle, multisz_val_name2, multisz_val_set));

  EXPECT_TRUE(nt::QueryRegValueMULTISZ(key_handle, multisz_val_name2,
                                       &multisz_val_get));
  EXPECT_EQ(multisz_val_get.size(), static_cast<DWORD>(0));
  multisz_val_set.clear();
  multisz_val_get.clear();

  // Test 3 - Malformed
  // ------------------------------
  std::wstring multisz_val3 = L"malformed";
  multisz_val_set.push_back(multisz_val3);
  BYTE* multisz_val3_byte = reinterpret_cast<BYTE*>(&multisz_val3[0]);
  std::vector<BYTE> malform;
  for (size_t i = 0; i < (multisz_val3.size() * sizeof(wchar_t)); i++)
    malform.push_back(multisz_val3_byte[i]);

  // 3.1: No null terminator.
  // ------------------------------
  const wchar_t* multisz_val_name3 = L"SzmultiTestValueMalformed";
  EXPECT_TRUE(nt::SetRegKeyValue(key_handle, multisz_val_name3, REG_MULTI_SZ,
                                 malform.data(),
                                 static_cast<DWORD>(malform.size())));
  EXPECT_TRUE(nt::QueryRegValueMULTISZ(key_handle, multisz_val_name3,
                                       &multisz_val_get));
  // Should be adjusted under the hood to equal multisz_val3.
  EXPECT_EQ(multisz_val_get, multisz_val_set);
  multisz_val_get.clear();

  // 3.2: Single trailing 0 byte.
  // ------------------------------
  const wchar_t* multisz_val_name4 = L"SzmultiTestValueMalformed2";
  malform.push_back(0);
  EXPECT_TRUE(nt::SetRegKeyValue(key_handle, multisz_val_name4, REG_MULTI_SZ,
                                 malform.data(),
                                 static_cast<DWORD>(malform.size())));
  EXPECT_TRUE(nt::QueryRegValueMULTISZ(key_handle, multisz_val_name4,
                                       &multisz_val_get));
  // Should be adjusted under the hood to equal multisz_val3.
  EXPECT_EQ(multisz_val_get, multisz_val_set);
  multisz_val_get.clear();

  // 3.3: Two trailing 0 bytes.
  // ------------------------------
  const wchar_t* multisz_val_name5 = L"SzmultiTestValueMalformed3";
  malform.push_back(0);
  EXPECT_TRUE(nt::SetRegKeyValue(key_handle, multisz_val_name5, REG_MULTI_SZ,
                                 malform.data(),
                                 static_cast<DWORD>(malform.size())));
  EXPECT_TRUE(nt::QueryRegValueMULTISZ(key_handle, multisz_val_name5,
                                       &multisz_val_get));
  // Should be adjusted under the hood to equal multisz_val3.
  EXPECT_EQ(multisz_val_get, multisz_val_set);
  multisz_val_get.clear();

  // 3.4: Three trailing 0 bytes.
  // ------------------------------
  const wchar_t* multisz_val_name6 = L"SzmultiTestValueMalformed4";
  malform.push_back(0);
  EXPECT_TRUE(nt::SetRegKeyValue(key_handle, multisz_val_name6, REG_MULTI_SZ,
                                 malform.data(),
                                 static_cast<DWORD>(malform.size())));
  EXPECT_TRUE(nt::QueryRegValueMULTISZ(key_handle, multisz_val_name6,
                                       &multisz_val_get));
  // Should be adjusted under the hood to equal multisz_val3.
  EXPECT_EQ(multisz_val_get, multisz_val_set);
  multisz_val_set.clear();
  multisz_val_get.clear();

  // Test 4 - Size zero
  // ------------------------------
  const wchar_t* multisz_val_name7 = L"SzmultiTestValueSize0";
  EXPECT_TRUE(nt::SetRegKeyValue(key_handle, multisz_val_name7, REG_MULTI_SZ,
                                 nullptr, 0));
  EXPECT_TRUE(nt::QueryRegValueMULTISZ(key_handle, multisz_val_name7,
                                       &multisz_val_get));
  // Should be empty.
  EXPECT_EQ(multisz_val_get, multisz_val_set);

  // Clean up done by NtRegistryTest.
}

TEST_F(NtRegistryTest, CreateRegKeyRecursion) {
  HANDLE key_handle;
  const wchar_t* sz_new_key_1 = L"test1\\new\\subkey";
  const wchar_t* sz_new_key_2 = L"test2\\new\\subkey\\blah\\";
  const wchar_t* sz_new_key_3 = L"\\test3\\new\\subkey\\\\blah2";

  // Tests for CreateRegKey recursion.
  ASSERT_TRUE(
      nt::CreateRegKey(nt::HKCU, sz_new_key_1, KEY_ALL_ACCESS, nullptr));
  EXPECT_TRUE(nt::OpenRegKey(nt::HKCU, sz_new_key_1, KEY_ALL_ACCESS,
                             &key_handle, nullptr));
  EXPECT_TRUE(nt::DeleteRegKey(key_handle));
  nt::CloseRegKey(key_handle);

  ASSERT_TRUE(
      nt::CreateRegKey(nt::HKCU, sz_new_key_2, KEY_ALL_ACCESS, nullptr));
  EXPECT_TRUE(nt::OpenRegKey(nt::HKCU, sz_new_key_2, KEY_ALL_ACCESS,
                             &key_handle, nullptr));
  EXPECT_TRUE(nt::DeleteRegKey(key_handle));
  nt::CloseRegKey(key_handle);

  ASSERT_TRUE(
      nt::CreateRegKey(nt::HKCU, sz_new_key_3, KEY_ALL_ACCESS, nullptr));
  EXPECT_TRUE(nt::OpenRegKey(nt::HKCU, L"test3\\new\\subkey\\blah2",
                             KEY_ALL_ACCESS, &key_handle, nullptr));
  EXPECT_TRUE(nt::DeleteRegKey(key_handle));
  nt::CloseRegKey(key_handle);

  // Subkey path can be null.
  ASSERT_TRUE(nt::CreateRegKey(nt::HKCU, nullptr, KEY_ALL_ACCESS, &key_handle));
  ASSERT_NE(key_handle, INVALID_HANDLE_VALUE);
  ASSERT_NE(key_handle, nullptr);
  nt::CloseRegKey(key_handle);

  // Clean up done by NtRegistryTest.
}

}  // namespace