tor-browser

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

test_ArchiveEncryption_nonce.js (2264B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 https://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 const { ArchiveUtils } = ChromeUtils.importESModule(
      7  "resource:///modules/backup/ArchiveUtils.sys.mjs"
      8 );
      9 const { NonceUtils } = ChromeUtils.importESModule(
     10  "resource:///modules/backup/ArchiveEncryption.sys.mjs"
     11 );
     12 
     13 /**
     14 * Tests that we can increment a nonce and set the last chunk byte.
     15 */
     16 add_task(async function test_nonce_arithmetic() {
     17  let nonce = new Uint8Array(16);
     18 
     19  NonceUtils.incrementNonce(nonce);
     20  Assert.deepEqual(
     21    nonce,
     22    new Uint8Array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1])
     23  );
     24 
     25  NonceUtils.incrementNonce(nonce, 255);
     26  Assert.deepEqual(
     27    nonce,
     28    new Uint8Array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0])
     29  );
     30 
     31  NonceUtils.incrementNonce(nonce);
     32  Assert.deepEqual(
     33    nonce,
     34    new Uint8Array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1])
     35  );
     36 
     37  NonceUtils.incrementNonce(nonce, 255);
     38  Assert.deepEqual(
     39    nonce,
     40    new Uint8Array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0])
     41  );
     42 
     43  NonceUtils.incrementNonce(nonce, 257);
     44  Assert.deepEqual(
     45    nonce,
     46    new Uint8Array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 1])
     47  );
     48 
     49  Assert.ok(
     50    !NonceUtils.lastChunkSetOnNonce(nonce),
     51    "Last chunk bit hasn't been flipped yet."
     52  );
     53 
     54  // When marking the last chunk on the nonce, we set the 12th byte of the
     55  // counter to 0x01.
     56  NonceUtils.setLastChunkOnNonce(nonce);
     57  Assert.deepEqual(
     58    nonce,
     59    new Uint8Array([0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 1])
     60  );
     61  Assert.ok(
     62    NonceUtils.lastChunkSetOnNonce(nonce),
     63    "Last chunk bit was flipped."
     64  );
     65 });
     66 
     67 /**
     68 * Tests that the nonce counter will throw if it is incremented past a value
     69 * indicating a greater number of bytes in the backup than
     70 * ArchiveUtils.ARCHIVE_MAX_BYTES_SIZE.
     71 */
     72 add_task(async function test_exceed_size() {
     73  let nonce = new Uint8Array(16);
     74 
     75  // Get us right up to the limit. Increasing by one past this value should
     76  // cause us to throw.
     77  NonceUtils.incrementNonce(
     78    nonce,
     79    ArchiveUtils.ARCHIVE_MAX_BYTES_SIZE /
     80      ArchiveUtils.ARCHIVE_CHUNK_MAX_BYTES_SIZE
     81  );
     82 
     83  Assert.throws(() => {
     84    NonceUtils.incrementNonce(nonce);
     85  }, /Exceeded/);
     86 });