2
0

SshTests_TTYDisabled.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. using Renci.SshNet.Common;
  2. using Renci.SshNet.IntegrationTests.Common;
  3. namespace Renci.SshNet.IntegrationTests
  4. {
  5. [TestClass]
  6. public class SshTests_TTYDisabled : TestBase
  7. {
  8. private IConnectionInfoFactory _connectionInfoFactory;
  9. private IConnectionInfoFactory _adminConnectionInfoFactory;
  10. private RemoteSshdConfig _remoteSshdConfig;
  11. [TestInitialize]
  12. public void SetUp()
  13. {
  14. _connectionInfoFactory = new LinuxVMConnectionFactory(SshServerHostName, SshServerPort);
  15. _adminConnectionInfoFactory = new LinuxAdminConnectionFactory(SshServerHostName, SshServerPort);
  16. _remoteSshdConfig = new RemoteSshd(_adminConnectionInfoFactory).OpenConfig();
  17. _remoteSshdConfig.AllowTcpForwarding()
  18. .PermitTTY(false)
  19. .PrintMotd(false)
  20. .Update()
  21. .Restart();
  22. }
  23. [TestCleanup]
  24. public void TearDown()
  25. {
  26. _remoteSshdConfig?.Reset();
  27. }
  28. [TestMethod]
  29. public void Ssh_CreateShellStream()
  30. {
  31. using (var client = new SshClient(_connectionInfoFactory.Create()))
  32. {
  33. client.Connect();
  34. try
  35. {
  36. client.CreateShellStream("xterm", 80, 24, 800, 600, 1024, null);
  37. Assert.Fail("Should not be able to create ShellStream with pseudo-terminal settings when PermitTTY is no at server side.");
  38. }
  39. catch (SshException ex)
  40. {
  41. Assert.AreEqual("The pseudo-terminal request was not accepted by the server. Consult the server log for more information.", ex.Message);
  42. }
  43. }
  44. }
  45. [TestMethod]
  46. public void Ssh_CreateShellStreamNoTerminal()
  47. {
  48. using (var client = new SshClient(_connectionInfoFactory.Create()))
  49. {
  50. client.Connect();
  51. using (var shellStream = client.CreateShellStreamNoTerminal(bufferSize: 1024))
  52. {
  53. var foo = new string('a', 90);
  54. shellStream.WriteLine($"echo {foo}");
  55. var line = shellStream.ReadLine(TimeSpan.FromSeconds(1));
  56. Assert.IsNotNull(line);
  57. Assert.IsTrue(line.EndsWith(foo), line);
  58. }
  59. }
  60. }
  61. [TestMethod]
  62. public void Ssh_CreateShell()
  63. {
  64. using (var client = new SshClient(_connectionInfoFactory.Create()))
  65. {
  66. client.Connect();
  67. using (var input = new MemoryStream())
  68. using (var output = new MemoryStream())
  69. using (var extOutput = new MemoryStream())
  70. {
  71. var shell = client.CreateShell(input, output, extOutput, "xterm", 80, 24, 800, 600, null, 1024);
  72. try
  73. {
  74. shell.Start();
  75. Assert.Fail("Should not be able to create ShellStream with terminal settings when PermitTTY is no at server side.");
  76. }
  77. catch (SshException ex)
  78. {
  79. Assert.AreEqual("The pseudo-terminal request was not accepted by the server. Consult the server log for more information.", ex.Message);
  80. }
  81. }
  82. }
  83. }
  84. [TestMethod]
  85. public void Ssh_CreateShellNoTerminal()
  86. {
  87. using (var client = new SshClient(_connectionInfoFactory.Create()))
  88. {
  89. client.Connect();
  90. using (var input = new MemoryStream())
  91. using (var output = new MemoryStream())
  92. using (var extOutput = new MemoryStream())
  93. {
  94. var shell = client.CreateShellNoTerminal(input, output, extOutput, 1024);
  95. shell.Start();
  96. var inputWriter = new StreamWriter(input, Encoding.ASCII, 1024);
  97. var foo = new string('a', 90);
  98. inputWriter.WriteLine($"echo {foo}");
  99. inputWriter.Flush();
  100. input.Position = 0;
  101. Thread.Sleep(1000);
  102. output.Position = 0;
  103. var outputReader = new StreamReader(output, Encoding.ASCII, false, 1024);
  104. var outputString = outputReader.ReadLine();
  105. Assert.IsNotNull(outputString);
  106. Assert.IsTrue(outputString.EndsWith(foo), outputString);
  107. shell.Stop();
  108. }
  109. }
  110. }
  111. }
  112. }