Bug 691643 - part 1: converting test_corrupt_keys.js. r=philikon

This commit is contained in:
Richard Newman 2011-10-04 09:03:18 -07:00
Родитель 45caacbbef
Коммит 3abbcd2310
1 изменённых файлов: 27 добавлений и 58 удалений

Просмотреть файл

@ -12,17 +12,6 @@ Cu.import("resource://services-sync/log4moz.js");
add_test(function test_locally_changed_keys() {
let passphrase = "abcdeabcdeabcdeabcdeabcdea";
// Tracking info/collections.
let collectionsHelper = track_collections_helper();
let upd = collectionsHelper.with_updated_collection;
let collections = collectionsHelper.collections;
let keysWBO = new ServerWBO("keys");
let clients = new ServerCollection();
let meta_global = new ServerWBO("global");
let history = new ServerCollection();
let hmacErrorCount = 0;
function counting(f) {
return function() {
@ -33,28 +22,16 @@ add_test(function test_locally_changed_keys() {
Weave.Service.handleHMACEvent = counting(Weave.Service.handleHMACEvent);
let server = httpd_setup({
// Special.
"/1.1/johndoe/storage/meta/global": upd("meta", meta_global.handler()),
"/1.1/johndoe/info/collections": collectionsHelper.handler,
"/1.1/johndoe/storage/crypto/keys": upd("crypto", keysWBO.handler()),
// Track modified times.
"/1.1/johndoe/storage/clients": upd("clients", clients.handler()),
"/1.1/johndoe/storage/clients/foobar": upd("clients", new ServerWBO("clients").handler()),
"/1.1/johndoe/storage/tabs": upd("tabs", new ServerCollection().handler()),
// Just so we don't get 404s in the logs.
"/1.1/johndoe/storage/bookmarks": new ServerCollection().handler(),
"/1.1/johndoe/storage/forms": new ServerCollection().handler(),
"/1.1/johndoe/storage/passwords": new ServerCollection().handler(),
"/1.1/johndoe/storage/prefs": new ServerCollection().handler(),
"/1.1/johndoe/storage/history": upd("history", history.handler()),
let server = new SyncServer();
let johndoe = server.registerUser("johndoe", "password");
johndoe.createContents({
meta: {},
crypto: {},
clients: {}
});
server.start();
try {
Svc.Prefs.set("registerEngines", "Tab");
_("Set up some tabs.");
let myTabs =
@ -97,7 +74,7 @@ add_test(function test_locally_changed_keys() {
"storageVersion": STORAGE_VERSION};
m.upload(Weave.Service.metaURL);
_("New meta/global: " + JSON.stringify(meta_global));
_("New meta/global: " + JSON.stringify(johndoe.collection("meta").wbo("global")));
// Upload keys.
generateNewKeys();
@ -113,9 +90,8 @@ add_test(function test_locally_changed_keys() {
Weave.Service.sync();
// Tabs exist.
_("Tabs modified: " + collections.tabs);
do_check_true(!!collections.tabs);
do_check_true(collections.tabs > 0);
_("Tabs modified: " + johndoe.modified("tabs"));
do_check_true(johndoe.modified("tabs") > 0);
let coll_modified = CollectionKeys.lastModified;
@ -123,7 +99,8 @@ add_test(function test_locally_changed_keys() {
let liveKeys = CollectionKeys.keyForCollection("history");
_("Keys now: " + liveKeys.keyPair);
let visitType = Ci.nsINavHistoryService.TRANSITION_LINK;
for (var i = 0; i < 5; i++) {
let history = johndoe.createCollection("history");
for (let i = 0; i < 5; i++) {
let id = 'record-no--' + i;
let modified = Date.now()/1000 - 60*(i+10);
@ -137,18 +114,14 @@ add_test(function test_locally_changed_keys() {
deleted: false};
w.encrypt();
let wbo = new ServerWBO(id, {ciphertext: w.ciphertext,
IV: w.IV,
hmac: w.hmac});
wbo.modified = modified;
history.insertWBO(wbo);
server.registerPathHandler(
"/1.1/johndoe/storage/history/record-no--" + i,
upd("history", wbo.handler()));
let payload = {ciphertext: w.ciphertext,
IV: w.IV,
hmac: w.hmac};
history.insert(id, payload, modified);
}
collections.history = Date.now()/1000;
let old_key_time = collections.crypto;
history.timestamp = Date.now() / 1000;
let old_key_time = johndoe.modified("crypto");
_("Old key time: " + old_key_time);
// Check that we can decrypt one.
@ -182,9 +155,9 @@ add_test(function test_locally_changed_keys() {
_("Busting some new server values.");
// Now what happens if we corrupt the HMAC on the server?
for (var i = 5; i < 10; i++) {
for (let i = 5; i < 10; i++) {
let id = 'record-no--' + i;
let modified = 1 + (Date.now()/1000);
let modified = 1 + (Date.now() / 1000);
let w = new CryptoWrapper("history", "id");
w.cleartext = {
@ -197,19 +170,15 @@ add_test(function test_locally_changed_keys() {
w.encrypt();
w.hmac = w.hmac.toUpperCase();
let wbo = new ServerWBO(id, {ciphertext: w.ciphertext,
IV: w.IV,
hmac: w.hmac});
wbo.modified = modified;
history.insertWBO(wbo);
server.registerPathHandler(
"/1.1/johndoe/storage/history/record-no--" + i,
upd("history", wbo.handler()));
let payload = {ciphertext: w.ciphertext,
IV: w.IV,
hmac: w.hmac};
history.insert(id, payload, modified);
}
collections.history = Date.now()/1000;
history.timestamp = Date.now() / 1000;
_("Server key time hasn't changed.");
do_check_eq(collections.crypto, old_key_time);
do_check_eq(johndoe.modified("crypto"), old_key_time);
_("Resetting HMAC error timer.");
Weave.Service.lastHMACEvent = 0;
@ -218,7 +187,7 @@ add_test(function test_locally_changed_keys() {
Weave.Service.sync();
_("Keys now: " + CollectionKeys.keyForCollection("history").keyPair);
_("Server keys have been updated, and we skipped over 5 more HMAC errors without adjusting history.");
do_check_true(collections.crypto > old_key_time);
do_check_true(johndoe.modified("crypto") > old_key_time);
do_check_eq(hmacErrorCount, 6);
do_check_false(store.urlExists("http://foo/bar?record-no--5"));
do_check_false(store.urlExists("http://foo/bar?record-no--6"));