Skip to content

Commit

Permalink
Stricter tests
Browse files Browse the repository at this point in the history
  • Loading branch information
brickpop committed Aug 19, 2024
1 parent e628286 commit 6387181
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 8 deletions.
17 changes: 15 additions & 2 deletions tests/asymmetric.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,23 @@ describe("Symmetric encryption", () => {
const ciphertext1 = encrypt(bytes, bob.publicKey);
const ciphertext2 = encrypt("Hello world", bob.publicKey);

// ok
expect(() => decryptBytes(ciphertext1, bob)).not.toThrow();
expect(() => decryptString(ciphertext2, bob)).not.toThrow();

expect(() => decryptBytes(ciphertext1, cindy)).toThrow();
expect(() => decryptString(ciphertext2, cindy)).toThrow();
// ko
try {
decryptBytes(ciphertext1, cindy);
throw new Error("Should have thrown but didn't");
} catch (err: any) {
expect(err.message).toBe("incorrect key pair for the given ciphertext");
}

try {
decryptString(ciphertext2, cindy);
throw new Error("Should have thrown but didn't");
} catch (err: any) {
expect(err.message).toBe("incorrect key pair for the given ciphertext");
}
});
});
14 changes: 10 additions & 4 deletions tests/proposal-encryption.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,12 @@ describe("Proposal data encryption", () => {
}).not.toThrow();

for (const otherKey of otherKeys) {
expect(() => {
try {
decryptProposal(data, otherKey);
}).toThrow();
throw new Error("Should have thrown but didn't");
} catch (err: any) {
expect(err.message).toBe("wrong secret key for the given ciphertext");
}
}
});
});
Expand Down Expand Up @@ -150,9 +153,12 @@ describe("Symmetric key encryption across members", () => {
);

for (let i = 0; i < encryptedItems.length; i++) {
expect(() => {
try {
decryptSymmetricKey(encryptedItems, intruders[i]);
}).toThrow();
throw new Error("Should have thrown but didn't");
} catch (err: any) {
expect(err.message).toBe("The given keypair cannot decrypt any of the ciphertext's");
}
}

for (let i = 0; i < encryptedItems.length; i++) {
Expand Down
15 changes: 13 additions & 2 deletions tests/symmetric.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,18 @@ describe("Symmetric encryption", () => {
expect(() => decryptBytes(encryptedPayload1, symKey)).not.toThrow();
expect(() => decryptString(encryptedPayload2, symKey)).not.toThrow();

expect(() => decryptBytes(encryptedPayload1, wrongKey)).toThrow();
expect(() => decryptString(encryptedPayload2, wrongKey)).toThrow();
try {
decryptBytes(encryptedPayload1, wrongKey);
throw new Error("Should have thrown but didn't");
} catch (err: any) {
expect(err.message).toBe("wrong secret key for the given ciphertext");
}

try {
decryptString(encryptedPayload2, wrongKey);
throw new Error("Should have thrown but didn't");
} catch (err: any) {
expect(err.message).toBe("wrong secret key for the given ciphertext");
}
});
});

0 comments on commit 6387181

Please sign in to comment.