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