2
0

SftpRmDirRequestTest.cs 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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
  12. {
  13. [TestClass]
  14. public class SftpRmDirRequestTest
  15. {
  16. private uint _protocolVersion;
  17. private uint _requestId;
  18. private Encoding _encoding;
  19. private string _path;
  20. private byte[] _pathBytes;
  21. [TestInitialize]
  22. public void Init()
  23. {
  24. var random = new Random();
  25. _protocolVersion = (uint)random.Next(0, int.MaxValue);
  26. _requestId = (uint)random.Next(0, int.MaxValue);
  27. _encoding = Encoding.Unicode;
  28. _path = random.Next().ToString(CultureInfo.InvariantCulture);
  29. _pathBytes = _encoding.GetBytes(_path);
  30. }
  31. [TestMethod]
  32. public void Constructor()
  33. {
  34. var request = new SftpRmDirRequest(_protocolVersion, _requestId, _path, _encoding, null);
  35. Assert.AreSame(_encoding, request.Encoding);
  36. Assert.AreEqual(_path, request.Path);
  37. Assert.AreEqual(_protocolVersion, request.ProtocolVersion);
  38. Assert.AreEqual(_requestId, request.RequestId);
  39. Assert.AreEqual(SftpMessageTypes.RmDir, request.SftpMessageType);
  40. }
  41. [TestMethod]
  42. public void Complete_SftpStatusResponse()
  43. {
  44. var statusActionInvocations = new List<SftpStatusResponse>();
  45. Action<SftpStatusResponse> statusAction = statusActionInvocations.Add;
  46. var statusResponse = new SftpStatusResponse(_protocolVersion);
  47. var request = new SftpRmDirRequest(_protocolVersion, _requestId, _path, _encoding, statusAction);
  48. request.Complete(statusResponse);
  49. Assert.AreEqual(1, statusActionInvocations.Count);
  50. Assert.AreSame(statusResponse, statusActionInvocations[0]);
  51. }
  52. [TestMethod]
  53. public void GetBytes()
  54. {
  55. var statusActionInvocations = new List<SftpStatusResponse>();
  56. var nameActionInvocations = new List<SftpNameResponse>();
  57. Action<SftpStatusResponse> statusAction = statusActionInvocations.Add;
  58. var request = new SftpRmDirRequest(_protocolVersion, _requestId, _path, _encoding, null);
  59. var bytes = request.GetBytes();
  60. var expectedBytesLength = 0;
  61. expectedBytesLength += 4; // Length
  62. expectedBytesLength += 1; // Type
  63. expectedBytesLength += 4; // RequestId
  64. expectedBytesLength += 4; // Path length
  65. expectedBytesLength += _pathBytes.Length; // Path
  66. Assert.AreEqual(expectedBytesLength, bytes.Length);
  67. var sshDataStream = new SshDataStream(bytes);
  68. Assert.AreEqual((uint)bytes.Length - 4, sshDataStream.ReadUInt32());
  69. Assert.AreEqual((byte)SftpMessageTypes.RmDir, sshDataStream.ReadByte());
  70. Assert.AreEqual(_requestId, sshDataStream.ReadUInt32());
  71. Assert.AreEqual((uint)_pathBytes.Length, sshDataStream.ReadUInt32());
  72. var actualPath = new byte[_pathBytes.Length];
  73. _ = sshDataStream.Read(actualPath, 0, actualPath.Length);
  74. Assert.IsTrue(_pathBytes.SequenceEqual(actualPath));
  75. Assert.IsTrue(sshDataStream.IsEndOfData);
  76. }
  77. }
  78. }