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

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

mercurial