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 <stdint.h> | 5 #include <stdint.h> |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
9 #include "device/usb/mock_usb_device_handle.h" | 9 #include "device/usb/mock_usb_device_handle.h" |
10 #include "device/usb/usb_descriptors.h" | 10 #include "device/usb/usb_descriptors.h" |
(...skipping 14 matching lines...) Expand all Loading... |
25 void ExpectStringDescriptors( | 25 void ExpectStringDescriptors( |
26 scoped_ptr<std::map<uint8_t, base::string16>> string_map) { | 26 scoped_ptr<std::map<uint8_t, base::string16>> string_map) { |
27 EXPECT_EQ(3u, string_map->size()); | 27 EXPECT_EQ(3u, string_map->size()); |
28 EXPECT_EQ(base::ASCIIToUTF16("String 1"), (*string_map)[1]); | 28 EXPECT_EQ(base::ASCIIToUTF16("String 1"), (*string_map)[1]); |
29 EXPECT_EQ(base::ASCIIToUTF16("String 2"), (*string_map)[2]); | 29 EXPECT_EQ(base::ASCIIToUTF16("String 2"), (*string_map)[2]); |
30 EXPECT_EQ(base::ASCIIToUTF16("String 3"), (*string_map)[3]); | 30 EXPECT_EQ(base::ASCIIToUTF16("String 3"), (*string_map)[3]); |
31 } | 31 } |
32 | 32 |
33 class UsbDescriptorsTest : public ::testing::Test {}; | 33 class UsbDescriptorsTest : public ::testing::Test {}; |
34 | 34 |
35 TEST_F(UsbDescriptorsTest, NoInterfaceAssociations) { | |
36 UsbConfigDescriptor config(1, false, false, 0); | |
37 config.interfaces.emplace_back(0, 0, 255, 255, 255); | |
38 config.interfaces.emplace_back(0, 1, 255, 255, 255); | |
39 config.interfaces.emplace_back(1, 0, 255, 255, 255); | |
40 config.AssignFirstInterfaceNumbers(); | |
41 | |
42 EXPECT_EQ(0, config.interfaces[0].first_interface); | |
43 EXPECT_EQ(0, config.interfaces[1].first_interface); | |
44 EXPECT_EQ(1, config.interfaces[2].first_interface); | |
45 } | |
46 | |
47 TEST_F(UsbDescriptorsTest, InterfaceAssociations) { | |
48 // Links interfaces 0 and 1 into a single function. | |
49 static const uint8_t kIAD1[] = {0x08, 0x0b, 0x00, 0x02, | |
50 0xff, 0xff, 0xff, 0x00}; | |
51 // Only references a single interface, 2. | |
52 static const uint8_t kIAD2[] = {0x08, 0x0b, 0x02, 0x01, | |
53 0xff, 0xff, 0xff, 0x00}; | |
54 // Malformed. References interface 3 but bInterfaceCount is 0. | |
55 static const uint8_t kIAD3[] = {0x08, 0x0b, 0x03, 0x00, | |
56 0xff, 0xff, 0xff, 0x00}; | |
57 // Malformed. References an indefined interface. | |
58 static const uint8_t kIAD4[] = {0x08, 0x0b, 0x07, 0x00, | |
59 0xff, 0xff, 0xff, 0x00}; | |
60 // Links interfaces 4 and 5 into a single function. | |
61 static const uint8_t kIAD5[] = {0x08, 0x0b, 0x04, 0x01, | |
62 0xff, 0xff, 0xff, 0x00}; | |
63 | |
64 UsbConfigDescriptor config(1, false, false, 0); | |
65 config.extra_data.assign(kIAD1, kIAD1 + sizeof(kIAD1)); | |
66 config.extra_data.insert(config.extra_data.end(), kIAD2, | |
67 kIAD2 + sizeof(kIAD2)); | |
68 config.interfaces.emplace_back(0, 0, 255, 255, 255); | |
69 config.interfaces.emplace_back(1, 0, 255, 255, 255); | |
70 UsbInterfaceDescriptor iface1a(1, 1, 255, 255, 255); | |
71 iface1a.extra_data.assign(kIAD3, kIAD3 + sizeof(kIAD3)); | |
72 config.interfaces.push_back(std::move(iface1a)); | |
73 config.interfaces.emplace_back(2, 0, 255, 255, 255); | |
74 config.interfaces.emplace_back(3, 0, 255, 255, 255); | |
75 UsbInterfaceDescriptor iface4(4, 0, 255, 255, 255); | |
76 iface4.extra_data.assign(kIAD4, kIAD4 + sizeof(kIAD4)); | |
77 iface4.extra_data.insert(iface4.extra_data.end(), kIAD5, | |
78 kIAD5 + sizeof(kIAD5)); | |
79 config.interfaces.push_back(std::move(iface4)); | |
80 config.interfaces.emplace_back(5, 0, 255, 255, 255); | |
81 config.AssignFirstInterfaceNumbers(); | |
82 | |
83 EXPECT_EQ(0, config.interfaces[0].first_interface); | |
84 EXPECT_EQ(0, config.interfaces[1].first_interface); | |
85 EXPECT_EQ(0, config.interfaces[2].first_interface); | |
86 EXPECT_EQ(2, config.interfaces[3].first_interface); | |
87 EXPECT_EQ(3, config.interfaces[4].first_interface); | |
88 EXPECT_EQ(4, config.interfaces[5].first_interface); | |
89 EXPECT_EQ(4, config.interfaces[5].first_interface); | |
90 } | |
91 | |
92 TEST_F(UsbDescriptorsTest, CorruptInterfaceAssociations) { | |
93 { | |
94 // Descriptor is too short. | |
95 static const uint8_t kIAD[] = {0x01}; | |
96 UsbConfigDescriptor config(1, false, false, 0); | |
97 config.extra_data.assign(kIAD, kIAD + sizeof(kIAD)); | |
98 config.AssignFirstInterfaceNumbers(); | |
99 } | |
100 { | |
101 // Descriptor is too long. | |
102 static const uint8_t kIAD[] = {0x09, 0x0b, 0x00, 0x00, | |
103 0x00, 0x00, 0x00, 0x00}; | |
104 UsbConfigDescriptor config(1, false, false, 0); | |
105 config.extra_data.assign(kIAD, kIAD + sizeof(kIAD)); | |
106 config.AssignFirstInterfaceNumbers(); | |
107 } | |
108 } | |
109 | |
110 TEST_F(UsbDescriptorsTest, StringDescriptor) { | 35 TEST_F(UsbDescriptorsTest, StringDescriptor) { |
111 static const uint8_t kBuffer[] = {0x1a, 0x03, 'H', 0, 'e', 0, 'l', 0, 'l', 0, | 36 static const uint8_t kBuffer[] = {0x1a, 0x03, 'H', 0, 'e', 0, 'l', 0, 'l', 0, |
112 'o', 0, ' ', 0, 'w', 0, 'o', 0, 'r', 0, | 37 'o', 0, ' ', 0, 'w', 0, 'o', 0, 'r', 0, |
113 'l', 0, 'd', 0, '!', 0}; | 38 'l', 0, 'd', 0, '!', 0}; |
114 base::string16 string; | 39 base::string16 string; |
115 ASSERT_TRUE(ParseUsbStringDescriptor( | 40 ASSERT_TRUE(ParseUsbStringDescriptor( |
116 std::vector<uint8_t>(kBuffer, kBuffer + sizeof(kBuffer)), &string)); | 41 std::vector<uint8_t>(kBuffer, kBuffer + sizeof(kBuffer)), &string)); |
117 EXPECT_EQ(base::ASCIIToUTF16("Hello world!"), string); | 42 EXPECT_EQ(base::ASCIIToUTF16("Hello world!"), string); |
118 } | 43 } |
119 | 44 |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
201 _, _, _)) | 126 _, _, _)) |
202 .WillOnce(InvokeCallback(kStringDescriptor3, sizeof(kStringDescriptor3))); | 127 .WillOnce(InvokeCallback(kStringDescriptor3, sizeof(kStringDescriptor3))); |
203 | 128 |
204 ReadUsbStringDescriptors(device_handle, std::move(string_map), | 129 ReadUsbStringDescriptors(device_handle, std::move(string_map), |
205 base::Bind(&ExpectStringDescriptors)); | 130 base::Bind(&ExpectStringDescriptors)); |
206 } | 131 } |
207 | 132 |
208 } // namespace | 133 } // namespace |
209 | 134 |
210 } // namespace device | 135 } // namespace device |
OLD | NEW |