| 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())); |
| 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 std::unique_ptr<base::AutoReset<FeatureSessionType>> scoped_session_type_( |
| 440 ScopedCurrentFeatureSessionType(FeatureSessionType::KIOSK)); |
| 441 ScopedCurrentChannel channel(version_info::Channel::DEV); |
| 442 |
| 443 scoped_refptr<device::MockUsbDevice> device = |
| 444 CreateTestUsbDevice(0x02ad, 0x138c, 0, {2, 3}); |
| 445 std::unique_ptr<UsbDevicePermission::CheckParam> param = |
| 446 UsbDevicePermission::CheckParam::ForUsbDevice(app.get(), device.get()); |
| 447 |
| 448 EXPECT_FALSE(permission_data.Check(param.get())); |
| 449 } |
| 450 |
| 451 { |
| 452 std::unique_ptr<base::AutoReset<FeatureSessionType>> scoped_session_type_( |
| 453 ScopedCurrentFeatureSessionType(FeatureSessionType::KIOSK)); |
| 454 ScopedCurrentChannel channel(version_info::Channel::DEV); |
| 455 |
| 456 scoped_refptr<device::MockUsbDevice> device = |
| 457 CreateTestUsbDevice(0x02ad, 0x138c, 0, {2, 3}); |
| 458 device->ActiveConfigurationChanged(kUsbConfigWithInterfaces); |
| 459 std::unique_ptr<UsbDevicePermission::CheckParam> param = |
| 460 UsbDevicePermission::CheckParam::ForUsbDevice(app.get(), device.get()); |
| 461 |
| 462 EXPECT_TRUE(permission_data.Check(param.get())); |
| 463 } |
| 464 |
| 465 { |
| 466 std::unique_ptr<base::AutoReset<FeatureSessionType>> scoped_session_type_( |
| 467 ScopedCurrentFeatureSessionType(FeatureSessionType::KIOSK)); |
| 468 ScopedCurrentChannel channel(version_info::Channel::DEV); |
| 469 |
| 470 scoped_refptr<device::MockUsbDevice> device = |
| 471 CreateTestUsbDevice(0x02ad, 0x138c, 0, {2, 3}); |
| 472 device->ActiveConfigurationChanged(kUsbConfigWithoutInterfaces); |
| 473 std::unique_ptr<UsbDevicePermission::CheckParam> param = |
| 474 UsbDevicePermission::CheckParam::ForUsbDevice(app.get(), device.get()); |
| 475 |
| 476 EXPECT_FALSE(permission_data.Check(param.get())); |
| 477 } |
| 478 |
| 479 { |
| 480 std::unique_ptr<base::AutoReset<FeatureSessionType>> scoped_session_type_( |
| 481 ScopedCurrentFeatureSessionType(FeatureSessionType::KIOSK)); |
| 482 ScopedCurrentChannel channel(version_info::Channel::DEV); |
| 483 |
| 484 scoped_refptr<device::MockUsbDevice> device = |
| 485 CreateTestUsbDevice(0x02ad, 0x138c, 0, {4, 5}); |
| 486 std::unique_ptr<UsbDevicePermission::CheckParam> param = |
| 487 UsbDevicePermission::CheckParam::ForUsbDevice(app.get(), device.get()); |
| 488 |
| 489 EXPECT_FALSE(permission_data.Check(param.get())); |
| 490 } |
| 491 } |
| 492 |
| 493 TEST(USBDevicePermissionTest, CheckDeviceAndInterfaceId) { |
| 494 std::unique_ptr<base::Value> permission_data_value = |
| 495 DictionaryBuilder() |
| 496 .Set("vendorId", 0x02ad) |
| 497 .Set("productId", 0x138c) |
| 498 .Set("interfaceId", 3) |
| 499 .Build(); |
| 500 UsbDevicePermissionData permission_data; |
| 501 EXPECT_TRUE(permission_data.FromValue(permission_data_value.get())); |
| 502 |
| 503 scoped_refptr<const Extension> app = |
| 504 CreateTestApp(std::move(permission_data_value)); |
| 505 |
| 506 { |
| 507 scoped_refptr<device::MockUsbDevice> device = |
| 508 CreateTestUsbDevice(0x02ad, 0x138c, 0, std::vector<uint8_t>()); |
| 509 std::unique_ptr<UsbDevicePermission::CheckParam> param = |
| 510 UsbDevicePermission::CheckParam::ForUsbDeviceAndInterface( |
| 511 app.get(), device.get(), 3); |
| 512 |
| 513 EXPECT_TRUE(permission_data.Check(param.get())); |
| 514 } |
| 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(), 2); |
| 522 |
| 523 EXPECT_FALSE(permission_data.Check(param.get())); |
| 524 } |
| 525 } |
| 526 |
| 527 TEST(USBDevicePermissionTest, |
| 528 CheckDeviceAndInterfaceIDAgainstMissingInterfaceId) { |
| 529 std::unique_ptr<base::Value> permission_data_value = |
| 530 DictionaryBuilder() |
| 531 .Set("vendorId", 0x02ad) |
| 532 .Set("productId", 0x138c) |
| 533 .Build(); |
| 534 UsbDevicePermissionData permission_data; |
| 535 EXPECT_TRUE(permission_data.FromValue(permission_data_value.get())); |
| 536 |
| 537 scoped_refptr<const Extension> app = |
| 538 CreateTestApp(std::move(permission_data_value)); |
| 539 |
| 540 { |
| 541 scoped_refptr<device::MockUsbDevice> device = |
| 542 CreateTestUsbDevice(0x02ad, 0x138c, 0, std::vector<uint8_t>()); |
| 543 std::unique_ptr<UsbDevicePermission::CheckParam> param = |
| 544 UsbDevicePermission::CheckParam::ForUsbDeviceAndInterface( |
| 545 app.get(), device.get(), 3); |
| 546 |
| 547 EXPECT_FALSE(permission_data.Check(param.get())); |
| 548 } |
| 549 } |
| 550 |
| 551 TEST(USBDevicePermissionTest, InvalidPermission_NoVendorId) { |
| 552 std::unique_ptr<base::Value> permission_data_value = |
| 553 DictionaryBuilder() |
| 554 .Set("productId", 0x138c) |
| 555 .Set("interfaceClass", 3) |
| 556 .Build(); |
| 557 UsbDevicePermissionData permission_data; |
| 558 ASSERT_FALSE(permission_data.FromValue(permission_data_value.get())); |
| 559 } |
| 560 |
| 561 TEST(USBDevicePermissionTest, InvalidPermission_OnlyVendorId) { |
| 562 std::unique_ptr<base::Value> permission_data_value = |
| 563 DictionaryBuilder().Set("vendorId", 0x02ad).Build(); |
| 564 UsbDevicePermissionData permission_data; |
| 565 ASSERT_FALSE(permission_data.FromValue(permission_data_value.get())); |
| 566 } |
| 567 |
| 568 TEST(USBDevicePermissionTest, InvalidPermission_NoProductIdWithInterfaceId) { |
| 569 std::unique_ptr<base::Value> permission_data_value = |
| 570 DictionaryBuilder().Set("vendorId", 0x02ad).Set("interfaceId", 3).Build(); |
| 571 UsbDevicePermissionData permission_data; |
| 572 ASSERT_FALSE(permission_data.FromValue(permission_data_value.get())); |
| 573 } |
| 574 |
| 575 TEST(USBDevicePermissionTest, RejectInterfaceIdIfInterfaceClassPresent) { |
| 576 std::unique_ptr<base::Value> permission_data_value = |
| 577 DictionaryBuilder() |
| 578 .Set("vendorId", 0x02ad) |
| 579 .Set("productId", 0x128c) |
| 580 .Set("interfaceId", 3) |
| 581 .Set("interfaceClass", 7) |
| 582 .Build(); |
| 583 UsbDevicePermissionData permission_data; |
| 584 ASSERT_FALSE(permission_data.FromValue(permission_data_value.get())); |
| 17 } | 585 } |
| 18 | 586 |
| 19 } // namespace extensions | 587 } // namespace extensions |
| OLD | NEW |