| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "device/usb/mojo/device_impl.h" | 5 #include "device/usb/mojo/device_impl.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <map> | 10 #include <map> |
| 11 #include <numeric> | 11 #include <numeric> |
| 12 #include <queue> | 12 #include <queue> |
| 13 #include <set> | 13 #include <set> |
| 14 #include <string> | 14 #include <string> |
| 15 #include <utility> | 15 #include <utility> |
| 16 #include <vector> | 16 #include <vector> |
| 17 | 17 |
| 18 #include "base/bind.h" | 18 #include "base/bind.h" |
| 19 #include "base/macros.h" | 19 #include "base/macros.h" |
| 20 #include "base/message_loop/message_loop.h" | 20 #include "base/message_loop/message_loop.h" |
| 21 #include "base/run_loop.h" | 21 #include "base/run_loop.h" |
| 22 #include "base/stl_util.h" | 22 #include "base/stl_util.h" |
| 23 #include "device/usb/mock_usb_device.h" | 23 #include "device/usb/mock_usb_device.h" |
| 24 #include "device/usb/mock_usb_device_handle.h" | 24 #include "device/usb/mock_usb_device_handle.h" |
| 25 #include "device/usb/mojo/fake_permission_provider.h" | 25 #include "device/usb/mojo/mock_permission_provider.h" |
| 26 #include "device/usb/mojo/type_converters.h" | 26 #include "device/usb/mojo/type_converters.h" |
| 27 #include "mojo/public/cpp/bindings/interface_request.h" | 27 #include "mojo/public/cpp/bindings/interface_request.h" |
| 28 #include "net/base/io_buffer.h" | 28 #include "net/base/io_buffer.h" |
| 29 #include "testing/gtest/include/gtest/gtest.h" | 29 #include "testing/gtest/include/gtest/gtest.h" |
| 30 | 30 |
| 31 using ::testing::Invoke; | 31 using ::testing::Invoke; |
| 32 using ::testing::_; | 32 using ::testing::_; |
| 33 | 33 |
| 34 namespace device { | 34 namespace device { |
| 35 namespace usb { | 35 namespace usb { |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 145 public: | 145 public: |
| 146 USBDeviceImplTest() | 146 USBDeviceImplTest() |
| 147 : message_loop_(new base::MessageLoop), | 147 : message_loop_(new base::MessageLoop), |
| 148 is_device_open_(false), | 148 is_device_open_(false), |
| 149 allow_reset_(false), | 149 allow_reset_(false), |
| 150 current_config_(0) {} | 150 current_config_(0) {} |
| 151 | 151 |
| 152 ~USBDeviceImplTest() override {} | 152 ~USBDeviceImplTest() override {} |
| 153 | 153 |
| 154 protected: | 154 protected: |
| 155 MockPermissionProvider& permission_provider() { return permission_provider_; } |
| 155 MockUsbDevice& mock_device() { return *mock_device_.get(); } | 156 MockUsbDevice& mock_device() { return *mock_device_.get(); } |
| 156 bool is_device_open() const { return is_device_open_; } | 157 bool is_device_open() const { return is_device_open_; } |
| 157 MockUsbDeviceHandle& mock_handle() { return *mock_handle_.get(); } | 158 MockUsbDeviceHandle& mock_handle() { return *mock_handle_.get(); } |
| 158 | 159 |
| 159 void set_allow_reset(bool allow_reset) { allow_reset_ = allow_reset; } | 160 void set_allow_reset(bool allow_reset) { allow_reset_ = allow_reset; } |
| 160 | 161 |
| 161 // Creates a mock device and binds a Device proxy to a Device service impl | 162 // Creates a mock device and binds a Device proxy to a Device service impl |
| 162 // wrapping the mock device. | 163 // wrapping the mock device. |
| 163 DevicePtr GetMockDeviceProxy(uint16_t vendor_id, | 164 DevicePtr GetMockDeviceProxy(uint16_t vendor_id, |
| 164 uint16_t product_id, | 165 uint16_t product_id, |
| (...skipping 269 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 434 | 435 |
| 435 std::queue<std::vector<uint8_t>> mock_inbound_data_; | 436 std::queue<std::vector<uint8_t>> mock_inbound_data_; |
| 436 std::queue<std::vector<uint8_t>> mock_outbound_data_; | 437 std::queue<std::vector<uint8_t>> mock_outbound_data_; |
| 437 std::queue<std::vector<UsbDeviceHandle::IsochronousPacket>> | 438 std::queue<std::vector<UsbDeviceHandle::IsochronousPacket>> |
| 438 mock_inbound_packets_; | 439 mock_inbound_packets_; |
| 439 std::queue<std::vector<UsbDeviceHandle::IsochronousPacket>> | 440 std::queue<std::vector<UsbDeviceHandle::IsochronousPacket>> |
| 440 mock_outbound_packets_; | 441 mock_outbound_packets_; |
| 441 | 442 |
| 442 std::set<uint8_t> claimed_interfaces_; | 443 std::set<uint8_t> claimed_interfaces_; |
| 443 | 444 |
| 444 FakePermissionProvider permission_provider_; | 445 MockPermissionProvider permission_provider_; |
| 445 | 446 |
| 446 DISALLOW_COPY_AND_ASSIGN(USBDeviceImplTest); | 447 DISALLOW_COPY_AND_ASSIGN(USBDeviceImplTest); |
| 447 }; | 448 }; |
| 448 | 449 |
| 449 } // namespace | 450 } // namespace |
| 450 | 451 |
| 451 TEST_F(USBDeviceImplTest, Disconnect) { | 452 TEST_F(USBDeviceImplTest, Disconnect) { |
| 452 DevicePtr device = GetMockDeviceProxy(); | 453 DevicePtr device = GetMockDeviceProxy(); |
| 453 | 454 |
| 454 base::RunLoop loop; | 455 base::RunLoop loop; |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 525 EXPECT_CALL(mock_device(), Open(_)); | 526 EXPECT_CALL(mock_device(), Open(_)); |
| 526 | 527 |
| 527 { | 528 { |
| 528 base::RunLoop loop; | 529 base::RunLoop loop; |
| 529 device->Open(base::Bind(&ExpectOpenAndThen, OpenDeviceError::OK, | 530 device->Open(base::Bind(&ExpectOpenAndThen, OpenDeviceError::OK, |
| 530 loop.QuitClosure())); | 531 loop.QuitClosure())); |
| 531 loop.Run(); | 532 loop.Run(); |
| 532 } | 533 } |
| 533 | 534 |
| 534 EXPECT_CALL(mock_handle(), SetConfiguration(42, _)); | 535 EXPECT_CALL(mock_handle(), SetConfiguration(42, _)); |
| 536 EXPECT_CALL(permission_provider(), HasConfigurationPermission(42, _)); |
| 535 | 537 |
| 536 { | 538 { |
| 537 // SetConfiguration should fail because 42 is not a valid mock | 539 // SetConfiguration should fail because 42 is not a valid mock |
| 538 // configuration. | 540 // configuration. |
| 539 base::RunLoop loop; | 541 base::RunLoop loop; |
| 540 device->SetConfiguration( | 542 device->SetConfiguration( |
| 541 42, base::Bind(&ExpectResultAndThen, false, loop.QuitClosure())); | 543 42, base::Bind(&ExpectResultAndThen, false, loop.QuitClosure())); |
| 542 loop.Run(); | 544 loop.Run(); |
| 543 } | 545 } |
| 544 | 546 |
| 545 EXPECT_CALL(mock_handle(), Close()); | 547 EXPECT_CALL(mock_handle(), Close()); |
| 546 } | 548 } |
| 547 | 549 |
| 548 TEST_F(USBDeviceImplTest, SetValidConfiguration) { | 550 TEST_F(USBDeviceImplTest, SetValidConfiguration) { |
| 549 DevicePtr device = GetMockDeviceProxy(); | 551 DevicePtr device = GetMockDeviceProxy(); |
| 550 | 552 |
| 551 EXPECT_CALL(mock_device(), Open(_)); | 553 EXPECT_CALL(mock_device(), Open(_)); |
| 552 | 554 |
| 553 { | 555 { |
| 554 base::RunLoop loop; | 556 base::RunLoop loop; |
| 555 device->Open(base::Bind(&ExpectOpenAndThen, OpenDeviceError::OK, | 557 device->Open(base::Bind(&ExpectOpenAndThen, OpenDeviceError::OK, |
| 556 loop.QuitClosure())); | 558 loop.QuitClosure())); |
| 557 loop.Run(); | 559 loop.Run(); |
| 558 } | 560 } |
| 559 | 561 |
| 560 EXPECT_CALL(mock_handle(), SetConfiguration(42, _)); | 562 EXPECT_CALL(mock_handle(), SetConfiguration(42, _)); |
| 563 EXPECT_CALL(permission_provider(), HasConfigurationPermission(42, _)); |
| 561 | 564 |
| 562 AddMockConfig(ConfigBuilder(42)); | 565 AddMockConfig(ConfigBuilder(42)); |
| 563 | 566 |
| 564 { | 567 { |
| 565 // SetConfiguration should succeed because 42 is a valid mock configuration. | 568 // SetConfiguration should succeed because 42 is a valid mock configuration. |
| 566 base::RunLoop loop; | 569 base::RunLoop loop; |
| 567 device->SetConfiguration( | 570 device->SetConfiguration( |
| 568 42, base::Bind(&ExpectResultAndThen, true, loop.QuitClosure())); | 571 42, base::Bind(&ExpectResultAndThen, true, loop.QuitClosure())); |
| 569 loop.Run(); | 572 loop.Run(); |
| 570 } | 573 } |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 618 base::RunLoop loop; | 621 base::RunLoop loop; |
| 619 device->Open(base::Bind(&ExpectOpenAndThen, OpenDeviceError::OK, | 622 device->Open(base::Bind(&ExpectOpenAndThen, OpenDeviceError::OK, |
| 620 loop.QuitClosure())); | 623 loop.QuitClosure())); |
| 621 loop.Run(); | 624 loop.Run(); |
| 622 } | 625 } |
| 623 | 626 |
| 624 // Now add a mock interface #1. | 627 // Now add a mock interface #1. |
| 625 AddMockConfig(ConfigBuilder(1).AddInterface(1, 0, 1, 2, 3)); | 628 AddMockConfig(ConfigBuilder(1).AddInterface(1, 0, 1, 2, 3)); |
| 626 | 629 |
| 627 EXPECT_CALL(mock_handle(), SetConfiguration(1, _)); | 630 EXPECT_CALL(mock_handle(), SetConfiguration(1, _)); |
| 631 EXPECT_CALL(permission_provider(), HasConfigurationPermission(1, _)); |
| 628 | 632 |
| 629 { | 633 { |
| 630 base::RunLoop loop; | 634 base::RunLoop loop; |
| 631 device->SetConfiguration( | 635 device->SetConfiguration( |
| 632 1, base::Bind(&ExpectResultAndThen, true, loop.QuitClosure())); | 636 1, base::Bind(&ExpectResultAndThen, true, loop.QuitClosure())); |
| 633 loop.Run(); | 637 loop.Run(); |
| 634 } | 638 } |
| 635 | 639 |
| 636 EXPECT_CALL(mock_device(), GetActiveConfiguration()); | 640 EXPECT_CALL(mock_device(), GetActiveConfiguration()); |
| 637 EXPECT_CALL(mock_handle(), ClaimInterface(2, _)); | |
| 638 | 641 |
| 639 { | 642 { |
| 640 // Try to claim an invalid interface and expect failure. | 643 // Try to claim an invalid interface and expect failure. |
| 641 base::RunLoop loop; | 644 base::RunLoop loop; |
| 642 device->ClaimInterface( | 645 device->ClaimInterface( |
| 643 2, base::Bind(&ExpectResultAndThen, false, loop.QuitClosure())); | 646 2, base::Bind(&ExpectResultAndThen, false, loop.QuitClosure())); |
| 644 loop.Run(); | 647 loop.Run(); |
| 645 } | 648 } |
| 646 | 649 |
| 647 EXPECT_CALL(mock_device(), GetActiveConfiguration()); | 650 EXPECT_CALL(mock_device(), GetActiveConfiguration()); |
| 648 EXPECT_CALL(mock_handle(), ClaimInterface(1, _)); | 651 EXPECT_CALL(mock_handle(), ClaimInterface(1, _)); |
| 652 EXPECT_CALL(permission_provider(), HasFunctionPermission(1, 1, _)); |
| 649 | 653 |
| 650 { | 654 { |
| 651 base::RunLoop loop; | 655 base::RunLoop loop; |
| 652 device->ClaimInterface( | 656 device->ClaimInterface( |
| 653 1, base::Bind(&ExpectResultAndThen, true, loop.QuitClosure())); | 657 1, base::Bind(&ExpectResultAndThen, true, loop.QuitClosure())); |
| 654 loop.Run(); | 658 loop.Run(); |
| 655 } | 659 } |
| 656 | 660 |
| 657 EXPECT_CALL(mock_handle(), ReleaseInterface(2, _)); | 661 EXPECT_CALL(mock_handle(), ReleaseInterface(2, _)); |
| 658 | 662 |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 724 base::RunLoop loop; | 728 base::RunLoop loop; |
| 725 device->Open(base::Bind(&ExpectOpenAndThen, OpenDeviceError::OK, | 729 device->Open(base::Bind(&ExpectOpenAndThen, OpenDeviceError::OK, |
| 726 loop.QuitClosure())); | 730 loop.QuitClosure())); |
| 727 loop.Run(); | 731 loop.Run(); |
| 728 } | 732 } |
| 729 | 733 |
| 730 AddMockConfig(ConfigBuilder(1).AddInterface(7, 0, 1, 2, 3)); | 734 AddMockConfig(ConfigBuilder(1).AddInterface(7, 0, 1, 2, 3)); |
| 731 | 735 |
| 732 EXPECT_CALL(mock_device(), GetActiveConfiguration()); | 736 EXPECT_CALL(mock_device(), GetActiveConfiguration()); |
| 733 EXPECT_CALL(mock_handle(), SetConfiguration(1, _)); | 737 EXPECT_CALL(mock_handle(), SetConfiguration(1, _)); |
| 738 EXPECT_CALL(permission_provider(), HasConfigurationPermission(1, _)); |
| 734 | 739 |
| 735 { | 740 { |
| 736 base::RunLoop loop; | 741 base::RunLoop loop; |
| 737 device->SetConfiguration( | 742 device->SetConfiguration( |
| 738 1, base::Bind(&ExpectResultAndThen, true, loop.QuitClosure())); | 743 1, base::Bind(&ExpectResultAndThen, true, loop.QuitClosure())); |
| 739 loop.Run(); | 744 loop.Run(); |
| 740 } | 745 } |
| 741 | 746 |
| 742 std::vector<uint8_t> fake_data; | 747 std::vector<uint8_t> fake_data; |
| 743 fake_data.push_back(41); | 748 fake_data.push_back(41); |
| 744 fake_data.push_back(42); | 749 fake_data.push_back(42); |
| 745 fake_data.push_back(43); | 750 fake_data.push_back(43); |
| 746 | 751 |
| 747 AddMockInboundData(fake_data); | 752 AddMockInboundData(fake_data); |
| 748 | 753 |
| 749 EXPECT_CALL(mock_handle(), | 754 EXPECT_CALL(mock_handle(), |
| 750 ControlTransfer(USB_DIRECTION_INBOUND, UsbDeviceHandle::STANDARD, | 755 ControlTransfer(USB_DIRECTION_INBOUND, UsbDeviceHandle::STANDARD, |
| 751 UsbDeviceHandle::DEVICE, 5, 6, 7, _, _, 0, _)); | 756 UsbDeviceHandle::DEVICE, 5, 6, 7, _, _, 0, _)); |
| 757 EXPECT_CALL(permission_provider(), HasConfigurationPermission(1, _)); |
| 752 | 758 |
| 753 { | 759 { |
| 754 auto params = ControlTransferParams::New(); | 760 auto params = ControlTransferParams::New(); |
| 755 params->type = ControlTransferType::STANDARD; | 761 params->type = ControlTransferType::STANDARD; |
| 756 params->recipient = ControlTransferRecipient::DEVICE; | 762 params->recipient = ControlTransferRecipient::DEVICE; |
| 757 params->request = 5; | 763 params->request = 5; |
| 758 params->value = 6; | 764 params->value = 6; |
| 759 params->index = 7; | 765 params->index = 7; |
| 760 base::RunLoop loop; | 766 base::RunLoop loop; |
| 761 device->ControlTransferIn( | 767 device->ControlTransferIn( |
| 762 std::move(params), static_cast<uint32_t>(fake_data.size()), 0, | 768 std::move(params), static_cast<uint32_t>(fake_data.size()), 0, |
| 763 base::Bind(&ExpectTransferInAndThen, TransferStatus::COMPLETED, | 769 base::Bind(&ExpectTransferInAndThen, TransferStatus::COMPLETED, |
| 764 fake_data, loop.QuitClosure())); | 770 fake_data, loop.QuitClosure())); |
| 765 loop.Run(); | 771 loop.Run(); |
| 766 } | 772 } |
| 767 | 773 |
| 768 AddMockOutboundData(fake_data); | 774 AddMockOutboundData(fake_data); |
| 769 | 775 |
| 770 EXPECT_CALL(mock_device(), GetActiveConfiguration()); | 776 EXPECT_CALL(mock_device(), GetActiveConfiguration()); |
| 771 EXPECT_CALL(mock_handle(), | 777 EXPECT_CALL(mock_handle(), |
| 772 ControlTransfer(USB_DIRECTION_OUTBOUND, UsbDeviceHandle::STANDARD, | 778 ControlTransfer(USB_DIRECTION_OUTBOUND, UsbDeviceHandle::STANDARD, |
| 773 UsbDeviceHandle::INTERFACE, 5, 6, 7, _, _, 0, _)); | 779 UsbDeviceHandle::INTERFACE, 5, 6, 7, _, _, 0, _)); |
| 780 EXPECT_CALL(permission_provider(), HasFunctionPermission(7, 1, _)); |
| 774 | 781 |
| 775 { | 782 { |
| 776 auto params = ControlTransferParams::New(); | 783 auto params = ControlTransferParams::New(); |
| 777 params->type = ControlTransferType::STANDARD; | 784 params->type = ControlTransferType::STANDARD; |
| 778 params->recipient = ControlTransferRecipient::INTERFACE; | 785 params->recipient = ControlTransferRecipient::INTERFACE; |
| 779 params->request = 5; | 786 params->request = 5; |
| 780 params->value = 6; | 787 params->value = 6; |
| 781 params->index = 7; | 788 params->index = 7; |
| 782 base::RunLoop loop; | 789 base::RunLoop loop; |
| 783 device->ControlTransferOut( | 790 device->ControlTransferOut( |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 900 base::Bind(&ExpectPacketsInAndThen, fake_inbound_data, | 907 base::Bind(&ExpectPacketsInAndThen, fake_inbound_data, |
| 901 expected_transferred_lengths, loop.QuitClosure())); | 908 expected_transferred_lengths, loop.QuitClosure())); |
| 902 loop.Run(); | 909 loop.Run(); |
| 903 } | 910 } |
| 904 | 911 |
| 905 EXPECT_CALL(mock_handle(), Close()); | 912 EXPECT_CALL(mock_handle(), Close()); |
| 906 } | 913 } |
| 907 | 914 |
| 908 } // namespace usb | 915 } // namespace usb |
| 909 } // namespace device | 916 } // namespace device |
| OLD | NEW |