SftpCloseRequestTest.cs 3.1 KB

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