SessionTest_Connecting_ServerSendsDebugMessageAfterKexInit_StrictKex.cs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. using System.Globalization;
  2. using System.Net.Sockets;
  3. using System.Text;
  4. using Microsoft.VisualStudio.TestTools.UnitTesting;
  5. using Renci.SshNet.Common;
  6. using Renci.SshNet.Messages.Transport;
  7. namespace Renci.SshNet.Tests.Classes
  8. {
  9. [TestClass]
  10. public class SessionTest_Connecting_ServerSendsDebugMessageAfterKexInit_StrictKex : SessionTest_ConnectingBase
  11. {
  12. protected override bool ServerSupportsStrictKex
  13. {
  14. get
  15. {
  16. return true;
  17. }
  18. }
  19. protected override void ActionAfterKexInit()
  20. {
  21. using var stream = new SshDataStream(0);
  22. stream.WriteByte(1);
  23. stream.Write("This is a debug message", Encoding.UTF8);
  24. stream.Write(CultureInfo.CurrentCulture.Name, Encoding.UTF8);
  25. var debugMessage = new DebugMessage();
  26. debugMessage.Load(stream.ToArray());
  27. var debug = debugMessage.GetPacket(8, null);
  28. // MitM sends debug message to client
  29. _ = ServerSocket.Send(debug, 4, debug.Length - 4, SocketFlags.None);
  30. // MitM drops server message
  31. ServerOutboundPacketSequence++;
  32. }
  33. [TestMethod]
  34. public void ShouldThrowSshException()
  35. {
  36. var message = Assert.ThrowsException<SshException>(Session.Connect).Message;
  37. Assert.AreEqual("Message type 4 is not valid in the current context.", message);
  38. }
  39. }
  40. }