SftpFSetStatRequestTest.cs 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. }