2
0

HardLinkRequestTest.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 HardLinkRequestTest
  15. {
  16. private uint _protocolVersion;
  17. private uint _requestId;
  18. private string _name;
  19. private string _oldPath;
  20. private byte[] _oldPathBytes;
  21. private string _newPath;
  22. private byte[] _newPathBytes;
  23. private byte[] _nameBytes;
  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. _oldPath = random.Next().ToString(CultureInfo.InvariantCulture);
  31. _oldPathBytes = Encoding.UTF8.GetBytes(_oldPath);
  32. _newPath = random.Next().ToString(CultureInfo.InvariantCulture);
  33. _newPathBytes = Encoding.UTF8.GetBytes(_newPath);
  34. _name = "hardlink@openssh.com";
  35. _nameBytes = Encoding.UTF8.GetBytes(_name);
  36. }
  37. [TestMethod]
  38. public void Constructor()
  39. {
  40. var request = new HardLinkRequest(_protocolVersion, _requestId, _oldPath, _newPath, null);
  41. Assert.AreEqual(_name, request.Name);
  42. Assert.AreEqual(_newPath, request.NewPath);
  43. Assert.AreEqual(_oldPath, request.OldPath);
  44. Assert.AreEqual(_protocolVersion, request.ProtocolVersion);
  45. Assert.AreEqual(_requestId, request.RequestId);
  46. Assert.AreEqual(SftpMessageTypes.Extended, request.SftpMessageType);
  47. }
  48. [TestMethod]
  49. public void Complete_SftpStatusResponse()
  50. {
  51. IList<SftpStatusResponse> statusActionInvocations = new List<SftpStatusResponse>();
  52. Action<SftpStatusResponse> statusAction = statusActionInvocations.Add;
  53. var statusResponse = new SftpStatusResponse(_protocolVersion);
  54. var request = new HardLinkRequest(_protocolVersion, _requestId, _oldPath, _newPath, statusAction);
  55. request.Complete(statusResponse);
  56. Assert.AreEqual(1, statusActionInvocations.Count);
  57. Assert.AreSame(statusResponse, statusActionInvocations[0]);
  58. }
  59. [TestMethod]
  60. public void GetBytes()
  61. {
  62. var request = new HardLinkRequest(_protocolVersion, _requestId, _oldPath, _newPath, null);
  63. var bytes = request.GetBytes();
  64. var expectedBytesLength = 0;
  65. expectedBytesLength += 4; // Length
  66. expectedBytesLength += 1; // Type
  67. expectedBytesLength += 4; // RequestId
  68. expectedBytesLength += 4; // Name length
  69. expectedBytesLength += _nameBytes.Length; // Name
  70. expectedBytesLength += 4; // OldPath length
  71. expectedBytesLength += _oldPathBytes.Length; // OldPath
  72. expectedBytesLength += 4; // NewPath length
  73. expectedBytesLength += _newPathBytes.Length; // NewPath
  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.Extended, sshDataStream.ReadByte());
  78. Assert.AreEqual(_requestId, sshDataStream.ReadUInt32());
  79. Assert.AreEqual((uint)_nameBytes.Length, sshDataStream.ReadUInt32());
  80. var actualNameBytes = new byte[_nameBytes.Length];
  81. _ = sshDataStream.Read(actualNameBytes, 0, actualNameBytes.Length);
  82. Assert.IsTrue(_nameBytes.SequenceEqual(actualNameBytes));
  83. Assert.AreEqual((uint)_oldPathBytes.Length, sshDataStream.ReadUInt32());
  84. var actualOldPath = new byte[_oldPathBytes.Length];
  85. _ = sshDataStream.Read(actualOldPath, 0, actualOldPath.Length);
  86. Assert.IsTrue(_oldPathBytes.SequenceEqual(actualOldPath));
  87. Assert.AreEqual((uint)_newPathBytes.Length, sshDataStream.ReadUInt32());
  88. var actualNewPath = new byte[_newPathBytes.Length];
  89. _ = sshDataStream.Read(actualNewPath, 0, actualNewPath.Length);
  90. Assert.IsTrue(_newPathBytes.SequenceEqual(actualNewPath));
  91. Assert.IsTrue(sshDataStream.IsEndOfData);
  92. }
  93. }
  94. }