2
0

SftpFSetStatRequestTest.cs 3.5 KB

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