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