SshDataTest.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. protected override void LoadData()
  78. {
  79. }
  80. protected override void SaveData()
  81. {
  82. Write(_value);
  83. }
  84. }
  85. private class RequestSshData : SshData
  86. {
  87. private uint _valueOne;
  88. private uint _valueTwo;
  89. protected override int BufferCapacity
  90. {
  91. get
  92. {
  93. var capacity = base.BufferCapacity;
  94. capacity += 4; // ValueOne
  95. capacity += 4; // ValueTwo
  96. return capacity;
  97. }
  98. }
  99. public uint ValueOne
  100. {
  101. get { return _valueOne; }
  102. set { _valueOne = value; }
  103. }
  104. public uint ValueTwo
  105. {
  106. get { return _valueTwo; }
  107. set { _valueTwo = value; }
  108. }
  109. protected override void LoadData()
  110. {
  111. _valueOne = ReadUInt32();
  112. _valueTwo = ReadUInt32();
  113. }
  114. protected override void SaveData()
  115. {
  116. Write(ValueOne);
  117. Write(ValueTwo);
  118. }
  119. }
  120. }
  121. }