tor-browser

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

serialPort_readable-manual.https.html (2919B)


      1 <!DOCTYPE html>
      2 <html>
      3  <head>
      4    <meta charset="utf-8">
      5    <title></title>
      6    <script src="/resources/testharness.js"></script>
      7    <script src="/resources/testharnessreport.js"></script>
      8    <script src="resources/common.js"></script>
      9    <script src="resources/manual.js"></script>
     10  </head>
     11  <body>
     12    <p>
     13      These tests require a device configured with the following Arduino sketch:
     14 
     15      <pre>
     16 uint32_t seed = 0;
     17 uint32_t bytesToSend = 0;
     18 
     19 uint8_t nextByte() {
     20  seed = (1103515245 * seed + 12345) % 0x8000000;
     21  return (seed >> 16) & 0xFF;
     22 }
     23 
     24 void setup() {
     25  Serial.begin(115200);
     26 }
     27 
     28 void loop() {
     29  if (!Serial) {
     30    return;
     31  }
     32 
     33  if (bytesToSend == 0) {
     34    // Read the seed and number of bytes to send from the host as 32-bit
     35    // little-endian values.
     36    if (Serial.available() &lt 8) {
     37      return;
     38    }
     39 
     40    uint8_t buf[8];
     41    Serial.readBytes((char*)buf, sizeof buf);
     42    seed = (uint32_t)buf[0] |
     43            ((uint32_t)buf[1] &lt;&lt; 8) |
     44            ((uint32_t)buf[2] &lt;&lt; 16) |
     45            ((uint32_t)buf[3] &lt;&lt; 24);
     46    bytesToSend = (uint32_t)buf[4] |
     47                  ((uint32_t)buf[5] &lt;&lt; 8) |
     48                  ((uint32_t)buf[6] &lt;&lt; 16) |
     49                  ((uint32_t)buf[7] &lt;&lt; 24);
     50  } else {
     51    uint8_t buf[64];
     52    uint32_t count = min(sizeof buf, bytesToSend);
     53    for (uint32_t i = 0; i &lt; count; ++i) {
     54      buf[i] = nextByte();
     55    }
     56    bytesToSend -= count;
     57    Serial.write((char*)buf, count);
     58  }
     59 }
     60      </pre>
     61    </p>
     62    <p>
     63      <progress id='progress'></progress>
     64    </p>
     65    <script>
     66      let seed = 10;
     67      const length = 1024 * 1024 * 10;
     68 
     69      function next_byte() {
     70        seed = (Math.imul(1103515245, seed) + 12345) % (1 << 31);
     71        return (seed >> 16) & 0xFF;
     72      }
     73 
     74      manual_serial_test(async (t, port) => {
     75        await port.open({baudRate: 115200, bufferSize: 1024});
     76 
     77        const config = new DataView(new ArrayBuffer(8));
     78        config.setUint32(0, seed, /*littleEndian=*/true);
     79        config.setUint32(4, length, /*littleEndian=*/true);
     80 
     81        const writer = port.writable.getWriter();
     82        writer.write(config);
     83 
     84        const progress = document.getElementById('progress');
     85        progress.max = length;
     86        progress.value = 0;
     87 
     88        const reader = port.readable.getReader();
     89        let bytesRead = 0;
     90        while (bytesRead < length) {
     91          const { value, done } = await reader.read();
     92          assert_false(done);
     93          for (let i = 0; i < value.byteLength; ++i) {
     94            assert_equals(value[i], next_byte(),
     95                          `mismatch at byte ${bytesRead + i}`);
     96          }
     97          bytesRead += value.byteLength;
     98          progress.value = bytesRead;
     99        }
    100 
    101        writer.releaseLock();
    102        reader.releaseLock();
    103        await port.close();
    104      }, `Reading ${length} bytes from the device succeeds.`);
    105    </script>
    106  </body>
    107 </html>