Skip to content

Commit

Permalink
Test that we can decrypt history messages
Browse files Browse the repository at this point in the history
This was previously untested.

The new test in `crypto.test.js` is taken from a test Simon added
in #1923, but I wanted to separate this test from all the other work that’s
being done in that PR. I’ve also copied this new test to create a
similar test for the modular variant of the library.

Co-authored-by: Simon Woolf <[email protected]>
  • Loading branch information
lawrence-forooghian and SimonWoolf committed Dec 5, 2024
1 parent 416c9b0 commit e64820d
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 0 deletions.
49 changes: 49 additions & 0 deletions test/browser/modular.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,39 @@ function registerAblyModularTests(Helper) {
);
}

async function testIsAbleToDecryptHistoryMessages(helper, clientClassConfig) {
const clientOptions = helper.ablyClientOptions();

const client = new clientClassConfig.clientClass({
...clientOptions,
plugins: {
...clientClassConfig.additionalPlugins,
FetchRequest,
Crypto,
},
});

await (clientClassConfig.isRealtime ? monitorConnectionThenCloseAndFinish : async (helper, op) => await op())(
helper,
async () => {
const channelName = 'encrypted_history',
messageText = 'Test message';

const key = await generateRandomKey();

const channel = client.channels.get(channelName, { cipher: { key: key } });
await channel.publish('event0', messageText);
let items;
await helper.waitFor(async () => {
items = (await channel.history()).items;
return items.length > 0;
}, 10_000);
expect(items[0].data).to.equal(messageText);
},
client,
);
}

for (const clientClassConfig of [
{ clientClass: BaseRest, isRealtime: false },
{
Expand All @@ -522,6 +555,22 @@ function registerAblyModularTests(Helper) {
});
});
}

for (const clientClassConfig of [
{ clientClass: BaseRest, isRealtime: false },
{
clientClass: BaseRealtime,
additionalPlugins: { WebSocketTransport, Rest },
isRealtime: true,
},
]) {
describe(clientClassConfig.clientClass.name, () => {
/** @nospec */
it('is able to decrypt history messages', async function () {
await testIsAbleToDecryptHistoryMessages(this.test.helper, clientClassConfig);
});
});
}
});
});

Expand Down
9 changes: 9 additions & 0 deletions test/common/modules/shared_helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,15 @@ define([
dumpPrivateApiUsage() {
privateApiRecorder.dump();
}

async waitFor(condition, remaining) {
const success = await condition();
if (success || remaining <= 0) {
return success;
}
await this.setTimeoutAsync(100);
return this.waitFor(condition, remaining - 100);
}
}

SharedHelper.testOnAllTransports.skip = function (thisInDescribe, name, testFn) {
Expand Down
25 changes: 25 additions & 0 deletions test/realtime/crypto.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -773,6 +773,31 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async
});
});

/**
* @spec RSL5a
*/
it('encrypted history', async function () {
if (!Crypto) {
done(new Error('Encryption not supported'));
return;
}

const helper = this.test.helper,
rest = helper.AblyRest(),
channelName = 'encrypted_history',
messageText = 'Test message';

const key = await Crypto.generateRandomKey();
const channel = rest.channels.get(channelName, { cipher: { key: key } });
await channel.publish('event0', messageText);
let items;
await helper.waitFor(async () => {
items = (await channel.history()).items;
return items.length > 0;
}, 10_000);
expect(items[0].data).to.equal(messageText);
});

/**
* Connect twice to the service, using different cipher keys.
* Publish an encrypted message on that channel using
Expand Down

0 comments on commit e64820d

Please sign in to comment.