2
0

SubsystemSession_SendData_Disposed.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Text;
  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 TimeSpan _operationTimeout;
  19. private Encoding _encoding;
  20. private IList<EventArgs> _disconnectedRegister;
  21. private IList<ExceptionEventArgs> _errorOccurredRegister;
  22. private ObjectDisposedException _actualException;
  23. [TestInitialize]
  24. public void Setup()
  25. {
  26. Arrange();
  27. Act();
  28. }
  29. protected void Arrange()
  30. {
  31. var random = new Random();
  32. _subsystemName = random.Next().ToString(CultureInfo.InvariantCulture);
  33. _operationTimeout = TimeSpan.FromSeconds(30);
  34. _encoding = Encoding.UTF8;
  35. _disconnectedRegister = new List<EventArgs>();
  36. _errorOccurredRegister = new List<ExceptionEventArgs>();
  37. _sessionMock = new Mock<ISession>(MockBehavior.Strict);
  38. _channelMock = new Mock<IChannelSession>(MockBehavior.Strict);
  39. var sequence = new MockSequence();
  40. _sessionMock.InSequence(sequence).Setup(p => p.CreateChannelSession()).Returns(_channelMock.Object);
  41. _channelMock.InSequence(sequence).Setup(p => p.Open());
  42. _channelMock.InSequence(sequence).Setup(p => p.SendSubsystemRequest(_subsystemName)).Returns(true);
  43. _channelMock.InSequence(sequence).Setup(p => p.Close());
  44. _channelMock.InSequence(sequence).Setup(p => p.Dispose());
  45. _subsystemSession = new SubsystemSessionStub(
  46. _sessionMock.Object,
  47. _subsystemName,
  48. _operationTimeout,
  49. _encoding);
  50. _subsystemSession.Disconnected += (sender, args) => _disconnectedRegister.Add(args);
  51. _subsystemSession.ErrorOccurred += (sender, args) => _errorOccurredRegister.Add(args);
  52. _subsystemSession.Connect();
  53. _subsystemSession.Dispose();
  54. }
  55. protected void Act()
  56. {
  57. try
  58. {
  59. _subsystemSession.SendData(new byte[0]);
  60. }
  61. catch (ObjectDisposedException ex)
  62. {
  63. _actualException = ex;
  64. }
  65. }
  66. [TestMethod]
  67. public void SendDataShouldThrowObjectDisposedException()
  68. {
  69. Assert.IsNotNull(_actualException);
  70. Assert.AreEqual(string.Format("Cannot access a disposed object.{0}Object name: '{1}'.", Environment.NewLine, _actualException.ObjectName), _actualException.Message);
  71. Assert.AreEqual(typeof(SubsystemSessionStub).FullName, _actualException.ObjectName);
  72. }
  73. [TestMethod]
  74. public void DisconnectHasNeverFired()
  75. {
  76. Assert.AreEqual(0, _disconnectedRegister.Count);
  77. }
  78. [TestMethod]
  79. public void ErrorOccurredHasNeverFired()
  80. {
  81. Assert.AreEqual(0, _errorOccurredRegister.Count);
  82. }
  83. [TestMethod]
  84. public void IsOpenShouldReturnFalse()
  85. {
  86. Assert.IsFalse(_subsystemSession.IsOpen);
  87. }
  88. }
  89. }