forked from sshnet/SSH.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve tests for partial success limit.
- Loading branch information
Showing
12 changed files
with
609 additions
and
112 deletions.
There are no files selected for viewing
190 changes: 190 additions & 0 deletions
190
...ticationTest_Failure_MultiList_AllAllowedAuthenticationsHaveReachedPartialSuccessLimit.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,190 @@ | ||
using System.Collections.Generic; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using Moq; | ||
using Renci.SshNet.Common; | ||
|
||
namespace Renci.SshNet.Tests.Classes | ||
{ | ||
/// <summary> | ||
/// * ConnectionInfo provides the following authentication methods (in order): | ||
/// o publickey | ||
/// o password | ||
/// * Partial success limit is 2 | ||
/// | ||
/// none | ||
/// (1=FAIL) | ||
/// | | ||
/// +------------------------+------------------------+ | ||
/// | | | | ||
/// password ◄--\ publickey keyboard-interactive | ||
/// (7=SKIP) | (2=PS) | ||
/// | | | ||
/// | password | ||
/// | (3=PS) | ||
/// | | | ||
/// | password | ||
/// | (4=PS) | ||
/// | | | ||
/// | publickey | ||
/// | (5=PS) | ||
/// | | | ||
/// \---- publickey | ||
/// (6=SKIP) | ||
/// </summary> | ||
[TestClass] | ||
public class ClientAuthenticationTest_Failure_MultiList_AllAllowedAuthenticationsHaveReachedPartialSuccessLimit : ClientAuthenticationTestBase | ||
{ | ||
private int _partialSuccessLimit; | ||
private ClientAuthentication _clientAuthentication; | ||
private SshAuthenticationException _actualException; | ||
|
||
protected override void SetupData() | ||
{ | ||
_partialSuccessLimit = 2; | ||
} | ||
|
||
protected override void SetupMocks() | ||
{ | ||
var seq = new MockSequence(); | ||
|
||
SessionMock.InSequence(seq).Setup(p => p.RegisterMessage("SSH_MSG_USERAUTH_FAILURE")); | ||
SessionMock.InSequence(seq).Setup(p => p.RegisterMessage("SSH_MSG_USERAUTH_SUCCESS")); | ||
SessionMock.InSequence(seq).Setup(p => p.RegisterMessage("SSH_MSG_USERAUTH_BANNER")); | ||
|
||
ConnectionInfoMock.InSequence(seq).Setup(p => p.CreateNoneAuthenticationMethod()) | ||
.Returns(NoneAuthenticationMethodMock.Object); | ||
|
||
/* 1 */ | ||
|
||
NoneAuthenticationMethodMock.InSequence(seq).Setup(p => p.Authenticate(SessionMock.Object)) | ||
.Returns(AuthenticationResult.Failure); | ||
ConnectionInfoMock.InSequence(seq) | ||
.Setup(p => p.AuthenticationMethods) | ||
.Returns(new List<IAuthenticationMethod> | ||
{ | ||
PublicKeyAuthenticationMethodMock.Object, | ||
PasswordAuthenticationMethodMock.Object | ||
}); | ||
NoneAuthenticationMethodMock.InSequence(seq) | ||
.Setup(p => p.AllowedAuthentications) | ||
.Returns(new[] {"password", "publickey", "keyboard-interactive"}); | ||
|
||
/* Enumerate supported authentication methods */ | ||
|
||
PublicKeyAuthenticationMethodMock.InSequence(seq).Setup(p => p.Name).Returns("publickey"); | ||
PasswordAuthenticationMethodMock.InSequence(seq).Setup(p => p.Name).Returns("password"); | ||
|
||
/* 2 */ | ||
|
||
PublicKeyAuthenticationMethodMock.InSequence(seq) | ||
.Setup(p => p.Authenticate(SessionMock.Object)) | ||
.Returns(AuthenticationResult.PartialSuccess); | ||
PublicKeyAuthenticationMethodMock.InSequence(seq) | ||
.Setup(p => p.AllowedAuthentications) | ||
.Returns(new[] {"password"}); | ||
|
||
/* Enumerate supported authentication methods */ | ||
|
||
PublicKeyAuthenticationMethodMock.InSequence(seq).Setup(p => p.Name).Returns("publickey"); | ||
PasswordAuthenticationMethodMock.InSequence(seq).Setup(p => p.Name).Returns("password"); | ||
|
||
/* 3 */ | ||
|
||
PasswordAuthenticationMethodMock.InSequence(seq) | ||
.Setup(p => p.Authenticate(SessionMock.Object)) | ||
.Returns(AuthenticationResult.PartialSuccess); | ||
PasswordAuthenticationMethodMock.InSequence(seq) | ||
.Setup(p => p.AllowedAuthentications) | ||
.Returns(new[] {"password"}); | ||
|
||
/* Enumerate supported authentication methods */ | ||
|
||
PublicKeyAuthenticationMethodMock.InSequence(seq).Setup(p => p.Name).Returns("publickey"); | ||
PasswordAuthenticationMethodMock.InSequence(seq).Setup(p => p.Name).Returns("password"); | ||
|
||
/* 4 */ | ||
|
||
PasswordAuthenticationMethodMock.InSequence(seq) | ||
.Setup(p => p.Authenticate(SessionMock.Object)) | ||
.Returns(AuthenticationResult.PartialSuccess); | ||
PasswordAuthenticationMethodMock.InSequence(seq) | ||
.Setup(p => p.AllowedAuthentications) | ||
.Returns(new[] {"publickey"}); | ||
|
||
/* Enumerate supported authentication methods */ | ||
|
||
PublicKeyAuthenticationMethodMock.InSequence(seq).Setup(p => p.Name).Returns("publickey"); | ||
PasswordAuthenticationMethodMock.InSequence(seq).Setup(p => p.Name).Returns("password"); | ||
|
||
/* 5 */ | ||
|
||
PublicKeyAuthenticationMethodMock.InSequence(seq) | ||
.Setup(p => p.Authenticate(SessionMock.Object)) | ||
.Returns(AuthenticationResult.PartialSuccess); | ||
PublicKeyAuthenticationMethodMock.InSequence(seq) | ||
.Setup(p => p.AllowedAuthentications) | ||
.Returns(new[] {"publickey"}); | ||
|
||
/* Enumerate supported authentication methods */ | ||
|
||
PublicKeyAuthenticationMethodMock.InSequence(seq).Setup(p => p.Name).Returns("publickey"); | ||
PasswordAuthenticationMethodMock.InSequence(seq).Setup(p => p.Name).Returns("password"); | ||
|
||
/* 6: Record partial success limit reached exception, and skip password authentication method */ | ||
|
||
PublicKeyAuthenticationMethodMock.InSequence(seq) | ||
.Setup(p => p.Name) | ||
.Returns("publickey-partial1"); | ||
|
||
/* 7: Record partial success limit reached exception, and skip password authentication method */ | ||
|
||
PasswordAuthenticationMethodMock.InSequence(seq) | ||
.Setup(p => p.Name) | ||
.Returns("password-partial1"); | ||
|
||
SessionMock.InSequence(seq).Setup(p => p.UnRegisterMessage("SSH_MSG_USERAUTH_FAILURE")); | ||
SessionMock.InSequence(seq).Setup(p => p.UnRegisterMessage("SSH_MSG_USERAUTH_SUCCESS")); | ||
SessionMock.InSequence(seq).Setup(p => p.UnRegisterMessage("SSH_MSG_USERAUTH_BANNER")); | ||
} | ||
|
||
protected override void Arrange() | ||
{ | ||
base.Arrange(); | ||
|
||
_clientAuthentication = new ClientAuthentication(_partialSuccessLimit); | ||
} | ||
|
||
protected override void Act() | ||
{ | ||
try | ||
{ | ||
_clientAuthentication.Authenticate(ConnectionInfoMock.Object, SessionMock.Object); | ||
Assert.Fail(); | ||
} | ||
catch (SshAuthenticationException ex) | ||
{ | ||
_actualException = ex; | ||
} | ||
} | ||
|
||
[TestMethod] | ||
public void AuthenticateOnPasswordAuthenticationMethodShouldHaveBeenInvokedTwice() | ||
{ | ||
PasswordAuthenticationMethodMock.Verify(p => p.Authenticate(SessionMock.Object), Times.Exactly(2)); | ||
} | ||
|
||
[TestMethod] | ||
public void AuthenticateOnPublicKeyAuthenticationMethodShouldHaveBeenInvokedTwice() | ||
{ | ||
PublicKeyAuthenticationMethodMock.Verify(p => p.Authenticate(SessionMock.Object), Times.Exactly(2)); | ||
} | ||
|
||
[TestMethod] | ||
public void AuthenticateShouldThrowSshAuthenticationException() | ||
{ | ||
Assert.IsNotNull(_actualException); | ||
Assert.IsNull(_actualException.InnerException); | ||
Assert.AreEqual("Reached authentication attempt limit for method (password-partial1).", _actualException.Message); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 0 additions & 14 deletions
14
...ticationTest_Success_MultiList_AllAllowedAuthenticationsHaveReachedPartialSuccessLimit.cs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.