2
0

SftpReadLinkRequestTest.cs 4.5 KB

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