tor-browser

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

streams-echo.https.any.js (10400B)


      1 // META: global=window,worker
      2 // META: script=/common/get-host-info.sub.js
      3 // META: script=resources/webtransport-test-helpers.sub.js
      4 
      5 promise_test(async t => {
      6  // Establish a WebTransport session.
      7  const wt = new WebTransport(webtransport_url('echo.py'));
      8  await wt.ready;
      9 
     10  // Create a bidirectional stream.
     11  const bidi_stream = await wt.createBidirectionalStream();
     12 
     13  // Write a message to the writable end, and close it.
     14  const writer = bidi_stream.writable.getWriter();
     15  const encoder = new TextEncoder();
     16  await writer.write(encoder.encode('Hello World'));
     17  await writer.close();
     18 
     19  // Read the data on the readable end.
     20  const reply = await read_stream_as_string(bidi_stream.readable);
     21 
     22  // Check that the message from the readable end matches the writable end.
     23  assert_equals(reply, 'Hello World');
     24 }, 'WebTransport client should be able to create and handle a bidirectional stream');
     25 
     26 promise_test(async t => {
     27  // Establish a WebTransport session.
     28  const wt = new WebTransport(webtransport_url('echo.py'));
     29 
     30  // Create a bidirectional stream.
     31  const bidi_stream = await wt.createBidirectionalStream();
     32 
     33  // Write a message to the writable end, and close it.
     34  const writer = bidi_stream.writable.getWriter();
     35  const encoder = new TextEncoder();
     36  await writer.write(encoder.encode('Hello World'));
     37  await writer.close();
     38 
     39  // Read the data on the readable end.
     40  const reply = await read_stream_as_string(bidi_stream.readable);
     41 
     42  // Check that the message from the readable end matches the writable end.
     43  assert_equals(reply, 'Hello World');
     44 }, 'WebTransport client should be able to create and handle a bidirectional stream without waiting for ready');
     45 
     46 promise_test(async t => {
     47  // Establish a WebTransport session.
     48  const wt = new WebTransport(webtransport_url('echo.py'));
     49  await wt.ready;
     50 
     51  // The echo handler creates a bidirectional stream when a WebTransport session
     52  // is established. Accept the bidirectional stream.
     53  const stream_reader = wt.incomingBidirectionalStreams.getReader();
     54  const { value: bidi_stream } = await stream_reader.read();
     55  stream_reader.releaseLock();
     56 
     57  // Write a message to the writable end, and close it.
     58  const encoder = new TextEncoderStream();
     59  encoder.readable.pipeTo(bidi_stream.writable);
     60  const writer = encoder.writable.getWriter();
     61  await writer.write('Hello World');
     62  await writer.close();
     63 
     64  // Read the data on the readable end.
     65  const reply = await read_stream_as_string(bidi_stream.readable);
     66 
     67  // Check that the message from the readable end matches the writable end.
     68  assert_equals(reply, 'Hello World');
     69 }, 'WebTransport server should be able to accept and handle a bidirectional stream');
     70 
     71 promise_test(async t => {
     72  // Establish a WebTransport session.
     73  const wt = new WebTransport(webtransport_url('echo.py'));
     74  await wt.ready;
     75 
     76  // Create a unidirectional stream.
     77  const writable = await wt.createUnidirectionalStream();
     78 
     79  // Write a message to the writable end, and close it.
     80  const encoder = new TextEncoderStream();
     81  encoder.readable.pipeTo(writable);
     82  const writer = encoder.writable.getWriter();
     83  await writer.write('Hello World');
     84  await writer.close();
     85 
     86  // The echo handler creates a new unidirectional stream to echo back data from
     87  // the server to client. Accept the unidirectional stream.
     88  const readable = wt.incomingUnidirectionalStreams;
     89  const stream_reader = readable.getReader();
     90  const { value: recv_stream } = await stream_reader.read();
     91  stream_reader.releaseLock();
     92 
     93  // Read the data on the readable end.
     94  const reply = await read_stream_as_string(recv_stream);
     95 
     96  // Make sure the message on the writable and readable ends of the streams
     97  // match.
     98  assert_equals(reply, 'Hello World');
     99 }, 'WebTransport client should be able to create, accept, and handle a unidirectional stream');
    100 
    101 promise_test(async t => {
    102  // Establish a WebTransport session.
    103  const wt = new WebTransport(webtransport_url('echo.py'));
    104 
    105  // Create a unidirectional stream.
    106  const writable = await wt.createUnidirectionalStream();
    107 
    108  // Write a message to the writable end, and close it.
    109  const encoder = new TextEncoderStream();
    110  encoder.readable.pipeTo(writable);
    111  const writer = encoder.writable.getWriter();
    112  await writer.write('Hello World');
    113  await writer.close();
    114 
    115  // The echo handler creates a new unidirectional stream to echo back data from
    116  // the server to client. Accept the unidirectional stream.
    117  const readable = wt.incomingUnidirectionalStreams;
    118  const stream_reader = readable.getReader();
    119  const { value: recv_stream } = await stream_reader.read();
    120  stream_reader.releaseLock();
    121 
    122  // Read the data on the readable end.
    123  const reply = await read_stream_as_string(recv_stream);
    124 
    125  // Make sure the message on the writable and readable ends of the streams
    126  // match.
    127  assert_equals(reply, 'Hello World');
    128 }, 'WebTransport client should be able to create, accept, and handle a unidirectional stream without waiting for ready');
    129 
    130 promise_test(async t => {
    131  // Establish a WebTransport session.
    132  const wt = new WebTransport(webtransport_url('echo.py'));
    133  await wt.ready;
    134 
    135  // The echo handler creates a bidirectional stream when a WebTransport session
    136  // is established. Accept the bidirectional stream.
    137  const stream_reader = wt.incomingBidirectionalStreams.getReader();
    138  const {value: bidi_stream} = await stream_reader.read();
    139  stream_reader.releaseLock();
    140 
    141  // Write data to the writable end, and close it.
    142  const buffer_size = 256;
    143  const data = new Uint8Array(buffer_size);
    144  for (let i = 0; i < data.byteLength; ++i) {
    145    data[i] = i;
    146  }
    147  const writer = bidi_stream.writable.getWriter();
    148  writer.write(data);
    149  await writer.close();
    150 
    151  // Read the data on the readable end and check if it matches the writable end.
    152  const reader = bidi_stream.readable.getReader({mode: 'byob'});
    153  assert_true(reader instanceof ReadableStreamBYOBReader);
    154  const half_buffer_size = buffer_size / 2;
    155  for (let i = 0; i < 2; i++) {
    156    let buffer = new ArrayBuffer(half_buffer_size);
    157    buffer = await readInto(reader, buffer);
    158    assert_array_equals(
    159        new Uint8Array(buffer),
    160        data.subarray(half_buffer_size * i, half_buffer_size * (i + 1)))
    161  }
    162  reader.releaseLock();
    163 }, 'Can read data from a bidirectional stream with BYOB reader');
    164 
    165 promise_test(async t => {
    166  // Establish a WebTransport session.
    167  const wt = new WebTransport(webtransport_url('echo.py'));
    168  await wt.ready;
    169 
    170  // Create a unidirectional stream.
    171  const writable = await wt.createUnidirectionalStream();
    172 
    173  // Write data to the writable end, and close it.
    174  const buffer_size = 256;
    175  const data = new Uint8Array(buffer_size);
    176  for (let i = 0; i < data.byteLength; ++i) {
    177    data[i] = i;
    178  }
    179  const writer = writable.getWriter();
    180  writer.write(data);
    181  await writer.close();
    182 
    183  // The echo handler creates a new unidirectional stream to echo back data from
    184  // the server to client. Accept the unidirectional stream.
    185  const readable = wt.incomingUnidirectionalStreams;
    186  const stream_reader = readable.getReader();
    187  const {value: recv_stream} = await stream_reader.read();
    188  stream_reader.releaseLock();
    189 
    190  // Read the data on the readable end and check if it matches the writable end.
    191  const reader = recv_stream.getReader({mode: 'byob'});
    192  assert_true(reader instanceof ReadableStreamBYOBReader);
    193  const half_buffer_size = buffer_size / 2;
    194  let buffer = new ArrayBuffer(half_buffer_size);
    195  for (let i = 0; i < 2; i++) {
    196    buffer = await readInto(reader, buffer);
    197    assert_array_equals(
    198        new Uint8Array(buffer),
    199        data.subarray(half_buffer_size * i, half_buffer_size * (i + 1)))
    200  }
    201  reader.releaseLock();
    202 }, 'Can read data from a unidirectional stream with BYOB reader');
    203 
    204 promise_test(async t => {
    205  // Establish a WebTransport session.
    206  const wt = new WebTransport(webtransport_url('echo.py'));
    207  await wt.ready;
    208 
    209  // Create a bidirectional stream.
    210  const bidi_stream = await wt.createBidirectionalStream();
    211 
    212  // Write a message to the writable end, and close it.
    213  const writer = bidi_stream.writable.getWriter();
    214  const bytes = new Uint8Array(16384);
    215  const [reply] = await Promise.all([
    216    read_stream(bidi_stream.readable),
    217    writer.write(bytes),
    218    writer.write(bytes),
    219    writer.write(bytes),
    220    writer.close()
    221  ]);
    222  let len = 0;
    223  for (chunk of reply) {
    224    len += chunk.length;
    225  }
    226  // Check that the message from the readable end matches the writable end.
    227  assert_equals(len, 3*bytes.length);
    228 }, 'Transfer large chunks of data on a bidirectional stream');
    229 
    230 promise_test(async t => {
    231  // Establish a WebTransport session.
    232  const wt = new WebTransport(webtransport_url('echo.py'));
    233  await wt.ready;
    234 
    235  // Create a unidirectional stream.
    236  const uni_stream = await wt.createUnidirectionalStream();
    237 
    238  // Write a message to the writable end, and close it.
    239  const writer = uni_stream.getWriter();
    240  const bytes = new Uint8Array(16384);
    241  await Promise.all([
    242    writer.write(bytes),
    243    writer.write(bytes),
    244    writer.write(bytes),
    245    writer.close()
    246  ]);
    247  // XXX Update once chrome fixes https://crbug.com/929585
    248  // The echo handler creates a new unidirectional stream to echo back data from
    249  // the server to client. Accept the unidirectional stream.
    250  const readable = wt.incomingUnidirectionalStreams;
    251  const stream_reader = readable.getReader();
    252  const { value: recv_stream } = await stream_reader.read();
    253  stream_reader.releaseLock();
    254 
    255  // Read the data on the readable end.
    256  const reply = await read_stream(recv_stream);
    257  let len = 0;
    258  for (chunk of reply) {
    259    len += chunk.length;
    260  }
    261  // Check that the message from the readable end matches the writable end.
    262  assert_equals(len, 3*bytes.length);
    263 }, 'Transfer large chunks of data on a unidirectional stream');
    264 
    265 promise_test(async t => {
    266  // Establish a WebTransport session.
    267  const wt = new WebTransport(webtransport_url('echo.py'));
    268  await wt.ready;
    269 
    270  // Create a bidirectional stream.
    271  const bidi_stream = await wt.createBidirectionalStream();
    272 
    273  // Close the writable end with no data at all.
    274  const writer = bidi_stream.writable.getWriter();
    275  writer.close();
    276 
    277  // Read the data on the readable end.
    278  const chunks = await read_stream(bidi_stream.readable);
    279  assert_equals(chunks.length, 0);
    280 
    281  await bidi_stream.readable.closed;
    282 }, 'Closing the stream with no data still resolves the read request');