mirror of
https://github.com/sshnet/SSH.NET.git
synced 2026-09-11 01:36:39 +00:00
Fix ChangeDirectory method
Fix GetCanonicalPath method Add tests for ChangeDirectory
This commit is contained in:
@@ -63,6 +63,7 @@
|
||||
<Compile Include="Security\TestHostKey.cs" />
|
||||
<Compile Include="Security\TestKeyExchange.cs" />
|
||||
<Compile Include="Security\TestPrivateKeyFile.cs" />
|
||||
<Compile Include="SftpClientTests\ChangeDirectory.cs" />
|
||||
<Compile Include="SftpClientTests\CreateDirectoryTest.cs" />
|
||||
<Compile Include="SftpClientTests\DeleteDirectoryTest.cs" />
|
||||
<Compile Include="SftpClientTests\DeleteFileTest.cs" />
|
||||
|
||||
@@ -0,0 +1,97 @@
|
||||
using System;
|
||||
using System.Text;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using Renci.SshNet.Tests.Properties;
|
||||
using Renci.SshNet.Common;
|
||||
|
||||
namespace Renci.SshNet.Tests.SftpClientTests
|
||||
{
|
||||
[TestClass]
|
||||
public class ChangeDirectory
|
||||
{
|
||||
[TestInitialize()]
|
||||
public void CleanCurrentFolder()
|
||||
{
|
||||
using (var client = new SshClient(Resources.HOST, Resources.USERNAME, Resources.PASSWORD))
|
||||
{
|
||||
client.Connect();
|
||||
client.RunCommand("rm -rf *");
|
||||
client.Disconnect();
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
[TestCategory("Sftp")]
|
||||
[ExpectedException(typeof(SshFileNotFoundException))]
|
||||
public void Test_Sftp_ChangeDirectory_Root_Dont_Exists()
|
||||
{
|
||||
using (var sftp = new SftpClient(Resources.HOST, Resources.USERNAME, Resources.PASSWORD))
|
||||
{
|
||||
sftp.Connect();
|
||||
sftp.ChangeDirectory("/asdasd");
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
[TestCategory("Sftp")]
|
||||
[ExpectedException(typeof(SshFileNotFoundException))]
|
||||
public void Test_Sftp_ChangeDirectory_Root_With_Slash_Dont_Exists()
|
||||
{
|
||||
using (var sftp = new SftpClient(Resources.HOST, Resources.USERNAME, Resources.PASSWORD))
|
||||
{
|
||||
sftp.Connect();
|
||||
sftp.ChangeDirectory("/asdasd/");
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
[TestCategory("Sftp")]
|
||||
[ExpectedException(typeof(SshFileNotFoundException))]
|
||||
public void Test_Sftp_ChangeDirectory_Subfolder_Dont_Exists()
|
||||
{
|
||||
using (var sftp = new SftpClient(Resources.HOST, Resources.USERNAME, Resources.PASSWORD))
|
||||
{
|
||||
sftp.Connect();
|
||||
sftp.ChangeDirectory("/asdasd/sssddds");
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
[TestCategory("Sftp")]
|
||||
[ExpectedException(typeof(SshFileNotFoundException))]
|
||||
public void Test_Sftp_ChangeDirectory_Subfolder_With_Slash_Dont_Exists()
|
||||
{
|
||||
using (var sftp = new SftpClient(Resources.HOST, Resources.USERNAME, Resources.PASSWORD))
|
||||
{
|
||||
sftp.Connect();
|
||||
sftp.ChangeDirectory("/asdasd/sssddds/");
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
[TestCategory("Sftp")]
|
||||
public void Test_Sftp_ChangeDirectory_Which_Exists()
|
||||
{
|
||||
using (var sftp = new SftpClient(Resources.HOST, Resources.USERNAME, Resources.PASSWORD))
|
||||
{
|
||||
sftp.Connect();
|
||||
sftp.ChangeDirectory("/usr");
|
||||
Assert.AreEqual("/usr", sftp.WorkingDirectory);
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
[TestCategory("Sftp")]
|
||||
public void Test_Sftp_ChangeDirectory_Which_Exists_With_Slash()
|
||||
{
|
||||
using (var sftp = new SftpClient(Resources.HOST, Resources.USERNAME, Resources.PASSWORD))
|
||||
{
|
||||
sftp.Connect();
|
||||
sftp.ChangeDirectory("/usr/");
|
||||
Assert.AreEqual("/usr", sftp.WorkingDirectory);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -130,6 +130,7 @@
|
||||
<Compile Include="Security\Cryptography\Ciphers\DesCipher.cs" />
|
||||
<Compile Include="Sftp\CloseCommand.cs" />
|
||||
<Compile Include="Sftp\OpenCommand.cs" />
|
||||
<Compile Include="Sftp\OpenDirectoryCommand.cs" />
|
||||
<Compile Include="Sftp\ReadCommand.cs" />
|
||||
<Compile Include="Sftp\SetStatusCommand.cs" />
|
||||
<Compile Include="Sftp\SftpFileAttributes.cs" />
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Renci.SshNet.Common;
|
||||
using Renci.SshNet.Sftp.Messages;
|
||||
using System.Globalization;
|
||||
|
||||
namespace Renci.SshNet.Sftp
|
||||
{
|
||||
internal class OpenDirectoryCommand : SftpCommand
|
||||
{
|
||||
private string _path;
|
||||
|
||||
public byte[] Handle { get; private set; }
|
||||
|
||||
public OpenDirectoryCommand(SftpSession sftpSession, string path)
|
||||
: base(sftpSession)
|
||||
{
|
||||
this._path = path;
|
||||
}
|
||||
|
||||
protected override void OnExecute()
|
||||
{
|
||||
this.SendOpenDirMessage(this._path);
|
||||
}
|
||||
|
||||
protected override void OnHandle(byte[] handle)
|
||||
{
|
||||
base.OnHandle(handle);
|
||||
|
||||
this.Handle = handle;
|
||||
|
||||
this.CompleteExecution();
|
||||
}
|
||||
|
||||
protected override void OnStatus(StatusCodes statusCode, string errorMessage, string language)
|
||||
{
|
||||
base.OnStatus(statusCode, errorMessage, language);
|
||||
|
||||
if (statusCode == StatusCodes.NoSuchFile)
|
||||
{
|
||||
throw new SshFileNotFoundException(string.Format(CultureInfo.CurrentCulture, "Path '{0}' is not found.", this._path));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -101,7 +101,25 @@ namespace Renci.SshNet.Sftp
|
||||
|
||||
public void ChangeDirectory(string path)
|
||||
{
|
||||
this.WorkingDirectory = this.GetCanonicalPath(path);
|
||||
var fullPath = this.GetCanonicalPath(path);
|
||||
|
||||
// Open directory
|
||||
using (var openCmd = new OpenDirectoryCommand(this, fullPath))
|
||||
{
|
||||
openCmd.CommandTimeout = this._operationTimeout;
|
||||
|
||||
// Try to open directory and throw an exception if can't
|
||||
openCmd.Execute();
|
||||
|
||||
using (SftpCommand closeCmd = new CloseCommand(this, openCmd.Handle))
|
||||
{
|
||||
closeCmd.CommandTimeout = this._operationTimeout;
|
||||
|
||||
closeCmd.Execute();
|
||||
}
|
||||
}
|
||||
|
||||
this.WorkingDirectory = fullPath;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -141,6 +159,9 @@ namespace Renci.SshNet.Sftp
|
||||
|
||||
var partialFullPath = string.Join("/", pathParts, 0, pathParts.Length - 1);
|
||||
|
||||
if (string.IsNullOrEmpty(partialFullPath))
|
||||
partialFullPath = "/";
|
||||
|
||||
canonizedPath = this.GetRealPath(partialFullPath);
|
||||
|
||||
if (string.IsNullOrEmpty(canonizedPath))
|
||||
|
||||
Reference in New Issue
Block a user