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_scan_filter.h" | |
9 #include "device/bluetooth/bluetooth_uuid.h" | |
10 #include "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" |
11 #include "url/gurl.h" | 9 #include "url/gurl.h" |
12 | 10 |
13 using device::BluetoothUUID; | 11 using device::BluetoothUUID; |
14 | 12 |
15 namespace content { | 13 namespace content { |
16 namespace { | 14 namespace { |
17 const url::Origin kTestOrigin1(GURL("https://www.example1.com")); | 15 const url::Origin kTestOrigin1(GURL("https://www.example1.com")); |
18 const url::Origin kTestOrigin2(GURL("https://www.example2.com")); | 16 const url::Origin kTestOrigin2(GURL("https://www.example2.com")); |
19 | 17 |
20 const std::string kDeviceAddress1 = "00:00:00"; | 18 const std::string kDeviceAddress1 = "00:00:00"; |
21 const std::string kDeviceAddress2 = "11:11:11"; | 19 const std::string kDeviceAddress2 = "11:11:11"; |
22 | 20 |
23 const char kGlucoseUUIDString[] = "00001808-0000-1000-8000-00805f9b34fb"; | 21 const char kGlucoseUUID[] = "00001808-0000-1000-8000-00805f9b34fb"; |
24 const char kHeartRateUUIDString[] = "0000180d-0000-1000-8000-00805f9b34fb"; | 22 const char kHeartRateUUID[] = "0000180d-0000-1000-8000-00805f9b34fb"; |
25 const char kBatteryServiceUUIDString[] = "0000180f-0000-1000-8000-00805f9b34fb"; | 23 const char kBatteryServiceUUID[] = "0000180f-0000-1000-8000-00805f9b34fb"; |
26 const char kBloodPressureUUIDString[] = "00001813-0000-1000-8000-00805f9b34fb"; | 24 const char kBloodPressureUUID[] = "00001813-0000-1000-8000-00805f9b34fb"; |
27 const char kCyclingPowerUUIDString[] = "00001818-0000-1000-8000-00805f9b34fb"; | 25 const char kCyclingPowerUUID[] = "00001818-0000-1000-8000-00805f9b34fb"; |
28 const BluetoothUUID kGlucoseUUID(kGlucoseUUIDString); | |
29 const BluetoothUUID kHeartRateUUID(kHeartRateUUIDString); | |
30 const BluetoothUUID kBatteryServiceUUID(kBatteryServiceUUIDString); | |
31 const BluetoothUUID kBloodPressureUUID(kBloodPressureUUIDString); | |
32 const BluetoothUUID kCyclingPowerUUID(kCyclingPowerUUIDString); | |
33 | 26 |
34 const std::vector<content::BluetoothScanFilter> kEmptyFilters = | 27 class BluetoothAllowedDevicesMapTest : public testing::Test { |
35 std::vector<BluetoothScanFilter>(); | 28 protected: |
36 const std::vector<device::BluetoothUUID> kEmptyOptionalServices = | 29 BluetoothAllowedDevicesMapTest() { |
37 std::vector<device::BluetoothUUID>(); | 30 empty_options_ = blink::mojom::WebBluetoothRequestDeviceOptions::New(); |
| 31 } |
| 32 |
| 33 ~BluetoothAllowedDevicesMapTest() override {} |
| 34 |
| 35 blink::mojom::WebBluetoothRequestDeviceOptionsPtr empty_options_; |
| 36 }; |
| 37 |
38 } // namespace | 38 } // namespace |
39 | 39 |
40 TEST(BluetoothAllowedDevicesMapTest, AddDeviceToMap) { | 40 TEST_F(BluetoothAllowedDevicesMapTest, UniqueOriginNotSupported) { |
| 41 BluetoothAllowedDevicesMap allowed_devices_map; |
| 42 |
| 43 EXPECT_DEATH_IF_SUPPORTED(allowed_devices_map.AddDevice( |
| 44 url::Origin(), kDeviceAddress1, empty_options_), |
| 45 ""); |
| 46 } |
| 47 |
| 48 TEST_F(BluetoothAllowedDevicesMapTest, AddDeviceToMap) { |
41 BluetoothAllowedDevicesMap allowed_devices_map; | 49 BluetoothAllowedDevicesMap allowed_devices_map; |
42 | 50 |
43 const std::string& device_id = allowed_devices_map.AddDevice( | 51 const std::string& device_id = allowed_devices_map.AddDevice( |
44 kTestOrigin1, kDeviceAddress1, kEmptyFilters, kEmptyOptionalServices); | 52 kTestOrigin1, kDeviceAddress1, empty_options_); |
45 | 53 |
46 // Test that we can retrieve the device address/id. | 54 // Test that we can retrieve the device address/id. |
47 EXPECT_EQ(device_id, | 55 EXPECT_EQ(device_id, |
48 allowed_devices_map.GetDeviceId(kTestOrigin1, kDeviceAddress1)); | 56 allowed_devices_map.GetDeviceId(kTestOrigin1, kDeviceAddress1)); |
49 EXPECT_EQ(kDeviceAddress1, | 57 EXPECT_EQ(kDeviceAddress1, |
50 allowed_devices_map.GetDeviceAddress(kTestOrigin1, device_id)); | 58 allowed_devices_map.GetDeviceAddress(kTestOrigin1, device_id)); |
51 } | 59 } |
52 | 60 |
53 TEST(BluetoothAllowedDevicesMapTest, AddDeviceToMapTwice) { | 61 TEST_F(BluetoothAllowedDevicesMapTest, AddDeviceToMapTwice) { |
54 BluetoothAllowedDevicesMap allowed_devices_map; | 62 BluetoothAllowedDevicesMap allowed_devices_map; |
55 const std::string& device_id1 = allowed_devices_map.AddDevice( | 63 const std::string& device_id1 = allowed_devices_map.AddDevice( |
56 kTestOrigin1, kDeviceAddress1, kEmptyFilters, kEmptyOptionalServices); | 64 kTestOrigin1, kDeviceAddress1, empty_options_); |
57 const std::string& device_id2 = allowed_devices_map.AddDevice( | 65 const std::string& device_id2 = allowed_devices_map.AddDevice( |
58 kTestOrigin1, kDeviceAddress1, kEmptyFilters, kEmptyOptionalServices); | 66 kTestOrigin1, kDeviceAddress1, empty_options_); |
59 | 67 |
60 EXPECT_EQ(device_id1, device_id2); | 68 EXPECT_EQ(device_id1, device_id2); |
61 | 69 |
62 // Test that we can retrieve the device address/id. | 70 // Test that we can retrieve the device address/id. |
63 EXPECT_EQ(device_id1, | 71 EXPECT_EQ(device_id1, |
64 allowed_devices_map.GetDeviceId(kTestOrigin1, kDeviceAddress1)); | 72 allowed_devices_map.GetDeviceId(kTestOrigin1, kDeviceAddress1)); |
65 EXPECT_EQ(kDeviceAddress1, | 73 EXPECT_EQ(kDeviceAddress1, |
66 allowed_devices_map.GetDeviceAddress(kTestOrigin1, device_id1)); | 74 allowed_devices_map.GetDeviceAddress(kTestOrigin1, device_id1)); |
67 } | 75 } |
68 | 76 |
69 TEST(BluetoothAllowedDevicesMapTest, AddTwoDevicesFromSameOriginToMap) { | 77 TEST_F(BluetoothAllowedDevicesMapTest, AddTwoDevicesFromSameOriginToMap) { |
70 BluetoothAllowedDevicesMap allowed_devices_map; | 78 BluetoothAllowedDevicesMap allowed_devices_map; |
71 const std::string& device_id1 = allowed_devices_map.AddDevice( | 79 const std::string& device_id1 = allowed_devices_map.AddDevice( |
72 kTestOrigin1, kDeviceAddress1, kEmptyFilters, kEmptyOptionalServices); | 80 kTestOrigin1, kDeviceAddress1, empty_options_); |
73 const std::string& device_id2 = allowed_devices_map.AddDevice( | 81 const std::string& device_id2 = allowed_devices_map.AddDevice( |
74 kTestOrigin1, kDeviceAddress2, kEmptyFilters, kEmptyOptionalServices); | 82 kTestOrigin1, kDeviceAddress2, empty_options_); |
75 | 83 |
76 EXPECT_NE(device_id1, device_id2); | 84 EXPECT_NE(device_id1, device_id2); |
77 | 85 |
78 // Test that we can retrieve the device address/id. | 86 // Test that we can retrieve the device address/id. |
79 EXPECT_EQ(device_id1, | 87 EXPECT_EQ(device_id1, |
80 allowed_devices_map.GetDeviceId(kTestOrigin1, kDeviceAddress1)); | 88 allowed_devices_map.GetDeviceId(kTestOrigin1, kDeviceAddress1)); |
81 EXPECT_EQ(device_id2, | 89 EXPECT_EQ(device_id2, |
82 allowed_devices_map.GetDeviceId(kTestOrigin1, kDeviceAddress2)); | 90 allowed_devices_map.GetDeviceId(kTestOrigin1, kDeviceAddress2)); |
83 | 91 |
84 EXPECT_EQ(kDeviceAddress1, | 92 EXPECT_EQ(kDeviceAddress1, |
85 allowed_devices_map.GetDeviceAddress(kTestOrigin1, device_id1)); | 93 allowed_devices_map.GetDeviceAddress(kTestOrigin1, device_id1)); |
86 EXPECT_EQ(kDeviceAddress2, | 94 EXPECT_EQ(kDeviceAddress2, |
87 allowed_devices_map.GetDeviceAddress(kTestOrigin1, device_id2)); | 95 allowed_devices_map.GetDeviceAddress(kTestOrigin1, device_id2)); |
88 } | 96 } |
89 | 97 |
90 TEST(BluetoothAllowedDevicesMapTest, AddTwoDevicesFromTwoOriginsToMap) { | 98 TEST_F(BluetoothAllowedDevicesMapTest, AddTwoDevicesFromTwoOriginsToMap) { |
91 BluetoothAllowedDevicesMap allowed_devices_map; | 99 BluetoothAllowedDevicesMap allowed_devices_map; |
92 const std::string& device_id1 = allowed_devices_map.AddDevice( | 100 const std::string& device_id1 = allowed_devices_map.AddDevice( |
93 kTestOrigin1, kDeviceAddress1, kEmptyFilters, kEmptyOptionalServices); | 101 kTestOrigin1, kDeviceAddress1, empty_options_); |
94 const std::string& device_id2 = allowed_devices_map.AddDevice( | 102 const std::string& device_id2 = allowed_devices_map.AddDevice( |
95 kTestOrigin2, kDeviceAddress2, kEmptyFilters, kEmptyOptionalServices); | 103 kTestOrigin2, kDeviceAddress2, empty_options_); |
96 | 104 |
97 EXPECT_NE(device_id1, device_id2); | 105 EXPECT_NE(device_id1, device_id2); |
98 | 106 |
99 // Test that the wrong origin doesn't have access to the device. | 107 // Test that the wrong origin doesn't have access to the device. |
100 | 108 |
101 EXPECT_EQ(base::EmptyString(), | 109 EXPECT_EQ(base::EmptyString(), |
102 allowed_devices_map.GetDeviceId(kTestOrigin1, kDeviceAddress2)); | 110 allowed_devices_map.GetDeviceId(kTestOrigin1, kDeviceAddress2)); |
103 EXPECT_EQ(base::EmptyString(), | 111 EXPECT_EQ(base::EmptyString(), |
104 allowed_devices_map.GetDeviceId(kTestOrigin2, kDeviceAddress1)); | 112 allowed_devices_map.GetDeviceId(kTestOrigin2, kDeviceAddress1)); |
105 | 113 |
106 EXPECT_EQ(base::EmptyString(), | 114 EXPECT_EQ(base::EmptyString(), |
107 allowed_devices_map.GetDeviceAddress(kTestOrigin1, device_id2)); | 115 allowed_devices_map.GetDeviceAddress(kTestOrigin1, device_id2)); |
108 EXPECT_EQ(base::EmptyString(), | 116 EXPECT_EQ(base::EmptyString(), |
109 allowed_devices_map.GetDeviceAddress(kTestOrigin2, device_id1)); | 117 allowed_devices_map.GetDeviceAddress(kTestOrigin2, device_id1)); |
110 | 118 |
111 // Test that we can retrieve the device address/id. | 119 // Test that we can retrieve the device address/id. |
112 EXPECT_EQ(device_id1, | 120 EXPECT_EQ(device_id1, |
113 allowed_devices_map.GetDeviceId(kTestOrigin1, kDeviceAddress1)); | 121 allowed_devices_map.GetDeviceId(kTestOrigin1, kDeviceAddress1)); |
114 EXPECT_EQ(device_id2, | 122 EXPECT_EQ(device_id2, |
115 allowed_devices_map.GetDeviceId(kTestOrigin2, kDeviceAddress2)); | 123 allowed_devices_map.GetDeviceId(kTestOrigin2, kDeviceAddress2)); |
116 | 124 |
117 EXPECT_EQ(kDeviceAddress1, | 125 EXPECT_EQ(kDeviceAddress1, |
118 allowed_devices_map.GetDeviceAddress(kTestOrigin1, device_id1)); | 126 allowed_devices_map.GetDeviceAddress(kTestOrigin1, device_id1)); |
119 EXPECT_EQ(kDeviceAddress2, | 127 EXPECT_EQ(kDeviceAddress2, |
120 allowed_devices_map.GetDeviceAddress(kTestOrigin2, device_id2)); | 128 allowed_devices_map.GetDeviceAddress(kTestOrigin2, device_id2)); |
121 } | 129 } |
122 | 130 |
123 TEST(BluetoothAllowedDevicesMapTest, AddDeviceFromTwoOriginsToMap) { | 131 TEST_F(BluetoothAllowedDevicesMapTest, AddDeviceFromTwoOriginsToMap) { |
124 BluetoothAllowedDevicesMap allowed_devices_map; | 132 BluetoothAllowedDevicesMap allowed_devices_map; |
125 const std::string& device_id1 = allowed_devices_map.AddDevice( | 133 const std::string& device_id1 = allowed_devices_map.AddDevice( |
126 kTestOrigin1, kDeviceAddress1, kEmptyFilters, kEmptyOptionalServices); | 134 kTestOrigin1, kDeviceAddress1, empty_options_); |
127 const std::string& device_id2 = allowed_devices_map.AddDevice( | 135 const std::string& device_id2 = allowed_devices_map.AddDevice( |
128 kTestOrigin2, kDeviceAddress1, kEmptyFilters, kEmptyOptionalServices); | 136 kTestOrigin2, kDeviceAddress1, empty_options_); |
129 | 137 |
130 EXPECT_NE(device_id1, device_id2); | 138 EXPECT_NE(device_id1, device_id2); |
131 | 139 |
132 // Test that the wrong origin doesn't have access to the device. | 140 // Test that the wrong origin doesn't have access to the device. |
133 EXPECT_EQ(base::EmptyString(), | 141 EXPECT_EQ(base::EmptyString(), |
134 allowed_devices_map.GetDeviceAddress(kTestOrigin1, device_id2)); | 142 allowed_devices_map.GetDeviceAddress(kTestOrigin1, device_id2)); |
135 EXPECT_EQ(base::EmptyString(), | 143 EXPECT_EQ(base::EmptyString(), |
136 allowed_devices_map.GetDeviceAddress(kTestOrigin2, device_id1)); | 144 allowed_devices_map.GetDeviceAddress(kTestOrigin2, device_id1)); |
137 } | 145 } |
138 | 146 |
139 TEST(BluetoothAllowedDevicesMapTest, AddRemoveAddDeviceToMap) { | 147 TEST_F(BluetoothAllowedDevicesMapTest, AddRemoveAddDeviceToMap) { |
140 BluetoothAllowedDevicesMap allowed_devices_map; | 148 BluetoothAllowedDevicesMap allowed_devices_map; |
141 const std::string device_id_first_time = allowed_devices_map.AddDevice( | 149 const std::string device_id_first_time = allowed_devices_map.AddDevice( |
142 kTestOrigin1, kDeviceAddress1, kEmptyFilters, kEmptyOptionalServices); | 150 kTestOrigin1, kDeviceAddress1, empty_options_); |
143 | 151 |
144 allowed_devices_map.RemoveDevice(kTestOrigin1, kDeviceAddress1); | 152 allowed_devices_map.RemoveDevice(kTestOrigin1, kDeviceAddress1); |
145 | 153 |
146 const std::string device_id_second_time = allowed_devices_map.AddDevice( | 154 const std::string device_id_second_time = allowed_devices_map.AddDevice( |
147 kTestOrigin1, kDeviceAddress1, kEmptyFilters, kEmptyOptionalServices); | 155 kTestOrigin1, kDeviceAddress1, empty_options_); |
148 | 156 |
149 EXPECT_NE(device_id_first_time, device_id_second_time); | 157 EXPECT_NE(device_id_first_time, device_id_second_time); |
150 } | 158 } |
151 | 159 |
152 TEST(BluetoothAllowedDevicesMapTest, RemoveDeviceFromMap) { | 160 TEST_F(BluetoothAllowedDevicesMapTest, RemoveDeviceFromMap) { |
153 BluetoothAllowedDevicesMap allowed_devices_map; | 161 BluetoothAllowedDevicesMap allowed_devices_map; |
154 | 162 |
155 const std::string& device_id = allowed_devices_map.AddDevice( | 163 const std::string& device_id = allowed_devices_map.AddDevice( |
156 kTestOrigin1, kDeviceAddress1, kEmptyFilters, kEmptyOptionalServices); | 164 kTestOrigin1, kDeviceAddress1, empty_options_); |
157 | 165 |
158 allowed_devices_map.RemoveDevice(kTestOrigin1, kDeviceAddress1); | 166 allowed_devices_map.RemoveDevice(kTestOrigin1, kDeviceAddress1); |
159 | 167 |
160 EXPECT_EQ(base::EmptyString(), | 168 EXPECT_EQ(base::EmptyString(), |
161 allowed_devices_map.GetDeviceId(kTestOrigin1, device_id)); | 169 allowed_devices_map.GetDeviceId(kTestOrigin1, device_id)); |
162 EXPECT_EQ(base::EmptyString(), allowed_devices_map.GetDeviceAddress( | 170 EXPECT_EQ(base::EmptyString(), allowed_devices_map.GetDeviceAddress( |
163 kTestOrigin1, kDeviceAddress1)); | 171 kTestOrigin1, kDeviceAddress1)); |
164 } | 172 } |
165 | 173 |
166 TEST(BluetoothAllowedDevicesMapTest, AllowedServices_OneOriginOneDevice) { | 174 TEST_F(BluetoothAllowedDevicesMapTest, AllowedServices_OneOriginOneDevice) { |
167 BluetoothAllowedDevicesMap allowed_devices_map; | 175 BluetoothAllowedDevicesMap allowed_devices_map; |
168 | 176 |
169 // Setup device. | 177 // Setup device. |
170 BluetoothScanFilter scanFilter1; | 178 blink::mojom::WebBluetoothRequestDeviceOptionsPtr options = |
171 scanFilter1.services.push_back(kGlucoseUUID); | 179 blink::mojom::WebBluetoothRequestDeviceOptions::New(); |
172 BluetoothScanFilter scanFilter2; | 180 blink::mojom::WebBluetoothScanFilterPtr scanFilter1 = |
173 scanFilter2.services.push_back(kHeartRateUUID); | 181 blink::mojom::WebBluetoothScanFilter::New(); |
174 | 182 blink::mojom::WebBluetoothScanFilterPtr scanFilter2 = |
175 std::vector<BluetoothScanFilter> filters; | 183 blink::mojom::WebBluetoothScanFilter::New(); |
176 filters.push_back(scanFilter1); | 184 |
177 filters.push_back(scanFilter2); | 185 scanFilter1->services.push_back(kGlucoseUUID); |
178 | 186 options->filters.push_back(scanFilter1.Clone()); |
179 std::vector<BluetoothUUID> optional_services; | 187 |
180 optional_services.push_back(kBatteryServiceUUID); | 188 scanFilter2->services.push_back(kHeartRateUUID); |
181 optional_services.push_back(kHeartRateUUID); | 189 options->filters.push_back(scanFilter2.Clone()); |
| 190 |
| 191 options->optional_services.push_back(kBatteryServiceUUID); |
| 192 options->optional_services.push_back(kHeartRateUUID); |
182 | 193 |
183 // Add to map. | 194 // Add to map. |
184 const std::string device_id1 = allowed_devices_map.AddDevice( | 195 const std::string device_id1 = |
185 kTestOrigin1, kDeviceAddress1, filters, optional_services); | 196 allowed_devices_map.AddDevice(kTestOrigin1, kDeviceAddress1, options); |
186 | 197 |
187 // Access allowed services. | 198 // Access allowed services. |
188 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessService( | 199 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessService( |
189 kTestOrigin1, device_id1, kGlucoseUUIDString)); | 200 kTestOrigin1, device_id1, kGlucoseUUID)); |
190 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessService( | 201 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessService( |
191 kTestOrigin1, device_id1, kHeartRateUUIDString)); | 202 kTestOrigin1, device_id1, kHeartRateUUID)); |
192 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessService( | 203 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessService( |
193 kTestOrigin1, device_id1, kBatteryServiceUUIDString)); | 204 kTestOrigin1, device_id1, kBatteryServiceUUID)); |
194 | 205 |
195 // Try to access a non-allowed service. | 206 // Try to access a non-allowed service. |
196 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessService( | 207 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessService( |
197 kTestOrigin1, device_id1, kBloodPressureUUIDString)); | 208 kTestOrigin1, device_id1, kBloodPressureUUID)); |
198 | 209 |
199 // Try to access allowed services after removing device. | 210 // Try to access allowed services after removing device. |
200 allowed_devices_map.RemoveDevice(kTestOrigin1, kDeviceAddress1); | 211 allowed_devices_map.RemoveDevice(kTestOrigin1, kDeviceAddress1); |
201 | 212 |
202 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessService( | 213 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessService( |
203 kTestOrigin1, device_id1, kGlucoseUUIDString)); | 214 kTestOrigin1, device_id1, kGlucoseUUID)); |
204 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessService( | 215 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessService( |
205 kTestOrigin1, device_id1, kHeartRateUUIDString)); | 216 kTestOrigin1, device_id1, kHeartRateUUID)); |
206 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessService( | 217 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessService( |
207 kTestOrigin1, device_id1, kBatteryServiceUUIDString)); | 218 kTestOrigin1, device_id1, kBatteryServiceUUID)); |
208 | 219 |
209 // Add device back. | 220 // Add device back. |
210 const std::string device_id2 = allowed_devices_map.AddDevice( | 221 blink::mojom::WebBluetoothRequestDeviceOptionsPtr options2 = |
211 kTestOrigin1, kDeviceAddress1, filters, kEmptyOptionalServices); | 222 blink::mojom::WebBluetoothRequestDeviceOptions::New(); |
212 | 223 |
213 // Access allowed services. | 224 options2->filters.push_back(scanFilter1.Clone()); |
214 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessService( | 225 options2->filters.push_back(scanFilter2.Clone()); |
215 kTestOrigin1, device_id2, kGlucoseUUIDString)); | 226 |
216 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessService( | 227 const std::string device_id2 = |
217 kTestOrigin1, device_id2, kHeartRateUUIDString)); | 228 allowed_devices_map.AddDevice(kTestOrigin1, kDeviceAddress1, options2); |
| 229 |
| 230 // Access allowed services. |
| 231 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessService( |
| 232 kTestOrigin1, device_id2, kGlucoseUUID)); |
| 233 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessService( |
| 234 kTestOrigin1, device_id2, kHeartRateUUID)); |
218 | 235 |
219 // Try to access a non-allowed service. | 236 // Try to access a non-allowed service. |
220 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessService( | 237 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessService( |
221 kTestOrigin1, device_id2, kBatteryServiceUUIDString)); | 238 kTestOrigin1, device_id2, kBatteryServiceUUID)); |
222 | 239 |
223 // Try to access services from old device. | 240 // Try to access services from old device. |
224 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessService( | 241 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessService( |
225 kTestOrigin1, device_id1, kGlucoseUUIDString)); | 242 kTestOrigin1, device_id1, kGlucoseUUID)); |
226 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessService( | 243 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessService( |
227 kTestOrigin1, device_id1, kHeartRateUUIDString)); | 244 kTestOrigin1, device_id1, kHeartRateUUID)); |
228 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessService( | 245 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessService( |
229 kTestOrigin1, device_id1, kBatteryServiceUUIDString)); | 246 kTestOrigin1, device_id1, kBatteryServiceUUID)); |
230 } | 247 } |
231 | 248 |
232 TEST(BluetoothAllowedDevicesMapTest, AllowedServices_OneOriginTwoDevices) { | 249 TEST_F(BluetoothAllowedDevicesMapTest, AllowedServices_OneOriginTwoDevices) { |
233 BluetoothAllowedDevicesMap allowed_devices_map; | 250 BluetoothAllowedDevicesMap allowed_devices_map; |
234 | 251 |
235 // Setup request for device #1. | 252 // Setup request for device #1. |
236 BluetoothScanFilter scanFilter1; | 253 blink::mojom::WebBluetoothRequestDeviceOptionsPtr options1 = |
237 scanFilter1.services.push_back(kGlucoseUUID); | 254 blink::mojom::WebBluetoothRequestDeviceOptions::New(); |
238 std::vector<BluetoothScanFilter> filters1; | 255 blink::mojom::WebBluetoothScanFilterPtr scanFilter1 = |
239 filters1.push_back(scanFilter1); | 256 blink::mojom::WebBluetoothScanFilter::New(); |
240 | 257 |
241 std::vector<BluetoothUUID> optional_services1; | 258 scanFilter1->services.push_back(kGlucoseUUID); |
242 optional_services1.push_back(kHeartRateUUID); | 259 options1->filters.push_back(std::move(scanFilter1)); |
| 260 |
| 261 options1->optional_services.push_back(kHeartRateUUID); |
243 | 262 |
244 // Setup request for device #2. | 263 // Setup request for device #2. |
245 BluetoothScanFilter scanFilter2; | 264 blink::mojom::WebBluetoothRequestDeviceOptionsPtr options2 = |
246 scanFilter2.services.push_back(kBatteryServiceUUID); | 265 blink::mojom::WebBluetoothRequestDeviceOptions::New(); |
247 std::vector<BluetoothScanFilter> filters2; | 266 blink::mojom::WebBluetoothScanFilterPtr scanFilter2 = |
248 filters2.push_back(scanFilter2); | 267 blink::mojom::WebBluetoothScanFilter::New(); |
249 | 268 |
250 std::vector<BluetoothUUID> optional_services2; | 269 scanFilter2->services.push_back(kBatteryServiceUUID); |
251 optional_services2.push_back(kBloodPressureUUID); | 270 options2->filters.push_back(std::move(scanFilter2)); |
| 271 |
| 272 options2->optional_services.push_back(kBloodPressureUUID); |
252 | 273 |
253 // Add devices to map. | 274 // Add devices to map. |
254 const std::string& device_id1 = allowed_devices_map.AddDevice( | 275 const std::string& device_id1 = |
255 kTestOrigin1, kDeviceAddress1, filters1, optional_services1); | 276 allowed_devices_map.AddDevice(kTestOrigin1, kDeviceAddress1, options1); |
256 const std::string& device_id2 = allowed_devices_map.AddDevice( | 277 const std::string& device_id2 = |
257 kTestOrigin1, kDeviceAddress2, filters2, optional_services2); | 278 allowed_devices_map.AddDevice(kTestOrigin1, kDeviceAddress2, options2); |
258 | 279 |
259 // Access allowed services. | 280 // Access allowed services. |
260 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessService( | 281 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessService( |
261 kTestOrigin1, device_id1, kGlucoseUUIDString)); | 282 kTestOrigin1, device_id1, kGlucoseUUID)); |
262 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessService( | 283 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessService( |
263 kTestOrigin1, device_id1, kHeartRateUUIDString)); | 284 kTestOrigin1, device_id1, kHeartRateUUID)); |
264 | 285 |
265 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessService( | 286 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessService( |
266 kTestOrigin1, device_id2, kBatteryServiceUUIDString)); | 287 kTestOrigin1, device_id2, kBatteryServiceUUID)); |
267 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessService( | 288 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessService( |
268 kTestOrigin1, device_id2, kBloodPressureUUIDString)); | 289 kTestOrigin1, device_id2, kBloodPressureUUID)); |
269 | 290 |
270 // Try to access non-allowed services. | 291 // Try to access non-allowed services. |
271 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessService( | 292 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessService( |
272 kTestOrigin1, device_id1, kBatteryServiceUUIDString)); | 293 kTestOrigin1, device_id1, kBatteryServiceUUID)); |
273 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessService( | 294 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessService( |
274 kTestOrigin1, device_id1, kBloodPressureUUIDString)); | 295 kTestOrigin1, device_id1, kBloodPressureUUID)); |
275 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessService( | 296 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessService( |
276 kTestOrigin1, device_id1, kCyclingPowerUUIDString)); | 297 kTestOrigin1, device_id1, kCyclingPowerUUID)); |
277 | 298 |
278 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessService( | 299 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessService( |
279 kTestOrigin1, device_id2, kGlucoseUUIDString)); | 300 kTestOrigin1, device_id2, kGlucoseUUID)); |
280 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessService( | 301 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessService( |
281 kTestOrigin1, device_id2, kHeartRateUUIDString)); | 302 kTestOrigin1, device_id2, kHeartRateUUID)); |
282 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessService( | 303 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessService( |
283 kTestOrigin1, device_id2, kCyclingPowerUUIDString)); | 304 kTestOrigin1, device_id2, kCyclingPowerUUID)); |
284 } | 305 } |
285 | 306 |
286 TEST(BluetoothAllowedDevicesMapTest, AllowedServices_TwoOriginsOneDevice) { | 307 TEST_F(BluetoothAllowedDevicesMapTest, AllowedServices_TwoOriginsOneDevice) { |
287 BluetoothAllowedDevicesMap allowed_devices_map; | 308 BluetoothAllowedDevicesMap allowed_devices_map; |
288 // Setup request #1 for device. | 309 // Setup request #1 for device. |
289 BluetoothScanFilter scanFilter1; | 310 blink::mojom::WebBluetoothRequestDeviceOptionsPtr options1 = |
290 scanFilter1.services.push_back(kGlucoseUUID); | 311 blink::mojom::WebBluetoothRequestDeviceOptions::New(); |
291 std::vector<BluetoothScanFilter> filters1; | 312 blink::mojom::WebBluetoothScanFilterPtr scanFilter1 = |
292 filters1.push_back(scanFilter1); | 313 blink::mojom::WebBluetoothScanFilter::New(); |
293 | 314 |
294 std::vector<BluetoothUUID> optional_services1; | 315 scanFilter1->services.push_back(kGlucoseUUID); |
295 optional_services1.push_back(kHeartRateUUID); | 316 options1->filters.push_back(std::move(scanFilter1)); |
| 317 |
| 318 options1->optional_services.push_back(kHeartRateUUID); |
296 | 319 |
297 // Setup request #2 for device. | 320 // Setup request #2 for device. |
298 BluetoothScanFilter scanFilter2; | 321 blink::mojom::WebBluetoothRequestDeviceOptionsPtr options2 = |
299 scanFilter2.services.push_back(kBatteryServiceUUID); | 322 blink::mojom::WebBluetoothRequestDeviceOptions::New(); |
300 std::vector<BluetoothScanFilter> filters2; | 323 blink::mojom::WebBluetoothScanFilterPtr scanFilter2 = |
301 filters2.push_back(scanFilter2); | 324 blink::mojom::WebBluetoothScanFilter::New(); |
302 | 325 |
303 std::vector<BluetoothUUID> optional_services2; | 326 scanFilter2->services.push_back(kBatteryServiceUUID); |
304 optional_services2.push_back(kBloodPressureUUID); | 327 options2->filters.push_back(std::move(scanFilter2)); |
| 328 |
| 329 options2->optional_services.push_back(kBloodPressureUUID); |
305 | 330 |
306 // Add devices to map. | 331 // Add devices to map. |
307 const std::string& device_id1 = allowed_devices_map.AddDevice( | 332 const std::string& device_id1 = |
308 kTestOrigin1, kDeviceAddress1, filters1, optional_services1); | 333 allowed_devices_map.AddDevice(kTestOrigin1, kDeviceAddress1, options1); |
309 const std::string& device_id2 = allowed_devices_map.AddDevice( | 334 const std::string& device_id2 = |
310 kTestOrigin2, kDeviceAddress1, filters2, optional_services2); | 335 allowed_devices_map.AddDevice(kTestOrigin2, kDeviceAddress1, options2); |
311 | 336 |
312 // Access allowed services. | 337 // Access allowed services. |
313 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessService( | 338 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessService( |
314 kTestOrigin1, device_id1, kGlucoseUUIDString)); | 339 kTestOrigin1, device_id1, kGlucoseUUID)); |
315 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessService( | 340 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessService( |
316 kTestOrigin1, device_id1, kHeartRateUUIDString)); | 341 kTestOrigin1, device_id1, kHeartRateUUID)); |
317 | 342 |
318 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessService( | 343 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessService( |
319 kTestOrigin2, device_id2, kBatteryServiceUUIDString)); | 344 kTestOrigin2, device_id2, kBatteryServiceUUID)); |
320 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessService( | 345 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessService( |
321 kTestOrigin2, device_id2, kBloodPressureUUIDString)); | 346 kTestOrigin2, device_id2, kBloodPressureUUID)); |
322 | 347 |
323 // Try to access non-allowed services. | 348 // Try to access non-allowed services. |
324 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessService( | 349 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessService( |
325 kTestOrigin1, device_id1, kBatteryServiceUUIDString)); | 350 kTestOrigin1, device_id1, kBatteryServiceUUID)); |
326 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessService( | 351 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessService( |
327 kTestOrigin1, device_id1, kBloodPressureUUIDString)); | 352 kTestOrigin1, device_id1, kBloodPressureUUID)); |
328 | 353 |
329 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessService( | 354 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessService( |
330 kTestOrigin1, device_id2, kGlucoseUUIDString)); | 355 kTestOrigin1, device_id2, kGlucoseUUID)); |
331 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessService( | 356 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessService( |
332 kTestOrigin1, device_id2, kHeartRateUUIDString)); | 357 kTestOrigin1, device_id2, kHeartRateUUID)); |
333 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessService( | 358 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessService( |
334 kTestOrigin1, device_id2, kBatteryServiceUUIDString)); | 359 kTestOrigin1, device_id2, kBatteryServiceUUID)); |
335 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessService( | 360 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessService( |
336 kTestOrigin1, device_id2, kBloodPressureUUIDString)); | 361 kTestOrigin1, device_id2, kBloodPressureUUID)); |
337 | 362 |
338 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessService( | 363 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessService( |
339 kTestOrigin2, device_id2, kGlucoseUUIDString)); | 364 kTestOrigin2, device_id2, kGlucoseUUID)); |
340 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessService( | 365 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessService( |
341 kTestOrigin2, device_id2, kHeartRateUUIDString)); | 366 kTestOrigin2, device_id2, kHeartRateUUID)); |
342 | 367 |
343 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessService( | 368 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessService( |
344 kTestOrigin2, device_id1, kGlucoseUUIDString)); | 369 kTestOrigin2, device_id1, kGlucoseUUID)); |
345 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessService( | 370 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessService( |
346 kTestOrigin2, device_id1, kHeartRateUUIDString)); | 371 kTestOrigin2, device_id1, kHeartRateUUID)); |
347 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessService( | 372 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessService( |
348 kTestOrigin2, device_id1, kBatteryServiceUUIDString)); | 373 kTestOrigin2, device_id1, kBatteryServiceUUID)); |
349 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessService( | 374 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessService( |
350 kTestOrigin2, device_id1, kBloodPressureUUIDString)); | 375 kTestOrigin2, device_id1, kBloodPressureUUID)); |
351 } | 376 } |
352 | 377 |
353 TEST(BluetoothAllowedDevicesMapTest, MergeServices) { | 378 TEST_F(BluetoothAllowedDevicesMapTest, MergeServices) { |
354 BluetoothAllowedDevicesMap allowed_devices_map; | 379 BluetoothAllowedDevicesMap allowed_devices_map; |
355 | 380 |
356 // Setup first request. | 381 // Setup first request. |
357 BluetoothScanFilter scanFilter1; | 382 blink::mojom::WebBluetoothRequestDeviceOptionsPtr options1 = |
358 scanFilter1.services.push_back(kGlucoseUUID); | 383 blink::mojom::WebBluetoothRequestDeviceOptions::New(); |
359 std::vector<BluetoothScanFilter> filters1; | 384 blink::mojom::WebBluetoothScanFilterPtr scanFilter1 = |
360 filters1.push_back(scanFilter1); | 385 blink::mojom::WebBluetoothScanFilter::New(); |
361 std::vector<BluetoothUUID> optional_services1; | 386 |
362 optional_services1.push_back(kBatteryServiceUUID); | 387 scanFilter1->services.push_back(kGlucoseUUID); |
| 388 options1->filters.push_back(std::move(scanFilter1)); |
| 389 |
| 390 options1->optional_services.push_back(kBatteryServiceUUID); |
363 | 391 |
364 // Add to map. | 392 // Add to map. |
365 const std::string device_id1 = allowed_devices_map.AddDevice( | 393 const std::string device_id1 = |
366 kTestOrigin1, kDeviceAddress1, filters1, optional_services1); | 394 allowed_devices_map.AddDevice(kTestOrigin1, kDeviceAddress1, options1); |
367 | 395 |
368 // Setup second request. | 396 // Setup second request. |
369 BluetoothScanFilter scanFilter2; | 397 blink::mojom::WebBluetoothRequestDeviceOptionsPtr options2 = |
370 scanFilter2.services.push_back(kHeartRateUUID); | 398 blink::mojom::WebBluetoothRequestDeviceOptions::New(); |
371 std::vector<BluetoothScanFilter> filters2; | 399 blink::mojom::WebBluetoothScanFilterPtr scanFilter2 = |
372 filters2.push_back(scanFilter2); | 400 blink::mojom::WebBluetoothScanFilter::New(); |
373 std::vector<BluetoothUUID> optional_services2; | 401 |
374 optional_services2.push_back(kBloodPressureUUID); | 402 scanFilter2->services.push_back(kHeartRateUUID); |
| 403 options2->filters.push_back(std::move(scanFilter2)); |
| 404 |
| 405 options2->optional_services.push_back(kBloodPressureUUID); |
375 | 406 |
376 // Add to map again. | 407 // Add to map again. |
377 const std::string device_id2 = allowed_devices_map.AddDevice( | 408 const std::string device_id2 = |
378 kTestOrigin1, kDeviceAddress1, filters2, optional_services2); | 409 allowed_devices_map.AddDevice(kTestOrigin1, kDeviceAddress1, options2); |
379 | 410 |
380 EXPECT_EQ(device_id1, device_id2); | 411 EXPECT_EQ(device_id1, device_id2); |
381 | 412 |
382 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessService( | 413 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessService( |
383 kTestOrigin1, device_id1, kGlucoseUUIDString)); | 414 kTestOrigin1, device_id1, kGlucoseUUID)); |
384 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessService( | 415 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessService( |
385 kTestOrigin1, device_id1, kBatteryServiceUUIDString)); | 416 kTestOrigin1, device_id1, kBatteryServiceUUID)); |
386 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessService( | 417 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessService( |
387 kTestOrigin1, device_id1, kHeartRateUUIDString)); | 418 kTestOrigin1, device_id1, kHeartRateUUID)); |
388 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessService( | 419 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessService( |
389 kTestOrigin1, device_id1, kBloodPressureUUIDString)); | 420 kTestOrigin1, device_id1, kBloodPressureUUID)); |
390 } | 421 } |
391 | 422 |
392 TEST(BluetoothAllowedDevicesMapTest, CorrectIdFormat) { | 423 TEST_F(BluetoothAllowedDevicesMapTest, CorrectIdFormat) { |
393 BluetoothAllowedDevicesMap allowed_devices_map; | 424 BluetoothAllowedDevicesMap allowed_devices_map; |
394 | 425 |
395 const std::string& device_id = allowed_devices_map.AddDevice( | 426 const std::string& device_id = allowed_devices_map.AddDevice( |
396 kTestOrigin1, kDeviceAddress1, kEmptyFilters, kEmptyOptionalServices); | 427 kTestOrigin1, kDeviceAddress1, empty_options_); |
397 | 428 |
398 EXPECT_TRUE(device_id.size() == 24) | 429 EXPECT_TRUE(device_id.size() == 24) |
399 << "Expected Lenghth of a 128bit string encoded to Base64."; | 430 << "Expected Lenghth of a 128bit string encoded to Base64."; |
400 EXPECT_TRUE((device_id[22] == '=') && (device_id[23] == '=')) | 431 EXPECT_TRUE((device_id[22] == '=') && (device_id[23] == '=')) |
401 << "Expected padding characters for a 128bit string encoded to Base64."; | 432 << "Expected padding characters for a 128bit string encoded to Base64."; |
402 } | 433 } |
403 | 434 |
404 } // namespace content | 435 } // namespace content |
OLD | NEW |