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