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