2
0

SshDataTest.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. private class BoolSshData : SshData
  71. {
  72. private readonly bool _value;
  73. public BoolSshData(bool value)
  74. {
  75. _value = value;
  76. }
  77. public new bool IsEndOfData
  78. {
  79. get { return base.IsEndOfData; }
  80. }
  81. public new byte ReadByte()
  82. {
  83. return base.ReadByte();
  84. }
  85. protected override void LoadData()
  86. {
  87. }
  88. protected override void SaveData()
  89. {
  90. Write(_value);
  91. }
  92. }
  93. private class RequestSshData : SshData
  94. {
  95. private uint _valueOne;
  96. private uint _valueTwo;
  97. public RequestSshData()
  98. {
  99. }
  100. public RequestSshData(uint one, uint two)
  101. {
  102. _valueOne = one;
  103. _valueTwo = two;
  104. }
  105. protected override int BufferCapacity
  106. {
  107. get
  108. {
  109. var capacity = base.BufferCapacity;
  110. capacity += 4; // ValueOne
  111. capacity += 4; // ValueTwo
  112. return capacity;
  113. }
  114. }
  115. public uint ValueOne
  116. {
  117. get { return _valueOne; }
  118. set { _valueOne = value; }
  119. }
  120. public uint ValueTwo
  121. {
  122. get { return _valueTwo; }
  123. set { _valueTwo = value; }
  124. }
  125. protected override void LoadData()
  126. {
  127. _valueOne = ReadUInt32();
  128. _valueTwo = ReadUInt32();
  129. }
  130. protected override void SaveData()
  131. {
  132. Write(ValueOne);
  133. Write(ValueTwo);
  134. }
  135. }
  136. private class ReplySshData : SshData
  137. {
  138. private uint _valueOne;
  139. public ReplySshData()
  140. {
  141. }
  142. protected override int BufferCapacity
  143. {
  144. get
  145. {
  146. var capacity = base.BufferCapacity;
  147. capacity += 4; // ValueOne
  148. return capacity;
  149. }
  150. }
  151. public uint ValueOne
  152. {
  153. get { return _valueOne; }
  154. set { _valueOne = value; }
  155. }
  156. protected override void LoadData()
  157. {
  158. _valueOne = ReadUInt32();
  159. }
  160. protected override void SaveData()
  161. {
  162. Write(ValueOne);
  163. }
  164. }
  165. }
  166. }