1// Copyright 2014 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "base/strings/sys_string_conversions.h"
6#include "device/bluetooth/bluetooth_low_energy_win.h"
7#include "testing/gtest/include/gtest/gtest.h"
8
9namespace {
10
11const char kValidDeviceInstanceId[] =
12    "BTHLE\\DEV_BC6A29AB5FB0\\8&31038925&0&BC6A29AB5FB0";
13
14const char kInvalidDeviceInstanceId[] =
15    "BTHLE\\BC6A29AB5FB0_DEV_\\8&31038925&0&BC6A29AB5FB0";
16
17}  // namespace
18
19namespace device {
20
21class BluetoothLowEnergyWinTest : public testing::Test {};
22
23TEST_F(BluetoothLowEnergyWinTest, ExtractValidBluetoothAddress) {
24  BLUETOOTH_ADDRESS btha;
25  std::string error;
26  bool success =
27      device::win::ExtractBluetoothAddressFromDeviceInstanceIdForTesting(
28          kValidDeviceInstanceId, &btha, &error);
29
30  EXPECT_TRUE(success);
31  EXPECT_TRUE(error.empty());
32  EXPECT_EQ(0xbc, btha.rgBytes[5]);
33  EXPECT_EQ(0x6a, btha.rgBytes[4]);
34  EXPECT_EQ(0x29, btha.rgBytes[3]);
35  EXPECT_EQ(0xab, btha.rgBytes[2]);
36  EXPECT_EQ(0x5f, btha.rgBytes[1]);
37  EXPECT_EQ(0xb0, btha.rgBytes[0]);
38}
39
40TEST_F(BluetoothLowEnergyWinTest, ExtractInvalidBluetoothAddress) {
41  BLUETOOTH_ADDRESS btha;
42  std::string error;
43  bool success =
44      device::win::ExtractBluetoothAddressFromDeviceInstanceIdForTesting(
45          kInvalidDeviceInstanceId, &btha, &error);
46
47  EXPECT_FALSE(success);
48  EXPECT_FALSE(error.empty());
49}
50
51TEST_F(BluetoothLowEnergyWinTest, DeviceRegistryPropertyValueAsString) {
52  std::string test_value = "String used for round trip test.";
53  std::wstring wide_value = base::SysUTF8ToWide(test_value);
54  size_t buffer_size = (wide_value.size() + 1) * sizeof(wchar_t);
55  scoped_ptr<uint8_t[]> buffer(new uint8_t[buffer_size]);
56  memcpy(buffer.get(), wide_value.c_str(), buffer_size);
57  scoped_ptr<device::win::DeviceRegistryPropertyValue> value =
58      device::win::DeviceRegistryPropertyValue::Create(
59          REG_SZ, buffer.Pass(), buffer_size).Pass();
60  EXPECT_EQ(test_value, value->AsString());
61}
62
63TEST_F(BluetoothLowEnergyWinTest, DeviceRegistryPropertyValueAsDWORD) {
64  DWORD test_value = 5u;
65  size_t buffer_size = sizeof(DWORD);
66  scoped_ptr<uint8_t[]> buffer(new uint8_t[buffer_size]);
67  memcpy(buffer.get(), &test_value, buffer_size);
68  scoped_ptr<device::win::DeviceRegistryPropertyValue> value =
69      device::win::DeviceRegistryPropertyValue::Create(
70          REG_DWORD, buffer.Pass(), buffer_size).Pass();
71  EXPECT_EQ(test_value, value->AsDWORD());
72}
73
74TEST_F(BluetoothLowEnergyWinTest, DevicePropertyValueAsUint32) {
75  uint32_t test_value = 5u;
76  size_t buffer_size = sizeof(uint32_t);
77  scoped_ptr<uint8_t[]> buffer(new uint8_t[buffer_size]);
78  memcpy(buffer.get(), &test_value, buffer_size);
79  scoped_ptr<device::win::DevicePropertyValue> value(
80      new device::win::DevicePropertyValue(
81          DEVPROP_TYPE_UINT32, buffer.Pass(), buffer_size));
82  EXPECT_EQ(test_value, value->AsUint32());
83}
84
85}  // namespace device
86