ChannelTest_SendEof_ChannelIsNotOpen.cs 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. using System;
  2. using System.Collections.Generic;
  3. using Microsoft.VisualStudio.TestTools.UnitTesting;
  4. using Moq;
  5. using Renci.SshNet.Common;
  6. using Renci.SshNet.Messages;
  7. namespace Renci.SshNet.Tests.Classes.Channels
  8. {
  9. [TestClass]
  10. public class ChannelTest_SendEof_ChannelIsNotOpen
  11. {
  12. private Mock<ISession> _sessionMock;
  13. private uint _localWindowSize;
  14. private uint _localPacketSize;
  15. private uint _localChannelNumber;
  16. private ChannelStub _channel;
  17. private List<ChannelEventArgs> _channelClosedRegister;
  18. private IList<ExceptionEventArgs> _channelExceptionRegister;
  19. private InvalidOperationException _actualException;
  20. [TestInitialize]
  21. public void Initialize()
  22. {
  23. Arrange();
  24. Act();
  25. }
  26. private void Arrange()
  27. {
  28. var random = new Random();
  29. _localChannelNumber = (uint)random.Next(0, int.MaxValue);
  30. _localWindowSize = (uint)random.Next(0, int.MaxValue);
  31. _localPacketSize = (uint)random.Next(0, int.MaxValue);
  32. _channelClosedRegister = new List<ChannelEventArgs>();
  33. _channelExceptionRegister = new List<ExceptionEventArgs>();
  34. _actualException = null;
  35. _sessionMock = new Mock<ISession>(MockBehavior.Strict);
  36. _channel = new ChannelStub(_sessionMock.Object, _localChannelNumber, _localWindowSize, _localPacketSize);
  37. _channel.Closed += (sender, args) => _channelClosedRegister.Add(args);
  38. _channel.Exception += (sender, args) => _channelExceptionRegister.Add(args);
  39. }
  40. private void Act()
  41. {
  42. try
  43. {
  44. _channel.SendEof();
  45. Assert.Fail();
  46. }
  47. catch (InvalidOperationException ex)
  48. {
  49. _actualException = ex;
  50. }
  51. }
  52. [TestMethod]
  53. public void IsOpenShouldReturnFalse()
  54. {
  55. Assert.IsFalse(_channel.IsOpen);
  56. }
  57. [TestMethod]
  58. public void SendEofShouldHaveThrownInvalidOperationException()
  59. {
  60. Assert.IsNotNull(_actualException);
  61. Assert.IsNull(_actualException.InnerException);
  62. Assert.AreEqual("The channel is closed.", _actualException.Message);
  63. }
  64. [TestMethod]
  65. public void SendMessageOnSessionShouldNeverBeInvoked()
  66. {
  67. _sessionMock.Verify(p => p.SendMessage(It.IsAny<Message>()), Times.Never);
  68. }
  69. [TestMethod]
  70. public void ClosedEventShouldNeverHaveFired()
  71. {
  72. Assert.AreEqual(0, _channelClosedRegister.Count);
  73. }
  74. [TestMethod]
  75. public void ExceptionShouldNeverHaveFired()
  76. {
  77. Assert.AreEqual(0, _channelExceptionRegister.Count);
  78. }
  79. }
  80. }