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/strings/string_number_conversions.h"
15 #include "base/strings/sys_string_conversions.h"
16 #include "device/bluetooth/bluetooth_uuid.h"
17 #include "testing/gtest/include/gtest/gtest.h"
21 const BluetoothSDPServiceAttributeID kServiceClassIDAttributeId = 0x0001;
22 const BluetoothSDPServiceAttributeID kProtocolDescriptorListAttributeId =
24 const BluetoothSDPServiceAttributeID kServiceNameAttributeId = 0x0100;
26 const uint8 kRfcommChannel = 0x0c;
27 const char kServiceName[] = "Headset Audio Gateway";
29 const device::BluetoothUUID kExpectedRfcommUuid(
30 "01234567-89ab-cdef-0123-456789abcdef");
31 const device::BluetoothUUID kExpectedSerialUuid("1101");
33 const int kMaxUuidSize = 16;
39 class BluetoothServiceRecordMacTest : public testing::Test {
42 IOBluetoothSDPUUID* ConvertUuid(const char* uuid_hex_char, size_t uuid_size) {
43 std::vector<uint8> uuid_bytes_vector;
44 uint8 uuid_buffer[kMaxUuidSize];
45 base::HexStringToBytes(uuid_hex_char, &uuid_bytes_vector);
46 std::copy(uuid_bytes_vector.begin(),
47 uuid_bytes_vector.end(),
49 return [IOBluetoothSDPUUID uuidWithBytes:uuid_buffer length:uuid_size];
52 IOBluetoothSDPDataElement* GetServiceClassId(IOBluetoothSDPUUID* uuid) {
53 IOBluetoothSDPDataElement* uuid_element =
54 [IOBluetoothSDPDataElement withElementValue:uuid];
55 return [IOBluetoothSDPDataElement
56 withElementValue:[NSArray arrayWithObject:uuid_element]];
59 IOBluetoothSDPDataElement* GetProtocolDescriptorList(bool supports_rfcomm) {
60 NSMutableArray* protocol_descriptor_list_sequence = [NSMutableArray array];
62 const uint8 l2cap_uuid_bytes[] = { 0x01, 0x00 };
64 IOBluetoothSDPUUID* l2cap_uuid =
65 [IOBluetoothSDPUUID uuidWithBytes:l2cap_uuid_bytes length:2];
66 [protocol_descriptor_list_sequence
67 addObject:[NSArray arrayWithObject:l2cap_uuid]];
69 if (supports_rfcomm) {
70 const uint8 rfcomm_uuid_bytes[] = { 0x00, 0x03 };
71 IOBluetoothSDPUUID* rfcomm_uuid =
72 [IOBluetoothSDPUUID uuidWithBytes:rfcomm_uuid_bytes length:2];
73 NSNumber* rfcomm_channel =
74 [NSNumber numberWithUnsignedChar:kRfcommChannel];
75 [protocol_descriptor_list_sequence
77 arrayWithObjects:rfcomm_uuid, rfcomm_channel, nil]];
80 return [IOBluetoothSDPDataElement
81 withElementValue:protocol_descriptor_list_sequence];
84 IOBluetoothSDPDataElement* GetServiceName(const std::string& service_name) {
85 return [IOBluetoothSDPDataElement
86 withElementValue:base::SysUTF8ToNSString(service_name)];
89 IOBluetoothSDPServiceRecord* GetServiceRecord(
90 IOBluetoothSDPUUID* uuid, bool supports_rfcomm) {
91 NSMutableDictionary* service_attrs = [NSMutableDictionary dictionary];
95 setObject:GetServiceClassId(uuid)
96 forKey:[NSNumber numberWithInt:kServiceClassIDAttributeId]];
99 setObject:GetProtocolDescriptorList(supports_rfcomm)
100 forKey:[NSNumber numberWithInt:kProtocolDescriptorListAttributeId]];
102 setObject:GetServiceName(kServiceName)
103 forKey:[NSNumber numberWithInt:kServiceNameAttributeId]];
105 return [IOBluetoothSDPServiceRecord withServiceDictionary:service_attrs
110 TEST_F(BluetoothServiceRecordMacTest, RfcommService) {
111 const char rfcomm_uuid_bytes[] = "0123456789abcdef0123456789abcdef";
112 IOBluetoothSDPUUID* rfcomm_uuid =
113 ConvertUuid(rfcomm_uuid_bytes, sizeof(rfcomm_uuid_bytes) / 2);
115 BluetoothServiceRecordMac record(GetServiceRecord(rfcomm_uuid, true));
116 EXPECT_EQ(kServiceName, record.name());
117 EXPECT_TRUE(record.SupportsRfcomm());
118 EXPECT_EQ(kRfcommChannel, record.rfcomm_channel());
119 EXPECT_EQ(kExpectedRfcommUuid, record.uuid());
122 TEST_F(BluetoothServiceRecordMacTest, ShortUuid) {
123 const char short_uuid_bytes[] = "1101";
124 IOBluetoothSDPUUID* short_uuid =
125 ConvertUuid(short_uuid_bytes, sizeof(short_uuid_bytes) / 2);
127 BluetoothServiceRecordMac record(GetServiceRecord(short_uuid, false));
128 EXPECT_EQ(kExpectedSerialUuid, record.uuid());
131 TEST_F(BluetoothServiceRecordMacTest, MediumUuid) {
132 const char medium_uuid_bytes[] = "00001101";
133 IOBluetoothSDPUUID* medium_uuid =
134 ConvertUuid(medium_uuid_bytes, sizeof(medium_uuid_bytes) / 2);
136 BluetoothServiceRecordMac record(GetServiceRecord(medium_uuid, false));
137 EXPECT_EQ(kExpectedSerialUuid, record.uuid());
140 TEST_F(BluetoothServiceRecordMacTest, UpperCaseUuid) {
141 const char upper_case_uuid_bytes[] = "0123456789ABCDEF0123456789ABCDEF";
142 IOBluetoothSDPUUID* upper_case_uuid =
143 ConvertUuid(upper_case_uuid_bytes, sizeof(upper_case_uuid_bytes) / 2);
145 BluetoothServiceRecordMac record(GetServiceRecord(upper_case_uuid, false));
146 EXPECT_EQ(kExpectedRfcommUuid, record.uuid());
149 TEST_F(BluetoothServiceRecordMacTest, InvalidUuid) {
150 BluetoothServiceRecordMac record(GetServiceRecord(nil, false));
151 EXPECT_FALSE(record.uuid().IsValid());
154 } // namespace device