Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(393)

Side by Side Diff: content/browser/bluetooth/bluetooth_allowed_devices_unittest.cc

Issue 2658473002: Refactor BluetoothAllowedDevicesMap (Closed)
Patch Set: set --similarity=20 when did git cl upload Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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.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" 8 #include "content/common/bluetooth/web_bluetooth_device_id.h"
9 #include "testing/gtest/include/gtest/gtest.h" 9 #include "testing/gtest/include/gtest/gtest.h"
10 #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"));
17 const url::Origin kTestOrigin2(GURL("https://www.example2.com"));
18 15
19 const std::string kDeviceAddress1 = "00:00:00"; 16 const std::string kDeviceAddress1 = "00:00:00";
20 const std::string kDeviceAddress2 = "11:11:11"; 17 const std::string kDeviceAddress2 = "11:11:11";
21 18
22 const std::string kDeviceName = "TestName"; 19 const std::string kDeviceName = "TestName";
23 20
24 const char kGlucoseUUIDString[] = "00001808-0000-1000-8000-00805f9b34fb"; 21 const char kGlucoseUUIDString[] = "00001808-0000-1000-8000-00805f9b34fb";
25 const char kHeartRateUUIDString[] = "0000180d-0000-1000-8000-00805f9b34fb"; 22 const char kHeartRateUUIDString[] = "0000180d-0000-1000-8000-00805f9b34fb";
26 const char kBatteryServiceUUIDString[] = "0000180f-0000-1000-8000-00805f9b34fb"; 23 const char kBatteryServiceUUIDString[] = "0000180f-0000-1000-8000-00805f9b34fb";
27 const char kBloodPressureUUIDString[] = "00001813-0000-1000-8000-00805f9b34fb"; 24 const char kBloodPressureUUIDString[] = "00001813-0000-1000-8000-00805f9b34fb";
28 const char kCyclingPowerUUIDString[] = "00001818-0000-1000-8000-00805f9b34fb"; 25 const char kCyclingPowerUUIDString[] = "00001818-0000-1000-8000-00805f9b34fb";
29 const BluetoothUUID kGlucoseUUID(kGlucoseUUIDString); 26 const BluetoothUUID kGlucoseUUID(kGlucoseUUIDString);
30 const BluetoothUUID kHeartRateUUID(kHeartRateUUIDString); 27 const BluetoothUUID kHeartRateUUID(kHeartRateUUIDString);
31 const BluetoothUUID kBatteryServiceUUID(kBatteryServiceUUIDString); 28 const BluetoothUUID kBatteryServiceUUID(kBatteryServiceUUIDString);
32 const BluetoothUUID kBloodPressureUUID(kBloodPressureUUIDString); 29 const BluetoothUUID kBloodPressureUUID(kBloodPressureUUIDString);
33 const BluetoothUUID kCyclingPowerUUID(kCyclingPowerUUIDString); 30 const BluetoothUUID kCyclingPowerUUID(kCyclingPowerUUIDString);
34 31
35 class BluetoothAllowedDevicesMapTest : public testing::Test { 32 class BluetoothAllowedDevicesTest : public testing::Test {
36 protected: 33 protected:
37 BluetoothAllowedDevicesMapTest() { 34 BluetoothAllowedDevicesTest() {
38 empty_options_ = blink::mojom::WebBluetoothRequestDeviceOptions::New(); 35 empty_options_ = blink::mojom::WebBluetoothRequestDeviceOptions::New();
39 } 36 }
40 37
41 ~BluetoothAllowedDevicesMapTest() override {} 38 ~BluetoothAllowedDevicesTest() override {}
42 39
43 blink::mojom::WebBluetoothRequestDeviceOptionsPtr empty_options_; 40 blink::mojom::WebBluetoothRequestDeviceOptionsPtr empty_options_;
44 }; 41 };
45 42
46 } // namespace 43 } // namespace
47 44
48 TEST_F(BluetoothAllowedDevicesMapTest, UniqueOriginNotSupported) { 45 TEST_F(BluetoothAllowedDevicesTest, AddDevice) {
scheib 2017/01/26 04:27:36 Testing of unique origins should be handled somewh
juncai 2017/01/30 20:34:55 Done.
49 BluetoothAllowedDevicesMap allowed_devices_map; 46 BluetoothAllowedDevices allowed_devices;
50 47
51 EXPECT_DEATH_IF_SUPPORTED(allowed_devices_map.AddDevice( 48 const WebBluetoothDeviceId& device_id =
52 url::Origin(), kDeviceAddress1, empty_options_), 49 allowed_devices.AddDevice(kDeviceAddress1, empty_options_);
53 ""); 50
51 // Test that we can retrieve the device address/id.
52 EXPECT_EQ(device_id, *allowed_devices.GetDeviceId(kDeviceAddress1));
53 EXPECT_EQ(kDeviceAddress1, allowed_devices.GetDeviceAddress(device_id));
54 } 54 }
55 55
56 TEST_F(BluetoothAllowedDevicesMapTest, AddDeviceToMap) { 56 TEST_F(BluetoothAllowedDevicesTest, AddDeviceTwice) {
57 BluetoothAllowedDevicesMap allowed_devices_map; 57 BluetoothAllowedDevices allowed_devices;
58 58 const WebBluetoothDeviceId& device_id1 =
59 const WebBluetoothDeviceId& device_id = allowed_devices_map.AddDevice( 59 allowed_devices.AddDevice(kDeviceAddress1, empty_options_);
60 kTestOrigin1, kDeviceAddress1, empty_options_); 60 const WebBluetoothDeviceId& device_id2 =
61 61 allowed_devices.AddDevice(kDeviceAddress1, empty_options_);
62 // Test that we can retrieve the device address/id.
63 EXPECT_EQ(device_id,
64 *allowed_devices_map.GetDeviceId(kTestOrigin1, kDeviceAddress1));
65 EXPECT_EQ(kDeviceAddress1,
66 allowed_devices_map.GetDeviceAddress(kTestOrigin1, device_id));
67 }
68
69 TEST_F(BluetoothAllowedDevicesMapTest, AddDeviceToMapTwice) {
70 BluetoothAllowedDevicesMap allowed_devices_map;
71 const WebBluetoothDeviceId& device_id1 = allowed_devices_map.AddDevice(
72 kTestOrigin1, kDeviceAddress1, empty_options_);
73 const WebBluetoothDeviceId& device_id2 = allowed_devices_map.AddDevice(
74 kTestOrigin1, kDeviceAddress1, empty_options_);
75 62
76 EXPECT_EQ(device_id1, device_id2); 63 EXPECT_EQ(device_id1, device_id2);
77 64
78 // Test that we can retrieve the device address/id. 65 // Test that we can retrieve the device address/id.
79 EXPECT_EQ(device_id1, 66 EXPECT_EQ(device_id1, *allowed_devices.GetDeviceId(kDeviceAddress1));
80 *allowed_devices_map.GetDeviceId(kTestOrigin1, kDeviceAddress1)); 67 EXPECT_EQ(kDeviceAddress1, allowed_devices.GetDeviceAddress(device_id1));
81 EXPECT_EQ(kDeviceAddress1,
82 allowed_devices_map.GetDeviceAddress(kTestOrigin1, device_id1));
83 } 68 }
84 69
85 TEST_F(BluetoothAllowedDevicesMapTest, AddTwoDevicesFromSameOriginToMap) { 70 TEST_F(BluetoothAllowedDevicesTest, AddTwoDevices) {
86 BluetoothAllowedDevicesMap allowed_devices_map; 71 BluetoothAllowedDevices allowed_devices;
87 const WebBluetoothDeviceId& device_id1 = allowed_devices_map.AddDevice( 72 const WebBluetoothDeviceId& device_id1 =
88 kTestOrigin1, kDeviceAddress1, empty_options_); 73 allowed_devices.AddDevice(kDeviceAddress1, empty_options_);
89 const WebBluetoothDeviceId& device_id2 = allowed_devices_map.AddDevice( 74 const WebBluetoothDeviceId& device_id2 =
90 kTestOrigin1, kDeviceAddress2, empty_options_); 75 allowed_devices.AddDevice(kDeviceAddress2, empty_options_);
91 76
92 EXPECT_NE(device_id1, device_id2); 77 EXPECT_NE(device_id1, device_id2);
93 78
94 // Test that we can retrieve the device address/id. 79 // Test that we can retrieve the device address/id.
95 EXPECT_EQ(device_id1, 80 EXPECT_EQ(device_id1, *allowed_devices.GetDeviceId(kDeviceAddress1));
96 *allowed_devices_map.GetDeviceId(kTestOrigin1, kDeviceAddress1)); 81 EXPECT_EQ(device_id2, *allowed_devices.GetDeviceId(kDeviceAddress2));
97 EXPECT_EQ(device_id2,
98 *allowed_devices_map.GetDeviceId(kTestOrigin1, kDeviceAddress2));
99 82
100 EXPECT_EQ(kDeviceAddress1, 83 EXPECT_EQ(kDeviceAddress1, allowed_devices.GetDeviceAddress(device_id1));
101 allowed_devices_map.GetDeviceAddress(kTestOrigin1, device_id1)); 84 EXPECT_EQ(kDeviceAddress2, allowed_devices.GetDeviceAddress(device_id2));
102 EXPECT_EQ(kDeviceAddress2,
103 allowed_devices_map.GetDeviceAddress(kTestOrigin1, device_id2));
104 } 85 }
105 86
106 TEST_F(BluetoothAllowedDevicesMapTest, AddTwoDevicesFromTwoOriginsToMap) { 87 TEST_F(BluetoothAllowedDevicesTest, AddRemoveAddDevice) {
scheib 2017/01/26 04:27:36 Let's find a new home for this test as well, verif
juncai 2017/01/30 20:34:55 Done.
107 BluetoothAllowedDevicesMap allowed_devices_map; 88 BluetoothAllowedDevices allowed_devices;
108 const WebBluetoothDeviceId& device_id1 = allowed_devices_map.AddDevice( 89 const WebBluetoothDeviceId device_id_first_time =
109 kTestOrigin1, kDeviceAddress1, empty_options_); 90 allowed_devices.AddDevice(kDeviceAddress1, empty_options_);
110 const WebBluetoothDeviceId& device_id2 = allowed_devices_map.AddDevice(
111 kTestOrigin2, kDeviceAddress2, empty_options_);
112 91
113 EXPECT_NE(device_id1, device_id2); 92 allowed_devices.RemoveDevice(kDeviceAddress1);
114
115 // Test that the wrong origin doesn't have access to the device.
116
117 EXPECT_EQ(nullptr,
118 allowed_devices_map.GetDeviceId(kTestOrigin1, kDeviceAddress2));
119 EXPECT_EQ(nullptr,
120 allowed_devices_map.GetDeviceId(kTestOrigin2, kDeviceAddress1));
121
122 EXPECT_EQ(base::EmptyString(),
123 allowed_devices_map.GetDeviceAddress(kTestOrigin1, device_id2));
124 EXPECT_EQ(base::EmptyString(),
125 allowed_devices_map.GetDeviceAddress(kTestOrigin2, device_id1));
126
127 // Test that we can retrieve the device address/id.
128 EXPECT_EQ(device_id1,
129 *allowed_devices_map.GetDeviceId(kTestOrigin1, kDeviceAddress1));
130 EXPECT_EQ(device_id2,
131 *allowed_devices_map.GetDeviceId(kTestOrigin2, kDeviceAddress2));
132
133 EXPECT_EQ(kDeviceAddress1,
134 allowed_devices_map.GetDeviceAddress(kTestOrigin1, device_id1));
135 EXPECT_EQ(kDeviceAddress2,
136 allowed_devices_map.GetDeviceAddress(kTestOrigin2, device_id2));
137 }
138
139 TEST_F(BluetoothAllowedDevicesMapTest, AddDeviceFromTwoOriginsToMap) {
140 BluetoothAllowedDevicesMap allowed_devices_map;
141 const WebBluetoothDeviceId& device_id1 = allowed_devices_map.AddDevice(
142 kTestOrigin1, kDeviceAddress1, empty_options_);
143 const WebBluetoothDeviceId& device_id2 = allowed_devices_map.AddDevice(
144 kTestOrigin2, kDeviceAddress1, empty_options_);
145
146 EXPECT_NE(device_id1, device_id2);
147
148 // Test that the wrong origin doesn't have access to the device.
149 EXPECT_EQ(base::EmptyString(),
150 allowed_devices_map.GetDeviceAddress(kTestOrigin1, device_id2));
151 EXPECT_EQ(base::EmptyString(),
152 allowed_devices_map.GetDeviceAddress(kTestOrigin2, device_id1));
153 }
154
155 TEST_F(BluetoothAllowedDevicesMapTest, AddRemoveAddDeviceToMap) {
156 BluetoothAllowedDevicesMap allowed_devices_map;
157 const WebBluetoothDeviceId device_id_first_time =
158 allowed_devices_map.AddDevice(kTestOrigin1, kDeviceAddress1,
159 empty_options_);
160
161 allowed_devices_map.RemoveDevice(kTestOrigin1, kDeviceAddress1);
162 93
163 const WebBluetoothDeviceId device_id_second_time = 94 const WebBluetoothDeviceId device_id_second_time =
164 allowed_devices_map.AddDevice(kTestOrigin1, kDeviceAddress1, 95 allowed_devices.AddDevice(kDeviceAddress1, empty_options_);
165 empty_options_);
166 96
167 EXPECT_NE(device_id_first_time, device_id_second_time); 97 EXPECT_NE(device_id_first_time, device_id_second_time);
168 } 98 }
169 99
170 TEST_F(BluetoothAllowedDevicesMapTest, RemoveDeviceFromMap) { 100 TEST_F(BluetoothAllowedDevicesTest, RemoveDevice) {
171 BluetoothAllowedDevicesMap allowed_devices_map; 101 BluetoothAllowedDevices allowed_devices;
172 102
173 const WebBluetoothDeviceId device_id = allowed_devices_map.AddDevice( 103 const WebBluetoothDeviceId device_id =
174 kTestOrigin1, kDeviceAddress1, empty_options_); 104 allowed_devices.AddDevice(kDeviceAddress1, empty_options_);
175 105
176 allowed_devices_map.RemoveDevice(kTestOrigin1, kDeviceAddress1); 106 allowed_devices.RemoveDevice(kDeviceAddress1);
177 107
178 EXPECT_EQ(nullptr, 108 EXPECT_EQ(nullptr, allowed_devices.GetDeviceId(kDeviceAddress1));
179 allowed_devices_map.GetDeviceId(kTestOrigin1, kDeviceAddress1)); 109 EXPECT_EQ(base::EmptyString(), allowed_devices.GetDeviceAddress(device_id));
180 EXPECT_EQ(base::EmptyString(),
181 allowed_devices_map.GetDeviceAddress(kTestOrigin1, device_id));
182 } 110 }
183 111
184 TEST_F(BluetoothAllowedDevicesMapTest, NoPermissionForAnyService) { 112 TEST_F(BluetoothAllowedDevicesTest, NoPermissionForAnyService) {
185 BluetoothAllowedDevicesMap allowed_devices_map; 113 BluetoothAllowedDevices allowed_devices;
186 114
187 // Setup device. 115 // Setup device.
188 blink::mojom::WebBluetoothRequestDeviceOptionsPtr options = 116 blink::mojom::WebBluetoothRequestDeviceOptionsPtr options =
189 blink::mojom::WebBluetoothRequestDeviceOptions::New(); 117 blink::mojom::WebBluetoothRequestDeviceOptions::New();
190 blink::mojom::WebBluetoothScanFilterPtr scan_filter = 118 blink::mojom::WebBluetoothScanFilterPtr scan_filter =
191 blink::mojom::WebBluetoothScanFilter::New(); 119 blink::mojom::WebBluetoothScanFilter::New();
192 120
193 scan_filter->name = kDeviceName; 121 scan_filter->name = kDeviceName;
194 options->filters.emplace(); 122 options->filters.emplace();
195 options->filters->push_back({scan_filter.Clone()}); 123 options->filters->push_back({scan_filter.Clone()});
196 124
197 // Add to map. 125 // Add to map.
198 const WebBluetoothDeviceId device_id = 126 const WebBluetoothDeviceId device_id =
199 allowed_devices_map.AddDevice(kTestOrigin1, kDeviceAddress1, options); 127 allowed_devices.AddDevice(kDeviceAddress1, options);
200 128
201 // Try to access at least one service. 129 // Try to access at least one service.
202 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessAtLeastOneService( 130 EXPECT_FALSE(allowed_devices.IsAllowedToAccessAtLeastOneService(device_id));
203 kTestOrigin1, device_id)); 131 EXPECT_FALSE(
204 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessService( 132 allowed_devices.IsAllowedToAccessService(device_id, kGlucoseUUID));
205 kTestOrigin1, device_id, kGlucoseUUID));
206 } 133 }
207 134
208 TEST_F(BluetoothAllowedDevicesMapTest, AllowedServices_OneOriginOneDevice) { 135 TEST_F(BluetoothAllowedDevicesTest, AllowedServices_OneDevice) {
209 BluetoothAllowedDevicesMap allowed_devices_map; 136 BluetoothAllowedDevices allowed_devices;
210 137
211 // Setup device. 138 // Setup device.
212 blink::mojom::WebBluetoothRequestDeviceOptionsPtr options = 139 blink::mojom::WebBluetoothRequestDeviceOptionsPtr options =
213 blink::mojom::WebBluetoothRequestDeviceOptions::New(); 140 blink::mojom::WebBluetoothRequestDeviceOptions::New();
214 blink::mojom::WebBluetoothScanFilterPtr scan_filter1 = 141 blink::mojom::WebBluetoothScanFilterPtr scan_filter1 =
215 blink::mojom::WebBluetoothScanFilter::New(); 142 blink::mojom::WebBluetoothScanFilter::New();
216 blink::mojom::WebBluetoothScanFilterPtr scan_filter2 = 143 blink::mojom::WebBluetoothScanFilterPtr scan_filter2 =
217 blink::mojom::WebBluetoothScanFilter::New(); 144 blink::mojom::WebBluetoothScanFilter::New();
218 145
219 scan_filter1->services.emplace(); 146 scan_filter1->services.emplace();
220 scan_filter1->services->push_back(kGlucoseUUID); 147 scan_filter1->services->push_back(kGlucoseUUID);
221 options->filters.emplace(); 148 options->filters.emplace();
222 options->filters->push_back(scan_filter1.Clone()); 149 options->filters->push_back(scan_filter1.Clone());
223 150
224 scan_filter2->services.emplace(); 151 scan_filter2->services.emplace();
225 scan_filter2->services->push_back(kHeartRateUUID); 152 scan_filter2->services->push_back(kHeartRateUUID);
226 options->filters->push_back(scan_filter2.Clone()); 153 options->filters->push_back(scan_filter2.Clone());
227 154
228 options->optional_services.push_back(kBatteryServiceUUID); 155 options->optional_services.push_back(kBatteryServiceUUID);
229 options->optional_services.push_back(kHeartRateUUID); 156 options->optional_services.push_back(kHeartRateUUID);
230 157
231 // Add to map. 158 // Add to map.
232 const WebBluetoothDeviceId device_id1 = 159 const WebBluetoothDeviceId device_id1 =
233 allowed_devices_map.AddDevice(kTestOrigin1, kDeviceAddress1, options); 160 allowed_devices.AddDevice(kDeviceAddress1, options);
234 161
235 // Access allowed services. 162 // Access allowed services.
236 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessAtLeastOneService( 163 EXPECT_TRUE(allowed_devices.IsAllowedToAccessAtLeastOneService(device_id1));
237 kTestOrigin1, device_id1)); 164 EXPECT_TRUE(
238 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessService( 165 allowed_devices.IsAllowedToAccessService(device_id1, kGlucoseUUID));
239 kTestOrigin1, device_id1, kGlucoseUUID)); 166 EXPECT_TRUE(
240 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessService( 167 allowed_devices.IsAllowedToAccessService(device_id1, kHeartRateUUID));
241 kTestOrigin1, device_id1, kHeartRateUUID)); 168 EXPECT_TRUE(allowed_devices.IsAllowedToAccessService(device_id1,
242 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessService( 169 kBatteryServiceUUID));
243 kTestOrigin1, device_id1, kBatteryServiceUUID));
244 170
245 // Try to access a non-allowed service. 171 // Try to access a non-allowed service.
246 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessService( 172 EXPECT_FALSE(
247 kTestOrigin1, device_id1, kBloodPressureUUID)); 173 allowed_devices.IsAllowedToAccessService(device_id1, kBloodPressureUUID));
248 174
249 // Try to access allowed services after removing device. 175 // Try to access allowed services after removing device.
250 allowed_devices_map.RemoveDevice(kTestOrigin1, kDeviceAddress1); 176 allowed_devices.RemoveDevice(kDeviceAddress1);
251 177
252 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessAtLeastOneService( 178 EXPECT_FALSE(allowed_devices.IsAllowedToAccessAtLeastOneService(device_id1));
253 kTestOrigin1, device_id1)); 179 EXPECT_FALSE(
254 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessService( 180 allowed_devices.IsAllowedToAccessService(device_id1, kGlucoseUUID));
255 kTestOrigin1, device_id1, kGlucoseUUID)); 181 EXPECT_FALSE(
256 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessService( 182 allowed_devices.IsAllowedToAccessService(device_id1, kHeartRateUUID));
257 kTestOrigin1, device_id1, kHeartRateUUID)); 183 EXPECT_FALSE(allowed_devices.IsAllowedToAccessService(device_id1,
258 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessService( 184 kBatteryServiceUUID));
259 kTestOrigin1, device_id1, kBatteryServiceUUID));
260 185
261 // Add device back. 186 // Add device back.
262 blink::mojom::WebBluetoothRequestDeviceOptionsPtr options2 = 187 blink::mojom::WebBluetoothRequestDeviceOptionsPtr options2 =
263 blink::mojom::WebBluetoothRequestDeviceOptions::New(); 188 blink::mojom::WebBluetoothRequestDeviceOptions::New();
264 189
265 options2->filters.emplace(); 190 options2->filters.emplace();
266 options2->filters->push_back(scan_filter1.Clone()); 191 options2->filters->push_back(scan_filter1.Clone());
267 options2->filters->push_back(scan_filter2.Clone()); 192 options2->filters->push_back(scan_filter2.Clone());
268 193
269 const WebBluetoothDeviceId device_id2 = 194 const WebBluetoothDeviceId device_id2 =
270 allowed_devices_map.AddDevice(kTestOrigin1, kDeviceAddress1, options2); 195 allowed_devices.AddDevice(kDeviceAddress1, options2);
271 196
272 // Access allowed services. 197 // Access allowed services.
273 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessAtLeastOneService( 198 EXPECT_TRUE(allowed_devices.IsAllowedToAccessAtLeastOneService(device_id2));
274 kTestOrigin1, device_id2)); 199 EXPECT_TRUE(
275 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessService( 200 allowed_devices.IsAllowedToAccessService(device_id2, kGlucoseUUID));
276 kTestOrigin1, device_id2, kGlucoseUUID)); 201 EXPECT_TRUE(
277 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessService( 202 allowed_devices.IsAllowedToAccessService(device_id2, kHeartRateUUID));
278 kTestOrigin1, device_id2, kHeartRateUUID));
279 203
280 // Try to access a non-allowed service. 204 // Try to access a non-allowed service.
281 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessService( 205 EXPECT_FALSE(allowed_devices.IsAllowedToAccessService(device_id2,
282 kTestOrigin1, device_id2, kBatteryServiceUUID)); 206 kBatteryServiceUUID));
283 207
284 // Try to access services from old device. 208 // Try to access services from old device.
285 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessService( 209 EXPECT_FALSE(
286 kTestOrigin1, device_id1, kGlucoseUUID)); 210 allowed_devices.IsAllowedToAccessService(device_id1, kGlucoseUUID));
287 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessService( 211 EXPECT_FALSE(
288 kTestOrigin1, device_id1, kHeartRateUUID)); 212 allowed_devices.IsAllowedToAccessService(device_id1, kHeartRateUUID));
289 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessService( 213 EXPECT_FALSE(allowed_devices.IsAllowedToAccessService(device_id1,
290 kTestOrigin1, device_id1, kBatteryServiceUUID)); 214 kBatteryServiceUUID));
291 } 215 }
292 216
293 TEST_F(BluetoothAllowedDevicesMapTest, AllowedServices_OneOriginTwoDevices) { 217 TEST_F(BluetoothAllowedDevicesTest, AllowedServices_TwoDevices) {
294 BluetoothAllowedDevicesMap allowed_devices_map; 218 BluetoothAllowedDevices allowed_devices;
295 219
296 // Setup request for device #1. 220 // Setup request for device #1.
297 blink::mojom::WebBluetoothRequestDeviceOptionsPtr options1 = 221 blink::mojom::WebBluetoothRequestDeviceOptionsPtr options1 =
298 blink::mojom::WebBluetoothRequestDeviceOptions::New(); 222 blink::mojom::WebBluetoothRequestDeviceOptions::New();
299 blink::mojom::WebBluetoothScanFilterPtr scan_filter1 = 223 blink::mojom::WebBluetoothScanFilterPtr scan_filter1 =
300 blink::mojom::WebBluetoothScanFilter::New(); 224 blink::mojom::WebBluetoothScanFilter::New();
301 225
302 scan_filter1->services.emplace(); 226 scan_filter1->services.emplace();
303 scan_filter1->services->push_back(kGlucoseUUID); 227 scan_filter1->services->push_back(kGlucoseUUID);
304 options1->filters.emplace(); 228 options1->filters.emplace();
305 options1->filters->push_back(std::move(scan_filter1)); 229 options1->filters->push_back(std::move(scan_filter1));
306 230
307 options1->optional_services.push_back(kHeartRateUUID); 231 options1->optional_services.push_back(kHeartRateUUID);
308 232
309 // Setup request for device #2. 233 // Setup request for device #2.
310 blink::mojom::WebBluetoothRequestDeviceOptionsPtr options2 = 234 blink::mojom::WebBluetoothRequestDeviceOptionsPtr options2 =
311 blink::mojom::WebBluetoothRequestDeviceOptions::New(); 235 blink::mojom::WebBluetoothRequestDeviceOptions::New();
312 blink::mojom::WebBluetoothScanFilterPtr scan_filter2 = 236 blink::mojom::WebBluetoothScanFilterPtr scan_filter2 =
313 blink::mojom::WebBluetoothScanFilter::New(); 237 blink::mojom::WebBluetoothScanFilter::New();
314 238
315 scan_filter2->services.emplace(); 239 scan_filter2->services.emplace();
316 scan_filter2->services->push_back(kBatteryServiceUUID); 240 scan_filter2->services->push_back(kBatteryServiceUUID);
317 options2->filters.emplace(); 241 options2->filters.emplace();
318 options2->filters->push_back(std::move(scan_filter2)); 242 options2->filters->push_back(std::move(scan_filter2));
319 243
320 options2->optional_services.push_back(kBloodPressureUUID); 244 options2->optional_services.push_back(kBloodPressureUUID);
321 245
322 // Add devices to map. 246 // Add devices to map.
323 const WebBluetoothDeviceId& device_id1 = 247 const WebBluetoothDeviceId& device_id1 =
324 allowed_devices_map.AddDevice(kTestOrigin1, kDeviceAddress1, options1); 248 allowed_devices.AddDevice(kDeviceAddress1, options1);
325 const WebBluetoothDeviceId& device_id2 = 249 const WebBluetoothDeviceId& device_id2 =
326 allowed_devices_map.AddDevice(kTestOrigin1, kDeviceAddress2, options2); 250 allowed_devices.AddDevice(kDeviceAddress2, options2);
327 251
328 // Access allowed services. 252 // Access allowed services.
329 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessAtLeastOneService( 253 EXPECT_TRUE(allowed_devices.IsAllowedToAccessAtLeastOneService(device_id1));
330 kTestOrigin1, device_id1)); 254 EXPECT_TRUE(
331 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessService( 255 allowed_devices.IsAllowedToAccessService(device_id1, kGlucoseUUID));
332 kTestOrigin1, device_id1, kGlucoseUUID)); 256 EXPECT_TRUE(
333 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessService( 257 allowed_devices.IsAllowedToAccessService(device_id1, kHeartRateUUID));
334 kTestOrigin1, device_id1, kHeartRateUUID));
335 258
336 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessAtLeastOneService( 259 EXPECT_TRUE(allowed_devices.IsAllowedToAccessAtLeastOneService(device_id2));
337 kTestOrigin1, device_id2)); 260 EXPECT_TRUE(allowed_devices.IsAllowedToAccessService(device_id2,
338 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessService( 261 kBatteryServiceUUID));
339 kTestOrigin1, device_id2, kBatteryServiceUUID)); 262 EXPECT_TRUE(
340 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessService( 263 allowed_devices.IsAllowedToAccessService(device_id2, kBloodPressureUUID));
341 kTestOrigin1, device_id2, kBloodPressureUUID));
342 264
343 // Try to access non-allowed services. 265 // Try to access non-allowed services.
344 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessService( 266 EXPECT_FALSE(allowed_devices.IsAllowedToAccessService(device_id1,
345 kTestOrigin1, device_id1, kBatteryServiceUUID)); 267 kBatteryServiceUUID));
346 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessService( 268 EXPECT_FALSE(
347 kTestOrigin1, device_id1, kBloodPressureUUID)); 269 allowed_devices.IsAllowedToAccessService(device_id1, kBloodPressureUUID));
348 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessService( 270 EXPECT_FALSE(
349 kTestOrigin1, device_id1, kCyclingPowerUUID)); 271 allowed_devices.IsAllowedToAccessService(device_id1, kCyclingPowerUUID));
350 272
351 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessService( 273 EXPECT_FALSE(
352 kTestOrigin1, device_id2, kGlucoseUUID)); 274 allowed_devices.IsAllowedToAccessService(device_id2, kGlucoseUUID));
353 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessService( 275 EXPECT_FALSE(
354 kTestOrigin1, device_id2, kHeartRateUUID)); 276 allowed_devices.IsAllowedToAccessService(device_id2, kHeartRateUUID));
355 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessService( 277 EXPECT_FALSE(
356 kTestOrigin1, device_id2, kCyclingPowerUUID)); 278 allowed_devices.IsAllowedToAccessService(device_id2, kCyclingPowerUUID));
357 } 279 }
358 280
359 TEST_F(BluetoothAllowedDevicesMapTest, AllowedServices_TwoOriginsOneDevice) { 281 TEST_F(BluetoothAllowedDevicesTest, MergeServices) {
scheib 2017/01/26 04:27:36 ditto
juncai 2017/01/30 20:34:55 Done.
360 BluetoothAllowedDevicesMap allowed_devices_map; 282 BluetoothAllowedDevices allowed_devices;
361 // Setup request #1 for device.
362 blink::mojom::WebBluetoothRequestDeviceOptionsPtr options1 =
363 blink::mojom::WebBluetoothRequestDeviceOptions::New();
364 blink::mojom::WebBluetoothScanFilterPtr scan_filter1 =
365 blink::mojom::WebBluetoothScanFilter::New();
366
367 scan_filter1->services.emplace();
368 scan_filter1->services->push_back(kGlucoseUUID);
369 options1->filters.emplace();
370 options1->filters->push_back(std::move(scan_filter1));
371
372 options1->optional_services.push_back(kHeartRateUUID);
373
374 // Setup request #2 for device.
375 blink::mojom::WebBluetoothRequestDeviceOptionsPtr options2 =
376 blink::mojom::WebBluetoothRequestDeviceOptions::New();
377 blink::mojom::WebBluetoothScanFilterPtr scan_filter2 =
378 blink::mojom::WebBluetoothScanFilter::New();
379
380 scan_filter2->services.emplace();
381 scan_filter2->services->push_back(kBatteryServiceUUID);
382 options2->filters.emplace();
383 options2->filters->push_back(std::move(scan_filter2));
384
385 options2->optional_services.push_back(kBloodPressureUUID);
386
387 // Add devices to map.
388 const WebBluetoothDeviceId& device_id1 =
389 allowed_devices_map.AddDevice(kTestOrigin1, kDeviceAddress1, options1);
390 const WebBluetoothDeviceId& device_id2 =
391 allowed_devices_map.AddDevice(kTestOrigin2, kDeviceAddress1, options2);
392
393 // Access allowed services.
394 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessAtLeastOneService(
395 kTestOrigin1, device_id1));
396 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessService(
397 kTestOrigin1, device_id1, kGlucoseUUID));
398 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessService(
399 kTestOrigin1, device_id1, kHeartRateUUID));
400
401 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessAtLeastOneService(
402 kTestOrigin2, device_id2));
403 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessService(
404 kTestOrigin2, device_id2, kBatteryServiceUUID));
405 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessService(
406 kTestOrigin2, device_id2, kBloodPressureUUID));
407
408 // Try to access non-allowed services.
409 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessService(
410 kTestOrigin1, device_id1, kBatteryServiceUUID));
411 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessService(
412 kTestOrigin1, device_id1, kBloodPressureUUID));
413
414 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessAtLeastOneService(
415 kTestOrigin1, device_id2));
416 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessService(
417 kTestOrigin1, device_id2, kGlucoseUUID));
418 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessService(
419 kTestOrigin1, device_id2, kHeartRateUUID));
420 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessService(
421 kTestOrigin1, device_id2, kBatteryServiceUUID));
422 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessService(
423 kTestOrigin1, device_id2, kBloodPressureUUID));
424
425 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessService(
426 kTestOrigin2, device_id2, kGlucoseUUID));
427 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessService(
428 kTestOrigin2, device_id2, kHeartRateUUID));
429
430 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessAtLeastOneService(
431 kTestOrigin2, device_id1));
432 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessService(
433 kTestOrigin2, device_id1, kGlucoseUUID));
434 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessService(
435 kTestOrigin2, device_id1, kHeartRateUUID));
436 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessService(
437 kTestOrigin2, device_id1, kBatteryServiceUUID));
438 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessService(
439 kTestOrigin2, device_id1, kBloodPressureUUID));
440 }
441
442 TEST_F(BluetoothAllowedDevicesMapTest, MergeServices) {
443 BluetoothAllowedDevicesMap allowed_devices_map;
444 283
445 // Setup first request. 284 // Setup first request.
446 blink::mojom::WebBluetoothRequestDeviceOptionsPtr options1 = 285 blink::mojom::WebBluetoothRequestDeviceOptionsPtr options1 =
447 blink::mojom::WebBluetoothRequestDeviceOptions::New(); 286 blink::mojom::WebBluetoothRequestDeviceOptions::New();
448 blink::mojom::WebBluetoothScanFilterPtr scan_filter1 = 287 blink::mojom::WebBluetoothScanFilterPtr scan_filter1 =
449 blink::mojom::WebBluetoothScanFilter::New(); 288 blink::mojom::WebBluetoothScanFilter::New();
450 289
451 scan_filter1->services.emplace(); 290 scan_filter1->services.emplace();
452 scan_filter1->services->push_back(kGlucoseUUID); 291 scan_filter1->services->push_back(kGlucoseUUID);
453 options1->filters.emplace(); 292 options1->filters.emplace();
454 options1->filters->push_back(std::move(scan_filter1)); 293 options1->filters->push_back(std::move(scan_filter1));
455 294
456 options1->optional_services.push_back(kBatteryServiceUUID); 295 options1->optional_services.push_back(kBatteryServiceUUID);
457 296
458 // Add to map. 297 // Add to map.
459 const WebBluetoothDeviceId device_id1 = 298 const WebBluetoothDeviceId device_id1 =
460 allowed_devices_map.AddDevice(kTestOrigin1, kDeviceAddress1, options1); 299 allowed_devices.AddDevice(kDeviceAddress1, options1);
461 300
462 // Setup second request. 301 // Setup second request.
463 blink::mojom::WebBluetoothRequestDeviceOptionsPtr options2 = 302 blink::mojom::WebBluetoothRequestDeviceOptionsPtr options2 =
464 blink::mojom::WebBluetoothRequestDeviceOptions::New(); 303 blink::mojom::WebBluetoothRequestDeviceOptions::New();
465 blink::mojom::WebBluetoothScanFilterPtr scan_filter2 = 304 blink::mojom::WebBluetoothScanFilterPtr scan_filter2 =
466 blink::mojom::WebBluetoothScanFilter::New(); 305 blink::mojom::WebBluetoothScanFilter::New();
467 306
468 scan_filter2->services.emplace(); 307 scan_filter2->services.emplace();
469 scan_filter2->services->push_back(kHeartRateUUID); 308 scan_filter2->services->push_back(kHeartRateUUID);
470 options2->filters.emplace(); 309 options2->filters.emplace();
471 options2->filters->push_back(std::move(scan_filter2)); 310 options2->filters->push_back(std::move(scan_filter2));
472 311
473 options2->optional_services.push_back(kBloodPressureUUID); 312 options2->optional_services.push_back(kBloodPressureUUID);
474 313
475 // Add to map again. 314 // Add to map again.
476 const WebBluetoothDeviceId device_id2 = 315 const WebBluetoothDeviceId device_id2 =
477 allowed_devices_map.AddDevice(kTestOrigin1, kDeviceAddress1, options2); 316 allowed_devices.AddDevice(kDeviceAddress1, options2);
478 317
479 EXPECT_EQ(device_id1, device_id2); 318 EXPECT_EQ(device_id1, device_id2);
480 319
481 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessAtLeastOneService( 320 EXPECT_TRUE(allowed_devices.IsAllowedToAccessAtLeastOneService(device_id1));
482 kTestOrigin1, device_id1)); 321 EXPECT_TRUE(
483 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessService( 322 allowed_devices.IsAllowedToAccessService(device_id1, kGlucoseUUID));
484 kTestOrigin1, device_id1, kGlucoseUUID)); 323 EXPECT_TRUE(allowed_devices.IsAllowedToAccessService(device_id1,
485 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessService( 324 kBatteryServiceUUID));
486 kTestOrigin1, device_id1, kBatteryServiceUUID)); 325 EXPECT_TRUE(
487 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessService( 326 allowed_devices.IsAllowedToAccessService(device_id1, kHeartRateUUID));
488 kTestOrigin1, device_id1, kHeartRateUUID)); 327 EXPECT_TRUE(
489 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessService( 328 allowed_devices.IsAllowedToAccessService(device_id1, kBloodPressureUUID));
490 kTestOrigin1, device_id1, kBloodPressureUUID));
491 } 329 }
492 330
493 TEST_F(BluetoothAllowedDevicesMapTest, CorrectIdFormat) { 331 TEST_F(BluetoothAllowedDevicesTest, CorrectIdFormat) {
494 BluetoothAllowedDevicesMap allowed_devices_map; 332 BluetoothAllowedDevices allowed_devices;
495 333
496 const WebBluetoothDeviceId& device_id = allowed_devices_map.AddDevice( 334 const WebBluetoothDeviceId& device_id =
497 kTestOrigin1, kDeviceAddress1, empty_options_); 335 allowed_devices.AddDevice(kDeviceAddress1, empty_options_);
498 336
499 EXPECT_TRUE(WebBluetoothDeviceId::IsValid(device_id.str())); 337 EXPECT_TRUE(WebBluetoothDeviceId::IsValid(device_id.str()));
500 } 338 }
501 339
502 TEST_F(BluetoothAllowedDevicesMapTest, NoFilterServices) { 340 TEST_F(BluetoothAllowedDevicesTest, NoFilterServices) {
503 BluetoothAllowedDevicesMap allowed_devices_map; 341 BluetoothAllowedDevices allowed_devices;
504 342
505 // Setup request. 343 // Setup request.
506 blink::mojom::WebBluetoothRequestDeviceOptionsPtr options = 344 blink::mojom::WebBluetoothRequestDeviceOptionsPtr options =
507 blink::mojom::WebBluetoothRequestDeviceOptions::New(); 345 blink::mojom::WebBluetoothRequestDeviceOptions::New();
508 blink::mojom::WebBluetoothScanFilterPtr scan_filter = 346 blink::mojom::WebBluetoothScanFilterPtr scan_filter =
509 blink::mojom::WebBluetoothScanFilter::New(); 347 blink::mojom::WebBluetoothScanFilter::New();
510 348
511 options->filters.emplace(); 349 options->filters.emplace();
512 options->filters->push_back(std::move(scan_filter)); 350 options->filters->push_back(std::move(scan_filter));
513 351
514 // Add to map. 352 // Add to map.
515 const WebBluetoothDeviceId device_id = 353 const WebBluetoothDeviceId device_id =
516 allowed_devices_map.AddDevice(kTestOrigin1, kDeviceAddress1, options); 354 allowed_devices.AddDevice(kDeviceAddress1, options);
517 355
518 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessAtLeastOneService( 356 EXPECT_FALSE(allowed_devices.IsAllowedToAccessAtLeastOneService(device_id));
519 kTestOrigin1, device_id));
520 } 357 }
521 358
522 } // namespace content 359 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698