Add a test for the passphrase storage. Updates #111

This commit is contained in:
JC Brand 2014-02-12 02:02:45 +02:00
parent e3b8a8f9dd
commit 398142c7de
2 changed files with 40 additions and 68 deletions

View File

@ -262,6 +262,20 @@
} }
}; };
this.getSessionPassphrase = function () {
if (this.prebind) {
var key = hex_sha1(this.connection.jid),
pass = window.sessionStorage[key];
if (typeof pass === 'undefined') {
pass = Math.floor(Math.random()*4294967295).toString();
window.sessionStorage[key] = pass;
}
return pass;
} else {
return this.connection.pass;
}
};
this.getVCard = function (jid, callback, errback) { this.getVCard = function (jid, callback, errback) {
if (!this.use_vcards) { if (!this.use_vcards) {
if (callback) { if (callback) {
@ -567,25 +581,11 @@
} }
}, },
getSessionPassphrase: function () {
if (converse.prebind) {
var key = hex_sha1(converse.connection.jid),
pass = window.sessionStorage[key];
if (typeof pass === 'undefined') {
pass = Math.floor(Math.random()*4294967295);
window.sessionStorage[key] = pass;
}
return pass;
} else {
return converse.connection.pass;
}
},
generatePrivateKey: function (callback, instance_tag) { generatePrivateKey: function (callback, instance_tag) {
var cipher = CryptoJS.lib.PasswordBasedCipher; var cipher = CryptoJS.lib.PasswordBasedCipher;
var key = new DSA(); var key = new DSA();
if (converse.cache_otr_key) { if (converse.cache_otr_key) {
pass = this.getSessionPassphrase(); pass = converse.getSessionPassphrase();
if (typeof pass !== "undefined") { if (typeof pass !== "undefined") {
// Encrypt the key and set in sessionStorage. Also store instance tag. // Encrypt the key and set in sessionStorage. Also store instance tag.
window.sessionStorage[hex_sha1(this.id+'priv_key')] = window.sessionStorage[hex_sha1(this.id+'priv_key')] =
@ -602,12 +602,10 @@
}, },
getSession: function (callback) { getSession: function (callback) {
// FIXME: sessionStorage is not supported in IE < 8. Perhaps a
// user alert is required here...
var cipher = CryptoJS.lib.PasswordBasedCipher; var cipher = CryptoJS.lib.PasswordBasedCipher;
var result, pass, instance_tag, saved_key; var result, pass, instance_tag, saved_key;
if (converse.cache_otr_key) { if (converse.cache_otr_key) {
pass = this.getSessionPassphrase(); pass = converse.getSessionPassphrase();
if (typeof pass !== "undefined") { if (typeof pass !== "undefined") {
instance_tag = window.sessionStorage[hex_sha1(this.id+'instance_tag')]; instance_tag = window.sessionStorage[hex_sha1(this.id+'instance_tag')];
saved_key = window.sessionStorage[hex_sha1(this.id+'priv_key')]; saved_key = window.sessionStorage[hex_sha1(this.id+'priv_key')];

View File

@ -8,60 +8,34 @@
); );
} (this, function (mock, utils) { } (this, function (mock, utils) {
return describe("Converse", $.proxy(function(mock, utils) { return describe("Converse", $.proxy(function(mock, utils) {
beforeEach($.proxy(function () {
window.localStorage.clear(); window.localStorage.clear();
window.sessionStorage.clear();
}, converse));
it("allows you to subscribe to emitted events", function () { it("can store a session passphrase in session storage", $.proxy(function () {
this.callback = function () {}; var pp;
spyOn(this, 'callback'); // With no prebind, the user's XMPP password is used and nothing is
converse.on('onInitialized', this.callback); // stored in session storage.
converse.emit('onInitialized'); this.prebind = false;
expect(this.callback).toHaveBeenCalled(); this.connection.pass = 's3cr3t!';
converse.emit('onInitialized'); expect(this.getSessionPassphrase()).toBe(this.connection.pass);
expect(this.callback.callCount, 2); expect(window.sessionStorage.length).toBe(0);
converse.emit('onInitialized'); expect(window.localStorage.length).toBe(0);
expect(this.callback.callCount, 3);
});
it("allows you to listen once for an emitted event", function () { // With prebind, a random passphrase is generated and stored in
this.callback = function () {}; // session storage.
spyOn(this, 'callback'); this.prebind = true;
converse.once('onInitialized', this.callback); pp = this.getSessionPassphrase();
converse.emit('onInitialized'); expect(pp).not.toBe(this.connection.pass);
expect(this.callback).toHaveBeenCalled(); expect(window.sessionStorage.length).toBe(1);
converse.emit('onInitialized'); expect(window.localStorage.length).toBe(0);
expect(this.callback.callCount, 1); expect(pp).toBe(window.sessionStorage[hex_sha1(converse.connection.jid)]);
converse.emit('onInitialized');
expect(this.callback.callCount, 1);
});
it("allows you to stop listening or subscribing to an event", function () { // Clean up
this.callback = function () {}; this.prebind = false;
this.anotherCallback = function () {}; }, converse));
this.neverCalled = function () {};
spyOn(this, 'callback');
spyOn(this, 'anotherCallback');
spyOn(this, 'neverCalled');
converse.on('onInitialized', this.callback);
converse.on('onInitialized', this.anotherCallback);
converse.emit('onInitialized');
expect(this.callback).toHaveBeenCalled();
expect(this.anotherCallback).toHaveBeenCalled();
converse.off('onInitialized', this.callback);
converse.emit('onInitialized');
expect(this.callback.callCount, 1);
expect(this.anotherCallback.callCount, 2);
converse.once('onInitialized', this.neverCalled);
converse.off('onInitialized', this.neverCalled);
converse.emit('onInitialized');
expect(this.callback.callCount, 1);
expect(this.anotherCallback.callCount, 3);
expect(this.neverCalled).not.toHaveBeenCalled();
});
}, converse, mock, utils)); }, converse, mock, utils));
})); }));