Skip to content

Commit

Permalink
Use AsyncTestDelegate in AssertThrows
Browse files Browse the repository at this point in the history
  • Loading branch information
jpassing committed Mar 16, 2024
1 parent 196647f commit bc7e137
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 56 deletions.
4 changes: 2 additions & 2 deletions sources/Jpki.NUnit/AssertThrows.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ private static Exception Unwrap(this Exception e)
}
}

public static TActual? AggregateException<TActual>(TestDelegate code) where TActual : Exception // TODO: use Task
public static TActual? AggregateException<TActual>(AsyncTestDelegate code) where TActual : Exception
{
return AssertThat.Throws<TActual>(() =>
{
try
{
code();
code().Wait();
}
catch (AggregateException e)
{
Expand Down
4 changes: 2 additions & 2 deletions sources/Jpki.Powershell.Test/Runtime/Http/TestHttpServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public void DisposeStopsListener()
runTask = server.RunAsync(CancellationToken.None);
}

AssertThrows.AggregateException<TaskCanceledException>(() => runTask.Wait());
AssertThrows.AggregateException<TaskCanceledException>(() => runTask);
}

//---------------------------------------------------------------------
Expand All @@ -59,7 +59,7 @@ public void StopStopsListener()
var runTask = server.RunAsync(CancellationToken.None);
server.Stop();

AssertThrows.AggregateException<TaskCanceledException>(() => runTask.Wait());
AssertThrows.AggregateException<TaskCanceledException>(() => runTask);
}

//---------------------------------------------------------------------
Expand Down
4 changes: 2 additions & 2 deletions sources/Jpki.Powershell.Test/Runtime/Http/TestRestClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public void WhenUrlNotFound_ThenGetStringThrowsException()
AssertThrows.AggregateException<HttpRequestException>(
() => client.GetStringAsync(
NotFoundUrl,
CancellationToken.None).Wait());
CancellationToken.None));
}

//---------------------------------------------------------------------
Expand Down Expand Up @@ -122,7 +122,7 @@ public void WhenUrlNotFound_ThenGetJsonThrowsException()
AssertThrows.AggregateException<HttpRequestException>(
() => client.GetJsonAsync<SampleResource>(
NotFoundUrl,
CancellationToken.None).Wait());
CancellationToken.None));
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions sources/Jpki.Powershell.Test/Runtime/Text/TestJson.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ public void DeserializeStream()
var c = Json.Deserialize<SomeClass>(stream);

AssertThat.NotNull(c);
AssertThat.AreEqual("aa", c.A);
AssertThat.AreEqual("bb", c.B);
AssertThat.AreEqual("aa", c!.A);
AssertThat.AreEqual("bb", c!.B);
}
}

Expand All @@ -74,8 +74,8 @@ public void DeserializeString()
var c = Json.Deserialize<SomeClass>(json);

AssertThat.NotNull(c);
AssertThat.AreEqual("aa", c.A);
AssertThat.AreEqual("bb", c.B);
AssertThat.AreEqual("aa", c!.A);
AssertThat.AreEqual("bb", c!.B);
}

[Test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,36 +84,32 @@ public void ApiVersion()
public void WhenSignatureAlgorithmsEmpty_ThenCreateCredentialThrowsException()
{
AssertThrows.AggregateException<ArgumentException>(
() => Authenticators.WindowsHello
.CreateCredentialAsync(
this.form!.Handle,
Data.NonResidentRelyingParty,
Data.User,
ClientData.FromJson("{}"),
new AttestationOptions()
{
SignatureAlgorithms = Array.Empty<CoseSignatureAlgorithm>()
},
CancellationToken.None)
.Wait());
() => Authenticators.WindowsHello.CreateCredentialAsync(
this.form!.Handle,
Data.NonResidentRelyingParty,
Data.User,
ClientData.FromJson("{}"),
new AttestationOptions()
{
SignatureAlgorithms = Array.Empty<CoseSignatureAlgorithm>()
},
CancellationToken.None));
}

[Test]
public void WhenSignatureAlgorithmsInvalid_ThenCreateCredentialThrowsException()
{
AssertThrows.AggregateException<ArgumentException>(
() => Authenticators.WindowsHello
.CreateCredentialAsync(
this.form!.Handle,
Data.NonResidentRelyingParty,
Data.User,
ClientData.FromJson("{}"),
new AttestationOptions()
{
SignatureAlgorithms = new[] { (CoseSignatureAlgorithm)9999 }
},
CancellationToken.None)
.Wait());
() => Authenticators.WindowsHello.CreateCredentialAsync(
this.form!.Handle,
Data.NonResidentRelyingParty,
Data.User,
ClientData.FromJson("{}"),
new AttestationOptions()
{
SignatureAlgorithms = new[] { (CoseSignatureAlgorithm)9999 }
},
CancellationToken.None));
}

[Test]
Expand All @@ -127,18 +123,16 @@ public void WhenNoPlatformAuthenticatorPresent_ThenCreateCredentialThrowsExcepti
}

AssertThrows.AggregateException<WebAuthnException>(
() => Authenticators.WindowsHello
.CreateCredentialAsync(
this.form!.Handle,
Data.NonResidentRelyingParty,
Data.User,
ClientData.FromJson("{}"),
new AttestationOptions()
{
AuthenticatorAttachment = AuthenticatorAttachment.Platform
},
CancellationToken.None)
.Wait());
() => Authenticators.WindowsHello.CreateCredentialAsync(
this.form!.Handle,
Data.NonResidentRelyingParty,
Data.User,
ClientData.FromJson("{}"),
new AttestationOptions()
{
AuthenticatorAttachment = AuthenticatorAttachment.Platform
},
CancellationToken.None));
}

[Test]
Expand All @@ -147,20 +141,19 @@ public async Task WhenCancelled_ThenCreateCredentialThrowsException()
{
using (var cts = new CancellationTokenSource())
{
var attestationTask = Authenticators.WindowsHello
.CreateCredentialAsync(
this.form!.Handle,
Data.NonResidentRelyingParty,
Data.User,
ClientData.FromJson("{}"),
new AttestationOptions(),
cts.Token);
var attestationTask = Authenticators.WindowsHello.CreateCredentialAsync(
this.form!.Handle,
Data.NonResidentRelyingParty,
Data.User,
ClientData.FromJson("{}"),
new AttestationOptions(),
cts.Token);

await Task.Delay(250);
cts.Cancel();

AssertThrows.AggregateException<OperationCanceledException>(
() => attestationTask.Wait());
() => attestationTask);
}
}

Expand All @@ -186,7 +179,7 @@ public async Task WhenCancelled_ThenCreateAssertionThrowsException()
cts.Cancel();

AssertThrows.AggregateException<OperationCanceledException>(
() => attestationTask.Wait());
() => attestationTask);
}
}

Expand Down

0 comments on commit bc7e137

Please sign in to comment.