protected virtual void Arrange() { Random = new Random(); _serverEndPoint = new IPEndPoint(IPAddress.Loopback, 8122); ConnectionInfo = new ConnectionInfo( _serverEndPoint.Address.ToString(), _serverEndPoint.Port, "user", new PasswordAuthenticationMethod("user", "password")); ConnectionInfo.Timeout = TimeSpan.FromSeconds(20); _keyExchangeAlgorithm = Random.Next().ToString(CultureInfo.InvariantCulture); SessionId = new byte[10]; Random.NextBytes(SessionId); DisconnectedRegister = new List<EventArgs>(); DisconnectReceivedRegister = new List<MessageEventArgs<DisconnectMessage>>(); ErrorOccurredRegister = new List<ExceptionEventArgs>(); ServerBytesReceivedRegister = new List<byte[]>(); _serviceFactoryMock = new Mock<IServiceFactory>(MockBehavior.Strict); _keyExchangeMock = new Mock<IKeyExchange>(MockBehavior.Strict); _clientAuthenticationMock = new Mock<IClientAuthentication>(MockBehavior.Strict); Session = new Session(ConnectionInfo, _serviceFactoryMock.Object); Session.Disconnected += (sender, args) => DisconnectedRegister.Add(args); Session.DisconnectReceived += (sender, args) => DisconnectReceivedRegister.Add(args); Session.ErrorOccured += (sender, args) => ErrorOccurredRegister.Add(args); Session.KeyExchangeInitReceived += (sender, args) => { var newKeysMessage = new NewKeysMessage(); var newKeys = newKeysMessage.GetPacket(8, null); ServerSocket.Send(newKeys, 4, newKeys.Length - 4, SocketFlags.None); }; _serviceFactoryMock.Setup(p => p.CreateKeyExchange(ConnectionInfo.KeyExchangeAlgorithms, new[] { _keyExchangeAlgorithm })).Returns(_keyExchangeMock.Object); _keyExchangeMock.Setup(p => p.Name).Returns(_keyExchangeAlgorithm); _keyExchangeMock.Setup(p => p.Start(Session, It.IsAny<KeyExchangeInitMessage>())); _keyExchangeMock.Setup(p => p.ExchangeHash).Returns(SessionId); _keyExchangeMock.Setup(p => p.CreateServerCipher()).Returns((Cipher) null); _keyExchangeMock.Setup(p => p.CreateClientCipher()).Returns((Cipher) null); _keyExchangeMock.Setup(p => p.CreateServerHash()).Returns((HashAlgorithm) null); _keyExchangeMock.Setup(p => p.CreateClientHash()).Returns((HashAlgorithm) null); _keyExchangeMock.Setup(p => p.CreateCompressor()).Returns((Compressor) null); _keyExchangeMock.Setup(p => p.CreateDecompressor()).Returns((Compressor) null); _keyExchangeMock.Setup(p => p.Dispose()); _serviceFactoryMock.Setup(p => p.CreateClientAuthentication()).Returns(_clientAuthenticationMock.Object); _clientAuthenticationMock.Setup(p => p.Authenticate(ConnectionInfo, Session)); ServerListener = new AsyncSocketListener(_serverEndPoint); ServerListener.Connected += socket => { ServerSocket = socket; socket.Send(Encoding.ASCII.GetBytes("\r\n")); socket.Send(Encoding.ASCII.GetBytes("WELCOME banner\r\n")); socket.Send(Encoding.ASCII.GetBytes("SSH-2.0-SshStub\r\n")); }; var counter = 0; ServerListener.BytesReceived += (received, socket) => { ServerBytesReceivedRegister.Add(received); switch (counter++) { case 0: var keyExchangeInitMessage = new KeyExchangeInitMessage { CompressionAlgorithmsClientToServer = new string[0], CompressionAlgorithmsServerToClient = new string[0], EncryptionAlgorithmsClientToServer = new string[0], EncryptionAlgorithmsServerToClient = new string[0], KeyExchangeAlgorithms = new[] {_keyExchangeAlgorithm}, LanguagesClientToServer = new string[0], LanguagesServerToClient = new string[0], MacAlgorithmsClientToServer = new string[0], MacAlgorithmsServerToClient = new string[0], ServerHostKeyAlgorithms = new string[0] }; var keyExchangeInit = keyExchangeInitMessage.GetPacket(8, null); ServerSocket.Send(keyExchangeInit, 4, keyExchangeInit.Length - 4, SocketFlags.None); break; case 1: var serviceAcceptMessage = ServiceAcceptMessageBuilder.Create(ServiceName.UserAuthentication) .Build(); ServerSocket.Send(serviceAcceptMessage, 0, serviceAcceptMessage.Length, SocketFlags.None); break; } }; ServerListener.Start(); Session.Connect(); }
protected virtual void SetupData() { Random = new Random(); _serverEndPoint = new IPEndPoint(IPAddress.Loopback, 8122); ConnectionInfo = new ConnectionInfo( _serverEndPoint.Address.ToString(), _serverEndPoint.Port, "user", new PasswordAuthenticationMethod("user", "password")) { Timeout = TimeSpan.FromSeconds(20) }; _keyExchangeAlgorithm = Random.Next().ToString(CultureInfo.InvariantCulture); SessionId = new byte[10]; Random.NextBytes(SessionId); DisconnectedRegister = new List<EventArgs>(); DisconnectReceivedRegister = new List<MessageEventArgs<DisconnectMessage>>(); ErrorOccurredRegister = new List<ExceptionEventArgs>(); ServerBytesReceivedRegister = new List<byte[]>(); _disconnectMessage = new DisconnectMessage(DisconnectReason.ServiceNotAvailable, "Not today!"); Session = new Session(ConnectionInfo, _serviceFactoryMock.Object); Session.Disconnected += (sender, args) => DisconnectedRegister.Add(args); Session.DisconnectReceived += (sender, args) => DisconnectReceivedRegister.Add(args); Session.ErrorOccured += (sender, args) => ErrorOccurredRegister.Add(args); Session.KeyExchangeInitReceived += (sender, args) => { var newKeysMessage = new NewKeysMessage(); var newKeys = newKeysMessage.GetPacket(8, null); ServerSocket.Send(newKeys, 4, newKeys.Length - 4, SocketFlags.None); }; ServerListener = new AsyncSocketListener(_serverEndPoint); ServerListener.Connected += socket => { ServerSocket = socket; socket.Send(Encoding.ASCII.GetBytes("\r\n")); socket.Send(Encoding.ASCII.GetBytes("WELCOME banner\r\n")); socket.Send(Encoding.ASCII.GetBytes("SSH-2.0-SshStub\r\n")); }; var counter = 0; ServerListener.BytesReceived += (received, socket) => { ServerBytesReceivedRegister.Add(received); switch (counter++) { case 0: var keyExchangeInitMessage = new KeyExchangeInitMessage { CompressionAlgorithmsClientToServer = new string[0], CompressionAlgorithmsServerToClient = new string[0], EncryptionAlgorithmsClientToServer = new string[0], EncryptionAlgorithmsServerToClient = new string[0], KeyExchangeAlgorithms = new[] { _keyExchangeAlgorithm }, LanguagesClientToServer = new string[0], LanguagesServerToClient = new string[0], MacAlgorithmsClientToServer = new string[0], MacAlgorithmsServerToClient = new string[0], ServerHostKeyAlgorithms = new string[0] }; var keyExchangeInit = keyExchangeInitMessage.GetPacket(8, null); ServerSocket.Send(keyExchangeInit, 4, keyExchangeInit.Length - 4, SocketFlags.None); break; case 1: var serviceAcceptMessage =ServiceAcceptMessageBuilder.Create(ServiceName.UserAuthentication).Build(); ServerSocket.Send(serviceAcceptMessage, 0, serviceAcceptMessage.Length, SocketFlags.None); break; } }; }