SftpCloseRequestTest.cs 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. expectedBytesLength += 4; // Length
  53. expectedBytesLength += 1; // Type
  54. expectedBytesLength += 4; // RequestId
  55. expectedBytesLength += 4; // Handle length
  56. expectedBytesLength += _handle.Length; // Handle
  57. Assert.AreEqual(expectedBytesLength, bytes.Length);
  58. var sshDataStream = new SshDataStream(bytes);
  59. Assert.AreEqual((uint)bytes.Length - 4, sshDataStream.ReadUInt32());
  60. Assert.AreEqual((byte)SftpMessageTypes.Close, sshDataStream.ReadByte());
  61. Assert.AreEqual(_requestId, sshDataStream.ReadUInt32());
  62. Assert.AreEqual((uint)_handle.Length, sshDataStream.ReadUInt32());
  63. var actualHandle = new byte[_handle.Length];
  64. _ = sshDataStream.Read(actualHandle, 0, actualHandle.Length);
  65. Assert.IsTrue(_handle.SequenceEqual(actualHandle));
  66. Assert.IsTrue(sshDataStream.IsEndOfData);
  67. }
  68. }
  69. }