1// Copyright 2014 The Chromium Authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5#include <vector> 6 7#include "base/memory/ref_counted.h" 8#include "device/usb/usb_descriptors.h" 9#include "device/usb/usb_device.h" 10#include "device/usb/usb_device_filter.h" 11#include "device/usb/usb_device_handle.h" 12#include "testing/gmock/include/gmock/gmock.h" 13#include "testing/gtest/include/gtest/gtest.h" 14 15namespace device { 16 17namespace { 18 19using testing::NiceMock; 20using testing::ReturnRef; 21 22class MockUsbDevice : public UsbDevice { 23 public: 24 MockUsbDevice(uint16 vendor_id, uint16 product_id, uint32 unique_id) 25 : UsbDevice(vendor_id, product_id, unique_id) {} 26 27 MOCK_METHOD0(Open, scoped_refptr<UsbDeviceHandle>()); 28 MOCK_METHOD1(Close, bool(scoped_refptr<UsbDeviceHandle>)); 29#if defined(OS_CHROMEOS) 30 MOCK_METHOD2(RequestUsbAccess, void(int, const base::Callback<void(bool)>&)); 31#endif 32 MOCK_METHOD0(GetConfiguration, const UsbConfigDescriptor&()); 33 MOCK_METHOD1(GetManufacturer, bool(base::string16*)); 34 MOCK_METHOD1(GetProduct, bool(base::string16*)); 35 MOCK_METHOD1(GetSerialNumber, bool(base::string16*)); 36 37 private: 38 virtual ~MockUsbDevice() {} 39}; 40 41class UsbFilterTest : public testing::Test { 42 public: 43 virtual void SetUp() OVERRIDE { 44 UsbInterfaceDescriptor interface; 45 interface.interface_number = 1; 46 interface.alternate_setting = 0; 47 interface.interface_class = 0xFF; 48 interface.interface_subclass = 0x42; 49 interface.interface_protocol = 0x01; 50 config_.interfaces.push_back(interface); 51 52 android_phone_ = new MockUsbDevice(0x18d1, 0x4ee2, 0); 53 ON_CALL(*android_phone_.get(), GetConfiguration()) 54 .WillByDefault(ReturnRef(config_)); 55 } 56 57 protected: 58 UsbConfigDescriptor config_; 59 scoped_refptr<MockUsbDevice> android_phone_; 60}; 61 62TEST_F(UsbFilterTest, MatchAny) { 63 UsbDeviceFilter filter; 64 ASSERT_TRUE(filter.Matches(android_phone_)); 65} 66 67TEST_F(UsbFilterTest, MatchVendorId) { 68 UsbDeviceFilter filter; 69 filter.SetVendorId(0x18d1); 70 ASSERT_TRUE(filter.Matches(android_phone_)); 71} 72 73TEST_F(UsbFilterTest, MatchVendorIdNegative) { 74 UsbDeviceFilter filter; 75 filter.SetVendorId(0x1d6b); 76 ASSERT_FALSE(filter.Matches(android_phone_)); 77} 78 79TEST_F(UsbFilterTest, MatchProductId) { 80 UsbDeviceFilter filter; 81 filter.SetVendorId(0x18d1); 82 filter.SetProductId(0x4ee2); 83 ASSERT_TRUE(filter.Matches(android_phone_)); 84} 85 86TEST_F(UsbFilterTest, MatchProductIdNegative) { 87 UsbDeviceFilter filter; 88 filter.SetVendorId(0x18d1); 89 filter.SetProductId(0x4ee1); 90 ASSERT_FALSE(filter.Matches(android_phone_)); 91} 92 93TEST_F(UsbFilterTest, MatchInterfaceClass) { 94 UsbDeviceFilter filter; 95 filter.SetInterfaceClass(0xff); 96 ASSERT_TRUE(filter.Matches(android_phone_)); 97} 98 99TEST_F(UsbFilterTest, MatchInterfaceClassNegative) { 100 UsbDeviceFilter filter; 101 filter.SetInterfaceClass(0xe0); 102 ASSERT_FALSE(filter.Matches(android_phone_)); 103} 104 105TEST_F(UsbFilterTest, MatchInterfaceSubclass) { 106 UsbDeviceFilter filter; 107 filter.SetInterfaceClass(0xff); 108 filter.SetInterfaceSubclass(0x42); 109 ASSERT_TRUE(filter.Matches(android_phone_)); 110} 111 112TEST_F(UsbFilterTest, MatchInterfaceSubclassNegative) { 113 UsbDeviceFilter filter; 114 filter.SetInterfaceClass(0xff); 115 filter.SetInterfaceSubclass(0x01); 116 ASSERT_FALSE(filter.Matches(android_phone_)); 117} 118 119TEST_F(UsbFilterTest, MatchInterfaceProtocol) { 120 UsbDeviceFilter filter; 121 filter.SetInterfaceClass(0xff); 122 filter.SetInterfaceSubclass(0x42); 123 filter.SetInterfaceProtocol(0x01); 124 ASSERT_TRUE(filter.Matches(android_phone_)); 125} 126 127TEST_F(UsbFilterTest, MatchInterfaceProtocolNegative) { 128 UsbDeviceFilter filter; 129 filter.SetInterfaceClass(0xff); 130 filter.SetInterfaceSubclass(0x42); 131 filter.SetInterfaceProtocol(0x02); 132 ASSERT_FALSE(filter.Matches(android_phone_)); 133} 134 135TEST_F(UsbFilterTest, MatchAnyEmptyListNegative) { 136 std::vector<UsbDeviceFilter> filters; 137 ASSERT_FALSE(UsbDeviceFilter::MatchesAny(android_phone_, filters)); 138} 139 140TEST_F(UsbFilterTest, MatchesAnyVendorId) { 141 std::vector<UsbDeviceFilter> filters(1); 142 filters.back().SetVendorId(0x18d1); 143 ASSERT_TRUE(UsbDeviceFilter::MatchesAny(android_phone_, filters)); 144} 145 146TEST_F(UsbFilterTest, MatchesAnyVendorIdNegative) { 147 std::vector<UsbDeviceFilter> filters(1); 148 filters.back().SetVendorId(0x1d6b); 149 ASSERT_FALSE(UsbDeviceFilter::MatchesAny(android_phone_, filters)); 150} 151 152} // namespace 153 154} // namespace device 155