2
0

SshCommandTest_EndExecute_AsyncResultFromOtherInstance.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. using System;
  2. using System.Globalization;
  3. using System.Text;
  4. using Microsoft.Extensions.Logging.Abstractions;
  5. using Microsoft.VisualStudio.TestTools.UnitTesting;
  6. using Moq;
  7. using Renci.SshNet.Channels;
  8. using Renci.SshNet.Tests.Common;
  9. namespace Renci.SshNet.Tests.Classes
  10. {
  11. [TestClass]
  12. public class SshCommandTest_EndExecute_AsyncResultFromOtherInstance : TestBase
  13. {
  14. private Mock<ISession> _sessionMock;
  15. private Mock<IChannelSession> _channelSessionAMock;
  16. private Mock<IChannelSession> _channelSessionBMock;
  17. private string _commandText;
  18. private Encoding _encoding;
  19. private SshCommand _sshCommandA;
  20. private SshCommand _sshCommandB;
  21. private ArgumentException _actualException;
  22. private IAsyncResult _asyncResultB;
  23. protected override void OnInit()
  24. {
  25. base.OnInit();
  26. Arrange();
  27. Act();
  28. }
  29. private void Arrange()
  30. {
  31. _sessionMock = new Mock<ISession>(MockBehavior.Strict);
  32. _sessionMock.Setup(p => p.SessionLoggerFactory).Returns(NullLoggerFactory.Instance);
  33. _channelSessionAMock = new Mock<IChannelSession>(MockBehavior.Strict);
  34. _channelSessionBMock = new Mock<IChannelSession>(MockBehavior.Strict);
  35. _commandText = new Random().Next().ToString(CultureInfo.InvariantCulture);
  36. _encoding = Encoding.UTF8;
  37. _asyncResultB = null;
  38. var seq = new MockSequence();
  39. _ = _sessionMock.InSequence(seq)
  40. .Setup(p => p.CreateChannelSession())
  41. .Returns(_channelSessionAMock.Object);
  42. _ = _channelSessionAMock.InSequence(seq)
  43. .Setup(p => p.Open());
  44. _ = _channelSessionAMock.InSequence(seq)
  45. .Setup(p => p.SendExecRequest(_commandText))
  46. .Returns(true);
  47. _ = _sessionMock.InSequence(seq)
  48. .Setup(p => p.CreateChannelSession())
  49. .Returns(_channelSessionBMock.Object);
  50. _ = _channelSessionBMock.InSequence(seq)
  51. .Setup(p => p.Open());
  52. _ = _channelSessionBMock.InSequence(seq)
  53. .Setup(p => p.SendExecRequest(_commandText))
  54. .Returns(true);
  55. _sshCommandA = new SshCommand(_sessionMock.Object, _commandText, _encoding);
  56. _ = _sshCommandA.BeginExecute();
  57. _sshCommandB = new SshCommand(_sessionMock.Object, _commandText, _encoding);
  58. _asyncResultB = _sshCommandB.BeginExecute();
  59. }
  60. private void Act()
  61. {
  62. try
  63. {
  64. _ = _sshCommandA.EndExecute(_asyncResultB);
  65. Assert.Fail();
  66. }
  67. catch (ArgumentException ex)
  68. {
  69. _actualException = ex;
  70. }
  71. }
  72. [TestMethod]
  73. public void EndExecuteShouldHaveThrownArgumentException()
  74. {
  75. Assert.IsNotNull(_actualException);
  76. Assert.IsNull(_actualException.InnerException);
  77. Assert.AreEqual("asyncResult", _actualException.ParamName);
  78. }
  79. }
  80. }