tor-browser

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

test_ArchiveEncryptionState.js (3705B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 https://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 const { ArchiveEncryptionState } = ChromeUtils.importESModule(
      7  "resource:///modules/backup/ArchiveEncryptionState.sys.mjs"
      8 );
      9 
     10 const TEST_RECOVERY_CODE = "This is my recovery code.";
     11 
     12 /**
     13 * Tests that we can initialize an ArchiveEncryptionState from scratch, and
     14 * generate all of the necessary information for encrypting a backup.
     15 */
     16 add_task(async function test_ArchiveEncryptionState_enable() {
     17  let { instance: encState, recoveryCode } =
     18    await ArchiveEncryptionState.initialize(TEST_RECOVERY_CODE);
     19 
     20  Assert.equal(recoveryCode, TEST_RECOVERY_CODE, "Got back recovery code.");
     21  Assert.ok(encState.publicKey, "A public key was computed.");
     22  Assert.ok(encState.backupAuthKey, "An auth key was computed.");
     23  Assert.ok(encState.salt, "A salt was computed.");
     24  Assert.ok(encState.nonce, "A nonce was computed.");
     25  Assert.ok(encState.wrappedSecrets, "Wrapped secrets were computed.");
     26 });
     27 
     28 /**
     29 * Tests that an ArchiveEncryptionState can be serialized to an object that
     30 * can be written to disk, and then deserialized back out again.
     31 */
     32 add_task(
     33  async function test_ArchiveEncryptionState_serialization_deserialization() {
     34    let { instance: encState } =
     35      await ArchiveEncryptionState.initialize(TEST_RECOVERY_CODE);
     36    let serialization = await encState.serialize();
     37 
     38    // We'll pretend to write this serialization to disk by stringifying it,
     39    // and then parsing it again.
     40    serialization = JSON.parse(JSON.stringify(serialization));
     41 
     42    Assert.equal(
     43      serialization.version,
     44      ArchiveEncryptionState.VERSION,
     45      "The ArchiveEncryptionState version was included in the serialization."
     46    );
     47 
     48    let { instance: recoveredState } =
     49      await ArchiveEncryptionState.initialize(serialization);
     50 
     51    Assert.deepEqual(
     52      encState.publicKey,
     53      recoveredState.publicKey,
     54      "Public keys match"
     55    );
     56    Assert.deepEqual(
     57      encState.backupAuthKey,
     58      recoveredState.backupAuthKey,
     59      "Auth keys match"
     60    );
     61    Assert.deepEqual(encState.salt, recoveredState.salt, "Salts match");
     62    Assert.deepEqual(encState.nonce, recoveredState.nonce, "Nonces match");
     63    Assert.deepEqual(
     64      encState.wrappedSecrets,
     65      recoveredState.wrappedSecrets,
     66      "Wrapped secrets match"
     67    );
     68  }
     69 );
     70 
     71 /**
     72 * Tests that ArchiveEncryptionState will throw if it attempts to deserialize
     73 * a serialized state from a newer version of ArchiveEncryptionState.
     74 */
     75 add_task(async function test_ArchiveEncryptionState_deserialize_newer() {
     76  let { instance: encState } =
     77    await ArchiveEncryptionState.initialize(TEST_RECOVERY_CODE);
     78  let serialization = await encState.serialize();
     79 
     80  // We'll pretend to write this serialization to disk by stringifying it,
     81  // and then parsing it again.
     82  serialization = JSON.parse(JSON.stringify(serialization));
     83 
     84  // Poke in a version that's greater than the one that ArchiveEncryptionState
     85  // currently knows how to deal with.
     86  serialization.version = ArchiveEncryptionState.VERSION + 1;
     87 
     88  await Assert.rejects(
     89    ArchiveEncryptionState.initialize(serialization),
     90    /newer version/,
     91    "Should have thrown when deserializing from a newer version"
     92  );
     93 });
     94 
     95 /**
     96 * Tests that if no recovery code is passed to enable(), that one is generated
     97 * and returned instead.
     98 */
     99 add_task(async function test_ArchiveEncryptionState_generate_recoveryCode() {
    100  let { recoveryCode } = await ArchiveEncryptionState.initialize();
    101 
    102  Assert.equal(
    103    recoveryCode.length,
    104    ArchiveEncryptionState.GENERATED_RECOVERY_CODE_LENGTH,
    105    "The generated recovery code has the right length."
    106  );
    107 });