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 <algorithm> |
| 8 |
| 9 #include "base/bind.h" |
| 10 #include "base/stl_util.h" |
| 11 #include "device/usb/mock_usb_device_handle.h" |
7 #include "device/usb/webusb_descriptors.h" | 12 #include "device/usb/webusb_descriptors.h" |
8 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
9 | 14 |
| 15 using testing::_; |
| 16 |
10 namespace device { | 17 namespace device { |
11 | 18 |
12 namespace { | 19 namespace { |
13 | 20 |
14 const uint8_t kExampleBosDescriptor[] = { | 21 const uint8_t kExampleBosDescriptor[] = { |
15 // BOS descriptor. | 22 // BOS descriptor. |
16 0x05, 0x0F, 0x4C, 0x00, 0x03, | 23 0x05, 0x0F, 0x4C, 0x00, 0x03, |
17 | 24 |
18 // Container ID descriptor. | 25 // Container ID descriptor. |
19 0x14, 0x10, 0x04, 0x00, 0x2A, 0xF9, 0xF6, 0xC2, 0x98, 0x10, 0x2B, 0x49, | 26 0x14, 0x10, 0x04, 0x00, 0x2A, 0xF9, 0xF6, 0xC2, 0x98, 0x10, 0x2B, 0x49, |
20 0x8E, 0x64, 0xFF, 0x01, 0x0C, 0x7F, 0x94, 0xE1, | 27 0x8E, 0x64, 0xFF, 0x01, 0x0C, 0x7F, 0x94, 0xE1, |
21 | 28 |
22 // WebUSB Platform Capability descriptor. | 29 // WebUSB Platform Capability descriptor. |
23 0x17, 0x10, 0x05, 0x00, 0x38, 0xB6, 0x08, 0x34, 0xA9, 0x09, 0xA0, 0x47, | 30 0x18, 0x10, 0x05, 0x00, 0x38, 0xB6, 0x08, 0x34, 0xA9, 0x09, 0xA0, 0x47, |
24 0x8B, 0xFD, 0xA0, 0x76, 0x88, 0x15, 0xB6, 0x65, 0x00, 0x01, 0x42, | 31 0x8B, 0xFD, 0xA0, 0x76, 0x88, 0x15, 0xB6, 0x65, 0x00, 0x01, 0x42, 0x01, |
25 | 32 |
26 // Microsoft OS 2.0 Platform Capability descriptor. | 33 // Microsoft OS 2.0 Platform Capability descriptor. |
27 0x1C, 0x10, 0x05, 0x00, 0xDF, 0x60, 0xDD, 0xD8, 0x89, 0x45, 0xC7, 0x4C, | 34 0x1C, 0x10, 0x05, 0x00, 0xDF, 0x60, 0xDD, 0xD8, 0x89, 0x45, 0xC7, 0x4C, |
28 0x9C, 0xD2, 0x65, 0x9D, 0x9E, 0x64, 0x8A, 0x9F, 0x00, 0x00, 0x03, 0x06, | 35 0x9C, 0xD2, 0x65, 0x9D, 0x9E, 0x64, 0x8A, 0x9F, 0x00, 0x00, 0x03, 0x06, |
29 0x00, 0x00, 0x01, 0x00}; | 36 0x00, 0x00, 0x01, 0x00}; |
30 | 37 |
31 const uint8_t kExampleDescriptorSet[] = { | 38 const uint8_t kExampleAllowedOrigins[] = { |
32 // Descriptor set header. | 39 // Allowed origins header. |
33 0x04, 0x00, 0x9E, 0x00, | 40 0x07, 0x00, 0x12, 0x00, 0x01, 0x01, 0x02, |
34 | |
35 // URL descriptor: https://example.com:80 | |
36 0x18, 0x03, 'h', 't', 't', 'p', 's', ':', '/', '/', 'e', 'x', 'a', 'm', 'p', | |
37 'l', 'e', '.', 'c', 'o', 'm', ':', '8', '0', | |
38 | |
39 // Configuration subset header. { | 41 // Configuration subset header. { |
40 0x05, 0x01, 0x01, 0x6A, 0x00, | 42 0x06, 0x01, 0x01, 0x01, 0x03, 0x04, |
41 | |
42 // URL descriptor: https://example.com:81 | |
43 0x18, 0x03, 'h', 't', 't', 'p', 's', ':', '/', '/', 'e', 'x', 'a', 'm', 'p', | |
44 'l', 'e', '.', 'c', 'o', 'm', ':', '8', '1', | |
45 | |
46 // Function subset header. { | 43 // Function subset header. { |
47 0x05, 0x02, 0x01, 0x35, 0x00, | 44 0x05, 0x02, 0x01, 0x05, 0x06 |
48 | |
49 // URL descriptor: https://example.com:82 | |
50 0x18, 0x03, 'h', 't', 't', 'p', 's', ':', '/', '/', 'e', 'x', 'a', 'm', 'p', | |
51 'l', 'e', '.', 'c', 'o', 'm', ':', '8', '2', | |
52 | |
53 // URL descriptor: https://example.com:83 | |
54 0x18, 0x03, 'h', 't', 't', 'p', 's', ':', '/', '/', 'e', 'x', 'a', 'm', 'p', | |
55 'l', 'e', '.', 'c', 'o', 'm', ':', '8', '3', | |
56 | |
57 // } | 45 // } |
58 // URL descriptor: https://example.com:84 | |
59 0x18, 0x03, 'h', 't', 't', 'p', 's', ':', '/', '/', 'e', 'x', 'a', 'm', 'p', | |
60 'l', 'e', '.', 'c', 'o', 'm', ':', '8', '4', | |
61 | |
62 // } | 46 // } |
63 // URL descriptor: https://example.com:85 | |
64 0x18, 0x03, 'h', 't', 't', 'p', 's', ':', '/', '/', 'e', 'x', 'a', 'm', 'p', | |
65 'l', 'e', '.', 'c', 'o', 'm', ':', '8', '5', | |
66 }; | 47 }; |
67 | 48 |
68 const uint8_t kExampleUrlDescriptor[] = { | 49 const uint8_t kExampleUrlDescriptor1[] = { |
69 0x18, 0x03, 'h', 't', 't', 'p', 's', ':', '/', '/', 'e', 'x', | 50 0x19, 0x03, 0x01, 'e', 'x', 'a', 'm', 'p', 'l', 'e', '.', 'c', 'o', |
70 'a', 'm', 'p', 'l', 'e', '.', 'c', 'o', 'm', ':', '8', '0'}; | 51 'm', '/', 'i', 'n', 'd', 'e', 'x', '.', 'h', 't', 'm', 'l'}; |
| 52 |
| 53 const uint8_t kExampleUrlDescriptor2[] = {0x11, 0x03, 0x01, 'e', 'x', 'a', |
| 54 'm', 'p', 'l', 'e', '.', 'c', |
| 55 'o', 'm', ':', '8', '1'}; |
| 56 |
| 57 const uint8_t kExampleUrlDescriptor3[] = {0x11, 0x03, 0x01, 'e', 'x', 'a', |
| 58 'm', 'p', 'l', 'e', '.', 'c', |
| 59 'o', 'm', ':', '8', '2'}; |
| 60 |
| 61 const uint8_t kExampleUrlDescriptor4[] = {0x11, 0x03, 0x01, 'e', 'x', 'a', |
| 62 'm', 'p', 'l', 'e', '.', 'c', |
| 63 'o', 'm', ':', '8', '3'}; |
| 64 |
| 65 const uint8_t kExampleUrlDescriptor5[] = {0x11, 0x03, 0x01, 'e', 'x', 'a', |
| 66 'm', 'p', 'l', 'e', '.', 'c', |
| 67 'o', 'm', ':', '8', '4'}; |
| 68 |
| 69 const uint8_t kExampleUrlDescriptor6[] = {0x11, 0x03, 0x01, 'e', 'x', 'a', |
| 70 'm', 'p', 'l', 'e', '.', 'c', |
| 71 'o', 'm', ':', '8', '5'}; |
| 72 |
| 73 ACTION_P2(InvokeCallback, data, length) { |
| 74 size_t transferred_length = std::min(length, arg7); |
| 75 memcpy(arg6->data(), data, transferred_length); |
| 76 arg9.Run(USB_TRANSFER_COMPLETED, arg6, transferred_length); |
| 77 } |
| 78 |
| 79 void ExpectAllowedOriginsAndLandingPage( |
| 80 scoped_ptr<WebUsbAllowedOrigins> allowed_origins, |
| 81 const GURL& landing_page) { |
| 82 EXPECT_EQ(GURL("https://example.com/index.html"), landing_page); |
| 83 ASSERT_TRUE(allowed_origins); |
| 84 ASSERT_EQ(2u, allowed_origins->origins.size()); |
| 85 EXPECT_EQ(GURL("https://example.com"), allowed_origins->origins[0]); |
| 86 EXPECT_EQ(GURL("https://example.com:81"), allowed_origins->origins[1]); |
| 87 ASSERT_EQ(1u, allowed_origins->configurations.size()); |
| 88 EXPECT_EQ(GURL("https://example.com:82"), |
| 89 allowed_origins->configurations[0].origins[0]); |
| 90 EXPECT_EQ(GURL("https://example.com:83"), |
| 91 allowed_origins->configurations[0].origins[1]); |
| 92 ASSERT_EQ(1u, allowed_origins->configurations[0].functions.size()); |
| 93 EXPECT_EQ(GURL("https://example.com:84"), |
| 94 allowed_origins->configurations[0].functions[0].origins[0]); |
| 95 EXPECT_EQ(GURL("https://example.com:85"), |
| 96 allowed_origins->configurations[0].functions[0].origins[1]); |
| 97 } |
71 | 98 |
72 class WebUsbDescriptorsTest : public ::testing::Test {}; | 99 class WebUsbDescriptorsTest : public ::testing::Test {}; |
73 | 100 |
74 TEST_F(WebUsbDescriptorsTest, PlatformCapabilityDescriptor) { | 101 TEST_F(WebUsbDescriptorsTest, PlatformCapabilityDescriptor) { |
75 WebUsbPlatformCapabilityDescriptor descriptor; | 102 WebUsbPlatformCapabilityDescriptor descriptor; |
76 | 103 |
77 ASSERT_TRUE(descriptor.ParseFromBosDescriptor(std::vector<uint8_t>( | 104 ASSERT_TRUE(descriptor.ParseFromBosDescriptor(std::vector<uint8_t>( |
78 kExampleBosDescriptor, | 105 kExampleBosDescriptor, |
79 kExampleBosDescriptor + sizeof(kExampleBosDescriptor)))); | 106 kExampleBosDescriptor + sizeof(kExampleBosDescriptor)))); |
80 EXPECT_EQ(0x0100, descriptor.version); | 107 EXPECT_EQ(0x0100, descriptor.version); |
(...skipping 14 matching lines...) Expand all Loading... |
95 static const uint8_t kBuffer[] = {0x06, 0x0F, 0x05, 0x00, 0x01}; | 122 static const uint8_t kBuffer[] = {0x06, 0x0F, 0x05, 0x00, 0x01}; |
96 | 123 |
97 WebUsbPlatformCapabilityDescriptor descriptor; | 124 WebUsbPlatformCapabilityDescriptor descriptor; |
98 ASSERT_FALSE(descriptor.ParseFromBosDescriptor( | 125 ASSERT_FALSE(descriptor.ParseFromBosDescriptor( |
99 std::vector<uint8_t>(kBuffer, kBuffer + sizeof(kBuffer)))); | 126 std::vector<uint8_t>(kBuffer, kBuffer + sizeof(kBuffer)))); |
100 } | 127 } |
101 | 128 |
102 TEST_F(WebUsbDescriptorsTest, InvalidBosDescriptor) { | 129 TEST_F(WebUsbDescriptorsTest, InvalidBosDescriptor) { |
103 WebUsbPlatformCapabilityDescriptor descriptor; | 130 WebUsbPlatformCapabilityDescriptor descriptor; |
104 ASSERT_FALSE(descriptor.ParseFromBosDescriptor(std::vector<uint8_t>( | 131 ASSERT_FALSE(descriptor.ParseFromBosDescriptor(std::vector<uint8_t>( |
105 kExampleUrlDescriptor, | 132 kExampleUrlDescriptor1, |
106 kExampleUrlDescriptor + sizeof(kExampleUrlDescriptor)))); | 133 kExampleUrlDescriptor1 + sizeof(kExampleUrlDescriptor1)))); |
107 } | 134 } |
108 | 135 |
109 TEST_F(WebUsbDescriptorsTest, ShortBosDescriptor) { | 136 TEST_F(WebUsbDescriptorsTest, ShortBosDescriptor) { |
110 // wTotalLength is less than bLength. bNumDeviceCaps == 1 to expose buffer | 137 // wTotalLength is less than bLength. bNumDeviceCaps == 1 to expose buffer |
111 // length checking bugs. | 138 // length checking bugs. |
112 static const uint8_t kBuffer[] = {0x05, 0x0F, 0x04, 0x00, 0x01}; | 139 static const uint8_t kBuffer[] = {0x05, 0x0F, 0x04, 0x00, 0x01}; |
113 | 140 |
114 WebUsbPlatformCapabilityDescriptor descriptor; | 141 WebUsbPlatformCapabilityDescriptor descriptor; |
115 ASSERT_FALSE(descriptor.ParseFromBosDescriptor( | 142 ASSERT_FALSE(descriptor.ParseFromBosDescriptor( |
116 std::vector<uint8_t>(kBuffer, kBuffer + sizeof(kBuffer)))); | 143 std::vector<uint8_t>(kBuffer, kBuffer + sizeof(kBuffer)))); |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
210 // The WebUSB Platform Capability Descriptor is version 0.9 (too old). | 237 // The WebUSB Platform Capability Descriptor is version 0.9 (too old). |
211 static const uint8_t kBuffer[] = {0x05, 0x0F, 0x1C, 0x00, 0x01, 0x17, 0x10, | 238 static const uint8_t kBuffer[] = {0x05, 0x0F, 0x1C, 0x00, 0x01, 0x17, 0x10, |
212 0x05, 0x00, 0x38, 0xB6, 0x08, 0x34, 0xA9, | 239 0x05, 0x00, 0x38, 0xB6, 0x08, 0x34, 0xA9, |
213 0x09, 0xA0, 0x47, 0x8B, 0xFD, 0xA0, 0x76, | 240 0x09, 0xA0, 0x47, 0x8B, 0xFD, 0xA0, 0x76, |
214 0x88, 0x15, 0xB6, 0x65, 0x90, 0x00, 0x01}; | 241 0x88, 0x15, 0xB6, 0x65, 0x90, 0x00, 0x01}; |
215 WebUsbPlatformCapabilityDescriptor descriptor; | 242 WebUsbPlatformCapabilityDescriptor descriptor; |
216 ASSERT_FALSE(descriptor.ParseFromBosDescriptor( | 243 ASSERT_FALSE(descriptor.ParseFromBosDescriptor( |
217 std::vector<uint8_t>(kBuffer, kBuffer + sizeof(kBuffer)))); | 244 std::vector<uint8_t>(kBuffer, kBuffer + sizeof(kBuffer)))); |
218 } | 245 } |
219 | 246 |
220 TEST_F(WebUsbDescriptorsTest, DescriptorSet) { | 247 TEST_F(WebUsbDescriptorsTest, AllowedOrigins) { |
221 WebUsbDescriptorSet descriptor; | 248 WebUsbAllowedOrigins descriptor; |
222 | 249 |
223 ASSERT_TRUE(descriptor.Parse(std::vector<uint8_t>( | 250 ASSERT_TRUE(descriptor.Parse(std::vector<uint8_t>( |
224 kExampleDescriptorSet, | 251 kExampleAllowedOrigins, |
225 kExampleDescriptorSet + sizeof(kExampleDescriptorSet)))); | 252 kExampleAllowedOrigins + sizeof(kExampleAllowedOrigins)))); |
226 EXPECT_EQ(2u, descriptor.origins.size()); | 253 EXPECT_EQ(2u, descriptor.origin_ids.size()); |
227 EXPECT_EQ(GURL("https://example.com:80"), descriptor.origins[0]); | 254 EXPECT_EQ(1, descriptor.origin_ids[0]); |
228 EXPECT_EQ(GURL("https://example.com:85"), descriptor.origins[1]); | 255 EXPECT_EQ(2, descriptor.origin_ids[1]); |
229 EXPECT_EQ(1u, descriptor.configurations.size()); | 256 EXPECT_EQ(1u, descriptor.configurations.size()); |
230 | 257 |
231 const WebUsbConfigurationSubset& config1 = descriptor.configurations[0]; | 258 const WebUsbConfigurationSubset& config1 = descriptor.configurations[0]; |
232 EXPECT_EQ(2u, config1.origins.size()); | 259 EXPECT_EQ(2u, config1.origin_ids.size()); |
233 EXPECT_EQ(GURL("https://example.com:81"), config1.origins[0]); | 260 EXPECT_EQ(3, config1.origin_ids[0]); |
234 EXPECT_EQ(GURL("https://example.com:84"), config1.origins[1]); | 261 EXPECT_EQ(4, config1.origin_ids[1]); |
235 EXPECT_EQ(1u, config1.functions.size()); | 262 EXPECT_EQ(1u, config1.functions.size()); |
236 | 263 |
237 const WebUsbFunctionSubset& function1 = config1.functions[0]; | 264 const WebUsbFunctionSubset& function1 = config1.functions[0]; |
238 EXPECT_EQ(2u, function1.origins.size()); | 265 EXPECT_EQ(2u, function1.origin_ids.size()); |
239 EXPECT_EQ(GURL("https://example.com:82"), function1.origins[0]); | 266 EXPECT_EQ(5, function1.origin_ids[0]); |
240 EXPECT_EQ(GURL("https://example.com:83"), function1.origins[1]); | 267 EXPECT_EQ(6, function1.origin_ids[1]); |
241 } | 268 } |
242 | 269 |
243 TEST_F(WebUsbDescriptorsTest, ShortDescriptorSetHeader) { | 270 TEST_F(WebUsbDescriptorsTest, ShortDescriptorSetHeader) { |
244 // bLength is too short for a WebUSB Descriptor Set Header. | 271 // bLength less than 5, which makes this descriptor invalid. |
245 static const uint8_t kBuffer[] = {0x03, 0x00, 0x03}; | 272 static const uint8_t kBuffer[] = {0x04, 0x00, 0x05, 0x00, 0x00}; |
246 WebUsbDescriptorSet descriptor; | 273 WebUsbAllowedOrigins descriptor; |
247 ASSERT_FALSE(descriptor.Parse( | 274 ASSERT_FALSE(descriptor.Parse( |
248 std::vector<uint8_t>(kBuffer, kBuffer + sizeof(kBuffer)))); | 275 std::vector<uint8_t>(kBuffer, kBuffer + sizeof(kBuffer)))); |
249 } | 276 } |
250 | 277 |
251 TEST_F(WebUsbDescriptorsTest, LongDescriptorSetHeader) { | 278 TEST_F(WebUsbDescriptorsTest, LongDescriptorSetHeader) { |
252 // bLength is too long for a WebUSB DescriptorSet Header. | 279 // bLength is longer than the buffer size. |
253 static const uint8_t kBuffer[] = {0x05, 0x00, 0x04, 0x00}; | 280 static const uint8_t kBuffer[] = {0x06, 0x00, 0x05, 0x00, 0x00}; |
254 WebUsbDescriptorSet descriptor; | 281 WebUsbAllowedOrigins descriptor; |
255 ASSERT_FALSE(descriptor.Parse( | 282 ASSERT_FALSE(descriptor.Parse( |
256 std::vector<uint8_t>(kBuffer, kBuffer + sizeof(kBuffer)))); | 283 std::vector<uint8_t>(kBuffer, kBuffer + sizeof(kBuffer)))); |
257 } | 284 } |
258 | 285 |
259 TEST_F(WebUsbDescriptorsTest, UrlNotDescriptorSet) { | |
260 WebUsbDescriptorSet descriptor; | |
261 ASSERT_FALSE(descriptor.Parse(std::vector<uint8_t>( | |
262 kExampleUrlDescriptor, | |
263 kExampleUrlDescriptor + sizeof(kExampleUrlDescriptor)))); | |
264 } | |
265 | |
266 TEST_F(WebUsbDescriptorsTest, ShortDescriptorSet) { | 286 TEST_F(WebUsbDescriptorsTest, ShortDescriptorSet) { |
267 // wTotalLength is shorter than bLength, making the WebUSB Descriptor Set | 287 // wTotalLength is shorter than bLength, making the header inconsistent. |
268 // Header inconsistent. | 288 static const uint8_t kBuffer[] = {0x05, 0x00, 0x04, 0x00, 0x00}; |
269 static const uint8_t kBuffer[] = {0x04, 0x00, 0x03, 0x00}; | 289 WebUsbAllowedOrigins descriptor; |
270 WebUsbDescriptorSet descriptor; | |
271 ASSERT_FALSE(descriptor.Parse( | 290 ASSERT_FALSE(descriptor.Parse( |
272 std::vector<uint8_t>(kBuffer, kBuffer + sizeof(kBuffer)))); | 291 std::vector<uint8_t>(kBuffer, kBuffer + sizeof(kBuffer)))); |
273 } | 292 } |
274 | 293 |
275 TEST_F(WebUsbDescriptorsTest, LongDescriptorSet) { | 294 TEST_F(WebUsbDescriptorsTest, LongDescriptorSet) { |
276 // The WebUSB Descriptor Set Header's wTotalLength is longer than the buffer. | 295 // wTotalLength is longer than the buffer size. |
277 static const uint8_t kBuffer[] = {0x04, 0x00, 0x05, 0x00}; | 296 static const uint8_t kBuffer[] = {0x05, 0x00, 0x06, 0x00, 0x00}; |
278 WebUsbDescriptorSet descriptor; | 297 WebUsbAllowedOrigins descriptor; |
279 ASSERT_FALSE(descriptor.Parse( | 298 ASSERT_FALSE(descriptor.Parse( |
280 std::vector<uint8_t>(kBuffer, kBuffer + sizeof(kBuffer)))); | 299 std::vector<uint8_t>(kBuffer, kBuffer + sizeof(kBuffer)))); |
281 } | 300 } |
282 | 301 |
283 TEST_F(WebUsbDescriptorsTest, ShortDescriptorInDescriptorSet) { | 302 TEST_F(WebUsbDescriptorsTest, TooManyConfigurations) { |
284 // bLength for the descriptor within the descriptor set is too short. | 303 static const uint8_t kBuffer[] = {0x05, 0x00, 0x05, 0x00, 0x01}; |
285 static const uint8_t kBuffer[] = {0x04, 0x00, 0x05, 0x00, 0x01}; | 304 WebUsbAllowedOrigins descriptor; |
286 WebUsbDescriptorSet descriptor; | |
287 ASSERT_FALSE(descriptor.Parse( | 305 ASSERT_FALSE(descriptor.Parse( |
288 std::vector<uint8_t>(kBuffer, kBuffer + sizeof(kBuffer)))); | 306 std::vector<uint8_t>(kBuffer, kBuffer + sizeof(kBuffer)))); |
289 } | 307 } |
290 | 308 |
291 TEST_F(WebUsbDescriptorsTest, LongDescriptorInDescriptorSet) { | 309 TEST_F(WebUsbDescriptorsTest, ShortConfiguration) { |
292 // bLength for the descriptor within the descriptor set is longer than the | 310 // The configuration's bLength is less than 4, making it invalid. |
293 // remaining portion of the buffer and the wTotalLength of the descriptor set. | 311 static const uint8_t kBuffer[] = {0x05, 0x00, 0x08, 0x00, |
294 static const uint8_t kBuffer[] = {0x04, 0x00, 0x06, 0x00, 0x03, 0x03}; | 312 0x01, 0x03, 0x01, 0x01}; |
295 WebUsbDescriptorSet descriptor; | 313 WebUsbAllowedOrigins descriptor; |
296 ASSERT_FALSE(descriptor.Parse( | 314 ASSERT_FALSE(descriptor.Parse( |
297 std::vector<uint8_t>(kBuffer, kBuffer + sizeof(kBuffer)))); | 315 std::vector<uint8_t>(kBuffer, kBuffer + sizeof(kBuffer)))); |
298 } | 316 } |
299 | 317 |
300 TEST_F(WebUsbDescriptorsTest, InvalidDescriptorInDescriptorSet) { | 318 TEST_F(WebUsbDescriptorsTest, LongConfiguration) { |
301 // A descriptor set cannot contain a descriptor with this bDescriptorType. | 319 // The configuration's bLength is longer than the buffer. |
302 static const uint8_t kBuffer[] = {0x04, 0x00, 0x06, 0x00, 0x02, 0x04}; | 320 static const uint8_t kBuffer[] = {0x05, 0x00, 0x09, 0x00, 0x01, |
303 WebUsbDescriptorSet descriptor; | 321 0x05, 0x01, 0x01, 0x00}; |
| 322 WebUsbAllowedOrigins descriptor; |
304 ASSERT_FALSE(descriptor.Parse( | 323 ASSERT_FALSE(descriptor.Parse( |
305 std::vector<uint8_t>(kBuffer, kBuffer + sizeof(kBuffer)))); | 324 std::vector<uint8_t>(kBuffer, kBuffer + sizeof(kBuffer)))); |
306 } | 325 } |
307 | 326 |
308 TEST_F(WebUsbDescriptorsTest, EmptyUrlInDescriptorSet) { | 327 TEST_F(WebUsbDescriptorsTest, TooManyFunctions) { |
309 // The URL in this descriptor set is the empty string. | 328 static const uint8_t kBuffer[] = {0x05, 0x00, 0x09, 0x00, 0x01, |
310 static const uint8_t kBuffer[] = {0x04, 0x00, 0x06, 0x00, 0x02, 0x03}; | 329 0x04, 0x01, 0x01, 0x01}; |
311 WebUsbDescriptorSet descriptor; | 330 WebUsbAllowedOrigins descriptor; |
312 ASSERT_FALSE(descriptor.Parse( | 331 ASSERT_FALSE(descriptor.Parse( |
313 std::vector<uint8_t>(kBuffer, kBuffer + sizeof(kBuffer)))); | 332 std::vector<uint8_t>(kBuffer, kBuffer + sizeof(kBuffer)))); |
314 } | 333 } |
315 | 334 |
316 TEST_F(WebUsbDescriptorsTest, InvalidUrlInDescriptorSet) { | 335 TEST_F(WebUsbDescriptorsTest, ShortFunction) { |
317 // The URL in this descriptor set is not a valid URL: "This is not a URL." | 336 // The function's bLength is less than 3, making it invalid. |
318 static const uint8_t kBuffer[] = { | 337 static const uint8_t kBuffer[] = {0x05, 0x00, 0x0B, 0x00, 0x01, 0x04, |
319 0x04, 0x00, 0x18, 0x00, 0x14, 0x03, 'T', 'h', 'i', 's', ' ', 'i', | 338 0x01, 0x01, 0x01, 0x02, 0x02}; |
320 's', ' ', 'n', 'o', 't', ' ', 'a', ' ', 'U', 'R', 'L', '.'}; | 339 WebUsbAllowedOrigins descriptor; |
321 WebUsbDescriptorSet descriptor; | |
322 ASSERT_FALSE(descriptor.Parse( | 340 ASSERT_FALSE(descriptor.Parse( |
323 std::vector<uint8_t>(kBuffer, kBuffer + sizeof(kBuffer)))); | 341 std::vector<uint8_t>(kBuffer, kBuffer + sizeof(kBuffer)))); |
324 } | 342 } |
325 | 343 |
326 TEST_F(WebUsbDescriptorsTest, ShortConfigurationSubsetHeader) { | 344 TEST_F(WebUsbDescriptorsTest, LongFunction) { |
327 // bLength is too short for a WebUSB Configuration Subset Header. | 345 // The function's bLength is longer than the buffer. |
328 static const uint8_t kBuffer[] = {0x04, 0x00, 0x05, 0x00, 0x02, 0x01}; | 346 static const uint8_t kBuffer[] = {0x05, 0x00, 0x0C, 0x00, 0x01, 0x04, |
329 WebUsbDescriptorSet descriptor; | 347 0x01, 0x01, 0x01, 0x04, 0x02, 0x01}; |
| 348 WebUsbAllowedOrigins descriptor; |
330 ASSERT_FALSE(descriptor.Parse( | 349 ASSERT_FALSE(descriptor.Parse( |
331 std::vector<uint8_t>(kBuffer, kBuffer + sizeof(kBuffer)))); | 350 std::vector<uint8_t>(kBuffer, kBuffer + sizeof(kBuffer)))); |
332 } | 351 } |
333 | |
334 TEST_F(WebUsbDescriptorsTest, ShortConfigurationSubset) { | |
335 // The configuration subset header's wTotalLength is shorter than its bLength, | |
336 // making it inconsistent. | |
337 static const uint8_t kBuffer[] = {0x04, 0x00, 0x09, 0x00, 0x05, | |
338 0x01, 0x01, 0x04, 0x00}; | |
339 WebUsbDescriptorSet descriptor; | |
340 ASSERT_FALSE(descriptor.Parse( | |
341 std::vector<uint8_t>(kBuffer, kBuffer + sizeof(kBuffer)))); | |
342 } | |
343 | |
344 TEST_F(WebUsbDescriptorsTest, LongConfigurationSubset) { | |
345 // wTotalLength of the configuration subset header extends beyond wTotalLength | |
346 // for the descriptor set and the length of the buffer. | |
347 static const uint8_t kBuffer[] = {0x04, 0x00, 0x09, 0x00, 0x05, | |
348 0x01, 0x01, 0x06, 0x00}; | |
349 WebUsbDescriptorSet descriptor; | |
350 ASSERT_FALSE(descriptor.Parse( | |
351 std::vector<uint8_t>(kBuffer, kBuffer + sizeof(kBuffer)))); | |
352 } | |
353 | |
354 TEST_F(WebUsbDescriptorsTest, ShortDescriptorInConfigurationSubset) { | |
355 // bLength for the descriptor within the configuration subset is too short. | |
356 static const uint8_t kBuffer[] = {0x04, 0x00, 0x0A, 0x00, 0x05, | |
357 0x01, 0x01, 0x06, 0x00, 0x01}; | |
358 WebUsbDescriptorSet descriptor; | |
359 ASSERT_FALSE(descriptor.Parse( | |
360 std::vector<uint8_t>(kBuffer, kBuffer + sizeof(kBuffer)))); | |
361 } | |
362 | |
363 TEST_F(WebUsbDescriptorsTest, LongDescriptorInConfigurationSubset) { | |
364 // bLength for the descriptor within the configuration subset is longer than | |
365 // the remaining portion of the buffer and the wTotalLength of the | |
366 // configuration subset. | |
367 static const uint8_t kBuffer[] = {0x04, 0x00, 0x0B, 0x00, 0x05, 0x01, | |
368 0x01, 0x07, 0x00, 0x03, 0x03}; | |
369 WebUsbDescriptorSet descriptor; | |
370 ASSERT_FALSE(descriptor.Parse( | |
371 std::vector<uint8_t>(kBuffer, kBuffer + sizeof(kBuffer)))); | |
372 } | |
373 | |
374 TEST_F(WebUsbDescriptorsTest, InvalidDescriptorInConfigurationSubset) { | |
375 // A configuration subset cannot contain a descriptor with this | |
376 // bDescriptorType. | |
377 static const uint8_t kBuffer[] = {0x04, 0x00, 0x0B, 0x00, 0x05, 0x01, | |
378 0x01, 0x07, 0x00, 0x02, 0x01}; | |
379 WebUsbDescriptorSet descriptor; | |
380 ASSERT_FALSE(descriptor.Parse( | |
381 std::vector<uint8_t>(kBuffer, kBuffer + sizeof(kBuffer)))); | |
382 } | |
383 | |
384 TEST_F(WebUsbDescriptorsTest, ShortFunctionSubsetHeader) { | |
385 // bLength is too short for a WebUSB Function Subset Header. | |
386 static const uint8_t kBuffer[] = {0x04, 0x00, 0x0B, 0x00, 0x05, 0x01, | |
387 0x01, 0x07, 0x00, 0x02, 0x02}; | |
388 WebUsbDescriptorSet descriptor; | |
389 ASSERT_FALSE(descriptor.Parse( | |
390 std::vector<uint8_t>(kBuffer, kBuffer + sizeof(kBuffer)))); | |
391 } | |
392 | |
393 TEST_F(WebUsbDescriptorsTest, ShortFunctionSubset) { | |
394 // The function subset header's wTotalLength is shorter than its bLength, | |
395 // making it inconsistent. | |
396 static const uint8_t kBuffer[] = {0x04, 0x00, 0x0E, 0x00, 0x05, 0x01, 0x01, | |
397 0x0A, 0x00, 0x05, 0x02, 0x01, 0x04, 0x00}; | |
398 WebUsbDescriptorSet descriptor; | |
399 ASSERT_FALSE(descriptor.Parse( | |
400 std::vector<uint8_t>(kBuffer, kBuffer + sizeof(kBuffer)))); | |
401 } | |
402 | |
403 TEST_F(WebUsbDescriptorsTest, LongFunctionSubset) { | |
404 // wTotalLength of the function subset header extends beyond wTotalLength for | |
405 // for the configuration subset and the length of the buffer. | |
406 static const uint8_t kBuffer[] = {0x04, 0x00, 0x0E, 0x00, 0x05, 0x01, 0x01, | |
407 0x0A, 0x00, 0x05, 0x02, 0x01, 0x06, 0x00}; | |
408 WebUsbDescriptorSet descriptor; | |
409 ASSERT_FALSE(descriptor.Parse( | |
410 std::vector<uint8_t>(kBuffer, kBuffer + sizeof(kBuffer)))); | |
411 } | |
412 | |
413 TEST_F(WebUsbDescriptorsTest, ShortDescriptorInFunctionSubset) { | |
414 // bLength for the descriptor within the function subset is too short. | |
415 static const uint8_t kBuffer[] = {0x04, 0x00, 0x0F, 0x00, 0x05, | |
416 0x01, 0x01, 0x0B, 0x00, 0x05, | |
417 0x02, 0x01, 0x06, 0x00, 0x01}; | |
418 WebUsbDescriptorSet descriptor; | |
419 ASSERT_FALSE(descriptor.Parse( | |
420 std::vector<uint8_t>(kBuffer, kBuffer + sizeof(kBuffer)))); | |
421 } | |
422 | |
423 TEST_F(WebUsbDescriptorsTest, LongDescriptorInFunctionSubset) { | |
424 // bLength for the descriptor within the function subset is longer than the | |
425 // remaining portion of the buffer and the wTotalLength of the function | |
426 // subset. | |
427 static const uint8_t kBuffer[] = {0x04, 0x00, 0x10, 0x00, 0x05, 0x01, | |
428 0x01, 0x0C, 0x00, 0x05, 0x02, 0x01, | |
429 0x07, 0x00, 0x03, 0x03}; | |
430 WebUsbDescriptorSet descriptor; | |
431 ASSERT_FALSE(descriptor.Parse( | |
432 std::vector<uint8_t>(kBuffer, kBuffer + sizeof(kBuffer)))); | |
433 } | |
434 | |
435 TEST_F(WebUsbDescriptorsTest, InvalidDescriptorInFunctionSubset) { | |
436 // A function subset cannot contain a descriptor with this bDescriptorType. | |
437 static const uint8_t kBuffer[] = {0x04, 0x00, 0x10, 0x00, 0x05, 0x01, | |
438 0x01, 0x0C, 0x00, 0x05, 0x02, 0x01, | |
439 0x07, 0x00, 0x02, 0x02}; | |
440 WebUsbDescriptorSet descriptor; | |
441 ASSERT_FALSE(descriptor.Parse( | |
442 std::vector<uint8_t>(kBuffer, kBuffer + sizeof(kBuffer)))); | |
443 } | |
444 | 352 |
445 TEST_F(WebUsbDescriptorsTest, UrlDescriptor) { | 353 TEST_F(WebUsbDescriptorsTest, UrlDescriptor) { |
446 GURL url; | 354 GURL url; |
447 ASSERT_TRUE(ParseWebUsbUrlDescriptor( | 355 ASSERT_TRUE(ParseWebUsbUrlDescriptor( |
448 std::vector<uint8_t>( | 356 std::vector<uint8_t>( |
449 kExampleUrlDescriptor, | 357 kExampleUrlDescriptor1, |
450 kExampleUrlDescriptor + sizeof(kExampleUrlDescriptor)), | 358 kExampleUrlDescriptor1 + sizeof(kExampleUrlDescriptor1)), |
451 &url)); | 359 &url)); |
452 EXPECT_EQ(GURL("https://example.com:80"), url); | 360 EXPECT_EQ(GURL("https://example.com/index.html"), url); |
453 } | 361 } |
454 | 362 |
455 TEST_F(WebUsbDescriptorsTest, ShortUrlDescriptorHeader) { | 363 TEST_F(WebUsbDescriptorsTest, ShortUrlDescriptorHeader) { |
456 // The buffer is just too darn short. | 364 // The buffer is just too darn short. |
457 static const uint8_t kBuffer[] = {0x01}; | 365 static const uint8_t kBuffer[] = {0x01}; |
458 GURL url; | 366 GURL url; |
459 ASSERT_FALSE(ParseWebUsbUrlDescriptor( | 367 ASSERT_FALSE(ParseWebUsbUrlDescriptor( |
460 std::vector<uint8_t>(kBuffer, kBuffer + sizeof(kBuffer)), &url)); | 368 std::vector<uint8_t>(kBuffer, kBuffer + sizeof(kBuffer)), &url)); |
461 } | 369 } |
462 | 370 |
463 TEST_F(WebUsbDescriptorsTest, ShortUrlDescriptor) { | 371 TEST_F(WebUsbDescriptorsTest, ShortUrlDescriptor) { |
464 // bLength is too short. | 372 // bLength is too short. |
465 static const uint8_t kBuffer[] = {0x01, 0x03}; | 373 static const uint8_t kBuffer[] = {0x01, 0x03}; |
466 GURL url; | 374 GURL url; |
467 ASSERT_FALSE(ParseWebUsbUrlDescriptor( | 375 ASSERT_FALSE(ParseWebUsbUrlDescriptor( |
468 std::vector<uint8_t>(kBuffer, kBuffer + sizeof(kBuffer)), &url)); | 376 std::vector<uint8_t>(kBuffer, kBuffer + sizeof(kBuffer)), &url)); |
469 } | 377 } |
470 | 378 |
471 TEST_F(WebUsbDescriptorsTest, LongUrlDescriptor) { | 379 TEST_F(WebUsbDescriptorsTest, LongUrlDescriptor) { |
472 // bLength is too long. | 380 // bLength is too long. |
473 static const uint8_t kBuffer[] = {0x03, 0x03}; | 381 static const uint8_t kBuffer[] = {0x03, 0x03}; |
474 GURL url; | 382 GURL url; |
475 ASSERT_FALSE(ParseWebUsbUrlDescriptor( | 383 ASSERT_FALSE(ParseWebUsbUrlDescriptor( |
476 std::vector<uint8_t>(kBuffer, kBuffer + sizeof(kBuffer)), &url)); | 384 std::vector<uint8_t>(kBuffer, kBuffer + sizeof(kBuffer)), &url)); |
477 } | 385 } |
478 | 386 |
479 TEST_F(WebUsbDescriptorsTest, EmptyUrl) { | 387 TEST_F(WebUsbDescriptorsTest, EmptyUrl) { |
480 // The URL in this descriptor set is the empty string. | 388 // The URL in this descriptor set is the empty string. |
481 static const uint8_t kBuffer[] = {0x02, 0x03}; | 389 static const uint8_t kBuffer[] = {0x03, 0x03, 0x00}; |
482 GURL url; | 390 GURL url; |
483 ASSERT_FALSE(ParseWebUsbUrlDescriptor( | 391 ASSERT_FALSE(ParseWebUsbUrlDescriptor( |
484 std::vector<uint8_t>(kBuffer, kBuffer + sizeof(kBuffer)), &url)); | 392 std::vector<uint8_t>(kBuffer, kBuffer + sizeof(kBuffer)), &url)); |
485 } | 393 } |
486 | 394 |
487 TEST_F(WebUsbDescriptorsTest, InvalidUrl) { | 395 TEST_F(WebUsbDescriptorsTest, InvalidUrl) { |
488 // The URL in this descriptor set is not a valid URL: "This is not a URL." | 396 // The URL in this descriptor set is not a valid URL: "http://???" |
489 static const uint8_t kBuffer[] = {0x14, 0x03, 'T', 'h', 'i', 's', ' ', | 397 static const uint8_t kBuffer[] = {0x06, 0x03, 0x00, '?', '?', '?'}; |
490 'i', 's', ' ', 'n', 'o', 't', ' ', | |
491 'a', ' ', 'U', 'R', 'L', '.'}; | |
492 GURL url; | 398 GURL url; |
493 ASSERT_FALSE(ParseWebUsbUrlDescriptor( | 399 ASSERT_FALSE(ParseWebUsbUrlDescriptor( |
494 std::vector<uint8_t>(kBuffer, kBuffer + sizeof(kBuffer)), &url)); | 400 std::vector<uint8_t>(kBuffer, kBuffer + sizeof(kBuffer)), &url)); |
495 } | 401 } |
496 | 402 |
| 403 TEST_F(WebUsbDescriptorsTest, ReadDescriptors) { |
| 404 scoped_refptr<MockUsbDeviceHandle> device_handle( |
| 405 new MockUsbDeviceHandle(nullptr)); |
| 406 |
| 407 EXPECT_CALL(*device_handle, |
| 408 ControlTransfer(USB_DIRECTION_INBOUND, UsbDeviceHandle::STANDARD, |
| 409 UsbDeviceHandle::DEVICE, 0x06, 0x0F00, 0x0000, _, |
| 410 _, _, _)) |
| 411 .Times(2) |
| 412 .WillRepeatedly( |
| 413 InvokeCallback(kExampleBosDescriptor, sizeof(kExampleBosDescriptor))); |
| 414 EXPECT_CALL(*device_handle, |
| 415 ControlTransfer(USB_DIRECTION_INBOUND, UsbDeviceHandle::VENDOR, |
| 416 UsbDeviceHandle::DEVICE, 0x42, 0x0000, 0x0001, _, |
| 417 _, _, _)) |
| 418 .Times(2) |
| 419 .WillRepeatedly(InvokeCallback(kExampleAllowedOrigins, |
| 420 sizeof(kExampleAllowedOrigins))); |
| 421 EXPECT_CALL(*device_handle, |
| 422 ControlTransfer(USB_DIRECTION_INBOUND, UsbDeviceHandle::VENDOR, |
| 423 UsbDeviceHandle::DEVICE, 0x42, 0x0001, 0x0002, _, |
| 424 _, _, _)) |
| 425 .WillOnce(InvokeCallback(kExampleUrlDescriptor1, |
| 426 sizeof(kExampleUrlDescriptor1))); |
| 427 EXPECT_CALL(*device_handle, |
| 428 ControlTransfer(USB_DIRECTION_INBOUND, UsbDeviceHandle::VENDOR, |
| 429 UsbDeviceHandle::DEVICE, 0x42, 0x0002, 0x0002, _, |
| 430 _, _, _)) |
| 431 .WillOnce(InvokeCallback(kExampleUrlDescriptor2, |
| 432 sizeof(kExampleUrlDescriptor2))); |
| 433 EXPECT_CALL(*device_handle, |
| 434 ControlTransfer(USB_DIRECTION_INBOUND, UsbDeviceHandle::VENDOR, |
| 435 UsbDeviceHandle::DEVICE, 0x42, 0x0003, 0x0002, _, |
| 436 _, _, _)) |
| 437 .WillOnce(InvokeCallback(kExampleUrlDescriptor3, |
| 438 sizeof(kExampleUrlDescriptor3))); |
| 439 EXPECT_CALL(*device_handle, |
| 440 ControlTransfer(USB_DIRECTION_INBOUND, UsbDeviceHandle::VENDOR, |
| 441 UsbDeviceHandle::DEVICE, 0x42, 0x0004, 0x0002, _, |
| 442 _, _, _)) |
| 443 .WillOnce(InvokeCallback(kExampleUrlDescriptor4, |
| 444 sizeof(kExampleUrlDescriptor4))); |
| 445 EXPECT_CALL(*device_handle, |
| 446 ControlTransfer(USB_DIRECTION_INBOUND, UsbDeviceHandle::VENDOR, |
| 447 UsbDeviceHandle::DEVICE, 0x42, 0x0005, 0x0002, _, |
| 448 _, _, _)) |
| 449 .WillOnce(InvokeCallback(kExampleUrlDescriptor5, |
| 450 sizeof(kExampleUrlDescriptor5))); |
| 451 EXPECT_CALL(*device_handle, |
| 452 ControlTransfer(USB_DIRECTION_INBOUND, UsbDeviceHandle::VENDOR, |
| 453 UsbDeviceHandle::DEVICE, 0x42, 0x0006, 0x0002, _, |
| 454 _, _, _)) |
| 455 .WillOnce(InvokeCallback(kExampleUrlDescriptor6, |
| 456 sizeof(kExampleUrlDescriptor6))); |
| 457 |
| 458 ReadWebUsbDescriptors(device_handle, |
| 459 base::Bind(&ExpectAllowedOriginsAndLandingPage)); |
| 460 } |
| 461 |
497 } // namespace | 462 } // namespace |
498 | 463 |
499 } // namespace device | 464 } // namespace device |
OLD | NEW |