Skip to content

Commit

Permalink
Helper for decrypting existing decryption failures
Browse files Browse the repository at this point in the history
  • Loading branch information
richvdh committed Mar 25, 2024
1 parent 63d450e commit 1f7f70d
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 19 deletions.
31 changes: 30 additions & 1 deletion spec/unit/testing.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

import { mkDecryptionFailureMatrixEvent, mkEncryptedMatrixEvent, mkMatrixEvent } from "../../src/testing";
import {
decryptExistingEvent,
mkDecryptionFailureMatrixEvent,
mkEncryptedMatrixEvent,
mkMatrixEvent,
} from "../../src/testing";
import { EventType } from "../../src";
import { DecryptionFailureCode } from "../../src/crypto-api";

Expand Down Expand Up @@ -88,4 +93,28 @@ describe("testing", () => {
expect(event.isState()).toBe(false);
});
});

describe("decryptExistingEvent", () => {
it("decrypts an event", async () => {
const event = await mkDecryptionFailureMatrixEvent({
sender: "@alice:test",
roomId: "!test:room",
code: DecryptionFailureCode.UNKNOWN_ERROR,
msg: "blah",
});

expect(event.isEncrypted()).toBe(true);
expect(event.isDecryptionFailure()).toBe(true);
await decryptExistingEvent(event, {
plainContent: { body: "blah" },
plainType: "m.room.test",
});

expect(event.isEncrypted()).toBe(true);
expect(event.isDecryptionFailure()).toBe(false);
expect(event.decryptionFailureReason).toBe(null);
expect(event.getContent()).toEqual({ body: "blah" });
expect(event.getType()).toEqual("m.room.test");
});
});
});
53 changes: 35 additions & 18 deletions src/testing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,6 @@ export async function mkEncryptedMatrixEvent(opts: {
/** Optional `event_id` for the event. If provided will be used as event ID; else an ID is generated. */
eventId?: string;
}): Promise<MatrixEvent> {
// we construct an event which has been decrypted by stubbing out CryptoBackend.decryptEvent and then
// calling MatrixEvent.attemptDecryption.

const mxEvent = mkMatrixEvent({
type: EventType.RoomMessageEncrypted,
roomId: opts.roomId,
Expand All @@ -117,21 +114,7 @@ export async function mkEncryptedMatrixEvent(opts: {
eventId: opts.eventId,
});

const decryptionResult: EventDecryptionResult = {
claimedEd25519Key: "",
clearEvent: {
type: opts.plainType,
content: opts.plainContent,
},
forwardingCurve25519KeyChain: [],
senderCurve25519Key: "",
untrusted: false,
};

const mockCrypto = {
decryptEvent: async (_ev): Promise<IEventDecryptionResult> => decryptionResult,
} as Parameters<MatrixEvent["attemptDecryption"]>[0];
await mxEvent.attemptDecryption(mockCrypto);
await decryptExistingEvent(mxEvent, { plainType: opts.plainType, plainContent: opts.plainContent });
return mxEvent;
}

Expand Down Expand Up @@ -172,3 +155,37 @@ export async function mkDecryptionFailureMatrixEvent(opts: {
await mxEvent.attemptDecryption(mockCrypto);
return mxEvent;
}

/**
* Given an event previously returned by {@link mkDecryptionFailureMatrixEvent}, simulate a successful re-decryption
* attempt.
*
* @param mxEvent - The event that will be decrypted.
* @param opts - New data for the successful decryption.
*/
export async function decryptExistingEvent(
mxEvent: MatrixEvent,
opts: {
/** The type the event will have, once it has been decrypted. */
plainType: EventType | string;

/** The content the event will have, once it has been decrypted. */
plainContent: IContent;
},
): Promise<void> {
const decryptionResult: EventDecryptionResult = {
claimedEd25519Key: "",
clearEvent: {
type: opts.plainType,
content: opts.plainContent,
},
forwardingCurve25519KeyChain: [],
senderCurve25519Key: "",
untrusted: false,
};

const mockCrypto = {
decryptEvent: async (_ev): Promise<EventDecryptionResult> => decryptionResult,
} as Parameters<MatrixEvent["attemptDecryption"]>[0];
await mxEvent.attemptDecryption(mockCrypto);
}

0 comments on commit 1f7f70d

Please sign in to comment.