2
0

SftpSymLinkRequestTest.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 SftpSymLinkRequestTest : TestBase
  16. {
  17. private uint _protocolVersion;
  18. private uint _requestId;
  19. private Encoding _encoding;
  20. private string _newLinkPath;
  21. private byte[] _newLinkPathBytes;
  22. private string _existingPath;
  23. private byte[] _existingPathBytes;
  24. protected override void OnInit()
  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. _newLinkPath = random.Next().ToString(CultureInfo.InvariantCulture);
  31. _newLinkPathBytes = _encoding.GetBytes(_newLinkPath);
  32. _existingPath = random.Next().ToString(CultureInfo.InvariantCulture);
  33. _existingPathBytes = _encoding.GetBytes(_existingPath);
  34. }
  35. [TestMethod]
  36. public void Constructor()
  37. {
  38. var request = new SftpSymLinkRequest(
  39. _protocolVersion,
  40. _requestId,
  41. _newLinkPath,
  42. _existingPath,
  43. _encoding,
  44. null);
  45. Assert.AreSame(_encoding, request.Encoding);
  46. Assert.AreEqual(_existingPath, request.ExistingPath);
  47. Assert.AreEqual(_newLinkPath, request.NewLinkPath);
  48. Assert.AreEqual(_protocolVersion, request.ProtocolVersion);
  49. Assert.AreEqual(_requestId, request.RequestId);
  50. Assert.AreEqual(SftpMessageTypes.SymLink, request.SftpMessageType);
  51. }
  52. [TestMethod]
  53. public void Complete_SftpStatusResponse()
  54. {
  55. var statusActionInvocations = new List<SftpStatusResponse>();
  56. Action<SftpStatusResponse> statusAction = statusActionInvocations.Add;
  57. var statusResponse = new SftpStatusResponse(_protocolVersion);
  58. var request = new SftpSymLinkRequest(
  59. _protocolVersion,
  60. _requestId,
  61. _newLinkPath,
  62. _existingPath,
  63. _encoding,
  64. statusAction);
  65. request.Complete(statusResponse);
  66. Assert.AreEqual(1, statusActionInvocations.Count);
  67. Assert.AreSame(statusResponse, statusActionInvocations[0]);
  68. }
  69. [TestMethod]
  70. public void GetBytes()
  71. {
  72. var request = new SftpSymLinkRequest(
  73. _protocolVersion,
  74. _requestId,
  75. _newLinkPath,
  76. _existingPath,
  77. _encoding,
  78. null);
  79. var bytes = request.GetBytes();
  80. var expectedBytesLength = 0;
  81. expectedBytesLength += 4; // Length
  82. expectedBytesLength += 1; // Type
  83. expectedBytesLength += 4; // RequestId
  84. expectedBytesLength += 4; // NewLinkPath length
  85. expectedBytesLength += _newLinkPathBytes.Length; // NewLinkPath
  86. expectedBytesLength += 4; // ExistingPath length
  87. expectedBytesLength += _existingPathBytes.Length; // ExistingPath
  88. Assert.AreEqual(expectedBytesLength, bytes.Length);
  89. var sshDataStream = new SshDataStream(bytes);
  90. Assert.AreEqual((uint)bytes.Length - 4, sshDataStream.ReadUInt32());
  91. Assert.AreEqual((byte)SftpMessageTypes.SymLink, sshDataStream.ReadByte());
  92. Assert.AreEqual(_requestId, sshDataStream.ReadUInt32());
  93. Assert.AreEqual((uint)_newLinkPathBytes.Length, sshDataStream.ReadUInt32());
  94. var actualNewLinkPath = new byte[_newLinkPathBytes.Length];
  95. _ = sshDataStream.Read(actualNewLinkPath, 0, actualNewLinkPath.Length);
  96. Assert.IsTrue(_newLinkPathBytes.SequenceEqual(actualNewLinkPath));
  97. Assert.AreEqual((uint)_existingPathBytes.Length, sshDataStream.ReadUInt32());
  98. var actualExistingPath = new byte[_existingPathBytes.Length];
  99. _ = sshDataStream.Read(actualExistingPath, 0, actualExistingPath.Length);
  100. Assert.IsTrue(_existingPathBytes.SequenceEqual(actualExistingPath));
  101. Assert.IsTrue(sshDataStream.IsEndOfData);
  102. }
  103. }
  104. }