Integrate libaxolotl async storage changes
* Session records are now opaque strings, so treat them that way: - no more cross checking identity key and session records - Move hasOpenSession to axolotl wrapper - Remote registration ids must be fetched async'ly via protocol wrapper * Implement async AxolotlStore using textsecure.storage * Add some db stores and move prekeys and signed keys to indexeddb * Add storage tests * Rename identityKey storage key from libaxolotl25519KeyidentityKey to simply identityKey, since it's no longer hardcoded in libaxolotl * Rework registration and key-generation, keeping logic in libtextsecure and rendering in options.js. * Remove key_worker since workers are handled at the libaxolotl level nowpull/749/head
parent
8304aa903a
commit
96eafc7750
@ -0,0 +1,183 @@
|
||||
/* vim: ts=4:sw=4
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
;(function() {
|
||||
'use strict';
|
||||
|
||||
function isStringable(thing) {
|
||||
return (thing === Object(thing) &&
|
||||
(thing.__proto__ == StaticArrayBufferProto ||
|
||||
thing.__proto__ == StaticUint8ArrayProto ||
|
||||
thing.__proto__ == StaticByteBufferProto));
|
||||
}
|
||||
function convertToArrayBuffer(thing) {
|
||||
if (thing === undefined)
|
||||
return undefined;
|
||||
if (thing === Object(thing)) {
|
||||
if (thing.__proto__ == StaticArrayBufferProto)
|
||||
return thing;
|
||||
//TODO: Several more cases here...
|
||||
}
|
||||
|
||||
if (thing instanceof Array) {
|
||||
// Assuming Uint16Array from curve25519
|
||||
//TODO: Move to convertToArrayBuffer
|
||||
var res = new ArrayBuffer(thing.length * 2);
|
||||
var uint = new Uint16Array(res);
|
||||
for (var i = 0; i < thing.length; i++)
|
||||
uint[i] = thing[i];
|
||||
return res;
|
||||
}
|
||||
|
||||
var str;
|
||||
if (isStringable(thing))
|
||||
str = stringObject(thing);
|
||||
else if (typeof thing == "string")
|
||||
str = thing;
|
||||
else
|
||||
throw new Error("Tried to convert a non-stringable thing of type " + typeof thing + " to an array buffer");
|
||||
var res = new ArrayBuffer(str.length);
|
||||
var uint = new Uint8Array(res);
|
||||
for (var i = 0; i < str.length; i++)
|
||||
uint[i] = str.charCodeAt(i);
|
||||
return res;
|
||||
}
|
||||
|
||||
var Model = Backbone.Model.extend({ database: Whisper.Database });
|
||||
var PreKey = Model.extend({ storeName: 'preKeys' });
|
||||
var SignedPreKey = Model.extend({ storeName: 'signedPreKeys' });
|
||||
|
||||
function AxolotlStore() {}
|
||||
|
||||
AxolotlStore.prototype = {
|
||||
constructor: AxolotlStore,
|
||||
get: function(key,defaultValue) {
|
||||
return textsecure.storage.get(key, defaultValue);
|
||||
},
|
||||
put: function(key, value) {
|
||||
textsecure.storage.put(key, value);
|
||||
},
|
||||
remove: function(key) {
|
||||
textsecure.storage.remove(key);
|
||||
},
|
||||
getMyIdentityKey: function() {
|
||||
var res = textsecure.storage.get('identityKey');
|
||||
if (res === undefined)
|
||||
return undefined;
|
||||
|
||||
return {
|
||||
pubKey: convertToArrayBuffer(res.pubKey),
|
||||
privKey: convertToArrayBuffer(res.privKey)
|
||||
};
|
||||
},
|
||||
getMyRegistrationId: function() {
|
||||
return textsecure.storage.get('registrationId');
|
||||
},
|
||||
|
||||
getIdentityKey: function(identifier) {
|
||||
if (identifier === null || identifier === undefined)
|
||||
throw new Error("Tried to get identity key for undefined/null key");
|
||||
return Promise.resolve(convertToArrayBuffer(textsecure.storage.devices.getIdentityKeyForNumber(textsecure.utils.unencodeNumber(identifier)[0])));
|
||||
},
|
||||
putIdentityKey: function(identifier, identityKey) {
|
||||
if (identifier === null || identifier === undefined)
|
||||
throw new Error("Tried to put identity key for undefined/null key");
|
||||
return Promise.resolve(textsecure.storage.devices.checkSaveIdentityKeyForNumber(textsecure.utils.unencodeNumber(identifier)[0], identityKey));
|
||||
},
|
||||
|
||||
/* Returns a prekeypair object or undefined */
|
||||
getPreKey: function(keyId) {
|
||||
var prekey = new PreKey({id: keyId});
|
||||
return new Promise(function(resolve) {
|
||||
prekey.fetch().then(function() {
|
||||
resolve({
|
||||
pubKey: prekey.attributes.publicKey,
|
||||
privKey: prekey.attributes.privateKey
|
||||
});
|
||||
}).fail(resolve);
|
||||
});
|
||||
},
|
||||
putPreKey: function(keyId, keyPair) {
|
||||
var prekey = new PreKey({
|
||||
id : keyId,
|
||||
publicKey : keyPair.pubKey,
|
||||
privateKey : keyPair.privKey
|
||||
});
|
||||
return new Promise(function(resolve) {
|
||||
prekey.save().always(function() {
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
},
|
||||
removePreKey: function(keyId) {
|
||||
var prekey = new PreKey({id: keyId});
|
||||
return new Promise(function(resolve) {
|
||||
prekey.destroy().then(function() {
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
},
|
||||
|
||||
/* Returns a signed keypair object or undefined */
|
||||
getSignedPreKey: function(keyId) {
|
||||
var prekey = new SignedPreKey({id: keyId});
|
||||
return new Promise(function(resolve) {
|
||||
prekey.fetch().then(function() {
|
||||
resolve({
|
||||
pubKey: prekey.attributes.publicKey,
|
||||
privKey: prekey.attributes.privateKey
|
||||
});
|
||||
}).fail(resolve);
|
||||
});
|
||||
},
|
||||
putSignedPreKey: function(keyId, keyPair) {
|
||||
var prekey = new SignedPreKey({
|
||||
id : keyId,
|
||||
publicKey : keyPair.pubKey,
|
||||
privateKey : keyPair.privKey
|
||||
});
|
||||
return new Promise(function(resolve) {
|
||||
prekey.save().always(function() {
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
},
|
||||
removeSignedPreKey: function(keyId) {
|
||||
var prekey = new SignedPreKey({id: keyId});
|
||||
return new Promise(function(resolve) {
|
||||
prekey.destroy().then(function() {
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
},
|
||||
|
||||
getSession: function(identifier) {
|
||||
if (identifier === null || identifier === undefined)
|
||||
throw new Error("Tried to get session for undefined/null key");
|
||||
return new Promise(function(resolve) {
|
||||
resolve(textsecure.storage.sessions.getSessionsForNumber(identifier));
|
||||
});
|
||||
},
|
||||
putSession: function(identifier, record) {
|
||||
if (identifier === null || identifier === undefined)
|
||||
throw new Error("Tried to put session for undefined/null key");
|
||||
return new Promise(function(resolve) {
|
||||
resolve(textsecure.storage.sessions.putSessionsForDevice(identifier, record));
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
window.AxolotlStore = AxolotlStore;
|
||||
})();
|
File diff suppressed because one or more lines are too long
@ -0,0 +1,181 @@
|
||||
/* vim: ts=4:sw=4
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
describe("Key generation", function() {
|
||||
var count = 10;
|
||||
this.timeout(count*1000);
|
||||
|
||||
function validateStoredKeyPair(keyPair) {
|
||||
/* Ensure the keypair matches the format used internally by libaxolotl */
|
||||
assert.isObject(keyPair, 'Stored keyPair is not an object');
|
||||
assert.instanceOf(keyPair.pubKey, ArrayBuffer);
|
||||
assert.instanceOf(keyPair.privKey, ArrayBuffer);
|
||||
assert.strictEqual(keyPair.pubKey.byteLength, 33);
|
||||
assert.strictEqual(new Uint8Array(keyPair.pubKey)[0], 5);
|
||||
assert.strictEqual(keyPair.privKey.byteLength, 32);
|
||||
}
|
||||
function itStoresPreKey(keyId) {
|
||||
it('prekey ' + keyId + ' is valid', function(done) {
|
||||
return textsecure.storage.axolotl.getPreKey(keyId).then(function(keyPair) {
|
||||
validateStoredKeyPair(keyPair);
|
||||
}).then(done,done);
|
||||
});
|
||||
}
|
||||
function itStoresSignedPreKey(keyId) {
|
||||
it('signed prekey ' + keyId + ' is valid', function(done) {
|
||||
return textsecure.storage.axolotl.getSignedPreKey(keyId).then(function(keyPair) {
|
||||
validateStoredKeyPair(keyPair);
|
||||
}).then(done,done);
|
||||
});
|
||||
}
|
||||
function validateResultKey(resultKey) {
|
||||
return textsecure.storage.axolotl.getPreKey(resultKey.keyId).then(function(keyPair) {
|
||||
assertEqualArrayBuffers(resultKey.publicKey, keyPair.pubKey);
|
||||
});
|
||||
}
|
||||
function validateResultSignedKey(resultSignedKey) {
|
||||
return textsecure.storage.axolotl.getSignedPreKey(resultSignedKey.keyId).then(function(keyPair) {
|
||||
assertEqualArrayBuffers(resultSignedKey.publicKey, keyPair.pubKey);
|
||||
});
|
||||
}
|
||||
|
||||
before(function(done) {
|
||||
localStorage.clear();
|
||||
axolotl.util.generateIdentityKeyPair().then(function(keyPair) {
|
||||
return textsecure.storage.axolotl.put('identityKey', keyPair);
|
||||
}).then(done, done);
|
||||
});
|
||||
|
||||
describe('the first time', function() {
|
||||
var result;
|
||||
/* result should have this format
|
||||
* {
|
||||
* preKeys: [ { keyId, publicKey }, ... ],
|
||||
* signedPreKey: { keyId, publicKey, signature },
|
||||
* identityKey: <ArrayBuffer>
|
||||
* }
|
||||
*/
|
||||
before(function(done) {
|
||||
generateKeys(count).then(function(res) {
|
||||
result = res;
|
||||
}).then(done,done);
|
||||
});
|
||||
for (var i = 1; i <= count; i++) {
|
||||
itStoresPreKey(i);
|
||||
}
|
||||
itStoresSignedPreKey(1);
|
||||
|
||||
it('result contains ' + count + ' preKeys', function() {
|
||||
assert.isArray(result.preKeys);
|
||||
assert.lengthOf(result.preKeys, count);
|
||||
for (var i = 0; i < count; i++) {
|
||||
assert.isObject(result.preKeys[i]);
|
||||
}
|
||||
});
|
||||
it('result contains the correct keyIds', function() {
|
||||
for (var i = 0; i < count; i++) {
|
||||
assert.strictEqual(result.preKeys[i].keyId, i+1);
|
||||
}
|
||||
});
|
||||
it('result contains the correct public keys', function(done) {
|
||||
Promise.all(result.preKeys.map(validateResultKey)).then(function() {
|
||||
done();
|
||||
}).catch(done);
|
||||
});
|
||||
it('returns a signed prekey', function(done) {
|
||||
assert.strictEqual(result.signedPreKey.keyId, 1);
|
||||
assert.instanceOf(result.signedPreKey.signature, ArrayBuffer);
|
||||
validateResultSignedKey(result.signedPreKey).then(done,done);
|
||||
});
|
||||
});
|
||||
describe('the second time', function() {
|
||||
var result;
|
||||
before(function(done) {
|
||||
generateKeys(count).then(function(res) {
|
||||
result = res;
|
||||
}).then(done,done);
|
||||
});
|
||||
for (var i = 1; i <= 2*count; i++) {
|
||||
itStoresPreKey(i);
|
||||
}
|
||||
itStoresSignedPreKey(1);
|
||||
itStoresSignedPreKey(2);
|
||||
it('result contains ' + count + ' preKeys', function() {
|
||||
assert.isArray(result.preKeys);
|
||||
assert.lengthOf(result.preKeys, count);
|
||||
for (var i = 0; i < count; i++) {
|
||||
assert.isObject(result.preKeys[i]);
|
||||
}
|
||||
});
|
||||
it('result contains the correct keyIds', function() {
|
||||
for (var i = 1; i <= count; i++) {
|
||||
assert.strictEqual(result.preKeys[i-1].keyId, i+count);
|
||||
}
|
||||
});
|
||||
it('result contains the correct public keys', function(done) {
|
||||
Promise.all(result.preKeys.map(validateResultKey)).then(function() {
|
||||
done();
|
||||
}).catch(done);
|
||||
});
|
||||
it('returns a signed prekey', function(done) {
|
||||
assert.strictEqual(result.signedPreKey.keyId, 2);
|
||||
assert.instanceOf(result.signedPreKey.signature, ArrayBuffer);
|
||||
validateResultSignedKey(result.signedPreKey).then(done,done);
|
||||
});
|
||||
});
|
||||
describe('the third time', function() {
|
||||
var result;
|
||||
before(function(done) {
|
||||
generateKeys(count).then(function(res) {
|
||||
result = res;
|
||||
}).then(done,done);
|
||||
});
|
||||
for (var i = 1; i <= 3*count; i++) {
|
||||
itStoresPreKey(i);
|
||||
}
|
||||
itStoresSignedPreKey(2);
|
||||
itStoresSignedPreKey(3);
|
||||
it('result contains ' + count + ' preKeys', function() {
|
||||
assert.isArray(result.preKeys);
|
||||
assert.lengthOf(result.preKeys, count);
|
||||
for (var i = 0; i < count; i++) {
|
||||
assert.isObject(result.preKeys[i]);
|
||||
}
|
||||
});
|
||||
it('result contains the correct keyIds', function() {
|
||||
for (var i = 1; i <= count; i++) {
|
||||
assert.strictEqual(result.preKeys[i-1].keyId, i+2*count);
|
||||
}
|
||||
});
|
||||
it('result contains the correct public keys', function(done) {
|
||||
Promise.all(result.preKeys.map(validateResultKey)).then(function() {
|
||||
done();
|
||||
}).catch(done);
|
||||
});
|
||||
it('result contains a signed prekey', function(done) {
|
||||
assert.strictEqual(result.signedPreKey.keyId, 3);
|
||||
assert.instanceOf(result.signedPreKey.signature, ArrayBuffer);
|
||||
validateResultSignedKey(result.signedPreKey).then(done,done);
|
||||
});
|
||||
it('deletes signed key 1', function() {
|
||||
textsecure.storage.axolotl.getSignedPreKey(1).then(function(keyPair) {
|
||||
assert.isUndefined(keyPair);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
@ -0,0 +1,93 @@
|
||||
function AxolotlStore() {
|
||||
this.store = {};
|
||||
}
|
||||
|
||||
AxolotlStore.prototype = {
|
||||
getMyIdentityKey: function() {
|
||||
return this.get('identityKey');
|
||||
},
|
||||
getMyRegistrationId: function() {
|
||||
return this.get('registrationId');
|
||||
},
|
||||
put: function(key, value) {
|
||||
if (key === undefined || value === undefined || key === null || value === null)
|
||||
throw new Error("Tried to store undefined/null");
|
||||
this.store[key] = value;
|
||||
},
|
||||
get: function(key, defaultValue) {
|
||||
if (key === null || key === undefined)
|
||||
throw new Error("Tried to get value for undefined/null key");
|
||||
if (key in this.store) {
|
||||
return this.store[key];
|
||||
} else {
|
||||
return defaultValue;
|
||||
}
|
||||
},
|
||||
remove: function(key) {
|
||||
if (key === null || key === undefined)
|
||||
throw new Error("Tried to remove value for undefined/null key");
|
||||
delete this.store[key];
|
||||
},
|
||||
|
||||
getIdentityKey: function(identifier) {
|
||||
if (identifier === null || identifier === undefined)
|
||||
throw new Error("Tried to get identity key for undefined/null key");
|
||||
return new Promise(function(resolve) {
|
||||
resolve(this.get('identityKey' + identifier));
|
||||
}.bind(this));
|
||||
},
|
||||
putIdentityKey: function(identifier, identityKey) {
|
||||
if (identifier === null || identifier === undefined)
|
||||
throw new Error("Tried to put identity key for undefined/null key");
|
||||
return new Promise(function(resolve) {
|
||||
resolve(this.put('identityKey' + identifier, identityKey));
|
||||
}.bind(this));
|
||||
},
|
||||
|
||||
/* Returns a prekeypair object or undefined */
|
||||
getPreKey: function(keyId) {
|
||||
return new Promise(function(resolve) {
|
||||
var res = this.get('25519KeypreKey' + keyId);
|
||||
resolve(res);
|
||||
}.bind(this));
|
||||
},
|
||||
putPreKey: function(keyId, keyPair) {
|
||||
return new Promise(function(resolve) {
|
||||
resolve(this.put('25519KeypreKey' + keyId, keyPair));
|
||||
}.bind(this));
|
||||
},
|
||||
removePreKey: function(keyId) {
|
||||
return new Promise(function(resolve) {
|
||||
resolve(this.remove('25519KeypreKey' + keyId));
|
||||
}.bind(this));
|
||||
},
|
||||
|
||||
/* Returns a signed keypair object or undefined */
|
||||
getSignedPreKey: function(keyId) {
|
||||
return new Promise(function(resolve) {
|
||||
var res = this.get('25519KeysignedKey' + keyId);
|
||||
resolve(res);
|
||||
}.bind(this));
|
||||
},
|
||||
putSignedPreKey: function(keyId, keyPair) {
|
||||
return new Promise(function(resolve) {
|
||||
resolve(this.put('25519KeysignedKey' + keyId, keyPair));
|
||||
}.bind(this));
|
||||
},
|
||||
removeSignedPreKey: function(keyId) {
|
||||
return new Promise(function(resolve) {
|
||||
resolve(this.remove('25519KeysignedKey' + keyId));
|
||||
}.bind(this));
|
||||
},
|
||||
|
||||
getSession: function(identifier) {
|
||||
return new Promise(function(resolve) {
|
||||
resolve(this.get('session' + identifier));
|
||||
}.bind(this));
|
||||
},
|
||||
putSession: function(identifier, record) {
|
||||
return new Promise(function(resolve) {
|
||||
resolve(this.put('session' + identifier, record));
|
||||
}.bind(this));
|
||||
}
|
||||
};
|
@ -0,0 +1,93 @@
|
||||
/* vim: ts=4:sw=4
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
describe("AxolotlStore", function() {
|
||||
before(function() { localStorage.clear(); });
|
||||
var store = textsecure.storage.axolotl;
|
||||
var identifier = '+5558675309';
|
||||
var identityKey = {
|
||||
pubKey: textsecure.crypto.getRandomBytes(33),
|
||||
privKey: textsecure.crypto.getRandomBytes(32),
|
||||
};
|
||||
var testKey = {
|
||||
pubKey: textsecure.crypto.getRandomBytes(33),
|
||||
privKey: textsecure.crypto.getRandomBytes(32),
|
||||
};
|
||||
it('retrieves my registration id', function() {
|
||||
store.put('registrationId', 1337);
|
||||
var reg = store.getMyRegistrationId();
|
||||
assert.strictEqual(reg, 1337);
|
||||
});
|
||||
it('retrieves my identity key', function() {
|
||||
store.put('identityKey', identityKey);
|
||||
var key = store.getMyIdentityKey();
|
||||
assertEqualArrayBuffers(key.pubKey, identityKey.pubKey);
|
||||
assertEqualArrayBuffers(key.privKey, identityKey.privKey);
|
||||
});
|
||||
it('stores identity keys', function(done) {
|
||||
store.putIdentityKey(identifier, testKey.pubKey).then(function() {
|
||||
return store.getIdentityKey(identifier).then(function(key) {
|
||||
assertEqualArrayBuffers(key, testKey.pubKey);
|
||||
});
|
||||
}).then(done,done);
|
||||
});
|
||||
it('stores prekeys', function(done) {
|
||||
store.putPreKey(1, testKey).then(function() {
|
||||
return store.getPreKey(1).then(function(key) {
|
||||
assertEqualArrayBuffers(key.pubKey, testKey.pubKey);
|
||||
assertEqualArrayBuffers(key.privKey, testKey.privKey);
|
||||
});
|
||||
}).then(done,done);
|
||||
});
|
||||
it('deletes prekeys', function(done) {
|
||||
before(function(done) {
|
||||
store.putPreKey(2, testKey).then(done);
|
||||
});
|
||||
store.removePreKey(2, testKey).then(function() {
|
||||
return store.getPreKey(2).then(function(key) {
|
||||
assert.isUndefined(key);
|
||||
});
|
||||
}).then(done,done);
|
||||
});
|
||||
it('stores signed prekeys', function(done) {
|
||||
store.putSignedPreKey(3, testKey).then(function() {
|
||||
return store.getSignedPreKey(3).then(function(key) {
|
||||
assertEqualArrayBuffers(key.pubKey, testKey.pubKey);
|
||||
assertEqualArrayBuffers(key.privKey, testKey.privKey);
|
||||
});
|
||||
}).then(done,done);
|
||||
});
|
||||
it('deletes signed prekeys', function(done) {
|
||||
before(function(done) {
|
||||
store.putSignedPreKey(4, testKey).then(done);
|
||||
});
|
||||
store.removeSignedPreKey(4, testKey).then(function() {
|
||||
return store.getSignedPreKey(4).then(function(key) {
|
||||
assert.isUndefined(key);
|
||||
});
|
||||
}).then(done,done);
|
||||
});
|
||||
it('stores sessions', function(done) {
|
||||
var testRecord = "an opaque string";
|
||||
store.putSession(identifier + '.1', testRecord).then(function() {
|
||||
return store.getSession(identifier + '.1').then(function(record) {
|
||||
assert.deepEqual(record, testRecord);
|
||||
});
|
||||
}).then(done,done);
|
||||
});
|
||||
});
|
@ -0,0 +1,93 @@
|
||||
/* vim: ts=4:sw=4
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
describe("AxolotlStore", function() {
|
||||
before(function() { localStorage.clear(); });
|
||||
var store = textsecure.storage.axolotl;
|
||||
var identifier = '+5558675309';
|
||||
var identityKey = {
|
||||
pubKey: textsecure.crypto.getRandomBytes(33),
|
||||
privKey: textsecure.crypto.getRandomBytes(32),
|
||||
};
|
||||
var testKey = {
|
||||
pubKey: textsecure.crypto.getRandomBytes(33),
|
||||
privKey: textsecure.crypto.getRandomBytes(32),
|
||||
};
|
||||
it('retrieves my registration id', function() {
|
||||
store.put('registrationId', 1337);
|
||||
var reg = store.getMyRegistrationId();
|
||||
assert.strictEqual(reg, 1337);
|
||||
});
|
||||
it('retrieves my identity key', function() {
|
||||
store.put('identityKey', identityKey);
|
||||
var key = store.getMyIdentityKey();
|
||||
assertEqualArrayBuffers(key.pubKey, identityKey.pubKey);
|
||||
assertEqualArrayBuffers(key.privKey, identityKey.privKey);
|
||||
});
|
||||
it('stores identity keys', function(done) {
|
||||
store.putIdentityKey(identifier, testKey.pubKey).then(function() {
|
||||
return store.getIdentityKey(identifier).then(function(key) {
|
||||
assertEqualArrayBuffers(key, testKey.pubKey);
|
||||
});
|
||||
}).then(done,done);
|
||||
});
|
||||
it('stores prekeys', function(done) {
|
||||
store.putPreKey(1, testKey).then(function() {
|
||||
return store.getPreKey(1).then(function(key) {
|
||||
assertEqualArrayBuffers(key.pubKey, testKey.pubKey);
|
||||
assertEqualArrayBuffers(key.privKey, testKey.privKey);
|
||||
});
|
||||
}).then(done,done);
|
||||
});
|
||||
it('deletes prekeys', function(done) {
|
||||
before(function(done) {
|
||||
store.putPreKey(2, testKey).then(done);
|
||||
});
|
||||
store.removePreKey(2, testKey).then(function() {
|
||||
return store.getPreKey(2).then(function(key) {
|
||||
assert.isUndefined(key);
|
||||
});
|
||||
}).then(done,done);
|
||||
});
|
||||
it('stores signed prekeys', function(done) {
|
||||
store.putSignedPreKey(3, testKey).then(function() {
|
||||
return store.getSignedPreKey(3).then(function(key) {
|
||||
assertEqualArrayBuffers(key.pubKey, testKey.pubKey);
|
||||
assertEqualArrayBuffers(key.privKey, testKey.privKey);
|
||||
});
|
||||
}).then(done,done);
|
||||
});
|
||||
it('deletes signed prekeys', function(done) {
|
||||
before(function(done) {
|
||||
store.putSignedPreKey(4, testKey).then(done);
|
||||
});
|
||||
store.removeSignedPreKey(4, testKey).then(function() {
|
||||
return store.getSignedPreKey(4).then(function(key) {
|
||||
assert.isUndefined(key);
|
||||
});
|
||||
}).then(done,done);
|
||||
});
|
||||
it('stores sessions', function(done) {
|
||||
var testRecord = "an opaque string";
|
||||
store.putSession(identifier + '.1', testRecord).then(function() {
|
||||
return store.getSession(identifier + '.1').then(function(record) {
|
||||
assert.deepEqual(record, testRecord);
|
||||
});
|
||||
}).then(done,done);
|
||||
});
|
||||
});
|
Loading…
Reference in New Issue