Bug 1688832: part 5) Add `static` `AccessibleCaretManager::GetSelection`, `::GetFrame...
[gecko.git] / services / sync / modules-testing / fakeservices.js
blobf13ef27c89de1459bafe1e480a5831ed15c30047
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 var EXPORTED_SYMBOLS = [
8   "FakeCryptoService",
9   "FakeFilesystemService",
10   "FakeGUIDService",
11   "fakeSHA256HMAC",
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;
22   let self = this;
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];
35     }
36   }
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);
41   };
43   Utils.jsonLoad = async function jsonLoad(filePath, that) {
44     let obj;
45     let json = self.fakeContents["weave/" + filePath + ".json"];
46     if (json) {
47       obj = JSON.parse(json);
48     }
49     return obj;
50   };
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();
57   };
59   Utils.jsonRemove = function jsonRemove(filePath, that) {
60     delete self.fakeContents["weave/" + filePath + ".json"];
61     return Promise.resolve();
62   };
65 function fakeSHA256HMAC(message) {
66   message = message.substr(0, 64);
67   while (message.length < 64) {
68     message += " ";
69   }
70   return message;
73 function FakeGUIDService() {
74   let latestGUID = 0;
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);
80   };
84  * Mock implementation of WeaveCrypto. It does not encrypt or
85  * decrypt, merely returning the input verbatim.
86  */
87 function FakeCryptoService() {
88   this.counter = 0;
90   delete Weave.Crypto; // get rid of the getter first
91   Weave.Crypto = this;
93   RawCryptoWrapper.prototype.ciphertextHMAC = function ciphertextHMAC(
94     keyBundle
95   ) {
96     return fakeSHA256HMAC(this.ciphertext);
97   };
99 FakeCryptoService.prototype = {
100   async encrypt(clearText, symmetricKey, iv) {
101     return clearText;
102   },
104   async decrypt(cipherText, symmetricKey, iv) {
105     return cipherText;
106   },
108   async generateRandomKey() {
109     return btoa("fake-symmetric-key-" + this.counter++);
110   },
112   generateRandomIV: function generateRandomIV() {
113     // A base64-encoded IV is 24 characters long
114     return btoa("fake-fake-fake-random-iv");
115   },
117   expandData: function expandData(data, len) {
118     return data;
119   },
121   generateRandomBytes: function generateRandomBytes(byteCount) {
122     return "not-so-random-now-are-we-HA-HA-HA! >:)".slice(byteCount);
123   },