michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: "use strict"; michael@0: michael@0: this.EXPORTED_SYMBOLS = [ michael@0: "FakeCryptoService", michael@0: "FakeFilesystemService", michael@0: "FakeGUIDService", michael@0: "fakeSHA256HMAC", michael@0: ]; michael@0: michael@0: const {utils: Cu} = Components; michael@0: michael@0: Cu.import("resource://services-sync/record.js"); michael@0: Cu.import("resource://services-sync/util.js"); michael@0: michael@0: let btoa = Cu.import("resource://gre/modules/Log.jsm").btoa; michael@0: michael@0: this.FakeFilesystemService = function FakeFilesystemService(contents) { michael@0: this.fakeContents = contents; michael@0: let self = this; michael@0: michael@0: Utils.jsonSave = function jsonSave(filePath, that, obj, callback) { michael@0: let json = typeof obj == "function" ? obj.call(that) : obj; michael@0: self.fakeContents["weave/" + filePath + ".json"] = JSON.stringify(json); michael@0: callback.call(that); michael@0: }; michael@0: michael@0: Utils.jsonLoad = function jsonLoad(filePath, that, cb) { michael@0: let obj; michael@0: let json = self.fakeContents["weave/" + filePath + ".json"]; michael@0: if (json) { michael@0: obj = JSON.parse(json); michael@0: } michael@0: cb.call(that, obj); michael@0: }; michael@0: }; michael@0: michael@0: this.fakeSHA256HMAC = function fakeSHA256HMAC(message) { michael@0: message = message.substr(0, 64); michael@0: while (message.length < 64) { michael@0: message += " "; michael@0: } michael@0: return message; michael@0: } michael@0: michael@0: this.FakeGUIDService = function FakeGUIDService() { michael@0: let latestGUID = 0; michael@0: michael@0: Utils.makeGUID = function makeGUID() { michael@0: return "fake-guid-" + latestGUID++; michael@0: }; michael@0: } michael@0: michael@0: /* michael@0: * Mock implementation of WeaveCrypto. It does not encrypt or michael@0: * decrypt, merely returning the input verbatim. michael@0: */ michael@0: this.FakeCryptoService = function FakeCryptoService() { michael@0: this.counter = 0; michael@0: michael@0: delete Svc.Crypto; // get rid of the getter first michael@0: Svc.Crypto = this; michael@0: michael@0: CryptoWrapper.prototype.ciphertextHMAC = function ciphertextHMAC(keyBundle) { michael@0: return fakeSHA256HMAC(this.ciphertext); michael@0: }; michael@0: } michael@0: FakeCryptoService.prototype = { michael@0: michael@0: encrypt: function encrypt(clearText, symmetricKey, iv) { michael@0: return clearText; michael@0: }, michael@0: michael@0: decrypt: function decrypt(cipherText, symmetricKey, iv) { michael@0: return cipherText; michael@0: }, michael@0: michael@0: generateRandomKey: function generateRandomKey() { michael@0: return btoa("fake-symmetric-key-" + this.counter++); michael@0: }, michael@0: michael@0: generateRandomIV: function generateRandomIV() { michael@0: // A base64-encoded IV is 24 characters long michael@0: return btoa("fake-fake-fake-random-iv"); michael@0: }, michael@0: michael@0: expandData: function expandData(data, len) { michael@0: return data; michael@0: }, michael@0: michael@0: deriveKeyFromPassphrase: function deriveKeyFromPassphrase(passphrase, michael@0: salt, keyLength) { michael@0: return "some derived key string composed of bytes"; michael@0: }, michael@0: michael@0: generateRandomBytes: function generateRandomBytes(byteCount) { michael@0: return "not-so-random-now-are-we-HA-HA-HA! >:)".slice(byteCount); michael@0: } michael@0: }; michael@0: