Skip to content

Commit 83b4652

Browse files
committed
test: fix remaining Nsubstitute tests
1 parent 2ec3f65 commit 83b4652

File tree

6 files changed

+27
-41
lines changed

6 files changed

+27
-41
lines changed

Yubico.YubiKey/src/Yubico/YubiKey/Pipelines/ScpApduTransform.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ private void InitializeScp11(Scp11KeyParameters keyParameters)
129129
}
130130

131131
[DoesNotReturn]
132-
private T ThrowIfUninitialized<T>() => throw new InvalidOperationException($"{nameof(Scp.ScpState)} has not been initialized. The Setup method must be called.");
132+
private static T ThrowIfUninitialized<T>() => throw new InvalidOperationException($"{nameof(Scp.ScpState)} has not been initialized. The Setup method must be called.");
133133

134134
private static bool ShouldNotEncode(Type commandType)
135135
{

Yubico.YubiKey/tests/unit/Yubico/YubiKey/Fido2/Fido2SessionTests.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2022 Yubico AB
1+
// Copyright 2022 Yubico AB
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License").
44
// You may not use this file except in compliance with the License.
@@ -42,7 +42,7 @@ void Constructor_ValidYubiKeyDevice_Succeeds()
4242
var mockConnection = Substitute.For<IYubiKeyConnection>();
4343
var mockResponse = new GetInfoResponse(new ResponseApdu(Fido2InfoTests.GetSampleEncoded(), SWConstants.Success));
4444

45-
_ = mockConnection.SendCommand(Arg.Any<IYubiKeyCommand<IYubiKeyResponse>>())
45+
mockConnection.SendCommand(Arg.Any<GetInfoCommand>())
4646
.Returns(mockResponse);
4747

4848
_ = mockYubiKey.Connect(YubiKeyApplication.Fido2)
@@ -60,13 +60,13 @@ void Constructor_GivenValidYubiKeyDevice_ConnectsToFido2Application()
6060
var mockConnection = Substitute.For<IYubiKeyConnection>();
6161
var mockResponse = new GetInfoResponse(new ResponseApdu(Fido2InfoTests.GetSampleEncoded(), SWConstants.Success));
6262

63-
_ = mockConnection.SendCommand(Arg.Any<IYubiKeyCommand<IYubiKeyResponse>>())
63+
_ = mockConnection.SendCommand(Arg.Any<GetInfoCommand>())
6464
.Returns(mockResponse);
6565

6666
_ = mockYubiKey.Connect(YubiKeyApplication.Fido2)
6767
.Returns(mockConnection);
6868

69-
var session = new Fido2Session(mockYubiKey);
69+
_ = new Fido2Session(mockYubiKey);
7070

7171
mockYubiKey.Received().Connect(YubiKeyApplication.Fido2);
7272
}
@@ -78,13 +78,13 @@ void GetAuthenticatorInfo_SendsGetInfoCommand()
7878
var mockConnection = Substitute.For<IYubiKeyConnection>();
7979
var mockResponse = new GetInfoResponse(new ResponseApdu(Fido2InfoTests.GetSampleEncoded(), SWConstants.Success));
8080

81-
_ = mockConnection.SendCommand(Arg.Any<IYubiKeyCommand<IYubiKeyResponse>>())
81+
_ = mockConnection.SendCommand(Arg.Any<GetInfoCommand>())
8282
.Returns(mockResponse);
8383

8484
_ = mockYubiKey.Connect(YubiKeyApplication.Fido2)
8585
.Returns(mockConnection);
8686

87-
var session = new Fido2Session(mockYubiKey);
87+
_ = new Fido2Session(mockYubiKey);
8888

8989
mockConnection.Received().SendCommand(Arg.Any<GetInfoCommand>());
9090
}

Yubico.YubiKey/tests/unit/Yubico/YubiKey/Pipelines/OathResponseChainingTransformTests.cs

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,7 @@ public void Invoke_Called_CallsNextTransformInvokeMethod()
7878
_ = transform.Invoke(new CommandApdu(), typeof(object), typeof(object));
7979

8080
// Assert
81-
mockTransform.Verify(x =>
82-
x.Invoke(Arg.Any<CommandApdu>(), Arg.Any<Type>(), Arg.Any<Type>()), Times.Once());
81+
mockTransform.Received(1).Invoke(Arg.Any<CommandApdu>(), Arg.Any<Type>(), Arg.Any<Type>());
8382
}
8483

