Commit d10150de authored by Maksim Ivanov's avatar Maksim Ivanov Committed by Commit Bot

Fix ArrayBuffer bug in chrome.test.checkDeepEq

This makes the chrome.test.checkDeepEq() and the chrome.test.assertEq()
functions more robust when handling the ArrayBuffer values, in
particular handling the following scenario:

  chrome.test.checkDeepEq([], (new Uint8Array([1,2,3])).buffer);
  chrome.test.checkDeepEq([], (new Uint8Array([])).buffer);

Before this CL, the code above evaluated to |true|, despite that the
values are logically different. The bug was caused by the fact that
iterating using a "for" loop over an ArrayBuffer doesn't actually visit
any of its values, which led to it being considered as an empty
container.

The proposed fix is to additionally compare the results of the
"instanceof" check.

Also fix the "ExtensionWebRequestApiTest.PostData2" test to explicitly
clear the array buffer in the field where it shouldn't be used in the
comparison, rather than rely on the previous implicit "any array buffer
is considered equal to an empty array" behavior.

Bug: 1078761
Change-Id: Ie48293d7894a6212a7ec07444ad328603cfe0c8e
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2245487Reviewed-by: default avatarIstiaque Ahmed <lazyboy@chromium.org>
Commit-Queue: Maksim Ivanov <emaxx@chromium.org>
Cr-Commit-Position: refs/heads/master@{#779709}
parent 3733d579
...@@ -350,6 +350,15 @@ function captureEvent(name, details, callback) { ...@@ -350,6 +350,15 @@ function captureEvent(name, details, callback) {
delete details.responseHeaders; delete details.responseHeaders;
} }
if (details?.requestBody?.raw) {
for (const rawItem of details.requestBody.raw) {
chrome.test.assertTrue(rawItem.bytes instanceof ArrayBuffer);
// Stub out the bytes with an empty array buffer, since the expectations
// don't hardcode real bytes.
rawItem.bytes = new ArrayBuffer;
}
}
// Check if the equivalent event is already captured, and issue a unique // Check if the equivalent event is already captured, and issue a unique
// |eventCount| to identify each. // |eventCount| to identify each.
var eventCount = 0; var eventCount = 0;
......
...@@ -99,7 +99,7 @@ function sendPost(formFile, parseableForm) { ...@@ -99,7 +99,7 @@ function sendPost(formFile, parseableForm) {
requestBody: parseableForm ? { requestBody: parseableForm ? {
formData: formData formData: formData
} : { } : {
raw: [{bytes: {}}] // ArrayBuffer raw: [{bytes: new ArrayBuffer}] // wildcard: matches any buffer
}, },
initiator: getDomain(initiators.WEB_INITIATED) initiator: getDomain(initiators.WEB_INITIATED)
} }
......
...@@ -167,6 +167,10 @@ apiBridge.registerCustomHook(function(api) { ...@@ -167,6 +167,10 @@ apiBridge.registerCustomHook(function(api) {
if (Array.isArray(expected) !== Array.isArray(actual)) if (Array.isArray(expected) !== Array.isArray(actual))
return false; return false;
// Handle the ArrayBuffer cases. Bail out in case of type mismatch, to
// prevent the ArrayBuffer from being treated as an empty enumerable below.
if ((actual instanceof ArrayBuffer) !== (expected instanceof ArrayBuffer))
return false;
if ((actual instanceof ArrayBuffer) && (expected instanceof ArrayBuffer)) { if ((actual instanceof ArrayBuffer) && (expected instanceof ArrayBuffer)) {
if (actual.byteLength != expected.byteLength) if (actual.byteLength != expected.byteLength)
return false; return false;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment