Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Streams: Tests for abuse of RSBYOBRequest (#9558)
In whatwg/streams#870 it was made impossible to directly construct a ReadableStreamBYOBRequest. Prior to that it was possible to violate the state invariants of ReadableByteStreamController by constructing then using a BYOBRequest directly. Add tests that verify that it is not possible to construct a ReadableStreamBYOBRequest directly. Also add tests that it isn't possible to violate the invariants of ReadableByteStreamController by re-using a BYOBRequest object. Also add a test that it isn't possible to keep a BYOBRequest object valid while releasing the reader lock, as this would also be a way to violate ReadableByteStreamController's invariants. Fixes whatwg/streams#870
- Loading branch information
Showing
7 changed files
with
215 additions
and
0 deletions.
There are no files selected for viewing
11 changes: 11 additions & 0 deletions
11
streams/readable-byte-streams/construct-byob-request.dedicatedworker.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<!DOCTYPE html> | ||
<meta charset="utf-8"> | ||
<title>construct-byob-request.js dedicated worker wrapper file</title> | ||
|
||
<script src="/resources/testharness.js"></script> | ||
<script src="/resources/testharnessreport.js"></script> | ||
|
||
<script> | ||
'use strict'; | ||
fetch_tests_from_worker(new Worker('construct-byob-request.js')); | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<!DOCTYPE html> | ||
<meta charset="utf-8"> | ||
<title>construct-byob-request.js browser context wrapper file</title> | ||
|
||
<script src="/resources/testharness.js"></script> | ||
<script src="/resources/testharnessreport.js"></script> | ||
|
||
<script src="../resources/rs-utils.js"></script> | ||
|
||
<script src="construct-byob-request.js"></script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
'use strict'; | ||
|
||
// Prior to whatwg/stream#870 it was possible to construct a ReadableStreamBYOBRequest directly. This made it possible | ||
// to construct requests that were out-of-sync with the state of the ReadableStream. They could then be used to call | ||
// internal operations, resulting in asserts or bad behaviour. This file contains regression tests for the change. | ||
|
||
if (self.importScripts) { | ||
self.importScripts('../resources/rs-utils.js'); | ||
self.importScripts('/resources/testharness.js'); | ||
} | ||
|
||
function getRealByteStreamController() { | ||
let controller; | ||
new ReadableStream({ | ||
start(c) { | ||
controller = c; | ||
}, | ||
type: 'bytes' | ||
}); | ||
return controller; | ||
} | ||
|
||
const ReadableByteStreamController = getRealByteStreamController().constructor; | ||
|
||
// Create an object pretending to have prototype |prototype|, of type |type|. |type| is one of "undefined", "null", | ||
// "fake", or "real". "real" will call the realObjectCreator function to get a real instance of the object. | ||
function createDummyObject(prototype, type, realObjectCreator) { | ||
switch (type) { | ||
case 'undefined': | ||
return undefined; | ||
|
||
case 'null': | ||
return null; | ||
|
||
case 'fake': | ||
return Object.create(prototype); | ||
|
||
case 'real': | ||
return realObjectCreator(); | ||
} | ||
|
||
throw new Error('not reached'); | ||
} | ||
|
||
const dummyTypes = ['undefined', 'null', 'fake', 'real']; | ||
|
||
function runTests(ReadableStreamBYOBRequest) { | ||
for (const controllerType of dummyTypes) { | ||
const controller = createDummyObject(ReadableByteStreamController.prototype, controllerType, | ||
getRealByteStreamController); | ||
for (const viewType of dummyTypes) { | ||
const view = createDummyObject(Uint8Array.prototype, viewType, () => new Uint8Array(16)); | ||
test(() => { | ||
assert_throws(new TypeError(), () => new ReadableStreamBYOBRequest(controller, view), | ||
'constructor should throw'); | ||
}, `ReadableStreamBYOBRequest constructor should throw when passed a ${controllerType} ` + | ||
`ReadableByteStreamController and a ${viewType} view`); | ||
} | ||
} | ||
} | ||
|
||
function getConstructorAndRunTests() { | ||
let ReadableStreamBYOBRequest; | ||
const rs = new ReadableStream({ | ||
pull(controller) { | ||
const byobRequest = controller.byobRequest; | ||
ReadableStreamBYOBRequest = byobRequest.constructor; | ||
byobRequest.respond(4); | ||
}, | ||
type: 'bytes' | ||
}); | ||
rs.getReader({ mode: 'byob' }).read(new Uint8Array(8)).then(() => { | ||
runTests(ReadableStreamBYOBRequest); | ||
done(); | ||
}); | ||
} | ||
|
||
// We can only get at the ReadableStreamBYOBRequest constructor asynchronously, so we need to make the test harness wait | ||
// for us to explicitly tell it all our tests have run. | ||
setup({ explicit_done: true }); | ||
|
||
getConstructorAndRunTests(); |
12 changes: 12 additions & 0 deletions
12
streams/readable-byte-streams/construct-byob-request.serviceworker.https.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<!DOCTYPE html> | ||
<meta charset="utf-8"> | ||
<title>construct-byob-request.js service worker wrapper file</title> | ||
|
||
<script src="/resources/testharness.js"></script> | ||
<script src="/resources/testharnessreport.js"></script> | ||
<script src="/service-workers/service-worker/resources/test-helpers.sub.js"></script> | ||
|
||
<script> | ||
'use strict'; | ||
service_worker_test('construct-byob-request.js', 'Service worker test setup'); | ||
</script> |
11 changes: 11 additions & 0 deletions
11
streams/readable-byte-streams/construct-byob-request.sharedworker.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<!DOCTYPE html> | ||
<meta charset="utf-8"> | ||
<title>construct-byob-request.js shared worker wrapper file</title> | ||
|
||
<script src="/resources/testharness.js"></script> | ||
<script src="/resources/testharnessreport.js"></script> | ||
|
||
<script> | ||
'use strict'; | ||
fetch_tests_from_worker(new SharedWorker('construct-byob-request.js')); | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters