Skip to content

Commit

Permalink
Merge pull request #132 from dorssel/purge
Browse files Browse the repository at this point in the history
Rename DeleteAll to Purge
  • Loading branch information
dorssel authored Jan 4, 2025
2 parents d587aca + 0b8b689 commit 9045ae6
Show file tree
Hide file tree
Showing 24 changed files with 221 additions and 140 deletions.
2 changes: 1 addition & 1 deletion Examples/WebAssembly/LocalStorageStateManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public void DeletePublicPart()
LocalStorage.RemoveItem(GetKey(XmssKeyPart.Public));
}

public void DeleteAll()
public void Purge()
{
LocalStorage.RemoveItems([
GetKey(XmssKeyPart.PrivateStateless),
Expand Down
4 changes: 2 additions & 2 deletions Examples/WebAssembly/Pages/Home.razor
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,15 @@ else
void DestroyKey()
{
Xmss.Dispose();
new LocalStorageStateManager(LocalStorage, KeyName).DeleteAll();
new LocalStorageStateManager(LocalStorage, KeyName).Purge();
MessageToSign = string.Empty;
Signature = Array.Empty<byte>();
Xmss = new Xmss();
}

void CreateKey()
{
new LocalStorageStateManager(LocalStorage, KeyName).DeleteAll();
new LocalStorageStateManager(LocalStorage, KeyName).Purge();
Xmss.GeneratePrivateKey(new LocalStorageStateManager(LocalStorage, KeyName), XmssParameterSet.XMSS_SHA2_10_256, false);
}

Expand Down
33 changes: 33 additions & 0 deletions UnitTests/Internal.UnitTests/XmssExceptionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,37 @@ public void ThrowIfNotOkay_Okay()
{
XmssException.ThrowIfNotOkay(XmssError.XMSS_OKAY);
}

[TestMethod]
public void ThrowFaultDetectedIf_False()
{
XmssException.ThrowFaultDetectedIf(false);
}

[TestMethod]
public void ThrowFaultDetectedIf_True()
{
Assert.ThrowsException<XmssException>(() =>
{
XmssException.ThrowFaultDetectedIf(true);
});
}

[TestMethod]
public void ThrowFaultDetectedIf_Null()
{
XmssException.ThrowFaultDetectedIf(null);
}

[TestMethod]
public void ThrowFaultDetectedIf_Exception()
{
var inner = new InvalidOperationException();

var exception = Assert.ThrowsException<XmssException>(() =>
{
XmssException.ThrowFaultDetectedIf(inner);
});
Assert.AreEqual(inner, exception.InnerException);
}
}
6 changes: 3 additions & 3 deletions UnitTests/UnitTests/CalculateTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ sealed class CalculateTests
[TestMethod]
public async Task CalculatePublicKeyAsync_AndImport()
{
var stateManager = new MemoryStateManager();
var stateManager = new MockStateManager();

// generate
{
Expand Down Expand Up @@ -49,7 +49,7 @@ public async Task CalculatePublicKeyAsync_AndImport()
[TestMethod]
public async Task CalculatePublicKeyAsync_Report_AndGenerateAgain()
{
var stateManager = new MemoryStateManager();
var stateManager = new MockStateManager();

// generate
{
Expand Down Expand Up @@ -84,7 +84,7 @@ await Assert.ThrowsExceptionAsync<InvalidOperationException>(async () =>
[TestMethod]
public async Task CalculatePublicKeyAsync_DeletePublicFails()
{
var stateManager = new MemoryStateManager();
var stateManager = new MockStateManager();

// generate
{
Expand Down
2 changes: 1 addition & 1 deletion UnitTests/UnitTests/CryptoConfigTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public void Create_Name()
public void SignatureAlgorithm(XmssParameterSet parameterSet)
{
using var xmss = new Xmss();
xmss.GeneratePrivateKey(new MemoryStateManager(), parameterSet, false);
xmss.GeneratePrivateKey(new MockStateManager(), parameterSet, false);

Assert.IsFalse(string.IsNullOrEmpty(xmss.SignatureAlgorithm));
}
Expand Down
16 changes: 8 additions & 8 deletions UnitTests/UnitTests/GenerateTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public void GeneratePrivateKey(XmssParameterSet parameterSet)
Assert.IsFalse(xmss.HasPrivateKey);
Assert.IsFalse(xmss.HasPublicKey);

xmss.GeneratePrivateKey(new MemoryStateManager(), parameterSet, true);
xmss.GeneratePrivateKey(new MockStateManager(), parameterSet, true);

Assert.IsTrue(xmss.HasPrivateKey);
Assert.IsFalse(xmss.HasPublicKey);
Expand All @@ -37,13 +37,13 @@ public void GeneratePrivateKey_AfterPrivateKey()
Assert.IsFalse(xmss.HasPrivateKey);
Assert.IsFalse(xmss.HasPublicKey);

xmss.GeneratePrivateKey(new MemoryStateManager(), XmssParameterSet.XMSS_SHA2_10_256, false);
xmss.GeneratePrivateKey(new MockStateManager(), XmssParameterSet.XMSS_SHA2_10_256, false);

Assert.IsTrue(xmss.HasPrivateKey);
Assert.IsFalse(xmss.HasPublicKey);
Assert.AreEqual(XmssParameterSet.XMSS_SHA2_10_256, xmss.ParameterSet);

xmss.GeneratePrivateKey(new MemoryStateManager(), XmssParameterSet.XMSS_SHA2_16_256, false);
xmss.GeneratePrivateKey(new MockStateManager(), XmssParameterSet.XMSS_SHA2_16_256, false);

Assert.IsTrue(xmss.HasPrivateKey);
Assert.IsFalse(xmss.HasPublicKey);
Expand All @@ -53,12 +53,12 @@ public void GeneratePrivateKey_AfterPrivateKey()
[TestMethod]
public void GeneratePrivateKey_StoreStatelessFails()
{
var stateManager = new MemoryStateManager();
var stateManager = new MockStateManager();
using var xmss = new Xmss();

stateManager.Setup(); // Load stateful (verify not exists)
stateManager.Setup(); // Load stateless (verify not exists)
stateManager.Setup(); // DeleteAll
stateManager.Setup(); // Purge
stateManager.Setup(false); // Store stateless

Assert.ThrowsException<XmssStateManagerException>(() =>
Expand All @@ -71,14 +71,14 @@ public void GeneratePrivateKey_StoreStatelessFails()
[TestMethod]
public void GeneratePrivateKey_StoreStatelessAndRollbackFail()
{
var stateManager = new MemoryStateManager();
var stateManager = new MockStateManager();
using var xmss = new Xmss();

stateManager.Setup(); // Load stateful (verify not exists)
stateManager.Setup(); // Load stateless (verify not exists)
stateManager.Setup(); // DeleteAll
stateManager.Setup(); // Purge
stateManager.Setup(false); // Store stateless
stateManager.Setup(false); // DeleteAll
stateManager.Setup(false); // Purge

Assert.ThrowsException<AggregateException>(() =>
{
Expand Down
10 changes: 5 additions & 5 deletions UnitTests/UnitTests/ImportTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ sealed class ImportTests
[DataRow(XmssParameterSet.XMSS_SHAKE256_20_256)]
public void ImportPrivateKey(XmssParameterSet parameterSet)
{
var stateManager = new MemoryStateManager();
var stateManager = new MockStateManager();

{
using var tmpXmss = new Xmss();
Expand All @@ -41,8 +41,8 @@ public void ImportPrivateKey(XmssParameterSet parameterSet)
[TestMethod]
public void ImportPrivateKey_AfterPrivateKey()
{
var stateManager1 = new MemoryStateManager();
var stateManager2 = new MemoryStateManager();
var stateManager1 = new MockStateManager();
var stateManager2 = new MockStateManager();

{
using var tmpXmss = new Xmss();
Expand Down Expand Up @@ -75,7 +75,7 @@ public void ImportPrivateKey_AfterPrivateKey()
public void ImportPrivateKey_Invalid()
{
using var xmss = new Xmss();
var stateManager = new MemoryStateManager();
var stateManager = new MockStateManager();
xmss.GeneratePrivateKey(stateManager, XmssParameterSet.XMSS_SHA2_10_256, false);

// corrupt blob such that not even the parameter set is valid
Expand Down Expand Up @@ -144,7 +144,7 @@ public void ImportRfcPublicKey_WrongSize()
public void ImportRfcPublicKey_AfterPrivateKey()
{
using var xmss = new Xmss();
xmss.GeneratePrivateKey(new MemoryStateManager(), XmssParameterSet.XMSS_SHA2_10_256, false);
xmss.GeneratePrivateKey(new MockStateManager(), XmssParameterSet.XMSS_SHA2_10_256, false);

Assert.IsTrue(xmss.HasPrivateKey);
Assert.IsFalse(xmss.HasPublicKey);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace UnitTests;

sealed class MemoryStateManager()
sealed class MockStateManager()
: IXmssStateManager
{
readonly Dictionary<XmssKeyPart, byte[]?> State = new()
Expand Down Expand Up @@ -84,7 +84,7 @@ public void DeletePublicPart()
State[XmssKeyPart.Public] = null;
}

public void DeleteAll()
public void Purge()
{
ThrowIfPlanned();

Expand Down
Loading

0 comments on commit 9045ae6

Please sign in to comment.