1 // Copyright 2013 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.
5 #include "device/bluetooth/bluetooth_service_record_mac.h"
7 #import <IOBluetooth/objc/IOBluetoothSDPDataElement.h>
8 #import <IOBluetooth/objc/IOBluetoothSDPServiceRecord.h>
9 #import <IOBluetooth/objc/IOBluetoothSDPUUID.h>
13 #include "base/basictypes.h"
14 #include "base/string_number_conversions.h"
15 #include "base/strings/sys_string_conversions.h"
16 #include "testing/gtest/include/gtest/gtest.h"
20 const BluetoothSDPServiceAttributeID kServiceClassIDAttributeId = 0x0001;
21 const BluetoothSDPServiceAttributeID kProtocolDescriptorListAttributeId =
23 const BluetoothSDPServiceAttributeID kServiceNameAttributeId = 0x0100;
25 const uint8 kRfcommChannel = 0x0c;
26 const char kServiceName[] = "Headset Audio Gateway";
28 const char kExpectedRfcommUuid[] = "01234567-89ab-cdef-0123-456789abcdef";
29 const char kExpectedSerialUuid[] = "00001101-0000-1000-8000-00805f9b34fb";
31 const int kMaxUuidSize = 16;
37 class BluetoothServiceRecordMacTest : public testing::Test {
40 IOBluetoothSDPUUID* ConvertUuid(const char* uuid_hex_char, size_t uuid_size) {
41 std::vector<uint8> uuid_bytes_vector;
42 uint8 uuid_buffer[kMaxUuidSize];
43 base::HexStringToBytes(uuid_hex_char, &uuid_bytes_vector);
44 std::copy(uuid_bytes_vector.begin(),
45 uuid_bytes_vector.end(),
47 return [IOBluetoothSDPUUID uuidWithBytes:uuid_buffer length:uuid_size];
50 IOBluetoothSDPDataElement* GetServiceClassId(IOBluetoothSDPUUID* uuid) {
51 IOBluetoothSDPDataElement* uuid_element =
52 [IOBluetoothSDPDataElement withElementValue:uuid];
53 return [IOBluetoothSDPDataElement
54 withElementValue:[NSArray arrayWithObject:uuid_element]];
57 IOBluetoothSDPDataElement* GetProtocolDescriptorList(bool supports_rfcomm) {
58 NSMutableArray* protocol_descriptor_list_sequence = [NSMutableArray array];
60 const uint8 l2cap_uuid_bytes[] = { 0x01, 0x00 };
62 IOBluetoothSDPUUID* l2cap_uuid =
63 [IOBluetoothSDPUUID uuidWithBytes:l2cap_uuid_bytes length:2];
64 [protocol_descriptor_list_sequence
65 addObject:[NSArray arrayWithObject:l2cap_uuid]];
67 if (supports_rfcomm) {
68 const uint8 rfcomm_uuid_bytes[] = { 0x00, 0x03 };
69 IOBluetoothSDPUUID* rfcomm_uuid =
70 [IOBluetoothSDPUUID uuidWithBytes:rfcomm_uuid_bytes length:2];
71 NSNumber* rfcomm_channel =
72 [NSNumber numberWithUnsignedChar:kRfcommChannel];
73 [protocol_descriptor_list_sequence
75 arrayWithObjects:rfcomm_uuid, rfcomm_channel, nil]];
78 return [IOBluetoothSDPDataElement
79 withElementValue:protocol_descriptor_list_sequence];
82 IOBluetoothSDPDataElement* GetServiceName(const std::string& service_name) {
83 return [IOBluetoothSDPDataElement
84 withElementValue:base::SysUTF8ToNSString(service_name)];
87 IOBluetoothSDPServiceRecord* GetServiceRecord(
88 IOBluetoothSDPUUID* uuid, bool supports_rfcomm) {
89 NSMutableDictionary* service_attrs = [NSMutableDictionary dictionary];
93 setObject:GetServiceClassId(uuid)
94 forKey:[NSNumber numberWithInt:kServiceClassIDAttributeId]];
97 setObject:GetProtocolDescriptorList(supports_rfcomm)
98 forKey:[NSNumber numberWithInt:kProtocolDescriptorListAttributeId]];
100 setObject:GetServiceName(kServiceName)
101 forKey:[NSNumber numberWithInt:kServiceNameAttributeId]];
103 return [IOBluetoothSDPServiceRecord withServiceDictionary:service_attrs
108 TEST_F(BluetoothServiceRecordMacTest, RfcommService) {
109 const char rfcomm_uuid_bytes[] = "0123456789abcdef0123456789abcdef";
110 IOBluetoothSDPUUID* rfcomm_uuid =
111 ConvertUuid(rfcomm_uuid_bytes, sizeof(rfcomm_uuid_bytes) / 2);
113 BluetoothServiceRecordMac record(GetServiceRecord(rfcomm_uuid, true));
114 EXPECT_EQ(kServiceName, record.name());
115 EXPECT_TRUE(record.SupportsRfcomm());
116 EXPECT_EQ(kRfcommChannel, record.rfcomm_channel());
117 EXPECT_EQ(kExpectedRfcommUuid, record.uuid());
120 TEST_F(BluetoothServiceRecordMacTest, ShortUuid) {
121 const char short_uuid_bytes[] = "1101";
122 IOBluetoothSDPUUID* short_uuid =
123 ConvertUuid(short_uuid_bytes, sizeof(short_uuid_bytes) / 2);
125 BluetoothServiceRecordMac record(GetServiceRecord(short_uuid, false));
126 EXPECT_EQ(kExpectedSerialUuid, record.uuid());
129 TEST_F(BluetoothServiceRecordMacTest, MediumUuid) {
130 const char medium_uuid_bytes[] = "00001101";
131 IOBluetoothSDPUUID* medium_uuid =
132 ConvertUuid(medium_uuid_bytes, sizeof(medium_uuid_bytes) / 2);
134 BluetoothServiceRecordMac record(GetServiceRecord(medium_uuid, false));
135 EXPECT_EQ(kExpectedSerialUuid, record.uuid());
138 TEST_F(BluetoothServiceRecordMacTest, UpperCaseUuid) {
139 const char upper_case_uuid_bytes[] = "0123456789ABCDEF0123456789ABCDEF";
140 IOBluetoothSDPUUID* upper_case_uuid =
141 ConvertUuid(upper_case_uuid_bytes, sizeof(upper_case_uuid_bytes) / 2);
143 BluetoothServiceRecordMac record(GetServiceRecord(upper_case_uuid, false));
144 EXPECT_EQ(kExpectedRfcommUuid, record.uuid());
147 TEST_F(BluetoothServiceRecordMacTest, InvalidUuid) {
148 BluetoothServiceRecordMac record(GetServiceRecord(nil, false));
149 EXPECT_EQ("", record.uuid());
152 } // namespace device