OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/strings/string_number_conversions.h" | |
5 #include "device/bluetooth/bluetooth_adapter_mac.h" | 6 #include "device/bluetooth/bluetooth_adapter_mac.h" |
6 #include "device/bluetooth/test/bluetooth_test_mac.h" | 7 #include "device/bluetooth/test/bluetooth_test_mac.h" |
7 #include "device/bluetooth/test/mock_bluetooth_central_manager_mac.h" | 8 #include "device/bluetooth/test/mock_bluetooth_central_manager_mac.h" |
9 #include "third_party/ocmock/OCMock/OCMock.h" | |
10 | |
11 #if defined(OS_IOS) | |
12 #import <CoreBluetooth/CoreBluetooth.h> | |
13 #else // !defined(OS_IOS) | |
14 #import <IOBluetooth/IOBluetooth.h> | |
15 #endif // defined(OS_IOS) | |
8 | 16 |
9 namespace device { | 17 namespace device { |
10 | 18 |
19 // UUID1 hashes to 01:00:00:90:1E:BE, and UUID2 to 02:00:00:8B:74:63. | |
20 const std::string BluetoothTestMac::kTestPeripheralUUID1 = | |
21 "34045B00-0000-0000-0000-000000000000"; | |
22 const std::string BluetoothTestMac::kTestPeripheralUUID2 = | |
23 "EC1B8F00-0000-0000-0000-000000000000"; | |
24 | |
11 BluetoothTestMac::BluetoothTestMac() {} | 25 BluetoothTestMac::BluetoothTestMac() {} |
12 | 26 |
13 BluetoothTestMac::~BluetoothTestMac() {} | 27 BluetoothTestMac::~BluetoothTestMac() {} |
14 | 28 |
15 void BluetoothTestMac::SetUp() {} | 29 void BluetoothTestMac::SetUp() {} |
16 | 30 |
17 void BluetoothTestMac::InitWithDefaultAdapter() { | 31 void BluetoothTestMac::InitWithDefaultAdapter() { |
18 adapter_mac_ = BluetoothAdapterMac::CreateAdapter().get(); | 32 adapter_mac_ = BluetoothAdapterMac::CreateAdapter().get(); |
19 adapter_ = adapter_mac_; | 33 adapter_ = adapter_mac_; |
20 } | 34 } |
(...skipping 18 matching lines...) Expand all Loading... | |
39 .get(); | 53 .get(); |
40 adapter_ = adapter_mac_; | 54 adapter_ = adapter_mac_; |
41 | 55 |
42 if (BluetoothAdapterMac::IsLowEnergyAvailable()) { | 56 if (BluetoothAdapterMac::IsLowEnergyAvailable()) { |
43 id low_energy_central_manager = [[MockCentralManager alloc] init]; | 57 id low_energy_central_manager = [[MockCentralManager alloc] init]; |
44 [low_energy_central_manager setState:CBCentralManagerStatePoweredOn]; | 58 [low_energy_central_manager setState:CBCentralManagerStatePoweredOn]; |
45 adapter_mac_->SetCentralManagerForTesting(low_energy_central_manager); | 59 adapter_mac_->SetCentralManagerForTesting(low_energy_central_manager); |
46 } | 60 } |
47 } | 61 } |
48 | 62 |
63 CBPeripheral* CreateMockPeripheral(NSString* identifier) { | |
64 Class peripheral_class = NSClassFromString(@"CBPeripheral"); | |
65 id mock_peripheral = [OCMockObject mockForClass:[peripheral_class class]]; | |
66 [static_cast<CBPeripheral*>( | |
67 [[mock_peripheral stub] andReturnValue:@(CBPeripheralStateDisconnected)]) | |
68 performSelector:@selector(state)]; | |
69 [[[mock_peripheral stub] andReturn:[NSString string]] name]; | |
70 Class uuid_class = NSClassFromString(@"NSUUID"); | |
71 [[[mock_peripheral stub] | |
72 andReturn:[[uuid_class performSelector:@selector(UUID)] | |
73 performSelector:@selector(initWithUUIDString:) | |
74 withObject:identifier]] identifier]; | |
75 | |
76 return mock_peripheral; | |
77 } | |
78 | |
79 NSDictionary* CreateAdvertisementData(NSString* name, NSArray* uuids) { | |
80 NSMutableDictionary* advertisement_data = | |
81 [NSMutableDictionary dictionaryWithDictionary:@{ | |
82 @"CBAdvertisementDataLocalNameKey" : name, | |
83 @"CBAdvertisementDataServiceDataKey" : [NSDictionary dictionary], | |
84 @"CBAdvertisementDataIsConnectable" : @(YES), | |
85 }]; | |
86 if (uuids) | |
87 [advertisement_data setObject:uuids | |
88 forKey:@"CBAdvertisementDataServiceUUIDsKey"]; | |
89 return advertisement_data; | |
90 } | |
91 | |
92 void BluetoothTestMac::DiscoverLowEnergyDevice(int device_ordinal) { | |
93 CBCentralManager* central_manager = adapter_mac_->low_energy_central_manager_; | |
94 BluetoothLowEnergyCentralManagerDelegate* central_manager_delegate = | |
95 adapter_mac_->low_energy_central_manager_delegate_; | |
96 switch (device_ordinal) { | |
97 case 1: { | |
98 CBPeripheral* peripheral = CreateMockPeripheral( | |
99 [NSString stringWithUTF8String:kTestPeripheralUUID1.c_str()]); | |
100 NSString* name = [NSString stringWithUTF8String:kTestDeviceName.c_str()]; | |
101 NSArray* uuids = @[ | |
102 [CBUUID | |
103 UUIDWithString:[NSString stringWithUTF8String:kTestUUID1.c_str()]], | |
104 [CBUUID | |
105 UUIDWithString:[NSString stringWithUTF8String:kTestUUID2.c_str()]] | |
106 ]; | |
107 NSDictionary* advertisement_data = CreateAdvertisementData(name, uuids); | |
108 [central_manager_delegate centralManager:central_manager | |
109 didDiscoverPeripheral:peripheral | |
110 advertisementData:advertisement_data | |
111 RSSI:0]; | |
112 break; | |
113 } | |
114 case 2: { | |
115 CBPeripheral* peripheral = CreateMockPeripheral( | |
116 [NSString stringWithUTF8String:kTestPeripheralUUID1.c_str()]); | |
117 NSString* name = [NSString stringWithUTF8String:kTestDeviceName.c_str()]; | |
118 NSArray* uuids = @[ | |
119 [CBUUID | |
120 UUIDWithString:[NSString stringWithUTF8String:kTestUUID3.c_str()]], | |
121 [CBUUID | |
122 UUIDWithString:[NSString stringWithUTF8String:kTestUUID4.c_str()]] | |
123 ]; | |
124 NSDictionary* advertisement_data = CreateAdvertisementData(name, uuids); | |
125 [central_manager_delegate centralManager:central_manager | |
126 didDiscoverPeripheral:peripheral | |
127 advertisementData:advertisement_data | |
128 RSSI:0]; | |
129 break; | |
130 } | |
131 case 3: { | |
132 CBPeripheral* peripheral = CreateMockPeripheral( | |
133 [NSString stringWithUTF8String:kTestPeripheralUUID1.c_str()]); | |
134 NSString* name = | |
135 [NSString stringWithUTF8String:kTestDeviceNameEmpty.c_str()]; | |
136 NSArray* uuids = nil; | |
137 NSDictionary* advertisement_data = CreateAdvertisementData(name, uuids); | |
138 [central_manager_delegate centralManager:central_manager | |
139 didDiscoverPeripheral:peripheral | |
140 advertisementData:advertisement_data | |
141 RSSI:0]; | |
142 break; | |
143 } | |
144 case 4: { | |
145 CBPeripheral* peripheral = CreateMockPeripheral( | |
146 [NSString stringWithUTF8String:kTestPeripheralUUID2.c_str()]); | |
147 NSString* name = | |
148 [NSString stringWithUTF8String:kTestDeviceNameEmpty.c_str()]; | |
149 NSArray* uuids = nil; | |
150 NSDictionary* advertisement_data = CreateAdvertisementData(name, uuids); | |
151 [central_manager_delegate centralManager:central_manager | |
152 didDiscoverPeripheral:peripheral | |
153 advertisementData:advertisement_data | |
154 RSSI:0]; | |
155 break; | |
156 } | |
157 } | |
158 } | |
159 | |
160 bool BluetoothTestMac::PlatformSupportsLowEnergy() { | |
161 return BluetoothAdapterMac::IsLowEnergyAvailable(); | |
162 } | |
163 | |
164 std::string BluetoothTestMac::FindCBUUIDForHashTarget() { | |
165 // The desired first 6 digits of the hash. | |
166 const std::string target = "010000"; | |
167 // 128 bit buffer to be encoded as a hex string. | |
168 int64_t input[2] = {0}; | |
169 // There are 2^24 ~ 10^7 possible configurations for the first 6 digits, ie. | |
170 // each input has probability 10^-7 of succeeding, under the dubious | |
171 // assumption that traversing inputs sequentially is as good as traversing | |
172 // them randomly. After 4*10^7 iterations then the probability of not | |
173 // succeeeding is < exp(-18) ~ 10^-8 via the Chernoff Bound | |
174 // http://www.cs.berkeley.edu/~jfc/cs174/lecs/lec10/lec10.pdf. | |
scheib
2015/07/23 18:15:25
Nice try at nerd sniping https://xkcd.com/356/
I s
krstnmnlsn
2015/07/23 22:03:29
Haha! next time..
| |
175 while (input[0] < LLONG_MAX) { | |
176 // Encode as a hexidecimal number. Note that on x86 input[0] is stored as a | |
177 // little-endian number, and so read backwards by HexEncode. | |
178 std::string input_str = base::HexEncode(&input, sizeof(input)); | |
179 input_str.insert(20, "-"); | |
180 input_str.insert(16, "-"); | |
181 input_str.insert(12, "-"); | |
182 input_str.insert(8, "-"); | |
183 char raw[3]; | |
184 crypto::SHA256HashString(input_str, raw, sizeof(raw)); | |
185 if (base::HexEncode(raw, sizeof(raw)) == target) { | |
186 return input_str; | |
187 break; | |
scheib
2015/07/23 18:15:25
Don't need a break; after a return;
krstnmnlsn
2015/07/23 22:03:29
:P
| |
188 } | |
189 ++input[0]; | |
190 } | |
191 return ""; | |
192 } | |
193 | |
49 } // namespace device | 194 } // namespace device |
OLD | NEW |