2
0

SftpRenameRequestTest.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 SftpRenameRequestTest : TestBase
  16. {
  17. private uint _protocolVersion;
  18. private uint _requestId;
  19. private Encoding _encoding;
  20. private string _oldPath;
  21. private byte[] _oldPathBytes;
  22. private string _newPath;
  23. private byte[] _newPathBytes;
  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. _oldPath = random.Next().ToString(CultureInfo.InvariantCulture);
  31. _oldPathBytes = _encoding.GetBytes(_oldPath);
  32. _newPath = random.Next().ToString(CultureInfo.InvariantCulture);
  33. _newPathBytes = _encoding.GetBytes(_newPath);
  34. }
  35. [TestMethod]
  36. public void Constructor()
  37. {
  38. var request = new SftpRenameRequest(_protocolVersion, _requestId, _oldPath, _newPath, _encoding, null);
  39. Assert.AreSame(_encoding, request.Encoding);
  40. Assert.AreEqual(_newPath, request.NewPath);
  41. Assert.AreEqual(_oldPath, request.OldPath);
  42. Assert.AreEqual(_protocolVersion, request.ProtocolVersion);
  43. Assert.AreEqual(_requestId, request.RequestId);
  44. Assert.AreEqual(SftpMessageTypes.Rename, request.SftpMessageType);
  45. }
  46. [TestMethod]
  47. public void Complete_SftpStatusResponse()
  48. {
  49. var statusActionInvocations = new List<SftpStatusResponse>();
  50. Action<SftpStatusResponse> statusAction = statusActionInvocations.Add;
  51. var statusResponse = new SftpStatusResponse(_protocolVersion);
  52. var request = new SftpRenameRequest(_protocolVersion, _requestId, _oldPath, _newPath, _encoding, statusAction);
  53. request.Complete(statusResponse);
  54. Assert.AreEqual(1, statusActionInvocations.Count);
  55. Assert.AreSame(statusResponse, statusActionInvocations[0]);
  56. }
  57. [TestMethod]
  58. public void GetBytes()
  59. {
  60. var request = new SftpRenameRequest(_protocolVersion, _requestId, _oldPath, _newPath, _encoding, null);
  61. var bytes = request.GetBytes();
  62. var expectedBytesLength = 0;
  63. expectedBytesLength += 4; // Length
  64. expectedBytesLength += 1; // Type
  65. expectedBytesLength += 4; // RequestId
  66. expectedBytesLength += 4; // OldPath length
  67. expectedBytesLength += _oldPathBytes.Length; // OldPath
  68. expectedBytesLength += 4; // NewPath length
  69. expectedBytesLength += _newPathBytes.Length; // NewPath
  70. Assert.AreEqual(expectedBytesLength, bytes.Length);
  71. var sshDataStream = new SshDataStream(bytes);
  72. Assert.AreEqual((uint)bytes.Length - 4, sshDataStream.ReadUInt32());
  73. Assert.AreEqual((byte)SftpMessageTypes.Rename, sshDataStream.ReadByte());
  74. Assert.AreEqual(_requestId, sshDataStream.ReadUInt32());
  75. Assert.AreEqual((uint)_oldPathBytes.Length, sshDataStream.ReadUInt32());
  76. var actualOldPath = new byte[_oldPathBytes.Length];
  77. _ = sshDataStream.Read(actualOldPath, 0, actualOldPath.Length);
  78. Assert.IsTrue(_oldPathBytes.SequenceEqual(actualOldPath));
  79. Assert.AreEqual((uint)_newPathBytes.Length, sshDataStream.ReadUInt32());
  80. var actualNewPath = new byte[_newPathBytes.Length];
  81. _ = sshDataStream.Read(actualNewPath, 0, actualNewPath.Length);
  82. Assert.IsTrue(_newPathBytes.SequenceEqual(actualNewPath));
  83. Assert.IsTrue(sshDataStream.IsEndOfData);
  84. }
  85. }
  86. }