2
0

SubsystemSession_SendData_Disposed.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using Microsoft.Extensions.Logging.Abstractions;
  5. using Microsoft.VisualStudio.TestTools.UnitTesting;
  6. using Moq;
  7. using Renci.SshNet.Channels;
  8. using Renci.SshNet.Common;
  9. namespace Renci.SshNet.Tests.Classes
  10. {
  11. [TestClass]
  12. public class SubsystemSession_SendData_Disposed
  13. {
  14. private Mock<ISession> _sessionMock;
  15. private Mock<IChannelSession> _channelMock;
  16. private string _subsystemName;
  17. private SubsystemSessionStub _subsystemSession;
  18. private int _operationTimeout;
  19. private IList<EventArgs> _disconnectedRegister;
  20. private IList<ExceptionEventArgs> _errorOccurredRegister;
  21. private ObjectDisposedException _actualException;
  22. [TestInitialize]
  23. public void Setup()
  24. {
  25. Arrange();
  26. Act();
  27. }
  28. protected void Arrange()
  29. {
  30. var random = new Random();
  31. _subsystemName = random.Next().ToString(CultureInfo.InvariantCulture);
  32. _operationTimeout = 30000;
  33. _disconnectedRegister = new List<EventArgs>();
  34. _errorOccurredRegister = new List<ExceptionEventArgs>();
  35. _sessionMock = new Mock<ISession>(MockBehavior.Strict);
  36. _sessionMock.Setup(p => p.SessionLoggerFactory).Returns(NullLoggerFactory.Instance);
  37. _channelMock = new Mock<IChannelSession>(MockBehavior.Strict);
  38. var sequence = new MockSequence();
  39. _sessionMock.InSequence(sequence).Setup(p => p.CreateChannelSession()).Returns(_channelMock.Object);
  40. _channelMock.InSequence(sequence).Setup(p => p.Open());
  41. _channelMock.InSequence(sequence).Setup(p => p.SendSubsystemRequest(_subsystemName)).Returns(true);
  42. _channelMock.InSequence(sequence).Setup(p => p.Dispose());
  43. _subsystemSession = new SubsystemSessionStub(
  44. _sessionMock.Object,
  45. _subsystemName,
  46. _operationTimeout);
  47. _subsystemSession.Disconnected += (sender, args) => _disconnectedRegister.Add(args);
  48. _subsystemSession.ErrorOccurred += (sender, args) => _errorOccurredRegister.Add(args);
  49. _subsystemSession.Connect();
  50. _subsystemSession.Dispose();
  51. }
  52. protected void Act()
  53. {
  54. try
  55. {
  56. _subsystemSession.SendData(new byte[0]);
  57. }
  58. catch (ObjectDisposedException ex)
  59. {
  60. _actualException = ex;
  61. }
  62. }
  63. [TestMethod]
  64. public void SendDataShouldThrowObjectDisposedException()
  65. {
  66. Assert.IsNotNull(_actualException);
  67. Assert.AreEqual(string.Format("Cannot access a disposed object.{0}Object name: '{1}'.", Environment.NewLine, _actualException.ObjectName), _actualException.Message);
  68. Assert.AreEqual(typeof(SubsystemSessionStub).FullName, _actualException.ObjectName);
  69. }
  70. [TestMethod]
  71. public void DisconnectHasNeverFired()
  72. {
  73. Assert.AreEqual(0, _disconnectedRegister.Count);
  74. }
  75. [TestMethod]
  76. public void ErrorOccurredHasNeverFired()
  77. {
  78. Assert.AreEqual(0, _errorOccurredRegister.Count);
  79. }
  80. [TestMethod]
  81. public void IsOpenShouldReturnFalse()
  82. {
  83. Assert.IsFalse(_subsystemSession.IsOpen);
  84. }
  85. }
  86. }