2
0

SftpBlockRequestTest.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 SftpBlockRequestTest
  13. {
  14. private uint _protocolVersion;
  15. private uint _requestId;
  16. private byte[] _handle;
  17. private ulong _offset;
  18. private ulong _length;
  19. private uint _lockMask;
  20. [TestInitialize]
  21. public void Init()
  22. {
  23. var random = new Random();
  24. _protocolVersion = (uint)random.Next(0, int.MaxValue);
  25. _requestId = (uint)random.Next(0, int.MaxValue);
  26. _handle = new byte[random.Next(1, 10)];
  27. random.NextBytes(_handle);
  28. _offset = (ulong)random.Next(0, int.MaxValue);
  29. _length = (ulong)random.Next(0, int.MaxValue);
  30. _lockMask = (uint)random.Next(0, int.MaxValue);
  31. }
  32. [TestMethod]
  33. public void Constructor()
  34. {
  35. var request = new SftpBlockRequest(_protocolVersion, _requestId, _handle, _offset, _length, _lockMask, null);
  36. Assert.AreSame(_handle, request.Handle);
  37. Assert.AreEqual(_length, request.Length);
  38. Assert.AreEqual(_lockMask, request.LockMask);
  39. Assert.AreEqual(_offset, request.Offset);
  40. Assert.AreEqual(_protocolVersion, request.ProtocolVersion);
  41. Assert.AreEqual(_requestId, request.RequestId);
  42. Assert.AreEqual(SftpMessageTypes.Block, request.SftpMessageType);
  43. }
  44. [TestMethod]
  45. public void Complete_SftpStatusResponse()
  46. {
  47. IList<SftpStatusResponse> statusActionInvocations = new List<SftpStatusResponse>();
  48. Action<SftpStatusResponse> statusAction = statusActionInvocations.Add;
  49. var statusResponse = new SftpStatusResponse(_protocolVersion);
  50. var request = new SftpBlockRequest(_protocolVersion, _requestId, _handle, _offset, _length, _lockMask, statusAction);
  51. request.Complete(statusResponse);
  52. Assert.AreEqual(1, statusActionInvocations.Count);
  53. Assert.AreSame(statusResponse, statusActionInvocations[0]);
  54. }
  55. [TestMethod]
  56. public void GetBytes()
  57. {
  58. var request = new SftpBlockRequest(_protocolVersion, _requestId, _handle, _offset, _length, _lockMask, null);
  59. var bytes = request.GetBytes();
  60. var expectedBytesLength = 0;
  61. expectedBytesLength += 4; // Length
  62. expectedBytesLength += 1; // Type
  63. expectedBytesLength += 4; // RequestId
  64. expectedBytesLength += 4; // Handle length
  65. expectedBytesLength += _handle.Length; // Handle
  66. expectedBytesLength += 8; // Offset
  67. expectedBytesLength += 8; // Length
  68. expectedBytesLength += 4; // LockMask
  69. Assert.AreEqual(expectedBytesLength, bytes.Length);
  70. var sshDataStream = new SshDataStream(bytes);
  71. Assert.AreEqual((uint)bytes.Length - 4, sshDataStream.ReadUInt32());
  72. Assert.AreEqual((byte)SftpMessageTypes.Block, sshDataStream.ReadByte());
  73. Assert.AreEqual(_requestId, sshDataStream.ReadUInt32());
  74. Assert.AreEqual((uint)_handle.Length, sshDataStream.ReadUInt32());
  75. var actualHandle = new byte[_handle.Length];
  76. _ = sshDataStream.Read(actualHandle, 0, actualHandle.Length);
  77. Assert.IsTrue(_handle.SequenceEqual(actualHandle));
  78. Assert.AreEqual(_offset, sshDataStream.ReadUInt64());
  79. Assert.AreEqual(_length, sshDataStream.ReadUInt64());
  80. Assert.AreEqual(_lockMask, sshDataStream.ReadUInt32());
  81. Assert.IsTrue(sshDataStream.IsEndOfData);
  82. }
  83. }
  84. }