tor-browser

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

test_websocket_no_duplicate_packet.html (3127B)


      1 <!DOCTYPE HTML>
      2 <html>
      3 <head>
      4  <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"></meta>
      5  <title>WebSocket test - big blob on content side</title>
      6  <script src="/tests/SimpleTest/SimpleTest.js"></script>
      7  <script type="text/javascript" src="websocket_helpers.js"></script>
      8  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
      9 </head>
     10 <body>
     11 <script class="testbody" type="text/javascript">
     12 
     13 // Test steps:
     14 // 1. Create a websocket and send 8 chunks of 1MB random data.
     15 // 2. Store the hash of each chunk (1MB of random data).
     16 // 3. Websocket server returns the same data back.
     17 // 4. Calculate the hash again and check the hash is the same as the stored one.
     18 
     19 function genRandomPayload() {
     20  const count = 128;
     21  const chunkSize = 64 * 1024;
     22  let buffer = new Uint8Array(chunkSize * count);
     23  let offset = 0;
     24  for (let i = 0; i < count; i++) {
     25    let data = new Uint8Array(chunkSize);
     26    crypto.getRandomValues(data);
     27    buffer.set(data, offset);
     28    offset += chunkSize;
     29  }
     30 
     31  return buffer;
     32 }
     33 
     34 function genRandomFile() {
     35  return new File([genRandomPayload()], "payload.bin", {
     36    type: 'application/octet-stream'
     37  });
     38 }
     39 
     40 async function toHexString(buffer) {
     41  let hashBuffer = await crypto.subtle.digest("SHA-256", buffer);
     42  let hashBytes = new Uint8Array(hashBuffer);
     43  let toHex = b => b.toString(16).padStart(2, "0");
     44  return Array.from(hashBytes, toHex).join("");
     45 }
     46 
     47 let message_count = 0;
     48 let sentHashArray = [];
     49 async function sendFile(file, ws) {
     50  const oneMiB = 1 * 1024 * 1024;
     51 
     52  let offset = 0;
     53  while (offset < file.size) {
     54    let blob = file.slice(offset, offset + oneMiB);
     55    let buffer = await blob.arrayBuffer();
     56    let hash = await toHexString(buffer);
     57    sentHashArray.push(hash);
     58    ws.send(buffer);
     59    offset += blob.size;
     60    message_count++;
     61  }
     62 }
     63 
     64 var ws = CreateTestWS("wss://example.com/tests/dom/websocket/tests/file_websocket_bigBlob");
     65 is(ws.readyState, 0, "Initial readyState is 0");
     66 ws.binaryType = "blob";
     67 
     68 ws.onopen = function() {
     69  is(ws.readyState, 1, "Open readyState is 1");
     70  let file = genRandomFile();
     71  sendFile(file, ws);
     72 }
     73 
     74 let receivedBlobs = [];
     75 ws.onmessage = function(e) {
     76  ok(e.data instanceof Blob, "We should be receiving a Blob");
     77  receivedBlobs.push(e.data);
     78  message_count--;
     79  if (message_count == 0) {
     80    ws.close();
     81  }
     82 }
     83 
     84 async function checkContent() {
     85  is(receivedBlobs.length, sentHashArray.length, "length should be the same");
     86  for (let index = 0; index < receivedBlobs.length; index++) {
     87    let buffer = await receivedBlobs[index].arrayBuffer();
     88    let hash = await toHexString(buffer);
     89    is(hash, sentHashArray[index], "hash should be equal");
     90  }
     91 }
     92 
     93 ws.onclose = function() {
     94  is(ws.readyState, 3, "Close readyState is 3");
     95  checkContent().then(() => {
     96    SimpleTest.finish();
     97  });
     98 }
     99 
    100 SimpleTest.requestFlakyTimeout("The web socket tests are really fragile, but avoiding timeouts might be hard, since it's testing stuff on the network. " +
    101                               "Expect all sorts of flakiness in this test...");
    102 SimpleTest.waitForExplicitFinish();
    103 
    104 </script>
    105 </body>
    106 </html>