SftpRmDirRequestTest.cs 3.6 KB

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