fakeservices.sys.mjs (3508B)
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/. */ 4 5 import { Weave } from "resource://services-sync/main.sys.mjs"; 6 import { RawCryptoWrapper } from "resource://services-sync/record.sys.mjs"; 7 import { Utils } from "resource://services-sync/util.sys.mjs"; 8 9 export function FakeFilesystemService(contents) { 10 this.fakeContents = contents; 11 let self = this; 12 13 // Save away the unmocked versions of the functions we replace here for tests 14 // that really want the originals. As this may be called many times per test, 15 // we must be careful to not replace them with ones we previously replaced. 16 // (And why are we bothering with these mocks in the first place? Is the 17 // performance of the filesystem *really* such that it outweighs the downside 18 // of not running our real JSON functions in the tests? Eg, these mocks don't 19 // always throw exceptions when the real ones do. Anyway...) 20 for (let name of ["jsonSave", "jsonLoad", "jsonMove", "jsonRemove"]) { 21 let origName = "_real_" + name; 22 if (!Utils[origName]) { 23 Utils[origName] = Utils[name]; 24 } 25 } 26 27 Utils.jsonSave = async function jsonSave(filePath, that, obj) { 28 let json = typeof obj == "function" ? obj.call(that) : obj; 29 self.fakeContents["weave/" + filePath + ".json"] = JSON.stringify(json); 30 }; 31 32 Utils.jsonLoad = async function jsonLoad(filePath) { 33 let obj; 34 let json = self.fakeContents["weave/" + filePath + ".json"]; 35 if (json) { 36 obj = JSON.parse(json); 37 } 38 return obj; 39 }; 40 41 Utils.jsonMove = function jsonMove(aFrom, aTo) { 42 const fromPath = "weave/" + aFrom + ".json"; 43 self.fakeContents["weave/" + aTo + ".json"] = self.fakeContents[fromPath]; 44 delete self.fakeContents[fromPath]; 45 return Promise.resolve(); 46 }; 47 48 Utils.jsonRemove = function jsonRemove(filePath) { 49 delete self.fakeContents["weave/" + filePath + ".json"]; 50 return Promise.resolve(); 51 }; 52 } 53 54 export function fakeSHA256HMAC(message) { 55 message = message.substr(0, 64); 56 while (message.length < 64) { 57 message += " "; 58 } 59 return message; 60 } 61 62 export function FakeGUIDService() { 63 let latestGUID = 0; 64 65 Utils.makeGUID = function makeGUID() { 66 // ensure that this always returns a unique 12 character string 67 let nextGUID = "fake-guid-" + String(latestGUID++).padStart(2, "0"); 68 return nextGUID.slice(nextGUID.length - 12, nextGUID.length); 69 }; 70 } 71 72 /* 73 * Mock implementation of WeaveCrypto. It does not encrypt or 74 * decrypt, merely returning the input verbatim. 75 */ 76 export function FakeCryptoService() { 77 this.counter = 0; 78 79 delete Weave.Crypto; // get rid of the getter first 80 Weave.Crypto = this; 81 82 RawCryptoWrapper.prototype.ciphertextHMAC = function ciphertextHMAC() { 83 return fakeSHA256HMAC(this.ciphertext); 84 }; 85 } 86 87 FakeCryptoService.prototype = { 88 async encrypt(clearText) { 89 return clearText; 90 }, 91 92 async decrypt(cipherText) { 93 return cipherText; 94 }, 95 96 async generateRandomKey() { 97 return btoa("fake-symmetric-key-" + this.counter++); 98 }, 99 100 generateRandomIV: function generateRandomIV() { 101 // A base64-encoded IV is 24 characters long 102 return btoa("fake-fake-fake-random-iv"); 103 }, 104 105 expandData: function expandData(data) { 106 return data; 107 }, 108 109 generateRandomBytes: function generateRandomBytes(byteCount) { 110 return "not-so-random-now-are-we-HA-HA-HA! >:)".slice(byteCount); 111 }, 112 };