| 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 // Interface class permission should be ignored when not in kiosk session. |
| 184 EXPECT_FALSE(permission_data.Check(param.get())); |
| 185 } |
| 186 |
| 187 { |
| 188 std::unique_ptr<base::AutoReset<FeatureSessionType>> scoped_session_type_( |
| 189 ScopedCurrentFeatureSessionType(FeatureSessionType::KIOSK)); |
| 190 ScopedCurrentChannel channel(version_info::Channel::DEV); |
| 191 |
| 192 std::unique_ptr<UsbDevicePermission::CheckParam> param = |
| 193 UsbDevicePermission::CheckParam::ForDeviceWithAnyInterfaceClass( |
| 194 app.get(), 0x02ad, 0x138c, |
| 195 UsbDevicePermissionData::SPECIAL_VALUE_UNSPECIFIED); |
| 196 |
| 197 // |ForDeviceWithAnyInterfaceClass| creates check param that matches any |
| 198 // interfaceClass permission property - needed in cases where set of |
| 199 // interface classes supported by a USB device is not known. |
| 200 // In this case, there is a usbDevice permisison with only interfaceClass |
| 201 // set, so the param is accepted. |
| 202 EXPECT_TRUE(permission_data.Check(param.get())); |
| 203 } |
| 204 } |
| 205 |
| 206 TEST(USBDevicePermissionTest, InterfaceClassWithVendorId) { |
| 207 std::unique_ptr<base::Value> permission_data_value = |
| 208 DictionaryBuilder() |
| 209 .Set("vendorId", 0x02ad) |
| 210 .Set("interfaceClass", 3) |
| 211 .Build(); |
| 212 UsbDevicePermissionData permission_data; |
| 213 EXPECT_TRUE(permission_data.FromValue(permission_data_value.get())); |
| 214 |
| 215 scoped_refptr<const Extension> app = |
| 216 CreateTestApp(std::move(permission_data_value)); |
| 217 |
| 218 { |
| 219 std::unique_ptr<UsbDevicePermission::CheckParam> param = |
| 220 UsbDevicePermission::CheckParam::ForDeviceWithAnyInterfaceClass( |
| 221 app.get(), 0x02ad, 0x138c, |
| 222 UsbDevicePermissionData::SPECIAL_VALUE_UNSPECIFIED); |
| 223 |
| 224 EXPECT_FALSE(permission_data.Check(param.get())); |
| 225 } |
| 226 |
| 227 { |
| 228 std::unique_ptr<base::AutoReset<FeatureSessionType>> scoped_session_type_( |
| 229 ScopedCurrentFeatureSessionType(FeatureSessionType::KIOSK)); |
| 230 ScopedCurrentChannel channel(version_info::Channel::DEV); |
| 231 |
| 232 std::unique_ptr<UsbDevicePermission::CheckParam> param = |
| 233 UsbDevicePermission::CheckParam::ForDeviceWithAnyInterfaceClass( |
| 234 app.get(), 0x02ad, 0x138c, |
| 235 UsbDevicePermissionData::SPECIAL_VALUE_UNSPECIFIED); |
| 236 |
| 237 EXPECT_TRUE(permission_data.Check(param.get())); |
| 238 } |
| 239 |
| 240 { |
| 241 std::unique_ptr<base::AutoReset<FeatureSessionType>> scoped_session_type_( |
| 242 ScopedCurrentFeatureSessionType(FeatureSessionType::KIOSK)); |
| 243 ScopedCurrentChannel channel(version_info::Channel::DEV); |
| 244 |
| 245 std::unique_ptr<UsbDevicePermission::CheckParam> param = |
| 246 UsbDevicePermission::CheckParam::ForDeviceWithAnyInterfaceClass( |
| 247 app.get(), 0x138c, 0x138c, |
| 248 UsbDevicePermissionData::SPECIAL_VALUE_UNSPECIFIED); |
| 249 |
| 250 EXPECT_FALSE(permission_data.Check(param.get())); |
| 251 } |
| 252 } |
| 253 |
| 254 TEST(USBDevicePermissionTest, CheckHidUsbAgainstInterfaceClass) { |
| 255 std::unique_ptr<base::Value> permission_data_value = |
| 256 DictionaryBuilder() |
| 257 .Set("vendorId", 0x02ad) |
| 258 .Set("interfaceClass", 3) |
| 259 .Build(); |
| 260 UsbDevicePermissionData permission_data; |
| 261 EXPECT_TRUE(permission_data.FromValue(permission_data_value.get())); |
| 262 |
| 263 scoped_refptr<const Extension> app = |
| 264 CreateTestApp(std::move(permission_data_value)); |
| 265 |
| 266 { |
| 267 std::unique_ptr<UsbDevicePermission::CheckParam> param = |
| 268 UsbDevicePermission::CheckParam::ForHidDevice(app.get(), 0x02ad, |
| 269 0x138c); |
| 270 |
| 271 EXPECT_FALSE(permission_data.Check(param.get())); |
| 272 } |
| 273 |
| 274 { |
| 275 std::unique_ptr<base::AutoReset<FeatureSessionType>> scoped_session_type_( |
| 276 ScopedCurrentFeatureSessionType(FeatureSessionType::KIOSK)); |
| 277 ScopedCurrentChannel channel(version_info::Channel::DEV); |
| 278 |
| 279 std::unique_ptr<UsbDevicePermission::CheckParam> param = |
| 280 UsbDevicePermission::CheckParam::ForHidDevice(app.get(), 0x02ad, |
| 281 0x138c); |
| 282 |
| 283 EXPECT_TRUE(permission_data.Check(param.get())); |
| 284 } |
| 285 |
| 286 { |
| 287 std::unique_ptr<base::AutoReset<FeatureSessionType>> scoped_session_type_( |
| 288 ScopedCurrentFeatureSessionType(FeatureSessionType::KIOSK)); |
| 289 ScopedCurrentChannel channel(version_info::Channel::DEV); |
| 290 |
| 291 std::unique_ptr<UsbDevicePermission::CheckParam> param = |
| 292 UsbDevicePermission::CheckParam::ForHidDevice(app.get(), 0x138c, |
| 293 0x138c); |
| 294 |
| 295 EXPECT_FALSE(permission_data.Check(param.get())); |
| 296 } |
| 297 } |
| 298 |
| 299 TEST(USBDevicePermissionTest, CheckHidUsbAgainstDeviceIds) { |
| 300 std::unique_ptr<base::Value> permission_data_value = |
| 301 DictionaryBuilder() |
| 302 .Set("vendorId", 0x02ad) |
| 303 .Set("productId", 0x138c) |
| 304 .Build(); |
| 305 UsbDevicePermissionData permission_data; |
| 306 EXPECT_TRUE(permission_data.FromValue(permission_data_value.get())); |
| 307 |
| 308 scoped_refptr<const Extension> app = |
| 309 CreateTestApp(std::move(permission_data_value)); |
| 310 |
| 311 { |
| 312 std::unique_ptr<UsbDevicePermission::CheckParam> param = |
| 313 UsbDevicePermission::CheckParam::ForHidDevice(app.get(), 0x02ad, |
| 314 0x138c); |
| 315 |
| 316 EXPECT_TRUE(permission_data.Check(param.get())); |
| 317 } |
| 318 |
| 319 { |
| 320 std::unique_ptr<UsbDevicePermission::CheckParam> param = |
| 321 UsbDevicePermission::CheckParam::ForHidDevice(app.get(), 0x138c, |
| 322 0x138c); |
| 323 |
| 324 EXPECT_FALSE(permission_data.Check(param.get())); |
| 325 } |
| 326 } |
| 327 |
| 328 TEST(USBDevicePermissionTest, CheckDeviceAgainstDeviceIds) { |
| 329 std::unique_ptr<base::Value> permission_data_value = |
| 330 DictionaryBuilder() |
| 331 .Set("vendorId", 0x02ad) |
| 332 .Set("productId", 0x138c) |
| 333 .Build(); |
| 334 UsbDevicePermissionData permission_data; |
| 335 EXPECT_TRUE(permission_data.FromValue(permission_data_value.get())); |
| 336 |
| 337 scoped_refptr<const Extension> app = |
| 338 CreateTestApp(std::move(permission_data_value)); |
| 339 |
| 340 { |
| 341 scoped_refptr<device::MockUsbDevice> device = |
| 342 CreateTestUsbDevice(0x02ad, 0x138c, 0x9, std::vector<uint8_t>()); |
| 343 std::unique_ptr<UsbDevicePermission::CheckParam> param = |
| 344 UsbDevicePermission::CheckParam::ForUsbDevice(app.get(), device.get()); |
| 345 |
| 346 EXPECT_TRUE(permission_data.Check(param.get())); |
| 347 } |
| 348 |
| 349 { |
| 350 scoped_refptr<device::MockUsbDevice> device = |
| 351 CreateTestUsbDevice(0x138c, 0x138c, 0x9, std::vector<uint8_t>()); |
| 352 std::unique_ptr<UsbDevicePermission::CheckParam> param = |
| 353 UsbDevicePermission::CheckParam::ForUsbDevice(app.get(), device.get()); |
| 354 |
| 355 EXPECT_FALSE(permission_data.Check(param.get())); |
| 356 } |
| 357 } |
| 358 |
| 359 TEST(USBDevicePermissionTest, CheckDeviceAgainstDeviceClass) { |
| 360 std::unique_ptr<base::Value> permission_data_value = |
| 361 DictionaryBuilder().Set("interfaceClass", 0x9).Build(); |
| 362 UsbDevicePermissionData permission_data; |
| 363 EXPECT_TRUE(permission_data.FromValue(permission_data_value.get())); |
| 364 |
| 365 scoped_refptr<const Extension> app = |
| 366 CreateTestApp(std::move(permission_data_value)); |
| 367 |
| 368 { |
| 369 scoped_refptr<device::MockUsbDevice> device = |
| 370 CreateTestUsbDevice(0x02ad, 0x138c, 0x9, std::vector<uint8_t>()); |
| 371 std::unique_ptr<UsbDevicePermission::CheckParam> param = |
| 372 UsbDevicePermission::CheckParam::ForUsbDevice(app.get(), device.get()); |
| 373 |
| 374 EXPECT_FALSE(permission_data.Check(param.get())); |
| 375 } |
| 376 |
| 377 { |
| 378 std::unique_ptr<base::AutoReset<FeatureSessionType>> scoped_session_type_( |
| 379 ScopedCurrentFeatureSessionType(FeatureSessionType::KIOSK)); |
| 380 ScopedCurrentChannel channel(version_info::Channel::DEV); |
| 381 |
| 382 scoped_refptr<device::MockUsbDevice> device = |
| 383 CreateTestUsbDevice(0x02ad, 0x138c, 0x9, std::vector<uint8_t>()); |
| 384 std::unique_ptr<UsbDevicePermission::CheckParam> param = |
| 385 UsbDevicePermission::CheckParam::ForUsbDevice(app.get(), device.get()); |
| 386 |
| 387 EXPECT_TRUE(permission_data.Check(param.get())); |
| 388 } |
| 389 { |
| 390 std::unique_ptr<base::AutoReset<FeatureSessionType>> scoped_session_type_( |
| 391 ScopedCurrentFeatureSessionType(FeatureSessionType::KIOSK)); |
| 392 ScopedCurrentChannel channel(version_info::Channel::DEV); |
| 393 |
| 394 scoped_refptr<device::MockUsbDevice> device = |
| 395 CreateTestUsbDevice(0x02ad, 0x138c, 0x3, std::vector<uint8_t>()); |
| 396 std::unique_ptr<UsbDevicePermission::CheckParam> param = |
| 397 UsbDevicePermission::CheckParam::ForUsbDevice(app.get(), device.get()); |
| 398 |
| 399 EXPECT_FALSE(permission_data.Check(param.get())); |
| 400 } |
| 401 } |
| 402 |
| 403 TEST(USBDevicePermissionTest, IgnoreNullDeviceClass) { |
| 404 std::unique_ptr<base::Value> permission_data_value = |
| 405 DictionaryBuilder().Set("interfaceClass", 0).Build(); |
| 406 UsbDevicePermissionData permission_data; |
| 407 EXPECT_TRUE(permission_data.FromValue(permission_data_value.get())); |
| 408 |
| 409 scoped_refptr<const Extension> app = |
| 410 CreateTestApp(std::move(permission_data_value)); |
| 411 |
| 412 { |
| 413 std::unique_ptr<base::AutoReset<FeatureSessionType>> scoped_session_type_( |
| 414 ScopedCurrentFeatureSessionType(FeatureSessionType::KIOSK)); |
| 415 ScopedCurrentChannel channel(version_info::Channel::DEV); |
| 416 |
| 417 scoped_refptr<device::MockUsbDevice> device = |
| 418 CreateTestUsbDevice(0x02ad, 0x138c, 0, std::vector<uint8_t>()); |
| 419 std::unique_ptr<UsbDevicePermission::CheckParam> param = |
| 420 UsbDevicePermission::CheckParam::ForUsbDevice(app.get(), device.get()); |
| 421 |
| 422 EXPECT_FALSE(permission_data.Check(param.get())); |
| 423 } |
| 424 } |
| 425 |
| 426 TEST(USBDevicePermissionTest, CheckDeviceAgainstInterfaceClass) { |
| 427 std::unique_ptr<base::Value> permission_data_value = |
| 428 DictionaryBuilder().Set("interfaceClass", 0x3).Build(); |
| 429 UsbDevicePermissionData permission_data; |
| 430 EXPECT_TRUE(permission_data.FromValue(permission_data_value.get())); |
| 431 |
| 432 scoped_refptr<const Extension> app = |
| 433 CreateTestApp(std::move(permission_data_value)); |
| 434 |
| 435 { |
| 436 scoped_refptr<device::MockUsbDevice> device = |
| 437 CreateTestUsbDevice(0x02ad, 0x138c, 0, {2, 3}); |
| 438 std::unique_ptr<UsbDevicePermission::CheckParam> param = |
| 439 UsbDevicePermission::CheckParam::ForUsbDevice(app.get(), device.get()); |
| 440 |
| 441 EXPECT_FALSE(permission_data.Check(param.get())); |
| 442 } |
| 443 |
| 444 { |
| 445 // Interface should match inactive configuration when none of configurations |
| 446 // is active. |
| 447 std::unique_ptr<base::AutoReset<FeatureSessionType>> scoped_session_type_( |
| 448 ScopedCurrentFeatureSessionType(FeatureSessionType::KIOSK)); |
| 449 ScopedCurrentChannel channel(version_info::Channel::DEV); |
| 450 |
| 451 scoped_refptr<device::MockUsbDevice> device = |
| 452 CreateTestUsbDevice(0x02ad, 0x138c, 0, {2, 3}); |
| 453 std::unique_ptr<UsbDevicePermission::CheckParam> param = |
| 454 UsbDevicePermission::CheckParam::ForUsbDevice(app.get(), device.get()); |
| 455 |
| 456 EXPECT_TRUE(permission_data.Check(param.get())); |
| 457 } |
| 458 |
| 459 { |
| 460 std::unique_ptr<base::AutoReset<FeatureSessionType>> scoped_session_type_( |
| 461 ScopedCurrentFeatureSessionType(FeatureSessionType::KIOSK)); |
| 462 ScopedCurrentChannel channel(version_info::Channel::DEV); |
| 463 |
| 464 scoped_refptr<device::MockUsbDevice> device = |
| 465 CreateTestUsbDevice(0x02ad, 0x138c, 0, {2, 3}); |
| 466 device->ActiveConfigurationChanged(kUsbConfigWithInterfaces); |
| 467 std::unique_ptr<UsbDevicePermission::CheckParam> param = |
| 468 UsbDevicePermission::CheckParam::ForUsbDevice(app.get(), device.get()); |
| 469 |
| 470 EXPECT_TRUE(permission_data.Check(param.get())); |
| 471 } |
| 472 |
| 473 { |
| 474 // Interface should match inactive configuration when another configuration |
| 475 // is active. |
| 476 std::unique_ptr<base::AutoReset<FeatureSessionType>> scoped_session_type_( |
| 477 ScopedCurrentFeatureSessionType(FeatureSessionType::KIOSK)); |
| 478 ScopedCurrentChannel channel(version_info::Channel::DEV); |
| 479 |
| 480 scoped_refptr<device::MockUsbDevice> device = |
| 481 CreateTestUsbDevice(0x02ad, 0x138c, 0, {2, 3}); |
| 482 device->ActiveConfigurationChanged(kUsbConfigWithoutInterfaces); |
| 483 std::unique_ptr<UsbDevicePermission::CheckParam> param = |
| 484 UsbDevicePermission::CheckParam::ForUsbDevice(app.get(), device.get()); |
| 485 |
| 486 EXPECT_TRUE(permission_data.Check(param.get())); |
| 487 } |
| 488 |
| 489 { |
| 490 std::unique_ptr<base::AutoReset<FeatureSessionType>> scoped_session_type_( |
| 491 ScopedCurrentFeatureSessionType(FeatureSessionType::KIOSK)); |
| 492 ScopedCurrentChannel channel(version_info::Channel::DEV); |
| 493 |
| 494 scoped_refptr<device::MockUsbDevice> device = |
| 495 CreateTestUsbDevice(0x02ad, 0x138c, 0, {4, 5}); |
| 496 std::unique_ptr<UsbDevicePermission::CheckParam> param = |
| 497 UsbDevicePermission::CheckParam::ForUsbDevice(app.get(), device.get()); |
| 498 |
| 499 EXPECT_FALSE(permission_data.Check(param.get())); |
| 500 } |
| 501 } |
| 502 |
| 503 TEST(USBDevicePermissionTest, CheckDeviceAndInterfaceId) { |
| 504 std::unique_ptr<base::Value> permission_data_value = |
| 505 DictionaryBuilder() |
| 506 .Set("vendorId", 0x02ad) |
| 507 .Set("productId", 0x138c) |
| 508 .Set("interfaceId", 3) |
| 509 .Build(); |
| 510 UsbDevicePermissionData permission_data; |
| 511 EXPECT_TRUE(permission_data.FromValue(permission_data_value.get())); |
| 512 |
| 513 scoped_refptr<const Extension> app = |
| 514 CreateTestApp(std::move(permission_data_value)); |
| 515 |
| 516 { |
| 517 scoped_refptr<device::MockUsbDevice> device = |
| 518 CreateTestUsbDevice(0x02ad, 0x138c, 0, std::vector<uint8_t>()); |
| 519 std::unique_ptr<UsbDevicePermission::CheckParam> param = |
| 520 UsbDevicePermission::CheckParam::ForUsbDeviceAndInterface( |
| 521 app.get(), device.get(), 3); |
| 522 |
| 523 EXPECT_TRUE(permission_data.Check(param.get())); |
| 524 } |
| 525 |
| 526 { |
| 527 scoped_refptr<device::MockUsbDevice> device = |
| 528 CreateTestUsbDevice(0x02ad, 0x138c, 0, std::vector<uint8_t>()); |
| 529 std::unique_ptr<UsbDevicePermission::CheckParam> param = |
| 530 UsbDevicePermission::CheckParam::ForUsbDeviceAndInterface( |
| 531 app.get(), device.get(), 2); |
| 532 |
| 533 EXPECT_FALSE(permission_data.Check(param.get())); |
| 534 } |
| 535 } |
| 536 |
| 537 TEST(USBDevicePermissionTest, |
| 538 CheckDeviceAndInterfaceIDAgainstMissingInterfaceId) { |
| 539 std::unique_ptr<base::Value> permission_data_value = |
| 540 DictionaryBuilder() |
| 541 .Set("vendorId", 0x02ad) |
| 542 .Set("productId", 0x138c) |
| 543 .Build(); |
| 544 UsbDevicePermissionData permission_data; |
| 545 EXPECT_TRUE(permission_data.FromValue(permission_data_value.get())); |
| 546 |
| 547 scoped_refptr<const Extension> app = |
| 548 CreateTestApp(std::move(permission_data_value)); |
| 549 |
| 550 { |
| 551 scoped_refptr<device::MockUsbDevice> device = |
| 552 CreateTestUsbDevice(0x02ad, 0x138c, 0, std::vector<uint8_t>()); |
| 553 std::unique_ptr<UsbDevicePermission::CheckParam> param = |
| 554 UsbDevicePermission::CheckParam::ForUsbDeviceAndInterface( |
| 555 app.get(), device.get(), 3); |
| 556 |
| 557 EXPECT_FALSE(permission_data.Check(param.get())); |
| 558 } |
| 559 } |
| 560 |
| 561 TEST(USBDevicePermissionTest, InvalidPermission_NoVendorId) { |
| 562 std::unique_ptr<base::Value> permission_data_value = |
| 563 DictionaryBuilder() |
| 564 .Set("productId", 0x138c) |
| 565 .Set("interfaceClass", 3) |
| 566 .Build(); |
| 567 UsbDevicePermissionData permission_data; |
| 568 ASSERT_FALSE(permission_data.FromValue(permission_data_value.get())); |
| 569 } |
| 570 |
| 571 TEST(USBDevicePermissionTest, InvalidPermission_OnlyVendorId) { |
| 572 std::unique_ptr<base::Value> permission_data_value = |
| 573 DictionaryBuilder().Set("vendorId", 0x02ad).Build(); |
| 574 UsbDevicePermissionData permission_data; |
| 575 ASSERT_FALSE(permission_data.FromValue(permission_data_value.get())); |
| 576 } |
| 577 |
| 578 TEST(USBDevicePermissionTest, InvalidPermission_NoProductIdWithInterfaceId) { |
| 579 std::unique_ptr<base::Value> permission_data_value = |
| 580 DictionaryBuilder().Set("vendorId", 0x02ad).Set("interfaceId", 3).Build(); |
| 581 UsbDevicePermissionData permission_data; |
| 582 ASSERT_FALSE(permission_data.FromValue(permission_data_value.get())); |
| 583 } |
| 584 |
| 585 TEST(USBDevicePermissionTest, RejectInterfaceIdIfInterfaceClassPresent) { |
| 586 std::unique_ptr<base::Value> permission_data_value = |
| 587 DictionaryBuilder() |
| 588 .Set("vendorId", 0x02ad) |
| 589 .Set("productId", 0x128c) |
| 590 .Set("interfaceId", 3) |
| 591 .Set("interfaceClass", 7) |
| 592 .Build(); |
| 593 UsbDevicePermissionData permission_data; |
| 594 ASSERT_FALSE(permission_data.FromValue(permission_data_value.get())); |
| 17 } | 595 } |
| 18 | 596 |
| 19 } // namespace extensions | 597 } // namespace extensions |
| OLD | NEW |