blob: 389c3a243c5b7778a837c58893bf48db178a834e [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
2 * Copyright 2003 The WebRTC Project Authors. All rights reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11// Unittest for registry access API
12
13#include "webrtc/base/gunit.h"
14#include "webrtc/base/common.h"
15#include "webrtc/base/win32regkey.h"
16
17namespace rtc {
18
19#ifndef EXPECT_SUCCEEDED
20#define EXPECT_SUCCEEDED(x) EXPECT_TRUE(SUCCEEDED(x))
21#endif
22
23#ifndef EXPECT_FAILED
24#define EXPECT_FAILED(x) EXPECT_TRUE(FAILED(x))
25#endif
26
27#define kBaseKey L"Software\\Google\\__TEST"
28#define kSubkeyName L"subkey_test"
29
30const wchar_t kRkey1[] = kBaseKey;
31const wchar_t kRkey1SubkeyName[] = kSubkeyName;
32const wchar_t kRkey1Subkey[] = kBaseKey L"\\" kSubkeyName;
33const wchar_t kFullRkey1[] = L"HKCU\\" kBaseKey;
34const wchar_t kFullRkey1Subkey[] = L"HKCU\\" kBaseKey L"\\" kSubkeyName;
35
36const wchar_t kValNameInt[] = L"Int32 Value";
37const DWORD kIntVal = 20;
38const DWORD kIntVal2 = 30;
39
40const wchar_t kValNameInt64[] = L"Int64 Value";
41const DWORD64 kIntVal64 = 119600064000000000uI64;
42
43const wchar_t kValNameFloat[] = L"Float Value";
44const float kFloatVal = 12.3456789f;
45
46const wchar_t kValNameDouble[] = L"Double Value";
47const double kDoubleVal = 98.7654321;
48
49const wchar_t kValNameStr[] = L"Str Value";
50const wchar_t kStrVal[] = L"Some string data 1";
51const wchar_t kStrVal2[] = L"Some string data 2";
52
53const wchar_t kValNameBinary[] = L"Binary Value";
54const char kBinaryVal[] = "Some binary data abcdefghi 1";
55const char kBinaryVal2[] = "Some binary data abcdefghi 2";
56
57const wchar_t kValNameMultiStr[] = L"MultiStr Value";
58const wchar_t kMultiSZ[] = L"abc\0def\0P12345\0";
59const wchar_t kEmptyMultiSZ[] = L"";
60const wchar_t kInvalidMultiSZ[] = {L'6', L'7', L'8'};
61
62// friend function of RegKey
63void RegKeyHelperFunctionsTest() {
64 // Try out some dud values
65 std::wstring temp_key = L"";
66 EXPECT_TRUE(RegKey::GetRootKeyInfo(&temp_key) == NULL);
67 EXPECT_STREQ(temp_key.c_str(), L"");
68
69 temp_key = L"a";
70 EXPECT_TRUE(RegKey::GetRootKeyInfo(&temp_key) == NULL);
71 EXPECT_STREQ(temp_key.c_str(), L"");
72
73 // The basics
74 temp_key = L"HKLM\\a";
75 EXPECT_EQ(RegKey::GetRootKeyInfo(&temp_key), HKEY_LOCAL_MACHINE);
76 EXPECT_STREQ(temp_key.c_str(), L"a");
77
78 temp_key = L"HKEY_LOCAL_MACHINE\\a";
79 EXPECT_EQ(RegKey::GetRootKeyInfo(&temp_key), HKEY_LOCAL_MACHINE);
80 EXPECT_STREQ(temp_key.c_str(), L"a");
81
82 temp_key = L"HKCU\\a";
83 EXPECT_EQ(RegKey::GetRootKeyInfo(&temp_key), HKEY_CURRENT_USER);
84 EXPECT_STREQ(temp_key.c_str(), L"a");
85
86 temp_key = L"HKEY_CURRENT_USER\\a";
87 EXPECT_EQ(RegKey::GetRootKeyInfo(&temp_key), HKEY_CURRENT_USER);
88 EXPECT_STREQ(temp_key.c_str(), L"a");
89
90 temp_key = L"HKU\\a";
91 EXPECT_EQ(RegKey::GetRootKeyInfo(&temp_key), HKEY_USERS);
92 EXPECT_STREQ(temp_key.c_str(), L"a");
93
94 temp_key = L"HKEY_USERS\\a";
95 EXPECT_EQ(RegKey::GetRootKeyInfo(&temp_key), HKEY_USERS);
96 EXPECT_STREQ(temp_key.c_str(), L"a");
97
98 temp_key = L"HKCR\\a";
99 EXPECT_EQ(RegKey::GetRootKeyInfo(&temp_key), HKEY_CLASSES_ROOT);
100 EXPECT_STREQ(temp_key.c_str(), L"a");
101
102 temp_key = L"HKEY_CLASSES_ROOT\\a";
103 EXPECT_EQ(RegKey::GetRootKeyInfo(&temp_key), HKEY_CLASSES_ROOT);
104 EXPECT_STREQ(temp_key.c_str(), L"a");
105
106 // Make sure it is case insensitive
107 temp_key = L"hkcr\\a";
108 EXPECT_EQ(RegKey::GetRootKeyInfo(&temp_key), HKEY_CLASSES_ROOT);
109 EXPECT_STREQ(temp_key.c_str(), L"a");
110
111 temp_key = L"hkey_CLASSES_ROOT\\a";
112 EXPECT_EQ(RegKey::GetRootKeyInfo(&temp_key), HKEY_CLASSES_ROOT);
113 EXPECT_STREQ(temp_key.c_str(), L"a");
114
115 //
116 // Test RegKey::GetParentKeyInfo
117 //
118
119 // dud cases
120 temp_key = L"";
121 EXPECT_STREQ(RegKey::GetParentKeyInfo(&temp_key).c_str(), L"");
122 EXPECT_STREQ(temp_key.c_str(), L"");
123
124 temp_key = L"a";
125 EXPECT_STREQ(RegKey::GetParentKeyInfo(&temp_key).c_str(), L"");
126 EXPECT_STREQ(temp_key.c_str(), L"a");
127
128 temp_key = L"a\\b";
129 EXPECT_STREQ(RegKey::GetParentKeyInfo(&temp_key).c_str(), L"a");
130 EXPECT_STREQ(temp_key.c_str(), L"b");
131
132 temp_key = L"\\b";
133 EXPECT_STREQ(RegKey::GetParentKeyInfo(&temp_key).c_str(), L"");
134 EXPECT_STREQ(temp_key.c_str(), L"b");
135
136 // Some regular cases
137 temp_key = L"HKEY_CLASSES_ROOT\\moon";
138 EXPECT_STREQ(RegKey::GetParentKeyInfo(&temp_key).c_str(),
139 L"HKEY_CLASSES_ROOT");
140 EXPECT_STREQ(temp_key.c_str(), L"moon");
141
142 temp_key = L"HKEY_CLASSES_ROOT\\moon\\doggy";
143 EXPECT_STREQ(RegKey::GetParentKeyInfo(&temp_key).c_str(),
144 L"HKEY_CLASSES_ROOT\\moon");
145 EXPECT_STREQ(temp_key.c_str(), L"doggy");
146
147 //
148 // Test MultiSZBytesToStringArray
149 //
150
151 std::vector<std::wstring> result;
152 EXPECT_SUCCEEDED(RegKey::MultiSZBytesToStringArray(
Peter Boström0c4e06b2015-10-07 12:23:21 +0200153 reinterpret_cast<const uint8_t*>(kMultiSZ), sizeof(kMultiSZ), &result));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000154 EXPECT_EQ(result.size(), 3);
155 EXPECT_STREQ(result[0].c_str(), L"abc");
156 EXPECT_STREQ(result[1].c_str(), L"def");
157 EXPECT_STREQ(result[2].c_str(), L"P12345");
158
159 EXPECT_SUCCEEDED(RegKey::MultiSZBytesToStringArray(
Peter Boström0c4e06b2015-10-07 12:23:21 +0200160 reinterpret_cast<const uint8_t*>(kEmptyMultiSZ), sizeof(kEmptyMultiSZ),
161 &result));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000162 EXPECT_EQ(result.size(), 0);
163 EXPECT_FALSE(SUCCEEDED(RegKey::MultiSZBytesToStringArray(
Peter Boström0c4e06b2015-10-07 12:23:21 +0200164 reinterpret_cast<const uint8_t*>(kInvalidMultiSZ),
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000165 sizeof(kInvalidMultiSZ), &result)));
166}
167
168TEST(RegKeyTest, RegKeyHelperFunctionsTest) {
169 RegKeyHelperFunctionsTest();
170}
171
pbos@webrtc.orgf7a58932015-01-14 09:03:16 +0000172void RegKeyNonStaticFunctionsTest() {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000173 DWORD int_val = 0;
174 DWORD64 int64_val = 0;
175 wchar_t* str_val = NULL;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200176 uint8_t* binary_val = NULL;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000177 DWORD uint8_count = 0;
178
179 // Just in case...
180 // make sure the no test key residue is left from previous aborted runs
181 RegKey::DeleteKey(kFullRkey1);
182
183 // initial state
184 RegKey r_key;
185 EXPECT_TRUE(r_key.key() == NULL);
186
187 // create a reg key
188 EXPECT_SUCCEEDED(r_key.Create(HKEY_CURRENT_USER, kRkey1));
189
190 // do the create twice - it should return the already created one
191 EXPECT_SUCCEEDED(r_key.Create(HKEY_CURRENT_USER, kRkey1));
192
193 // now do an open - should work just fine
194 EXPECT_SUCCEEDED(r_key.Open(HKEY_CURRENT_USER, kRkey1));
195
196 // get an in-existent value
197 EXPECT_EQ(r_key.GetValue(kValNameInt, &int_val),
198 HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND));
199
200 // set and get some values
201
202 // set an INT 32
203 EXPECT_SUCCEEDED(r_key.SetValue(kValNameInt, kIntVal));
204
205 // check that the value exists
206 EXPECT_TRUE(r_key.HasValue(kValNameInt));
207
208 // read it back
209 EXPECT_SUCCEEDED(r_key.GetValue(kValNameInt, &int_val));
210 EXPECT_EQ(int_val, kIntVal);
211
212 // set it again!
213 EXPECT_SUCCEEDED(r_key.SetValue(kValNameInt, kIntVal2));
214
215 // read it again
216 EXPECT_SUCCEEDED(r_key.GetValue(kValNameInt, &int_val));
217 EXPECT_EQ(int_val, kIntVal2);
218
219 // delete the value
220 EXPECT_SUCCEEDED(r_key.DeleteValue(kValNameInt));
221
222 // check that the value is gone
223 EXPECT_FALSE(r_key.HasValue(kValNameInt));
224
225 // set an INT 64
226 EXPECT_SUCCEEDED(r_key.SetValue(kValNameInt64, kIntVal64));
227
228 // check that the value exists
229 EXPECT_TRUE(r_key.HasValue(kValNameInt64));
230
231 // read it back
232 EXPECT_SUCCEEDED(r_key.GetValue(kValNameInt64, &int64_val));
233 EXPECT_EQ(int64_val, kIntVal64);
234
235 // delete the value
236 EXPECT_SUCCEEDED(r_key.DeleteValue(kValNameInt64));
237
238 // check that the value is gone
239 EXPECT_FALSE(r_key.HasValue(kValNameInt64));
240
241 // set a string
242 EXPECT_SUCCEEDED(r_key.SetValue(kValNameStr, kStrVal));
243
244 // check that the value exists
245 EXPECT_TRUE(r_key.HasValue(kValNameStr));
246
247 // read it back
248 EXPECT_SUCCEEDED(r_key.GetValue(kValNameStr, &str_val));
249 EXPECT_TRUE(lstrcmp(str_val, kStrVal) == 0);
250 delete[] str_val;
251
252 // set it again
253 EXPECT_SUCCEEDED(r_key.SetValue(kValNameStr, kStrVal2));
254
255 // read it again
256 EXPECT_SUCCEEDED(r_key.GetValue(kValNameStr, &str_val));
257 EXPECT_TRUE(lstrcmp(str_val, kStrVal2) == 0);
258 delete[] str_val;
259
260 // delete the value
261 EXPECT_SUCCEEDED(r_key.DeleteValue(kValNameStr));
262
263 // check that the value is gone
264 EXPECT_FALSE(r_key.HasValue(kValNameInt));
265
266 // set a binary value
267 EXPECT_SUCCEEDED(r_key.SetValue(kValNameBinary,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200268 reinterpret_cast<const uint8_t*>(kBinaryVal),
269 sizeof(kBinaryVal) - 1));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000270
271 // check that the value exists
272 EXPECT_TRUE(r_key.HasValue(kValNameBinary));
273
274 // read it back
275 EXPECT_SUCCEEDED(r_key.GetValue(kValNameBinary, &binary_val, &uint8_count));
276 EXPECT_TRUE(memcmp(binary_val, kBinaryVal, sizeof(kBinaryVal) - 1) == 0);
277 delete[] binary_val;
278
279 // set it again
280 EXPECT_SUCCEEDED(r_key.SetValue(kValNameBinary,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200281 reinterpret_cast<const uint8_t*>(kBinaryVal2),
282 sizeof(kBinaryVal) - 1));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000283
284 // read it again
285 EXPECT_SUCCEEDED(r_key.GetValue(kValNameBinary, &binary_val, &uint8_count));
286 EXPECT_TRUE(memcmp(binary_val, kBinaryVal2, sizeof(kBinaryVal2) - 1) == 0);
287 delete[] binary_val;
288
289 // delete the value
290 EXPECT_SUCCEEDED(r_key.DeleteValue(kValNameBinary));
291
292 // check that the value is gone
293 EXPECT_FALSE(r_key.HasValue(kValNameBinary));
294
295 // set some values and check the total count
296
297 // set an INT 32
298 EXPECT_SUCCEEDED(r_key.SetValue(kValNameInt, kIntVal));
299
300 // set an INT 64
301 EXPECT_SUCCEEDED(r_key.SetValue(kValNameInt64, kIntVal64));
302
303 // set a string
304 EXPECT_SUCCEEDED(r_key.SetValue(kValNameStr, kStrVal));
305
306 // set a binary value
307 EXPECT_SUCCEEDED(r_key.SetValue(kValNameBinary,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200308 reinterpret_cast<const uint8_t*>(kBinaryVal),
309 sizeof(kBinaryVal) - 1));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000310
311 // get the value count
Peter Boström0c4e06b2015-10-07 12:23:21 +0200312 uint32_t value_count = r_key.GetValueCount();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000313 EXPECT_EQ(value_count, 4);
314
315 // check the value names
316 std::wstring value_name;
317 DWORD type = 0;
318
319 EXPECT_SUCCEEDED(r_key.GetValueNameAt(0, &value_name, &type));
320 EXPECT_STREQ(value_name.c_str(), kValNameInt);
321 EXPECT_EQ(type, REG_DWORD);
322
323 EXPECT_SUCCEEDED(r_key.GetValueNameAt(1, &value_name, &type));
324 EXPECT_STREQ(value_name.c_str(), kValNameInt64);
325 EXPECT_EQ(type, REG_QWORD);
326
327 EXPECT_SUCCEEDED(r_key.GetValueNameAt(2, &value_name, &type));
328 EXPECT_STREQ(value_name.c_str(), kValNameStr);
329 EXPECT_EQ(type, REG_SZ);
330
331 EXPECT_SUCCEEDED(r_key.GetValueNameAt(3, &value_name, &type));
332 EXPECT_STREQ(value_name.c_str(), kValNameBinary);
333 EXPECT_EQ(type, REG_BINARY);
334
335 // check that there are no more values
336 EXPECT_FAILED(r_key.GetValueNameAt(4, &value_name, &type));
337
Peter Boström0c4e06b2015-10-07 12:23:21 +0200338 uint32_t subkey_count = r_key.GetSubkeyCount();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000339 EXPECT_EQ(subkey_count, 0);
340
341 // now create a subkey and make sure we can get the name
342 RegKey temp_key;
343 EXPECT_SUCCEEDED(temp_key.Create(HKEY_CURRENT_USER, kRkey1Subkey));
344
345 // check the subkey exists
346 EXPECT_TRUE(r_key.HasSubkey(kRkey1SubkeyName));
347
348 // check the name
349 EXPECT_EQ(r_key.GetSubkeyCount(), 1);
350
351 std::wstring subkey_name;
352 EXPECT_SUCCEEDED(r_key.GetSubkeyNameAt(0, &subkey_name));
353 EXPECT_STREQ(subkey_name.c_str(), kRkey1SubkeyName);
354
355 // delete the key
356 EXPECT_SUCCEEDED(r_key.DeleteSubKey(kRkey1));
357
358 // close this key
359 EXPECT_SUCCEEDED(r_key.Close());
360
361 // whack the whole key
362 EXPECT_SUCCEEDED(RegKey::DeleteKey(kFullRkey1));
363}
364
pbos@webrtc.orgf7a58932015-01-14 09:03:16 +0000365void RegKeyStaticFunctionsTest() {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000366 DWORD int_val = 0;
367 DWORD64 int64_val = 0;
368 float float_val = 0;
369 double double_val = 0;
370 wchar_t* str_val = NULL;
371 std::wstring wstr_val;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200372 uint8_t* binary_val = NULL;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000373 DWORD uint8_count = 0;
374
375 // Just in case...
376 // make sure the no test key residue is left from previous aborted runs
377 RegKey::DeleteKey(kFullRkey1);
378
379 // get an in-existent value from an un-existent key
380 EXPECT_EQ(RegKey::GetValue(kFullRkey1, kValNameInt, &int_val),
381 HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND));
382
Peter Boström0c4e06b2015-10-07 12:23:21 +0200383 // set int32_t
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000384 EXPECT_SUCCEEDED(RegKey::SetValue(kFullRkey1, kValNameInt, kIntVal));
385
386 // check that the value exists
387 EXPECT_TRUE(RegKey::HasValue(kFullRkey1, kValNameInt));
388
389 // get an in-existent value from an existent key
390 EXPECT_EQ(RegKey::GetValue(kFullRkey1, L"bogus", &int_val),
391 HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND));
392
393 // read it back
394 EXPECT_SUCCEEDED(RegKey::GetValue(kFullRkey1, kValNameInt, &int_val));
395 EXPECT_EQ(int_val, kIntVal);
396
397 // delete the value
398 EXPECT_SUCCEEDED(RegKey::DeleteValue(kFullRkey1, kValNameInt));
399
400 // check that the value is gone
401 EXPECT_FALSE(RegKey::HasValue(kFullRkey1, kValNameInt));
402
Peter Boström0c4e06b2015-10-07 12:23:21 +0200403 // set int64_t
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000404 EXPECT_SUCCEEDED(RegKey::SetValue(kFullRkey1, kValNameInt64, kIntVal64));
405
406 // check that the value exists
407 EXPECT_TRUE(RegKey::HasValue(kFullRkey1, kValNameInt64));
408
409 // read it back
410 EXPECT_SUCCEEDED(RegKey::GetValue(kFullRkey1, kValNameInt64, &int64_val));
411 EXPECT_EQ(int64_val, kIntVal64);
412
413 // delete the value
414 EXPECT_SUCCEEDED(RegKey::DeleteValue(kFullRkey1, kValNameInt64));
415
416 // check that the value is gone
417 EXPECT_FALSE(RegKey::HasValue(kFullRkey1, kValNameInt64));
418
419 // set float
420 EXPECT_SUCCEEDED(RegKey::SetValue(kFullRkey1, kValNameFloat, kFloatVal));
421
422 // check that the value exists
423 EXPECT_TRUE(RegKey::HasValue(kFullRkey1, kValNameFloat));
424
425 // read it back
426 EXPECT_SUCCEEDED(RegKey::GetValue(kFullRkey1, kValNameFloat, &float_val));
427 EXPECT_EQ(float_val, kFloatVal);
428
429 // delete the value
430 EXPECT_SUCCEEDED(RegKey::DeleteValue(kFullRkey1, kValNameFloat));
431
432 // check that the value is gone
433 EXPECT_FALSE(RegKey::HasValue(kFullRkey1, kValNameFloat));
434 EXPECT_FAILED(RegKey::GetValue(kFullRkey1, kValNameFloat, &float_val));
435
436 // set double
437 EXPECT_SUCCEEDED(RegKey::SetValue(kFullRkey1, kValNameDouble, kDoubleVal));
438
439 // check that the value exists
440 EXPECT_TRUE(RegKey::HasValue(kFullRkey1, kValNameDouble));
441
442 // read it back
443 EXPECT_SUCCEEDED(RegKey::GetValue(kFullRkey1, kValNameDouble, &double_val));
444 EXPECT_EQ(double_val, kDoubleVal);
445
446 // delete the value
447 EXPECT_SUCCEEDED(RegKey::DeleteValue(kFullRkey1, kValNameDouble));
448
449 // check that the value is gone
450 EXPECT_FALSE(RegKey::HasValue(kFullRkey1, kValNameDouble));
451 EXPECT_FAILED(RegKey::GetValue(kFullRkey1, kValNameDouble, &double_val));
452
453 // set string
454 EXPECT_SUCCEEDED(RegKey::SetValue(kFullRkey1, kValNameStr, kStrVal));
455
456 // check that the value exists
457 EXPECT_TRUE(RegKey::HasValue(kFullRkey1, kValNameStr));
458
459 // read it back
460 EXPECT_SUCCEEDED(RegKey::GetValue(kFullRkey1, kValNameStr, &str_val));
461 EXPECT_TRUE(lstrcmp(str_val, kStrVal) == 0);
462 delete[] str_val;
463
464 // read it back in std::wstring
465 EXPECT_SUCCEEDED(RegKey::GetValue(kFullRkey1, kValNameStr, &wstr_val));
466 EXPECT_STREQ(wstr_val.c_str(), kStrVal);
467
468 // get an in-existent value from an existent key
469 EXPECT_EQ(RegKey::GetValue(kFullRkey1, L"bogus", &str_val),
470 HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND));
471
472 // delete the value
473 EXPECT_SUCCEEDED(RegKey::DeleteValue(kFullRkey1, kValNameStr));
474
475 // check that the value is gone
476 EXPECT_FALSE(RegKey::HasValue(kFullRkey1, kValNameStr));
477
478 // set binary
Peter Boström0c4e06b2015-10-07 12:23:21 +0200479 EXPECT_SUCCEEDED(RegKey::SetValue(
480 kFullRkey1, kValNameBinary, reinterpret_cast<const uint8_t*>(kBinaryVal),
481 sizeof(kBinaryVal) - 1));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000482
483 // check that the value exists
484 EXPECT_TRUE(RegKey::HasValue(kFullRkey1, kValNameBinary));
485
486 // read it back
487 EXPECT_SUCCEEDED(RegKey::GetValue(kFullRkey1, kValNameBinary,
488 &binary_val, &uint8_count));
489 EXPECT_TRUE(memcmp(binary_val, kBinaryVal, sizeof(kBinaryVal)-1) == 0);
490 delete[] binary_val;
491
492 // delete the value
493 EXPECT_SUCCEEDED(RegKey::DeleteValue(kFullRkey1, kValNameBinary));
494
495 // check that the value is gone
496 EXPECT_FALSE(RegKey::HasValue(kFullRkey1, kValNameBinary));
497
498 // special case - set a binary value with length 0
Peter Boström0c4e06b2015-10-07 12:23:21 +0200499 EXPECT_SUCCEEDED(
500 RegKey::SetValue(kFullRkey1, kValNameBinary,
501 reinterpret_cast<const uint8_t*>(kBinaryVal), 0));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000502
503 // check that the value exists
504 EXPECT_TRUE(RegKey::HasValue(kFullRkey1, kValNameBinary));
505
506 // read it back
507 EXPECT_SUCCEEDED(RegKey::GetValue(kFullRkey1, kValNameBinary,
508 &binary_val, &uint8_count));
509 EXPECT_EQ(uint8_count, 0);
510 EXPECT_TRUE(binary_val == NULL);
511 delete[] binary_val;
512
513 // delete the value
514 EXPECT_SUCCEEDED(RegKey::DeleteValue(kFullRkey1, kValNameBinary));
515
516 // check that the value is gone
517 EXPECT_FALSE(RegKey::HasValue(kFullRkey1, kValNameBinary));
518
519 // special case - set a NULL binary value
520 EXPECT_SUCCEEDED(RegKey::SetValue(kFullRkey1, kValNameBinary, NULL, 100));
521
522 // check that the value exists
523 EXPECT_TRUE(RegKey::HasValue(kFullRkey1, kValNameBinary));
524
525 // read it back
526 EXPECT_SUCCEEDED(RegKey::GetValue(kFullRkey1, kValNameBinary,
527 &binary_val, &uint8_count));
528 EXPECT_EQ(uint8_count, 0);
529 EXPECT_TRUE(binary_val == NULL);
530 delete[] binary_val;
531
532 // delete the value
533 EXPECT_SUCCEEDED(RegKey::DeleteValue(kFullRkey1, kValNameBinary));
534
535 // check that the value is gone
536 EXPECT_FALSE(RegKey::HasValue(kFullRkey1, kValNameBinary));
537
538 // test read/write REG_MULTI_SZ value
539 std::vector<std::wstring> result;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200540 EXPECT_SUCCEEDED(RegKey::SetValueMultiSZ(
541 kFullRkey1, kValNameMultiStr, reinterpret_cast<const uint8_t*>(kMultiSZ),
542 sizeof(kMultiSZ)));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000543 EXPECT_SUCCEEDED(RegKey::GetValue(kFullRkey1, kValNameMultiStr, &result));
544 EXPECT_EQ(result.size(), 3);
545 EXPECT_STREQ(result[0].c_str(), L"abc");
546 EXPECT_STREQ(result[1].c_str(), L"def");
547 EXPECT_STREQ(result[2].c_str(), L"P12345");
Peter Boström0c4e06b2015-10-07 12:23:21 +0200548 EXPECT_SUCCEEDED(RegKey::SetValueMultiSZ(
549 kFullRkey1, kValNameMultiStr,
550 reinterpret_cast<const uint8_t*>(kEmptyMultiSZ), sizeof(kEmptyMultiSZ)));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000551 EXPECT_SUCCEEDED(RegKey::GetValue(kFullRkey1, kValNameMultiStr, &result));
552 EXPECT_EQ(result.size(), 0);
553 // writing REG_MULTI_SZ value will automatically add ending null characters
Peter Boström0c4e06b2015-10-07 12:23:21 +0200554 EXPECT_SUCCEEDED(
555 RegKey::SetValueMultiSZ(kFullRkey1, kValNameMultiStr,
556 reinterpret_cast<const uint8_t*>(kInvalidMultiSZ),
557 sizeof(kInvalidMultiSZ)));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000558 EXPECT_SUCCEEDED(RegKey::GetValue(kFullRkey1, kValNameMultiStr, &result));
559 EXPECT_EQ(result.size(), 1);
560 EXPECT_STREQ(result[0].c_str(), L"678");
561
562 // Run the following test only in dev machine
563 // This is because the build machine might not have admin privilege
564#ifdef IS_PRIVATE_BUILD
565 // get a temp file name
566 wchar_t temp_path[MAX_PATH] = {0};
567 EXPECT_LT(::GetTempPath(ARRAY_SIZE(temp_path), temp_path),
568 static_cast<DWORD>(ARRAY_SIZE(temp_path)));
569 wchar_t temp_file[MAX_PATH] = {0};
570 EXPECT_NE(::GetTempFileName(temp_path, L"rkut_",
571 ::GetTickCount(), temp_file), 0);
572
573 // test save
574 EXPECT_SUCCEEDED(RegKey::SetValue(kFullRkey1Subkey, kValNameInt, kIntVal));
575 EXPECT_SUCCEEDED(RegKey::SetValue(kFullRkey1Subkey, kValNameInt64, kIntVal64));
576 EXPECT_SUCCEEDED(RegKey::Save(kFullRkey1Subkey, temp_file));
577 EXPECT_SUCCEEDED(RegKey::DeleteValue(kFullRkey1Subkey, kValNameInt));
578 EXPECT_SUCCEEDED(RegKey::DeleteValue(kFullRkey1Subkey, kValNameInt64));
579
580 // test restore
581 EXPECT_SUCCEEDED(RegKey::Restore(kFullRkey1Subkey, temp_file));
582 int_val = 0;
583 EXPECT_SUCCEEDED(RegKey::GetValue(kFullRkey1Subkey, kValNameInt, &int_val));
584 EXPECT_EQ(int_val, kIntVal);
585 int64_val = 0;
586 EXPECT_SUCCEEDED(RegKey::GetValue(kFullRkey1Subkey,
587 kValNameInt64,
588 &int64_val));
589 EXPECT_EQ(int64_val, kIntVal64);
590
591 // delete the temp file
592 EXPECT_EQ(TRUE, ::DeleteFile(temp_file));
593#endif
594
595 // whack the whole key
596 EXPECT_SUCCEEDED(RegKey::DeleteKey(kFullRkey1));
597}
598
pbos@webrtc.orgf7a58932015-01-14 09:03:16 +0000599// Run both tests under the same test target. Because they access (read and
600// write) the same registry keys they can't run in parallel with eachother.
601TEST(RegKeyTest, RegKeyFunctionsTest) {
602 RegKeyNonStaticFunctionsTest();
603 RegKeyStaticFunctionsTest();
604}
605
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000606} // namespace rtc