diff options
author | rpaquay@chromium.org <rpaquay@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-07-09 21:55:05 +0000 |
---|---|---|
committer | rpaquay@chromium.org <rpaquay@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-07-09 21:55:05 +0000 |
commit | a39509c5728540a5e923efb841367c710c70994e (patch) | |
tree | 8b2d1d92a69094bc9d5ebf29cee00ac17996f247 /device | |
parent | d56a92b4c7de24406aa0b0c6e98719ec3eba8b94 (diff) | |
download | chromium_src-a39509c5728540a5e923efb841367c710c70994e.zip chromium_src-a39509c5728540a5e923efb841367c710c70994e.tar.gz chromium_src-a39509c5728540a5e923efb841367c710c70994e.tar.bz2 |
Add unit tests for DeviceRegistryPropertyValue.
Review URL: https://codereview.chromium.org/376313002
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@282135 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'device')
-rw-r--r-- | device/bluetooth/bluetooth_low_energy_win.cc | 100 | ||||
-rw-r--r-- | device/bluetooth/bluetooth_low_energy_win.h | 32 | ||||
-rw-r--r-- | device/bluetooth/bluetooth_low_energy_win_unittest.cc | 24 |
3 files changed, 110 insertions, 46 deletions
diff --git a/device/bluetooth/bluetooth_low_energy_win.cc b/device/bluetooth/bluetooth_low_energy_win.cc index 7a3276f..38fc4a1 100644 --- a/device/bluetooth/bluetooth_low_energy_win.cc +++ b/device/bluetooth/bluetooth_low_energy_win.cc @@ -5,12 +5,13 @@ #include "device/bluetooth/bluetooth_low_energy_win.h" #include "base/logging.h" -#include "base/memory/scoped_ptr.h" #include "base/strings/sys_string_conversions.h" #include "base/win/windows_version.h" namespace { +using device::win::DeviceRegistryPropertyValue; + const char kPlatformNotSupported[] = "Bluetooth Low energy is only supported on Windows 8 and later."; const char kDeviceEnumError[] = "Error enumerating Bluetooth LE devices."; @@ -19,6 +20,7 @@ const char kDeviceInfoError[] = const char kDeviceAddressError[] = "Device instance ID value does not seem to contain a Bluetooth Adapter " "address."; +const char kDeviceFriendlyNameError[] = "Device name is not valid."; const char kInvalidBluetoothAddress[] = "Bluetooth address format is invalid."; // Like ScopedHandle but for HDEVINFO. Only use this on HDEVINFO returned from @@ -123,50 +125,6 @@ bool CheckExpectedLength(size_t actual_length, return true; } -// Represents a device registry property value -class DeviceRegistryPropertyValue { - public: - static scoped_ptr<DeviceRegistryPropertyValue> - Create(DWORD property_type, scoped_ptr<UINT8[]> value, size_t value_size) { - if (property_type == REG_SZ) { - // Ensure string is zero terminated. - size_t character_size = value_size / sizeof(WCHAR); - CHECK_EQ(character_size * sizeof(WCHAR), value_size); - CHECK_GE(character_size, 1u); - WCHAR* value_string = reinterpret_cast<WCHAR*>(value.get()); - value_string[character_size - 1] = 0; - } - return scoped_ptr<DeviceRegistryPropertyValue>( - new DeviceRegistryPropertyValue( - property_type, value.Pass(), value_size)); - } - - bool AsString(std::string* value, std::string* error) { - if (property_type_ != REG_SZ) { - *error = "Property is not a string"; - return false; - } - - WCHAR* value_string = reinterpret_cast<WCHAR*>(value_.get()); - *value = base::SysWideToUTF8(value_string); - return true; - } - - private: - DeviceRegistryPropertyValue(DWORD property_type, - scoped_ptr<UINT8[]> value, - size_t value_size) - : property_type_(property_type), - value_(value.Pass()), - value_size_(value_size) {} - - DWORD property_type_; - scoped_ptr<UINT8[]> value_; - size_t value_size_; - - DISALLOW_COPY_AND_ASSIGN(DeviceRegistryPropertyValue); -}; - bool CollectBluetoothLowEnergyDeviceRegistryProperty( const ScopedDeviceInfoSetHandle& device_info_handle, PSP_DEVINFO_DATA device_info_data, @@ -252,10 +210,12 @@ bool CollectDeviceFriendlyName( return false; } - if (!property_value->AsString(&device_info->friendly_name, error)) { + if (property_value->property_type() != REG_SZ) { + *error = kDeviceFriendlyNameError; return false; } + device_info->friendly_name = property_value->AsString(); return true; } @@ -420,6 +380,54 @@ HRESULT OpenBluetoothLowEnergyService(const GUID& service_guid, namespace device { namespace win { +// static +scoped_ptr<DeviceRegistryPropertyValue> DeviceRegistryPropertyValue::Create( + DWORD property_type, + scoped_ptr<uint8_t[]> value, + size_t value_size) { + switch (property_type) { + case REG_SZ: { + // Ensure string is zero terminated. + size_t character_size = value_size / sizeof(WCHAR); + CHECK_EQ(character_size * sizeof(WCHAR), value_size); + CHECK_GE(character_size, 1u); + WCHAR* value_string = reinterpret_cast<WCHAR*>(value.get()); + value_string[character_size - 1] = 0; + break; + } + case REG_DWORD: { + CHECK_EQ(value_size, sizeof(DWORD)); + break; + } + } + return scoped_ptr<DeviceRegistryPropertyValue>( + new DeviceRegistryPropertyValue(property_type, value.Pass(), value_size)); +} + +DeviceRegistryPropertyValue::DeviceRegistryPropertyValue( + DWORD property_type, + scoped_ptr<uint8_t[]> value, + size_t value_size) + : property_type_(property_type), + value_(value.Pass()), + value_size_(value_size) { +} + +DeviceRegistryPropertyValue::~DeviceRegistryPropertyValue() { +} + +std::string DeviceRegistryPropertyValue::AsString() const { + CHECK_EQ(property_type_, static_cast<DWORD>(REG_SZ)); + WCHAR* value_string = reinterpret_cast<WCHAR*>(value_.get()); + return base::SysWideToUTF8(value_string); +} + +DWORD DeviceRegistryPropertyValue::AsDWORD() const { + CHECK_EQ(property_type_, static_cast<DWORD>(REG_DWORD)); + DWORD* value = reinterpret_cast<DWORD*>(value_.get()); + return *value; +} + bool IsBluetoothLowEnergySupported() { return base::win::GetVersion() >= base::win::VERSION_WIN8; } diff --git a/device/bluetooth/bluetooth_low_energy_win.h b/device/bluetooth/bluetooth_low_energy_win.h index 6c997e9..c08db21 100644 --- a/device/bluetooth/bluetooth_low_energy_win.h +++ b/device/bluetooth/bluetooth_low_energy_win.h @@ -40,12 +40,44 @@ DEFINE_GUID(GUID_BLUETOOTHLE_DEVICE_INTERFACE, #include <bluetoothapis.h> #include "base/files/file_path.h" +#include "base/memory/scoped_ptr.h" #include "base/memory/scoped_vector.h" #include "base/win/scoped_handle.h" namespace device { namespace win { +// Represents a device registry property value +class DeviceRegistryPropertyValue { + public: + // Creates a property value instance, where |property_type| is one of REG_xxx + // registry value type (e.g. REG_SZ, REG_DWORD), |value| is a byte array + // containing the propery value and |value_size| is the number of bytes in + // |value|. Note the returned instance takes ownership of |value| array. + static scoped_ptr<DeviceRegistryPropertyValue> Create( + DWORD property_type, + scoped_ptr<uint8_t[]> value, + size_t value_size); + ~DeviceRegistryPropertyValue(); + + // Returns the vaue type a REG_xxx value (e.g. REG_SZ, REG_DWORD, ...) + DWORD property_type() const { return property_type_; } + + std::string AsString() const; + DWORD AsDWORD() const; + + private: + DeviceRegistryPropertyValue(DWORD property_type, + scoped_ptr<uint8_t[]> value, + size_t value_size); + + DWORD property_type_; + scoped_ptr<uint8_t[]> value_; + size_t value_size_; + + DISALLOW_COPY_AND_ASSIGN(DeviceRegistryPropertyValue); +}; + // Returns true only on Windows platforms supporting Bluetooth Low Energy. bool IsBluetoothLowEnergySupported(); diff --git a/device/bluetooth/bluetooth_low_energy_win_unittest.cc b/device/bluetooth/bluetooth_low_energy_win_unittest.cc index 2db9d20..94ff8cb 100644 --- a/device/bluetooth/bluetooth_low_energy_win_unittest.cc +++ b/device/bluetooth/bluetooth_low_energy_win_unittest.cc @@ -2,6 +2,7 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +#include "base/strings/sys_string_conversions.h" #include "device/bluetooth/bluetooth_low_energy_win.h" #include "testing/gtest/include/gtest/gtest.h" @@ -47,4 +48,27 @@ TEST_F(BluetoothLowEnergyWinTest, ExtractInvalidBluetoothAddress) { EXPECT_FALSE(error.empty()); } +TEST_F(BluetoothLowEnergyWinTest, DeviceRegistryPropertyValueAsString) { + std::string test_value = "String used for round trip test."; + std::wstring wide_value = base::SysUTF8ToWide(test_value); + size_t buffer_size = (wide_value.size() + 1) * sizeof(wchar_t); + scoped_ptr<uint8_t[]> buffer(new uint8_t[buffer_size]); + memcpy(buffer.get(), wide_value.c_str(), buffer_size); + scoped_ptr<device::win::DeviceRegistryPropertyValue> value = + device::win::DeviceRegistryPropertyValue::Create( + REG_SZ, buffer.Pass(), buffer_size).Pass(); + EXPECT_EQ(test_value, value->AsString()); +} + +TEST_F(BluetoothLowEnergyWinTest, DeviceRegistryPropertyValueAsDWORD) { + DWORD test_value = 5u; + size_t buffer_size = sizeof(DWORD); + scoped_ptr<uint8_t[]> buffer(new uint8_t[buffer_size]); + memcpy(buffer.get(), &test_value, buffer_size); + scoped_ptr<device::win::DeviceRegistryPropertyValue> value = + device::win::DeviceRegistryPropertyValue::Create( + REG_DWORD, buffer.Pass(), buffer_size).Pass(); + EXPECT_EQ(test_value, value->AsDWORD()); +} + } // namespace device |