2
0

PosixRenameRequestTest.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 PosixRenameRequestTest
  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. private Encoding _encoding;
  25. [TestInitialize]
  26. public void Init()
  27. {
  28. var random = new Random();
  29. _encoding = Encoding.Unicode;
  30. _protocolVersion = (uint)random.Next(0, int.MaxValue);
  31. _requestId = (uint)random.Next(0, int.MaxValue);
  32. _oldPath = random.Next().ToString(CultureInfo.InvariantCulture);
  33. _oldPathBytes = _encoding.GetBytes(_oldPath);
  34. _newPath = random.Next().ToString(CultureInfo.InvariantCulture);
  35. _newPathBytes = _encoding.GetBytes(_newPath);
  36. _name = "posix-rename@openssh.com";
  37. _nameBytes = Encoding.UTF8.GetBytes(_name);
  38. }
  39. [TestMethod]
  40. public void Constructor()
  41. {
  42. var request = new PosixRenameRequest(_protocolVersion, _requestId, _oldPath, _newPath, _encoding, null);
  43. Assert.AreSame(_encoding, request.Encoding);
  44. Assert.AreEqual(_name, request.Name);
  45. Assert.AreEqual(_newPath, request.NewPath);
  46. Assert.AreEqual(_oldPath, request.OldPath);
  47. Assert.AreEqual(_protocolVersion, request.ProtocolVersion);
  48. Assert.AreEqual(_requestId, request.RequestId);
  49. Assert.AreEqual(SftpMessageTypes.Extended, request.SftpMessageType);
  50. }
  51. [TestMethod]
  52. public void Complete_SftpStatusResponse()
  53. {
  54. IList<SftpStatusResponse> statusActionInvocations = new List<SftpStatusResponse>();
  55. Action<SftpStatusResponse> statusAction = statusActionInvocations.Add;
  56. var statusResponse = new SftpStatusResponse(_protocolVersion);
  57. var request = new PosixRenameRequest(_protocolVersion, _requestId, _oldPath, _newPath, _encoding, statusAction);
  58. request.Complete(statusResponse);
  59. Assert.AreEqual(1, statusActionInvocations.Count);
  60. Assert.AreSame(statusResponse, statusActionInvocations[0]);
  61. }
  62. [TestMethod]
  63. public void GetBytes()
  64. {
  65. var request = new PosixRenameRequest(_protocolVersion, _requestId, _oldPath, _newPath, _encoding, null);
  66. var bytes = request.GetBytes();
  67. var expectedBytesLength = 0;
  68. expectedBytesLength += 4; // Length
  69. expectedBytesLength += 1; // Type
  70. expectedBytesLength += 4; // RequestId
  71. expectedBytesLength += 4; // Name length
  72. expectedBytesLength += _nameBytes.Length; // Name
  73. expectedBytesLength += 4; // OldPath length
  74. expectedBytesLength += _oldPathBytes.Length; // OldPath
  75. expectedBytesLength += 4; // NewPath length
  76. expectedBytesLength += _newPathBytes.Length; // NewPath
  77. Assert.AreEqual(expectedBytesLength, bytes.Length);
  78. var sshDataStream = new SshDataStream(bytes);
  79. Assert.AreEqual((uint)bytes.Length - 4, sshDataStream.ReadUInt32());
  80. Assert.AreEqual((byte)SftpMessageTypes.Extended, sshDataStream.ReadByte());
  81. Assert.AreEqual(_requestId, sshDataStream.ReadUInt32());
  82. Assert.AreEqual((uint)_nameBytes.Length, sshDataStream.ReadUInt32());
  83. var actualNameBytes = new byte[_nameBytes.Length];
  84. _ = sshDataStream.Read(actualNameBytes, 0, actualNameBytes.Length);
  85. Assert.IsTrue(_nameBytes.SequenceEqual(actualNameBytes));
  86. Assert.AreEqual((uint)_oldPathBytes.Length, sshDataStream.ReadUInt32());
  87. var actualOldPath = new byte[_oldPathBytes.Length];
  88. _ = sshDataStream.Read(actualOldPath, 0, actualOldPath.Length);
  89. Assert.IsTrue(_oldPathBytes.SequenceEqual(actualOldPath));
  90. Assert.AreEqual((uint)_newPathBytes.Length, sshDataStream.ReadUInt32());
  91. var actualNewPath = new byte[_newPathBytes.Length];
  92. _ = sshDataStream.Read(actualNewPath, 0, actualNewPath.Length);
  93. Assert.IsTrue(_newPathBytes.SequenceEqual(actualNewPath));
  94. Assert.IsTrue(sshDataStream.IsEndOfData);
  95. }
  96. }
  97. }