2
0

SftpSetStatRequestTest.cs 3.9 KB

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