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
|
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="resources/manual.js"></script>
</head>
<body>
<p>
These tests require a USB device to be connected.
</p>
<script>
const kGetDescriptorRequest = 0x06;
const kDeviceDescriptorType = 0x01;
const kDeviceDescriptorLength = 18;
const kConfigurationDescriptorType = 0x02;
const kConfigurationDescriptorLength = 9;
const kStringDescriptorType = 0x03;
const kStringDescriptorMaxLength = 0xFF;
const kInterfaceDescriptorType = 0x04;
const kInterfaceDescriptorLength = 9;
const kEndpointDescriptorType = 0x05;
const kEndpointDescriptorLength = 7;
let device = null;
let pending_subtests = 0;
const string_tests = new Set();
const string_languages = [];
async function subtest_complete() {
if (--pending_subtests == 0) {
await device.close();
}
}
function manual_usb_subtest(func, name, properties) {
pending_subtests++;
promise_test(async (test) => {
test.add_cleanup(subtest_complete);
await func(test);
}, name, properties);
}
function read_string(index, expected) {
// A device may use the same string in multiple places. Don't bother
// repeating the test.
if (string_tests.has(index)) {
return;
}
string_tests.add(index);
const string_values = new Set();
const decoder = new TextDecoder('utf-16le');
for (const language of string_languages) {
const language_string = language.toString(16).padStart(4, '0');
manual_usb_subtest(async (t) => {
if (expected != undefined) {
t.add_cleanup(() => {
if (string_values.size == string_languages.length) {
assert_true(string_values.has(expected));
}
});
}
const result = await device.controlTransferIn({
requestType: 'standard',
recipient: 'device',
request: kGetDescriptorRequest,
value: kStringDescriptorType << 8 | index,
index: language
}, kStringDescriptorMaxLength);
assert_equals(result.status, 'ok', 'transfer status');
const length = result.data.getUint8(0);
assert_greater_than_equal(length, 2, 'descriptor length');
assert_equals(result.data.byteLength, length, 'transfer length');
assert_equals(result.data.getUint8(1), kStringDescriptorType,
'descriptor type');
const string_buffer = new Uint8Array(
result.data.buffer, result.data.byteOffset + 2, length - 2);
string_values.add(decoder.decode(string_buffer));
},
`Read string descriptor ${index} in language 0x${language_string}`);
}
}
function check_interface_descriptor(configuration, data) {
assert_greater_than_equal(
data.getUint8(0), kInterfaceDescriptorLength, 'descriptor length');
const interface_number = data.getUint8(2);
const iface = configuration.interfaces.find((iface) => {
return iface.interfaceNumber == interface_number;
});
assert_not_equals(
iface, undefined, `unknown interface ${interface_number}`);
const alternate_setting = data.getUint8(3);
const alternate = iface.alternates.find((alternate) => {
return alternate.alternateSetting == alternate_setting;
});
assert_not_equals(
alternate, undefined, `unknown alternate ${alternate_setting}`);
assert_equals(data.getUint8(4), alternate.endpoints.length,
'number of endpoints');
assert_equals(
data.getUint8(5), alternate.interfaceClass, 'interface class');
assert_equals(data.getUint8(6), alternate.interfaceSubclass,
'interface subclass');
assert_equals(data.getUint8(7), alternate.interfaceProtocol,
'interface protocol');
const interface_string = data.getUint8(8);
if (interface_string != 0) {
// TODO(crbug.com/727819): Check that the string descriptor matches
// iface.interfaceName.
read_string(interface_string);
}
return alternate;
}
function check_endpoint_descriptor(alternate, data) {
assert_greater_than_equal(
data.getUint8(0), kEndpointDescriptorLength, 'descriptor length');
const endpoint_address = data.getUint8(2);
const direction = endpoint_address & 0x80 ? 'in' : 'out';
const endpoint_number = endpoint_address & 0x0f;
const endpoint = alternate.endpoints.find((endpoint) => {
return endpoint.direction == direction &&
endpoint.endpointNumber == endpoint_number;
});
assert_not_equals(
endpoint, undefined, `unknown endpoint ${endpoint_number}`);
const attributes = data.getUint8(3);
switch (attributes & 0x03) {
case 0:
assert_equals(endpoint.type, 'control', 'endpoint type');
break;
case 1:
assert_equals(endpoint.type, 'isochronous', 'endpoint type');
break;
case 2:
assert_equals(endpoint.type, 'bulk', 'endpoint type');
break;
case 3:
assert_equals(endpoint.type, 'interrupt', 'endpoint type');
break;
}
assert_equals(data.getUint16(4, /*littleEndian=*/true),
endpoint.packetSize, 'packet size');
}
function read_config_descriptor(config_value) {
manual_usb_subtest(async (t) => {
const configuration = device.configurations.find((config) => {
return config.configurationValue == config_value;
});
assert_not_equals(configuration, undefined);
let result = await device.controlTransferIn({
requestType: 'standard',
recipient: 'device',
request: kGetDescriptorRequest,
value: kConfigurationDescriptorType << 8 | (config_value - 1),
index: 0
}, kConfigurationDescriptorLength);
assert_equals(result.status, 'ok', 'transfer status');
let length = result.data.getUint8(0);
assert_greater_than_equal(
length, kConfigurationDescriptorLength, 'descriptor length');
assert_equals(result.data.byteLength, length, 'transfer length');
const total_length = result.data.getUint16(2, /*littleEndian=*/true);
result = await device.controlTransferIn({
requestType: 'standard',
recipient: 'device',
request: kGetDescriptorRequest,
value: kConfigurationDescriptorType << 8 | (config_value - 1),
index: 0
}, total_length);
assert_equals(result.status, 'ok', 'transfer status');
assert_equals(
result.data.byteLength, total_length, 'transfer length');
assert_equals(result.data.getUint8(0), length, 'descriptor length');
assert_equals(result.data.getUint8(1), kConfigurationDescriptorType,
'descriptor type');
assert_equals(result.data.getUint16(2, /*littleEndian=*/true),
total_length, 'total length');
assert_equals(
result.data.getUint8(4), configuration.interfaces.length,
'number of interfaces');
assert_equals(
result.data.getUint8(5), config_value, 'configuration value');
const configuration_string = result.data.getUint8(6);
if (configuration_string != 0) {
// TODO(crbug.com/727819): Check that the string descriptor matches
// configuration.configurationName.
read_string(configuration_string);
}
let offset = length;
let alternate = undefined;
while (offset < total_length) {
length = result.data.getUint8(offset);
assert_less_than_equal(offset + length, total_length);
const view = new DataView(
result.data.buffer, result.data.byteOffset + offset, length);
switch (view.getUint8(1)) {
case kConfigurationDescriptorType:
assert_unreached('cannot contain multiple config descriptors');
break;
case kInterfaceDescriptorType:
alternate = check_interface_descriptor(configuration, view);
break;
case kEndpointDescriptorType:
assert_not_equals(alternate, undefined,
'endpoint not defined after interface');
check_endpoint_descriptor(alternate, view);
break;
}
offset += length;
}
}, `Read config descriptor ${config_value}`);
}
function read_string_descriptor_languages(device_descriptor) {
manual_usb_subtest(async (t) => {
const result = await device.controlTransferIn({
requestType: 'standard',
recipient: 'device',
request: kGetDescriptorRequest,
value: kStringDescriptorType << 8,
index: 0
}, kStringDescriptorMaxLength);
assert_equals(result.status, 'ok', 'transfer status');
assert_equals(result.data.getUint8(1), kStringDescriptorType,
'descriptor type');
const length = result.data.getUint8(0);
assert_greater_than_equal(length, 2, 'descriptor length')
assert_greater_than_equal(
result.data.byteLength, length, 'transfer length');
for (let index = 2; index < length; index += 2) {
string_languages.push(
result.data.getUint16(index, /*littleEndian=*/true));
}
const manufacturer_string = device_descriptor.getUint8(14);
if (manufacturer_string != 0) {
assert_not_equals(device.manufacturerName, undefined);
read_string(manufacturer_string, device.manufacturerName);
}
const product_string = device_descriptor.getUint8(15);
if (product_string != 0) {
assert_not_equals(device.productName, undefined);
read_string(product_string, device.productName);
}
const serial_number_string = device_descriptor.getUint8(16);
if (serial_number_string != 0) {
assert_not_equals(device.serialNumber, undefined);
read_string(serial_number_string, device.serialNumber);
}
const num_configurations = device_descriptor.getUint8(17);
for (let config_value = 1; config_value <= num_configurations;
++config_value) {
read_config_descriptor(config_value);
}
}, `Read supported languages`);
}
promise_test(async (t) => {
device = await getDeviceForManualTest();
await device.open();
const result = await device.controlTransferIn({
requestType: 'standard',
recipient: 'device',
request: kGetDescriptorRequest,
value: kDeviceDescriptorType << 8,
index: 0,
}, kDeviceDescriptorLength);
assert_equals(result.status, 'ok', 'transfer status');
assert_equals(
result.data.byteLength, kDeviceDescriptorLength, 'transfer length');
assert_greater_than_equal(
result.data.getUint8(0),
kDeviceDescriptorLength, 'descriptor length');
assert_equals(result.data.getUint8(1), kDeviceDescriptorType,
'descriptor type');
const bcd_usb = result.data.getUint16(2, /*littleEndian=*/true);
assert_equals(
bcd_usb >> 8, device.usbVersionMajor, 'USB version major');
assert_equals(
(bcd_usb & 0xf0) >> 4, device.usbVersionMinor, 'USB version minor');
assert_equals(
bcd_usb & 0xf, device.usbVersionSubminor, 'USV version subminor');
assert_equals(
result.data.getUint8(4), device.deviceClass, 'device class');
assert_equals(
result.data.getUint8(5), device.deviceSubclass, 'device subclass');
assert_equals(
result.data.getUint8(6), device.deviceProtocol, 'device protocol');
assert_equals(result.data.getUint16(8, /*littleEndian=*/true),
device.vendorId, 'vendor id');
assert_equals(result.data.getUint16(10, /*littleEndian=*/true),
device.productId, 'product id');
const bcd_device = result.data.getUint16(12, /*littleEndian=*/true);
assert_equals(
bcd_device >> 8, device.deviceVersionMajor, 'device version major');
assert_equals((bcd_device & 0xf0) >> 4, device.deviceVersionMinor,
'device version minor');
assert_equals(bcd_device & 0xf, device.deviceVersionSubminor,
'device version subminor');
assert_equals(result.data.getUint8(17), device.configurations.length,
'number of configurations');
read_string_descriptor_languages(result.data);
if (pending_subtests == 0) {
await device.close();
}
}, 'Read device descriptor');
</script>
</body>
</html>
|