1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
|
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "device/usb/webusb_descriptors.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace device {
namespace {
const uint8_t kExampleBosDescriptor[] = {
// BOS descriptor.
0x05, 0x0F, 0x4C, 0x00, 0x03,
// Container ID descriptor.
0x14, 0x10, 0x04, 0x00, 0x2A, 0xF9, 0xF6, 0xC2, 0x98, 0x10, 0x2B, 0x49,
0x8E, 0x64, 0xFF, 0x01, 0x0C, 0x7F, 0x94, 0xE1,
// WebUSB Platform Capability descriptor.
0x17, 0x10, 0x05, 0x00, 0x38, 0xB6, 0x08, 0x34, 0xA9, 0x09, 0xA0, 0x47,
0x8B, 0xFD, 0xA0, 0x76, 0x88, 0x15, 0xB6, 0x65, 0x00, 0x01, 0x42,
// Microsoft OS 2.0 Platform Capability descriptor.
0x1C, 0x10, 0x05, 0x00, 0xDF, 0x60, 0xDD, 0xD8, 0x89, 0x45, 0xC7, 0x4C,
0x9C, 0xD2, 0x65, 0x9D, 0x9E, 0x64, 0x8A, 0x9F, 0x00, 0x00, 0x03, 0x06,
0x00, 0x00, 0x01, 0x00};
const uint8_t kExampleDescriptorSet[] = {
// Descriptor set header.
0x04, 0x00, 0x9E, 0x00,
// URL descriptor: https://example.com:80
0x18, 0x03, 'h', 't', 't', 'p', 's', ':', '/', '/', 'e', 'x', 'a', 'm', 'p',
'l', 'e', '.', 'c', 'o', 'm', ':', '8', '0',
// Configuration subset header. {
0x05, 0x01, 0x01, 0x6A, 0x00,
// URL descriptor: https://example.com:81
0x18, 0x03, 'h', 't', 't', 'p', 's', ':', '/', '/', 'e', 'x', 'a', 'm', 'p',
'l', 'e', '.', 'c', 'o', 'm', ':', '8', '1',
// Function subset header. {
0x05, 0x02, 0x01, 0x35, 0x00,
// URL descriptor: https://example.com:82
0x18, 0x03, 'h', 't', 't', 'p', 's', ':', '/', '/', 'e', 'x', 'a', 'm', 'p',
'l', 'e', '.', 'c', 'o', 'm', ':', '8', '2',
// URL descriptor: https://example.com:83
0x18, 0x03, 'h', 't', 't', 'p', 's', ':', '/', '/', 'e', 'x', 'a', 'm', 'p',
'l', 'e', '.', 'c', 'o', 'm', ':', '8', '3',
// }
// URL descriptor: https://example.com:84
0x18, 0x03, 'h', 't', 't', 'p', 's', ':', '/', '/', 'e', 'x', 'a', 'm', 'p',
'l', 'e', '.', 'c', 'o', 'm', ':', '8', '4',
// }
// URL descriptor: https://example.com:85
0x18, 0x03, 'h', 't', 't', 'p', 's', ':', '/', '/', 'e', 'x', 'a', 'm', 'p',
'l', 'e', '.', 'c', 'o', 'm', ':', '8', '5',
};
const uint8_t kExampleUrlDescriptor[] = {
0x18, 0x03, 'h', 't', 't', 'p', 's', ':', '/', '/', 'e', 'x',
'a', 'm', 'p', 'l', 'e', '.', 'c', 'o', 'm', ':', '8', '0'};
class WebUsbDescriptorsTest : public ::testing::Test {};
TEST_F(WebUsbDescriptorsTest, PlatformCapabilityDescriptor) {
WebUsbPlatformCapabilityDescriptor descriptor;
ASSERT_TRUE(descriptor.ParseFromBosDescriptor(std::vector<uint8_t>(
kExampleBosDescriptor,
kExampleBosDescriptor + sizeof(kExampleBosDescriptor))));
EXPECT_EQ(0x0100, descriptor.version);
EXPECT_EQ(0x42, descriptor.vendor_code);
}
TEST_F(WebUsbDescriptorsTest, ShortBosDescriptorHeader) {
// This BOS descriptor is just too short.
static const uint8_t kBuffer[] = {0x03, 0x0F, 0x03};
WebUsbPlatformCapabilityDescriptor descriptor;
ASSERT_FALSE(descriptor.ParseFromBosDescriptor(
std::vector<uint8_t>(kBuffer, kBuffer + sizeof(kBuffer))));
}
TEST_F(WebUsbDescriptorsTest, LongBosDescriptorHeader) {
// BOS descriptor's bLength is too large.
static const uint8_t kBuffer[] = {0x06, 0x0F, 0x05, 0x00, 0x01};
WebUsbPlatformCapabilityDescriptor descriptor;
ASSERT_FALSE(descriptor.ParseFromBosDescriptor(
std::vector<uint8_t>(kBuffer, kBuffer + sizeof(kBuffer))));
}
TEST_F(WebUsbDescriptorsTest, InvalidBosDescriptor) {
WebUsbPlatformCapabilityDescriptor descriptor;
ASSERT_FALSE(descriptor.ParseFromBosDescriptor(std::vector<uint8_t>(
kExampleUrlDescriptor,
kExampleUrlDescriptor + sizeof(kExampleUrlDescriptor))));
}
TEST_F(WebUsbDescriptorsTest, ShortBosDescriptor) {
// wTotalLength is less than bLength. bNumDeviceCaps == 1 to expose buffer
// length checking bugs.
static const uint8_t kBuffer[] = {0x05, 0x0F, 0x04, 0x00, 0x01};
WebUsbPlatformCapabilityDescriptor descriptor;
ASSERT_FALSE(descriptor.ParseFromBosDescriptor(
std::vector<uint8_t>(kBuffer, kBuffer + sizeof(kBuffer))));
}
TEST_F(WebUsbDescriptorsTest, LongBosDescriptor) {
// wTotalLength is too large. bNumDeviceCaps == 1 to expose buffer
// length checking bugs.
static const uint8_t kBuffer[] = {0x05, 0x0F, 0x06, 0x00, 0x01};
WebUsbPlatformCapabilityDescriptor descriptor;
ASSERT_FALSE(descriptor.ParseFromBosDescriptor(
std::vector<uint8_t>(kBuffer, kBuffer + sizeof(kBuffer))));
}
TEST_F(WebUsbDescriptorsTest, UnexpectedlyEmptyBosDescriptor) {
// bNumDeviceCaps == 1 but there are no actual descriptors.
static const uint8_t kBuffer[] = {0x05, 0x0F, 0x05, 0x00, 0x01};
WebUsbPlatformCapabilityDescriptor descriptor;
ASSERT_FALSE(descriptor.ParseFromBosDescriptor(
std::vector<uint8_t>(kBuffer, kBuffer + sizeof(kBuffer))));
}
TEST_F(WebUsbDescriptorsTest, ShortCapabilityDescriptor) {
// The single capability descriptor in the BOS descriptor is too short.
static const uint8_t kBuffer[] = {0x05, 0x0F, 0x06, 0x00, 0x01, 0x02, 0x10};
WebUsbPlatformCapabilityDescriptor descriptor;
ASSERT_FALSE(descriptor.ParseFromBosDescriptor(
std::vector<uint8_t>(kBuffer, kBuffer + sizeof(kBuffer))));
}
TEST_F(WebUsbDescriptorsTest, LongCapabilityDescriptor) {
// The bLength on a capability descriptor in the BOS descriptor is longer than
// the remaining space defined by wTotalLength.
static const uint8_t kBuffer[] = {0x05, 0x0F, 0x08, 0x00,
0x01, 0x04, 0x10, 0x05};
WebUsbPlatformCapabilityDescriptor descriptor;
ASSERT_FALSE(descriptor.ParseFromBosDescriptor(
std::vector<uint8_t>(kBuffer, kBuffer + sizeof(kBuffer))));
}
TEST_F(WebUsbDescriptorsTest, NotACapabilityDescriptor) {
// There is something other than a device capability descriptor in the BOS
// descriptor.
static const uint8_t kBuffer[] = {0x05, 0x0F, 0x08, 0x00,
0x01, 0x03, 0x0F, 0x05};
WebUsbPlatformCapabilityDescriptor descriptor;
ASSERT_FALSE(descriptor.ParseFromBosDescriptor(
std::vector<uint8_t>(kBuffer, kBuffer + sizeof(kBuffer))));
}
TEST_F(WebUsbDescriptorsTest, NoPlatformCapabilityDescriptor) {
// The BOS descriptor only contains a Container ID descriptor.
static const uint8_t kBuffer[] = {0x05, 0x0F, 0x19, 0x00, 0x01, 0x14, 0x10,
0x04, 0x00, 0x2A, 0xF9, 0xF6, 0xC2, 0x98,
0x10, 0x2B, 0x49, 0x8E, 0x64, 0xFF, 0x01,
0x0C, 0x7F, 0x94, 0xE1};
WebUsbPlatformCapabilityDescriptor descriptor;
ASSERT_FALSE(descriptor.ParseFromBosDescriptor(
std::vector<uint8_t>(kBuffer, kBuffer + sizeof(kBuffer))));
}
TEST_F(WebUsbDescriptorsTest, ShortPlatformCapabilityDescriptor) {
// The platform capability descriptor is too short to contain a UUID.
static const uint8_t kBuffer[] = {
0x05, 0x0F, 0x18, 0x00, 0x01, 0x13, 0x10, 0x05, 0x00, 0x2A, 0xF9, 0xF6,
0xC2, 0x98, 0x10, 0x2B, 0x49, 0x8E, 0x64, 0xFF, 0x01, 0x0C, 0x7F, 0x94};
WebUsbPlatformCapabilityDescriptor descriptor;
ASSERT_FALSE(descriptor.ParseFromBosDescriptor(
std::vector<uint8_t>(kBuffer, kBuffer + sizeof(kBuffer))));
}
TEST_F(WebUsbDescriptorsTest, NoWebUsbCapabilityDescriptor) {
// The BOS descriptor only contains another kind of platform capability
// descriptor.
static const uint8_t kBuffer[] = {0x05, 0x0F, 0x19, 0x00, 0x01, 0x14, 0x10,
0x05, 0x00, 0x2A, 0xF9, 0xF6, 0xC2, 0x98,
0x10, 0x2B, 0x49, 0x8E, 0x64, 0xFF, 0x01,
0x0C, 0x7F, 0x94, 0xE1};
WebUsbPlatformCapabilityDescriptor descriptor;
ASSERT_FALSE(descriptor.ParseFromBosDescriptor(
std::vector<uint8_t>(kBuffer, kBuffer + sizeof(kBuffer))));
}
TEST_F(WebUsbDescriptorsTest, ShortWebUsbPlatformCapabilityDescriptor) {
// The WebUSB Platform Capability Descriptor is too short.
static const uint8_t kBuffer[] = {0x05, 0x0F, 0x19, 0x00, 0x01, 0x14, 0x10,
0x05, 0x00, 0x38, 0xB6, 0x08, 0x34, 0xA9,
0x09, 0xA0, 0x47, 0x8B, 0xFD, 0xA0, 0x76,
0x88, 0x15, 0xB6, 0x65};
WebUsbPlatformCapabilityDescriptor descriptor;
ASSERT_FALSE(descriptor.ParseFromBosDescriptor(
std::vector<uint8_t>(kBuffer, kBuffer + sizeof(kBuffer))));
}
TEST_F(WebUsbDescriptorsTest, WebUsbPlatformCapabilityDescriptorOutOfDate) {
// The WebUSB Platform Capability Descriptor is version 0.9 (too old).
static const uint8_t kBuffer[] = {0x05, 0x0F, 0x1C, 0x00, 0x01, 0x17, 0x10,
0x05, 0x00, 0x38, 0xB6, 0x08, 0x34, 0xA9,
0x09, 0xA0, 0x47, 0x8B, 0xFD, 0xA0, 0x76,
0x88, 0x15, 0xB6, 0x65, 0x90, 0x00, 0x01};
WebUsbPlatformCapabilityDescriptor descriptor;
ASSERT_FALSE(descriptor.ParseFromBosDescriptor(
std::vector<uint8_t>(kBuffer, kBuffer + sizeof(kBuffer))));
}
TEST_F(WebUsbDescriptorsTest, DescriptorSet) {
WebUsbDescriptorSet descriptor;
ASSERT_TRUE(descriptor.Parse(std::vector<uint8_t>(
kExampleDescriptorSet,
kExampleDescriptorSet + sizeof(kExampleDescriptorSet))));
EXPECT_EQ(2u, descriptor.origins.size());
EXPECT_EQ(GURL("https://example.com:80"), descriptor.origins[0]);
EXPECT_EQ(GURL("https://example.com:85"), descriptor.origins[1]);
EXPECT_EQ(1u, descriptor.configurations.size());
const WebUsbConfigurationSubset& config1 = descriptor.configurations[0];
EXPECT_EQ(2u, config1.origins.size());
EXPECT_EQ(GURL("https://example.com:81"), config1.origins[0]);
EXPECT_EQ(GURL("https://example.com:84"), config1.origins[1]);
EXPECT_EQ(1u, config1.functions.size());
const WebUsbFunctionSubset& function1 = config1.functions[0];
EXPECT_EQ(2u, function1.origins.size());
EXPECT_EQ(GURL("https://example.com:82"), function1.origins[0]);
EXPECT_EQ(GURL("https://example.com:83"), function1.origins[1]);
}
TEST_F(WebUsbDescriptorsTest, ShortDescriptorSetHeader) {
// bLength is too short for a WebUSB Descriptor Set Header.
static const uint8_t kBuffer[] = {0x03, 0x00, 0x03};
WebUsbDescriptorSet descriptor;
ASSERT_FALSE(descriptor.Parse(
std::vector<uint8_t>(kBuffer, kBuffer + sizeof(kBuffer))));
}
TEST_F(WebUsbDescriptorsTest, LongDescriptorSetHeader) {
// bLength is too long for a WebUSB DescriptorSet Header.
static const uint8_t kBuffer[] = {0x05, 0x00, 0x04, 0x00};
WebUsbDescriptorSet descriptor;
ASSERT_FALSE(descriptor.Parse(
std::vector<uint8_t>(kBuffer, kBuffer + sizeof(kBuffer))));
}
TEST_F(WebUsbDescriptorsTest, UrlNotDescriptorSet) {
WebUsbDescriptorSet descriptor;
ASSERT_FALSE(descriptor.Parse(std::vector<uint8_t>(
kExampleUrlDescriptor,
kExampleUrlDescriptor + sizeof(kExampleUrlDescriptor))));
}
TEST_F(WebUsbDescriptorsTest, ShortDescriptorSet) {
// wTotalLength is shorter than bLength, making the WebUSB Descriptor Set
// Header inconsistent.
static const uint8_t kBuffer[] = {0x04, 0x00, 0x03, 0x00};
WebUsbDescriptorSet descriptor;
ASSERT_FALSE(descriptor.Parse(
std::vector<uint8_t>(kBuffer, kBuffer + sizeof(kBuffer))));
}
TEST_F(WebUsbDescriptorsTest, LongDescriptorSet) {
// The WebUSB Descriptor Set Header's wTotalLength is longer than the buffer.
static const uint8_t kBuffer[] = {0x04, 0x00, 0x05, 0x00};
WebUsbDescriptorSet descriptor;
ASSERT_FALSE(descriptor.Parse(
std::vector<uint8_t>(kBuffer, kBuffer + sizeof(kBuffer))));
}
TEST_F(WebUsbDescriptorsTest, ShortDescriptorInDescriptorSet) {
// bLength for the descriptor within the descriptor set is too short.
static const uint8_t kBuffer[] = {0x04, 0x00, 0x05, 0x00, 0x01};
WebUsbDescriptorSet descriptor;
ASSERT_FALSE(descriptor.Parse(
std::vector<uint8_t>(kBuffer, kBuffer + sizeof(kBuffer))));
}
TEST_F(WebUsbDescriptorsTest, LongDescriptorInDescriptorSet) {
// bLength for the descriptor within the descriptor set is longer than the
// remaining portion of the buffer and the wTotalLength of the descriptor set.
static const uint8_t kBuffer[] = {0x04, 0x00, 0x06, 0x00, 0x03, 0x03};
WebUsbDescriptorSet descriptor;
ASSERT_FALSE(descriptor.Parse(
std::vector<uint8_t>(kBuffer, kBuffer + sizeof(kBuffer))));
}
TEST_F(WebUsbDescriptorsTest, InvalidDescriptorInDescriptorSet) {
// A descriptor set cannot contain a descriptor with this bDescriptorType.
static const uint8_t kBuffer[] = {0x04, 0x00, 0x06, 0x00, 0x02, 0x04};
WebUsbDescriptorSet descriptor;
ASSERT_FALSE(descriptor.Parse(
std::vector<uint8_t>(kBuffer, kBuffer + sizeof(kBuffer))));
}
TEST_F(WebUsbDescriptorsTest, EmptyUrlInDescriptorSet) {
// The URL in this descriptor set is the empty string.
static const uint8_t kBuffer[] = {0x04, 0x00, 0x06, 0x00, 0x02, 0x03};
WebUsbDescriptorSet descriptor;
ASSERT_FALSE(descriptor.Parse(
std::vector<uint8_t>(kBuffer, kBuffer + sizeof(kBuffer))));
}
TEST_F(WebUsbDescriptorsTest, InvalidUrlInDescriptorSet) {
// The URL in this descriptor set is not a valid URL: "This is not a URL."
static const uint8_t kBuffer[] = {
0x04, 0x00, 0x18, 0x00, 0x14, 0x03, 'T', 'h', 'i', 's', ' ', 'i',
's', ' ', 'n', 'o', 't', ' ', 'a', ' ', 'U', 'R', 'L', '.'};
WebUsbDescriptorSet descriptor;
ASSERT_FALSE(descriptor.Parse(
std::vector<uint8_t>(kBuffer, kBuffer + sizeof(kBuffer))));
}
TEST_F(WebUsbDescriptorsTest, ShortConfigurationSubsetHeader) {
// bLength is too short for a WebUSB Configuration Subset Header.
static const uint8_t kBuffer[] = {0x04, 0x00, 0x05, 0x00, 0x02, 0x01};
WebUsbDescriptorSet descriptor;
ASSERT_FALSE(descriptor.Parse(
std::vector<uint8_t>(kBuffer, kBuffer + sizeof(kBuffer))));
}
TEST_F(WebUsbDescriptorsTest, ShortConfigurationSubset) {
// The configuration subset header's wTotalLength is shorter than its bLength,
// making it inconsistent.
static const uint8_t kBuffer[] = {0x04, 0x00, 0x09, 0x00, 0x05,
0x01, 0x01, 0x04, 0x00};
WebUsbDescriptorSet descriptor;
ASSERT_FALSE(descriptor.Parse(
std::vector<uint8_t>(kBuffer, kBuffer + sizeof(kBuffer))));
}
TEST_F(WebUsbDescriptorsTest, LongConfigurationSubset) {
// wTotalLength of the configuration subset header extends beyond wTotalLength
// for the descriptor set and the length of the buffer.
static const uint8_t kBuffer[] = {0x04, 0x00, 0x09, 0x00, 0x05,
0x01, 0x01, 0x06, 0x00};
WebUsbDescriptorSet descriptor;
ASSERT_FALSE(descriptor.Parse(
std::vector<uint8_t>(kBuffer, kBuffer + sizeof(kBuffer))));
}
TEST_F(WebUsbDescriptorsTest, ShortDescriptorInConfigurationSubset) {
// bLength for the descriptor within the configuration subset is too short.
static const uint8_t kBuffer[] = {0x04, 0x00, 0x0A, 0x00, 0x05,
0x01, 0x01, 0x06, 0x00, 0x01};
WebUsbDescriptorSet descriptor;
ASSERT_FALSE(descriptor.Parse(
std::vector<uint8_t>(kBuffer, kBuffer + sizeof(kBuffer))));
}
TEST_F(WebUsbDescriptorsTest, LongDescriptorInConfigurationSubset) {
// bLength for the descriptor within the configuration subset is longer than
// the remaining portion of the buffer and the wTotalLength of the
// configuration subset.
static const uint8_t kBuffer[] = {0x04, 0x00, 0x0B, 0x00, 0x05, 0x01,
0x01, 0x07, 0x00, 0x03, 0x03};
WebUsbDescriptorSet descriptor;
ASSERT_FALSE(descriptor.Parse(
std::vector<uint8_t>(kBuffer, kBuffer + sizeof(kBuffer))));
}
TEST_F(WebUsbDescriptorsTest, InvalidDescriptorInConfigurationSubset) {
// A configuration subset cannot contain a descriptor with this
// bDescriptorType.
static const uint8_t kBuffer[] = {0x04, 0x00, 0x0B, 0x00, 0x05, 0x01,
0x01, 0x07, 0x00, 0x02, 0x01};
WebUsbDescriptorSet descriptor;
ASSERT_FALSE(descriptor.Parse(
std::vector<uint8_t>(kBuffer, kBuffer + sizeof(kBuffer))));
}
TEST_F(WebUsbDescriptorsTest, ShortFunctionSubsetHeader) {
// bLength is too short for a WebUSB Function Subset Header.
static const uint8_t kBuffer[] = {0x04, 0x00, 0x0B, 0x00, 0x05, 0x01,
0x01, 0x07, 0x00, 0x02, 0x02};
WebUsbDescriptorSet descriptor;
ASSERT_FALSE(descriptor.Parse(
std::vector<uint8_t>(kBuffer, kBuffer + sizeof(kBuffer))));
}
TEST_F(WebUsbDescriptorsTest, ShortFunctionSubset) {
// The function subset header's wTotalLength is shorter than its bLength,
// making it inconsistent.
static const uint8_t kBuffer[] = {0x04, 0x00, 0x0E, 0x00, 0x05, 0x01, 0x01,
0x0A, 0x00, 0x05, 0x02, 0x01, 0x04, 0x00};
WebUsbDescriptorSet descriptor;
ASSERT_FALSE(descriptor.Parse(
std::vector<uint8_t>(kBuffer, kBuffer + sizeof(kBuffer))));
}
TEST_F(WebUsbDescriptorsTest, LongFunctionSubset) {
// wTotalLength of the function subset header extends beyond wTotalLength for
// for the configuration subset and the length of the buffer.
static const uint8_t kBuffer[] = {0x04, 0x00, 0x0E, 0x00, 0x05, 0x01, 0x01,
0x0A, 0x00, 0x05, 0x02, 0x01, 0x06, 0x00};
WebUsbDescriptorSet descriptor;
ASSERT_FALSE(descriptor.Parse(
std::vector<uint8_t>(kBuffer, kBuffer + sizeof(kBuffer))));
}
TEST_F(WebUsbDescriptorsTest, ShortDescriptorInFunctionSubset) {
// bLength for the descriptor within the function subset is too short.
static const uint8_t kBuffer[] = {0x04, 0x00, 0x0F, 0x00, 0x05,
0x01, 0x01, 0x0B, 0x00, 0x05,
0x02, 0x01, 0x06, 0x00, 0x01};
WebUsbDescriptorSet descriptor;
ASSERT_FALSE(descriptor.Parse(
std::vector<uint8_t>(kBuffer, kBuffer + sizeof(kBuffer))));
}
TEST_F(WebUsbDescriptorsTest, LongDescriptorInFunctionSubset) {
// bLength for the descriptor within the function subset is longer than the
// remaining portion of the buffer and the wTotalLength of the function
// subset.
static const uint8_t kBuffer[] = {0x04, 0x00, 0x10, 0x00, 0x05, 0x01,
0x01, 0x0C, 0x00, 0x05, 0x02, 0x01,
0x07, 0x00, 0x03, 0x03};
WebUsbDescriptorSet descriptor;
ASSERT_FALSE(descriptor.Parse(
std::vector<uint8_t>(kBuffer, kBuffer + sizeof(kBuffer))));
}
TEST_F(WebUsbDescriptorsTest, InvalidDescriptorInFunctionSubset) {
// A function subset cannot contain a descriptor with this bDescriptorType.
static const uint8_t kBuffer[] = {0x04, 0x00, 0x10, 0x00, 0x05, 0x01,
0x01, 0x0C, 0x00, 0x05, 0x02, 0x01,
0x07, 0x00, 0x02, 0x02};
WebUsbDescriptorSet descriptor;
ASSERT_FALSE(descriptor.Parse(
std::vector<uint8_t>(kBuffer, kBuffer + sizeof(kBuffer))));
}
TEST_F(WebUsbDescriptorsTest, UrlDescriptor) {
GURL url;
ASSERT_TRUE(ParseWebUsbUrlDescriptor(
std::vector<uint8_t>(
kExampleUrlDescriptor,
kExampleUrlDescriptor + sizeof(kExampleUrlDescriptor)),
&url));
EXPECT_EQ(GURL("https://example.com:80"), url);
}
TEST_F(WebUsbDescriptorsTest, ShortUrlDescriptorHeader) {
// The buffer is just too darn short.
static const uint8_t kBuffer[] = {0x01};
GURL url;
ASSERT_FALSE(ParseWebUsbUrlDescriptor(
std::vector<uint8_t>(kBuffer, kBuffer + sizeof(kBuffer)), &url));
}
TEST_F(WebUsbDescriptorsTest, ShortUrlDescriptor) {
// bLength is too short.
static const uint8_t kBuffer[] = {0x01, 0x03};
GURL url;
ASSERT_FALSE(ParseWebUsbUrlDescriptor(
std::vector<uint8_t>(kBuffer, kBuffer + sizeof(kBuffer)), &url));
}
TEST_F(WebUsbDescriptorsTest, LongUrlDescriptor) {
// bLength is too long.
static const uint8_t kBuffer[] = {0x03, 0x03};
GURL url;
ASSERT_FALSE(ParseWebUsbUrlDescriptor(
std::vector<uint8_t>(kBuffer, kBuffer + sizeof(kBuffer)), &url));
}
TEST_F(WebUsbDescriptorsTest, EmptyUrl) {
// The URL in this descriptor set is the empty string.
static const uint8_t kBuffer[] = {0x02, 0x03};
GURL url;
ASSERT_FALSE(ParseWebUsbUrlDescriptor(
std::vector<uint8_t>(kBuffer, kBuffer + sizeof(kBuffer)), &url));
}
TEST_F(WebUsbDescriptorsTest, InvalidUrl) {
// The URL in this descriptor set is not a valid URL: "This is not a URL."
static const uint8_t kBuffer[] = {0x14, 0x03, 'T', 'h', 'i', 's', ' ',
'i', 's', ' ', 'n', 'o', 't', ' ',
'a', ' ', 'U', 'R', 'L', '.'};
GURL url;
ASSERT_FALSE(ParseWebUsbUrlDescriptor(
std::vector<uint8_t>(kBuffer, kBuffer + sizeof(kBuffer)), &url));
}
} // namespace
} // namespace device
|