PipeStreamTest_Dispose.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. using System;
  2. using System.IO;
  3. using Microsoft.VisualStudio.TestTools.UnitTesting;
  4. using Renci.SshNet.Common;
  5. using Renci.SshNet.Tests.Common;
  6. namespace Renci.SshNet.Tests.Classes
  7. {
  8. [TestClass]
  9. public class PipeStreamTest_Dispose : TestBase
  10. {
  11. private PipeStream _pipeStream;
  12. protected override void OnInit()
  13. {
  14. base.OnInit();
  15. Arrange();
  16. Act();
  17. }
  18. private void Arrange()
  19. {
  20. _pipeStream = new PipeStream();
  21. }
  22. private void Act()
  23. {
  24. _pipeStream.Dispose();
  25. }
  26. [TestMethod]
  27. public void BlockLastReadBuffer_Getter_ShouldThrowObjectDisposedException()
  28. {
  29. try
  30. {
  31. var value = _pipeStream.BlockLastReadBuffer;
  32. Assert.Fail("" + value);
  33. }
  34. catch (ObjectDisposedException)
  35. {
  36. }
  37. }
  38. [TestMethod]
  39. public void BlockLastReadBuffer_Setter_ShouldThrowObjectDisposedException()
  40. {
  41. try
  42. {
  43. _pipeStream.BlockLastReadBuffer = true;
  44. Assert.Fail();
  45. }
  46. catch (ObjectDisposedException)
  47. {
  48. }
  49. }
  50. [TestMethod]
  51. public void CanRead_ShouldReturnTrue()
  52. {
  53. Assert.IsFalse(_pipeStream.CanRead);
  54. }
  55. [TestMethod]
  56. public void Flush_ShouldThrowObjectDisposedException()
  57. {
  58. try
  59. {
  60. _pipeStream.Flush();
  61. Assert.Fail();
  62. }
  63. catch (ObjectDisposedException)
  64. {
  65. }
  66. }
  67. [TestMethod]
  68. public void MaxBufferLength_Getter_ShouldReturnTwoHundredMegabyte()
  69. {
  70. Assert.AreEqual(200 * 1024 * 1024, _pipeStream.MaxBufferLength);
  71. }
  72. [TestMethod]
  73. public void MaxBufferLength_Setter_ShouldModifyMaxBufferLength()
  74. {
  75. var newValue = new Random().Next(1, int.MaxValue);
  76. _pipeStream.MaxBufferLength = newValue;
  77. Assert.AreEqual(newValue, _pipeStream.MaxBufferLength);
  78. }
  79. [TestMethod]
  80. public void Length_ShouldThrowObjectDisposedException()
  81. {
  82. try
  83. {
  84. var value = _pipeStream.Length;
  85. Assert.Fail("" + value);
  86. }
  87. catch (ObjectDisposedException)
  88. {
  89. }
  90. }
  91. [TestMethod]
  92. public void Position_Getter_ShouldReturnZero()
  93. {
  94. Assert.AreEqual(0, _pipeStream.Position);
  95. }
  96. [TestMethod]
  97. public void Position_Setter_ShouldThrowNotSupportedException()
  98. {
  99. try
  100. {
  101. _pipeStream.Position = 0;
  102. Assert.Fail();
  103. }
  104. catch (NotSupportedException)
  105. {
  106. }
  107. }
  108. [TestMethod]
  109. public void Read_ByteArrayAndOffsetAndCount_ShouldThrowObjectDisposedException()
  110. {
  111. var buffer = new byte[0];
  112. const int offset = 0;
  113. const int count = 0;
  114. try
  115. {
  116. _pipeStream.Read(buffer, offset, count);
  117. Assert.Fail();
  118. }
  119. catch (ObjectDisposedException)
  120. {
  121. }
  122. }
  123. [TestMethod]
  124. public void ReadByte_ShouldThrowObjectDisposedException()
  125. {
  126. try
  127. {
  128. _pipeStream.ReadByte();
  129. Assert.Fail();
  130. }
  131. catch (ObjectDisposedException)
  132. {
  133. }
  134. }
  135. [TestMethod]
  136. public void Seek_ShouldThrowNotSupportedException()
  137. {
  138. try
  139. {
  140. _pipeStream.Seek(0, SeekOrigin.Begin);
  141. Assert.Fail();
  142. }
  143. catch (NotSupportedException)
  144. {
  145. }
  146. }
  147. [TestMethod]
  148. public void SetLength_ShouldThrowNotSupportedException()
  149. {
  150. try
  151. {
  152. _pipeStream.SetLength(0);
  153. Assert.Fail();
  154. }
  155. catch (NotSupportedException)
  156. {
  157. }
  158. }
  159. [TestMethod]
  160. public void Write_ByteArrayAndOffsetAndCount_ShouldThrowObjectDisposedException()
  161. {
  162. var buffer = new byte[0];
  163. const int offset = 0;
  164. const int count = 0;
  165. try
  166. {
  167. _pipeStream.Write(buffer, offset, count);
  168. Assert.Fail();
  169. }
  170. catch (ObjectDisposedException)
  171. {
  172. }
  173. }
  174. [TestMethod]
  175. public void WriteByte_ShouldThrowObjectDisposedException()
  176. {
  177. const byte b = 0x0a;
  178. try
  179. {
  180. _pipeStream.WriteByte(b);
  181. Assert.Fail();
  182. }
  183. catch (ObjectDisposedException)
  184. {
  185. }
  186. }
  187. }
  188. }