From 9ecfa0d38cddba51dcefb191094892cd4887c1eb Mon Sep 17 00:00:00 2001 From: Gert Driesen Date: Sun, 6 Aug 2017 18:22:44 +0200 Subject: [PATCH] Refactor loading of a response to an extended request (SSH_FXP_EXTENDED): Continue reading from SshDataStream instead of creating new instance for type specific part. --- .../Classes/Common/SshDataTest.cs | 60 ------------------- .../ExtendedReplies/StatVfsReplyInfoTest.cs | 9 ++- src/Renci.SshNet/Common/SshData.cs | 34 ++--------- .../ExtendedReplies/ExtendedReplyInfo.cs | 14 +---- .../ExtendedReplies/StatVfsReplyInfo.cs | 36 +++++------ .../Responses/SftpExtendedReplyResponse.cs | 10 ++-- 6 files changed, 34 insertions(+), 129 deletions(-) diff --git a/src/Renci.SshNet.Tests/Classes/Common/SshDataTest.cs b/src/Renci.SshNet.Tests/Classes/Common/SshDataTest.cs index 330ff50f..62991093 100644 --- a/src/Renci.SshNet.Tests/Classes/Common/SshDataTest.cs +++ b/src/Renci.SshNet.Tests/Classes/Common/SshDataTest.cs @@ -86,66 +86,6 @@ namespace Renci.SshNet.Tests.Classes.Common Assert.AreEqual(two, request.ValueTwo); } - - [TestMethod] - public void OfType() - { - const uint one = 123456u; - const uint two = 456789u; - - var sshDataStream = new SshDataStream(8); - sshDataStream.Write(one); - sshDataStream.Write(two); - - var sshData = sshDataStream.ToArray(); - - var request = new RequestSshData(); - request.Load(sshData); - - var reply = request.OfType(); - Assert.IsNotNull(reply); - Assert.AreEqual(one, reply.ValueOne); - } - - [TestMethod] - public void OfType_LoadWithOffset() - { - const uint one = 123456u; - const uint two = 456789u; - - var sshDataStream = new SshDataStream(11); - sshDataStream.WriteByte(0x05); - sshDataStream.WriteByte(0x07); - sshDataStream.WriteByte(0x0f); - sshDataStream.Write(one); - sshDataStream.Write(two); - - var sshData = sshDataStream.ToArray(); - - var request = new RequestSshData(); - request.Load(sshData, 3, sshData.Length - 3); - var reply = request.OfType(); - Assert.IsNotNull(reply); - Assert.AreEqual(one, reply.ValueOne); - } - - [TestMethod] - public void OfType_ShouldThrowArgumentNullExceptionWhenNoDataIsLoaded() - { - var request = new RequestSshData(); - - try - { - request.OfType(); - Assert.Fail(); - } - catch (ArgumentNullException ex) - { - Assert.IsNull(ex.InnerException); - Assert.AreEqual("data", ex.ParamName); - } - } - private class BoolSshData : SshData { private readonly bool _value; diff --git a/src/Renci.SshNet.Tests/Classes/Sftp/Responses/ExtendedReplies/StatVfsReplyInfoTest.cs b/src/Renci.SshNet.Tests/Classes/Sftp/Responses/ExtendedReplies/StatVfsReplyInfoTest.cs index b1cc9a4e..d2b4f6c8 100644 --- a/src/Renci.SshNet.Tests/Classes/Sftp/Responses/ExtendedReplies/StatVfsReplyInfoTest.cs +++ b/src/Renci.SshNet.Tests/Classes/Sftp/Responses/ExtendedReplies/StatVfsReplyInfoTest.cs @@ -50,8 +50,6 @@ namespace Renci.SshNet.Tests.Classes.Sftp.Responses [TestMethod] public void Load() { - var target = new StatVfsReplyInfo(); - var sshDataStream = new SshDataStream(4 + 1 + 4 + 88); sshDataStream.Write(_responseId); sshDataStream.Write(_bsize); @@ -66,7 +64,12 @@ namespace Renci.SshNet.Tests.Classes.Sftp.Responses sshDataStream.Write((ulong) 0x1); sshDataStream.Write(_namemax); - target.Load(sshDataStream.ToArray()); + var extendedReplyResponse = new SftpExtendedReplyResponse(SftpSession.MaximumSupportedVersion); + extendedReplyResponse.Load(sshDataStream.ToArray()); + + Assert.AreEqual(_responseId, extendedReplyResponse.ResponseId); + + var target = extendedReplyResponse.GetReply(); Assert.IsNotNull(target.Information); diff --git a/src/Renci.SshNet/Common/SshData.cs b/src/Renci.SshNet/Common/SshData.cs index 63cfcb23..8e4eca40 100644 --- a/src/Renci.SshNet/Common/SshData.cs +++ b/src/Renci.SshNet/Common/SshData.cs @@ -45,10 +45,6 @@ namespace Renci.SshNet.Common } } - private byte[] _loadedData; - private int _offset; - private int _count; - /// /// Gets the size of the message in bytes. /// @@ -61,9 +57,11 @@ namespace Renci.SshNet.Common } /// - /// Gets data bytes array + /// Gets data bytes array. /// - /// Byte array representation of data structure. + /// + /// A array representation of data structure. + /// public byte[] GetBytes() { var messageLength = BufferCapacity; @@ -83,13 +81,6 @@ namespace Renci.SshNet.Common SaveData(); } - internal T OfType() where T : SshData, new() - { - var result = new T(); - result.Load(_loadedData, _offset, _count); - return result; - } - /// /// Loads data from specified bytes. /// @@ -120,7 +111,7 @@ namespace Renci.SshNet.Common private void LoadInternal(byte[] value, int offset, int count) { - LoadBytes(value, offset, count); + _stream = new SshDataStream(value, offset, count); LoadData(); } @@ -134,21 +125,6 @@ namespace Renci.SshNet.Common /// protected abstract void SaveData(); - /// - /// Loads data bytes into internal buffer. - /// - /// The bytes. - /// The zero-based offset in at which to begin reading SSH data. - /// The number of bytes to load. - private void LoadBytes(byte[] bytes, int offset, int count) - { - _loadedData = bytes; - _offset = offset; - _count = count; - - _stream = new SshDataStream(bytes, _offset, count); - } - /// /// Reads all data left in internal buffer at current position. /// diff --git a/src/Renci.SshNet/Sftp/Responses/ExtendedReplies/ExtendedReplyInfo.cs b/src/Renci.SshNet/Sftp/Responses/ExtendedReplies/ExtendedReplyInfo.cs index a705fc42..3f98a986 100644 --- a/src/Renci.SshNet/Sftp/Responses/ExtendedReplies/ExtendedReplyInfo.cs +++ b/src/Renci.SshNet/Sftp/Responses/ExtendedReplies/ExtendedReplyInfo.cs @@ -1,19 +1,9 @@ using Renci.SshNet.Common; -using System; namespace Renci.SshNet.Sftp.Responses { - internal abstract class ExtendedReplyInfo : SshData + internal abstract class ExtendedReplyInfo { - protected override void LoadData() - { - // skip response id - ReadUInt32(); - } - - protected override void SaveData() - { - throw new NotImplementedException(); - } + public abstract void LoadData(SshDataStream stream); } } diff --git a/src/Renci.SshNet/Sftp/Responses/ExtendedReplies/StatVfsReplyInfo.cs b/src/Renci.SshNet/Sftp/Responses/ExtendedReplies/StatVfsReplyInfo.cs index a8773190..71c45dc1 100644 --- a/src/Renci.SshNet/Sftp/Responses/ExtendedReplies/StatVfsReplyInfo.cs +++ b/src/Renci.SshNet/Sftp/Responses/ExtendedReplies/StatVfsReplyInfo.cs @@ -1,29 +1,25 @@ -namespace Renci.SshNet.Sftp.Responses +using Renci.SshNet.Common; + +namespace Renci.SshNet.Sftp.Responses { internal class StatVfsReplyInfo : ExtendedReplyInfo { public SftpFileSytemInformation Information { get; private set; } - protected override void LoadData() + public override void LoadData(SshDataStream stream) { - base.LoadData(); - - Information = new SftpFileSytemInformation(ReadUInt64(), - ReadUInt64(), - ReadUInt64(), - ReadUInt64(), - ReadUInt64(), - ReadUInt64(), - ReadUInt64(), - ReadUInt64(), - ReadUInt64(), - ReadUInt64(), - ReadUInt64()); - } - - protected override void SaveData() - { - throw new System.NotImplementedException(); + Information = new SftpFileSytemInformation(stream.ReadUInt64(), // FileSystemBlockSize + stream.ReadUInt64(), // BlockSize + stream.ReadUInt64(), // TotalBlocks + stream.ReadUInt64(), // FreeBlocks + stream.ReadUInt64(), // AvailableBlocks + stream.ReadUInt64(), // TotalNodes + stream.ReadUInt64(), // FreeNodes + stream.ReadUInt64(), // AvailableNodes + stream.ReadUInt64(), // Sid + stream.ReadUInt64(), // Flags + stream.ReadUInt64() // MaxNameLenght + ); } } } \ No newline at end of file diff --git a/src/Renci.SshNet/Sftp/Responses/SftpExtendedReplyResponse.cs b/src/Renci.SshNet/Sftp/Responses/SftpExtendedReplyResponse.cs index 86b6a662..1a7048ec 100644 --- a/src/Renci.SshNet/Sftp/Responses/SftpExtendedReplyResponse.cs +++ b/src/Renci.SshNet/Sftp/Responses/SftpExtendedReplyResponse.cs @@ -1,6 +1,4 @@ -using Renci.SshNet.Common; - -namespace Renci.SshNet.Sftp.Responses +namespace Renci.SshNet.Sftp.Responses { internal class SftpExtendedReplyResponse : SftpResponse { @@ -14,9 +12,11 @@ namespace Renci.SshNet.Sftp.Responses { } - public T GetReply() where T : SshData, new() + public T GetReply() where T : ExtendedReplyInfo, new() { - return OfType(); + var result = new T(); + result.LoadData(DataStream); + return result; } } }