SftpRenameRequestTest.cs 4.2 KB

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