8584
[Fact]
@@ -124,16 +123,14 @@ public void Invoke_BytesAvailableThenSuccess_CallsInvokeTwice()
124123
var response1 = new ResponseApdu(new byte[] { SW1Constants.BytesAvailable, 0x00 });
125124
var response2 = new ResponseApdu(new byte[] { SW1Constants.Success, 0x00 });
126125
_ = mockTransform.Invoke(Arg.Any<CommandApdu>(), Arg.Any<Type>(), Arg.Any<Type>())
127-
.Returns(response1)
128-
.Returns(response2);
126+
.Returns(response1, response2);
129127
var transform = new OathResponseChainingTransform(mockTransform);
130128

131129
// Act
132-
ResponseApdu actualResponse = transform.Invoke(new CommandApdu(), typeof(object), typeof(object));
130+
_ = transform.Invoke(new CommandApdu(), typeof(object), typeof(object));
133131

134132
// Assert
135-
mockTransform.Verify(x =>
136-
x.Invoke(Arg.Any<CommandApdu>(), Arg.Any<Type>(), Arg.Any<Type>()), Times.Exactly(2));
133+
mockTransform.Received(2).Invoke(Arg.Any<CommandApdu>(), Arg.Any<Type>(), Arg.Any<Type>());
137134
}
138135

139136
[Fact]
@@ -146,16 +143,14 @@ public void Invoke_BytesAvailableThenSuccess_CallsInvokeWithCorrectIns()
146143
var response1 = new ResponseApdu(new byte[] { SW1Constants.BytesAvailable, 0x00 });
147144
var response2 = new ResponseApdu(new byte[] { SW1Constants.Success, 0x00 });
148145
_ = mockTransform.Invoke(Arg.Any<CommandApdu>(), Arg.Any<Type>(), Arg.Any<Type>())
149-
.Returns(response1)
150-
.Returns(response2);
146+
.Returns(response1, response2);
151147
var transform = new OathResponseChainingTransform(mockTransform);
152148

153149
// Act
154-
ResponseApdu actualResponse = transform.Invoke(new CommandApdu(), typeof(object), typeof(object));
150+
_ = transform.Invoke(new CommandApdu(), typeof(object), typeof(object));
155151

156152
// Assert
157-
mockTransform.Verify(x =>
158-
x.Invoke(Arg.Is<CommandApdu>(c => c.Ins == expectedIns), Arg.Any<Type>(), Arg.Any<Type>()), Times.Once);
153+
mockTransform.Received(1).Invoke(Arg.Is<CommandApdu>(c => c.Ins == expectedIns), Arg.Any<Type>(), Arg.Any<Type>());
159154
}
160155

161156
[Fact]
@@ -167,8 +162,7 @@ public void Invoke_BytesAvailble_ConcatsAllBuffers()
167162
var response2 = new ResponseApdu(new byte[] { 5, 6, 7, 8, SW1Constants.Success, 0x00 });
168163
byte[] expectedData = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
169164
_ = mockTransform.Invoke(Arg.Any<CommandApdu>(), Arg.Any<Type>(), Arg.Any<Type>())
170-
.Returns(response1)
171-
.Returns(response2);
165+
.Returns(response1, response2);
172166
var transform = new OathResponseChainingTransform(mockTransform);
173167

174168
// Act
@@ -186,8 +180,7 @@ public void Invoke_BytesAvailable_ReturnsLastStatusWord()
186180
var response1 = new ResponseApdu(new byte[] { SW1Constants.BytesAvailable, 0x00 });
187181
var response2 = new ResponseApdu(new byte[] { SW1Constants.NoPreciseDiagnosis, 0x00 });
188182
_ = mockTransform.Invoke(Arg.Any<CommandApdu>(), Arg.Any<Type>(), Arg.Any<Type>())
189-
.Returns(response1)
190-
.Returns(response2);
183+
.Returns(response1, response2);
191184
var transform = new OathResponseChainingTransform(mockTransform);
192185

193186
// Act

Yubico.YubiKey/tests/unit/Yubico/YubiKey/Pipelines/ResponseChainingTransformTests.cs

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,7 @@ public void Invoke_Called_CallsNextTransformInvokeMethod()
7878
_ = transform.Invoke(new CommandApdu(), typeof(object), typeof(object));
7979

8080
// Assert
81-
mockTransform.Verify(x =>
82-
x.Invoke(Arg.Any<CommandApdu>(), Arg.Any<Type>(), Arg.Any<Type>()), Times.Once());
81+
mockTransform.Received(1).Invoke(Arg.Any<CommandApdu>(), Arg.Any<Type>(), Arg.Any<Type>());
8382
}
8483

