diff --git a/webrtc/BUILD.gn b/webrtc/BUILD.gn index a45e2fc090..ea23e39eb6 100644 --- a/webrtc/BUILD.gn +++ b/webrtc/BUILD.gn @@ -478,7 +478,6 @@ if (rtc_include_tests) { if (is_win) { sources += [ "base/win32_unittest.cc", - "base/win32regkey_unittest.cc", "base/win32window_unittest.cc", ] } diff --git a/webrtc/base/BUILD.gn b/webrtc/base/BUILD.gn index 5f68157e76..adb639537f 100644 --- a/webrtc/base/BUILD.gn +++ b/webrtc/base/BUILD.gn @@ -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", diff --git a/webrtc/base/win32regkey.cc b/webrtc/base/win32regkey.cc deleted file mode 100644 index 447086aff5..0000000000 --- a/webrtc/base/win32regkey.cc +++ /dev/null @@ -1,1106 +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 wrapers class implementation -// -// Change made by S. Ganesh - ganesh@google.com: -// Use SHQueryValueEx instead of RegQueryValueEx throughout. -// A call to the SHLWAPI function is essentially a call to the standard -// function but with post-processing: -// * to fix REG_SZ or REG_EXPAND_SZ data that is not properly null-terminated; -// * to expand REG_EXPAND_SZ data. - -#include "webrtc/base/win32regkey.h" - -#include - -#include - -#include "webrtc/base/common.h" -#include "webrtc/base/logging.h" - -namespace rtc { - -RegKey::RegKey() { - h_key_ = NULL; -} - -RegKey::~RegKey() { - Close(); -} - -HRESULT RegKey::Create(HKEY parent_key, const wchar_t* key_name) { - return Create(parent_key, - key_name, - REG_NONE, - REG_OPTION_NON_VOLATILE, - KEY_ALL_ACCESS, - NULL, - NULL); -} - -HRESULT RegKey::Open(HKEY parent_key, const wchar_t* key_name) { - return Open(parent_key, key_name, KEY_ALL_ACCESS); -} - -bool RegKey::HasValue(const TCHAR* value_name) const { - return (ERROR_SUCCESS == ::RegQueryValueEx(h_key_, value_name, NULL, - NULL, NULL, NULL)); -} - -HRESULT RegKey::SetValue(const wchar_t* full_key_name, - const wchar_t* value_name, - DWORD value) { - ASSERT(full_key_name != NULL); - - return SetValueStaticHelper(full_key_name, value_name, REG_DWORD, &value); -} - -HRESULT RegKey::SetValue(const wchar_t* full_key_name, - const wchar_t* value_name, - DWORD64 value) { - ASSERT(full_key_name != NULL); - - return SetValueStaticHelper(full_key_name, value_name, REG_QWORD, &value); -} - -HRESULT RegKey::SetValue(const wchar_t* full_key_name, - const wchar_t* value_name, - float value) { - ASSERT(full_key_name != NULL); - - return SetValueStaticHelper(full_key_name, value_name, - REG_BINARY, &value, sizeof(value)); -} - -HRESULT RegKey::SetValue(const wchar_t* full_key_name, - const wchar_t* value_name, - double value) { - ASSERT(full_key_name != NULL); - - return SetValueStaticHelper(full_key_name, value_name, - REG_BINARY, &value, sizeof(value)); -} - -HRESULT RegKey::SetValue(const wchar_t* full_key_name, - const wchar_t* value_name, - const TCHAR* value) { - ASSERT(full_key_name != NULL); - ASSERT(value != NULL); - - return SetValueStaticHelper(full_key_name, value_name, - REG_SZ, const_cast(value)); -} - -HRESULT RegKey::SetValue(const wchar_t* full_key_name, - const wchar_t* value_name, - const uint8_t* value, - DWORD byte_count) { - ASSERT(full_key_name != NULL); - - return SetValueStaticHelper(full_key_name, value_name, REG_BINARY, - const_cast(value), byte_count); -} - -HRESULT RegKey::SetValueMultiSZ(const wchar_t* full_key_name, - const wchar_t* value_name, - const uint8_t* value, - DWORD byte_count) { - ASSERT(full_key_name != NULL); - - return SetValueStaticHelper(full_key_name, value_name, REG_MULTI_SZ, - const_cast(value), byte_count); -} - -HRESULT RegKey::GetValue(const wchar_t* full_key_name, - const wchar_t* value_name, - DWORD* value) { - ASSERT(full_key_name != NULL); - ASSERT(value != NULL); - - return GetValueStaticHelper(full_key_name, value_name, REG_DWORD, value); -} - -HRESULT RegKey::GetValue(const wchar_t* full_key_name, - const wchar_t* value_name, - DWORD64* value) { - ASSERT(full_key_name != NULL); - ASSERT(value != NULL); - - return GetValueStaticHelper(full_key_name, value_name, REG_QWORD, value); -} - -HRESULT RegKey::GetValue(const wchar_t* full_key_name, - const wchar_t* value_name, - float* value) { - ASSERT(value != NULL); - ASSERT(full_key_name != NULL); - - DWORD byte_count = 0; - byte* buffer_raw = nullptr; - HRESULT hr = GetValueStaticHelper(full_key_name, value_name, - REG_BINARY, &buffer_raw, &byte_count); - std::unique_ptr buffer(buffer_raw); - if (SUCCEEDED(hr)) { - ASSERT(byte_count == sizeof(*value)); - if (byte_count == sizeof(*value)) { - *value = *reinterpret_cast(buffer.get()); - } - } - return hr; -} - -HRESULT RegKey::GetValue(const wchar_t* full_key_name, - const wchar_t* value_name, - double* value) { - ASSERT(value != NULL); - ASSERT(full_key_name != NULL); - - DWORD byte_count = 0; - byte* buffer_raw = nullptr; - HRESULT hr = GetValueStaticHelper(full_key_name, value_name, - REG_BINARY, &buffer_raw, &byte_count); - std::unique_ptr buffer(buffer_raw); - if (SUCCEEDED(hr)) { - ASSERT(byte_count == sizeof(*value)); - if (byte_count == sizeof(*value)) { - *value = *reinterpret_cast(buffer.get()); - } - } - return hr; -} - -HRESULT RegKey::GetValue(const wchar_t* full_key_name, - const wchar_t* value_name, - wchar_t** value) { - ASSERT(full_key_name != NULL); - ASSERT(value != NULL); - - return GetValueStaticHelper(full_key_name, value_name, REG_SZ, value); -} - -HRESULT RegKey::GetValue(const wchar_t* full_key_name, - const wchar_t* value_name, - std::wstring* value) { - ASSERT(full_key_name != NULL); - ASSERT(value != NULL); - - wchar_t* buffer_raw = nullptr; - HRESULT hr = RegKey::GetValue(full_key_name, value_name, &buffer_raw); - std::unique_ptr buffer(buffer_raw); - if (SUCCEEDED(hr)) { - value->assign(buffer.get()); - } - return hr; -} - -HRESULT RegKey::GetValue(const wchar_t* full_key_name, - const wchar_t* value_name, - std::vector* value) { - ASSERT(full_key_name != NULL); - ASSERT(value != NULL); - - return GetValueStaticHelper(full_key_name, value_name, REG_MULTI_SZ, value); -} - -HRESULT RegKey::GetValue(const wchar_t* full_key_name, - const wchar_t* value_name, - uint8_t** value, - DWORD* byte_count) { - ASSERT(full_key_name != NULL); - ASSERT(value != NULL); - ASSERT(byte_count != NULL); - - return GetValueStaticHelper(full_key_name, value_name, - REG_BINARY, value, byte_count); -} - -HRESULT RegKey::DeleteSubKey(const wchar_t* key_name) { - ASSERT(key_name != NULL); - ASSERT(h_key_ != NULL); - - LONG res = ::RegDeleteKey(h_key_, key_name); - HRESULT hr = HRESULT_FROM_WIN32(res); - if (hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND) || - hr == HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND)) { - hr = S_FALSE; - } - return hr; -} - -HRESULT RegKey::DeleteValue(const wchar_t* value_name) { - ASSERT(h_key_ != NULL); - - LONG res = ::RegDeleteValue(h_key_, value_name); - HRESULT hr = HRESULT_FROM_WIN32(res); - if (hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND) || - hr == HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND)) { - hr = S_FALSE; - } - return hr; -} - -HRESULT RegKey::Close() { - HRESULT hr = S_OK; - if (h_key_ != NULL) { - LONG res = ::RegCloseKey(h_key_); - hr = HRESULT_FROM_WIN32(res); - h_key_ = NULL; - } - return hr; -} - -HRESULT RegKey::Create(HKEY parent_key, - const wchar_t* key_name, - wchar_t* lpszClass, - DWORD options, - REGSAM sam_desired, - LPSECURITY_ATTRIBUTES lpSecAttr, - LPDWORD lpdwDisposition) { - ASSERT(key_name != NULL); - ASSERT(parent_key != NULL); - - DWORD dw = 0; - HKEY h_key = NULL; - LONG res = ::RegCreateKeyEx(parent_key, key_name, 0, lpszClass, options, - sam_desired, lpSecAttr, &h_key, &dw); - HRESULT hr = HRESULT_FROM_WIN32(res); - - if (lpdwDisposition) { - *lpdwDisposition = dw; - } - - // we have to close the currently opened key - // before replacing it with the new one - if (hr == S_OK) { - hr = Close(); - ASSERT(hr == S_OK); - h_key_ = h_key; - } - return hr; -} - -HRESULT RegKey::Open(HKEY parent_key, - const wchar_t* key_name, - REGSAM sam_desired) { - ASSERT(key_name != NULL); - ASSERT(parent_key != NULL); - - HKEY h_key = NULL; - LONG res = ::RegOpenKeyEx(parent_key, key_name, 0, sam_desired, &h_key); - HRESULT hr = HRESULT_FROM_WIN32(res); - - // we have to close the currently opened key - // before replacing it with the new one - if (hr == S_OK) { - // close the currently opened key if any - hr = Close(); - ASSERT(hr == S_OK); - h_key_ = h_key; - } - return hr; -} - -// save the key and all of its subkeys and values to a file -HRESULT RegKey::Save(const wchar_t* full_key_name, const wchar_t* file_name) { - ASSERT(full_key_name != NULL); - ASSERT(file_name != NULL); - - std::wstring key_name(full_key_name); - HKEY h_key = GetRootKeyInfo(&key_name); - if (!h_key) { - return E_FAIL; - } - - RegKey key; - HRESULT hr = key.Open(h_key, key_name.c_str(), KEY_READ); - if (FAILED(hr)) { - return hr; - } - - AdjustCurrentProcessPrivilege(SE_BACKUP_NAME, true); - LONG res = ::RegSaveKey(key.h_key_, file_name, NULL); - AdjustCurrentProcessPrivilege(SE_BACKUP_NAME, false); - - return HRESULT_FROM_WIN32(res); -} - -// restore the key and all of its subkeys and values which are saved into a file -HRESULT RegKey::Restore(const wchar_t* full_key_name, - const wchar_t* file_name) { - ASSERT(full_key_name != NULL); - ASSERT(file_name != NULL); - - std::wstring key_name(full_key_name); - HKEY h_key = GetRootKeyInfo(&key_name); - if (!h_key) { - return E_FAIL; - } - - RegKey key; - HRESULT hr = key.Open(h_key, key_name.c_str(), KEY_WRITE); - if (FAILED(hr)) { - return hr; - } - - AdjustCurrentProcessPrivilege(SE_RESTORE_NAME, true); - LONG res = ::RegRestoreKey(key.h_key_, file_name, REG_FORCE_RESTORE); - AdjustCurrentProcessPrivilege(SE_RESTORE_NAME, false); - - return HRESULT_FROM_WIN32(res); -} - -// check if the current key has the specified subkey -bool RegKey::HasSubkey(const wchar_t* key_name) const { - ASSERT(key_name != NULL); - - RegKey key; - HRESULT hr = key.Open(h_key_, key_name, KEY_READ); - key.Close(); - return hr == S_OK; -} - -// static flush key -HRESULT RegKey::FlushKey(const wchar_t* full_key_name) { - ASSERT(full_key_name != NULL); - - HRESULT hr = HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND); - // get the root HKEY - std::wstring key_name(full_key_name); - HKEY h_key = GetRootKeyInfo(&key_name); - - if (h_key != NULL) { - LONG res = ::RegFlushKey(h_key); - hr = HRESULT_FROM_WIN32(res); - } - return hr; -} - -// static SET helper -HRESULT RegKey::SetValueStaticHelper(const wchar_t* full_key_name, - const wchar_t* value_name, - DWORD type, - LPVOID value, - DWORD byte_count) { - ASSERT(full_key_name != NULL); - - HRESULT hr = HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND); - // get the root HKEY - std::wstring key_name(full_key_name); - HKEY h_key = GetRootKeyInfo(&key_name); - - if (h_key != NULL) { - RegKey key; - hr = key.Create(h_key, key_name.c_str()); - if (hr == S_OK) { - switch (type) { - case REG_DWORD: - hr = key.SetValue(value_name, *(static_cast(value))); - break; - case REG_QWORD: - hr = key.SetValue(value_name, *(static_cast(value))); - break; - case REG_SZ: - hr = key.SetValue(value_name, static_cast(value)); - break; - case REG_BINARY: - hr = key.SetValue(value_name, static_cast(value), - byte_count); - break; - case REG_MULTI_SZ: - hr = key.SetValue(value_name, static_cast(value), - byte_count, type); - break; - default: - ASSERT(false); - hr = HRESULT_FROM_WIN32(ERROR_DATATYPE_MISMATCH); - break; - } - // close the key after writing - HRESULT temp_hr = key.Close(); - if (hr == S_OK) { - hr = temp_hr; - } - } - } - return hr; -} - -// static GET helper -HRESULT RegKey::GetValueStaticHelper(const wchar_t* full_key_name, - const wchar_t* value_name, - DWORD type, - LPVOID value, - DWORD* byte_count) { - ASSERT(full_key_name != NULL); - - HRESULT hr = HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND); - // get the root HKEY - std::wstring key_name(full_key_name); - HKEY h_key = GetRootKeyInfo(&key_name); - - if (h_key != NULL) { - RegKey key; - hr = key.Open(h_key, key_name.c_str(), KEY_READ); - if (hr == S_OK) { - switch (type) { - case REG_DWORD: - hr = key.GetValue(value_name, reinterpret_cast(value)); - break; - case REG_QWORD: - hr = key.GetValue(value_name, reinterpret_cast(value)); - break; - case REG_SZ: - hr = key.GetValue(value_name, reinterpret_cast(value)); - break; - case REG_MULTI_SZ: - hr = key.GetValue(value_name, reinterpret_cast< - std::vector*>(value)); - break; - case REG_BINARY: - hr = key.GetValue(value_name, reinterpret_cast(value), - byte_count); - break; - default: - ASSERT(false); - hr = HRESULT_FROM_WIN32(ERROR_DATATYPE_MISMATCH); - break; - } - // close the key after writing - HRESULT temp_hr = key.Close(); - if (hr == S_OK) { - hr = temp_hr; - } - } - } - return hr; -} - -// GET helper -HRESULT RegKey::GetValueHelper(const wchar_t* value_name, - DWORD* type, - uint8_t** value, - DWORD* byte_count) const { - ASSERT(byte_count != NULL); - ASSERT(value != NULL); - ASSERT(type != NULL); - - // init return buffer - *value = NULL; - - // get the size of the return data buffer - LONG res = ::SHQueryValueEx(h_key_, value_name, NULL, type, NULL, byte_count); - HRESULT hr = HRESULT_FROM_WIN32(res); - - if (hr == S_OK) { - // if the value length is 0, nothing to do - if (*byte_count != 0) { - // allocate the buffer - *value = new byte[*byte_count]; - ASSERT(*value != NULL); - - // make the call again to get the data - res = ::SHQueryValueEx(h_key_, value_name, NULL, - type, *value, byte_count); - hr = HRESULT_FROM_WIN32(res); - ASSERT(hr == S_OK); - } - } - return hr; -} - -// Int32 Get -HRESULT RegKey::GetValue(const wchar_t* value_name, DWORD* value) const { - ASSERT(value != NULL); - - DWORD type = 0; - DWORD byte_count = sizeof(DWORD); - LONG res = ::SHQueryValueEx(h_key_, value_name, NULL, &type, - value, &byte_count); - HRESULT hr = HRESULT_FROM_WIN32(res); - ASSERT((hr != S_OK) || (type == REG_DWORD)); - ASSERT((hr != S_OK) || (byte_count == sizeof(DWORD))); - return hr; -} - -// Int64 Get -HRESULT RegKey::GetValue(const wchar_t* value_name, DWORD64* value) const { - ASSERT(value != NULL); - - DWORD type = 0; - DWORD byte_count = sizeof(DWORD64); - LONG res = ::SHQueryValueEx(h_key_, value_name, NULL, &type, - value, &byte_count); - HRESULT hr = HRESULT_FROM_WIN32(res); - ASSERT((hr != S_OK) || (type == REG_QWORD)); - ASSERT((hr != S_OK) || (byte_count == sizeof(DWORD64))); - return hr; -} - -// String Get -HRESULT RegKey::GetValue(const wchar_t* value_name, wchar_t** value) const { - ASSERT(value != NULL); - - DWORD byte_count = 0; - DWORD type = 0; - - // first get the size of the string buffer - LONG res = ::SHQueryValueEx(h_key_, value_name, NULL, - &type, NULL, &byte_count); - HRESULT hr = HRESULT_FROM_WIN32(res); - - if (hr == S_OK) { - // allocate room for the string and a terminating \0 - *value = new wchar_t[(byte_count / sizeof(wchar_t)) + 1]; - - if ((*value) != NULL) { - if (byte_count != 0) { - // make the call again - res = ::SHQueryValueEx(h_key_, value_name, NULL, &type, - *value, &byte_count); - hr = HRESULT_FROM_WIN32(res); - } else { - (*value)[0] = L'\0'; - } - - ASSERT((hr != S_OK) || (type == REG_SZ) || - (type == REG_MULTI_SZ) || (type == REG_EXPAND_SZ)); - } else { - hr = E_OUTOFMEMORY; - } - } - - return hr; -} - -// get a string value -HRESULT RegKey::GetValue(const wchar_t* value_name, std::wstring* value) const { - ASSERT(value != NULL); - - DWORD byte_count = 0; - DWORD type = 0; - - // first get the size of the string buffer - LONG res = ::SHQueryValueEx(h_key_, value_name, NULL, - &type, NULL, &byte_count); - HRESULT hr = HRESULT_FROM_WIN32(res); - - if (hr == S_OK) { - if (byte_count != 0) { - // Allocate some memory and make the call again - value->resize(byte_count / sizeof(wchar_t) + 1); - res = ::SHQueryValueEx(h_key_, value_name, NULL, &type, - &value->at(0), &byte_count); - hr = HRESULT_FROM_WIN32(res); - value->resize(wcslen(value->data())); - } else { - value->clear(); - } - - ASSERT((hr != S_OK) || (type == REG_SZ) || - (type == REG_MULTI_SZ) || (type == REG_EXPAND_SZ)); - } - - return hr; -} - -// convert REG_MULTI_SZ bytes to string array -HRESULT RegKey::MultiSZBytesToStringArray(const uint8_t* buffer, - DWORD byte_count, - std::vector* value) { - ASSERT(buffer != NULL); - ASSERT(value != NULL); - - const wchar_t* data = reinterpret_cast(buffer); - DWORD data_len = byte_count / sizeof(wchar_t); - value->clear(); - if (data_len > 1) { - // must be terminated by two null characters - if (data[data_len - 1] != 0 || data[data_len - 2] != 0) { - return E_INVALIDARG; - } - - // put null-terminated strings into arrays - while (*data) { - std::wstring str(data); - value->push_back(str); - data += str.length() + 1; - } - } - return S_OK; -} - -// get a std::vector value from REG_MULTI_SZ type -HRESULT RegKey::GetValue(const wchar_t* value_name, - std::vector* value) const { - ASSERT(value != NULL); - - DWORD byte_count = 0; - DWORD type = 0; - uint8_t* buffer = 0; - - // first get the size of the buffer - HRESULT hr = GetValueHelper(value_name, &type, &buffer, &byte_count); - ASSERT((hr != S_OK) || (type == REG_MULTI_SZ)); - - if (SUCCEEDED(hr)) { - hr = MultiSZBytesToStringArray(buffer, byte_count, value); - } - - return hr; -} - -// Binary data Get -HRESULT RegKey::GetValue(const wchar_t* value_name, - uint8_t** value, - DWORD* byte_count) const { - ASSERT(byte_count != NULL); - ASSERT(value != NULL); - - DWORD type = 0; - HRESULT hr = GetValueHelper(value_name, &type, value, byte_count); - ASSERT((hr != S_OK) || (type == REG_MULTI_SZ) || (type == REG_BINARY)); - return hr; -} - -// Raw data get -HRESULT RegKey::GetValue(const wchar_t* value_name, - uint8_t** value, - DWORD* byte_count, - DWORD* type) const { - ASSERT(type != NULL); - ASSERT(byte_count != NULL); - ASSERT(value != NULL); - - return GetValueHelper(value_name, type, value, byte_count); -} - -// Int32 set -HRESULT RegKey::SetValue(const wchar_t* value_name, DWORD value) const { - ASSERT(h_key_ != NULL); - - LONG res = - ::RegSetValueEx(h_key_, value_name, NULL, REG_DWORD, - reinterpret_cast(&value), sizeof(DWORD)); - return HRESULT_FROM_WIN32(res); -} - -// Int64 set -HRESULT RegKey::SetValue(const wchar_t* value_name, DWORD64 value) const { - ASSERT(h_key_ != NULL); - - LONG res = ::RegSetValueEx(h_key_, value_name, NULL, REG_QWORD, - reinterpret_cast(&value), - sizeof(DWORD64)); - return HRESULT_FROM_WIN32(res); -} - -// String set -HRESULT RegKey::SetValue(const wchar_t* value_name, - const wchar_t* value) const { - ASSERT(value != NULL); - ASSERT(h_key_ != NULL); - - LONG res = ::RegSetValueEx(h_key_, value_name, NULL, REG_SZ, - reinterpret_cast(value), - (lstrlen(value) + 1) * sizeof(wchar_t)); - return HRESULT_FROM_WIN32(res); -} - -// Binary data set -HRESULT RegKey::SetValue(const wchar_t* value_name, - const uint8_t* value, - DWORD byte_count) const { - ASSERT(h_key_ != NULL); - - // special case - if 'value' is NULL make sure byte_count is zero - if (value == NULL) { - byte_count = 0; - } - - LONG res = ::RegSetValueEx(h_key_, value_name, NULL, - REG_BINARY, value, byte_count); - return HRESULT_FROM_WIN32(res); -} - -// Raw data set -HRESULT RegKey::SetValue(const wchar_t* value_name, - const uint8_t* value, - DWORD byte_count, - DWORD type) const { - ASSERT(value != NULL); - ASSERT(h_key_ != NULL); - - LONG res = ::RegSetValueEx(h_key_, value_name, NULL, type, value, byte_count); - return HRESULT_FROM_WIN32(res); -} - -bool RegKey::HasKey(const wchar_t* full_key_name) { - ASSERT(full_key_name != NULL); - - // get the root HKEY - std::wstring key_name(full_key_name); - HKEY h_key = GetRootKeyInfo(&key_name); - - if (h_key != NULL) { - RegKey key; - HRESULT hr = key.Open(h_key, key_name.c_str(), KEY_READ); - key.Close(); - return S_OK == hr; - } - return false; -} - -// static version of HasValue -bool RegKey::HasValue(const wchar_t* full_key_name, const wchar_t* value_name) { - ASSERT(full_key_name != NULL); - - bool has_value = false; - // get the root HKEY - std::wstring key_name(full_key_name); - HKEY h_key = GetRootKeyInfo(&key_name); - - if (h_key != NULL) { - RegKey key; - if (key.Open(h_key, key_name.c_str(), KEY_READ) == S_OK) { - has_value = key.HasValue(value_name); - key.Close(); - } - } - return has_value; -} - -HRESULT RegKey::GetValueType(const wchar_t* full_key_name, - const wchar_t* value_name, - DWORD* value_type) { - ASSERT(full_key_name != NULL); - ASSERT(value_type != NULL); - - *value_type = REG_NONE; - - std::wstring key_name(full_key_name); - HKEY h_key = GetRootKeyInfo(&key_name); - - RegKey key; - HRESULT hr = key.Open(h_key, key_name.c_str(), KEY_READ); - if (SUCCEEDED(hr)) { - LONG res = ::SHQueryValueEx(key.h_key_, value_name, NULL, value_type, - NULL, NULL); - if (res != ERROR_SUCCESS) { - hr = HRESULT_FROM_WIN32(res); - } - } - - return hr; -} - -HRESULT RegKey::DeleteKey(const wchar_t* full_key_name) { - ASSERT(full_key_name != NULL); - - return DeleteKey(full_key_name, true); -} - -HRESULT RegKey::DeleteKey(const wchar_t* full_key_name, bool recursively) { - ASSERT(full_key_name != NULL); - - // need to open the parent key first - // get the root HKEY - std::wstring key_name(full_key_name); - HKEY h_key = GetRootKeyInfo(&key_name); - - // get the parent key - std::wstring parent_key(GetParentKeyInfo(&key_name)); - - RegKey key; - HRESULT hr = key.Open(h_key, parent_key.c_str()); - - if (hr == S_OK) { - hr = recursively ? key.RecurseDeleteSubKey(key_name.c_str()) - : key.DeleteSubKey(key_name.c_str()); - } else if (hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND) || - hr == HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND)) { - hr = S_FALSE; - } - - key.Close(); - return hr; -} - -HRESULT RegKey::DeleteValue(const wchar_t* full_key_name, - const wchar_t* value_name) { - ASSERT(full_key_name != NULL); - - HRESULT hr = HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND); - // get the root HKEY - std::wstring key_name(full_key_name); - HKEY h_key = GetRootKeyInfo(&key_name); - - if (h_key != NULL) { - RegKey key; - hr = key.Open(h_key, key_name.c_str()); - if (hr == S_OK) { - hr = key.DeleteValue(value_name); - key.Close(); - } - } - return hr; -} - -HRESULT RegKey::RecurseDeleteSubKey(const wchar_t* key_name) { - ASSERT(key_name != NULL); - - RegKey key; - HRESULT hr = key.Open(h_key_, key_name); - - if (hr == S_OK) { - // enumerate all subkeys of this key and recursivelly delete them - FILETIME time = {0}; - wchar_t key_name_buf[kMaxKeyNameChars] = {0}; - DWORD key_name_buf_size = kMaxKeyNameChars; - while (hr == S_OK && - ::RegEnumKeyEx(key.h_key_, 0, key_name_buf, &key_name_buf_size, - NULL, NULL, NULL, &time) == ERROR_SUCCESS) { - hr = key.RecurseDeleteSubKey(key_name_buf); - - // restore the buffer size - key_name_buf_size = kMaxKeyNameChars; - } - // close the top key - key.Close(); - } - - if (hr == S_OK) { - // the key has no more children keys - // delete the key and all of its values - hr = DeleteSubKey(key_name); - } - - return hr; -} - -HKEY RegKey::GetRootKeyInfo(std::wstring* full_key_name) { - ASSERT(full_key_name != NULL); - - HKEY h_key = NULL; - // get the root HKEY - size_t index = full_key_name->find(L'\\'); - std::wstring root_key; - - if (index == -1) { - root_key = *full_key_name; - *full_key_name = L""; - } else { - root_key = full_key_name->substr(0, index); - *full_key_name = full_key_name->substr(index + 1, - full_key_name->length() - index - 1); - } - - for (std::wstring::iterator iter = root_key.begin(); - iter != root_key.end(); ++iter) { - *iter = toupper(*iter); - } - - if (!root_key.compare(L"HKLM") || - !root_key.compare(L"HKEY_LOCAL_MACHINE")) { - h_key = HKEY_LOCAL_MACHINE; - } else if (!root_key.compare(L"HKCU") || - !root_key.compare(L"HKEY_CURRENT_USER")) { - h_key = HKEY_CURRENT_USER; - } else if (!root_key.compare(L"HKU") || - !root_key.compare(L"HKEY_USERS")) { - h_key = HKEY_USERS; - } else if (!root_key.compare(L"HKCR") || - !root_key.compare(L"HKEY_CLASSES_ROOT")) { - h_key = HKEY_CLASSES_ROOT; - } - - return h_key; -} - - -// Returns true if this key name is 'safe' for deletion -// (doesn't specify a key root) -bool RegKey::SafeKeyNameForDeletion(const wchar_t* key_name) { - ASSERT(key_name != NULL); - std::wstring key(key_name); - - HKEY root_key = GetRootKeyInfo(&key); - - if (!root_key) { - key = key_name; - } - if (key.empty()) { - return false; - } - bool found_subkey = false, backslash_found = false; - for (size_t i = 0 ; i < key.length() ; ++i) { - if (key[i] == L'\\') { - backslash_found = true; - } else if (backslash_found) { - found_subkey = true; - break; - } - } - return (root_key == HKEY_USERS) ? found_subkey : true; -} - -std::wstring RegKey::GetParentKeyInfo(std::wstring* key_name) { - ASSERT(key_name != NULL); - - // get the parent key - size_t index = key_name->rfind(L'\\'); - std::wstring parent_key; - if (index == -1) { - parent_key = L""; - } else { - parent_key = key_name->substr(0, index); - *key_name = key_name->substr(index + 1, key_name->length() - index - 1); - } - - return parent_key; -} - -// get the number of values for this key -uint32_t RegKey::GetValueCount() { - DWORD num_values = 0; - - if (ERROR_SUCCESS != ::RegQueryInfoKey( - h_key_, // key handle - NULL, // buffer for class name - NULL, // size of class string - NULL, // reserved - NULL, // number of subkeys - NULL, // longest subkey size - NULL, // longest class string - &num_values, // number of values for this key - NULL, // longest value name - NULL, // longest value data - NULL, // security descriptor - NULL)) { // last write time - ASSERT(false); - } - return num_values; -} - -// Enumerators for the value_names for this key - -// 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 -HRESULT RegKey::GetValueNameAt(int index, std::wstring* value_name, - DWORD* type) { - ASSERT(value_name != NULL); - - LONG res = ERROR_SUCCESS; - wchar_t value_name_buf[kMaxValueNameChars] = {0}; - DWORD value_name_buf_size = kMaxValueNameChars; - res = ::RegEnumValue(h_key_, index, value_name_buf, &value_name_buf_size, - NULL, type, NULL, NULL); - - if (res == ERROR_SUCCESS) { - value_name->assign(value_name_buf); - } - - return HRESULT_FROM_WIN32(res); -} - -uint32_t RegKey::GetSubkeyCount() { - // number of values for key - DWORD num_subkeys = 0; - - if (ERROR_SUCCESS != ::RegQueryInfoKey( - h_key_, // key handle - NULL, // buffer for class name - NULL, // size of class string - NULL, // reserved - &num_subkeys, // number of subkeys - NULL, // longest subkey size - NULL, // longest class string - NULL, // number of values for this key - NULL, // longest value name - NULL, // longest value data - NULL, // security descriptor - NULL)) { // last write time - ASSERT(false); - } - return num_subkeys; -} - -HRESULT RegKey::GetSubkeyNameAt(int index, std::wstring* key_name) { - ASSERT(key_name != NULL); - - LONG res = ERROR_SUCCESS; - wchar_t key_name_buf[kMaxKeyNameChars] = {0}; - DWORD key_name_buf_size = kMaxKeyNameChars; - - res = ::RegEnumKeyEx(h_key_, index, key_name_buf, &key_name_buf_size, - NULL, NULL, NULL, NULL); - - if (res == ERROR_SUCCESS) { - key_name->assign(key_name_buf); - } - - return HRESULT_FROM_WIN32(res); -} - -// Is the key empty: having no sub-keys and values -bool RegKey::IsKeyEmpty(const wchar_t* full_key_name) { - ASSERT(full_key_name != NULL); - - bool is_empty = true; - - // Get the root HKEY - std::wstring key_name(full_key_name); - HKEY h_key = GetRootKeyInfo(&key_name); - - // Open the key to check - if (h_key != NULL) { - RegKey key; - HRESULT hr = key.Open(h_key, key_name.c_str(), KEY_READ); - if (SUCCEEDED(hr)) { - is_empty = key.GetSubkeyCount() == 0 && key.GetValueCount() == 0; - key.Close(); - } - } - - return is_empty; -} - -bool AdjustCurrentProcessPrivilege(const TCHAR* privilege, bool to_enable) { - ASSERT(privilege != NULL); - - bool ret = false; - HANDLE token; - if (::OpenProcessToken(::GetCurrentProcess(), - TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &token)) { - LUID luid; - memset(&luid, 0, sizeof(luid)); - if (::LookupPrivilegeValue(NULL, privilege, &luid)) { - TOKEN_PRIVILEGES privs; - privs.PrivilegeCount = 1; - privs.Privileges[0].Luid = luid; - privs.Privileges[0].Attributes = to_enable ? SE_PRIVILEGE_ENABLED : 0; - if (::AdjustTokenPrivileges(token, FALSE, &privs, 0, NULL, 0)) { - ret = true; - } else { - LOG_GLE(LS_ERROR) << "AdjustTokenPrivileges failed"; - } - } else { - LOG_GLE(LS_ERROR) << "LookupPrivilegeValue failed"; - } - CloseHandle(token); - } else { - LOG_GLE(LS_ERROR) << "OpenProcessToken(GetCurrentProcess) failed"; - } - - return ret; -} - -} // namespace rtc diff --git a/webrtc/base/win32regkey.h b/webrtc/base/win32regkey.h deleted file mode 100644 index d5c51b9b06..0000000000 --- a/webrtc/base/win32regkey.h +++ /dev/null @@ -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 -#include - -#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 value from REG_MULTI_SZ type - HRESULT GetValue(const wchar_t* value_name, - std::vector* 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* 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* 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_ diff --git a/webrtc/base/win32regkey_unittest.cc b/webrtc/base/win32regkey_unittest.cc deleted file mode 100644 index 1702ef741d..0000000000 --- a/webrtc/base/win32regkey_unittest.cc +++ /dev/null @@ -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 result; - EXPECT_SUCCEEDED(RegKey::MultiSZBytesToStringArray( - reinterpret_cast(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(kEmptyMultiSZ), sizeof(kEmptyMultiSZ), - &result)); - EXPECT_EQ(result.size(), 0); - EXPECT_FALSE(SUCCEEDED(RegKey::MultiSZBytesToStringArray( - reinterpret_cast(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(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(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(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(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(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 result; - EXPECT_SUCCEEDED(RegKey::SetValueMultiSZ( - kFullRkey1, kValNameMultiStr, reinterpret_cast(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(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(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(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