You've already forked matrix-js-sdk
mirror of
https://github.com/matrix-org/matrix-js-sdk.git
synced 2025-12-04 05:02:41 +03:00
matrix-client-crypto-spec: reorder
Separate the helper functions from the tests, and order them by functionality rather than by test order. I've been struggling to find the tests among the helpers, and struggling to find the helpers among the other helpers. Hopefully this will help.
This commit is contained in:
@@ -18,79 +18,45 @@ MockStorageApi.prototype = {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
describe("MatrixClient crypto", function() {
|
var aliHttpBackend;
|
||||||
if (!sdk.CRYPTO_ENABLED) {
|
var bobHttpBackend;
|
||||||
return;
|
var aliClient;
|
||||||
}
|
var roomId = "!room:localhost";
|
||||||
|
var aliUserId = "@ali:localhost";
|
||||||
|
var aliDeviceId = "zxcvb";
|
||||||
|
var aliAccessToken = "aseukfgwef";
|
||||||
|
var bobClient;
|
||||||
|
var bobUserId = "@bob:localhost";
|
||||||
|
var bobDeviceId = "bvcxz";
|
||||||
|
var bobAccessToken = "fewgfkuesa";
|
||||||
|
var bobOneTimeKeys;
|
||||||
|
var aliDeviceKeys;
|
||||||
|
var bobDeviceKeys;
|
||||||
|
var bobDeviceCurve25519Key;
|
||||||
|
var bobDeviceEd25519Key;
|
||||||
|
var aliLocalStore;
|
||||||
|
var aliStorage;
|
||||||
|
var bobStorage;
|
||||||
|
var aliMessages;
|
||||||
|
var bobMessages;
|
||||||
|
|
||||||
var baseUrl = "http://localhost.or.something";
|
|
||||||
var aliHttpBackend;
|
|
||||||
var bobHttpBackend;
|
|
||||||
var aliClient;
|
|
||||||
var roomId = "!room:localhost";
|
|
||||||
var aliUserId = "@ali:localhost";
|
|
||||||
var aliDeviceId = "zxcvb";
|
|
||||||
var aliAccessToken = "aseukfgwef";
|
|
||||||
var bobClient;
|
|
||||||
var bobUserId = "@bob:localhost";
|
|
||||||
var bobDeviceId = "bvcxz";
|
|
||||||
var bobAccessToken = "fewgfkuesa";
|
|
||||||
var bobOneTimeKeys;
|
|
||||||
var aliDeviceKeys;
|
|
||||||
var bobDeviceKeys;
|
|
||||||
var bobDeviceCurve25519Key;
|
|
||||||
var bobDeviceEd25519Key;
|
|
||||||
var aliLocalStore;
|
|
||||||
var aliStorage;
|
|
||||||
var bobStorage;
|
|
||||||
var aliMessages;
|
|
||||||
var bobMessages;
|
|
||||||
|
|
||||||
beforeEach(function() {
|
function aliUploadsKeys() {
|
||||||
aliLocalStore = new MockStorageApi();
|
var uploadPath = "/keys/upload/" + aliDeviceId;
|
||||||
aliStorage = new sdk.WebStorageSessionStore(aliLocalStore);
|
aliHttpBackend.when("POST", uploadPath).respond(200, function(path, content) {
|
||||||
bobStorage = new sdk.WebStorageSessionStore(new MockStorageApi());
|
expect(content.one_time_keys).toEqual({});
|
||||||
utils.beforeEach(this);
|
aliDeviceKeys = content.device_keys;
|
||||||
|
return {one_time_key_counts: {curve25519: 0}};
|
||||||
aliHttpBackend = new HttpBackend();
|
|
||||||
aliClient = sdk.createClient({
|
|
||||||
baseUrl: baseUrl,
|
|
||||||
userId: aliUserId,
|
|
||||||
accessToken: aliAccessToken,
|
|
||||||
deviceId: aliDeviceId,
|
|
||||||
sessionStore: aliStorage,
|
|
||||||
request: aliHttpBackend.requestFn,
|
|
||||||
});
|
});
|
||||||
|
return q.all([
|
||||||
bobHttpBackend = new HttpBackend();
|
aliClient.uploadKeys(0),
|
||||||
bobClient = sdk.createClient({
|
aliHttpBackend.flush(uploadPath, 1),
|
||||||
baseUrl: baseUrl,
|
]).then(function() {
|
||||||
userId: bobUserId,
|
console.log("ali uploaded keys");
|
||||||
accessToken: bobAccessToken,
|
|
||||||
deviceId: bobDeviceId,
|
|
||||||
sessionStore: bobStorage,
|
|
||||||
request: bobHttpBackend.requestFn,
|
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
aliMessages = [];
|
function bobUploadsKeys() {
|
||||||
bobMessages = [];
|
|
||||||
});
|
|
||||||
|
|
||||||
afterEach(function() {
|
|
||||||
aliClient.stopClient();
|
|
||||||
bobClient.stopClient();
|
|
||||||
});
|
|
||||||
|
|
||||||
describe("Ali account setup", function() {
|
|
||||||
it("should have device keys", function(done) {
|
|
||||||
expect(aliClient.deviceKeys).toBeDefined();
|
|
||||||
expect(aliClient.deviceKeys.user_id).toEqual(aliUserId);
|
|
||||||
expect(aliClient.deviceKeys.device_id).toEqual(aliDeviceId);
|
|
||||||
done();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
function bobUploadsKeys() {
|
|
||||||
var uploadPath = "/keys/upload/bvcxz";
|
var uploadPath = "/keys/upload/bvcxz";
|
||||||
bobHttpBackend.when("POST", uploadPath).respond(200, function(path, content) {
|
bobHttpBackend.when("POST", uploadPath).respond(200, function(path, content) {
|
||||||
expect(content.one_time_keys).toEqual({});
|
expect(content.one_time_keys).toEqual({});
|
||||||
@@ -116,15 +82,9 @@ describe("MatrixClient crypto", function() {
|
|||||||
bobDeviceCurve25519Key = bobDeviceKeys.keys["curve25519:bvcxz"];
|
bobDeviceCurve25519Key = bobDeviceKeys.keys["curve25519:bvcxz"];
|
||||||
bobDeviceEd25519Key = bobDeviceKeys.keys["ed25519:bvcxz"];
|
bobDeviceEd25519Key = bobDeviceKeys.keys["ed25519:bvcxz"];
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
it("Bob uploads without one-time keys and with one-time keys", function(done) {
|
function aliDownloadsKeys() {
|
||||||
q()
|
|
||||||
.then(bobUploadsKeys)
|
|
||||||
.catch(utils.failTest).done(done);
|
|
||||||
});
|
|
||||||
|
|
||||||
function aliDownloadsKeys() {
|
|
||||||
var bobKeys = {};
|
var bobKeys = {};
|
||||||
bobKeys[bobDeviceId] = bobDeviceKeys;
|
bobKeys[bobDeviceId] = bobDeviceKeys;
|
||||||
aliHttpBackend.when("POST", "/keys/query").respond(200, function(path, content) {
|
aliHttpBackend.when("POST", "/keys/query").respond(200, function(path, content) {
|
||||||
@@ -147,16 +107,24 @@ describe("MatrixClient crypto", function() {
|
|||||||
expect(devices[bobDeviceId].keys).toEqual(bobDeviceKeys.keys);
|
expect(devices[bobDeviceId].keys).toEqual(bobDeviceKeys.keys);
|
||||||
expect(devices[bobDeviceId].verified).toBe(false);
|
expect(devices[bobDeviceId].verified).toBe(false);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
it("Ali downloads Bobs keys", function(done) {
|
function bobDownloadsKeys() {
|
||||||
q()
|
var aliKeys = {};
|
||||||
.then(bobUploadsKeys)
|
aliKeys[aliDeviceId] = aliDeviceKeys;
|
||||||
.then(aliDownloadsKeys)
|
bobHttpBackend.when("POST", "/keys/query").respond(200, function(path, content) {
|
||||||
.catch(utils.failTest).done(done);
|
expect(content.device_keys[aliUserId]).toEqual({});
|
||||||
|
var result = {};
|
||||||
|
result[aliUserId] = aliKeys;
|
||||||
|
return {device_keys: result};
|
||||||
});
|
});
|
||||||
|
return q.all([
|
||||||
|
bobClient.downloadKeys([aliUserId]),
|
||||||
|
bobHttpBackend.flush(),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
function aliEnablesEncryption() {
|
function aliEnablesEncryption() {
|
||||||
aliHttpBackend.when("POST", "/keys/claim").respond(200, function(path, content) {
|
aliHttpBackend.when("POST", "/keys/claim").respond(200, function(path, content) {
|
||||||
expect(content.one_time_keys[bobUserId][bobDeviceId]).toEqual("curve25519");
|
expect(content.one_time_keys[bobUserId][bobDeviceId]).toEqual("curve25519");
|
||||||
for (var keyId in bobOneTimeKeys) {
|
for (var keyId in bobOneTimeKeys) {
|
||||||
@@ -182,17 +150,40 @@ describe("MatrixClient crypto", function() {
|
|||||||
});
|
});
|
||||||
aliHttpBackend.flush();
|
aliHttpBackend.flush();
|
||||||
return p;
|
return p;
|
||||||
}
|
}
|
||||||
|
|
||||||
it("Ali enables encryption", function(done) {
|
function bobEnablesEncryption() {
|
||||||
q()
|
return bobClient.setRoomEncryption(roomId, {
|
||||||
.then(bobUploadsKeys)
|
algorithm: "m.olm.v1.curve25519-aes-sha2",
|
||||||
.then(aliDownloadsKeys)
|
members: [aliUserId, bobUserId]
|
||||||
.then(aliEnablesEncryption)
|
}).then(function(res) {
|
||||||
.catch(utils.failTest).done(done);
|
console.log("bob enabled encryption");
|
||||||
|
expect(res.missingUsers).toEqual([]);
|
||||||
|
expect(res.missingDevices).toEqual({});
|
||||||
|
expect(bobClient.isRoomEncrypted(roomId)).toBeTruthy();
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
function sendMessage(httpBackend, client) {
|
function aliSendsMessage() {
|
||||||
|
return sendMessage(aliHttpBackend, aliClient).then(function(content) {
|
||||||
|
aliMessages.push(content);
|
||||||
|
var ciphertext = content.ciphertext[bobDeviceCurve25519Key];
|
||||||
|
expect(ciphertext).toBeDefined();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function bobSendsMessage() {
|
||||||
|
return sendMessage(bobHttpBackend, bobClient).then(function(content) {
|
||||||
|
bobMessages.push(content);
|
||||||
|
var aliKeyId = "curve25519:" + aliDeviceId;
|
||||||
|
var aliDeviceCurve25519Key = aliDeviceKeys.keys[aliKeyId];
|
||||||
|
var ciphertext = content.ciphertext[aliDeviceCurve25519Key];
|
||||||
|
expect(ciphertext).toBeDefined();
|
||||||
|
return ciphertext;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function sendMessage(httpBackend, client) {
|
||||||
var path = "/send/m.room.encrypted/";
|
var path = "/send/m.room.encrypted/";
|
||||||
var sent;
|
var sent;
|
||||||
httpBackend.when("PUT", path).respond(200, function(path, content) {
|
httpBackend.when("PUT", path).respond(200, function(path, content) {
|
||||||
@@ -208,32 +199,19 @@ describe("MatrixClient crypto", function() {
|
|||||||
return q.all([p1, p2]).then(function() {
|
return q.all([p1, p2]).then(function() {
|
||||||
return sent;
|
return sent;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function aliSendsMessage() {
|
function aliRecvMessage() {
|
||||||
return sendMessage(aliHttpBackend, aliClient).then(function(content) {
|
var message = bobMessages.shift();
|
||||||
aliMessages.push(content);
|
return recvMessage(aliHttpBackend, aliClient, message);
|
||||||
var ciphertext = content.ciphertext[bobDeviceCurve25519Key];
|
}
|
||||||
expect(ciphertext).toBeDefined();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
it("Ali sends a message", function(done) {
|
function bobRecvMessage() {
|
||||||
q()
|
var message = aliMessages.shift();
|
||||||
.then(bobUploadsKeys)
|
return recvMessage(bobHttpBackend, bobClient, message);
|
||||||
.then(aliDownloadsKeys)
|
}
|
||||||
.then(aliEnablesEncryption)
|
|
||||||
.then(aliSendsMessage)
|
|
||||||
.catch(utils.failTest).done(done);
|
|
||||||
});
|
|
||||||
|
|
||||||
function startClient(httpBackend, client) {
|
function recvMessage(httpBackend, client, message) {
|
||||||
client.startClient();
|
|
||||||
httpBackend.when("GET", "/pushrules").respond(200, {});
|
|
||||||
httpBackend.when("POST", "/filter").respond(200, { filter_id: "fid" });
|
|
||||||
}
|
|
||||||
|
|
||||||
function recvMessage(httpBackend, client, message) {
|
|
||||||
var syncData = {
|
var syncData = {
|
||||||
next_batch: "x",
|
next_batch: "x",
|
||||||
rooms: {
|
rooms: {
|
||||||
@@ -267,12 +245,93 @@ describe("MatrixClient crypto", function() {
|
|||||||
startClient(httpBackend, client);
|
startClient(httpBackend, client);
|
||||||
httpBackend.flush();
|
httpBackend.flush();
|
||||||
return deferred.promise;
|
return deferred.promise;
|
||||||
|
}
|
||||||
|
|
||||||
|
function startClient(httpBackend, client) {
|
||||||
|
client.startClient();
|
||||||
|
httpBackend.when("GET", "/pushrules").respond(200, {});
|
||||||
|
httpBackend.when("POST", "/filter").respond(200, { filter_id: "fid" });
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
describe("MatrixClient crypto", function() {
|
||||||
|
if (!sdk.CRYPTO_ENABLED) {
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
function bobRecvMessage() {
|
beforeEach(function() {
|
||||||
var message = aliMessages.shift();
|
aliLocalStore = new MockStorageApi();
|
||||||
return recvMessage(bobHttpBackend, bobClient, message);
|
aliStorage = new sdk.WebStorageSessionStore(aliLocalStore);
|
||||||
}
|
bobStorage = new sdk.WebStorageSessionStore(new MockStorageApi());
|
||||||
|
utils.beforeEach(this);
|
||||||
|
|
||||||
|
aliHttpBackend = new HttpBackend();
|
||||||
|
aliClient = sdk.createClient({
|
||||||
|
baseUrl: "http://alis.server",
|
||||||
|
userId: aliUserId,
|
||||||
|
accessToken: aliAccessToken,
|
||||||
|
deviceId: aliDeviceId,
|
||||||
|
sessionStore: aliStorage,
|
||||||
|
request: aliHttpBackend.requestFn,
|
||||||
|
});
|
||||||
|
|
||||||
|
bobHttpBackend = new HttpBackend();
|
||||||
|
bobClient = sdk.createClient({
|
||||||
|
baseUrl: "http://bobs.server",
|
||||||
|
userId: bobUserId,
|
||||||
|
accessToken: bobAccessToken,
|
||||||
|
deviceId: bobDeviceId,
|
||||||
|
sessionStore: bobStorage,
|
||||||
|
request: bobHttpBackend.requestFn,
|
||||||
|
});
|
||||||
|
|
||||||
|
aliMessages = [];
|
||||||
|
bobMessages = [];
|
||||||
|
});
|
||||||
|
|
||||||
|
afterEach(function() {
|
||||||
|
aliClient.stopClient();
|
||||||
|
bobClient.stopClient();
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("Ali account setup", function() {
|
||||||
|
it("should have device keys", function(done) {
|
||||||
|
expect(aliClient.deviceKeys).toBeDefined();
|
||||||
|
expect(aliClient.deviceKeys.user_id).toEqual(aliUserId);
|
||||||
|
expect(aliClient.deviceKeys.device_id).toEqual(aliDeviceId);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("Bob uploads without one-time keys and with one-time keys", function(done) {
|
||||||
|
q()
|
||||||
|
.then(bobUploadsKeys)
|
||||||
|
.catch(utils.failTest).done(done);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("Ali downloads Bobs keys", function(done) {
|
||||||
|
q()
|
||||||
|
.then(bobUploadsKeys)
|
||||||
|
.then(aliDownloadsKeys)
|
||||||
|
.catch(utils.failTest).done(done);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("Ali enables encryption", function(done) {
|
||||||
|
q()
|
||||||
|
.then(bobUploadsKeys)
|
||||||
|
.then(aliDownloadsKeys)
|
||||||
|
.then(aliEnablesEncryption)
|
||||||
|
.catch(utils.failTest).done(done);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("Ali sends a message", function(done) {
|
||||||
|
q()
|
||||||
|
.then(bobUploadsKeys)
|
||||||
|
.then(aliDownloadsKeys)
|
||||||
|
.then(aliEnablesEncryption)
|
||||||
|
.then(aliSendsMessage)
|
||||||
|
.catch(utils.failTest).done(done);
|
||||||
|
});
|
||||||
|
|
||||||
it("Bob receives a message", function(done) {
|
it("Bob receives a message", function(done) {
|
||||||
q()
|
q()
|
||||||
@@ -296,66 +355,6 @@ describe("MatrixClient crypto", function() {
|
|||||||
.catch(utils.failTest).done(done);
|
.catch(utils.failTest).done(done);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
function aliUploadsKeys() {
|
|
||||||
var uploadPath = "/keys/upload/" + aliDeviceId;
|
|
||||||
aliHttpBackend.when("POST", uploadPath).respond(200, function(path, content) {
|
|
||||||
expect(content.one_time_keys).toEqual({});
|
|
||||||
aliDeviceKeys = content.device_keys;
|
|
||||||
return {one_time_key_counts: {curve25519: 0}};
|
|
||||||
});
|
|
||||||
return q.all([
|
|
||||||
aliClient.uploadKeys(0),
|
|
||||||
aliHttpBackend.flush(uploadPath, 1),
|
|
||||||
]).then(function() {
|
|
||||||
console.log("ali uploaded keys");
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
function bobDownloadsKeys() {
|
|
||||||
var aliKeys = {};
|
|
||||||
aliKeys[aliDeviceId] = aliDeviceKeys;
|
|
||||||
bobHttpBackend.when("POST", "/keys/query").respond(200, function(path, content) {
|
|
||||||
expect(content.device_keys[aliUserId]).toEqual({});
|
|
||||||
var result = {};
|
|
||||||
result[aliUserId] = aliKeys;
|
|
||||||
return {device_keys: result};
|
|
||||||
});
|
|
||||||
return q.all([
|
|
||||||
bobClient.downloadKeys([aliUserId]),
|
|
||||||
bobHttpBackend.flush(),
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
|
|
||||||
function bobEnablesEncryption() {
|
|
||||||
return bobClient.setRoomEncryption(roomId, {
|
|
||||||
algorithm: "m.olm.v1.curve25519-aes-sha2",
|
|
||||||
members: [aliUserId, bobUserId]
|
|
||||||
}).then(function(res) {
|
|
||||||
console.log("bob enabled encryption");
|
|
||||||
expect(res.missingUsers).toEqual([]);
|
|
||||||
expect(res.missingDevices).toEqual({});
|
|
||||||
expect(bobClient.isRoomEncrypted(roomId)).toBeTruthy();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
function bobSendsMessage() {
|
|
||||||
return sendMessage(bobHttpBackend, bobClient).then(function(content) {
|
|
||||||
bobMessages.push(content);
|
|
||||||
var aliKeyId = "curve25519:" + aliDeviceId;
|
|
||||||
var aliDeviceCurve25519Key = aliDeviceKeys.keys[aliKeyId];
|
|
||||||
var ciphertext = content.ciphertext[aliDeviceCurve25519Key];
|
|
||||||
expect(ciphertext).toBeDefined();
|
|
||||||
return ciphertext;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
function aliRecvMessage() {
|
|
||||||
var message = bobMessages.shift();
|
|
||||||
return recvMessage(aliHttpBackend, aliClient, message);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
it("Bob replies to the message", function(done) {
|
it("Bob replies to the message", function(done) {
|
||||||
q()
|
q()
|
||||||
.then(bobUploadsKeys)
|
.then(bobUploadsKeys)
|
||||||
|
|||||||
Reference in New Issue
Block a user