services/sync/modules-testing/fakeservices.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     1 /* This Source Code Form is subject to the terms of the Mozilla Public
     2  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
     3  * You can obtain one at http://mozilla.org/MPL/2.0/. */
     5 "use strict";
     7 this.EXPORTED_SYMBOLS = [
     8   "FakeCryptoService",
     9   "FakeFilesystemService",
    10   "FakeGUIDService",
    11   "fakeSHA256HMAC",
    12 ];
    14 const {utils: Cu} = Components;
    16 Cu.import("resource://services-sync/record.js");
    17 Cu.import("resource://services-sync/util.js");
    19 let btoa = Cu.import("resource://gre/modules/Log.jsm").btoa;
    21 this.FakeFilesystemService = function FakeFilesystemService(contents) {
    22   this.fakeContents = contents;
    23   let self = this;
    25   Utils.jsonSave = function jsonSave(filePath, that, obj, callback) {
    26     let json = typeof obj == "function" ? obj.call(that) : obj;
    27     self.fakeContents["weave/" + filePath + ".json"] = JSON.stringify(json);
    28     callback.call(that);
    29   };
    31   Utils.jsonLoad = function jsonLoad(filePath, that, cb) {
    32     let obj;
    33     let json = self.fakeContents["weave/" + filePath + ".json"];
    34     if (json) {
    35       obj = JSON.parse(json);
    36     }
    37     cb.call(that, obj);
    38   };
    39 };
    41 this.fakeSHA256HMAC = function fakeSHA256HMAC(message) {
    42    message = message.substr(0, 64);
    43    while (message.length < 64) {
    44      message += " ";
    45    }
    46    return message;
    47 }
    49 this.FakeGUIDService = function FakeGUIDService() {
    50   let latestGUID = 0;
    52   Utils.makeGUID = function makeGUID() {
    53     return "fake-guid-" + latestGUID++;
    54   };
    55 }
    57 /*
    58  * Mock implementation of WeaveCrypto. It does not encrypt or
    59  * decrypt, merely returning the input verbatim.
    60  */
    61 this.FakeCryptoService = function FakeCryptoService() {
    62   this.counter = 0;
    64   delete Svc.Crypto;  // get rid of the getter first
    65   Svc.Crypto = this;
    67   CryptoWrapper.prototype.ciphertextHMAC = function ciphertextHMAC(keyBundle) {
    68     return fakeSHA256HMAC(this.ciphertext);
    69   };
    70 }
    71 FakeCryptoService.prototype = {
    73   encrypt: function encrypt(clearText, symmetricKey, iv) {
    74     return clearText;
    75   },
    77   decrypt: function decrypt(cipherText, symmetricKey, iv) {
    78     return cipherText;
    79   },
    81   generateRandomKey: function generateRandomKey() {
    82     return btoa("fake-symmetric-key-" + this.counter++);
    83   },
    85   generateRandomIV: function generateRandomIV() {
    86     // A base64-encoded IV is 24 characters long
    87     return btoa("fake-fake-fake-random-iv");
    88   },
    90   expandData: function expandData(data, len) {
    91     return data;
    92   },
    94   deriveKeyFromPassphrase: function deriveKeyFromPassphrase(passphrase,
    95                                                             salt, keyLength) {
    96     return "some derived key string composed of bytes";
    97   },
    99   generateRandomBytes: function generateRandomBytes(byteCount) {
   100     return "not-so-random-now-are-we-HA-HA-HA! >:)".slice(byteCount);
   101   }
   102 };

mercurial