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 "content/browser/bluetooth/bluetooth_allowed_devices_map.h" | 5 #include "content/browser/bluetooth/bluetooth_allowed_devices_map.h" |
6 | 6 |
7 #include "base/strings/string_util.h" | 7 #include "base/strings/string_util.h" |
8 #include "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" |
9 #include "url/gurl.h" | 9 #include "url/gurl.h" |
10 | 10 |
11 using device::BluetoothUUID; | 11 using device::BluetoothUUID; |
12 | 12 |
13 namespace content { | 13 namespace content { |
14 namespace { | 14 namespace { |
15 const url::Origin kTestOrigin1(GURL("https://www.example1.com")); | 15 const url::Origin kTestOrigin1(GURL("https://www.example1.com")); |
16 const url::Origin kTestOrigin2(GURL("https://www.example2.com")); | 16 const url::Origin kTestOrigin2(GURL("https://www.example2.com")); |
17 | 17 |
18 const std::string kDeviceAddress1 = "00:00:00"; | 18 const std::string kDeviceAddress1 = "00:00:00"; |
19 const std::string kDeviceAddress2 = "11:11:11"; | 19 const std::string kDeviceAddress2 = "11:11:11"; |
20 | 20 |
21 const char kGlucoseUUID[] = "00001808-0000-1000-8000-00805f9b34fb"; | 21 const char kGlucoseUUIDString[] = "00001808-0000-1000-8000-00805f9b34fb"; |
22 const char kHeartRateUUID[] = "0000180d-0000-1000-8000-00805f9b34fb"; | 22 const char kHeartRateUUIDString[] = "0000180d-0000-1000-8000-00805f9b34fb"; |
23 const char kBatteryServiceUUID[] = "0000180f-0000-1000-8000-00805f9b34fb"; | 23 const char kBatteryServiceUUIDString[] = "0000180f-0000-1000-8000-00805f9b34fb"; |
24 const char kBloodPressureUUID[] = "00001813-0000-1000-8000-00805f9b34fb"; | 24 const char kBloodPressureUUIDString[] = "00001813-0000-1000-8000-00805f9b34fb"; |
25 const char kCyclingPowerUUID[] = "00001818-0000-1000-8000-00805f9b34fb"; | 25 const char kCyclingPowerUUIDString[] = "00001818-0000-1000-8000-00805f9b34fb"; |
26 const BluetoothUUID kGlucoseUUID(kGlucoseUUIDString); | |
27 const BluetoothUUID kHeartRateUUID(kHeartRateUUIDString); | |
28 const BluetoothUUID kBatteryServiceUUID(kBatteryServiceUUIDString); | |
29 const BluetoothUUID kBloodPressureUUID(kBloodPressureUUIDString); | |
30 const BluetoothUUID kCyclingPowerUUID(kCyclingPowerUUIDString); | |
26 | 31 |
27 class BluetoothAllowedDevicesMapTest : public testing::Test { | 32 class BluetoothAllowedDevicesMapTest : public testing::Test { |
28 protected: | 33 protected: |
29 BluetoothAllowedDevicesMapTest() { | 34 BluetoothAllowedDevicesMapTest() { |
30 empty_options_ = blink::mojom::WebBluetoothRequestDeviceOptions::New(); | 35 empty_options_ = blink::mojom::WebBluetoothRequestDeviceOptions::New(); |
31 } | 36 } |
32 | 37 |
33 ~BluetoothAllowedDevicesMapTest() override {} | 38 ~BluetoothAllowedDevicesMapTest() override {} |
34 | 39 |
40 base::Optional<BluetoothUUID> GetGlucoseUUIDOpt() { | |
41 return base::make_optional(BluetoothUUID(kGlucoseUUID)); | |
42 } | |
43 base::Optional<BluetoothUUID> GetHeartRateUUIDOpt() { | |
44 return base::make_optional(BluetoothUUID(kHeartRateUUID)); | |
Jeffrey Yasskin
2016/05/31 21:52:57
You can drop the BluetoothUUID() copy constructor
ortuno
2016/06/01 15:37:58
Done. I also removed base::make_optional since the
| |
45 } | |
46 base::Optional<BluetoothUUID> GetBatteryServiceUUIDOpt() { | |
47 return base::make_optional(BluetoothUUID(kBatteryServiceUUID)); | |
48 } | |
49 base::Optional<BluetoothUUID> GetBloodPressureUUIDOpt() { | |
50 return base::make_optional(BluetoothUUID(kBloodPressureUUID)); | |
51 } | |
52 base::Optional<BluetoothUUID> GetCyclingPowerUUIDOpt() { | |
53 return base::make_optional(BluetoothUUID(kCyclingPowerUUID)); | |
54 } | |
55 | |
35 blink::mojom::WebBluetoothRequestDeviceOptionsPtr empty_options_; | 56 blink::mojom::WebBluetoothRequestDeviceOptionsPtr empty_options_; |
36 }; | 57 }; |
37 | 58 |
38 } // namespace | 59 } // namespace |
39 | 60 |
40 TEST_F(BluetoothAllowedDevicesMapTest, UniqueOriginNotSupported) { | 61 TEST_F(BluetoothAllowedDevicesMapTest, UniqueOriginNotSupported) { |
41 BluetoothAllowedDevicesMap allowed_devices_map; | 62 BluetoothAllowedDevicesMap allowed_devices_map; |
42 | 63 |
43 EXPECT_DEATH_IF_SUPPORTED(allowed_devices_map.AddDevice( | 64 EXPECT_DEATH_IF_SUPPORTED(allowed_devices_map.AddDevice( |
44 url::Origin(), kDeviceAddress1, empty_options_), | 65 url::Origin(), kDeviceAddress1, empty_options_), |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
175 BluetoothAllowedDevicesMap allowed_devices_map; | 196 BluetoothAllowedDevicesMap allowed_devices_map; |
176 | 197 |
177 // Setup device. | 198 // Setup device. |
178 blink::mojom::WebBluetoothRequestDeviceOptionsPtr options = | 199 blink::mojom::WebBluetoothRequestDeviceOptionsPtr options = |
179 blink::mojom::WebBluetoothRequestDeviceOptions::New(); | 200 blink::mojom::WebBluetoothRequestDeviceOptions::New(); |
180 blink::mojom::WebBluetoothScanFilterPtr scanFilter1 = | 201 blink::mojom::WebBluetoothScanFilterPtr scanFilter1 = |
181 blink::mojom::WebBluetoothScanFilter::New(); | 202 blink::mojom::WebBluetoothScanFilter::New(); |
182 blink::mojom::WebBluetoothScanFilterPtr scanFilter2 = | 203 blink::mojom::WebBluetoothScanFilterPtr scanFilter2 = |
183 blink::mojom::WebBluetoothScanFilter::New(); | 204 blink::mojom::WebBluetoothScanFilter::New(); |
184 | 205 |
185 scanFilter1->services.push_back(kGlucoseUUID); | 206 scanFilter1->services.push_back(GetGlucoseUUIDOpt()); |
186 options->filters.push_back(scanFilter1.Clone()); | 207 options->filters.push_back(scanFilter1.Clone()); |
187 | 208 |
188 scanFilter2->services.push_back(kHeartRateUUID); | 209 scanFilter2->services.push_back(GetHeartRateUUIDOpt()); |
189 options->filters.push_back(scanFilter2.Clone()); | 210 options->filters.push_back(scanFilter2.Clone()); |
190 | 211 |
191 options->optional_services.push_back(kBatteryServiceUUID); | 212 options->optional_services.push_back(GetBatteryServiceUUIDOpt()); |
192 options->optional_services.push_back(kHeartRateUUID); | 213 options->optional_services.push_back(GetHeartRateUUIDOpt()); |
193 | 214 |
194 // Add to map. | 215 // Add to map. |
195 const std::string device_id1 = | 216 const std::string device_id1 = |
196 allowed_devices_map.AddDevice(kTestOrigin1, kDeviceAddress1, options); | 217 allowed_devices_map.AddDevice(kTestOrigin1, kDeviceAddress1, options); |
197 | 218 |
198 // Access allowed services. | 219 // Access allowed services. |
199 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessService( | 220 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessService( |
200 kTestOrigin1, device_id1, kGlucoseUUID)); | 221 kTestOrigin1, device_id1, kGlucoseUUID)); |
201 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessService( | 222 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessService( |
202 kTestOrigin1, device_id1, kHeartRateUUID)); | 223 kTestOrigin1, device_id1, kHeartRateUUID)); |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
248 | 269 |
249 TEST_F(BluetoothAllowedDevicesMapTest, AllowedServices_OneOriginTwoDevices) { | 270 TEST_F(BluetoothAllowedDevicesMapTest, AllowedServices_OneOriginTwoDevices) { |
250 BluetoothAllowedDevicesMap allowed_devices_map; | 271 BluetoothAllowedDevicesMap allowed_devices_map; |
251 | 272 |
252 // Setup request for device #1. | 273 // Setup request for device #1. |
253 blink::mojom::WebBluetoothRequestDeviceOptionsPtr options1 = | 274 blink::mojom::WebBluetoothRequestDeviceOptionsPtr options1 = |
254 blink::mojom::WebBluetoothRequestDeviceOptions::New(); | 275 blink::mojom::WebBluetoothRequestDeviceOptions::New(); |
255 blink::mojom::WebBluetoothScanFilterPtr scanFilter1 = | 276 blink::mojom::WebBluetoothScanFilterPtr scanFilter1 = |
256 blink::mojom::WebBluetoothScanFilter::New(); | 277 blink::mojom::WebBluetoothScanFilter::New(); |
257 | 278 |
258 scanFilter1->services.push_back(kGlucoseUUID); | 279 scanFilter1->services.push_back(GetGlucoseUUIDOpt()); |
259 options1->filters.push_back(std::move(scanFilter1)); | 280 options1->filters.push_back(std::move(scanFilter1)); |
260 | 281 |
261 options1->optional_services.push_back(kHeartRateUUID); | 282 options1->optional_services.push_back(GetHeartRateUUIDOpt()); |
262 | 283 |
263 // Setup request for device #2. | 284 // Setup request for device #2. |
264 blink::mojom::WebBluetoothRequestDeviceOptionsPtr options2 = | 285 blink::mojom::WebBluetoothRequestDeviceOptionsPtr options2 = |
265 blink::mojom::WebBluetoothRequestDeviceOptions::New(); | 286 blink::mojom::WebBluetoothRequestDeviceOptions::New(); |
266 blink::mojom::WebBluetoothScanFilterPtr scanFilter2 = | 287 blink::mojom::WebBluetoothScanFilterPtr scanFilter2 = |
267 blink::mojom::WebBluetoothScanFilter::New(); | 288 blink::mojom::WebBluetoothScanFilter::New(); |
268 | 289 |
269 scanFilter2->services.push_back(kBatteryServiceUUID); | 290 scanFilter2->services.push_back(GetBatteryServiceUUIDOpt()); |
270 options2->filters.push_back(std::move(scanFilter2)); | 291 options2->filters.push_back(std::move(scanFilter2)); |
271 | 292 |
272 options2->optional_services.push_back(kBloodPressureUUID); | 293 options2->optional_services.push_back(GetBloodPressureUUIDOpt()); |
273 | 294 |
274 // Add devices to map. | 295 // Add devices to map. |
275 const std::string& device_id1 = | 296 const std::string& device_id1 = |
276 allowed_devices_map.AddDevice(kTestOrigin1, kDeviceAddress1, options1); | 297 allowed_devices_map.AddDevice(kTestOrigin1, kDeviceAddress1, options1); |
277 const std::string& device_id2 = | 298 const std::string& device_id2 = |
278 allowed_devices_map.AddDevice(kTestOrigin1, kDeviceAddress2, options2); | 299 allowed_devices_map.AddDevice(kTestOrigin1, kDeviceAddress2, options2); |
279 | 300 |
280 // Access allowed services. | 301 // Access allowed services. |
281 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessService( | 302 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessService( |
282 kTestOrigin1, device_id1, kGlucoseUUID)); | 303 kTestOrigin1, device_id1, kGlucoseUUID)); |
(...skipping 22 matching lines...) Expand all Loading... | |
305 } | 326 } |
306 | 327 |
307 TEST_F(BluetoothAllowedDevicesMapTest, AllowedServices_TwoOriginsOneDevice) { | 328 TEST_F(BluetoothAllowedDevicesMapTest, AllowedServices_TwoOriginsOneDevice) { |
308 BluetoothAllowedDevicesMap allowed_devices_map; | 329 BluetoothAllowedDevicesMap allowed_devices_map; |
309 // Setup request #1 for device. | 330 // Setup request #1 for device. |
310 blink::mojom::WebBluetoothRequestDeviceOptionsPtr options1 = | 331 blink::mojom::WebBluetoothRequestDeviceOptionsPtr options1 = |
311 blink::mojom::WebBluetoothRequestDeviceOptions::New(); | 332 blink::mojom::WebBluetoothRequestDeviceOptions::New(); |
312 blink::mojom::WebBluetoothScanFilterPtr scanFilter1 = | 333 blink::mojom::WebBluetoothScanFilterPtr scanFilter1 = |
313 blink::mojom::WebBluetoothScanFilter::New(); | 334 blink::mojom::WebBluetoothScanFilter::New(); |
314 | 335 |
315 scanFilter1->services.push_back(kGlucoseUUID); | 336 scanFilter1->services.push_back(GetGlucoseUUIDOpt()); |
316 options1->filters.push_back(std::move(scanFilter1)); | 337 options1->filters.push_back(std::move(scanFilter1)); |
317 | 338 |
318 options1->optional_services.push_back(kHeartRateUUID); | 339 options1->optional_services.push_back(GetHeartRateUUIDOpt()); |
319 | 340 |
320 // Setup request #2 for device. | 341 // Setup request #2 for device. |
321 blink::mojom::WebBluetoothRequestDeviceOptionsPtr options2 = | 342 blink::mojom::WebBluetoothRequestDeviceOptionsPtr options2 = |
322 blink::mojom::WebBluetoothRequestDeviceOptions::New(); | 343 blink::mojom::WebBluetoothRequestDeviceOptions::New(); |
323 blink::mojom::WebBluetoothScanFilterPtr scanFilter2 = | 344 blink::mojom::WebBluetoothScanFilterPtr scanFilter2 = |
324 blink::mojom::WebBluetoothScanFilter::New(); | 345 blink::mojom::WebBluetoothScanFilter::New(); |
325 | 346 |
326 scanFilter2->services.push_back(kBatteryServiceUUID); | 347 scanFilter2->services.push_back(GetBatteryServiceUUIDOpt()); |
327 options2->filters.push_back(std::move(scanFilter2)); | 348 options2->filters.push_back(std::move(scanFilter2)); |
328 | 349 |
329 options2->optional_services.push_back(kBloodPressureUUID); | 350 options2->optional_services.push_back(GetBloodPressureUUIDOpt()); |
330 | 351 |
331 // Add devices to map. | 352 // Add devices to map. |
332 const std::string& device_id1 = | 353 const std::string& device_id1 = |
333 allowed_devices_map.AddDevice(kTestOrigin1, kDeviceAddress1, options1); | 354 allowed_devices_map.AddDevice(kTestOrigin1, kDeviceAddress1, options1); |
334 const std::string& device_id2 = | 355 const std::string& device_id2 = |
335 allowed_devices_map.AddDevice(kTestOrigin2, kDeviceAddress1, options2); | 356 allowed_devices_map.AddDevice(kTestOrigin2, kDeviceAddress1, options2); |
336 | 357 |
337 // Access allowed services. | 358 // Access allowed services. |
338 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessService( | 359 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessService( |
339 kTestOrigin1, device_id1, kGlucoseUUID)); | 360 kTestOrigin1, device_id1, kGlucoseUUID)); |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
377 | 398 |
378 TEST_F(BluetoothAllowedDevicesMapTest, MergeServices) { | 399 TEST_F(BluetoothAllowedDevicesMapTest, MergeServices) { |
379 BluetoothAllowedDevicesMap allowed_devices_map; | 400 BluetoothAllowedDevicesMap allowed_devices_map; |
380 | 401 |
381 // Setup first request. | 402 // Setup first request. |
382 blink::mojom::WebBluetoothRequestDeviceOptionsPtr options1 = | 403 blink::mojom::WebBluetoothRequestDeviceOptionsPtr options1 = |
383 blink::mojom::WebBluetoothRequestDeviceOptions::New(); | 404 blink::mojom::WebBluetoothRequestDeviceOptions::New(); |
384 blink::mojom::WebBluetoothScanFilterPtr scanFilter1 = | 405 blink::mojom::WebBluetoothScanFilterPtr scanFilter1 = |
385 blink::mojom::WebBluetoothScanFilter::New(); | 406 blink::mojom::WebBluetoothScanFilter::New(); |
386 | 407 |
387 scanFilter1->services.push_back(kGlucoseUUID); | 408 scanFilter1->services.push_back(GetGlucoseUUIDOpt()); |
388 options1->filters.push_back(std::move(scanFilter1)); | 409 options1->filters.push_back(std::move(scanFilter1)); |
389 | 410 |
390 options1->optional_services.push_back(kBatteryServiceUUID); | 411 options1->optional_services.push_back(GetBatteryServiceUUIDOpt()); |
391 | 412 |
392 // Add to map. | 413 // Add to map. |
393 const std::string device_id1 = | 414 const std::string device_id1 = |
394 allowed_devices_map.AddDevice(kTestOrigin1, kDeviceAddress1, options1); | 415 allowed_devices_map.AddDevice(kTestOrigin1, kDeviceAddress1, options1); |
395 | 416 |
396 // Setup second request. | 417 // Setup second request. |
397 blink::mojom::WebBluetoothRequestDeviceOptionsPtr options2 = | 418 blink::mojom::WebBluetoothRequestDeviceOptionsPtr options2 = |
398 blink::mojom::WebBluetoothRequestDeviceOptions::New(); | 419 blink::mojom::WebBluetoothRequestDeviceOptions::New(); |
399 blink::mojom::WebBluetoothScanFilterPtr scanFilter2 = | 420 blink::mojom::WebBluetoothScanFilterPtr scanFilter2 = |
400 blink::mojom::WebBluetoothScanFilter::New(); | 421 blink::mojom::WebBluetoothScanFilter::New(); |
401 | 422 |
402 scanFilter2->services.push_back(kHeartRateUUID); | 423 scanFilter2->services.push_back(GetHeartRateUUIDOpt()); |
403 options2->filters.push_back(std::move(scanFilter2)); | 424 options2->filters.push_back(std::move(scanFilter2)); |
404 | 425 |
405 options2->optional_services.push_back(kBloodPressureUUID); | 426 options2->optional_services.push_back(GetBloodPressureUUIDOpt()); |
406 | 427 |
407 // Add to map again. | 428 // Add to map again. |
408 const std::string device_id2 = | 429 const std::string device_id2 = |
409 allowed_devices_map.AddDevice(kTestOrigin1, kDeviceAddress1, options2); | 430 allowed_devices_map.AddDevice(kTestOrigin1, kDeviceAddress1, options2); |
410 | 431 |
411 EXPECT_EQ(device_id1, device_id2); | 432 EXPECT_EQ(device_id1, device_id2); |
412 | 433 |
413 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessService( | 434 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessService( |
414 kTestOrigin1, device_id1, kGlucoseUUID)); | 435 kTestOrigin1, device_id1, kGlucoseUUID)); |
415 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessService( | 436 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessService( |
(...skipping 10 matching lines...) Expand all Loading... | |
426 const std::string& device_id = allowed_devices_map.AddDevice( | 447 const std::string& device_id = allowed_devices_map.AddDevice( |
427 kTestOrigin1, kDeviceAddress1, empty_options_); | 448 kTestOrigin1, kDeviceAddress1, empty_options_); |
428 | 449 |
429 EXPECT_TRUE(device_id.size() == 24) | 450 EXPECT_TRUE(device_id.size() == 24) |
430 << "Expected Lenghth of a 128bit string encoded to Base64."; | 451 << "Expected Lenghth of a 128bit string encoded to Base64."; |
431 EXPECT_TRUE((device_id[22] == '=') && (device_id[23] == '=')) | 452 EXPECT_TRUE((device_id[22] == '=') && (device_id[23] == '=')) |
432 << "Expected padding characters for a 128bit string encoded to Base64."; | 453 << "Expected padding characters for a 128bit string encoded to Base64."; |
433 } | 454 } |
434 | 455 |
435 } // namespace content | 456 } // namespace content |
OLD | NEW |