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/. */
7 var EXPORTED_SYMBOLS = [
9 "FakeFilesystemService",
14 const { Weave } = ChromeUtils.import("resource://services-sync/main.js");
15 const { RawCryptoWrapper } = ChromeUtils.import(
16 "resource://services-sync/record.js"
18 const { Utils } = ChromeUtils.import("resource://services-sync/util.js");
20 function FakeFilesystemService(contents) {
21 this.fakeContents = contents;
24 // Save away the unmocked versions of the functions we replace here for tests
25 // that really want the originals. As this may be called many times per test,
26 // we must be careful to not replace them with ones we previously replaced.
27 // (And why are we bothering with these mocks in the first place? Is the
28 // performance of the filesystem *really* such that it outweighs the downside
29 // of not running our real JSON functions in the tests? Eg, these mocks don't
30 // always throw exceptions when the real ones do. Anyway...)
31 for (let name of ["jsonSave", "jsonLoad", "jsonMove", "jsonRemove"]) {
32 let origName = "_real_" + name;
33 if (!Utils[origName]) {
34 Utils[origName] = Utils[name];
38 Utils.jsonSave = async function jsonSave(filePath, that, obj) {
39 let json = typeof obj == "function" ? obj.call(that) : obj;
40 self.fakeContents["weave/" + filePath + ".json"] = JSON.stringify(json);
43 Utils.jsonLoad = async function jsonLoad(filePath, that) {
45 let json = self.fakeContents["weave/" + filePath + ".json"];
47 obj = JSON.parse(json);
52 Utils.jsonMove = function jsonMove(aFrom, aTo, that) {
53 const fromPath = "weave/" + aFrom + ".json";
54 self.fakeContents["weave/" + aTo + ".json"] = self.fakeContents[fromPath];
55 delete self.fakeContents[fromPath];
56 return Promise.resolve();
59 Utils.jsonRemove = function jsonRemove(filePath, that) {
60 delete self.fakeContents["weave/" + filePath + ".json"];
61 return Promise.resolve();
65 function fakeSHA256HMAC(message) {
66 message = message.substr(0, 64);
67 while (message.length < 64) {
73 function FakeGUIDService() {
76 Utils.makeGUID = function makeGUID() {
77 // ensure that this always returns a unique 12 character string
78 let nextGUID = "fake-guid-" + String(latestGUID++).padStart(2, "0");
79 return nextGUID.slice(nextGUID.length - 12, nextGUID.length);
84 * Mock implementation of WeaveCrypto. It does not encrypt or
85 * decrypt, merely returning the input verbatim.
87 function FakeCryptoService() {
90 delete Weave.Crypto; // get rid of the getter first
93 RawCryptoWrapper.prototype.ciphertextHMAC = function ciphertextHMAC(
96 return fakeSHA256HMAC(this.ciphertext);
99 FakeCryptoService.prototype = {
100 async encrypt(clearText, symmetricKey, iv) {
104 async decrypt(cipherText, symmetricKey, iv) {
108 async generateRandomKey() {
109 return btoa("fake-symmetric-key-" + this.counter++);
112 generateRandomIV: function generateRandomIV() {
113 // A base64-encoded IV is 24 characters long
114 return btoa("fake-fake-fake-random-iv");
117 expandData: function expandData(data, len) {
121 generateRandomBytes: function generateRandomBytes(byteCount) {
122 return "not-so-random-now-are-we-HA-HA-HA! >:)".slice(byteCount);