services/sync/modules-testing/fakeservices.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/services/sync/modules-testing/fakeservices.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,103 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this file,
     1.6 + * You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +"use strict";
     1.9 +
    1.10 +this.EXPORTED_SYMBOLS = [
    1.11 +  "FakeCryptoService",
    1.12 +  "FakeFilesystemService",
    1.13 +  "FakeGUIDService",
    1.14 +  "fakeSHA256HMAC",
    1.15 +];
    1.16 +
    1.17 +const {utils: Cu} = Components;
    1.18 +
    1.19 +Cu.import("resource://services-sync/record.js");
    1.20 +Cu.import("resource://services-sync/util.js");
    1.21 +
    1.22 +let btoa = Cu.import("resource://gre/modules/Log.jsm").btoa;
    1.23 +
    1.24 +this.FakeFilesystemService = function FakeFilesystemService(contents) {
    1.25 +  this.fakeContents = contents;
    1.26 +  let self = this;
    1.27 +
    1.28 +  Utils.jsonSave = function jsonSave(filePath, that, obj, callback) {
    1.29 +    let json = typeof obj == "function" ? obj.call(that) : obj;
    1.30 +    self.fakeContents["weave/" + filePath + ".json"] = JSON.stringify(json);
    1.31 +    callback.call(that);
    1.32 +  };
    1.33 +
    1.34 +  Utils.jsonLoad = function jsonLoad(filePath, that, cb) {
    1.35 +    let obj;
    1.36 +    let json = self.fakeContents["weave/" + filePath + ".json"];
    1.37 +    if (json) {
    1.38 +      obj = JSON.parse(json);
    1.39 +    }
    1.40 +    cb.call(that, obj);
    1.41 +  };
    1.42 +};
    1.43 +
    1.44 +this.fakeSHA256HMAC = function fakeSHA256HMAC(message) {
    1.45 +   message = message.substr(0, 64);
    1.46 +   while (message.length < 64) {
    1.47 +     message += " ";
    1.48 +   }
    1.49 +   return message;
    1.50 +}
    1.51 +
    1.52 +this.FakeGUIDService = function FakeGUIDService() {
    1.53 +  let latestGUID = 0;
    1.54 +
    1.55 +  Utils.makeGUID = function makeGUID() {
    1.56 +    return "fake-guid-" + latestGUID++;
    1.57 +  };
    1.58 +}
    1.59 +
    1.60 +/*
    1.61 + * Mock implementation of WeaveCrypto. It does not encrypt or
    1.62 + * decrypt, merely returning the input verbatim.
    1.63 + */
    1.64 +this.FakeCryptoService = function FakeCryptoService() {
    1.65 +  this.counter = 0;
    1.66 +
    1.67 +  delete Svc.Crypto;  // get rid of the getter first
    1.68 +  Svc.Crypto = this;
    1.69 +
    1.70 +  CryptoWrapper.prototype.ciphertextHMAC = function ciphertextHMAC(keyBundle) {
    1.71 +    return fakeSHA256HMAC(this.ciphertext);
    1.72 +  };
    1.73 +}
    1.74 +FakeCryptoService.prototype = {
    1.75 +
    1.76 +  encrypt: function encrypt(clearText, symmetricKey, iv) {
    1.77 +    return clearText;
    1.78 +  },
    1.79 +
    1.80 +  decrypt: function decrypt(cipherText, symmetricKey, iv) {
    1.81 +    return cipherText;
    1.82 +  },
    1.83 +
    1.84 +  generateRandomKey: function generateRandomKey() {
    1.85 +    return btoa("fake-symmetric-key-" + this.counter++);
    1.86 +  },
    1.87 +
    1.88 +  generateRandomIV: function generateRandomIV() {
    1.89 +    // A base64-encoded IV is 24 characters long
    1.90 +    return btoa("fake-fake-fake-random-iv");
    1.91 +  },
    1.92 +
    1.93 +  expandData: function expandData(data, len) {
    1.94 +    return data;
    1.95 +  },
    1.96 +
    1.97 +  deriveKeyFromPassphrase: function deriveKeyFromPassphrase(passphrase,
    1.98 +                                                            salt, keyLength) {
    1.99 +    return "some derived key string composed of bytes";
   1.100 +  },
   1.101 +
   1.102 +  generateRandomBytes: function generateRandomBytes(byteCount) {
   1.103 +    return "not-so-random-now-are-we-HA-HA-HA! >:)".slice(byteCount);
   1.104 +  }
   1.105 +};
   1.106 +

mercurial