signal-desktop/js/test.js

63 lines
2.2 KiB
JavaScript
Raw Normal View History

2014-01-17 06:08:47 +00:00
// Setup dumb test wrapper
var testsdiv = $('#tests');
2014-01-22 06:23:41 +00:00
var testsOutstanding = [];
2014-01-17 06:08:47 +00:00
function TEST(func, name) {
var funcName = name === undefined ? func + "" : name;
2014-01-22 06:23:41 +00:00
var testIndex = testsOutstanding.length;
function callback(result) {
if (result)
2014-01-17 06:08:47 +00:00
testsdiv.append('<p style="color: green;">' + funcName + ' passed</p>');
else
testsdiv.append('<p style="color: red;">' + funcName + ' returned false</p>');
2014-01-22 06:23:41 +00:00
delete testsOutstanding[testIndex];
}
try {
testsOutstanding[testIndex] = funcName;
func(callback);
2014-01-17 06:08:47 +00:00
} catch (e) {
testsdiv.append('<p style="color: red;">' + funcName + ' threw ' + e + '</p>');
}
}
2014-01-22 06:23:41 +00:00
registerOnLoadFunction(function() {
localStorage.clear();
// Random tests to check my JS knowledge
TEST(function(callback) { callback(!objectContainsKeys({})); });
TEST(function(callback) { callback(objectContainsKeys({ a: undefined })); });
TEST(function(callback) { callback(objectContainsKeys({ a: null })); });
// Basic sanity-checks on the crypto library
TEST(function(callback) {
var PushMessageProto = dcodeIO.ProtoBuf.loadProtoFile("protos/IncomingPushMessageSignal.proto").build("textsecure.PushMessageContent");
var IncomingMessageProto = dcodeIO.ProtoBuf.loadProtoFile("protos/IncomingPushMessageSignal.proto").build("textsecure.IncomingPushMessageSignal");
var text_message = new PushMessageProto();
text_message.body = "Hi Mom";
var server_message = {type: 0, // unencrypted
source: "+19999999999", timestamp: 42, message: text_message.encode() };
2014-01-17 06:08:47 +00:00
2014-01-22 06:23:41 +00:00
crypto.handleIncomingPushMessageProto(server_message, function(message) {
callback(message.body == text_message.body &&
message.attachments.length == text_message.attachments.length &&
text_message.attachments.length == 0);
});
}, 'Unencrypted PushMessageProto "decrypt"');
2014-01-17 06:08:47 +00:00
2014-01-22 06:23:41 +00:00
TEST(function(callback) {
crypto.generateKeys(function() {
callback(true);
});
}, "Test simple create key");
2014-01-17 06:08:47 +00:00
2014-01-22 06:23:41 +00:00
// TODO: Run through the test vectors for the axolotl ratchet
2014-01-17 06:08:47 +00:00
2014-01-22 06:23:41 +00:00
window.setTimeout(function() {
for (var i = 0; i < testsOutstanding.length; i++)
if (testsOutstanding[i] !== undefined)
testsdiv.append('<p style="color: red;">' + testsOutstanding[i] + ' timed out</p>');
2014-01-17 06:08:47 +00:00
2014-01-22 06:23:41 +00:00
localStorage.clear();
}, 1000);
});