SftpRemoveRequestTest.cs 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 SftpRemoveRequestTest
  15. {
  16. private uint _protocolVersion;
  17. private uint _requestId;
  18. private Encoding _encoding;
  19. private string _filename;
  20. private byte[] _filenameBytes;
  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. _filename = random.Next().ToString(CultureInfo.InvariantCulture);
  29. _filenameBytes = _encoding.GetBytes(_filename);
  30. }
  31. [TestMethod]
  32. public void Constructor()
  33. {
  34. var request = new SftpRemoveRequest(_protocolVersion, _requestId, _filename, _encoding, null);
  35. Assert.AreSame(_encoding, request.Encoding);
  36. Assert.AreEqual(_filename, request.Filename);
  37. Assert.AreEqual(_protocolVersion, request.ProtocolVersion);
  38. Assert.AreEqual(_requestId, request.RequestId);
  39. Assert.AreEqual(SftpMessageTypes.Remove, 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 SftpRemoveRequest(_protocolVersion, _requestId, _filename, _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 request = new SftpRemoveRequest(_protocolVersion, _requestId, _filename, _encoding, null);
  56. var bytes = request.GetBytes();
  57. var expectedBytesLength = 0;
  58. expectedBytesLength += 4; // Length
  59. expectedBytesLength += 1; // Type
  60. expectedBytesLength += 4; // RequestId
  61. expectedBytesLength += 4; // Filename length
  62. expectedBytesLength += _filenameBytes.Length; // Filename
  63. Assert.AreEqual(expectedBytesLength, bytes.Length);
  64. var sshDataStream = new SshDataStream(bytes);
  65. Assert.AreEqual((uint)bytes.Length - 4, sshDataStream.ReadUInt32());
  66. Assert.AreEqual((byte)SftpMessageTypes.Remove, sshDataStream.ReadByte());
  67. Assert.AreEqual(_requestId, sshDataStream.ReadUInt32());
  68. Assert.AreEqual((uint)_filenameBytes.Length, sshDataStream.ReadUInt32());
  69. var actualFilename = new byte[_filenameBytes.Length];
  70. _ = sshDataStream.Read(actualFilename, 0, actualFilename.Length);
  71. Assert.IsTrue(_filenameBytes.SequenceEqual(actualFilename));
  72. Assert.IsTrue(sshDataStream.IsEndOfData);
  73. }
  74. }
  75. }