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