2
0

StatVfsRequestTest.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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.ExtendedRequests
  12. {
  13. [TestClass]
  14. public class StatVfsRequestTest
  15. {
  16. private uint _protocolVersion;
  17. private uint _requestId;
  18. private string _name;
  19. private string _path;
  20. private byte[] _pathBytes;
  21. private byte[] _nameBytes;
  22. private Encoding _encoding;
  23. [TestInitialize]
  24. public void Init()
  25. {
  26. var random = new Random();
  27. _encoding = Encoding.Unicode;
  28. _protocolVersion = (uint)random.Next(0, int.MaxValue);
  29. _requestId = (uint)random.Next(0, int.MaxValue);
  30. _path = random.Next().ToString(CultureInfo.InvariantCulture);
  31. _pathBytes = _encoding.GetBytes(_path);
  32. _name = "statvfs@openssh.com";
  33. _nameBytes = Encoding.UTF8.GetBytes(_name);
  34. }
  35. [TestMethod]
  36. public void Constructor()
  37. {
  38. var request = new StatVfsRequest(_protocolVersion, _requestId, _path, _encoding, null, null);
  39. Assert.AreSame(_encoding, request.Encoding);
  40. Assert.AreEqual(_name, request.Name);
  41. Assert.AreEqual(_path, request.Path);
  42. Assert.AreEqual(_protocolVersion, request.ProtocolVersion);
  43. Assert.AreEqual(_requestId, request.RequestId);
  44. Assert.AreEqual(SftpMessageTypes.Extended, request.SftpMessageType);
  45. }
  46. [TestMethod]
  47. public void Complete_SftpStatusResponse()
  48. {
  49. IList<SftpStatusResponse> statusActionInvocations = new List<SftpStatusResponse>();
  50. IList<SftpExtendedReplyResponse> extendedReplyActionInvocations = new List<SftpExtendedReplyResponse>();
  51. Action<SftpExtendedReplyResponse> extendedAction = extendedReplyActionInvocations.Add;
  52. Action<SftpStatusResponse> statusAction = statusActionInvocations.Add;
  53. var statusResponse = new SftpStatusResponse(_protocolVersion);
  54. var request = new StatVfsRequest(_protocolVersion, _requestId, _path, _encoding, extendedAction, statusAction);
  55. request.Complete(statusResponse);
  56. Assert.AreEqual(1, statusActionInvocations.Count);
  57. Assert.AreSame(statusResponse, statusActionInvocations[0]);
  58. Assert.AreEqual(0, extendedReplyActionInvocations.Count);
  59. }
  60. [TestMethod]
  61. public void Complete_SftpExtendedReplyResponse()
  62. {
  63. IList<SftpStatusResponse> statusActionInvocations = new List<SftpStatusResponse>();
  64. IList<SftpExtendedReplyResponse> extendedReplyActionInvocations = new List<SftpExtendedReplyResponse>();
  65. Action<SftpExtendedReplyResponse> extendedAction = extendedReplyActionInvocations.Add;
  66. Action<SftpStatusResponse> statusAction = statusActionInvocations.Add;
  67. var extendedReplyResponse = new SftpExtendedReplyResponse(_protocolVersion);
  68. var request = new StatVfsRequest(_protocolVersion, _requestId, _path, _encoding, extendedAction, statusAction);
  69. request.Complete(extendedReplyResponse);
  70. Assert.AreEqual(0, statusActionInvocations.Count);
  71. Assert.AreEqual(1, extendedReplyActionInvocations.Count);
  72. Assert.AreSame(extendedReplyResponse, extendedReplyActionInvocations[0]);
  73. }
  74. [TestMethod]
  75. public void GetBytes()
  76. {
  77. var request = new StatVfsRequest(_protocolVersion, _requestId, _path, _encoding, null, null);
  78. var bytes = request.GetBytes();
  79. var expectedBytesLength = 0;
  80. expectedBytesLength += 4; // Length
  81. expectedBytesLength += 1; // Type
  82. expectedBytesLength += 4; // RequestId
  83. expectedBytesLength += 4; // Name length
  84. expectedBytesLength += _nameBytes.Length; // Name
  85. expectedBytesLength += 4; // Path length
  86. expectedBytesLength += _pathBytes.Length; // Path
  87. Assert.AreEqual(expectedBytesLength, bytes.Length);
  88. var sshDataStream = new SshDataStream(bytes);
  89. Assert.AreEqual((uint)bytes.Length - 4, sshDataStream.ReadUInt32());
  90. Assert.AreEqual((byte)SftpMessageTypes.Extended, sshDataStream.ReadByte());
  91. Assert.AreEqual(_requestId, sshDataStream.ReadUInt32());
  92. Assert.AreEqual((uint)_nameBytes.Length, sshDataStream.ReadUInt32());
  93. var actualNameBytes = new byte[_nameBytes.Length];
  94. _ = sshDataStream.Read(actualNameBytes, 0, actualNameBytes.Length);
  95. Assert.IsTrue(_nameBytes.SequenceEqual(actualNameBytes));
  96. Assert.AreEqual((uint)_pathBytes.Length, sshDataStream.ReadUInt32());
  97. var actualPath = new byte[_pathBytes.Length];
  98. _ = sshDataStream.Read(actualPath, 0, actualPath.Length);
  99. Assert.IsTrue(_pathBytes.SequenceEqual(actualPath));
  100. Assert.IsTrue(sshDataStream.IsEndOfData);
  101. }
  102. }
  103. }