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 #import <IOBluetooth/objc/IOBluetoothSDPDataElement.h> | |
6 #import <IOBluetooth/objc/IOBluetoothSDPServiceRecord.h> | |
7 #import <IOBluetooth/objc/IOBluetoothSDPUUID.h> | |
8 | |
9 #include <string> | |
10 | |
11 #include "base/basictypes.h" | |
12 #include "base/string_number_conversions.h" | |
13 #include "base/strings/sys_string_conversions.h" | |
14 #include "device/bluetooth/bluetooth_service_record_mac.h" | |
Mark Mentovai
2013/03/11 22:38:48
The test corresponding to this header is allowed t
youngki
2013/03/12 04:15:12
Done.
| |
15 #include "testing/gtest/include/gtest/gtest.h" | |
16 | |
17 namespace { | |
18 | |
19 const BluetoothSDPServiceAttributeID kServiceClassIDAttributeId = 0x0001; | |
20 const BluetoothSDPServiceAttributeID kProtocolDescriptorListAttributeId | |
21 = 0x0004; | |
Mark Mentovai
2013/03/11 22:38:48
I think it’s more usual to see the = at the end of
youngki
2013/03/12 04:15:12
Done.
| |
22 const BluetoothSDPServiceAttributeID kServiceNameAttributeId = 0x0100; | |
23 | |
24 const uint8 kRfcommChannel = 0x0c; | |
25 const char kServiceName[] = "Headset Audio Gateway"; | |
26 | |
27 const char kRfcommUuid[] = "0123456789abcdef0123456789abcdef"; | |
Mark Mentovai
2013/03/11 22:38:48
kRfcommUuid, kUpperCaseUuid, kShortUuid, and kMedi
youngki
2013/03/12 04:15:12
Done.
| |
28 const int kRfcommUuidSize = sizeof(kRfcommUuid) / 2; | |
Mark Mentovai
2013/03/11 22:38:48
These are size_t, not int. But I don’t think you n
youngki
2013/03/12 04:15:12
Done.
| |
29 const char kUpperCaseUuid[] = "0123456789ABCDEF0123456789ABCDEF"; | |
30 const int kUpperCaseUuidSize = sizeof(kUpperCaseUuid) / 2; | |
31 | |
32 const char kExpectedRfcommUuid[] = "01234567-89ab-cdef-0123-456789abcdef"; | |
33 | |
34 const char kShortUuid[] = "1101"; | |
35 const int kShortUuidSize = sizeof(kShortUuid) / 2; | |
36 const char kMediumUuid[] = "00001101"; | |
37 const int kMediumUuidSize = sizeof(kMediumUuid) / 2; | |
38 | |
39 const char kExpectedSerialUuid[] = "00001101-0000-1000-8000-00805f9b34fb"; | |
40 | |
Mark Mentovai
2013/03/11 22:38:48
Extra blank line?
youngki
2013/03/12 04:15:12
Done.
| |
41 | |
42 const int kMaxUuidSize = 16; | |
43 | |
44 } | |
Mark Mentovai
2013/03/11 22:38:48
} // namespace
youngki
2013/03/12 04:15:12
Done.
| |
45 | |
46 namespace device { | |
47 | |
48 class BluetoothServiceRecordMacTest : public testing::Test { | |
49 public: | |
50 | |
51 IOBluetoothSDPUUID* ConvertUuid(const char* uuid_hex_char, int uuid_size) { | |
52 std::vector<uint8> uuid_bytes_vector; | |
53 uint8 uuid_buffer[kMaxUuidSize]; | |
54 base::HexStringToBytes(uuid_hex_char, &uuid_bytes_vector); | |
55 std::copy(uuid_bytes_vector.begin(), | |
56 uuid_bytes_vector.end(), | |
57 uuid_buffer); | |
58 return [IOBluetoothSDPUUID uuidWithBytes: uuid_buffer length: uuid_size]; | |
Mark Mentovai
2013/03/11 22:38:48
No space after the : for an argument in an Objecti
youngki
2013/03/12 04:15:12
Done.
| |
59 } | |
60 | |
61 IOBluetoothSDPDataElement* GetServiceClassId(IOBluetoothSDPUUID* uuid) { | |
62 IOBluetoothSDPDataElement* uuid_element = | |
63 [IOBluetoothSDPDataElement withElementValue: uuid]; | |
64 return [IOBluetoothSDPDataElement | |
65 withElementValue: [NSArray arrayWithObject: uuid_element]]; | |
66 } | |
67 | |
68 IOBluetoothSDPDataElement* GetProtocolDescriptorList(bool supports_rfcomm) { | |
69 NSMutableArray* protocol_descriptor_list_sequence = [NSMutableArray array]; | |
70 | |
71 const uint8 l2cap_uuid_bytes[] = { 0x01, 0x00 }; | |
72 | |
73 IOBluetoothSDPUUID* l2cap_uuid = | |
74 [IOBluetoothSDPUUID uuidWithBytes: l2cap_uuid_bytes length: 2]; | |
75 [protocol_descriptor_list_sequence | |
76 addObject: [NSArray arrayWithObject: l2cap_uuid]]; | |
77 | |
78 if (supports_rfcomm) { | |
79 const uint8 rfcomm_uuid_bytes[] = { 0x00, 0x03 }; | |
80 IOBluetoothSDPUUID* rfcomm_uuid = | |
81 [IOBluetoothSDPUUID uuidWithBytes: rfcomm_uuid_bytes length: 2]; | |
82 NSNumber* rfcomm_channel = | |
83 [NSNumber numberWithUnsignedChar: kRfcommChannel]; | |
84 [protocol_descriptor_list_sequence | |
85 addObject: | |
86 [NSArray arrayWithObjects: rfcomm_uuid, rfcomm_channel, nil]]; | |
87 } | |
88 | |
89 return [IOBluetoothSDPDataElement | |
90 withElementValue: protocol_descriptor_list_sequence]; | |
91 } | |
92 | |
93 IOBluetoothSDPDataElement* GetServiceName(const std::string& service_name) { | |
94 return [IOBluetoothSDPDataElement | |
95 withElementValue: base::SysUTF8ToNSString(service_name)]; | |
96 } | |
97 | |
98 IOBluetoothSDPServiceRecord* GetServiceRecord( | |
99 IOBluetoothSDPUUID* uuid, bool supports_rfcomm) { | |
100 NSMutableDictionary* service_attrs = [NSMutableDictionary dictionary]; | |
101 | |
102 if (uuid != nil) { | |
103 [service_attrs setObject: GetServiceClassId(uuid) | |
104 forKey: [NSNumber numberWithInt: kServiceClassIDAttributeId]]; | |
105 } | |
106 [service_attrs setObject: GetProtocolDescriptorList(supports_rfcomm) | |
107 forKey: [NSNumber numberWithInt: kProtocolDescriptorListAttributeId]]; | |
108 [service_attrs setObject: GetServiceName(kServiceName) | |
109 forKey: [NSNumber numberWithInt: kServiceNameAttributeId]]; | |
110 | |
111 return [IOBluetoothSDPServiceRecord withServiceDictionary: service_attrs | |
112 device: nil]; | |
113 } | |
114 }; | |
115 | |
116 TEST_F(BluetoothServiceRecordMacTest, RfcommService) { | |
117 IOBluetoothSDPUUID* rfcomm_uuid = ConvertUuid(kRfcommUuid, kRfcommUuidSize); | |
118 | |
119 BluetoothServiceRecordMac record(GetServiceRecord(rfcomm_uuid, true)); | |
120 EXPECT_EQ(kServiceName, record.name()); | |
121 EXPECT_TRUE(record.SupportsRfcomm()); | |
122 EXPECT_EQ(kRfcommChannel, record.rfcomm_channel()); | |
123 EXPECT_EQ(kExpectedRfcommUuid, record.uuid()); | |
124 } | |
125 | |
126 TEST_F(BluetoothServiceRecordMacTest, ShortUuid) { | |
127 IOBluetoothSDPUUID* short_uuid = ConvertUuid(kShortUuid, kShortUuidSize); | |
128 | |
129 BluetoothServiceRecordMac record(GetServiceRecord(short_uuid, false)); | |
130 EXPECT_EQ(kExpectedSerialUuid, record.uuid()); | |
131 } | |
132 | |
133 TEST_F(BluetoothServiceRecordMacTest, MediumUuid) { | |
134 IOBluetoothSDPUUID* medium_uuid = ConvertUuid(kMediumUuid, kMediumUuidSize); | |
135 | |
136 BluetoothServiceRecordMac record(GetServiceRecord(medium_uuid, false)); | |
137 EXPECT_EQ(kExpectedSerialUuid, record.uuid()); | |
138 } | |
139 | |
140 TEST_F(BluetoothServiceRecordMacTest, UpperCaseUuid) { | |
141 IOBluetoothSDPUUID* upper_case_uuid = | |
142 ConvertUuid(kUpperCaseUuid, kUpperCaseUuidSize); | |
143 | |
144 BluetoothServiceRecordMac record(GetServiceRecord(upper_case_uuid, false)); | |
145 EXPECT_EQ(kExpectedRfcommUuid, record.uuid()); | |
146 } | |
147 | |
148 TEST_F(BluetoothServiceRecordMacTest, InvalidUuid) { | |
149 BluetoothServiceRecordMac record(GetServiceRecord(nil, false)); | |
150 EXPECT_EQ("", record.uuid()); | |
151 } | |
152 | |
153 } // namespace device | |
OLD | NEW |