Repository navigation
Make dispose idempotent and reject use after dispose #38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,7 @@ | |
| import org.junit.jupiter.api.Assertions; | ||
| import org.junit.jupiter.api.BeforeAll; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.function.Executable; | ||
| import org.junit.jupiter.params.ParameterizedTest; | ||
| import org.junit.jupiter.params.provider.MethodSource; | ||
|
|
||
|
|
@@ -64,6 +65,46 @@ public void testAllSigs(String sig_name) { | |
| System.out.println(sb.toString()); | ||
| } | ||
|
|
||
| /** | ||
| * Test that disposing of a Sig twice is harmless | ||
| */ | ||
| @Test | ||
| public void testDisposeSigTwice() { | ||
| Signature signer = new Signature("ML-DSA-44"); | ||
| signer.generate_keypair(); | ||
|
|
||
| signer.dispose_sig(); | ||
| signer.dispose_sig(); | ||
| } | ||
|
|
||
| /** | ||
| * Test that a Sig cannot be used after it has been disposed of | ||
| */ | ||
| @Test | ||
| public void testUseAfterDisposeSigExpectedException() { | ||
| byte[] context = "01234567890".getBytes(); | ||
| Signature signer = new Signature("ML-DSA-44"); | ||
| byte[] signer_public_key = signer.generate_keypair(); | ||
| byte[] signature = signer.sign(message); | ||
|
|
||
| signer.dispose_sig(); | ||
|
|
||
| assertDisposed(() -> signer.generate_keypair()); | ||
| assertDisposed(() -> signer.sign(message)); | ||
| assertDisposed(() -> signer.verify(message, signature, signer_public_key)); | ||
| assertDisposed(() -> signer.sign(message, context)); | ||
| assertDisposed(() -> signer.verify(message, signature, context, signer_public_key)); | ||
| assertDisposed(() -> signer.export_public_key()); | ||
| assertDisposed(() -> signer.export_secret_key()); | ||
| assertDisposed(() -> signer.print_signature()); | ||
| assertDisposed(() -> signer.print_details()); | ||
| } | ||
|
|
||
| private static void assertDisposed(Executable executable) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If print/export should also fail after dispose, assert that here. Now they still succeed on cached/wiped Java state.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. The use-after-dispose tests in |
||
| IllegalStateException e = Assertions.assertThrows(IllegalStateException.class, executable); | ||
| Assertions.assertEquals("Signature has been disposed", e.getMessage()); | ||
| } | ||
|
|
||
| /** | ||
| * Test Sigs with context. | ||
| */ | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😬 we had missed this, good catch.