2
0

SubsystemSession_SendData_Disposed.cs 3.3 KB

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