Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(95)

Side by Side Diff: device/devices_app/usb/device_impl_unittest.cc

Issue 1369643002: Add configuration and interface permission checks to DeviceImpl. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add new method ot Android USB mocks. Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 <map> 5 #include <map>
6 #include <queue> 6 #include <queue>
7 #include <set> 7 #include <set>
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
11 #include "base/macros.h" 11 #include "base/macros.h"
12 #include "base/message_loop/message_loop.h" 12 #include "base/message_loop/message_loop.h"
13 #include "base/run_loop.h" 13 #include "base/run_loop.h"
14 #include "base/stl_util.h" 14 #include "base/stl_util.h"
15 #include "device/devices_app/usb/device_impl.h" 15 #include "device/devices_app/usb/device_impl.h"
16 #include "device/devices_app/usb/fake_permission_provider.h"
16 #include "device/usb/mock_usb_device.h" 17 #include "device/usb/mock_usb_device.h"
17 #include "device/usb/mock_usb_device_handle.h" 18 #include "device/usb/mock_usb_device_handle.h"
18 #include "testing/gtest/include/gtest/gtest.h" 19 #include "testing/gtest/include/gtest/gtest.h"
19 #include "third_party/mojo/src/mojo/public/cpp/bindings/interface_request.h" 20 #include "third_party/mojo/src/mojo/public/cpp/bindings/interface_request.h"
20 21
21 using ::testing::Invoke; 22 using ::testing::Invoke;
22 using ::testing::_; 23 using ::testing::_;
23 24
24 namespace device { 25 namespace device {
25 namespace usb { 26 namespace usb {
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after
143 // wrapping the mock device. 144 // wrapping the mock device.
144 DevicePtr GetMockDeviceProxy(uint16_t vendor_id, 145 DevicePtr GetMockDeviceProxy(uint16_t vendor_id,
145 uint16_t product_id, 146 uint16_t product_id,
146 const std::string& manufacturer, 147 const std::string& manufacturer,
147 const std::string& product, 148 const std::string& product,
148 const std::string& serial) { 149 const std::string& serial) {
149 mock_device_ = 150 mock_device_ =
150 new MockUsbDevice(vendor_id, product_id, manufacturer, product, serial); 151 new MockUsbDevice(vendor_id, product_id, manufacturer, product, serial);
151 mock_handle_ = new MockUsbDeviceHandle(mock_device_.get()); 152 mock_handle_ = new MockUsbDeviceHandle(mock_device_.get());
152 153
154 PermissionProviderPtr permission_provider;
155 permission_provider_.Bind(mojo::GetProxy(&permission_provider));
153 DevicePtr proxy; 156 DevicePtr proxy;
154 new DeviceImpl(mock_device_, mojo::GetProxy(&proxy)); 157 new DeviceImpl(mock_device_, permission_provider.Pass(),
158 mojo::GetProxy(&proxy));
155 159
156 // Set up mock handle calls to respond based on mock device configs 160 // Set up mock handle calls to respond based on mock device configs
157 // established by the test. 161 // established by the test.
158 ON_CALL(mock_device(), Open(_)) 162 ON_CALL(mock_device(), Open(_))
159 .WillByDefault(Invoke(this, &USBDeviceImplTest::OpenMockHandle)); 163 .WillByDefault(Invoke(this, &USBDeviceImplTest::OpenMockHandle));
164 ON_CALL(mock_device(), GetActiveConfiguration())
165 .WillByDefault(
166 Invoke(this, &USBDeviceImplTest::GetActiveConfiguration));
160 ON_CALL(mock_handle(), Close()) 167 ON_CALL(mock_handle(), Close())
161 .WillByDefault(Invoke(this, &USBDeviceImplTest::CloseMockHandle)); 168 .WillByDefault(Invoke(this, &USBDeviceImplTest::CloseMockHandle));
162 ON_CALL(mock_handle(), SetConfiguration(_, _)) 169 ON_CALL(mock_handle(), SetConfiguration(_, _))
163 .WillByDefault(Invoke(this, &USBDeviceImplTest::SetConfiguration)); 170 .WillByDefault(Invoke(this, &USBDeviceImplTest::SetConfiguration));
164 ON_CALL(mock_handle(), ClaimInterface(_, _)) 171 ON_CALL(mock_handle(), ClaimInterface(_, _))
165 .WillByDefault(Invoke(this, &USBDeviceImplTest::ClaimInterface)); 172 .WillByDefault(Invoke(this, &USBDeviceImplTest::ClaimInterface));
166 ON_CALL(mock_handle(), ReleaseInterface(_)) 173 ON_CALL(mock_handle(), ReleaseInterface(_))
167 .WillByDefault(Invoke(this, &USBDeviceImplTest::ReleaseInterface)); 174 .WillByDefault(Invoke(this, &USBDeviceImplTest::ReleaseInterface));
168 ON_CALL(mock_handle(), SetInterfaceAlternateSetting(_, _, _)) 175 ON_CALL(mock_handle(), SetInterfaceAlternateSetting(_, _, _))
169 .WillByDefault( 176 .WillByDefault(
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
203 EXPECT_FALSE(is_device_open_); 210 EXPECT_FALSE(is_device_open_);
204 is_device_open_ = true; 211 is_device_open_ = true;
205 callback.Run(mock_handle_); 212 callback.Run(mock_handle_);
206 } 213 }
207 214
208 void CloseMockHandle() { 215 void CloseMockHandle() {
209 EXPECT_TRUE(is_device_open_); 216 EXPECT_TRUE(is_device_open_);
210 is_device_open_ = false; 217 is_device_open_ = false;
211 } 218 }
212 219
220 const UsbConfigDescriptor* GetActiveConfiguration() {
221 if (current_config_ == 0) {
222 return nullptr;
223 } else {
224 const auto it = mock_configs_.find(current_config_);
225 EXPECT_TRUE(it != mock_configs_.end());
226 return &it->second;
227 }
228 }
229
213 void SetConfiguration(uint8_t value, 230 void SetConfiguration(uint8_t value,
214 const UsbDeviceHandle::ResultCallback& callback) { 231 const UsbDeviceHandle::ResultCallback& callback) {
215 if (mock_configs_.find(value) != mock_configs_.end()) { 232 if (mock_configs_.find(value) != mock_configs_.end()) {
216 current_config_ = value; 233 current_config_ = value;
217 callback.Run(true); 234 callback.Run(true);
218 } else { 235 } else {
219 callback.Run(false); 236 callback.Run(false);
220 } 237 }
221 } 238 }
222 239
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after
335 bool allow_reset_; 352 bool allow_reset_;
336 353
337 std::map<uint8_t, UsbConfigDescriptor> mock_configs_; 354 std::map<uint8_t, UsbConfigDescriptor> mock_configs_;
338 uint8_t current_config_; 355 uint8_t current_config_;
339 356
340 std::queue<std::vector<uint8_t>> mock_inbound_data_; 357 std::queue<std::vector<uint8_t>> mock_inbound_data_;
341 std::queue<std::vector<uint8_t>> mock_outbound_data_; 358 std::queue<std::vector<uint8_t>> mock_outbound_data_;
342 359
343 std::set<uint8_t> claimed_interfaces_; 360 std::set<uint8_t> claimed_interfaces_;
344 361
362 FakePermissionProvider permission_provider_;
363
345 DISALLOW_COPY_AND_ASSIGN(USBDeviceImplTest); 364 DISALLOW_COPY_AND_ASSIGN(USBDeviceImplTest);
346 }; 365 };
347 366
348 } // namespace 367 } // namespace
349 368
350 TEST_F(USBDeviceImplTest, Open) { 369 TEST_F(USBDeviceImplTest, Open) {
351 DevicePtr device = GetMockDeviceProxy(); 370 DevicePtr device = GetMockDeviceProxy();
352 371
353 EXPECT_FALSE(is_device_open()); 372 EXPECT_FALSE(is_device_open());
354 373
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after
496 EXPECT_CALL(mock_device(), Open(_)); 515 EXPECT_CALL(mock_device(), Open(_));
497 516
498 { 517 {
499 base::RunLoop loop; 518 base::RunLoop loop;
500 device->Open(base::Bind(&ExpectOpenAndThen, OPEN_DEVICE_ERROR_OK, 519 device->Open(base::Bind(&ExpectOpenAndThen, OPEN_DEVICE_ERROR_OK,
501 loop.QuitClosure())); 520 loop.QuitClosure()));
502 loop.Run(); 521 loop.Run();
503 } 522 }
504 523
505 // Now add a mock interface #1. 524 // Now add a mock interface #1.
506 AddMockConfig(ConfigBuilder(0).AddInterface(1, 0, 1, 2, 3)); 525 AddMockConfig(ConfigBuilder(1).AddInterface(1, 0, 1, 2, 3));
507 526
527 EXPECT_CALL(mock_handle(), SetConfiguration(1, _));
528
529 {
530 base::RunLoop loop;
531 device->SetConfiguration(
532 1, base::Bind(&ExpectResultAndThen, true, loop.QuitClosure()));
533 loop.Run();
534 }
535
536 EXPECT_CALL(mock_device(), GetActiveConfiguration());
508 EXPECT_CALL(mock_handle(), ClaimInterface(2, _)); 537 EXPECT_CALL(mock_handle(), ClaimInterface(2, _));
509 538
510 { 539 {
511 // Try to claim an invalid interface and expect failure. 540 // Try to claim an invalid interface and expect failure.
512 base::RunLoop loop; 541 base::RunLoop loop;
513 device->ClaimInterface( 542 device->ClaimInterface(
514 2, base::Bind(&ExpectResultAndThen, false, loop.QuitClosure())); 543 2, base::Bind(&ExpectResultAndThen, false, loop.QuitClosure()));
515 loop.Run(); 544 loop.Run();
516 } 545 }
517 546
547 EXPECT_CALL(mock_device(), GetActiveConfiguration());
518 EXPECT_CALL(mock_handle(), ClaimInterface(1, _)); 548 EXPECT_CALL(mock_handle(), ClaimInterface(1, _));
519 549
520 { 550 {
521 base::RunLoop loop; 551 base::RunLoop loop;
522 device->ClaimInterface( 552 device->ClaimInterface(
523 1, base::Bind(&ExpectResultAndThen, true, loop.QuitClosure())); 553 1, base::Bind(&ExpectResultAndThen, true, loop.QuitClosure()));
524 loop.Run(); 554 loop.Run();
525 } 555 }
526 556
527 EXPECT_CALL(mock_handle(), ReleaseInterface(2)); 557 EXPECT_CALL(mock_handle(), ReleaseInterface(2));
(...skipping 24 matching lines...) Expand all
552 582
553 EXPECT_CALL(mock_device(), Open(_)); 583 EXPECT_CALL(mock_device(), Open(_));
554 584
555 { 585 {
556 base::RunLoop loop; 586 base::RunLoop loop;
557 device->Open(base::Bind(&ExpectOpenAndThen, OPEN_DEVICE_ERROR_OK, 587 device->Open(base::Bind(&ExpectOpenAndThen, OPEN_DEVICE_ERROR_OK,
558 loop.QuitClosure())); 588 loop.QuitClosure()));
559 loop.Run(); 589 loop.Run();
560 } 590 }
561 591
562 AddMockConfig(ConfigBuilder(0) 592 AddMockConfig(ConfigBuilder(1)
563 .AddInterface(1, 0, 1, 2, 3) 593 .AddInterface(1, 0, 1, 2, 3)
564 .AddInterface(1, 42, 1, 2, 3) 594 .AddInterface(1, 42, 1, 2, 3)
565 .AddInterface(2, 0, 1, 2, 3)); 595 .AddInterface(2, 0, 1, 2, 3));
566 596
567 EXPECT_CALL(mock_handle(), SetInterfaceAlternateSetting(1, 42, _)); 597 EXPECT_CALL(mock_handle(), SetInterfaceAlternateSetting(1, 42, _));
568 598
569 { 599 {
570 base::RunLoop loop; 600 base::RunLoop loop;
571 device->SetInterfaceAlternateSetting( 601 device->SetInterfaceAlternateSetting(
572 1, 42, base::Bind(&ExpectResultAndThen, true, loop.QuitClosure())); 602 1, 42, base::Bind(&ExpectResultAndThen, true, loop.QuitClosure()));
(...skipping 17 matching lines...) Expand all
590 620
591 EXPECT_CALL(mock_device(), Open(_)); 621 EXPECT_CALL(mock_device(), Open(_));
592 622
593 { 623 {
594 base::RunLoop loop; 624 base::RunLoop loop;
595 device->Open(base::Bind(&ExpectOpenAndThen, OPEN_DEVICE_ERROR_OK, 625 device->Open(base::Bind(&ExpectOpenAndThen, OPEN_DEVICE_ERROR_OK,
596 loop.QuitClosure())); 626 loop.QuitClosure()));
597 loop.Run(); 627 loop.Run();
598 } 628 }
599 629
630 AddMockConfig(ConfigBuilder(1).AddInterface(7, 0, 1, 2, 3));
631
632 EXPECT_CALL(mock_device(), GetActiveConfiguration());
633 EXPECT_CALL(mock_handle(), SetConfiguration(1, _));
634
635 {
636 base::RunLoop loop;
637 device->SetConfiguration(
638 1, base::Bind(&ExpectResultAndThen, true, loop.QuitClosure()));
639 loop.Run();
640 }
641
600 std::vector<uint8_t> fake_data; 642 std::vector<uint8_t> fake_data;
601 fake_data.push_back(41); 643 fake_data.push_back(41);
602 fake_data.push_back(42); 644 fake_data.push_back(42);
603 fake_data.push_back(43); 645 fake_data.push_back(43);
604 646
605 AddMockInboundData(fake_data); 647 AddMockInboundData(fake_data);
606 648
607 EXPECT_CALL(mock_handle(), 649 EXPECT_CALL(mock_handle(),
608 ControlTransfer(USB_DIRECTION_INBOUND, UsbDeviceHandle::STANDARD, 650 ControlTransfer(USB_DIRECTION_INBOUND, UsbDeviceHandle::STANDARD,
609 UsbDeviceHandle::DEVICE, 5, 6, 7, _, _, 0, _)); 651 UsbDeviceHandle::DEVICE, 5, 6, 7, _, _, 0, _));
610 652
611 { 653 {
612 auto params = ControlTransferParams::New(); 654 auto params = ControlTransferParams::New();
613 params->type = CONTROL_TRANSFER_TYPE_STANDARD; 655 params->type = CONTROL_TRANSFER_TYPE_STANDARD;
614 params->recipient = CONTROL_TRANSFER_RECIPIENT_DEVICE; 656 params->recipient = CONTROL_TRANSFER_RECIPIENT_DEVICE;
615 params->request = 5; 657 params->request = 5;
616 params->value = 6; 658 params->value = 6;
617 params->index = 7; 659 params->index = 7;
618 base::RunLoop loop; 660 base::RunLoop loop;
619 device->ControlTransferIn( 661 device->ControlTransferIn(
620 params.Pass(), static_cast<uint32_t>(fake_data.size()), 0, 662 params.Pass(), static_cast<uint32_t>(fake_data.size()), 0,
621 base::Bind(&ExpectTransferInAndThen, TRANSFER_STATUS_COMPLETED, 663 base::Bind(&ExpectTransferInAndThen, TRANSFER_STATUS_COMPLETED,
622 fake_data, loop.QuitClosure())); 664 fake_data, loop.QuitClosure()));
623 loop.Run(); 665 loop.Run();
624 } 666 }
625 667
626 AddMockConfig(ConfigBuilder(0).AddInterface(7, 0, 1, 2, 3));
627 AddMockOutboundData(fake_data); 668 AddMockOutboundData(fake_data);
628 669
670 EXPECT_CALL(mock_device(), GetActiveConfiguration());
629 EXPECT_CALL(mock_handle(), 671 EXPECT_CALL(mock_handle(),
630 ControlTransfer(USB_DIRECTION_OUTBOUND, UsbDeviceHandle::STANDARD, 672 ControlTransfer(USB_DIRECTION_OUTBOUND, UsbDeviceHandle::STANDARD,
631 UsbDeviceHandle::INTERFACE, 5, 6, 7, _, _, 0, _)); 673 UsbDeviceHandle::INTERFACE, 5, 6, 7, _, _, 0, _));
632 674
633 { 675 {
634 auto params = ControlTransferParams::New(); 676 auto params = ControlTransferParams::New();
635 params->type = CONTROL_TRANSFER_TYPE_STANDARD; 677 params->type = CONTROL_TRANSFER_TYPE_STANDARD;
636 params->recipient = CONTROL_TRANSFER_RECIPIENT_INTERFACE; 678 params->recipient = CONTROL_TRANSFER_RECIPIENT_INTERFACE;
637 params->request = 5; 679 params->request = 5;
638 params->value = 6; 680 params->value = 6;
(...skipping 22 matching lines...) Expand all
661 } 703 }
662 704
663 std::string message1 = "say hello please"; 705 std::string message1 = "say hello please";
664 std::vector<uint8_t> fake_outbound_data(message1.size()); 706 std::vector<uint8_t> fake_outbound_data(message1.size());
665 std::copy(message1.begin(), message1.end(), fake_outbound_data.begin()); 707 std::copy(message1.begin(), message1.end(), fake_outbound_data.begin());
666 708
667 std::string message2 = "hello world!"; 709 std::string message2 = "hello world!";
668 std::vector<uint8_t> fake_inbound_data(message2.size()); 710 std::vector<uint8_t> fake_inbound_data(message2.size());
669 std::copy(message2.begin(), message2.end(), fake_inbound_data.begin()); 711 std::copy(message2.begin(), message2.end(), fake_inbound_data.begin());
670 712
671 AddMockConfig(ConfigBuilder(0).AddInterface(7, 0, 1, 2, 3)); 713 AddMockConfig(ConfigBuilder(1).AddInterface(7, 0, 1, 2, 3));
672 AddMockOutboundData(fake_outbound_data); 714 AddMockOutboundData(fake_outbound_data);
673 AddMockInboundData(fake_inbound_data); 715 AddMockInboundData(fake_inbound_data);
674 716
675 EXPECT_CALL(mock_handle(), GenericTransfer(USB_DIRECTION_OUTBOUND, 0x01, _, 717 EXPECT_CALL(mock_handle(), GenericTransfer(USB_DIRECTION_OUTBOUND, 0x01, _,
676 fake_outbound_data.size(), 0, _)); 718 fake_outbound_data.size(), 0, _));
677 719
678 { 720 {
679 base::RunLoop loop; 721 base::RunLoop loop;
680 device->GenericTransferOut( 722 device->GenericTransferOut(
681 1, mojo::Array<uint8_t>::From(fake_outbound_data), 0, 723 1, mojo::Array<uint8_t>::From(fake_outbound_data), 0,
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
714 std::string outbound_packet_data = "aaaaaaaabbbbbbbbccccccccdddddddd"; 756 std::string outbound_packet_data = "aaaaaaaabbbbbbbbccccccccdddddddd";
715 std::vector<uint8_t> fake_outbound_packets(outbound_packet_data.size()); 757 std::vector<uint8_t> fake_outbound_packets(outbound_packet_data.size());
716 std::copy(outbound_packet_data.begin(), outbound_packet_data.end(), 758 std::copy(outbound_packet_data.begin(), outbound_packet_data.end(),
717 fake_outbound_packets.begin()); 759 fake_outbound_packets.begin());
718 760
719 std::string inbound_packet_data = "ddddddddccccccccbbbbbbbbaaaaaaaa"; 761 std::string inbound_packet_data = "ddddddddccccccccbbbbbbbbaaaaaaaa";
720 std::vector<uint8_t> fake_inbound_packets(inbound_packet_data.size()); 762 std::vector<uint8_t> fake_inbound_packets(inbound_packet_data.size());
721 std::copy(inbound_packet_data.begin(), inbound_packet_data.end(), 763 std::copy(inbound_packet_data.begin(), inbound_packet_data.end(),
722 fake_inbound_packets.begin()); 764 fake_inbound_packets.begin());
723 765
724 AddMockConfig(ConfigBuilder(0).AddInterface(7, 0, 1, 2, 3)); 766 AddMockConfig(ConfigBuilder(1).AddInterface(7, 0, 1, 2, 3));
725 AddMockOutboundData(fake_outbound_packets); 767 AddMockOutboundData(fake_outbound_packets);
726 AddMockInboundData(fake_inbound_packets); 768 AddMockInboundData(fake_inbound_packets);
727 769
728 EXPECT_CALL(mock_handle(), 770 EXPECT_CALL(mock_handle(),
729 IsochronousTransfer(USB_DIRECTION_OUTBOUND, 0x01, _, 771 IsochronousTransfer(USB_DIRECTION_OUTBOUND, 0x01, _,
730 fake_outbound_packets.size(), 4, 8, 0, _)); 772 fake_outbound_packets.size(), 4, 8, 0, _));
731 773
732 { 774 {
733 base::RunLoop loop; 775 base::RunLoop loop;
734 mojo::Array<mojo::Array<uint8_t>> packets = 776 mojo::Array<mojo::Array<uint8_t>> packets =
(...skipping 27 matching lines...) Expand all
762 1, 4, 8, 0, base::Bind(&ExpectPacketsAndThen, TRANSFER_STATUS_COMPLETED, 804 1, 4, 8, 0, base::Bind(&ExpectPacketsAndThen, TRANSFER_STATUS_COMPLETED,
763 packets, loop.QuitClosure())); 805 packets, loop.QuitClosure()));
764 loop.Run(); 806 loop.Run();
765 } 807 }
766 808
767 EXPECT_CALL(mock_handle(), Close()); 809 EXPECT_CALL(mock_handle(), Close());
768 } 810 }
769 811
770 } // namespace usb 812 } // namespace usb
771 } // namespace device 813 } // namespace device
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698