SftpReadRequestTest.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Microsoft.VisualStudio.TestTools.UnitTesting;
  5. using Renci.SshNet.Common;
  6. using Renci.SshNet.Sftp;
  7. using Renci.SshNet.Sftp.Requests;
  8. using Renci.SshNet.Sftp.Responses;
  9. namespace Renci.SshNet.Tests.Classes.Sftp.Requests
  10. {
  11. [TestClass]
  12. public class SftpReadRequestTest
  13. {
  14. private uint _protocolVersion;
  15. private uint _requestId;
  16. private byte[] _handle;
  17. private ulong _offset;
  18. private uint _length;
  19. [TestInitialize]
  20. public void Init()
  21. {
  22. var random = new Random();
  23. _protocolVersion = (uint)random.Next(0, int.MaxValue);
  24. _requestId = (uint)random.Next(0, int.MaxValue);
  25. _handle = new byte[random.Next(1, 10)];
  26. random.NextBytes(_handle);
  27. _offset = (ulong)random.Next(0, int.MaxValue);
  28. _length = (uint)random.Next(0, int.MaxValue);
  29. }
  30. [TestMethod]
  31. public void Constructor()
  32. {
  33. var request = new SftpReadRequest(_protocolVersion, _requestId, _handle, _offset, _length, null, null);
  34. Assert.AreSame(_handle, request.Handle);
  35. Assert.AreEqual(_length, request.Length);
  36. Assert.AreEqual(_offset, request.Offset);
  37. Assert.AreEqual(_protocolVersion, request.ProtocolVersion);
  38. Assert.AreEqual(_requestId, request.RequestId);
  39. Assert.AreEqual(SftpMessageTypes.Read, request.SftpMessageType);
  40. }
  41. [TestMethod]
  42. public void Complete_SftpDataResponse()
  43. {
  44. var statusActionInvocations = new List<SftpStatusResponse>();
  45. var dataActionInvocations = new List<SftpDataResponse>();
  46. Action<SftpStatusResponse> statusAction = statusActionInvocations.Add;
  47. Action<SftpDataResponse> dataAction = dataActionInvocations.Add;
  48. var dataResponse = new SftpDataResponse(_protocolVersion);
  49. var request = new SftpReadRequest(
  50. _protocolVersion,
  51. _requestId,
  52. _handle,
  53. _offset,
  54. _length,
  55. dataAction,
  56. statusAction);
  57. request.Complete(dataResponse);
  58. Assert.AreEqual(0, statusActionInvocations.Count);
  59. Assert.AreEqual(1, dataActionInvocations.Count);
  60. Assert.AreSame(dataResponse, dataActionInvocations[0]);
  61. }
  62. [TestMethod]
  63. public void Complete_SftpStatusResponse()
  64. {
  65. var statusActionInvocations = new List<SftpStatusResponse>();
  66. var dataActionInvocations = new List<SftpDataResponse>();
  67. Action<SftpStatusResponse> statusAction = statusActionInvocations.Add;
  68. Action<SftpDataResponse> dataAction = dataActionInvocations.Add;
  69. var statusResponse = new SftpStatusResponse(_protocolVersion);
  70. var request = new SftpReadRequest(
  71. _protocolVersion,
  72. _requestId,
  73. _handle,
  74. _offset,
  75. _length,
  76. dataAction,
  77. statusAction);
  78. request.Complete(statusResponse);
  79. Assert.AreEqual(1, statusActionInvocations.Count);
  80. Assert.AreSame(statusResponse, statusActionInvocations[0]);
  81. Assert.AreEqual(0, dataActionInvocations.Count);
  82. }
  83. [TestMethod]
  84. public void GetBytes()
  85. {
  86. var request = new SftpReadRequest(_protocolVersion, _requestId, _handle, _offset, _length, null, null);
  87. var bytes = request.GetBytes();
  88. var expectedBytesLength = 0;
  89. expectedBytesLength += 4; // Length
  90. expectedBytesLength += 1; // Type
  91. expectedBytesLength += 4; // RequestId
  92. expectedBytesLength += 4; // Handle length
  93. expectedBytesLength += _handle.Length; // Handle
  94. expectedBytesLength += 8; // Offset
  95. expectedBytesLength += 4; // Length
  96. Assert.AreEqual(expectedBytesLength, bytes.Length);
  97. var sshDataStream = new SshDataStream(bytes);
  98. Assert.AreEqual((uint)bytes.Length - 4, sshDataStream.ReadUInt32());
  99. Assert.AreEqual((byte)SftpMessageTypes.Read, sshDataStream.ReadByte());
  100. Assert.AreEqual(_requestId, sshDataStream.ReadUInt32());
  101. Assert.AreEqual((uint)_handle.Length, sshDataStream.ReadUInt32());
  102. var actualHandle = new byte[_handle.Length];
  103. _ = sshDataStream.Read(actualHandle, 0, actualHandle.Length);
  104. Assert.IsTrue(_handle.SequenceEqual(actualHandle));
  105. Assert.AreEqual(_offset, sshDataStream.ReadUInt64());
  106. Assert.AreEqual(_length, sshDataStream.ReadUInt32());
  107. Assert.IsTrue(sshDataStream.IsEndOfData);
  108. }
  109. }
  110. }