tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

bad-strategies.any.js (4601B)


      1 // META: global=window,worker,shadowrealm
      2 'use strict';
      3 
      4 test(() => {
      5 
      6  const theError = new Error('a unique string');
      7 
      8  assert_throws_exactly(theError, () => {
      9    new ReadableStream({}, {
     10      get size() {
     11        throw theError;
     12      },
     13      highWaterMark: 5
     14    });
     15  }, 'construction should re-throw the error');
     16 
     17 }, 'Readable stream: throwing strategy.size getter');
     18 
     19 promise_test(t => {
     20 
     21  const controllerError = { name: 'controller error' };
     22  const thrownError = { name: 'thrown error' };
     23 
     24  let controller;
     25  const rs = new ReadableStream(
     26    {
     27      start(c) {
     28        controller = c;
     29      }
     30    },
     31    {
     32      size() {
     33        controller.error(controllerError);
     34        throw thrownError;
     35      },
     36      highWaterMark: 5
     37    }
     38  );
     39 
     40  assert_throws_exactly(thrownError, () => controller.enqueue('a'), 'enqueue should re-throw the error');
     41 
     42  return promise_rejects_exactly(t, controllerError, rs.getReader().closed);
     43 
     44 }, 'Readable stream: strategy.size errors the stream and then throws');
     45 
     46 promise_test(t => {
     47 
     48  const theError = { name: 'my error' };
     49 
     50  let controller;
     51  const rs = new ReadableStream(
     52    {
     53      start(c) {
     54        controller = c;
     55      }
     56    },
     57    {
     58      size() {
     59        controller.error(theError);
     60        return Infinity;
     61      },
     62      highWaterMark: 5
     63    }
     64  );
     65 
     66  assert_throws_js(RangeError, () => controller.enqueue('a'), 'enqueue should throw a RangeError');
     67 
     68  return promise_rejects_exactly(t, theError, rs.getReader().closed, 'closed should reject with the error');
     69 
     70 }, 'Readable stream: strategy.size errors the stream and then returns Infinity');
     71 
     72 promise_test(() => {
     73 
     74  const theError = new Error('a unique string');
     75  const rs = new ReadableStream(
     76    {
     77      start(c) {
     78        assert_throws_exactly(theError, () => c.enqueue('a'), 'enqueue should throw the error');
     79      }
     80    },
     81    {
     82      size() {
     83        throw theError;
     84      },
     85      highWaterMark: 5
     86    }
     87  );
     88 
     89  return rs.getReader().closed.catch(e => {
     90    assert_equals(e, theError, 'closed should reject with the error');
     91  });
     92 
     93 }, 'Readable stream: throwing strategy.size method');
     94 
     95 test(() => {
     96 
     97  const theError = new Error('a unique string');
     98 
     99  assert_throws_exactly(theError, () => {
    100    new ReadableStream({}, {
    101      size() {
    102        return 1;
    103      },
    104      get highWaterMark() {
    105        throw theError;
    106      }
    107    });
    108  }, 'construction should re-throw the error');
    109 
    110 }, 'Readable stream: throwing strategy.highWaterMark getter');
    111 
    112 test(() => {
    113 
    114  for (const highWaterMark of [-1, -Infinity, NaN, 'foo', {}]) {
    115    assert_throws_js(RangeError, () => {
    116      new ReadableStream({}, {
    117        size() {
    118          return 1;
    119        },
    120        highWaterMark
    121      });
    122    }, 'construction should throw a RangeError for ' + highWaterMark);
    123  }
    124 
    125 }, 'Readable stream: invalid strategy.highWaterMark');
    126 
    127 promise_test(() => {
    128 
    129  const promises = [];
    130  for (const size of [NaN, -Infinity, Infinity, -1]) {
    131    let theError;
    132    const rs = new ReadableStream(
    133      {
    134        start(c) {
    135          try {
    136            c.enqueue('hi');
    137            assert_unreached('enqueue didn\'t throw');
    138          } catch (error) {
    139            assert_equals(error.name, 'RangeError', 'enqueue should throw a RangeError for ' + size);
    140            theError = error;
    141          }
    142        }
    143      },
    144      {
    145        size() {
    146          return size;
    147        },
    148        highWaterMark: 5
    149      }
    150    );
    151 
    152    promises.push(rs.getReader().closed.then(() => {
    153      assert_unreached('closed didn\'t throw');
    154    }, e => {
    155      assert_equals(e, theError, 'closed should reject with the error for ' + size);
    156    }));
    157  }
    158 
    159  return Promise.all(promises);
    160 
    161 }, 'Readable stream: invalid strategy.size return value');
    162 
    163 promise_test(() => {
    164 
    165  const promises = [];
    166  for (const size of [NaN, -Infinity, Infinity, -1]) {
    167    let theError;
    168    const rs = new ReadableStream(
    169      {
    170        pull(c) {
    171          try {
    172            c.enqueue('hi');
    173            assert_unreached('enqueue didn\'t throw');
    174          } catch (error) {
    175            assert_equals(error.name, 'RangeError', 'enqueue should throw a RangeError for ' + size);
    176            theError = error;
    177          }
    178        }
    179      },
    180      {
    181        size() {
    182          return size;
    183        },
    184        highWaterMark: 5
    185      }
    186    );
    187 
    188    promises.push(rs.getReader().closed.then(() => {
    189      assert_unreached('closed didn\'t throw');
    190    }, e => {
    191      assert_equals(e, theError, 'closed should reject with the error for ' + size);
    192    }));
    193  }
    194 
    195  return Promise.all(promises);
    196 
    197 }, 'Readable stream: invalid strategy.size return value when pulling');