2
0

SubsystemSession_Connect_Disposed.cs 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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_Connect_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. _subsystemSession = new SubsystemSessionStub(
  44. _sessionMock.Object,
  45. _subsystemName,
  46. _operationTimeout,
  47. _encoding);
  48. _subsystemSession.Disconnected += (sender, args) => _disconnectedRegister.Add(args);
  49. _subsystemSession.ErrorOccurred += (sender, args) => _errorOccurredRegister.Add(args);
  50. _subsystemSession.Dispose();
  51. }
  52. protected void Act()
  53. {
  54. try
  55. {
  56. _subsystemSession.Connect();
  57. }
  58. catch (ObjectDisposedException ex)
  59. {
  60. _actualException = ex;
  61. }
  62. }
  63. [TestMethod]
  64. public void ConnectShouldThrowObjectDisposedException()
  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. }