SshDataTest.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. using System;
  2. using Microsoft.VisualStudio.TestTools.UnitTesting;
  3. using Renci.SshNet.Common;
  4. namespace Renci.SshNet.Tests.Classes.Common
  5. {
  6. [TestClass]
  7. public class SshDataTest
  8. {
  9. [TestMethod]
  10. public void Write_Boolean_False()
  11. {
  12. var sshData = new BoolSshData(false);
  13. var bytes = sshData.GetBytes();
  14. Assert.AreEqual((byte)0, bytes[0]);
  15. }
  16. [TestMethod]
  17. public void Write_Boolean_True()
  18. {
  19. var sshData = new BoolSshData(true);
  20. var bytes = sshData.GetBytes();
  21. Assert.AreEqual((byte)1, bytes[0]);
  22. }
  23. [TestMethod]
  24. public void Load_Data()
  25. {
  26. const uint one = 123456u;
  27. const uint two = 456789u;
  28. var sshDataStream = new SshDataStream(8);
  29. sshDataStream.Write(one);
  30. sshDataStream.Write(two);
  31. var sshData = sshDataStream.ToArray();
  32. var request = new RequestSshData();
  33. request.Load(sshData);
  34. Assert.AreEqual(one, request.ValueOne);
  35. Assert.AreEqual(two, request.ValueTwo);
  36. }
  37. [TestMethod]
  38. public void Load_Data_ShouldThrowArgumentNullExceptionWhenDataIsNull()
  39. {
  40. const byte[] sshData = null;
  41. var request = new RequestSshData();
  42. try
  43. {
  44. request.Load(sshData);
  45. Assert.Fail();
  46. }
  47. catch (ArgumentNullException ex)
  48. {
  49. Assert.IsNull(ex.InnerException);
  50. Assert.AreEqual("data", ex.ParamName);
  51. }
  52. }
  53. [TestMethod]
  54. public void Load_DataAndOffsetAndCount()
  55. {
  56. const uint one = 123456u;
  57. const uint two = 456789u;
  58. var sshDataStream = new SshDataStream(11);
  59. sshDataStream.WriteByte(0x05);
  60. sshDataStream.WriteByte(0x07);
  61. sshDataStream.WriteByte(0x0f);
  62. sshDataStream.Write(one);
  63. sshDataStream.Write(two);
  64. var sshData = sshDataStream.ToArray();
  65. var request = new RequestSshData();
  66. request.Load(sshData, 3, sshData.Length - 3);
  67. Assert.AreEqual(one, request.ValueOne);
  68. Assert.AreEqual(two, request.ValueTwo);
  69. }
  70. [TestMethod]
  71. public void ReadBytes_Length_LengthEqualsNumberOfBytesAvailable()
  72. {
  73. MySshData sshData = new MySshData();
  74. sshData.Load(new byte[] { 0x01, 0x0d, 0x0a, 0x07, 0x0b }, 0, 4);
  75. sshData.Write(new byte[] { 0x09, 0x03, 0x06, 0x0b }, 0, 3);
  76. var bytes = sshData.ReadBytes(1);
  77. CollectionAssert.AreEqual(new byte[] { 0x07 }, bytes);
  78. }
  79. [TestMethod]
  80. public void ReadBytes_Length_LengthIsGreaterThanNumberOfBytesAvailable()
  81. {
  82. MySshData sshData = new MySshData();
  83. sshData.Load(new byte[] { 0x01, 0x0d, 0x0a }, 0, 3);
  84. try
  85. {
  86. sshData.ReadBytes(4);
  87. Assert.Fail();
  88. }
  89. catch (ArgumentOutOfRangeException ex)
  90. {
  91. Assert.AreEqual(typeof(ArgumentOutOfRangeException), ex.GetType());
  92. Assert.IsNull(ex.InnerException);
  93. Assert.AreEqual("length", ex.ParamName);
  94. }
  95. }
  96. [TestMethod]
  97. public void ReadBytes_Length_LengthIsLessThanNumberOfBytesAvailable()
  98. {
  99. MySshData sshData = new MySshData();
  100. sshData.Load(new byte[] { 0x05, 0x07, 0x02, 0x0b, 0x0a, 0x0d }, 0, 5);
  101. var bytes = sshData.ReadBytes(2);
  102. CollectionAssert.AreEqual(new byte[] { 0x05, 0x07 }, bytes);
  103. bytes = sshData.ReadBytes(3);
  104. CollectionAssert.AreEqual(new byte[] { 0x02, 0x0b, 0x0a }, bytes);
  105. }
  106. private class BoolSshData : SshData
  107. {
  108. private readonly bool _value;
  109. public BoolSshData(bool value)
  110. {
  111. _value = value;
  112. }
  113. protected override void LoadData()
  114. {
  115. }
  116. protected override void SaveData()
  117. {
  118. Write(_value);
  119. }
  120. }
  121. private class RequestSshData : SshData
  122. {
  123. private uint _valueOne;
  124. private uint _valueTwo;
  125. protected override int BufferCapacity
  126. {
  127. get
  128. {
  129. var capacity = base.BufferCapacity;
  130. capacity += 4; // ValueOne
  131. capacity += 4; // ValueTwo
  132. return capacity;
  133. }
  134. }
  135. public uint ValueOne
  136. {
  137. get { return _valueOne; }
  138. set { _valueOne = value; }
  139. }
  140. public uint ValueTwo
  141. {
  142. get { return _valueTwo; }
  143. set { _valueTwo = value; }
  144. }
  145. protected override void LoadData()
  146. {
  147. _valueOne = ReadUInt32();
  148. _valueTwo = ReadUInt32();
  149. }
  150. protected override void SaveData()
  151. {
  152. Write(ValueOne);
  153. Write(ValueTwo);
  154. }
  155. }
  156. private class MySshData : SshData
  157. {
  158. protected override void LoadData()
  159. {
  160. }
  161. protected override void SaveData()
  162. {
  163. }
  164. public new byte[] ReadBytes(int length)
  165. {
  166. return base.ReadBytes(length);
  167. }
  168. public new void Write(byte[] buffer, int offset, int count)
  169. {
  170. base.Write(buffer, offset, count);
  171. }
  172. }
  173. }
  174. }