Delete unused class rtc::RegKey.

BUG=webrtc:6424

Review-Url: https://codereview.webrtc.org/2571763002
Cr-Commit-Position: refs/heads/master@{#15592}
This commit is contained in:
nisse 2016-12-14 00:30:27 -08:00 committed by Commit bot
parent e5dc62aeb8
commit db397429d4
5 changed files with 0 additions and 2055 deletions

View File

@ -478,7 +478,6 @@ if (rtc_include_tests) {
if (is_win) {
sources += [
"base/win32_unittest.cc",
"base/win32regkey_unittest.cc",
"base/win32window_unittest.cc",
]
}

View File

@ -563,8 +563,6 @@ rtc_static_library("rtc_base") {
sources += [
"diskcache_win32.cc",
"diskcache_win32.h",
"win32regkey.cc",
"win32regkey.h",
"win32socketinit.cc",
"win32socketinit.h",
"win32socketserver.cc",

File diff suppressed because it is too large Load Diff

View File

@ -1,339 +0,0 @@
/*
* Copyright 2003 The WebRTC Project Authors. All rights reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
// Registry configuration wrappers class
//
// Offers static functions for convenient
// fast access for individual values
//
// Also provides a wrapper class for efficient
// batch operations on values of a given registry key.
//
#ifndef WEBRTC_BASE_WIN32REGKEY_H_
#define WEBRTC_BASE_WIN32REGKEY_H_
#include <string>
#include <vector>
#include "webrtc/base/basictypes.h"
#include "webrtc/base/constructormagic.h"
#include "webrtc/base/win32.h"
namespace rtc {
// maximum sizes registry key and value names
const int kMaxKeyNameChars = 255 + 1;
const int kMaxValueNameChars = 16383 + 1;
class RegKey {
public:
// constructor
RegKey();
// destructor
~RegKey();
// create a reg key
HRESULT Create(HKEY parent_key, const wchar_t* key_name);
HRESULT Create(HKEY parent_key,
const wchar_t* key_name,
wchar_t* reg_class,
DWORD options,
REGSAM sam_desired,
LPSECURITY_ATTRIBUTES lp_sec_attr,
LPDWORD lp_disposition);
// open an existing reg key
HRESULT Open(HKEY parent_key, const wchar_t* key_name);
HRESULT Open(HKEY parent_key, const wchar_t* key_name, REGSAM sam_desired);
// close this reg key
HRESULT Close();
// check if the key has a specified value
bool HasValue(const wchar_t* value_name) const;
// get the number of values for this key
uint32_t GetValueCount();
// Called to get the value name for the given value name index
// Use GetValueCount() to get the total value_name count for this key
// Returns failure if no key at the specified index
// If you modify the key while enumerating, the indexes will be out of order.
// Since the index order is not guaranteed, you need to reset your counting
// loop.
// 'type' refers to REG_DWORD, REG_QWORD, etc..
// 'type' can be NULL if not interested in the value type
HRESULT GetValueNameAt(int index, std::wstring* value_name, DWORD* type);
// check if the current key has the specified subkey
bool HasSubkey(const wchar_t* key_name) const;
// get the number of subkeys for this key
uint32_t GetSubkeyCount();
// Called to get the key name for the given key index
// Use GetSubkeyCount() to get the total count for this key
// Returns failure if no key at the specified index
// If you modify the key while enumerating, the indexes will be out of order.
// Since the index order is not guaranteed, you need to reset your counting
// loop.
HRESULT GetSubkeyNameAt(int index, std::wstring* key_name);
// SETTERS
// set an int32_t value - use when reading multiple values from a key
HRESULT SetValue(const wchar_t* value_name, DWORD value) const;
// set an int64_t value
HRESULT SetValue(const wchar_t* value_name, DWORD64 value) const;
// set a string value
HRESULT SetValue(const wchar_t* value_name, const wchar_t* value) const;
// set binary data
HRESULT SetValue(const wchar_t* value_name,
const uint8_t* value,
DWORD byte_count) const;
// set raw data, including type
HRESULT SetValue(const wchar_t* value_name,
const uint8_t* value,
DWORD byte_count,
DWORD type) const;
// GETTERS
// get an int32_t value
HRESULT GetValue(const wchar_t* value_name, DWORD* value) const;
// get an int64_t value
HRESULT GetValue(const wchar_t* value_name, DWORD64* value) const;
// get a string value - the caller must free the return buffer
HRESULT GetValue(const wchar_t* value_name, wchar_t** value) const;
// get a string value
HRESULT GetValue(const wchar_t* value_name, std::wstring* value) const;
// get a std::vector<std::wstring> value from REG_MULTI_SZ type
HRESULT GetValue(const wchar_t* value_name,
std::vector<std::wstring>* value) const;
// get binary data - the caller must free the return buffer
HRESULT GetValue(const wchar_t* value_name,
uint8_t** value,
DWORD* byte_count) const;
// get raw data, including type - the caller must free the return buffer
HRESULT GetValue(const wchar_t* value_name,
uint8_t** value,
DWORD* byte_count,
DWORD* type) const;
// STATIC VERSIONS
// flush
static HRESULT FlushKey(const wchar_t* full_key_name);
// check if a key exists
static bool HasKey(const wchar_t* full_key_name);
// check if the key has a specified value
static bool HasValue(const wchar_t* full_key_name, const wchar_t* value_name);
// SETTERS
// STATIC int32_t set
static HRESULT SetValue(const wchar_t* full_key_name,
const wchar_t* value_name,
DWORD value);
// STATIC int64_t set
static HRESULT SetValue(const wchar_t* full_key_name,
const wchar_t* value_name,
DWORD64 value);
// STATIC float set
static HRESULT SetValue(const wchar_t* full_key_name,
const wchar_t* value_name,
float value);
// STATIC double set
static HRESULT SetValue(const wchar_t* full_key_name,
const wchar_t* value_name,
double value);
// STATIC string set
static HRESULT SetValue(const wchar_t* full_key_name,
const wchar_t* value_name,
const wchar_t* value);
// STATIC binary data set
static HRESULT SetValue(const wchar_t* full_key_name,
const wchar_t* value_name,
const uint8_t* value,
DWORD byte_count);
// STATIC multi-string set
static HRESULT SetValueMultiSZ(const wchar_t* full_key_name,
const TCHAR* value_name,
const uint8_t* value,
DWORD byte_count);
// GETTERS
// STATIC int32_t get
static HRESULT GetValue(const wchar_t* full_key_name,
const wchar_t* value_name,
DWORD* value);
// STATIC int64_t get
//
// Note: if you are using time64 you should
// likely use GetLimitedTimeValue (util.h) instead of this method.
static HRESULT GetValue(const wchar_t* full_key_name,
const wchar_t* value_name,
DWORD64* value);
// STATIC float get
static HRESULT GetValue(const wchar_t* full_key_name,
const wchar_t* value_name,
float* value);
// STATIC double get
static HRESULT GetValue(const wchar_t* full_key_name,
const wchar_t* value_name,
double* value);
// STATIC string get
// Note: the caller must free the return buffer for wchar_t* version
static HRESULT GetValue(const wchar_t* full_key_name,
const wchar_t* value_name,
wchar_t** value);
static HRESULT GetValue(const wchar_t* full_key_name,
const wchar_t* value_name,
std::wstring* value);
// STATIC REG_MULTI_SZ get
static HRESULT GetValue(const wchar_t* full_key_name,
const wchar_t* value_name,
std::vector<std::wstring>* value);
// STATIC get binary data - the caller must free the return buffer
static HRESULT GetValue(const wchar_t* full_key_name,
const wchar_t* value_name,
uint8_t** value,
DWORD* byte_count);
// Get type of a registry value
static HRESULT GetValueType(const wchar_t* full_key_name,
const wchar_t* value_name,
DWORD* value_type);
// delete a subkey of the current key (with no subkeys)
HRESULT DeleteSubKey(const wchar_t* key_name);
// recursively delete a sub key of the current key (and all its subkeys)
HRESULT RecurseDeleteSubKey(const wchar_t* key_name);
// STATIC version of delete key - handles nested keys also
// delete a key and all its sub-keys recursively
// Returns S_FALSE if key didn't exist, S_OK if deletion was successful,
// and failure otherwise.
static HRESULT DeleteKey(const wchar_t* full_key_name);
// STATIC version of delete key
// delete a key recursively or non-recursively
// Returns S_FALSE if key didn't exist, S_OK if deletion was successful,
// and failure otherwise.
static HRESULT DeleteKey(const wchar_t* full_key_name, bool recursive);
// delete the specified value
HRESULT DeleteValue(const wchar_t* value_name);
// STATIC version of delete value
// Returns S_FALSE if key didn't exist, S_OK if deletion was successful,
// and failure otherwise.
static HRESULT DeleteValue(const wchar_t* full_key_name,
const wchar_t* value_name);
// Peek inside (use a RegKey as a smart wrapper around a registry handle)
HKEY key() { return h_key_; }
// helper function to get the HKEY and the root key from a string
// modifies the argument in place and returns the key name
// e.g. HKLM\\Software\\Google\... returns HKLM, "Software\\Google\..."
// Necessary for the static versions that use the full name of the reg key
static HKEY GetRootKeyInfo(std::wstring* full_key_name);
// Returns true if this key name is 'safe' for deletion (doesn't specify a key
// root)
static bool SafeKeyNameForDeletion(const wchar_t* key_name);
// save the key and all of its subkeys and values to a file
static HRESULT Save(const wchar_t* full_key_name, const wchar_t* file_name);
// restore the key and all of its subkeys and values which are saved into a
// file
static HRESULT Restore(const wchar_t* full_key_name,
const wchar_t* file_name);
// Is the key empty: having no sub-keys and values
static bool IsKeyEmpty(const wchar_t* full_key_name);
private:
// helper function to get any value from the registry
// used when the size of the data is unknown
HRESULT GetValueHelper(const wchar_t* value_name,
DWORD* type,
uint8_t** value,
DWORD* byte_count) const;
// helper function to get the parent key name and the subkey from a string
// modifies the argument in place and returns the key name
// Necessary for the static versions that use the full name of the reg key
static std::wstring GetParentKeyInfo(std::wstring* key_name);
// common SET Helper for the static case
static HRESULT SetValueStaticHelper(const wchar_t* full_key_name,
const wchar_t* value_name,
DWORD type,
LPVOID value,
DWORD byte_count = 0);
// common GET Helper for the static case
static HRESULT GetValueStaticHelper(const wchar_t* full_key_name,
const wchar_t* value_name,
DWORD type,
LPVOID value,
DWORD* byte_count = NULL);
// convert REG_MULTI_SZ bytes to string array
static HRESULT MultiSZBytesToStringArray(const uint8_t* buffer,
DWORD byte_count,
std::vector<std::wstring>* value);
// the HKEY for the current key
HKEY h_key_;
// for unittest
friend void RegKeyHelperFunctionsTest();
RTC_DISALLOW_COPY_AND_ASSIGN(RegKey);
};
} // namespace rtc
#endif // WEBRTC_BASE_WIN32REGKEY_H_

View File

@ -1,607 +0,0 @@
/*
* Copyright 2003 The WebRTC Project Authors. All rights reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
// Unittest for registry access API
#include "webrtc/base/arraysize.h"
#include "webrtc/base/gunit.h"
#include "webrtc/base/common.h"
#include "webrtc/base/win32regkey.h"
namespace rtc {
#ifndef EXPECT_SUCCEEDED
#define EXPECT_SUCCEEDED(x) EXPECT_TRUE(SUCCEEDED(x))
#endif
#ifndef EXPECT_FAILED
#define EXPECT_FAILED(x) EXPECT_TRUE(FAILED(x))
#endif
#define kBaseKey L"Software\\Google\\__TEST"
#define kSubkeyName L"subkey_test"
const wchar_t kRkey1[] = kBaseKey;
const wchar_t kRkey1SubkeyName[] = kSubkeyName;
const wchar_t kRkey1Subkey[] = kBaseKey L"\\" kSubkeyName;
const wchar_t kFullRkey1[] = L"HKCU\\" kBaseKey;
const wchar_t kFullRkey1Subkey[] = L"HKCU\\" kBaseKey L"\\" kSubkeyName;
const wchar_t kValNameInt[] = L"Int32 Value";
const DWORD kIntVal = 20;
const DWORD kIntVal2 = 30;
const wchar_t kValNameInt64[] = L"Int64 Value";
const DWORD64 kIntVal64 = 119600064000000000uI64;
const wchar_t kValNameFloat[] = L"Float Value";
const float kFloatVal = 12.3456789f;
const wchar_t kValNameDouble[] = L"Double Value";
const double kDoubleVal = 98.7654321;
const wchar_t kValNameStr[] = L"Str Value";
const wchar_t kStrVal[] = L"Some string data 1";
const wchar_t kStrVal2[] = L"Some string data 2";
const wchar_t kValNameBinary[] = L"Binary Value";
const char kBinaryVal[] = "Some binary data abcdefghi 1";
const char kBinaryVal2[] = "Some binary data abcdefghi 2";
const wchar_t kValNameMultiStr[] = L"MultiStr Value";
const wchar_t kMultiSZ[] = L"abc\0def\0P12345\0";
const wchar_t kEmptyMultiSZ[] = L"";
const wchar_t kInvalidMultiSZ[] = {L'6', L'7', L'8'};
// friend function of RegKey
void RegKeyHelperFunctionsTest() {
// Try out some dud values
std::wstring temp_key = L"";
EXPECT_TRUE(RegKey::GetRootKeyInfo(&temp_key) == NULL);
EXPECT_STREQ(temp_key.c_str(), L"");
temp_key = L"a";
EXPECT_TRUE(RegKey::GetRootKeyInfo(&temp_key) == NULL);
EXPECT_STREQ(temp_key.c_str(), L"");
// The basics
temp_key = L"HKLM\\a";
EXPECT_EQ(RegKey::GetRootKeyInfo(&temp_key), HKEY_LOCAL_MACHINE);
EXPECT_STREQ(temp_key.c_str(), L"a");
temp_key = L"HKEY_LOCAL_MACHINE\\a";
EXPECT_EQ(RegKey::GetRootKeyInfo(&temp_key), HKEY_LOCAL_MACHINE);
EXPECT_STREQ(temp_key.c_str(), L"a");
temp_key = L"HKCU\\a";
EXPECT_EQ(RegKey::GetRootKeyInfo(&temp_key), HKEY_CURRENT_USER);
EXPECT_STREQ(temp_key.c_str(), L"a");
temp_key = L"HKEY_CURRENT_USER\\a";
EXPECT_EQ(RegKey::GetRootKeyInfo(&temp_key), HKEY_CURRENT_USER);
EXPECT_STREQ(temp_key.c_str(), L"a");
temp_key = L"HKU\\a";
EXPECT_EQ(RegKey::GetRootKeyInfo(&temp_key), HKEY_USERS);
EXPECT_STREQ(temp_key.c_str(), L"a");
temp_key = L"HKEY_USERS\\a";
EXPECT_EQ(RegKey::GetRootKeyInfo(&temp_key), HKEY_USERS);
EXPECT_STREQ(temp_key.c_str(), L"a");
temp_key = L"HKCR\\a";
EXPECT_EQ(RegKey::GetRootKeyInfo(&temp_key), HKEY_CLASSES_ROOT);
EXPECT_STREQ(temp_key.c_str(), L"a");
temp_key = L"HKEY_CLASSES_ROOT\\a";
EXPECT_EQ(RegKey::GetRootKeyInfo(&temp_key), HKEY_CLASSES_ROOT);
EXPECT_STREQ(temp_key.c_str(), L"a");
// Make sure it is case insensitive
temp_key = L"hkcr\\a";
EXPECT_EQ(RegKey::GetRootKeyInfo(&temp_key), HKEY_CLASSES_ROOT);
EXPECT_STREQ(temp_key.c_str(), L"a");
temp_key = L"hkey_CLASSES_ROOT\\a";
EXPECT_EQ(RegKey::GetRootKeyInfo(&temp_key), HKEY_CLASSES_ROOT);
EXPECT_STREQ(temp_key.c_str(), L"a");
//
// Test RegKey::GetParentKeyInfo
//
// dud cases
temp_key = L"";
EXPECT_STREQ(RegKey::GetParentKeyInfo(&temp_key).c_str(), L"");
EXPECT_STREQ(temp_key.c_str(), L"");
temp_key = L"a";
EXPECT_STREQ(RegKey::GetParentKeyInfo(&temp_key).c_str(), L"");
EXPECT_STREQ(temp_key.c_str(), L"a");
temp_key = L"a\\b";
EXPECT_STREQ(RegKey::GetParentKeyInfo(&temp_key).c_str(), L"a");
EXPECT_STREQ(temp_key.c_str(), L"b");
temp_key = L"\\b";
EXPECT_STREQ(RegKey::GetParentKeyInfo(&temp_key).c_str(), L"");
EXPECT_STREQ(temp_key.c_str(), L"b");
// Some regular cases
temp_key = L"HKEY_CLASSES_ROOT\\moon";
EXPECT_STREQ(RegKey::GetParentKeyInfo(&temp_key).c_str(),
L"HKEY_CLASSES_ROOT");
EXPECT_STREQ(temp_key.c_str(), L"moon");
temp_key = L"HKEY_CLASSES_ROOT\\moon\\doggy";
EXPECT_STREQ(RegKey::GetParentKeyInfo(&temp_key).c_str(),
L"HKEY_CLASSES_ROOT\\moon");
EXPECT_STREQ(temp_key.c_str(), L"doggy");
//
// Test MultiSZBytesToStringArray
//
std::vector<std::wstring> result;
EXPECT_SUCCEEDED(RegKey::MultiSZBytesToStringArray(
reinterpret_cast<const uint8_t*>(kMultiSZ), sizeof(kMultiSZ), &result));
EXPECT_EQ(result.size(), 3);
EXPECT_STREQ(result[0].c_str(), L"abc");
EXPECT_STREQ(result[1].c_str(), L"def");
EXPECT_STREQ(result[2].c_str(), L"P12345");
EXPECT_SUCCEEDED(RegKey::MultiSZBytesToStringArray(
reinterpret_cast<const uint8_t*>(kEmptyMultiSZ), sizeof(kEmptyMultiSZ),
&result));
EXPECT_EQ(result.size(), 0);
EXPECT_FALSE(SUCCEEDED(RegKey::MultiSZBytesToStringArray(
reinterpret_cast<const uint8_t*>(kInvalidMultiSZ),
sizeof(kInvalidMultiSZ), &result)));
}
TEST(RegKeyTest, RegKeyHelperFunctionsTest) {
RegKeyHelperFunctionsTest();
}
void RegKeyNonStaticFunctionsTest() {
DWORD int_val = 0;
DWORD64 int64_val = 0;
wchar_t* str_val = NULL;
uint8_t* binary_val = NULL;
DWORD uint8_count = 0;
// Just in case...
// make sure the no test key residue is left from previous aborted runs
RegKey::DeleteKey(kFullRkey1);
// initial state
RegKey r_key;
EXPECT_TRUE(r_key.key() == NULL);
// create a reg key
EXPECT_SUCCEEDED(r_key.Create(HKEY_CURRENT_USER, kRkey1));
// do the create twice - it should return the already created one
EXPECT_SUCCEEDED(r_key.Create(HKEY_CURRENT_USER, kRkey1));
// now do an open - should work just fine
EXPECT_SUCCEEDED(r_key.Open(HKEY_CURRENT_USER, kRkey1));
// get an in-existent value
EXPECT_EQ(r_key.GetValue(kValNameInt, &int_val),
HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND));
// set and get some values
// set an INT 32
EXPECT_SUCCEEDED(r_key.SetValue(kValNameInt, kIntVal));
// check that the value exists
EXPECT_TRUE(r_key.HasValue(kValNameInt));
// read it back
EXPECT_SUCCEEDED(r_key.GetValue(kValNameInt, &int_val));
EXPECT_EQ(int_val, kIntVal);
// set it again!
EXPECT_SUCCEEDED(r_key.SetValue(kValNameInt, kIntVal2));
// read it again
EXPECT_SUCCEEDED(r_key.GetValue(kValNameInt, &int_val));
EXPECT_EQ(int_val, kIntVal2);
// delete the value
EXPECT_SUCCEEDED(r_key.DeleteValue(kValNameInt));
// check that the value is gone
EXPECT_FALSE(r_key.HasValue(kValNameInt));
// set an INT 64
EXPECT_SUCCEEDED(r_key.SetValue(kValNameInt64, kIntVal64));
// check that the value exists
EXPECT_TRUE(r_key.HasValue(kValNameInt64));
// read it back
EXPECT_SUCCEEDED(r_key.GetValue(kValNameInt64, &int64_val));
EXPECT_EQ(int64_val, kIntVal64);
// delete the value
EXPECT_SUCCEEDED(r_key.DeleteValue(kValNameInt64));
// check that the value is gone
EXPECT_FALSE(r_key.HasValue(kValNameInt64));
// set a string
EXPECT_SUCCEEDED(r_key.SetValue(kValNameStr, kStrVal));
// check that the value exists
EXPECT_TRUE(r_key.HasValue(kValNameStr));
// read it back
EXPECT_SUCCEEDED(r_key.GetValue(kValNameStr, &str_val));
EXPECT_TRUE(lstrcmp(str_val, kStrVal) == 0);
delete[] str_val;
// set it again
EXPECT_SUCCEEDED(r_key.SetValue(kValNameStr, kStrVal2));
// read it again
EXPECT_SUCCEEDED(r_key.GetValue(kValNameStr, &str_val));
EXPECT_TRUE(lstrcmp(str_val, kStrVal2) == 0);
delete[] str_val;
// delete the value
EXPECT_SUCCEEDED(r_key.DeleteValue(kValNameStr));
// check that the value is gone
EXPECT_FALSE(r_key.HasValue(kValNameInt));
// set a binary value
EXPECT_SUCCEEDED(r_key.SetValue(kValNameBinary,
reinterpret_cast<const uint8_t*>(kBinaryVal),
sizeof(kBinaryVal) - 1));
// check that the value exists
EXPECT_TRUE(r_key.HasValue(kValNameBinary));
// read it back
EXPECT_SUCCEEDED(r_key.GetValue(kValNameBinary, &binary_val, &uint8_count));
EXPECT_TRUE(memcmp(binary_val, kBinaryVal, sizeof(kBinaryVal) - 1) == 0);
delete[] binary_val;
// set it again
EXPECT_SUCCEEDED(r_key.SetValue(kValNameBinary,
reinterpret_cast<const uint8_t*>(kBinaryVal2),
sizeof(kBinaryVal) - 1));
// read it again
EXPECT_SUCCEEDED(r_key.GetValue(kValNameBinary, &binary_val, &uint8_count));
EXPECT_TRUE(memcmp(binary_val, kBinaryVal2, sizeof(kBinaryVal2) - 1) == 0);
delete[] binary_val;
// delete the value
EXPECT_SUCCEEDED(r_key.DeleteValue(kValNameBinary));
// check that the value is gone
EXPECT_FALSE(r_key.HasValue(kValNameBinary));
// set some values and check the total count
// set an INT 32
EXPECT_SUCCEEDED(r_key.SetValue(kValNameInt, kIntVal));
// set an INT 64
EXPECT_SUCCEEDED(r_key.SetValue(kValNameInt64, kIntVal64));
// set a string
EXPECT_SUCCEEDED(r_key.SetValue(kValNameStr, kStrVal));
// set a binary value
EXPECT_SUCCEEDED(r_key.SetValue(kValNameBinary,
reinterpret_cast<const uint8_t*>(kBinaryVal),
sizeof(kBinaryVal) - 1));
// get the value count
uint32_t value_count = r_key.GetValueCount();
EXPECT_EQ(value_count, 4);
// check the value names
std::wstring value_name;
DWORD type = 0;
EXPECT_SUCCEEDED(r_key.GetValueNameAt(0, &value_name, &type));
EXPECT_STREQ(value_name.c_str(), kValNameInt);
EXPECT_EQ(type, REG_DWORD);
EXPECT_SUCCEEDED(r_key.GetValueNameAt(1, &value_name, &type));
EXPECT_STREQ(value_name.c_str(), kValNameInt64);
EXPECT_EQ(type, REG_QWORD);
EXPECT_SUCCEEDED(r_key.GetValueNameAt(2, &value_name, &type));
EXPECT_STREQ(value_name.c_str(), kValNameStr);
EXPECT_EQ(type, REG_SZ);
EXPECT_SUCCEEDED(r_key.GetValueNameAt(3, &value_name, &type));
EXPECT_STREQ(value_name.c_str(), kValNameBinary);
EXPECT_EQ(type, REG_BINARY);
// check that there are no more values
EXPECT_FAILED(r_key.GetValueNameAt(4, &value_name, &type));
uint32_t subkey_count = r_key.GetSubkeyCount();
EXPECT_EQ(subkey_count, 0);
// now create a subkey and make sure we can get the name
RegKey temp_key;
EXPECT_SUCCEEDED(temp_key.Create(HKEY_CURRENT_USER, kRkey1Subkey));
// check the subkey exists
EXPECT_TRUE(r_key.HasSubkey(kRkey1SubkeyName));
// check the name
EXPECT_EQ(r_key.GetSubkeyCount(), 1);
std::wstring subkey_name;
EXPECT_SUCCEEDED(r_key.GetSubkeyNameAt(0, &subkey_name));
EXPECT_STREQ(subkey_name.c_str(), kRkey1SubkeyName);
// delete the key
EXPECT_SUCCEEDED(r_key.DeleteSubKey(kRkey1));
// close this key
EXPECT_SUCCEEDED(r_key.Close());
// whack the whole key
EXPECT_SUCCEEDED(RegKey::DeleteKey(kFullRkey1));
}
void RegKeyStaticFunctionsTest() {
DWORD int_val = 0;
DWORD64 int64_val = 0;
float float_val = 0;
double double_val = 0;
wchar_t* str_val = NULL;
std::wstring wstr_val;
uint8_t* binary_val = NULL;
DWORD uint8_count = 0;
// Just in case...
// make sure the no test key residue is left from previous aborted runs
RegKey::DeleteKey(kFullRkey1);
// get an in-existent value from an un-existent key
EXPECT_EQ(RegKey::GetValue(kFullRkey1, kValNameInt, &int_val),
HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND));
// set int32_t
EXPECT_SUCCEEDED(RegKey::SetValue(kFullRkey1, kValNameInt, kIntVal));
// check that the value exists
EXPECT_TRUE(RegKey::HasValue(kFullRkey1, kValNameInt));
// get an in-existent value from an existent key
EXPECT_EQ(RegKey::GetValue(kFullRkey1, L"bogus", &int_val),
HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND));
// read it back
EXPECT_SUCCEEDED(RegKey::GetValue(kFullRkey1, kValNameInt, &int_val));
EXPECT_EQ(int_val, kIntVal);
// delete the value
EXPECT_SUCCEEDED(RegKey::DeleteValue(kFullRkey1, kValNameInt));
// check that the value is gone
EXPECT_FALSE(RegKey::HasValue(kFullRkey1, kValNameInt));
// set int64_t
EXPECT_SUCCEEDED(RegKey::SetValue(kFullRkey1, kValNameInt64, kIntVal64));
// check that the value exists
EXPECT_TRUE(RegKey::HasValue(kFullRkey1, kValNameInt64));
// read it back
EXPECT_SUCCEEDED(RegKey::GetValue(kFullRkey1, kValNameInt64, &int64_val));
EXPECT_EQ(int64_val, kIntVal64);
// delete the value
EXPECT_SUCCEEDED(RegKey::DeleteValue(kFullRkey1, kValNameInt64));
// check that the value is gone
EXPECT_FALSE(RegKey::HasValue(kFullRkey1, kValNameInt64));
// set float
EXPECT_SUCCEEDED(RegKey::SetValue(kFullRkey1, kValNameFloat, kFloatVal));
// check that the value exists
EXPECT_TRUE(RegKey::HasValue(kFullRkey1, kValNameFloat));
// read it back
EXPECT_SUCCEEDED(RegKey::GetValue(kFullRkey1, kValNameFloat, &float_val));
EXPECT_EQ(float_val, kFloatVal);
// delete the value
EXPECT_SUCCEEDED(RegKey::DeleteValue(kFullRkey1, kValNameFloat));
// check that the value is gone
EXPECT_FALSE(RegKey::HasValue(kFullRkey1, kValNameFloat));
EXPECT_FAILED(RegKey::GetValue(kFullRkey1, kValNameFloat, &float_val));
// set double
EXPECT_SUCCEEDED(RegKey::SetValue(kFullRkey1, kValNameDouble, kDoubleVal));
// check that the value exists
EXPECT_TRUE(RegKey::HasValue(kFullRkey1, kValNameDouble));
// read it back
EXPECT_SUCCEEDED(RegKey::GetValue(kFullRkey1, kValNameDouble, &double_val));
EXPECT_EQ(double_val, kDoubleVal);
// delete the value
EXPECT_SUCCEEDED(RegKey::DeleteValue(kFullRkey1, kValNameDouble));
// check that the value is gone
EXPECT_FALSE(RegKey::HasValue(kFullRkey1, kValNameDouble));
EXPECT_FAILED(RegKey::GetValue(kFullRkey1, kValNameDouble, &double_val));
// set string
EXPECT_SUCCEEDED(RegKey::SetValue(kFullRkey1, kValNameStr, kStrVal));
// check that the value exists
EXPECT_TRUE(RegKey::HasValue(kFullRkey1, kValNameStr));
// read it back
EXPECT_SUCCEEDED(RegKey::GetValue(kFullRkey1, kValNameStr, &str_val));
EXPECT_TRUE(lstrcmp(str_val, kStrVal) == 0);
delete[] str_val;
// read it back in std::wstring
EXPECT_SUCCEEDED(RegKey::GetValue(kFullRkey1, kValNameStr, &wstr_val));
EXPECT_STREQ(wstr_val.c_str(), kStrVal);
// get an in-existent value from an existent key
EXPECT_EQ(RegKey::GetValue(kFullRkey1, L"bogus", &str_val),
HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND));
// delete the value
EXPECT_SUCCEEDED(RegKey::DeleteValue(kFullRkey1, kValNameStr));
// check that the value is gone
EXPECT_FALSE(RegKey::HasValue(kFullRkey1, kValNameStr));
// set binary
EXPECT_SUCCEEDED(RegKey::SetValue(
kFullRkey1, kValNameBinary, reinterpret_cast<const uint8_t*>(kBinaryVal),
sizeof(kBinaryVal) - 1));
// check that the value exists
EXPECT_TRUE(RegKey::HasValue(kFullRkey1, kValNameBinary));
// read it back
EXPECT_SUCCEEDED(RegKey::GetValue(kFullRkey1, kValNameBinary,
&binary_val, &uint8_count));
EXPECT_TRUE(memcmp(binary_val, kBinaryVal, sizeof(kBinaryVal)-1) == 0);
delete[] binary_val;
// delete the value
EXPECT_SUCCEEDED(RegKey::DeleteValue(kFullRkey1, kValNameBinary));
// check that the value is gone
EXPECT_FALSE(RegKey::HasValue(kFullRkey1, kValNameBinary));
// special case - set a binary value with length 0
EXPECT_SUCCEEDED(
RegKey::SetValue(kFullRkey1, kValNameBinary,
reinterpret_cast<const uint8_t*>(kBinaryVal), 0));
// check that the value exists
EXPECT_TRUE(RegKey::HasValue(kFullRkey1, kValNameBinary));
// read it back
EXPECT_SUCCEEDED(RegKey::GetValue(kFullRkey1, kValNameBinary,
&binary_val, &uint8_count));
EXPECT_EQ(uint8_count, 0);
EXPECT_TRUE(binary_val == NULL);
delete[] binary_val;
// delete the value
EXPECT_SUCCEEDED(RegKey::DeleteValue(kFullRkey1, kValNameBinary));
// check that the value is gone
EXPECT_FALSE(RegKey::HasValue(kFullRkey1, kValNameBinary));
// special case - set a NULL binary value
EXPECT_SUCCEEDED(RegKey::SetValue(kFullRkey1, kValNameBinary, NULL, 100));
// check that the value exists
EXPECT_TRUE(RegKey::HasValue(kFullRkey1, kValNameBinary));
// read it back
EXPECT_SUCCEEDED(RegKey::GetValue(kFullRkey1, kValNameBinary,
&binary_val, &uint8_count));
EXPECT_EQ(uint8_count, 0);
EXPECT_TRUE(binary_val == NULL);
delete[] binary_val;
// delete the value
EXPECT_SUCCEEDED(RegKey::DeleteValue(kFullRkey1, kValNameBinary));
// check that the value is gone
EXPECT_FALSE(RegKey::HasValue(kFullRkey1, kValNameBinary));
// test read/write REG_MULTI_SZ value
std::vector<std::wstring> result;
EXPECT_SUCCEEDED(RegKey::SetValueMultiSZ(
kFullRkey1, kValNameMultiStr, reinterpret_cast<const uint8_t*>(kMultiSZ),
sizeof(kMultiSZ)));
EXPECT_SUCCEEDED(RegKey::GetValue(kFullRkey1, kValNameMultiStr, &result));
EXPECT_EQ(result.size(), 3);
EXPECT_STREQ(result[0].c_str(), L"abc");
EXPECT_STREQ(result[1].c_str(), L"def");
EXPECT_STREQ(result[2].c_str(), L"P12345");
EXPECT_SUCCEEDED(RegKey::SetValueMultiSZ(
kFullRkey1, kValNameMultiStr,
reinterpret_cast<const uint8_t*>(kEmptyMultiSZ), sizeof(kEmptyMultiSZ)));
EXPECT_SUCCEEDED(RegKey::GetValue(kFullRkey1, kValNameMultiStr, &result));
EXPECT_EQ(result.size(), 0);
// writing REG_MULTI_SZ value will automatically add ending null characters
EXPECT_SUCCEEDED(
RegKey::SetValueMultiSZ(kFullRkey1, kValNameMultiStr,
reinterpret_cast<const uint8_t*>(kInvalidMultiSZ),
sizeof(kInvalidMultiSZ)));
EXPECT_SUCCEEDED(RegKey::GetValue(kFullRkey1, kValNameMultiStr, &result));
EXPECT_EQ(result.size(), 1);
EXPECT_STREQ(result[0].c_str(), L"678");
// Run the following test only in dev machine
// This is because the build machine might not have admin privilege
#ifdef IS_PRIVATE_BUILD
// get a temp file name
wchar_t temp_path[MAX_PATH] = {0};
EXPECT_LT(::GetTempPath(arraysize(temp_path), temp_path),
static_cast<DWORD>(arraysize(temp_path)));
wchar_t temp_file[MAX_PATH] = {0};
EXPECT_NE(::GetTempFileName(temp_path, L"rkut_",
::GetTickCount(), temp_file), 0);
// test save
EXPECT_SUCCEEDED(RegKey::SetValue(kFullRkey1Subkey, kValNameInt, kIntVal));
EXPECT_SUCCEEDED(RegKey::SetValue(kFullRkey1Subkey, kValNameInt64, kIntVal64));
EXPECT_SUCCEEDED(RegKey::Save(kFullRkey1Subkey, temp_file));
EXPECT_SUCCEEDED(RegKey::DeleteValue(kFullRkey1Subkey, kValNameInt));
EXPECT_SUCCEEDED(RegKey::DeleteValue(kFullRkey1Subkey, kValNameInt64));
// test restore
EXPECT_SUCCEEDED(RegKey::Restore(kFullRkey1Subkey, temp_file));
int_val = 0;
EXPECT_SUCCEEDED(RegKey::GetValue(kFullRkey1Subkey, kValNameInt, &int_val));
EXPECT_EQ(int_val, kIntVal);
int64_val = 0;
EXPECT_SUCCEEDED(RegKey::GetValue(kFullRkey1Subkey,
kValNameInt64,
&int64_val));
EXPECT_EQ(int64_val, kIntVal64);
// delete the temp file
EXPECT_EQ(TRUE, ::DeleteFile(temp_file));
#endif
// whack the whole key
EXPECT_SUCCEEDED(RegKey::DeleteKey(kFullRkey1));
}
// Run both tests under the same test target. Because they access (read and
// write) the same registry keys they can't run in parallel with eachother.
TEST(RegKeyTest, RegKeyFunctionsTest) {
RegKeyNonStaticFunctionsTest();
RegKeyStaticFunctionsTest();
}
} // namespace rtc