8584
[Fact]
@@ -124,16 +123,14 @@ public void Invoke_BytesAvailableThenSuccess_CallsInvokeTwice()
124123
var response1 = new ResponseApdu(new byte[] { SW1Constants.BytesAvailable, 0x00 });
125124
var response2 = new ResponseApdu(new byte[] { SW1Constants.Success, 0x00 });
126125
_ = mockTransform.Invoke(Arg.Any<CommandApdu>(), Arg.Any<Type>(), Arg.Any<Type>())
127-
.Returns(response1)
128-
.Returns(response2);
126+
.Returns(response1, response2);
129127
var transform = new ResponseChainingTransform(mockTransform);
130128

131129
// Act
132-
ResponseApdu actualResponse = transform.Invoke(new CommandApdu(), typeof(object), typeof(object));
130+
_ = transform.Invoke(new CommandApdu(), typeof(object), typeof(object));
133131

134132
// Assert
135-
mockTransform.Verify(x =>
136-
x.Invoke(Arg.Any<CommandApdu>(), Arg.Any<Type>(), Arg.Any<Type>()), Times.Exactly(2));
133+
mockTransform.Received(2).Invoke(Arg.Any<CommandApdu>(), Arg.Any<Type>(), Arg.Any<Type>());
137134
}
138135

139136
[Fact]
@@ -146,16 +143,14 @@ public void Invoke_BytesAvailableThenSuccess_CallsInvokeWithCorrectIns()
146143
var response1 = new ResponseApdu(new byte[] { SW1Constants.BytesAvailable, 0x00 });
147144
var response2 = new ResponseApdu(new byte[] { SW1Constants.Success, 0x00 });
148145
_ = mockTransform.Invoke(Arg.Any<CommandApdu>(), Arg.Any<Type>(), Arg.Any<Type>())
149-
.Returns(response1)
150-
.Returns(response2);
146+
.Returns(response1, response2);
151147
var transform = new ResponseChainingTransform(mockTransform);
152148

153149
// Act
154-
ResponseApdu actualResponse = transform.Invoke(new CommandApdu(), typeof(object), typeof(object));
150+
_ = transform.Invoke(new CommandApdu(), typeof(object), typeof(object));
155151

156152
// Assert
157-
mockTransform.Verify(x =>
158-
x.Invoke(Arg.Is<CommandApdu>(c => c.Ins == expectedIns), Arg.Any<Type>(), Arg.Any<Type>()), Times.Once);
153+
mockTransform.Received(1).Invoke(Arg.Is<CommandApdu>(c => c.Ins == expectedIns), Arg.Any<Type>(), Arg.Any<Type>());
159154
}
160155

161156
[Fact]
@@ -167,8 +162,7 @@ public void Invoke_BytesAvailble_ConcatsAllBuffers()
167162
var response2 = new ResponseApdu(new byte[] { 5, 6, 7, 8, SW1Constants.Success, 0x00 });
168163
byte[] expectedData = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
169164
_ = mockTransform.Invoke(Arg.Any<CommandApdu>(), Arg.Any<Type>(), Arg.Any<Type>())
170-
.Returns(response1)
171-
.Returns(response2);
165+
.Returns(response1, response2);
172166
var transform = new ResponseChainingTransform(mockTransform);
173167

174168
// Act
@@ -186,8 +180,7 @@ public void Invoke_BytesAvailable_ReturnsLastStatusWord()
186180
var response1 = new ResponseApdu(new byte[] { SW1Constants.BytesAvailable, 0x00 });
187181
var response2 = new ResponseApdu(new byte[] { SW1Constants.NoPreciseDiagnosis, 0x00 });
188182
_ = mockTransform.Invoke(Arg.Any<CommandApdu>(), Arg.Any<Type>(), Arg.Any<Type>())
189-
.Returns(response1)
190-
.Returns(response2);
183+
.Returns(response1, response2);
191184
var transform = new ResponseChainingTransform(mockTransform);
192185

193186
// Act

Yubico.YubiKey/tests/unit/Yubico/YubiKey/Pipelines/ScpApduTransformTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public void Invoke_ExemptedCommands_BypassEncoding()
106106
testCase.CommandType,
107107
testCase.ResponseType);
108108

109-
_previous.Reset();
109+
_previous.ClearReceivedCalls();
110110
}
111111
}
112112

Yubico.YubiKey/tests/utilities/Yubico/YubiKey/TestUtilities/TestDeviceSelection.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ public static IYubiKeyDevice SelectByMinimumVersion(
151151
ThrowDeviceNotFoundException("No matching YubiKey found", devices);
152152
}
153153

154-
return device;
154+
return device!;
155155
}
156156

157157
[DoesNotReturn]

0 commit comments

Comments
 (0)