SftpMkDirRequestTest.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 SftpMkDirRequestTest
  16. {
  17. private uint _protocolVersion;
  18. private uint _requestId;
  19. private Encoding _encoding;
  20. private string _path;
  21. private byte[] _pathBytes;
  22. private SftpFileAttributes _attributes;
  23. private byte[] _attributesBytes;
  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. _path = random.Next().ToString(CultureInfo.InvariantCulture);
  32. _pathBytes = _encoding.GetBytes(_path);
  33. _attributes = SftpFileAttributesBuilder.Empty;
  34. _attributesBytes = _attributes.GetBytes();
  35. }
  36. [TestMethod]
  37. public void Constructor()
  38. {
  39. var request = new SftpMkDirRequest(_protocolVersion, _requestId, _path, _encoding, null);
  40. Assert.AreSame(_encoding, request.Encoding);
  41. Assert.AreEqual(_path, request.Path);
  42. Assert.AreEqual(_protocolVersion, request.ProtocolVersion);
  43. Assert.AreEqual(_requestId, request.RequestId);
  44. Assert.AreEqual(SftpMessageTypes.MkDir, request.SftpMessageType);
  45. }
  46. [TestMethod]
  47. public void Complete_SftpStatusResponse()
  48. {
  49. var statusActionInvocations = new List<SftpStatusResponse>();
  50. Action<SftpStatusResponse> statusAction = statusActionInvocations.Add;
  51. var statusResponse = new SftpStatusResponse(_protocolVersion);
  52. var request = new SftpMkDirRequest(_protocolVersion, _requestId, _path, _encoding, statusAction);
  53. request.Complete(statusResponse);
  54. Assert.AreEqual(1, statusActionInvocations.Count);
  55. Assert.AreSame(statusResponse, statusActionInvocations[0]);
  56. }
  57. [TestMethod]
  58. public void GetBytes()
  59. {
  60. var request = new SftpMkDirRequest(_protocolVersion, _requestId, _path, _encoding, null);
  61. var bytes = request.GetBytes();
  62. var expectedBytesLength = 0;
  63. expectedBytesLength += 4; // Length
  64. expectedBytesLength += 1; // Type
  65. expectedBytesLength += 4; // RequestId
  66. expectedBytesLength += 4; // Path length
  67. expectedBytesLength += _pathBytes.Length; // Path
  68. expectedBytesLength += _attributesBytes.Length; // Attributes
  69. Assert.AreEqual(expectedBytesLength, bytes.Length);
  70. var sshDataStream = new SshDataStream(bytes);
  71. Assert.AreEqual((uint)bytes.Length - 4, sshDataStream.ReadUInt32());
  72. Assert.AreEqual((byte)SftpMessageTypes.MkDir, sshDataStream.ReadByte());
  73. Assert.AreEqual(_requestId, sshDataStream.ReadUInt32());
  74. Assert.AreEqual((uint)_pathBytes.Length, sshDataStream.ReadUInt32());
  75. var actualPath = new byte[_pathBytes.Length];
  76. _ = sshDataStream.Read(actualPath, 0, actualPath.Length);
  77. Assert.IsTrue(_pathBytes.SequenceEqual(actualPath));
  78. var actualAttributes = new byte[_attributesBytes.Length];
  79. _ = sshDataStream.Read(actualAttributes, 0, actualAttributes.Length);
  80. Assert.IsTrue(_attributesBytes.SequenceEqual(actualAttributes));
  81. Assert.IsTrue(sshDataStream.IsEndOfData);
  82. }
  83. }
  84. }