SshCommandTest_EndExecute_AsyncResultFromOtherInstance.cs 3.1 KB

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