SftpAttrsResponseTest.cs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. using System;
  2. using Microsoft.VisualStudio.TestTools.UnitTesting;
  3. using Renci.SshNet.Common;
  4. using Renci.SshNet.Sftp;
  5. using Renci.SshNet.Sftp.Responses;
  6. using Renci.SshNet.Tests.Common;
  7. namespace Renci.SshNet.Tests.Classes.Sftp.Responses
  8. {
  9. [TestClass]
  10. public class SftpAttrsResponseTest
  11. {
  12. private Random _random;
  13. private uint _protocolVersion;
  14. private uint _responseId;
  15. [TestInitialize]
  16. public void Init()
  17. {
  18. _random = new Random();
  19. _protocolVersion = (uint)_random.Next(0, int.MaxValue);
  20. _responseId = (uint)_random.Next(0, int.MaxValue);
  21. }
  22. [TestMethod]
  23. public void Constructor()
  24. {
  25. var target = new SftpAttrsResponse(_protocolVersion);
  26. Assert.IsNull(target.Attributes);
  27. Assert.AreEqual(_protocolVersion, target.ProtocolVersion);
  28. Assert.AreEqual((uint)0, target.ResponseId);
  29. Assert.AreEqual(SftpMessageTypes.Attrs, target.SftpMessageType);
  30. }
  31. [TestMethod]
  32. public void Load()
  33. {
  34. var target = new SftpAttrsResponse(_protocolVersion);
  35. var attributes = CreateSftpFileAttributes();
  36. var attributesBytes = attributes.GetBytes();
  37. var sshDataStream = new SshDataStream(4 + attributesBytes.Length);
  38. sshDataStream.Write(_responseId);
  39. sshDataStream.Write(attributesBytes, 0, attributesBytes.Length);
  40. target.Load(sshDataStream.ToArray());
  41. Assert.IsNotNull(target.Attributes);
  42. Assert.AreEqual(_protocolVersion, target.ProtocolVersion);
  43. Assert.AreEqual(_responseId, target.ResponseId);
  44. Assert.AreEqual(SftpMessageTypes.Attrs, target.SftpMessageType);
  45. // check attributes in detail
  46. Assert.AreEqual(attributes.GroupId, target.Attributes.GroupId);
  47. Assert.AreEqual(attributes.LastWriteTime, target.Attributes.LastWriteTime);
  48. Assert.AreEqual(attributes.LastWriteTime, target.Attributes.LastWriteTime);
  49. Assert.AreEqual(attributes.UserId, target.Attributes.UserId);
  50. }
  51. private SftpFileAttributes CreateSftpFileAttributes()
  52. {
  53. var attributes = SftpFileAttributesBuilder.Empty;
  54. attributes.GroupId = _random.Next();
  55. attributes.LastAccessTime = new DateTime(2014, 8, 23, 17, 43, 50, DateTimeKind.Local);
  56. attributes.LastWriteTime = new DateTime(2013, 7, 22, 16, 40, 42, DateTimeKind.Local);
  57. attributes.UserId = _random.Next();
  58. return attributes;
  59. }
  60. }
  61. }