Skip to content

Commit 510d61a

Browse files
committed
tests: cover Open() exception types and short input
Tighten three existing tests from ThrowsAny<Exception> to Throws<NKeysException> now that Open() normalizes exceptions. Add a theory test verifying short inputs (0, 28, 29, 43 bytes) are rejected with NKeysException.
1 parent aa1f613 commit 510d61a

1 file changed

Lines changed: 26 additions & 3 deletions

File tree

NATS.NKeys.Tests/NKeysTest.cs

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ public void DecodePubCurveKey_blackbox_wrong_sender_fails_open()
468468
var sealed1 = alice.Seal(Encoding.UTF8.GetBytes("secret"), bob.GetPublicKey());
469469

470470
// Bob tries to open with Eve's public key as sender — must fail
471-
Assert.ThrowsAny<Exception>(() => bob.Open(sealed1, eve.GetPublicKey()));
471+
Assert.Throws<NKeysException>(() => bob.Open(sealed1, eve.GetPublicKey()));
472472
}
473473

474474
[Fact]
@@ -482,7 +482,7 @@ public void DecodePubCurveKey_blackbox_wrong_receiver_fails_open()
482482
var sealed1 = alice.Seal(Encoding.UTF8.GetBytes("secret"), bob.GetPublicKey());
483483

484484
// Eve tries to open something meant for Bob
485-
Assert.ThrowsAny<Exception>(() => eve.Open(sealed1, alice.GetPublicKey()));
485+
Assert.Throws<NKeysException>(() => eve.Open(sealed1, alice.GetPublicKey()));
486486
}
487487

488488
[Fact]
@@ -496,7 +496,7 @@ public void DecodePubCurveKey_blackbox_tampered_ciphertext_fails()
496496
// Flip a byte in the encrypted payload (after version + nonce header)
497497
sealed1[30] ^= 0xFF;
498498

499-
Assert.ThrowsAny<Exception>(() => bob.Open(sealed1, alice.GetPublicKey()));
499+
Assert.Throws<NKeysException>(() => bob.Open(sealed1, alice.GetPublicKey()));
500500
}
501501

502502
[Fact]
@@ -517,6 +517,29 @@ public void DecodePubCurveKey_blackbox_each_seal_produces_different_ciphertext()
517517
Assert.Equal(message, bob.Open(sealed2, alice.GetPublicKey()));
518518
}
519519

520+
[Theory]
521+
[InlineData(0)]
522+
[InlineData(28)]
523+
[InlineData(29)]
524+
[InlineData(43)]
525+
public void Open_rejects_short_input(int length)
526+
{
527+
var kp = KeyPair.CreatePair(PrefixByte.Curve);
528+
var input = new byte[length];
529+
530+
// Fill with valid version header if long enough
531+
if (length >= 4)
532+
{
533+
input[0] = (byte)'x';
534+
input[1] = (byte)'k';
535+
input[2] = (byte)'v';
536+
input[3] = (byte)'1';
537+
}
538+
539+
var ex = Assert.Throws<NKeysException>(() => kp.Open(input, kp.GetPublicKey()));
540+
Assert.Equal("Encrypted input is not valid", ex.Message);
541+
}
542+
520543
[Fact]
521544
public void Public_key_does_not_have_seed_nor_secret_key()
522545
{

0 commit comments

Comments
 (0)