2011-12-14 17:10:34 -08:00
|
|
|
/* Any copyright is dedicated to the Public Domain.
|
|
|
|
http://creativecommons.org/publicdomain/zero/1.0/ */
|
|
|
|
|
2012-04-05 23:26:06 -07:00
|
|
|
Cu.import("resource://services-common/utils.js");
|
|
|
|
Cu.import("resource://services-common/async.js");
|
2012-03-22 15:49:50 -07:00
|
|
|
Cu.import("resource://services-sync/identity.js");
|
2011-01-28 13:21:21 -08:00
|
|
|
Cu.import("resource://services-sync/util.js");
|
2011-04-19 12:35:04 -07:00
|
|
|
Cu.import("resource://services-sync/record.js");
|
2011-06-14 09:31:31 -07:00
|
|
|
Cu.import("resource://services-sync/engines.js");
|
2012-02-27 17:52:48 -08:00
|
|
|
let btoa;
|
|
|
|
let atob;
|
2010-11-29 16:41:17 -08:00
|
|
|
|
2008-04-30 13:01:17 -07:00
|
|
|
let provider = {
|
|
|
|
getFile: function(prop, persistent) {
|
|
|
|
persistent.value = true;
|
2010-08-09 09:38:18 -07:00
|
|
|
switch (prop) {
|
|
|
|
case "ExtPrefDL":
|
2011-05-20 18:45:51 -07:00
|
|
|
return [Services.dirsvc.get("CurProcD", Ci.nsIFile)];
|
2010-08-09 09:38:18 -07:00
|
|
|
default:
|
|
|
|
throw Cr.NS_ERROR_FAILURE;
|
|
|
|
}
|
2008-04-30 13:01:17 -07:00
|
|
|
},
|
2011-05-20 18:45:51 -07:00
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.nsIDirectoryServiceProvider])
|
2008-06-16 16:42:32 -07:00
|
|
|
};
|
2011-05-20 18:45:51 -07:00
|
|
|
Services.dirsvc.QueryInterface(Ci.nsIDirectoryService).registerProvider(provider);
|
2008-06-16 16:42:32 -07:00
|
|
|
|
2011-10-27 22:25:01 -07:00
|
|
|
let timer;
|
|
|
|
function waitForZeroTimer(callback) {
|
|
|
|
// First wait >100ms (nsITimers can take up to that much time to fire, so
|
|
|
|
// we can account for the timer in delayedAutoconnect) and then two event
|
|
|
|
// loop ticks (to account for the Utils.nextTick() in autoConnect).
|
|
|
|
let ticks = 2;
|
|
|
|
function wait() {
|
|
|
|
if (ticks) {
|
|
|
|
ticks -= 1;
|
|
|
|
Utils.nextTick(wait);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
timer = Utils.namedTimer(wait, 150, {}, "timer");
|
|
|
|
}
|
|
|
|
|
2012-04-05 23:26:06 -07:00
|
|
|
btoa = Cu.import("resource://services-common/log4moz.js").btoa;
|
|
|
|
atob = Cu.import("resource://services-common/log4moz.js").atob;
|
2008-06-16 16:22:00 -07:00
|
|
|
|
2011-12-14 17:10:34 -08:00
|
|
|
// This is needed for loadAddonTestFunctions().
|
|
|
|
let gGlobalScope = this;
|
|
|
|
|
|
|
|
function ExtensionsTestPath(path) {
|
|
|
|
if (path[0] != "/") {
|
|
|
|
throw Error("Path must begin with '/': " + path);
|
|
|
|
}
|
|
|
|
|
|
|
|
return "../../../../toolkit/mozapps/extensions/test/xpcshell" + path;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Loads the AddonManager test functions by importing its test file.
|
|
|
|
*
|
|
|
|
* This should be called in the global scope of any test file needing to
|
|
|
|
* interface with the AddonManager. It should only be called once, or the
|
|
|
|
* universe will end.
|
|
|
|
*/
|
|
|
|
function loadAddonTestFunctions() {
|
|
|
|
const path = ExtensionsTestPath("/head_addons.js");
|
|
|
|
let file = do_get_file(path);
|
|
|
|
let uri = Services.io.newFileURI(file);
|
|
|
|
Services.scriptloader.loadSubScript(uri.spec, gGlobalScope);
|
|
|
|
createAppInfo("xpcshell@tests.mozilla.org", "XPCShell", "1", "1.9.2");
|
|
|
|
}
|
|
|
|
|
|
|
|
function getAddonInstall(name) {
|
|
|
|
let f = do_get_file(ExtensionsTestPath("/addons/" + name + ".xpi"));
|
|
|
|
let cb = Async.makeSyncCallback();
|
|
|
|
AddonManager.getInstallForFile(f, cb);
|
|
|
|
|
|
|
|
return Async.waitForSyncCallback(cb);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Obtains an addon from the add-on manager by id.
|
|
|
|
*
|
|
|
|
* This is merely a synchronous wrapper.
|
|
|
|
*
|
|
|
|
* @param id
|
|
|
|
* ID of add-on to fetch
|
|
|
|
* @return addon object on success or undefined or null on failure
|
|
|
|
*/
|
|
|
|
function getAddonFromAddonManagerByID(id) {
|
|
|
|
let cb = Async.makeSyncCallback();
|
|
|
|
AddonManager.getAddonByID(id, cb);
|
|
|
|
return Async.waitForSyncCallback(cb);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Installs an add-on synchronously from an addonInstall
|
|
|
|
*
|
|
|
|
* @param install addonInstall instance to install
|
|
|
|
*/
|
|
|
|
function installAddonFromInstall(install) {
|
|
|
|
let cb = Async.makeSyncCallback();
|
|
|
|
let listener = {onInstallEnded: cb};
|
|
|
|
AddonManager.addInstallListener(listener);
|
|
|
|
install.install();
|
|
|
|
Async.waitForSyncCallback(cb);
|
|
|
|
AddonManager.removeAddonListener(listener);
|
|
|
|
|
|
|
|
do_check_neq(null, install.addon);
|
|
|
|
do_check_neq(null, install.addon.syncGUID);
|
|
|
|
|
|
|
|
return install.addon;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convenience function to install an add-on from the extensions unit tests.
|
|
|
|
*
|
|
|
|
* @param name
|
|
|
|
* String name of add-on to install. e.g. test_install1
|
|
|
|
* @return addon object that was installed
|
|
|
|
*/
|
|
|
|
function installAddon(name) {
|
|
|
|
let install = getAddonInstall(name);
|
|
|
|
do_check_neq(null, install);
|
|
|
|
return installAddonFromInstall(install);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convenience function to uninstall an add-on synchronously.
|
|
|
|
*
|
|
|
|
* @param addon
|
|
|
|
* Addon instance to uninstall
|
|
|
|
*/
|
|
|
|
function uninstallAddon(addon) {
|
|
|
|
let cb = Async.makeSyncCallback();
|
|
|
|
let listener = {onUninstalled: function(uninstalled) {
|
|
|
|
if (uninstalled.id == addon.id) {
|
|
|
|
AddonManager.removeAddonListener(listener);
|
|
|
|
cb(uninstalled);
|
|
|
|
}
|
|
|
|
}};
|
|
|
|
|
|
|
|
AddonManager.addAddonListener(listener);
|
|
|
|
addon.uninstall();
|
|
|
|
Async.waitForSyncCallback(cb);
|
|
|
|
}
|
|
|
|
|
2008-06-20 17:47:32 -07:00
|
|
|
function FakeFilesystemService(contents) {
|
|
|
|
this.fakeContents = contents;
|
|
|
|
let self = this;
|
|
|
|
|
2011-01-26 21:43:33 -08: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-20 17:47:32 -07:00
|
|
|
};
|
|
|
|
|
2011-01-26 21:43:33 -08: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-20 17:47:32 -07:00
|
|
|
}
|
2011-01-26 21:43:33 -08:00
|
|
|
callback.call(that, obj);
|
2008-06-20 17:47:32 -07:00
|
|
|
};
|
|
|
|
};
|
2008-06-23 14:13:46 -07:00
|
|
|
|
|
|
|
function FakeGUIDService() {
|
|
|
|
let latestGUID = 0;
|
|
|
|
|
|
|
|
Utils.makeGUID = function fake_makeGUID() {
|
|
|
|
return "fake-guid-" + latestGUID++;
|
|
|
|
};
|
|
|
|
}
|
2008-06-23 20:57:10 -07:00
|
|
|
|
2010-06-01 15:07:50 -07:00
|
|
|
|
2011-08-26 10:27:29 -07:00
|
|
|
function fakeSHA256HMAC(message) {
|
|
|
|
message = message.substr(0, 64);
|
|
|
|
while (message.length < 64) {
|
|
|
|
message += " ";
|
|
|
|
}
|
|
|
|
return message;
|
|
|
|
}
|
|
|
|
|
2010-06-01 15:07:50 -07:00
|
|
|
/*
|
2011-04-20 14:27:19 -07:00
|
|
|
* Mock implementation of WeaveCrypto. It does not encrypt or
|
|
|
|
* decrypt, merely returning the input verbatim.
|
2010-06-01 15:07:50 -07:00
|
|
|
*/
|
|
|
|
function FakeCryptoService() {
|
|
|
|
this.counter = 0;
|
|
|
|
|
|
|
|
delete Svc.Crypto; // get rid of the getter first
|
|
|
|
Svc.Crypto = this;
|
2011-03-01 14:29:41 -08:00
|
|
|
|
2011-08-26 10:27:29 -07:00
|
|
|
CryptoWrapper.prototype.ciphertextHMAC = function ciphertextHMAC(keyBundle) {
|
|
|
|
return fakeSHA256HMAC(this.ciphertext);
|
|
|
|
};
|
2010-06-01 15:07:50 -07:00
|
|
|
}
|
|
|
|
FakeCryptoService.prototype = {
|
|
|
|
|
|
|
|
encrypt: function(aClearText, aSymmetricKey, aIV) {
|
|
|
|
return aClearText;
|
|
|
|
},
|
|
|
|
|
|
|
|
decrypt: function(aCipherText, aSymmetricKey, aIV) {
|
|
|
|
return aCipherText;
|
|
|
|
},
|
|
|
|
|
|
|
|
generateRandomKey: function() {
|
2010-11-29 16:41:17 -08:00
|
|
|
return btoa("fake-symmetric-key-" + this.counter++);
|
2010-06-01 15:07:50 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
generateRandomIV: function() {
|
2010-06-23 03:36:48 -07:00
|
|
|
// A base64-encoded IV is 24 characters long
|
2010-11-29 16:41:17 -08:00
|
|
|
return btoa("fake-fake-fake-random-iv");
|
2010-06-01 15:07:50 -07:00
|
|
|
},
|
|
|
|
|
2010-11-29 16:41:17 -08:00
|
|
|
expandData : function expandData(data, len) {
|
|
|
|
return data;
|
2010-06-01 15:07:50 -07:00
|
|
|
},
|
|
|
|
|
2010-11-29 16:41:17 -08:00
|
|
|
deriveKeyFromPassphrase : function (passphrase, salt, keyLength) {
|
|
|
|
return "some derived key string composed of bytes";
|
2010-06-01 15:07:50 -07:00
|
|
|
},
|
|
|
|
|
2010-11-29 16:41:17 -08:00
|
|
|
generateRandomBytes: function(aByteCount) {
|
|
|
|
return "not-so-random-now-are-we-HA-HA-HA! >:)".slice(aByteCount);
|
2011-05-20 18:45:51 -07:00
|
|
|
}
|
2010-06-01 15:07:50 -07:00
|
|
|
};
|
|
|
|
|
2012-03-22 15:49:50 -07:00
|
|
|
function setBasicCredentials(username, password, syncKey) {
|
|
|
|
let auth = Identity;
|
|
|
|
auth.username = username;
|
|
|
|
auth.basicPassword = password;
|
|
|
|
auth.syncKey = syncKey;
|
|
|
|
}
|
|
|
|
|
|
|
|
function SyncTestingInfrastructure(username, password, syncKey) {
|
|
|
|
Cu.import("resource://services-sync/service.js");
|
2010-06-01 15:07:50 -07:00
|
|
|
|
2012-03-22 15:49:50 -07:00
|
|
|
Identity.account = username || "foo";
|
|
|
|
Identity.basicPassword = password || "password";
|
|
|
|
Identity.syncKey = syncKey || "foo";
|
2008-06-23 20:57:10 -07:00
|
|
|
|
2012-03-22 15:49:50 -07:00
|
|
|
Service.serverURL = TEST_SERVER_URL;
|
|
|
|
Service.clusterURL = TEST_CLUSTER_URL;
|
2008-06-23 20:57:10 -07:00
|
|
|
|
|
|
|
this.logStats = initTestLogging();
|
|
|
|
this.fakeFilesystem = new FakeFilesystemService({});
|
|
|
|
this.fakeGUIDService = new FakeGUIDService();
|
2010-06-01 15:07:50 -07:00
|
|
|
this.fakeCryptoService = new FakeCryptoService();
|
2008-06-23 20:57:10 -07:00
|
|
|
}
|
2009-08-19 18:01:06 -07:00
|
|
|
|
2010-03-25 19:23:44 -07:00
|
|
|
_("Setting the identity for passphrase");
|
2010-06-16 14:30:08 -07:00
|
|
|
Cu.import("resource://services-sync/identity.js");
|
2010-03-25 19:23:44 -07:00
|
|
|
|
2010-12-06 17:25:35 -08:00
|
|
|
/*
|
|
|
|
* Test setup helpers.
|
|
|
|
*/
|
|
|
|
|
2011-08-26 10:27:29 -07:00
|
|
|
// Turn WBO cleartext into fake "encrypted" payload as it goes over the wire.
|
2010-12-06 17:25:35 -08:00
|
|
|
function encryptPayload(cleartext) {
|
|
|
|
if (typeof cleartext == "object") {
|
|
|
|
cleartext = JSON.stringify(cleartext);
|
|
|
|
}
|
|
|
|
|
|
|
|
return {ciphertext: cleartext, // ciphertext == cleartext with fake crypto
|
|
|
|
IV: "irrelevant",
|
2011-08-26 10:27:29 -07:00
|
|
|
hmac: fakeSHA256HMAC(cleartext, Utils.makeHMACKey(""))};
|
2010-12-06 17:25:35 -08:00
|
|
|
}
|
|
|
|
|
2011-04-19 12:35:04 -07:00
|
|
|
function generateNewKeys(collections) {
|
|
|
|
let wbo = CollectionKeys.generateNewKeysWBO(collections);
|
|
|
|
let modified = new_timestamp();
|
|
|
|
CollectionKeys.setContents(wbo.cleartext, modified);
|
|
|
|
}
|
|
|
|
|
2011-06-14 09:31:31 -07:00
|
|
|
/*
|
|
|
|
* A fake engine implementation.
|
|
|
|
* This is used all over the place.
|
2011-12-14 17:10:34 -08:00
|
|
|
*
|
2011-06-14 09:31:31 -07:00
|
|
|
* 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);
|
2011-12-13 15:46:54 -08:00
|
|
|
|
|
|
|
if (!(id in this.items)) {
|
|
|
|
record.deleted = true;
|
|
|
|
return record;
|
|
|
|
}
|
|
|
|
|
2011-06-14 09:31:31 -07:00
|
|
|
record.denomination = this.items[id] || "Data for new record: " + id;
|
|
|
|
return record;
|
|
|
|
},
|
|
|
|
|
|
|
|
changeItemID: function(oldID, newID) {
|
2012-01-25 13:32:05 -08:00
|
|
|
if (oldID in this.items) {
|
|
|
|
this.items[newID] = this.items[oldID];
|
|
|
|
}
|
|
|
|
|
2011-06-14 09:31:31 -07:00
|
|
|
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 08:00:27 -07:00
|
|
|
// Ensure that the engine starts with a clean slate.
|
|
|
|
this.toFetch = [];
|
|
|
|
this.previousFailed = [];
|
2011-06-14 09:31:31 -07:00
|
|
|
}
|
|
|
|
RotaryEngine.prototype = {
|
|
|
|
__proto__: SyncEngine.prototype,
|
|
|
|
_storeObj: RotaryStore,
|
|
|
|
_trackerObj: RotaryTracker,
|
|
|
|
_recordObj: RotaryRecord,
|
|
|
|
|
|
|
|
_findDupe: function(item) {
|
2011-12-13 15:46:54 -08:00
|
|
|
// This is a semaphore used for testing proper reconciling on dupe
|
|
|
|
// detection.
|
|
|
|
if (item.id == "DUPE_INCOMING") {
|
|
|
|
return "DUPE_LOCAL";
|
|
|
|
}
|
|
|
|
|
2011-06-14 09:31:31 -07:00
|
|
|
for (let [id, value] in Iterator(this._store.items)) {
|
|
|
|
if (item.denomination == value) {
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|