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); 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" }); PublicKeyAuthenticationMethodMock.InSequence(seq).Setup(p => p.Name).Returns("publickey"); PasswordAuthenticationMethodMock.InSequence(seq).Setup(p => p.Name).Returns("password"); PasswordAuthenticationMethodMock.InSequence(seq).Setup(p => p.Authenticate(SessionMock.Object)) .Returns(AuthenticationResult.Failure); // obtain name for inclusion in SshAuthenticationException PasswordAuthenticationMethodMock.InSequence(seq).Setup(p => p.Name).Returns("password"); 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 SetupMocks() { var sequence = new MockSequence(); SessionMock.InSequence(sequence).Setup(p => p.IsConnected).Returns(true); SessionMock.InSequence(sequence).Setup(p => p.TrySendMessage(It.Is <ChannelEofMessage>(c => c.LocalChannelNumber == _remoteChannelNumber))).Returns(true); SessionMock.InSequence(sequence).Setup(p => p.IsConnected).Returns(true); SessionMock.InSequence(sequence).Setup(p => p.TrySendMessage(It.Is <ChannelCloseMessage>(c => c.LocalChannelNumber == _remoteChannelNumber))).Returns(true); SessionMock.InSequence(sequence).Setup(p => p.ConnectionInfo).Returns(ConnectionInfoMock.Object); ConnectionInfoMock.InSequence(sequence).Setup(p => p.ChannelCloseTimeout).Returns(_channelCloseTimeout); SessionMock.InSequence(sequence) .Setup(p => p.TryWait(It.IsAny <EventWaitHandle>(), _channelCloseTimeout)) .Callback <WaitHandle, TimeSpan>((waitHandle, channelCloseTimeout) => { new Thread(() => { Thread.Sleep(100); // raise ChannelCloseReceived event to set waithandle for receiving // SSH_MSG_CHANNEL_CLOSE message from server which is waited on after // sending the SSH_MSG_CHANNEL_CLOSE message to the server SessionMock.Raise(s => s.ChannelCloseReceived += null, new MessageEventArgs <ChannelCloseMessage>(new ChannelCloseMessage(_localChannelNumber))); }).Start(); _closeTimer.Start(); try { waitHandle.WaitOne(); } finally { _closeTimer.Stop(); } }) .Returns(WaitResult.Success); }
protected override void SetupMocks() { _sequence = new MockSequence(); SessionMock.InSequence(_sequence).Setup(p => p.ConnectionInfo).Returns(ConnectionInfoMock.Object); ConnectionInfoMock.InSequence(_sequence).Setup(p => p.RetryAttempts).Returns(1); SessionMock.Setup(p => p.SessionSemaphore).Returns(_sessionSemaphore); SessionMock.InSequence(_sequence) .Setup( p => p.SendMessage( It.Is <ChannelOpenMessage>( m => m.LocalChannelNumber == _localChannelNumber && m.InitialWindowSize == _localWindowSize && m.MaximumPacketSize == _localPacketSize && m.Info is SessionChannelOpenInfo))); SessionMock.InSequence(_sequence) .Setup(p => p.WaitOnHandle(It.IsNotNull <WaitHandle>())) .Callback <WaitHandle>( w => { SessionMock.Raise( s => s.ChannelOpenConfirmationReceived += null, new MessageEventArgs <ChannelOpenConfirmationMessage>( new ChannelOpenConfirmationMessage( _localChannelNumber, _remoteWindowSize, _remotePacketSize, _remoteChannelNumber))); w.WaitOne(); }); SessionMock.Setup(p => p.IsConnected).Returns(false); }
protected override void SetupMocks() { var sequence = new MockSequence(); SessionMock.InSequence(sequence).Setup(p => p.IsConnected).Returns(true); SessionMock.InSequence(sequence).Setup(p => p.TrySendMessage(It.Is <ChannelCloseMessage>(c => c.LocalChannelNumber == _remoteChannelNumber))).Returns(true); SessionMock.InSequence(sequence).Setup(p => p.ConnectionInfo).Returns(ConnectionInfoMock.Object); ConnectionInfoMock.InSequence(sequence).Setup(p => p.ChannelCloseTimeout).Returns(_channelCloseTimeout); SessionMock.InSequence(sequence).Setup(p => p.TryWait(It.IsAny <EventWaitHandle>(), _channelCloseTimeout)).Returns(WaitResult.TimedOut); }
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> { PasswordAuthenticationMethodMock.Object, PublicKeyAuthenticationMethodMock.Object, KeyboardInteractiveAuthenticationMethodMock.Object, }); NoneAuthenticationMethodMock.InSequence(seq) .Setup(p => p.AllowedAuthentications) .Returns(new[] { "publickey", "password" }); /* Enumerate supported authentication methods */ PasswordAuthenticationMethodMock.InSequence(seq).Setup(p => p.Name).Returns("password"); PublicKeyAuthenticationMethodMock.InSequence(seq).Setup(p => p.Name).Returns("publickey"); KeyboardInteractiveAuthenticationMethodMock.InSequence(seq).Setup(p => p.Name).Returns("keyboard-interactive"); /* 2 */ PasswordAuthenticationMethodMock.InSequence(seq).Setup(p => p.Authenticate(SessionMock.Object)) .Returns(AuthenticationResult.PartialSuccess); PasswordAuthenticationMethodMock.InSequence(seq).Setup(p => p.AllowedAuthentications) .Returns(new[] { "keyboard-interactive", "publickey" }); /* Enumerate supported authentication methods */ PasswordAuthenticationMethodMock.InSequence(seq).Setup(p => p.Name).Returns("password"); PublicKeyAuthenticationMethodMock.InSequence(seq).Setup(p => p.Name).Returns("publickey"); KeyboardInteractiveAuthenticationMethodMock.InSequence(seq).Setup(p => p.Name).Returns("keyboard-interactive"); /* 3 */ PublicKeyAuthenticationMethodMock.InSequence(seq) .Setup(p => p.Authenticate(SessionMock.Object)) .Returns(AuthenticationResult.Success); 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 SetupMocks() { var sequence = new MockSequence(); SessionMock.InSequence(sequence).Setup(p => p.ConnectionInfo).Returns(ConnectionInfoMock.Object); ConnectionInfoMock.InSequence(sequence).Setup(p => p.RetryAttempts).Returns(1); SessionMock.Setup(p => p.SessionSemaphore).Returns(_sessionSemaphore); SessionMock.InSequence(sequence) .Setup( p => p.SendMessage( It.Is <ChannelOpenMessage>( m => m.LocalChannelNumber == _localChannelNumber && m.InitialWindowSize == _localWindowSize && m.MaximumPacketSize == _localPacketSize && m.Info is SessionChannelOpenInfo))); SessionMock.InSequence(sequence) .Setup(p => p.WaitOnHandle(It.IsNotNull <WaitHandle>())) .Callback <WaitHandle>( w => { SessionMock.Raise( s => s.ChannelOpenConfirmationReceived += null, new MessageEventArgs <ChannelOpenConfirmationMessage>( new ChannelOpenConfirmationMessage( _localChannelNumber, _remoteWindowSize, _remotePacketSize, _remoteChannelNumber))); w.WaitOne(); }); SessionMock.InSequence(sequence).Setup(p => p.IsConnected).Returns(true); SessionMock.InSequence(sequence) .Setup( p => p.TrySendMessage(It.Is <ChannelEofMessage>(m => m.LocalChannelNumber == _remoteChannelNumber))) .Returns(true); SessionMock.InSequence(sequence).Setup(p => p.IsConnected).Returns(true); SessionMock.InSequence(sequence) .Setup( p => p.TrySendMessage(It.Is <ChannelCloseMessage>(m => m.LocalChannelNumber == _remoteChannelNumber))) .Returns(true); SessionMock.InSequence(sequence).Setup(p => p.ConnectionInfo).Returns(ConnectionInfoMock.Object); ConnectionInfoMock.InSequence(sequence).Setup(p => p.ChannelCloseTimeout).Returns(_channelCloseTimeout); SessionMock.InSequence(sequence) .Setup(p => p.TryWait(It.IsNotNull <WaitHandle>(), _channelCloseTimeout)) .Callback <WaitHandle, TimeSpan>( (waitHandle, channelCloseTimeout) => { SessionMock.Raise( s => s.ChannelCloseReceived += null, new MessageEventArgs <ChannelCloseMessage>(new ChannelCloseMessage(_localChannelNumber))); waitHandle.WaitOne(); }) .Returns(WaitResult.Success); }
protected override void SetupMocks() { var sequence = new MockSequence(); SessionMock.InSequence(sequence).Setup(p => p.IsConnected).Returns(true); SessionMock.InSequence(sequence).Setup(p => p.TrySendMessage(It.Is <ChannelCloseMessage>(c => c.LocalChannelNumber == _remoteChannelNumber))).Returns(true); SessionMock.InSequence(sequence).Setup(p => p.ConnectionInfo).Returns(ConnectionInfoMock.Object); ConnectionInfoMock.InSequence(sequence).Setup(p => p.ChannelCloseTimeout).Returns(_channelCloseTimeout); SessionMock.InSequence(sequence) .Setup(p => p.TryWait(It.IsAny <EventWaitHandle>(), _channelCloseTimeout)) .Callback <WaitHandle, TimeSpan>((waitHandle, channelCloseTimeout) => _channelClosedWaitHandleSignaled = waitHandle.WaitOne(0)) .Returns(WaitResult.Success); }
protected override void SetupMocks() { var sequence = new MockSequence(); SessionMock.InSequence(sequence).Setup(p => p.ConnectionInfo).Returns(ConnectionInfoMock.Object); ConnectionInfoMock.InSequence(sequence).Setup(p => p.RetryAttempts).Returns(2); SessionMock.Setup(p => p.SessionSemaphore).Returns(_sessionSemaphore); SessionMock.InSequence(sequence) .Setup( p => p.SendMessage( It.Is <ChannelOpenMessage>( m => m.LocalChannelNumber == _localChannelNumber && m.InitialWindowSize == _localWindowSize && m.MaximumPacketSize == _localPacketSize && m.Info is SessionChannelOpenInfo))); SessionMock.InSequence(sequence) .Setup(p => p.WaitOnHandle(It.IsNotNull <WaitHandle>())) .Throws(_waitOnConfirmationException); }
protected override void SetupMocks() { var sequence = new MockSequence(); SessionMock.InSequence(sequence).Setup(p => p.IsConnected).Returns(true); SessionMock.InSequence(sequence).Setup(p => p.TrySendMessage(It.Is <ChannelCloseMessage>(c => c.LocalChannelNumber == _remoteChannelNumber))).Returns(true); SessionMock.InSequence(sequence).Setup(p => p.ConnectionInfo).Returns(ConnectionInfoMock.Object); ConnectionInfoMock.InSequence(sequence).Setup(p => p.ChannelCloseTimeout).Returns(_channelCloseTimeout); SessionMock.InSequence(sequence) .Setup(p => p.TryWait(It.IsAny <EventWaitHandle>(), _channelCloseTimeout)) .Callback <WaitHandle, TimeSpan>((waitHandle, channelCloseTimeout) => { _raiseChannelCloseReceivedThread = new Thread(() => { Thread.Sleep(100); // signal that the ChannelCloseMessage was received; we use this to verify whether we've actually // waited on the EventWaitHandle to be set; this needs to be set before we raise the ChannelCloseReceived // to make sure the waithandle is signaled when the Dispose method completes (or else the assert that // checks whether the handle has been signaled, will sometimes fail) _channelClosedReceived.Set(); // raise ChannelCloseReceived event to set waithandle for receiving SSH_MSG_CHANNEL_CLOSE message // from server which is waited on after sending the SSH_MSG_CHANNEL_CLOSE message to the server // // this will cause a new invocation of Close() that will block until the Close() that was invoked // as part of Dispose() has released the lock; as such, this thread cannot be joined until that // lock is released // // we're mocking the wait on the ChannelCloseMessage, but we still want // to get the channel in the state that it would have after actually receiving // the ChannelCloseMessage SessionMock.Raise(s => s.ChannelCloseReceived += null, new MessageEventArgs <ChannelCloseMessage>(new ChannelCloseMessage(_localChannelNumber))); }); _raiseChannelCloseReceivedThread.Start(); waitHandle.WaitOne(); }) .Returns(WaitResult.Success); }
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" }); /* Enumerate supported authentication methods */ PublicKeyAuthenticationMethodMock.InSequence(seq).Setup(p => p.Name).Returns("publickey"); PasswordAuthenticationMethodMock.InSequence(seq).Setup(p => p.Name).Returns("password"); /* 2 */ 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"); /* 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[] { "password" }); /* Enumerate supported authentication methods */ PublicKeyAuthenticationMethodMock.InSequence(seq).Setup(p => p.Name).Returns("publickey"); PasswordAuthenticationMethodMock.InSequence(seq).Setup(p => p.Name).Returns("password"); /* 5: Record partial success limit reached exception, and skip password authentication method */ PasswordAuthenticationMethodMock.InSequence(seq).Setup(p => p.Name).Returns("x_password_x"); 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")); }