OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "device/bluetooth/bluetooth_service_record_mac.h" | |
6 | |
7 #import <IOBluetooth/objc/IOBluetoothSDPDataElement.h> | |
8 #import <IOBluetooth/objc/IOBluetoothSDPServiceRecord.h> | |
9 #import <IOBluetooth/objc/IOBluetoothSDPUUID.h> | |
10 | |
11 #include <string> | |
12 | |
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" | |
18 | |
19 namespace { | |
20 | |
21 const BluetoothSDPServiceAttributeID kServiceClassIDAttributeId = 0x0001; | |
22 const BluetoothSDPServiceAttributeID kProtocolDescriptorListAttributeId = | |
23 0x0004; | |
24 const BluetoothSDPServiceAttributeID kServiceNameAttributeId = 0x0100; | |
25 | |
26 const uint8 kRfcommChannel = 0x0c; | |
27 const char kServiceName[] = "Headset Audio Gateway"; | |
28 | |
29 const device::BluetoothUUID kExpectedRfcommUuid( | |
30 "01234567-89ab-cdef-0123-456789abcdef"); | |
31 const device::BluetoothUUID kExpectedSerialUuid("1101"); | |
32 | |
33 const int kMaxUuidSize = 16; | |
34 | |
35 } // namespace | |
36 | |
37 namespace device { | |
38 | |
39 class BluetoothServiceRecordMacTest : public testing::Test { | |
40 public: | |
41 | |
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(), | |
48 uuid_buffer); | |
49 return [IOBluetoothSDPUUID uuidWithBytes:uuid_buffer length:uuid_size]; | |
50 } | |
51 | |
52 IOBluetoothSDPDataElement* GetServiceClassId(IOBluetoothSDPUUID* uuid) { | |
53 IOBluetoothSDPDataElement* uuid_element = | |
54 [IOBluetoothSDPDataElement withElementValue:uuid]; | |
55 return [IOBluetoothSDPDataElement | |
56 withElementValue:[NSArray arrayWithObject:uuid_element]]; | |
57 } | |
58 | |
59 IOBluetoothSDPDataElement* GetProtocolDescriptorList(bool supports_rfcomm) { | |
60 NSMutableArray* protocol_descriptor_list_sequence = [NSMutableArray array]; | |
61 | |
62 const uint8 l2cap_uuid_bytes[] = { 0x01, 0x00 }; | |
63 | |
64 IOBluetoothSDPUUID* l2cap_uuid = | |
65 [IOBluetoothSDPUUID uuidWithBytes:l2cap_uuid_bytes length:2]; | |
66 [protocol_descriptor_list_sequence | |
67 addObject:[NSArray arrayWithObject:l2cap_uuid]]; | |
68 | |
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 | |
76 addObject:[NSArray | |
77 arrayWithObjects:rfcomm_uuid, rfcomm_channel, nil]]; | |
78 } | |
79 | |
80 return [IOBluetoothSDPDataElement | |
81 withElementValue:protocol_descriptor_list_sequence]; | |
82 } | |
83 | |
84 IOBluetoothSDPDataElement* GetServiceName(const std::string& service_name) { | |
85 return [IOBluetoothSDPDataElement | |
86 withElementValue:base::SysUTF8ToNSString(service_name)]; | |
87 } | |
88 | |
89 IOBluetoothSDPServiceRecord* GetServiceRecord( | |
90 IOBluetoothSDPUUID* uuid, bool supports_rfcomm) { | |
91 NSMutableDictionary* service_attrs = [NSMutableDictionary dictionary]; | |
92 | |
93 if (uuid != nil) { | |
94 [service_attrs | |
95 setObject:GetServiceClassId(uuid) | |
96 forKey:[NSNumber numberWithInt:kServiceClassIDAttributeId]]; | |
97 } | |
98 [service_attrs | |
99 setObject:GetProtocolDescriptorList(supports_rfcomm) | |
100 forKey:[NSNumber numberWithInt:kProtocolDescriptorListAttributeId]]; | |
101 [service_attrs | |
102 setObject:GetServiceName(kServiceName) | |
103 forKey:[NSNumber numberWithInt:kServiceNameAttributeId]]; | |
104 | |
105 return [IOBluetoothSDPServiceRecord withServiceDictionary:service_attrs | |
106 device:nil]; | |
107 } | |
108 }; | |
109 | |
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); | |
114 | |
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()); | |
120 } | |
121 | |
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); | |
126 | |
127 BluetoothServiceRecordMac record(GetServiceRecord(short_uuid, false)); | |
128 EXPECT_EQ(kExpectedSerialUuid, record.uuid()); | |
129 } | |
130 | |
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); | |
135 | |
136 BluetoothServiceRecordMac record(GetServiceRecord(medium_uuid, false)); | |
137 EXPECT_EQ(kExpectedSerialUuid, record.uuid()); | |
138 } | |
139 | |
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); | |
144 | |
145 BluetoothServiceRecordMac record(GetServiceRecord(upper_case_uuid, false)); | |
146 EXPECT_EQ(kExpectedRfcommUuid, record.uuid()); | |
147 } | |
148 | |
149 TEST_F(BluetoothServiceRecordMacTest, InvalidUuid) { | |
150 BluetoothServiceRecordMac record(GetServiceRecord(nil, false)); | |
151 EXPECT_FALSE(record.uuid().IsValid()); | |
152 } | |
153 | |
154 } // namespace device | |
OLD | NEW |