summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/streams/readable-byte-streams/construct-byob-request.any.js
blob: 8d460a1c81b7bdc74bb56b856f0f3a07bd982585 (plain)
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
// META: global=window,worker
// META: script=../resources/rs-utils.js
'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.

function getRealByteStreamController() {
  let controller;
  new ReadableStream({
    start(c) {
      controller = c;
    },
    type: 'bytes'
  });
  return controller;
}

// 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'];

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_js(TypeError, () => new ReadableStreamBYOBRequest(controller, view),
                        'constructor should throw');
    }, `ReadableStreamBYOBRequest constructor should throw when passed a ${controllerType} ` +
        `ReadableByteStreamController and a ${viewType} view`);
  }
}