SftpFSetStatRequestTest.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 SftpFSetStatRequestTest
  14. {
  15. private uint _protocolVersion;
  16. private uint _requestId;
  17. private byte[] _handle;
  18. private SftpFileAttributes _attributes;
  19. private byte[] _attributesBytes;
  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. _attributes = SftpFileAttributesBuilder.Empty;
  29. _attributesBytes = _attributes.GetBytes();
  30. }
  31. [TestMethod]
  32. public void Constructor()
  33. {
  34. var request = new SftpFSetStatRequest(_protocolVersion, _requestId, _handle, _attributes, null);
  35. Assert.AreSame(_handle, request.Handle);
  36. Assert.AreEqual(_protocolVersion, request.ProtocolVersion);
  37. Assert.AreEqual(_requestId, request.RequestId);
  38. Assert.AreEqual(SftpMessageTypes.FSetStat, request.SftpMessageType);
  39. }
  40. [TestMethod]
  41. public void Complete_SftpStatusResponse()
  42. {
  43. IList<SftpStatusResponse> statusActionInvocations = new List<SftpStatusResponse>();
  44. Action<SftpStatusResponse> statusAction = statusActionInvocations.Add;
  45. var statusResponse = new SftpStatusResponse(_protocolVersion);
  46. var request = new SftpFSetStatRequest(_protocolVersion, _requestId, _handle, _attributes, 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 SftpFSetStatRequest(_protocolVersion, _requestId, _handle, _attributes, null);
  55. var bytes = request.GetBytes();
  56. var expectedBytesLength = 0;
  57. expectedBytesLength += 4; // Length
  58. expectedBytesLength += 1; // Type
  59. expectedBytesLength += 4; // RequestId
  60. expectedBytesLength += 4; // Handle length
  61. expectedBytesLength += _handle.Length; // Handle
  62. expectedBytesLength += _attributesBytes.Length; // Attributes
  63. Assert.AreEqual(expectedBytesLength, bytes.Length);
  64. var sshDataStream = new SshDataStream(bytes);
  65. Assert.AreEqual((uint)bytes.Length - 4, sshDataStream.ReadUInt32());
  66. Assert.AreEqual((byte)SftpMessageTypes.FSetStat, sshDataStream.ReadByte());
  67. Assert.AreEqual(_requestId, sshDataStream.ReadUInt32());
  68. Assert.AreEqual((uint)_handle.Length, sshDataStream.ReadUInt32());
  69. var actualHandle = new byte[_handle.Length];
  70. _ = sshDataStream.Read(actualHandle, 0, actualHandle.Length);
  71. Assert.IsTrue(_handle.SequenceEqual(actualHandle));
  72. var actualAttributes = new byte[_attributesBytes.Length];
  73. _ = sshDataStream.Read(actualAttributes, 0, actualAttributes.Length);
  74. Assert.IsTrue(_attributesBytes.SequenceEqual(actualAttributes));
  75. Assert.IsTrue(sshDataStream.IsEndOfData);
  76. }
  77. }
  78. }