SftpAttrsResponseTest.cs 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. #if TUNING
  38. var sshDataStream = new SshDataStream(4 + 1 + 4 + attributesBytes.Length);
  39. sshDataStream.Position = 4; // skip 4 bytes for SSH packet length
  40. #else
  41. var sshDataStream = new SshDataStream(1 + 4 + attributesBytes.Length);
  42. #endif
  43. sshDataStream.WriteByte((byte)SftpMessageTypes.Attrs);
  44. sshDataStream.Write(_responseId);
  45. sshDataStream.Write(attributesBytes, 0, attributesBytes.Length);
  46. target.Load(sshDataStream.ToArray());
  47. Assert.IsNotNull(target.Attributes);
  48. Assert.AreEqual(_protocolVersion, target.ProtocolVersion);
  49. Assert.AreEqual(_responseId, target.ResponseId);
  50. Assert.AreEqual(SftpMessageTypes.Attrs, target.SftpMessageType);
  51. // check attributes in detail
  52. Assert.AreEqual(attributes.GroupId, target.Attributes.GroupId);
  53. Assert.AreEqual(attributes.LastWriteTime, target.Attributes.LastWriteTime);
  54. Assert.AreEqual(attributes.LastWriteTime, target.Attributes.LastWriteTime);
  55. Assert.AreEqual(attributes.UserId, target.Attributes.UserId);
  56. }
  57. private SftpFileAttributes CreateSftpFileAttributes()
  58. {
  59. var attributes = SftpFileAttributes.Empty;
  60. attributes.GroupId = _random.Next();
  61. attributes.LastAccessTime = new DateTime(2014, 8, 23, 17, 43, 50, DateTimeKind.Local);
  62. attributes.LastWriteTime = new DateTime(2013, 7, 22, 16, 40, 42, DateTimeKind.Local);
  63. attributes.UserId = _random.Next();
  64. return attributes;
  65. }
  66. }
  67. }