2011-01-29 00:21:21 +03:00
|
|
|
Cu.import("resource://services-sync/util.js");
|
2011-04-19 23:35:04 +04:00
|
|
|
Cu.import("resource://services-sync/record.js");
|
2011-06-14 20:31:31 +04:00
|
|
|
Cu.import("resource://services-sync/engines.js");
|
2010-11-30 03:41:17 +03:00
|
|
|
var btoa;
|
|
|
|
|
2008-05-01 00:01:17 +04:00
|
|
|
let provider = {
|
|
|
|
getFile: function(prop, persistent) {
|
|
|
|
persistent.value = true;
|
2010-08-09 20:38:18 +04:00
|
|
|
switch (prop) {
|
|
|
|
case "ExtPrefDL":
|
2011-05-21 05:45:51 +04:00
|
|
|
return [Services.dirsvc.get("CurProcD", Ci.nsIFile)];
|
2010-08-09 20:38:18 +04:00
|
|
|
default:
|
|
|
|
throw Cr.NS_ERROR_FAILURE;
|
|
|
|
}
|
2008-05-01 00:01:17 +04:00
|
|
|
},
|
2011-05-21 05:45:51 +04:00
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.nsIDirectoryServiceProvider])
|
2008-06-17 03:42:32 +04:00
|
|
|
};
|
2011-05-21 05:45:51 +04:00
|
|
|
Services.dirsvc.QueryInterface(Ci.nsIDirectoryService).registerProvider(provider);
|
2008-06-17 03:42:32 +04:00
|
|
|
|
2010-11-30 03:41:17 +03:00
|
|
|
btoa = Cu.import("resource://services-sync/log4moz.js").btoa;
|
2008-06-20 04:04:04 +04:00
|
|
|
function getTestLogger(component) {
|
2008-11-04 01:41:39 +03:00
|
|
|
return Log4Moz.repository.getLogger("Testing");
|
2008-06-20 04:04:04 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
function initTestLogging(level) {
|
2008-06-17 03:22:00 +04:00
|
|
|
function LogStats() {
|
|
|
|
this.errorsLogged = 0;
|
|
|
|
}
|
|
|
|
LogStats.prototype = {
|
2008-06-10 07:51:23 +04:00
|
|
|
format: function BF_format(message) {
|
|
|
|
if (message.level == Log4Moz.Level.Error)
|
2008-06-17 03:22:00 +04:00
|
|
|
this.errorsLogged += 1;
|
2008-06-10 07:51:23 +04:00
|
|
|
return message.loggerName + "\t" + message.levelDesc + "\t" +
|
|
|
|
message.message + "\n";
|
|
|
|
}
|
|
|
|
};
|
2008-06-17 03:22:00 +04:00
|
|
|
LogStats.prototype.__proto__ = new Log4Moz.Formatter();
|
2008-06-10 07:51:23 +04:00
|
|
|
|
2008-11-04 01:41:39 +03:00
|
|
|
var log = Log4Moz.repository.rootLogger;
|
2008-06-17 03:22:00 +04:00
|
|
|
var logStats = new LogStats();
|
|
|
|
var appender = new Log4Moz.DumpAppender(logStats);
|
2008-06-20 04:04:04 +04:00
|
|
|
|
|
|
|
if (typeof(level) == "undefined")
|
|
|
|
level = "Debug";
|
|
|
|
getTestLogger().level = Log4Moz.Level[level];
|
|
|
|
|
|
|
|
log.level = Log4Moz.Level.Trace;
|
|
|
|
appender.level = Log4Moz.Level.Trace;
|
2010-06-02 02:07:50 +04:00
|
|
|
// Overwrite any other appenders (e.g. from previous incarnations)
|
2011-02-26 01:42:59 +03:00
|
|
|
log.ownAppenders = [appender];
|
|
|
|
log.updateAppenders();
|
2008-06-10 07:51:23 +04:00
|
|
|
|
2008-06-17 03:22:00 +04:00
|
|
|
return logStats;
|
|
|
|
}
|
|
|
|
|
2008-06-21 04:47:32 +04:00
|
|
|
function FakeFilesystemService(contents) {
|
|
|
|
this.fakeContents = contents;
|
|
|
|
let self = this;
|
|
|
|
|
2011-01-27 08:43:33 +03:00
|
|
|
Utils.jsonSave = function jsonSave(filePath, that, obj, callback) {
|
|
|
|
let json = typeof obj == "function" ? obj.call(that) : obj;
|
|
|
|
self.fakeContents["weave/" + filePath + ".json"] = JSON.stringify(json);
|
|
|
|
callback.call(that);
|
2008-06-21 04:47:32 +04:00
|
|
|
};
|
|
|
|
|
2011-01-27 08:43:33 +03:00
|
|
|
Utils.jsonLoad = function jsonLoad(filePath, that, callback) {
|
|
|
|
let obj;
|
|
|
|
let json = self.fakeContents["weave/" + filePath + ".json"];
|
|
|
|
if (json) {
|
|
|
|
obj = JSON.parse(json);
|
2008-06-21 04:47:32 +04:00
|
|
|
}
|
2011-01-27 08:43:33 +03:00
|
|
|
callback.call(that, obj);
|
2008-06-21 04:47:32 +04:00
|
|
|
};
|
|
|
|
};
|
2008-06-24 01:13:46 +04:00
|
|
|
|
|
|
|
function FakeGUIDService() {
|
|
|
|
let latestGUID = 0;
|
|
|
|
|
|
|
|
Utils.makeGUID = function fake_makeGUID() {
|
|
|
|
return "fake-guid-" + latestGUID++;
|
|
|
|
};
|
|
|
|
}
|
2008-06-24 07:57:10 +04:00
|
|
|
|
2010-06-02 02:07:50 +04:00
|
|
|
|
|
|
|
/*
|
2011-04-21 01:27:19 +04:00
|
|
|
* Mock implementation of WeaveCrypto. It does not encrypt or
|
|
|
|
* decrypt, merely returning the input verbatim.
|
2010-06-02 02:07:50 +04:00
|
|
|
*/
|
|
|
|
function FakeCryptoService() {
|
|
|
|
this.counter = 0;
|
|
|
|
|
|
|
|
delete Svc.Crypto; // get rid of the getter first
|
|
|
|
Svc.Crypto = this;
|
|
|
|
Utils.sha256HMAC = this.sha256HMAC;
|
2011-03-02 01:29:41 +03:00
|
|
|
|
|
|
|
CryptoWrapper.prototype.ciphertextHMAC = this.ciphertextHMAC;
|
2010-06-02 02:07:50 +04:00
|
|
|
}
|
|
|
|
FakeCryptoService.prototype = {
|
|
|
|
|
2011-03-02 01:29:41 +03:00
|
|
|
sha256HMAC: function Utils_sha256HMAC(message, hasher) {
|
2010-06-02 02:07:50 +04:00
|
|
|
message = message.substr(0, 64);
|
|
|
|
while (message.length < 64) {
|
|
|
|
message += " ";
|
|
|
|
}
|
|
|
|
return message;
|
|
|
|
},
|
|
|
|
|
2011-03-02 01:29:41 +03:00
|
|
|
ciphertextHMAC: function CryptoWrapper_ciphertextHMAC(keyBundle) {
|
|
|
|
return Utils.sha256HMAC(this.ciphertext);
|
|
|
|
},
|
|
|
|
|
2010-06-02 02:07:50 +04:00
|
|
|
encrypt: function(aClearText, aSymmetricKey, aIV) {
|
|
|
|
return aClearText;
|
|
|
|
},
|
|
|
|
|
|
|
|
decrypt: function(aCipherText, aSymmetricKey, aIV) {
|
|
|
|
return aCipherText;
|
|
|
|
},
|
|
|
|
|
|
|
|
generateRandomKey: function() {
|
2010-11-30 03:41:17 +03:00
|
|
|
return btoa("fake-symmetric-key-" + this.counter++);
|
2010-06-02 02:07:50 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
generateRandomIV: function() {
|
2010-06-23 14:36:48 +04:00
|
|
|
// A base64-encoded IV is 24 characters long
|
2010-11-30 03:41:17 +03:00
|
|
|
return btoa("fake-fake-fake-random-iv");
|
2010-06-02 02:07:50 +04:00
|
|
|
},
|
|
|
|
|
2010-11-30 03:41:17 +03:00
|
|
|
expandData : function expandData(data, len) {
|
|
|
|
return data;
|
2010-06-02 02:07:50 +04:00
|
|
|
},
|
|
|
|
|
2010-11-30 03:41:17 +03:00
|
|
|
deriveKeyFromPassphrase : function (passphrase, salt, keyLength) {
|
|
|
|
return "some derived key string composed of bytes";
|
2010-06-02 02:07:50 +04:00
|
|
|
},
|
|
|
|
|
2010-11-30 03:41:17 +03:00
|
|
|
generateRandomBytes: function(aByteCount) {
|
|
|
|
return "not-so-random-now-are-we-HA-HA-HA! >:)".slice(aByteCount);
|
2011-05-21 05:45:51 +04:00
|
|
|
}
|
2010-06-02 02:07:50 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2011-05-21 05:45:51 +04:00
|
|
|
function SyncTestingInfrastructure() {
|
2010-06-17 01:30:08 +04:00
|
|
|
Cu.import("resource://services-sync/identity.js");
|
2008-06-24 07:57:10 +04:00
|
|
|
|
|
|
|
ID.set('WeaveID',
|
|
|
|
new Identity('Mozilla Services Encryption Passphrase', 'foo'));
|
2008-06-30 22:54:10 +04:00
|
|
|
ID.set('WeaveCryptoID',
|
|
|
|
new Identity('Mozilla Services Encryption Passphrase', 'foo'));
|
2008-06-24 07:57:10 +04:00
|
|
|
|
|
|
|
this.logStats = initTestLogging();
|
|
|
|
this.fakeFilesystem = new FakeFilesystemService({});
|
|
|
|
this.fakeGUIDService = new FakeGUIDService();
|
2010-06-02 02:07:50 +04:00
|
|
|
this.fakeCryptoService = new FakeCryptoService();
|
2008-06-24 07:57:10 +04:00
|
|
|
}
|
2009-08-20 05:01:06 +04:00
|
|
|
|
2011-01-27 08:43:24 +03:00
|
|
|
/*
|
|
|
|
* Ensure exceptions from inside callbacks leads to test failures.
|
|
|
|
*/
|
|
|
|
function ensureThrows(func) {
|
|
|
|
return function() {
|
|
|
|
try {
|
|
|
|
func.apply(this, arguments);
|
|
|
|
} catch (ex) {
|
|
|
|
do_throw(ex);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-20 05:01:06 +04:00
|
|
|
/**
|
|
|
|
* Print some debug message to the console. All arguments will be printed,
|
|
|
|
* separated by spaces.
|
|
|
|
*
|
|
|
|
* @param [arg0, arg1, arg2, ...]
|
|
|
|
* Any number of arguments to print out
|
|
|
|
* @usage _("Hello World") -> prints "Hello World"
|
|
|
|
* @usage _(1, 2, 3) -> prints "1 2 3"
|
|
|
|
*/
|
|
|
|
let _ = function(some, debug, text, to) print(Array.slice(arguments).join(" "));
|
2010-03-26 05:23:44 +03:00
|
|
|
|
|
|
|
_("Setting the identity for passphrase");
|
2010-06-17 01:30:08 +04:00
|
|
|
Cu.import("resource://services-sync/identity.js");
|
2010-03-26 05:23:44 +03:00
|
|
|
|
2010-12-07 04:25:35 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Test setup helpers.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Turn WBO cleartext into "encrypted" payload as it goes over the wire
|
|
|
|
function encryptPayload(cleartext) {
|
|
|
|
if (typeof cleartext == "object") {
|
|
|
|
cleartext = JSON.stringify(cleartext);
|
|
|
|
}
|
|
|
|
|
|
|
|
return {ciphertext: cleartext, // ciphertext == cleartext with fake crypto
|
|
|
|
IV: "irrelevant",
|
|
|
|
hmac: Utils.sha256HMAC(cleartext, Utils.makeHMACKey(""))};
|
|
|
|
}
|
|
|
|
|
2011-04-19 23:35:04 +04:00
|
|
|
function generateNewKeys(collections) {
|
|
|
|
let wbo = CollectionKeys.generateNewKeysWBO(collections);
|
|
|
|
let modified = new_timestamp();
|
|
|
|
CollectionKeys.setContents(wbo.cleartext, modified);
|
|
|
|
}
|
|
|
|
|
2011-04-26 16:25:27 +04:00
|
|
|
function do_check_throws(aFunc, aResult, aStack)
|
|
|
|
{
|
|
|
|
if (!aStack) {
|
|
|
|
try {
|
|
|
|
// We might not have a 'Components' object.
|
|
|
|
aStack = Components.stack.caller;
|
|
|
|
} catch (e) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
aFunc();
|
|
|
|
} catch (e) {
|
|
|
|
do_check_eq(e.result, aResult, aStack);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
do_throw("Expected result " + aResult + ", none thrown.", aStack);
|
|
|
|
}
|
2011-06-14 20:31:31 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* A fake engine implementation.
|
|
|
|
* This is used all over the place.
|
|
|
|
*
|
|
|
|
* Complete with record, store, and tracker implementations.
|
|
|
|
*/
|
|
|
|
|
|
|
|
function RotaryRecord(collection, id) {
|
|
|
|
CryptoWrapper.call(this, collection, id);
|
|
|
|
}
|
|
|
|
RotaryRecord.prototype = {
|
|
|
|
__proto__: CryptoWrapper.prototype
|
|
|
|
};
|
|
|
|
Utils.deferGetSet(RotaryRecord, "cleartext", ["denomination"]);
|
|
|
|
|
|
|
|
function RotaryStore() {
|
|
|
|
Store.call(this, "Rotary");
|
|
|
|
this.items = {};
|
|
|
|
}
|
|
|
|
RotaryStore.prototype = {
|
|
|
|
__proto__: Store.prototype,
|
|
|
|
|
|
|
|
create: function Store_create(record) {
|
|
|
|
this.items[record.id] = record.denomination;
|
|
|
|
},
|
|
|
|
|
|
|
|
remove: function Store_remove(record) {
|
|
|
|
delete this.items[record.id];
|
|
|
|
},
|
|
|
|
|
|
|
|
update: function Store_update(record) {
|
|
|
|
this.items[record.id] = record.denomination;
|
|
|
|
},
|
|
|
|
|
|
|
|
itemExists: function Store_itemExists(id) {
|
|
|
|
return (id in this.items);
|
|
|
|
},
|
|
|
|
|
|
|
|
createRecord: function(id, collection) {
|
|
|
|
let record = new RotaryRecord(collection, id);
|
|
|
|
record.denomination = this.items[id] || "Data for new record: " + id;
|
|
|
|
return record;
|
|
|
|
},
|
|
|
|
|
|
|
|
changeItemID: function(oldID, newID) {
|
|
|
|
this.items[newID] = this.items[oldID];
|
|
|
|
delete this.items[oldID];
|
|
|
|
},
|
|
|
|
|
|
|
|
getAllIDs: function() {
|
|
|
|
let ids = {};
|
|
|
|
for (let id in this.items) {
|
|
|
|
ids[id] = true;
|
|
|
|
}
|
|
|
|
return ids;
|
|
|
|
},
|
|
|
|
|
|
|
|
wipe: function() {
|
|
|
|
this.items = {};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
function RotaryTracker() {
|
|
|
|
Tracker.call(this, "Rotary");
|
|
|
|
}
|
|
|
|
RotaryTracker.prototype = {
|
|
|
|
__proto__: Tracker.prototype
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
function RotaryEngine() {
|
|
|
|
SyncEngine.call(this, "Rotary");
|
2011-06-27 19:00:27 +04:00
|
|
|
// Ensure that the engine starts with a clean slate.
|
|
|
|
this.toFetch = [];
|
|
|
|
this.previousFailed = [];
|
2011-06-14 20:31:31 +04:00
|
|
|
}
|
|
|
|
RotaryEngine.prototype = {
|
|
|
|
__proto__: SyncEngine.prototype,
|
|
|
|
_storeObj: RotaryStore,
|
|
|
|
_trackerObj: RotaryTracker,
|
|
|
|
_recordObj: RotaryRecord,
|
|
|
|
|
|
|
|
_findDupe: function(item) {
|
|
|
|
for (let [id, value] in Iterator(this._store.items)) {
|
|
|
|
if (item.denomination == value) {
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|