2
0

SftpSetStatRequestTest.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Linq;
  5. using System.Text;
  6. using Microsoft.VisualStudio.TestTools.UnitTesting;
  7. using Renci.SshNet.Common;
  8. using Renci.SshNet.Sftp;
  9. using Renci.SshNet.Sftp.Requests;
  10. using Renci.SshNet.Sftp.Responses;
  11. using Renci.SshNet.Tests.Common;
  12. namespace Renci.SshNet.Tests.Classes.Sftp.Requests
  13. {
  14. [TestClass]
  15. public class SftpSetStatRequestTest
  16. {
  17. private uint _protocolVersion;
  18. private uint _requestId;
  19. private Encoding _encoding;
  20. private string _path;
  21. private byte[] _pathBytes;
  22. private SftpFileAttributes _attributes;
  23. private byte[] _attributesBytes;
  24. [TestInitialize]
  25. public void Init()
  26. {
  27. var random = new Random();
  28. _protocolVersion = (uint)random.Next(0, int.MaxValue);
  29. _requestId = (uint)random.Next(0, int.MaxValue);
  30. _encoding = Encoding.Unicode;
  31. _path = random.Next().ToString(CultureInfo.InvariantCulture);
  32. _pathBytes = _encoding.GetBytes(_path);
  33. _attributes = SftpFileAttributesBuilder.Empty;
  34. _attributesBytes = _attributes.GetBytes();
  35. }
  36. [TestMethod]
  37. public void Constructor()
  38. {
  39. var request = new SftpSetStatRequest(_protocolVersion, _requestId, _path, _encoding, _attributes, null);
  40. Assert.AreSame(_encoding, request.Encoding);
  41. Assert.AreEqual(_path, request.Path);
  42. Assert.AreEqual(_protocolVersion, request.ProtocolVersion);
  43. Assert.AreEqual(_requestId, request.RequestId);
  44. Assert.AreEqual(SftpMessageTypes.SetStat, request.SftpMessageType);
  45. }
  46. [TestMethod]
  47. public void Complete_SftpStatusResponse()
  48. {
  49. var statusActionInvocations = new List<SftpStatusResponse>();
  50. Action<SftpStatusResponse> statusAction = statusActionInvocations.Add;
  51. var statusResponse = new SftpStatusResponse(_protocolVersion);
  52. var request = new SftpSetStatRequest(
  53. _protocolVersion,
  54. _requestId,
  55. _path,
  56. _encoding,
  57. _attributes,
  58. statusAction);
  59. request.Complete(statusResponse);
  60. Assert.AreEqual(1, statusActionInvocations.Count);
  61. Assert.AreSame(statusResponse, statusActionInvocations[0]);
  62. }
  63. [TestMethod]
  64. public void GetBytes()
  65. {
  66. var request = new SftpSetStatRequest(_protocolVersion, _requestId, _path, _encoding, _attributes, null);
  67. var bytes = request.GetBytes();
  68. var expectedBytesLength = 0;
  69. expectedBytesLength += 4; // Length
  70. expectedBytesLength += 1; // Type
  71. expectedBytesLength += 4; // RequestId
  72. expectedBytesLength += 4; // Path length
  73. expectedBytesLength += _pathBytes.Length; // Path
  74. expectedBytesLength += _attributesBytes.Length; // Attributes
  75. Assert.AreEqual(expectedBytesLength, bytes.Length);
  76. var sshDataStream = new SshDataStream(bytes);
  77. Assert.AreEqual((uint)bytes.Length - 4, sshDataStream.ReadUInt32());
  78. Assert.AreEqual((byte)SftpMessageTypes.SetStat, sshDataStream.ReadByte());
  79. Assert.AreEqual(_requestId, sshDataStream.ReadUInt32());
  80. Assert.AreEqual((uint)_pathBytes.Length, sshDataStream.ReadUInt32());
  81. var actualPath = new byte[_pathBytes.Length];
  82. _ = sshDataStream.Read(actualPath, 0, actualPath.Length);
  83. Assert.IsTrue(_pathBytes.SequenceEqual(actualPath));
  84. var actualAttributes = new byte[_attributesBytes.Length];
  85. _ = sshDataStream.Read(actualAttributes, 0, actualAttributes.Length);
  86. Assert.IsTrue(_attributesBytes.SequenceEqual(actualAttributes));
  87. Assert.IsTrue(sshDataStream.IsEndOfData);
  88. }
  89. }
  90. }