SftpUnblockRequestTest.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. using Renci.SshNet.Tests.Common;
  10. namespace Renci.SshNet.Tests.Classes.Sftp.Requests
  11. {
  12. [TestClass]
  13. public class SftpUnblockRequestTest
  14. {
  15. private uint _protocolVersion;
  16. private uint _requestId;
  17. private byte[] _handle;
  18. private ulong _offset;
  19. private ulong _length;
  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. }
  31. [TestMethod]
  32. public void Constructor()
  33. {
  34. var request = new SftpUnblockRequest(_protocolVersion, _requestId, _handle, _offset, _length, null);
  35. Assert.AreSame(_handle, request.Handle);
  36. Assert.AreEqual(_protocolVersion, request.ProtocolVersion);
  37. Assert.AreEqual(_requestId, request.RequestId);
  38. Assert.AreEqual(SftpMessageTypes.Unblock, request.SftpMessageType);
  39. }
  40. [TestMethod]
  41. public void Complete_SftpStatusResponse()
  42. {
  43. var statusActionInvocations = new List<SftpStatusResponse>();
  44. Action<SftpStatusResponse> statusAction = statusActionInvocations.Add;
  45. var statusResponse = new SftpStatusResponse(_protocolVersion);
  46. var request = new SftpUnblockRequest(_protocolVersion, _requestId, _handle, _offset, _length, statusAction);
  47. request.Complete(statusResponse);
  48. Assert.AreEqual(1, statusActionInvocations.Count);
  49. Assert.AreSame(statusResponse, statusActionInvocations[0]);
  50. }
  51. [TestMethod]
  52. public void GetBytes()
  53. {
  54. var request = new SftpUnblockRequest(_protocolVersion, _requestId, _handle, _offset, _length, null);
  55. var bytes = request.GetBytes();
  56. var expectedBytesLength = 0;
  57. #if TUNING
  58. expectedBytesLength += 4; // Length
  59. #endif
  60. expectedBytesLength += 1; // Type
  61. expectedBytesLength += 4; // RequestId
  62. expectedBytesLength += 4; // Handle length
  63. expectedBytesLength += _handle.Length; // Handle
  64. expectedBytesLength += 8; // Offset
  65. expectedBytesLength += 8; // Length
  66. Assert.AreEqual(expectedBytesLength, bytes.Length);
  67. var sshDataStream = new SshDataStream(bytes);
  68. #if TUNING
  69. Assert.AreEqual((uint) bytes.Length - 4, sshDataStream.ReadUInt32());
  70. #endif
  71. Assert.AreEqual((byte) SftpMessageTypes.Unblock, sshDataStream.ReadByte());
  72. Assert.AreEqual(_requestId, sshDataStream.ReadUInt32());
  73. Assert.AreEqual((uint) _handle.Length, sshDataStream.ReadUInt32());
  74. var actualHandle = new byte[_handle.Length];
  75. sshDataStream.Read(actualHandle, 0, actualHandle.Length);
  76. Assert.IsTrue(_handle.SequenceEqual(actualHandle));
  77. Assert.AreEqual(_offset, sshDataStream.ReadUInt64());
  78. Assert.AreEqual(_length, sshDataStream.ReadUInt64());
  79. Assert.IsTrue(sshDataStream.IsEndOfData);
  80. }
  81. }
  82. }