| 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 "content/common/bluetooth/bluetooth_device_id.h" |
| 8 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
| 9 #include "url/gurl.h" | 10 #include "url/gurl.h" |
| 10 | 11 |
| 11 using device::BluetoothUUID; | 12 using device::BluetoothUUID; |
| 12 | 13 |
| 13 namespace content { | 14 namespace content { |
| 14 namespace { | 15 namespace { |
| 15 const url::Origin kTestOrigin1(GURL("https://www.example1.com")); | 16 const url::Origin kTestOrigin1(GURL("https://www.example1.com")); |
| 16 const url::Origin kTestOrigin2(GURL("https://www.example2.com")); | 17 const url::Origin kTestOrigin2(GURL("https://www.example2.com")); |
| 17 | 18 |
| (...skipping 28 matching lines...) Expand all Loading... |
| 46 BluetoothAllowedDevicesMap allowed_devices_map; | 47 BluetoothAllowedDevicesMap allowed_devices_map; |
| 47 | 48 |
| 48 EXPECT_DEATH_IF_SUPPORTED(allowed_devices_map.AddDevice( | 49 EXPECT_DEATH_IF_SUPPORTED(allowed_devices_map.AddDevice( |
| 49 url::Origin(), kDeviceAddress1, empty_options_), | 50 url::Origin(), kDeviceAddress1, empty_options_), |
| 50 ""); | 51 ""); |
| 51 } | 52 } |
| 52 | 53 |
| 53 TEST_F(BluetoothAllowedDevicesMapTest, AddDeviceToMap) { | 54 TEST_F(BluetoothAllowedDevicesMapTest, AddDeviceToMap) { |
| 54 BluetoothAllowedDevicesMap allowed_devices_map; | 55 BluetoothAllowedDevicesMap allowed_devices_map; |
| 55 | 56 |
| 56 const std::string& device_id = allowed_devices_map.AddDevice( | 57 const BluetoothDeviceId& device_id = allowed_devices_map.AddDevice( |
| 57 kTestOrigin1, kDeviceAddress1, empty_options_); | 58 kTestOrigin1, kDeviceAddress1, empty_options_); |
| 58 | 59 |
| 59 // Test that we can retrieve the device address/id. | 60 // Test that we can retrieve the device address/id. |
| 60 EXPECT_EQ(device_id, | 61 EXPECT_EQ(device_id, |
| 61 allowed_devices_map.GetDeviceId(kTestOrigin1, kDeviceAddress1)); | 62 *allowed_devices_map.GetDeviceId(kTestOrigin1, kDeviceAddress1)); |
| 62 EXPECT_EQ(kDeviceAddress1, | 63 EXPECT_EQ(kDeviceAddress1, |
| 63 allowed_devices_map.GetDeviceAddress(kTestOrigin1, device_id)); | 64 allowed_devices_map.GetDeviceAddress(kTestOrigin1, device_id)); |
| 64 } | 65 } |
| 65 | 66 |
| 66 TEST_F(BluetoothAllowedDevicesMapTest, AddDeviceToMapTwice) { | 67 TEST_F(BluetoothAllowedDevicesMapTest, AddDeviceToMapTwice) { |
| 67 BluetoothAllowedDevicesMap allowed_devices_map; | 68 BluetoothAllowedDevicesMap allowed_devices_map; |
| 68 const std::string& device_id1 = allowed_devices_map.AddDevice( | 69 const BluetoothDeviceId& device_id1 = allowed_devices_map.AddDevice( |
| 69 kTestOrigin1, kDeviceAddress1, empty_options_); | 70 kTestOrigin1, kDeviceAddress1, empty_options_); |
| 70 const std::string& device_id2 = allowed_devices_map.AddDevice( | 71 const BluetoothDeviceId& device_id2 = allowed_devices_map.AddDevice( |
| 71 kTestOrigin1, kDeviceAddress1, empty_options_); | 72 kTestOrigin1, kDeviceAddress1, empty_options_); |
| 72 | 73 |
| 73 EXPECT_EQ(device_id1, device_id2); | 74 EXPECT_EQ(device_id1, device_id2); |
| 74 | 75 |
| 75 // Test that we can retrieve the device address/id. | 76 // Test that we can retrieve the device address/id. |
| 76 EXPECT_EQ(device_id1, | 77 EXPECT_EQ(device_id1, |
| 77 allowed_devices_map.GetDeviceId(kTestOrigin1, kDeviceAddress1)); | 78 *allowed_devices_map.GetDeviceId(kTestOrigin1, kDeviceAddress1)); |
| 78 EXPECT_EQ(kDeviceAddress1, | 79 EXPECT_EQ(kDeviceAddress1, |
| 79 allowed_devices_map.GetDeviceAddress(kTestOrigin1, device_id1)); | 80 allowed_devices_map.GetDeviceAddress(kTestOrigin1, device_id1)); |
| 80 } | 81 } |
| 81 | 82 |
| 82 TEST_F(BluetoothAllowedDevicesMapTest, AddTwoDevicesFromSameOriginToMap) { | 83 TEST_F(BluetoothAllowedDevicesMapTest, AddTwoDevicesFromSameOriginToMap) { |
| 83 BluetoothAllowedDevicesMap allowed_devices_map; | 84 BluetoothAllowedDevicesMap allowed_devices_map; |
| 84 const std::string& device_id1 = allowed_devices_map.AddDevice( | 85 const BluetoothDeviceId& device_id1 = allowed_devices_map.AddDevice( |
| 85 kTestOrigin1, kDeviceAddress1, empty_options_); | 86 kTestOrigin1, kDeviceAddress1, empty_options_); |
| 86 const std::string& device_id2 = allowed_devices_map.AddDevice( | 87 const BluetoothDeviceId& device_id2 = allowed_devices_map.AddDevice( |
| 87 kTestOrigin1, kDeviceAddress2, empty_options_); | 88 kTestOrigin1, kDeviceAddress2, empty_options_); |
| 88 | 89 |
| 89 EXPECT_NE(device_id1, device_id2); | 90 EXPECT_NE(device_id1, device_id2); |
| 90 | 91 |
| 91 // Test that we can retrieve the device address/id. | 92 // Test that we can retrieve the device address/id. |
| 92 EXPECT_EQ(device_id1, | 93 EXPECT_EQ(device_id1, |
| 93 allowed_devices_map.GetDeviceId(kTestOrigin1, kDeviceAddress1)); | 94 *allowed_devices_map.GetDeviceId(kTestOrigin1, kDeviceAddress1)); |
| 94 EXPECT_EQ(device_id2, | 95 EXPECT_EQ(device_id2, |
| 95 allowed_devices_map.GetDeviceId(kTestOrigin1, kDeviceAddress2)); | 96 *allowed_devices_map.GetDeviceId(kTestOrigin1, kDeviceAddress2)); |
| 96 | 97 |
| 97 EXPECT_EQ(kDeviceAddress1, | 98 EXPECT_EQ(kDeviceAddress1, |
| 98 allowed_devices_map.GetDeviceAddress(kTestOrigin1, device_id1)); | 99 allowed_devices_map.GetDeviceAddress(kTestOrigin1, device_id1)); |
| 99 EXPECT_EQ(kDeviceAddress2, | 100 EXPECT_EQ(kDeviceAddress2, |
| 100 allowed_devices_map.GetDeviceAddress(kTestOrigin1, device_id2)); | 101 allowed_devices_map.GetDeviceAddress(kTestOrigin1, device_id2)); |
| 101 } | 102 } |
| 102 | 103 |
| 103 TEST_F(BluetoothAllowedDevicesMapTest, AddTwoDevicesFromTwoOriginsToMap) { | 104 TEST_F(BluetoothAllowedDevicesMapTest, AddTwoDevicesFromTwoOriginsToMap) { |
| 104 BluetoothAllowedDevicesMap allowed_devices_map; | 105 BluetoothAllowedDevicesMap allowed_devices_map; |
| 105 const std::string& device_id1 = allowed_devices_map.AddDevice( | 106 const BluetoothDeviceId& device_id1 = allowed_devices_map.AddDevice( |
| 106 kTestOrigin1, kDeviceAddress1, empty_options_); | 107 kTestOrigin1, kDeviceAddress1, empty_options_); |
| 107 const std::string& device_id2 = allowed_devices_map.AddDevice( | 108 const BluetoothDeviceId& device_id2 = allowed_devices_map.AddDevice( |
| 108 kTestOrigin2, kDeviceAddress2, empty_options_); | 109 kTestOrigin2, kDeviceAddress2, empty_options_); |
| 109 | 110 |
| 110 EXPECT_NE(device_id1, device_id2); | 111 EXPECT_NE(device_id1, device_id2); |
| 111 | 112 |
| 112 // Test that the wrong origin doesn't have access to the device. | 113 // Test that the wrong origin doesn't have access to the device. |
| 113 | 114 |
| 114 EXPECT_EQ(base::EmptyString(), | 115 EXPECT_EQ(nullptr, |
| 115 allowed_devices_map.GetDeviceId(kTestOrigin1, kDeviceAddress2)); | 116 allowed_devices_map.GetDeviceId(kTestOrigin1, kDeviceAddress2)); |
| 116 EXPECT_EQ(base::EmptyString(), | 117 EXPECT_EQ(nullptr, |
| 117 allowed_devices_map.GetDeviceId(kTestOrigin2, kDeviceAddress1)); | 118 allowed_devices_map.GetDeviceId(kTestOrigin2, kDeviceAddress1)); |
| 118 | 119 |
| 119 EXPECT_EQ(base::EmptyString(), | 120 EXPECT_EQ(base::EmptyString(), |
| 120 allowed_devices_map.GetDeviceAddress(kTestOrigin1, device_id2)); | 121 allowed_devices_map.GetDeviceAddress(kTestOrigin1, device_id2)); |
| 121 EXPECT_EQ(base::EmptyString(), | 122 EXPECT_EQ(base::EmptyString(), |
| 122 allowed_devices_map.GetDeviceAddress(kTestOrigin2, device_id1)); | 123 allowed_devices_map.GetDeviceAddress(kTestOrigin2, device_id1)); |
| 123 | 124 |
| 124 // Test that we can retrieve the device address/id. | 125 // Test that we can retrieve the device address/id. |
| 125 EXPECT_EQ(device_id1, | 126 EXPECT_EQ(device_id1, |
| 126 allowed_devices_map.GetDeviceId(kTestOrigin1, kDeviceAddress1)); | 127 *allowed_devices_map.GetDeviceId(kTestOrigin1, kDeviceAddress1)); |
| 127 EXPECT_EQ(device_id2, | 128 EXPECT_EQ(device_id2, |
| 128 allowed_devices_map.GetDeviceId(kTestOrigin2, kDeviceAddress2)); | 129 *allowed_devices_map.GetDeviceId(kTestOrigin2, kDeviceAddress2)); |
| 129 | 130 |
| 130 EXPECT_EQ(kDeviceAddress1, | 131 EXPECT_EQ(kDeviceAddress1, |
| 131 allowed_devices_map.GetDeviceAddress(kTestOrigin1, device_id1)); | 132 allowed_devices_map.GetDeviceAddress(kTestOrigin1, device_id1)); |
| 132 EXPECT_EQ(kDeviceAddress2, | 133 EXPECT_EQ(kDeviceAddress2, |
| 133 allowed_devices_map.GetDeviceAddress(kTestOrigin2, device_id2)); | 134 allowed_devices_map.GetDeviceAddress(kTestOrigin2, device_id2)); |
| 134 } | 135 } |
| 135 | 136 |
| 136 TEST_F(BluetoothAllowedDevicesMapTest, AddDeviceFromTwoOriginsToMap) { | 137 TEST_F(BluetoothAllowedDevicesMapTest, AddDeviceFromTwoOriginsToMap) { |
| 137 BluetoothAllowedDevicesMap allowed_devices_map; | 138 BluetoothAllowedDevicesMap allowed_devices_map; |
| 138 const std::string& device_id1 = allowed_devices_map.AddDevice( | 139 const BluetoothDeviceId& device_id1 = allowed_devices_map.AddDevice( |
| 139 kTestOrigin1, kDeviceAddress1, empty_options_); | 140 kTestOrigin1, kDeviceAddress1, empty_options_); |
| 140 const std::string& device_id2 = allowed_devices_map.AddDevice( | 141 const BluetoothDeviceId& device_id2 = allowed_devices_map.AddDevice( |
| 141 kTestOrigin2, kDeviceAddress1, empty_options_); | 142 kTestOrigin2, kDeviceAddress1, empty_options_); |
| 142 | 143 |
| 143 EXPECT_NE(device_id1, device_id2); | 144 EXPECT_NE(device_id1, device_id2); |
| 144 | 145 |
| 145 // Test that the wrong origin doesn't have access to the device. | 146 // Test that the wrong origin doesn't have access to the device. |
| 146 EXPECT_EQ(base::EmptyString(), | 147 EXPECT_EQ(base::EmptyString(), |
| 147 allowed_devices_map.GetDeviceAddress(kTestOrigin1, device_id2)); | 148 allowed_devices_map.GetDeviceAddress(kTestOrigin1, device_id2)); |
| 148 EXPECT_EQ(base::EmptyString(), | 149 EXPECT_EQ(base::EmptyString(), |
| 149 allowed_devices_map.GetDeviceAddress(kTestOrigin2, device_id1)); | 150 allowed_devices_map.GetDeviceAddress(kTestOrigin2, device_id1)); |
| 150 } | 151 } |
| 151 | 152 |
| 152 TEST_F(BluetoothAllowedDevicesMapTest, AddRemoveAddDeviceToMap) { | 153 TEST_F(BluetoothAllowedDevicesMapTest, AddRemoveAddDeviceToMap) { |
| 153 BluetoothAllowedDevicesMap allowed_devices_map; | 154 BluetoothAllowedDevicesMap allowed_devices_map; |
| 154 const std::string device_id_first_time = allowed_devices_map.AddDevice( | 155 const BluetoothDeviceId device_id_first_time = allowed_devices_map.AddDevice( |
| 155 kTestOrigin1, kDeviceAddress1, empty_options_); | 156 kTestOrigin1, kDeviceAddress1, empty_options_); |
| 156 | 157 |
| 157 allowed_devices_map.RemoveDevice(kTestOrigin1, kDeviceAddress1); | 158 allowed_devices_map.RemoveDevice(kTestOrigin1, kDeviceAddress1); |
| 158 | 159 |
| 159 const std::string device_id_second_time = allowed_devices_map.AddDevice( | 160 const BluetoothDeviceId device_id_second_time = allowed_devices_map.AddDevice( |
| 160 kTestOrigin1, kDeviceAddress1, empty_options_); | 161 kTestOrigin1, kDeviceAddress1, empty_options_); |
| 161 | 162 |
| 162 EXPECT_NE(device_id_first_time, device_id_second_time); | 163 EXPECT_NE(device_id_first_time, device_id_second_time); |
| 163 } | 164 } |
| 164 | 165 |
| 165 TEST_F(BluetoothAllowedDevicesMapTest, RemoveDeviceFromMap) { | 166 TEST_F(BluetoothAllowedDevicesMapTest, RemoveDeviceFromMap) { |
| 166 BluetoothAllowedDevicesMap allowed_devices_map; | 167 BluetoothAllowedDevicesMap allowed_devices_map; |
| 167 | 168 |
| 168 const std::string& device_id = allowed_devices_map.AddDevice( | 169 const BluetoothDeviceId device_id = allowed_devices_map.AddDevice( |
| 169 kTestOrigin1, kDeviceAddress1, empty_options_); | 170 kTestOrigin1, kDeviceAddress1, empty_options_); |
| 170 | 171 |
| 171 allowed_devices_map.RemoveDevice(kTestOrigin1, kDeviceAddress1); | 172 allowed_devices_map.RemoveDevice(kTestOrigin1, kDeviceAddress1); |
| 172 | 173 |
| 174 EXPECT_EQ(nullptr, |
| 175 allowed_devices_map.GetDeviceId(kTestOrigin1, kDeviceAddress1)); |
| 173 EXPECT_EQ(base::EmptyString(), | 176 EXPECT_EQ(base::EmptyString(), |
| 174 allowed_devices_map.GetDeviceId(kTestOrigin1, device_id)); | 177 allowed_devices_map.GetDeviceAddress(kTestOrigin1, device_id)); |
| 175 EXPECT_EQ(base::EmptyString(), allowed_devices_map.GetDeviceAddress( | |
| 176 kTestOrigin1, kDeviceAddress1)); | |
| 177 } | 178 } |
| 178 | 179 |
| 179 TEST_F(BluetoothAllowedDevicesMapTest, AllowedServices_OneOriginOneDevice) { | 180 TEST_F(BluetoothAllowedDevicesMapTest, AllowedServices_OneOriginOneDevice) { |
| 180 BluetoothAllowedDevicesMap allowed_devices_map; | 181 BluetoothAllowedDevicesMap allowed_devices_map; |
| 181 | 182 |
| 182 // Setup device. | 183 // Setup device. |
| 183 blink::mojom::WebBluetoothRequestDeviceOptionsPtr options = | 184 blink::mojom::WebBluetoothRequestDeviceOptionsPtr options = |
| 184 blink::mojom::WebBluetoothRequestDeviceOptions::New(); | 185 blink::mojom::WebBluetoothRequestDeviceOptions::New(); |
| 185 blink::mojom::WebBluetoothScanFilterPtr scanFilter1 = | 186 blink::mojom::WebBluetoothScanFilterPtr scanFilter1 = |
| 186 blink::mojom::WebBluetoothScanFilter::New(); | 187 blink::mojom::WebBluetoothScanFilter::New(); |
| 187 blink::mojom::WebBluetoothScanFilterPtr scanFilter2 = | 188 blink::mojom::WebBluetoothScanFilterPtr scanFilter2 = |
| 188 blink::mojom::WebBluetoothScanFilter::New(); | 189 blink::mojom::WebBluetoothScanFilter::New(); |
| 189 | 190 |
| 190 scanFilter1->services.push_back(kGlucoseUUID); | 191 scanFilter1->services.push_back(kGlucoseUUID); |
| 191 options->filters.push_back(scanFilter1.Clone()); | 192 options->filters.push_back(scanFilter1.Clone()); |
| 192 | 193 |
| 193 scanFilter2->services.push_back(kHeartRateUUID); | 194 scanFilter2->services.push_back(kHeartRateUUID); |
| 194 options->filters.push_back(scanFilter2.Clone()); | 195 options->filters.push_back(scanFilter2.Clone()); |
| 195 | 196 |
| 196 options->optional_services.push_back(kBatteryServiceUUID); | 197 options->optional_services.push_back(kBatteryServiceUUID); |
| 197 options->optional_services.push_back(kHeartRateUUID); | 198 options->optional_services.push_back(kHeartRateUUID); |
| 198 | 199 |
| 199 // Add to map. | 200 // Add to map. |
| 200 const std::string device_id1 = | 201 const BluetoothDeviceId device_id1 = |
| 201 allowed_devices_map.AddDevice(kTestOrigin1, kDeviceAddress1, options); | 202 allowed_devices_map.AddDevice(kTestOrigin1, kDeviceAddress1, options); |
| 202 | 203 |
| 203 // Access allowed services. | 204 // Access allowed services. |
| 204 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessService( | 205 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessService( |
| 205 kTestOrigin1, device_id1, kGlucoseUUID)); | 206 kTestOrigin1, device_id1, kGlucoseUUID)); |
| 206 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessService( | 207 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessService( |
| 207 kTestOrigin1, device_id1, kHeartRateUUID)); | 208 kTestOrigin1, device_id1, kHeartRateUUID)); |
| 208 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessService( | 209 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessService( |
| 209 kTestOrigin1, device_id1, kBatteryServiceUUID)); | 210 kTestOrigin1, device_id1, kBatteryServiceUUID)); |
| 210 | 211 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 222 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessService( | 223 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessService( |
| 223 kTestOrigin1, device_id1, kBatteryServiceUUID)); | 224 kTestOrigin1, device_id1, kBatteryServiceUUID)); |
| 224 | 225 |
| 225 // Add device back. | 226 // Add device back. |
| 226 blink::mojom::WebBluetoothRequestDeviceOptionsPtr options2 = | 227 blink::mojom::WebBluetoothRequestDeviceOptionsPtr options2 = |
| 227 blink::mojom::WebBluetoothRequestDeviceOptions::New(); | 228 blink::mojom::WebBluetoothRequestDeviceOptions::New(); |
| 228 | 229 |
| 229 options2->filters.push_back(scanFilter1.Clone()); | 230 options2->filters.push_back(scanFilter1.Clone()); |
| 230 options2->filters.push_back(scanFilter2.Clone()); | 231 options2->filters.push_back(scanFilter2.Clone()); |
| 231 | 232 |
| 232 const std::string device_id2 = | 233 const BluetoothDeviceId device_id2 = |
| 233 allowed_devices_map.AddDevice(kTestOrigin1, kDeviceAddress1, options2); | 234 allowed_devices_map.AddDevice(kTestOrigin1, kDeviceAddress1, options2); |
| 234 | 235 |
| 235 // Access allowed services. | 236 // Access allowed services. |
| 236 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessService( | 237 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessService( |
| 237 kTestOrigin1, device_id2, kGlucoseUUID)); | 238 kTestOrigin1, device_id2, kGlucoseUUID)); |
| 238 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessService( | 239 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessService( |
| 239 kTestOrigin1, device_id2, kHeartRateUUID)); | 240 kTestOrigin1, device_id2, kHeartRateUUID)); |
| 240 | 241 |
| 241 // Try to access a non-allowed service. | 242 // Try to access a non-allowed service. |
| 242 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessService( | 243 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessService( |
| (...skipping 27 matching lines...) Expand all Loading... |
| 270 blink::mojom::WebBluetoothRequestDeviceOptions::New(); | 271 blink::mojom::WebBluetoothRequestDeviceOptions::New(); |
| 271 blink::mojom::WebBluetoothScanFilterPtr scanFilter2 = | 272 blink::mojom::WebBluetoothScanFilterPtr scanFilter2 = |
| 272 blink::mojom::WebBluetoothScanFilter::New(); | 273 blink::mojom::WebBluetoothScanFilter::New(); |
| 273 | 274 |
| 274 scanFilter2->services.push_back(kBatteryServiceUUID); | 275 scanFilter2->services.push_back(kBatteryServiceUUID); |
| 275 options2->filters.push_back(std::move(scanFilter2)); | 276 options2->filters.push_back(std::move(scanFilter2)); |
| 276 | 277 |
| 277 options2->optional_services.push_back(kBloodPressureUUID); | 278 options2->optional_services.push_back(kBloodPressureUUID); |
| 278 | 279 |
| 279 // Add devices to map. | 280 // Add devices to map. |
| 280 const std::string& device_id1 = | 281 const BluetoothDeviceId& device_id1 = |
| 281 allowed_devices_map.AddDevice(kTestOrigin1, kDeviceAddress1, options1); | 282 allowed_devices_map.AddDevice(kTestOrigin1, kDeviceAddress1, options1); |
| 282 const std::string& device_id2 = | 283 const BluetoothDeviceId& device_id2 = |
| 283 allowed_devices_map.AddDevice(kTestOrigin1, kDeviceAddress2, options2); | 284 allowed_devices_map.AddDevice(kTestOrigin1, kDeviceAddress2, options2); |
| 284 | 285 |
| 285 // Access allowed services. | 286 // Access allowed services. |
| 286 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessService( | 287 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessService( |
| 287 kTestOrigin1, device_id1, kGlucoseUUID)); | 288 kTestOrigin1, device_id1, kGlucoseUUID)); |
| 288 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessService( | 289 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessService( |
| 289 kTestOrigin1, device_id1, kHeartRateUUID)); | 290 kTestOrigin1, device_id1, kHeartRateUUID)); |
| 290 | 291 |
| 291 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessService( | 292 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessService( |
| 292 kTestOrigin1, device_id2, kBatteryServiceUUID)); | 293 kTestOrigin1, device_id2, kBatteryServiceUUID)); |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 327 blink::mojom::WebBluetoothRequestDeviceOptions::New(); | 328 blink::mojom::WebBluetoothRequestDeviceOptions::New(); |
| 328 blink::mojom::WebBluetoothScanFilterPtr scanFilter2 = | 329 blink::mojom::WebBluetoothScanFilterPtr scanFilter2 = |
| 329 blink::mojom::WebBluetoothScanFilter::New(); | 330 blink::mojom::WebBluetoothScanFilter::New(); |
| 330 | 331 |
| 331 scanFilter2->services.push_back(kBatteryServiceUUID); | 332 scanFilter2->services.push_back(kBatteryServiceUUID); |
| 332 options2->filters.push_back(std::move(scanFilter2)); | 333 options2->filters.push_back(std::move(scanFilter2)); |
| 333 | 334 |
| 334 options2->optional_services.push_back(kBloodPressureUUID); | 335 options2->optional_services.push_back(kBloodPressureUUID); |
| 335 | 336 |
| 336 // Add devices to map. | 337 // Add devices to map. |
| 337 const std::string& device_id1 = | 338 const BluetoothDeviceId& device_id1 = |
| 338 allowed_devices_map.AddDevice(kTestOrigin1, kDeviceAddress1, options1); | 339 allowed_devices_map.AddDevice(kTestOrigin1, kDeviceAddress1, options1); |
| 339 const std::string& device_id2 = | 340 const BluetoothDeviceId& device_id2 = |
| 340 allowed_devices_map.AddDevice(kTestOrigin2, kDeviceAddress1, options2); | 341 allowed_devices_map.AddDevice(kTestOrigin2, kDeviceAddress1, options2); |
| 341 | 342 |
| 342 // Access allowed services. | 343 // Access allowed services. |
| 343 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessService( | 344 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessService( |
| 344 kTestOrigin1, device_id1, kGlucoseUUID)); | 345 kTestOrigin1, device_id1, kGlucoseUUID)); |
| 345 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessService( | 346 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessService( |
| 346 kTestOrigin1, device_id1, kHeartRateUUID)); | 347 kTestOrigin1, device_id1, kHeartRateUUID)); |
| 347 | 348 |
| 348 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessService( | 349 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessService( |
| 349 kTestOrigin2, device_id2, kBatteryServiceUUID)); | 350 kTestOrigin2, device_id2, kBatteryServiceUUID)); |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 388 blink::mojom::WebBluetoothRequestDeviceOptions::New(); | 389 blink::mojom::WebBluetoothRequestDeviceOptions::New(); |
| 389 blink::mojom::WebBluetoothScanFilterPtr scanFilter1 = | 390 blink::mojom::WebBluetoothScanFilterPtr scanFilter1 = |
| 390 blink::mojom::WebBluetoothScanFilter::New(); | 391 blink::mojom::WebBluetoothScanFilter::New(); |
| 391 | 392 |
| 392 scanFilter1->services.push_back(kGlucoseUUID); | 393 scanFilter1->services.push_back(kGlucoseUUID); |
| 393 options1->filters.push_back(std::move(scanFilter1)); | 394 options1->filters.push_back(std::move(scanFilter1)); |
| 394 | 395 |
| 395 options1->optional_services.push_back(kBatteryServiceUUID); | 396 options1->optional_services.push_back(kBatteryServiceUUID); |
| 396 | 397 |
| 397 // Add to map. | 398 // Add to map. |
| 398 const std::string device_id1 = | 399 const BluetoothDeviceId device_id1 = |
| 399 allowed_devices_map.AddDevice(kTestOrigin1, kDeviceAddress1, options1); | 400 allowed_devices_map.AddDevice(kTestOrigin1, kDeviceAddress1, options1); |
| 400 | 401 |
| 401 // Setup second request. | 402 // Setup second request. |
| 402 blink::mojom::WebBluetoothRequestDeviceOptionsPtr options2 = | 403 blink::mojom::WebBluetoothRequestDeviceOptionsPtr options2 = |
| 403 blink::mojom::WebBluetoothRequestDeviceOptions::New(); | 404 blink::mojom::WebBluetoothRequestDeviceOptions::New(); |
| 404 blink::mojom::WebBluetoothScanFilterPtr scanFilter2 = | 405 blink::mojom::WebBluetoothScanFilterPtr scanFilter2 = |
| 405 blink::mojom::WebBluetoothScanFilter::New(); | 406 blink::mojom::WebBluetoothScanFilter::New(); |
| 406 | 407 |
| 407 scanFilter2->services.push_back(kHeartRateUUID); | 408 scanFilter2->services.push_back(kHeartRateUUID); |
| 408 options2->filters.push_back(std::move(scanFilter2)); | 409 options2->filters.push_back(std::move(scanFilter2)); |
| 409 | 410 |
| 410 options2->optional_services.push_back(kBloodPressureUUID); | 411 options2->optional_services.push_back(kBloodPressureUUID); |
| 411 | 412 |
| 412 // Add to map again. | 413 // Add to map again. |
| 413 const std::string device_id2 = | 414 const BluetoothDeviceId device_id2 = |
| 414 allowed_devices_map.AddDevice(kTestOrigin1, kDeviceAddress1, options2); | 415 allowed_devices_map.AddDevice(kTestOrigin1, kDeviceAddress1, options2); |
| 415 | 416 |
| 416 EXPECT_EQ(device_id1, device_id2); | 417 EXPECT_EQ(device_id1, device_id2); |
| 417 | 418 |
| 418 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessService( | 419 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessService( |
| 419 kTestOrigin1, device_id1, kGlucoseUUID)); | 420 kTestOrigin1, device_id1, kGlucoseUUID)); |
| 420 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessService( | 421 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessService( |
| 421 kTestOrigin1, device_id1, kBatteryServiceUUID)); | 422 kTestOrigin1, device_id1, kBatteryServiceUUID)); |
| 422 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessService( | 423 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessService( |
| 423 kTestOrigin1, device_id1, kHeartRateUUID)); | 424 kTestOrigin1, device_id1, kHeartRateUUID)); |
| 424 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessService( | 425 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessService( |
| 425 kTestOrigin1, device_id1, kBloodPressureUUID)); | 426 kTestOrigin1, device_id1, kBloodPressureUUID)); |
| 426 } | 427 } |
| 427 | 428 |
| 428 TEST_F(BluetoothAllowedDevicesMapTest, CorrectIdFormat) { | 429 TEST_F(BluetoothAllowedDevicesMapTest, CorrectIdFormat) { |
| 429 BluetoothAllowedDevicesMap allowed_devices_map; | 430 BluetoothAllowedDevicesMap allowed_devices_map; |
| 430 | 431 |
| 431 const std::string& device_id = allowed_devices_map.AddDevice( | 432 const BluetoothDeviceId& device_id = allowed_devices_map.AddDevice( |
| 432 kTestOrigin1, kDeviceAddress1, empty_options_); | 433 kTestOrigin1, kDeviceAddress1, empty_options_); |
| 433 | 434 |
| 434 EXPECT_TRUE(device_id.size() == 24) | 435 EXPECT_TRUE(BluetoothDeviceId::IsValid(device_id.str())); |
| 435 << "Expected Lenghth of a 128bit string encoded to Base64."; | |
| 436 EXPECT_TRUE((device_id[22] == '=') && (device_id[23] == '=')) | |
| 437 << "Expected padding characters for a 128bit string encoded to Base64."; | |
| 438 } | 436 } |
| 439 | 437 |
| 440 } // namespace content | 438 } // namespace content |
| OLD | NEW |