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/string_number_conversions.h" | |
15 #include "base/strings/sys_string_conversions.h" | |
16 #include "testing/gtest/include/gtest/gtest.h" | |
17 | |
18 namespace { | |
19 | |
20 const BluetoothSDPServiceAttributeID kServiceClassIDAttributeId = 0x0001; | |
21 const BluetoothSDPServiceAttributeID kProtocolDescriptorListAttributeId = | |
22 0x0004; | |
23 const BluetoothSDPServiceAttributeID kServiceNameAttributeId = 0x0100; | |
24 | |
25 const uint8 kRfcommChannel = 0x0c; | |
26 const char kServiceName[] = "Headset Audio Gateway"; | |
27 | |
28 const char kExpectedRfcommUuid[] = "01234567-89ab-cdef-0123-456789abcdef"; | |
29 const char kExpectedSerialUuid[] = "00001101-0000-1000-8000-00805f9b34fb"; | |
30 | |
31 const int kMaxUuidSize = 16; | |
32 | |
33 } // namespace | |
34 | |
35 namespace device { | |
36 | |
37 class BluetoothServiceRecordMacTest : public testing::Test { | |
38 public: | |
39 | |
40 IOBluetoothSDPUUID* ConvertUuid(const char* uuid_hex_char, int uuid_size) { | |
Mark Mentovai
2013/03/12 16:02:54
This is more of a size_t than an int.
youngki
2013/03/12 19:45:47
Done.
| |
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(), | |
46 uuid_buffer); | |
47 return [IOBluetoothSDPUUID uuidWithBytes:uuid_buffer length:uuid_size]; | |
48 } | |
49 | |
50 IOBluetoothSDPDataElement* GetServiceClassId(IOBluetoothSDPUUID* uuid) { | |
51 IOBluetoothSDPDataElement* uuid_element = | |
52 [IOBluetoothSDPDataElement withElementValue:uuid]; | |
53 return [IOBluetoothSDPDataElement | |
54 withElementValue:[NSArray arrayWithObject:uuid_element]]; | |
55 } | |
56 | |
57 IOBluetoothSDPDataElement* GetProtocolDescriptorList(bool supports_rfcomm) { | |
58 NSMutableArray* protocol_descriptor_list_sequence = [NSMutableArray array]; | |
59 | |
60 const uint8 l2cap_uuid_bytes[] = { 0x01, 0x00 }; | |
61 | |
62 IOBluetoothSDPUUID* l2cap_uuid = | |
63 [IOBluetoothSDPUUID uuidWithBytes:l2cap_uuid_bytes length:2]; | |
64 [protocol_descriptor_list_sequence | |
65 addObject:[NSArray arrayWithObject:l2cap_uuid]]; | |
66 | |
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 | |
74 addObject:[NSArray | |
75 arrayWithObjects:rfcomm_uuid, rfcomm_channel, nil]]; | |
76 } | |
77 | |
78 return [IOBluetoothSDPDataElement | |
79 withElementValue:protocol_descriptor_list_sequence]; | |
80 } | |
81 | |
82 IOBluetoothSDPDataElement* GetServiceName(const std::string& service_name) { | |
83 return [IOBluetoothSDPDataElement | |
84 withElementValue:base::SysUTF8ToNSString(service_name)]; | |
85 } | |
86 | |
87 IOBluetoothSDPServiceRecord* GetServiceRecord( | |
88 IOBluetoothSDPUUID* uuid, bool supports_rfcomm) { | |
89 NSMutableDictionary* service_attrs = [NSMutableDictionary dictionary]; | |
90 | |
91 if (uuid != nil) { | |
92 [service_attrs | |
93 setObject:GetServiceClassId(uuid) | |
94 forKey:[NSNumber numberWithInt:kServiceClassIDAttributeId]]; | |
95 } | |
96 [service_attrs | |
97 setObject:GetProtocolDescriptorList(supports_rfcomm) | |
98 forKey:[NSNumber numberWithInt:kProtocolDescriptorListAttributeId]]; | |
99 [service_attrs | |
100 setObject:GetServiceName(kServiceName) | |
101 forKey:[NSNumber numberWithInt:kServiceNameAttributeId]]; | |
102 | |
103 return [IOBluetoothSDPServiceRecord withServiceDictionary:service_attrs | |
104 device:nil]; | |
105 } | |
106 }; | |
107 | |
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); | |
112 | |
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()); | |
118 } | |
119 | |
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); | |
124 | |
125 BluetoothServiceRecordMac record(GetServiceRecord(short_uuid, false)); | |
126 EXPECT_EQ(kExpectedSerialUuid, record.uuid()); | |
127 } | |
128 | |
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); | |
133 | |
134 BluetoothServiceRecordMac record(GetServiceRecord(medium_uuid, false)); | |
135 EXPECT_EQ(kExpectedSerialUuid, record.uuid()); | |
136 } | |
137 | |
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); | |
142 | |
143 BluetoothServiceRecordMac record(GetServiceRecord(upper_case_uuid, false)); | |
144 EXPECT_EQ(kExpectedRfcommUuid, record.uuid()); | |
145 } | |
146 | |
147 TEST_F(BluetoothServiceRecordMacTest, InvalidUuid) { | |
148 BluetoothServiceRecordMac record(GetServiceRecord(nil, false)); | |
149 EXPECT_EQ("", record.uuid()); | |
150 } | |
151 | |
152 } // namespace device | |
OLD | NEW |