Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 <memory> | |
| 6 | |
| 7 #include "base/memory/ref_counted.h" | |
| 8 #include "device/usb/mock_usb_device.h" | |
| 9 #include "device/usb/usb_descriptors.h" | |
| 10 #include "extensions/common/extension_builder.h" | |
| 11 #include "extensions/common/features/feature_channel.h" | |
| 12 #include "extensions/common/features/feature_session_type.h" | |
| 13 #include "extensions/common/permissions/usb_device_permission.h" | |
| 5 #include "extensions/common/permissions/usb_device_permission_data.h" | 14 #include "extensions/common/permissions/usb_device_permission_data.h" |
| 15 #include "extensions/common/value_builder.h" | |
| 6 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
| 7 | 17 |
| 8 namespace extensions { | 18 namespace extensions { |
| 9 | 19 |
| 20 namespace { | |
| 21 | |
| 22 // ID of test USB device config to which interfaces described by | |
| 23 // |interface_classes| parameter of |CreateTestUsbDevice| should be added. | |
| 24 const int kUsbConfigWithInterfaces = 1; | |
| 25 | |
| 26 // ID of test USB device config created by |CreateTestUsbDevice| that contains | |
| 27 // no interfaces. | |
| 28 const int kUsbConfigWithoutInterfaces = 2; | |
| 29 | |
| 30 scoped_refptr<device::MockUsbDevice> CreateTestUsbDevice( | |
| 31 uint16_t vendor_id, | |
| 32 uint16_t product_id, | |
| 33 uint8_t device_class, | |
| 34 const std::vector<uint8_t> interface_classes) { | |
| 35 std::vector<device::UsbConfigDescriptor> configs; | |
| 36 configs.emplace_back(kUsbConfigWithInterfaces, false, false, 0); | |
| 37 configs.emplace_back(kUsbConfigWithoutInterfaces, false, false, 0); | |
| 38 | |
| 39 for (size_t i = 0; i < interface_classes.size(); ++i) | |
| 40 configs[0].interfaces.emplace_back(i, 0, interface_classes[i], 255, 255); | |
| 41 | |
| 42 return new device::MockUsbDevice(vendor_id, product_id, device_class, | |
| 43 configs); | |
| 44 } | |
| 45 | |
| 46 scoped_refptr<const Extension> CreateTestApp( | |
| 47 std::unique_ptr<base::Value> usb_device_permission) { | |
| 48 return ExtensionBuilder() | |
| 49 .SetManifest( | |
| 50 DictionaryBuilder() | |
| 51 .Set("name", "test app") | |
| 52 .Set("version", "1") | |
| 53 .Set("app", | |
| 54 DictionaryBuilder() | |
| 55 .Set("background", | |
| 56 DictionaryBuilder() | |
| 57 .Set("scripts", ListBuilder() | |
| 58 .Append("background.js") | |
| 59 .Build()) | |
| 60 .Build()) | |
| 61 .Build()) | |
| 62 .Set("permissions", | |
| 63 ListBuilder() | |
| 64 .Append("usb") | |
| 65 .Append( | |
| 66 DictionaryBuilder() | |
| 67 .Set("usbDevice", ListBuilder() | |
| 68 .Append(std::move( | |
| 69 usb_device_permission)) | |
| 70 .Build()) | |
| 71 .Build()) | |
| 72 .Build()) | |
| 73 .Build()) | |
| 74 .Build(); | |
| 75 } | |
| 76 | |
| 77 } // namespace | |
| 78 | |
| 10 TEST(USBDevicePermissionTest, PermissionDataOrder) { | 79 TEST(USBDevicePermissionTest, PermissionDataOrder) { |
| 11 EXPECT_LT(UsbDevicePermissionData(0x02ad, 0x138c, -1), | 80 EXPECT_LT(UsbDevicePermissionData(0x02ad, 0x138c, -1, -1), |
| 12 UsbDevicePermissionData(0x02ad, 0x138d, -1)); | 81 UsbDevicePermissionData(0x02ad, 0x138d, -1, -1)); |
| 13 ASSERT_LT(UsbDevicePermissionData(0x02ad, 0x138d, -1), | 82 ASSERT_LT(UsbDevicePermissionData(0x02ad, 0x138d, -1, -1), |
| 14 UsbDevicePermissionData(0x02ae, 0x138c, -1)); | 83 UsbDevicePermissionData(0x02ae, 0x138c, -1, -1)); |
| 15 EXPECT_LT(UsbDevicePermissionData(0x02ad, 0x138c, -1), | 84 EXPECT_LT(UsbDevicePermissionData(0x02ad, 0x138c, 1, 3), |
| 16 UsbDevicePermissionData(0x02ad, 0x138c, 0)); | 85 UsbDevicePermissionData(0x02ad, 0x138c, 2, 2)); |
| 86 EXPECT_LT(UsbDevicePermissionData(0x02ad, 0x138c, 1, 2), | |
| 87 UsbDevicePermissionData(0x02ad, 0x138c, 1, 3)); | |
| 88 } | |
| 89 | |
| 90 TEST(USBDevicePermissionTest, CheckVendorAndProductId) { | |
| 91 std::unique_ptr<base::Value> permission_data_value = | |
| 92 DictionaryBuilder() | |
| 93 .Set("vendorId", 0x02ad) | |
| 94 .Set("productId", 0x138c) | |
| 95 .Build(); | |
| 96 | |
| 97 UsbDevicePermissionData permission_data; | |
| 98 ASSERT_TRUE(permission_data.FromValue(permission_data_value.get())); | |
| 99 | |
| 100 scoped_refptr<const Extension> app = | |
| 101 CreateTestApp(std::move(permission_data_value)); | |
| 102 | |
| 103 { | |
| 104 std::unique_ptr<UsbDevicePermission::CheckParam> param = | |
| 105 UsbDevicePermission::CheckParam::ForDeviceWithAnyInterfaceClass( | |
| 106 app.get(), 0x02ad, 0x138c, | |
| 107 UsbDevicePermissionData::SPECIAL_VALUE_UNSPECIFIED); | |
| 108 | |
| 109 EXPECT_TRUE(permission_data.Check(param.get())); | |
| 110 } | |
| 111 | |
| 112 { | |
| 113 std::unique_ptr<UsbDevicePermission::CheckParam> param = | |
| 114 UsbDevicePermission::CheckParam::ForDeviceWithAnyInterfaceClass( | |
| 115 app.get(), 0x138c, 0x02ad, | |
| 116 UsbDevicePermissionData::SPECIAL_VALUE_UNSPECIFIED); | |
| 117 | |
| 118 EXPECT_FALSE(permission_data.Check(param.get())); | |
| 119 } | |
| 120 | |
| 121 { | |
| 122 std::unique_ptr<UsbDevicePermission::CheckParam> param = | |
| 123 UsbDevicePermission::CheckParam::ForDeviceWithAnyInterfaceClass( | |
| 124 app.get(), 0x02ad, 0x138c, 3); | |
| 125 | |
| 126 EXPECT_FALSE(permission_data.Check(param.get())); | |
| 127 } | |
| 128 } | |
| 129 | |
| 130 TEST(USBDevicePermissionTest, CheckInterfaceId) { | |
| 131 std::unique_ptr<base::Value> permission_data_value = | |
| 132 DictionaryBuilder() | |
| 133 .Set("vendorId", 0x02ad) | |
| 134 .Set("productId", 0x138c) | |
| 135 .Set("interfaceId", 3) | |
| 136 .Build(); | |
| 137 UsbDevicePermissionData permission_data; | |
| 138 ASSERT_TRUE(permission_data.FromValue(permission_data_value.get())); | |
| 139 | |
| 140 scoped_refptr<const Extension> app = | |
| 141 CreateTestApp(std::move(permission_data_value)); | |
| 142 { | |
| 143 std::unique_ptr<UsbDevicePermission::CheckParam> param = | |
| 144 UsbDevicePermission::CheckParam::ForDeviceWithAnyInterfaceClass( | |
| 145 app.get(), 0x02ad, 0x138c, 3); | |
| 146 | |
| 147 EXPECT_TRUE(permission_data.Check(param.get())); | |
| 148 } | |
| 149 | |
| 150 { | |
| 151 std::unique_ptr<UsbDevicePermission::CheckParam> param = | |
| 152 UsbDevicePermission::CheckParam::ForDeviceWithAnyInterfaceClass( | |
| 153 app.get(), 0x02ad, 0x138c, 2); | |
| 154 | |
| 155 EXPECT_FALSE(permission_data.Check(param.get())); | |
| 156 } | |
| 157 | |
| 158 { | |
| 159 std::unique_ptr<UsbDevicePermission::CheckParam> param = | |
| 160 UsbDevicePermission::CheckParam::ForDeviceWithAnyInterfaceClass( | |
| 161 app.get(), 0x02ad, 0x138c, | |
| 162 UsbDevicePermissionData::SPECIAL_VALUE_UNSPECIFIED); | |
| 163 | |
| 164 EXPECT_TRUE(permission_data.Check(param.get())); | |
| 165 } | |
| 166 } | |
| 167 | |
| 168 TEST(USBDevicePermissionTest, InterfaceClass) { | |
| 169 std::unique_ptr<base::Value> permission_data_value = | |
| 170 DictionaryBuilder().Set("interfaceClass", 3).Build(); | |
| 171 UsbDevicePermissionData permission_data; | |
| 172 EXPECT_TRUE(permission_data.FromValue(permission_data_value.get())); | |
| 173 | |
| 174 scoped_refptr<const Extension> app = | |
| 175 CreateTestApp(std::move(permission_data_value)); | |
| 176 | |
| 177 { | |
| 178 std::unique_ptr<UsbDevicePermission::CheckParam> param = | |
| 179 UsbDevicePermission::CheckParam::ForDeviceWithAnyInterfaceClass( | |
| 180 app.get(), 0x02ad, 0x138c, | |
| 181 UsbDevicePermissionData::SPECIAL_VALUE_UNSPECIFIED); | |
| 182 | |
| 183 EXPECT_FALSE(permission_data.Check(param.get())); | |
| 184 } | |
| 185 | |
| 186 { | |
| 187 std::unique_ptr<base::AutoReset<FeatureSessionType>> scoped_session_type_( | |
| 188 ScopedCurrentFeatureSessionType(FeatureSessionType::KIOSK)); | |
| 189 ScopedCurrentChannel channel(version_info::Channel::DEV); | |
| 190 | |
| 191 std::unique_ptr<UsbDevicePermission::CheckParam> param = | |
| 192 UsbDevicePermission::CheckParam::ForDeviceWithAnyInterfaceClass( | |
| 193 app.get(), 0x02ad, 0x138c, | |
| 194 UsbDevicePermissionData::SPECIAL_VALUE_UNSPECIFIED); | |
| 195 | |
| 196 EXPECT_TRUE(permission_data.Check(param.get())); | |
|
Reilly Grant (use Gerrit)
2016/10/25 20:44:03
Can you add a comment explaining why this is expec
tbarzic
2016/10/25 21:17:08
Done.
Added more detailed comment to ForDeviceWit
| |
| 197 } | |
| 198 } | |
| 199 | |
| 200 TEST(USBDevicePermissionTest, InterfaceClassWithVendorId) { | |
| 201 std::unique_ptr<base::Value> permission_data_value = | |
| 202 DictionaryBuilder() | |
| 203 .Set("vendorId", 0x02ad) | |
| 204 .Set("interfaceClass", 3) | |
| 205 .Build(); | |
| 206 UsbDevicePermissionData permission_data; | |
| 207 EXPECT_TRUE(permission_data.FromValue(permission_data_value.get())); | |
| 208 | |
| 209 scoped_refptr<const Extension> app = | |
| 210 CreateTestApp(std::move(permission_data_value)); | |
| 211 | |
| 212 { | |
| 213 std::unique_ptr<UsbDevicePermission::CheckParam> param = | |
| 214 UsbDevicePermission::CheckParam::ForDeviceWithAnyInterfaceClass( | |
| 215 app.get(), 0x02ad, 0x138c, | |
| 216 UsbDevicePermissionData::SPECIAL_VALUE_UNSPECIFIED); | |
| 217 | |
| 218 EXPECT_FALSE(permission_data.Check(param.get())); | |
| 219 } | |
| 220 | |
| 221 { | |
| 222 std::unique_ptr<base::AutoReset<FeatureSessionType>> scoped_session_type_( | |
| 223 ScopedCurrentFeatureSessionType(FeatureSessionType::KIOSK)); | |
| 224 ScopedCurrentChannel channel(version_info::Channel::DEV); | |
| 225 | |
| 226 std::unique_ptr<UsbDevicePermission::CheckParam> param = | |
| 227 UsbDevicePermission::CheckParam::ForDeviceWithAnyInterfaceClass( | |
| 228 app.get(), 0x02ad, 0x138c, | |
| 229 UsbDevicePermissionData::SPECIAL_VALUE_UNSPECIFIED); | |
| 230 | |
| 231 EXPECT_TRUE(permission_data.Check(param.get())); | |
| 232 } | |
| 233 | |
| 234 { | |
| 235 std::unique_ptr<base::AutoReset<FeatureSessionType>> scoped_session_type_( | |
| 236 ScopedCurrentFeatureSessionType(FeatureSessionType::KIOSK)); | |
| 237 ScopedCurrentChannel channel(version_info::Channel::DEV); | |
| 238 | |
| 239 std::unique_ptr<UsbDevicePermission::CheckParam> param = | |
| 240 UsbDevicePermission::CheckParam::ForDeviceWithAnyInterfaceClass( | |
| 241 app.get(), 0x138c, 0x138c, | |
| 242 UsbDevicePermissionData::SPECIAL_VALUE_UNSPECIFIED); | |
| 243 | |
| 244 EXPECT_FALSE(permission_data.Check(param.get())); | |
| 245 } | |
| 246 } | |
| 247 | |
| 248 TEST(USBDevicePermissionTest, CheckHidUsbAgainstInterfaceClass) { | |
| 249 std::unique_ptr<base::Value> permission_data_value = | |
| 250 DictionaryBuilder() | |
| 251 .Set("vendorId", 0x02ad) | |
| 252 .Set("interfaceClass", 3) | |
| 253 .Build(); | |
| 254 UsbDevicePermissionData permission_data; | |
| 255 EXPECT_TRUE(permission_data.FromValue(permission_data_value.get())); | |
| 256 | |
| 257 scoped_refptr<const Extension> app = | |
| 258 CreateTestApp(std::move(permission_data_value)); | |
| 259 | |
| 260 { | |
| 261 std::unique_ptr<UsbDevicePermission::CheckParam> param = | |
| 262 UsbDevicePermission::CheckParam::ForHidDevice(app.get(), 0x02ad, | |
| 263 0x138c); | |
| 264 | |
| 265 EXPECT_FALSE(permission_data.Check(param.get())); | |
| 266 } | |
| 267 | |
| 268 { | |
| 269 std::unique_ptr<base::AutoReset<FeatureSessionType>> scoped_session_type_( | |
| 270 ScopedCurrentFeatureSessionType(FeatureSessionType::KIOSK)); | |
| 271 ScopedCurrentChannel channel(version_info::Channel::DEV); | |
| 272 | |
| 273 std::unique_ptr<UsbDevicePermission::CheckParam> param = | |
| 274 UsbDevicePermission::CheckParam::ForHidDevice(app.get(), 0x02ad, | |
| 275 0x138c); | |
| 276 | |
| 277 EXPECT_TRUE(permission_data.Check(param.get())); | |
| 278 } | |
| 279 | |
| 280 { | |
| 281 std::unique_ptr<base::AutoReset<FeatureSessionType>> scoped_session_type_( | |
| 282 ScopedCurrentFeatureSessionType(FeatureSessionType::KIOSK)); | |
| 283 ScopedCurrentChannel channel(version_info::Channel::DEV); | |
| 284 | |
| 285 std::unique_ptr<UsbDevicePermission::CheckParam> param = | |
| 286 UsbDevicePermission::CheckParam::ForHidDevice(app.get(), 0x138c, | |
| 287 0x138c); | |
| 288 | |
| 289 EXPECT_FALSE(permission_data.Check(param.get())); | |
| 290 } | |
| 291 } | |
| 292 | |
| 293 TEST(USBDevicePermissionTest, CheckHidUsbAgainstDeviceIds) { | |
| 294 std::unique_ptr<base::Value> permission_data_value = | |
| 295 DictionaryBuilder() | |
| 296 .Set("vendorId", 0x02ad) | |
| 297 .Set("productId", 0x138c) | |
| 298 .Build(); | |
| 299 UsbDevicePermissionData permission_data; | |
| 300 EXPECT_TRUE(permission_data.FromValue(permission_data_value.get())); | |
| 301 | |
| 302 scoped_refptr<const Extension> app = | |
| 303 CreateTestApp(std::move(permission_data_value)); | |
| 304 | |
| 305 { | |
| 306 std::unique_ptr<UsbDevicePermission::CheckParam> param = | |
| 307 UsbDevicePermission::CheckParam::ForHidDevice(app.get(), 0x02ad, | |
| 308 0x138c); | |
| 309 | |
| 310 EXPECT_TRUE(permission_data.Check(param.get())); | |
| 311 } | |
| 312 | |
| 313 { | |
| 314 std::unique_ptr<UsbDevicePermission::CheckParam> param = | |
| 315 UsbDevicePermission::CheckParam::ForHidDevice(app.get(), 0x138c, | |
| 316 0x138c); | |
| 317 | |
| 318 EXPECT_FALSE(permission_data.Check(param.get())); | |
| 319 } | |
| 320 } | |
| 321 | |
| 322 TEST(USBDevicePermissionTest, CheckDeviceAgainstDeviceIds) { | |
| 323 std::unique_ptr<base::Value> permission_data_value = | |
| 324 DictionaryBuilder() | |
| 325 .Set("vendorId", 0x02ad) | |
| 326 .Set("productId", 0x138c) | |
| 327 .Build(); | |
| 328 UsbDevicePermissionData permission_data; | |
| 329 EXPECT_TRUE(permission_data.FromValue(permission_data_value.get())); | |
| 330 | |
| 331 scoped_refptr<const Extension> app = | |
| 332 CreateTestApp(std::move(permission_data_value)); | |
| 333 | |
| 334 { | |
| 335 scoped_refptr<device::MockUsbDevice> device = | |
| 336 CreateTestUsbDevice(0x02ad, 0x138c, 0x9, std::vector<uint8_t>()); | |
| 337 std::unique_ptr<UsbDevicePermission::CheckParam> param = | |
| 338 UsbDevicePermission::CheckParam::ForUsbDevice(app.get(), device.get()); | |
| 339 | |
| 340 EXPECT_TRUE(permission_data.Check(param.get())); | |
| 341 } | |
| 342 | |
| 343 { | |
| 344 scoped_refptr<device::MockUsbDevice> device = | |
| 345 CreateTestUsbDevice(0x138c, 0x138c, 0x9, std::vector<uint8_t>()); | |
| 346 std::unique_ptr<UsbDevicePermission::CheckParam> param = | |
| 347 UsbDevicePermission::CheckParam::ForUsbDevice(app.get(), device.get()); | |
| 348 | |
| 349 EXPECT_FALSE(permission_data.Check(param.get())); | |
| 350 } | |
| 351 } | |
| 352 | |
| 353 TEST(USBDevicePermissionTest, CheckDeviceAgainstDeviceClass) { | |
| 354 std::unique_ptr<base::Value> permission_data_value = | |
| 355 DictionaryBuilder().Set("interfaceClass", 0x9).Build(); | |
| 356 UsbDevicePermissionData permission_data; | |
| 357 EXPECT_TRUE(permission_data.FromValue(permission_data_value.get())); | |
| 358 | |
| 359 scoped_refptr<const Extension> app = | |
| 360 CreateTestApp(std::move(permission_data_value)); | |
| 361 | |
| 362 { | |
| 363 scoped_refptr<device::MockUsbDevice> device = | |
| 364 CreateTestUsbDevice(0x02ad, 0x138c, 0x9, std::vector<uint8_t>()); | |
| 365 std::unique_ptr<UsbDevicePermission::CheckParam> param = | |
| 366 UsbDevicePermission::CheckParam::ForUsbDevice(app.get(), device.get()); | |
| 367 | |
| 368 EXPECT_FALSE(permission_data.Check(param.get())); | |
| 369 } | |
| 370 | |
| 371 { | |
| 372 std::unique_ptr<base::AutoReset<FeatureSessionType>> scoped_session_type_( | |
| 373 ScopedCurrentFeatureSessionType(FeatureSessionType::KIOSK)); | |
| 374 ScopedCurrentChannel channel(version_info::Channel::DEV); | |
| 375 | |
| 376 scoped_refptr<device::MockUsbDevice> device = | |
| 377 CreateTestUsbDevice(0x02ad, 0x138c, 0x9, std::vector<uint8_t>()); | |
| 378 std::unique_ptr<UsbDevicePermission::CheckParam> param = | |
| 379 UsbDevicePermission::CheckParam::ForUsbDevice(app.get(), device.get()); | |
| 380 | |
| 381 EXPECT_TRUE(permission_data.Check(param.get())); | |
| 382 } | |
| 383 { | |
| 384 std::unique_ptr<base::AutoReset<FeatureSessionType>> scoped_session_type_( | |
| 385 ScopedCurrentFeatureSessionType(FeatureSessionType::KIOSK)); | |
| 386 ScopedCurrentChannel channel(version_info::Channel::DEV); | |
| 387 | |
| 388 scoped_refptr<device::MockUsbDevice> device = | |
| 389 CreateTestUsbDevice(0x02ad, 0x138c, 0x3, std::vector<uint8_t>()); | |
| 390 std::unique_ptr<UsbDevicePermission::CheckParam> param = | |
| 391 UsbDevicePermission::CheckParam::ForUsbDevice(app.get(), device.get()); | |
| 392 | |
| 393 EXPECT_FALSE(permission_data.Check(param.get())); | |
| 394 } | |
| 395 } | |
| 396 | |
| 397 TEST(USBDevicePermissionTest, IgnoreNullDeviceClass) { | |
| 398 std::unique_ptr<base::Value> permission_data_value = | |
| 399 DictionaryBuilder().Set("interfaceClass", 0).Build(); | |
| 400 UsbDevicePermissionData permission_data; | |
| 401 EXPECT_TRUE(permission_data.FromValue(permission_data_value.get())); | |
| 402 | |
| 403 scoped_refptr<const Extension> app = | |
| 404 CreateTestApp(std::move(permission_data_value)); | |
| 405 | |
| 406 { | |
| 407 std::unique_ptr<base::AutoReset<FeatureSessionType>> scoped_session_type_( | |
| 408 ScopedCurrentFeatureSessionType(FeatureSessionType::KIOSK)); | |
| 409 ScopedCurrentChannel channel(version_info::Channel::DEV); | |
| 410 | |
| 411 scoped_refptr<device::MockUsbDevice> device = | |
| 412 CreateTestUsbDevice(0x02ad, 0x138c, 0, std::vector<uint8_t>()); | |
| 413 std::unique_ptr<UsbDevicePermission::CheckParam> param = | |
| 414 UsbDevicePermission::CheckParam::ForUsbDevice(app.get(), device.get()); | |
| 415 | |
| 416 EXPECT_FALSE(permission_data.Check(param.get())); | |
| 417 } | |
| 418 } | |
| 419 | |
| 420 TEST(USBDevicePermissionTest, CheckDeviceAgainstInterfaceClass) { | |
| 421 std::unique_ptr<base::Value> permission_data_value = | |
| 422 DictionaryBuilder().Set("interfaceClass", 0x3).Build(); | |
| 423 UsbDevicePermissionData permission_data; | |
| 424 EXPECT_TRUE(permission_data.FromValue(permission_data_value.get())); | |
| 425 | |
| 426 scoped_refptr<const Extension> app = | |
| 427 CreateTestApp(std::move(permission_data_value)); | |
| 428 | |
| 429 { | |
| 430 scoped_refptr<device::MockUsbDevice> device = | |
| 431 CreateTestUsbDevice(0x02ad, 0x138c, 0, {2, 3}); | |
| 432 std::unique_ptr<UsbDevicePermission::CheckParam> param = | |
| 433 UsbDevicePermission::CheckParam::ForUsbDevice(app.get(), device.get()); | |
| 434 | |
| 435 EXPECT_FALSE(permission_data.Check(param.get())); | |
| 436 } | |
| 437 | |
| 438 { | |
| 439 // Interface should match inactive configuration when none of configurations | |
| 440 // is active. | |
| 441 std::unique_ptr<base::AutoReset<FeatureSessionType>> scoped_session_type_( | |
| 442 ScopedCurrentFeatureSessionType(FeatureSessionType::KIOSK)); | |
| 443 ScopedCurrentChannel channel(version_info::Channel::DEV); | |
| 444 | |
| 445 scoped_refptr<device::MockUsbDevice> device = | |
| 446 CreateTestUsbDevice(0x02ad, 0x138c, 0, {2, 3}); | |
| 447 std::unique_ptr<UsbDevicePermission::CheckParam> param = | |
| 448 UsbDevicePermission::CheckParam::ForUsbDevice(app.get(), device.get()); | |
| 449 | |
| 450 EXPECT_TRUE(permission_data.Check(param.get())); | |
| 451 } | |
| 452 | |
| 453 { | |
| 454 std::unique_ptr<base::AutoReset<FeatureSessionType>> scoped_session_type_( | |
| 455 ScopedCurrentFeatureSessionType(FeatureSessionType::KIOSK)); | |
| 456 ScopedCurrentChannel channel(version_info::Channel::DEV); | |
| 457 | |
| 458 scoped_refptr<device::MockUsbDevice> device = | |
| 459 CreateTestUsbDevice(0x02ad, 0x138c, 0, {2, 3}); | |
| 460 device->ActiveConfigurationChanged(kUsbConfigWithInterfaces); | |
| 461 std::unique_ptr<UsbDevicePermission::CheckParam> param = | |
| 462 UsbDevicePermission::CheckParam::ForUsbDevice(app.get(), device.get()); | |
| 463 | |
| 464 EXPECT_TRUE(permission_data.Check(param.get())); | |
| 465 } | |
| 466 | |
| 467 { | |
| 468 // Interface should match inactive configuration when another configuration | |
| 469 // is active. | |
| 470 std::unique_ptr<base::AutoReset<FeatureSessionType>> scoped_session_type_( | |
| 471 ScopedCurrentFeatureSessionType(FeatureSessionType::KIOSK)); | |
| 472 ScopedCurrentChannel channel(version_info::Channel::DEV); | |
| 473 | |
| 474 scoped_refptr<device::MockUsbDevice> device = | |
| 475 CreateTestUsbDevice(0x02ad, 0x138c, 0, {2, 3}); | |
| 476 device->ActiveConfigurationChanged(kUsbConfigWithoutInterfaces); | |
| 477 std::unique_ptr<UsbDevicePermission::CheckParam> param = | |
| 478 UsbDevicePermission::CheckParam::ForUsbDevice(app.get(), device.get()); | |
| 479 | |
| 480 EXPECT_TRUE(permission_data.Check(param.get())); | |
| 481 } | |
| 482 | |
| 483 { | |
| 484 std::unique_ptr<base::AutoReset<FeatureSessionType>> scoped_session_type_( | |
| 485 ScopedCurrentFeatureSessionType(FeatureSessionType::KIOSK)); | |
| 486 ScopedCurrentChannel channel(version_info::Channel::DEV); | |
| 487 | |
| 488 scoped_refptr<device::MockUsbDevice> device = | |
| 489 CreateTestUsbDevice(0x02ad, 0x138c, 0, {4, 5}); | |
| 490 std::unique_ptr<UsbDevicePermission::CheckParam> param = | |
| 491 UsbDevicePermission::CheckParam::ForUsbDevice(app.get(), device.get()); | |
| 492 | |
| 493 EXPECT_FALSE(permission_data.Check(param.get())); | |
| 494 } | |
| 495 } | |
| 496 | |
| 497 TEST(USBDevicePermissionTest, CheckDeviceAndInterfaceId) { | |
| 498 std::unique_ptr<base::Value> permission_data_value = | |
| 499 DictionaryBuilder() | |
| 500 .Set("vendorId", 0x02ad) | |
| 501 .Set("productId", 0x138c) | |
| 502 .Set("interfaceId", 3) | |
| 503 .Build(); | |
| 504 UsbDevicePermissionData permission_data; | |
| 505 EXPECT_TRUE(permission_data.FromValue(permission_data_value.get())); | |
| 506 | |
| 507 scoped_refptr<const Extension> app = | |
| 508 CreateTestApp(std::move(permission_data_value)); | |
| 509 | |
| 510 { | |
| 511 scoped_refptr<device::MockUsbDevice> device = | |
| 512 CreateTestUsbDevice(0x02ad, 0x138c, 0, std::vector<uint8_t>()); | |
| 513 std::unique_ptr<UsbDevicePermission::CheckParam> param = | |
| 514 UsbDevicePermission::CheckParam::ForUsbDeviceAndInterface( | |
| 515 app.get(), device.get(), 3); | |
| 516 | |
| 517 EXPECT_TRUE(permission_data.Check(param.get())); | |
| 518 } | |
| 519 | |
| 520 { | |
| 521 scoped_refptr<device::MockUsbDevice> device = | |
| 522 CreateTestUsbDevice(0x02ad, 0x138c, 0, std::vector<uint8_t>()); | |
| 523 std::unique_ptr<UsbDevicePermission::CheckParam> param = | |
| 524 UsbDevicePermission::CheckParam::ForUsbDeviceAndInterface( | |
| 525 app.get(), device.get(), 2); | |
| 526 | |
| 527 EXPECT_FALSE(permission_data.Check(param.get())); | |
| 528 } | |
| 529 } | |
| 530 | |
| 531 TEST(USBDevicePermissionTest, | |
| 532 CheckDeviceAndInterfaceIDAgainstMissingInterfaceId) { | |
| 533 std::unique_ptr<base::Value> permission_data_value = | |
| 534 DictionaryBuilder() | |
| 535 .Set("vendorId", 0x02ad) | |
| 536 .Set("productId", 0x138c) | |
| 537 .Build(); | |
| 538 UsbDevicePermissionData permission_data; | |
| 539 EXPECT_TRUE(permission_data.FromValue(permission_data_value.get())); | |
| 540 | |
| 541 scoped_refptr<const Extension> app = | |
| 542 CreateTestApp(std::move(permission_data_value)); | |
| 543 | |
| 544 { | |
| 545 scoped_refptr<device::MockUsbDevice> device = | |
| 546 CreateTestUsbDevice(0x02ad, 0x138c, 0, std::vector<uint8_t>()); | |
| 547 std::unique_ptr<UsbDevicePermission::CheckParam> param = | |
| 548 UsbDevicePermission::CheckParam::ForUsbDeviceAndInterface( | |
| 549 app.get(), device.get(), 3); | |
| 550 | |
| 551 EXPECT_FALSE(permission_data.Check(param.get())); | |
| 552 } | |
| 553 } | |
| 554 | |
| 555 TEST(USBDevicePermissionTest, InvalidPermission_NoVendorId) { | |
| 556 std::unique_ptr<base::Value> permission_data_value = | |
| 557 DictionaryBuilder() | |
| 558 .Set("productId", 0x138c) | |
| 559 .Set("interfaceClass", 3) | |
| 560 .Build(); | |
| 561 UsbDevicePermissionData permission_data; | |
| 562 ASSERT_FALSE(permission_data.FromValue(permission_data_value.get())); | |
| 563 } | |
| 564 | |
| 565 TEST(USBDevicePermissionTest, InvalidPermission_OnlyVendorId) { | |
| 566 std::unique_ptr<base::Value> permission_data_value = | |
| 567 DictionaryBuilder().Set("vendorId", 0x02ad).Build(); | |
| 568 UsbDevicePermissionData permission_data; | |
| 569 ASSERT_FALSE(permission_data.FromValue(permission_data_value.get())); | |
| 570 } | |
| 571 | |
| 572 TEST(USBDevicePermissionTest, InvalidPermission_NoProductIdWithInterfaceId) { | |
| 573 std::unique_ptr<base::Value> permission_data_value = | |
| 574 DictionaryBuilder().Set("vendorId", 0x02ad).Set("interfaceId", 3).Build(); | |
| 575 UsbDevicePermissionData permission_data; | |
| 576 ASSERT_FALSE(permission_data.FromValue(permission_data_value.get())); | |
| 577 } | |
| 578 | |
| 579 TEST(USBDevicePermissionTest, RejectInterfaceIdIfInterfaceClassPresent) { | |
| 580 std::unique_ptr<base::Value> permission_data_value = | |
| 581 DictionaryBuilder() | |
| 582 .Set("vendorId", 0x02ad) | |
| 583 .Set("productId", 0x128c) | |
| 584 .Set("interfaceId", 3) | |
| 585 .Set("interfaceClass", 7) | |
| 586 .Build(); | |
| 587 UsbDevicePermissionData permission_data; | |
| 588 ASSERT_FALSE(permission_data.FromValue(permission_data_value.get())); | |
| 17 } | 589 } |
| 18 | 590 |
| 19 } // namespace extensions | 591 } // namespace extensions |
| OLD | NEW |