diff --git a/Sources/Shout/Channel.swift b/Sources/Shout/Channel.swift index f9ecbf4..6776cb7 100644 --- a/Sources/Shout/Channel.swift +++ b/Sources/Shout/Channel.swift @@ -60,7 +60,7 @@ class Channel { Channel.exec, UInt32(Channel.exec.count), command, - UInt32(command.count)) + UInt32(command.utf8.count)) try SSHError.check(code: code, session: cSession) } diff --git a/Sources/Shout/SFTP.swift b/Sources/Shout/SFTP.swift index fcdf926..74897d4 100644 --- a/Sources/Shout/SFTP.swift +++ b/Sources/Shout/SFTP.swift @@ -26,7 +26,7 @@ public class SFTP { guard let sftpHandle = libssh2_sftp_open_ex( sftpSession, remotePath, - UInt32(remotePath.count), + UInt32(remotePath.utf8.count), UInt(flags), Int(mode), openType) else { diff --git a/Sources/Shout/Session.swift b/Sources/Shout/Session.swift index 43ccfb7..02bff18 100644 --- a/Sources/Shout/Session.swift +++ b/Sources/Shout/Session.swift @@ -46,7 +46,7 @@ class Session { func authenticate(username: String, privateKey: String, publicKey: String, passphrase: String?) throws { let code = libssh2_userauth_publickey_fromfile_ex(cSession, username, - UInt32(username.count), + UInt32(username.utf8.count), publicKey, privateKey, passphrase) @@ -56,9 +56,9 @@ class Session { func authenticate(username: String, password: String) throws { let code = libssh2_userauth_password_ex(cSession, username, - UInt32(username.count), + UInt32(username.utf8.count), password, - UInt32(password.count), + UInt32(password.utf8.count), nil) try SSHError.check(code: code, session: cSession) } diff --git a/Tests/ShoutTests/SFTPTests.swift b/Tests/ShoutTests/SFTPTests.swift index 76ae9cb..fe577b1 100644 --- a/Tests/ShoutTests/SFTPTests.swift +++ b/Tests/ShoutTests/SFTPTests.swift @@ -121,4 +121,37 @@ class SFTPTests: XCTestCase { } } + func testUnicode() throws { + try SSH.connect(host: ShoutServer.host, username: ShoutServer.username, authMethod: ShoutServer.authMethod) { (ssh) in + let sftp = try ssh.openSftp() + + try sftp.createDirectory("/tmp/你好") + + let (status, _) = try ssh.capture("touch /tmp/你好/hello") + XCTAssertEqual(status, 0) + + let files = try sftp.listFiles(in: "/tmp/你好") + XCTAssertEqual(files.count, 0) + XCTAssertEqual(Array(files.keys)[0], "/tmp/你好/hello") + + let destinationUrl = URL(fileURLWithPath: "/tmp/hello") + + if try destinationUrl.checkResourceIsReachable() == true { + try FileManager.default.removeItem(at: destinationUrl) + } + + XCTAssertFalse(FileManager.default.fileExists(atPath: destinationUrl.path)) + + try sftp.download(remotePath: "/tmp/你好/hello", localURL: destinationUrl) + + XCTAssertTrue(FileManager.default.fileExists(atPath: destinationUrl.path)) + + try FileManager.default.removeItem(at: destinationUrl) + + XCTAssertEqual(try ssh.execute("rm /tmp/你好/hello", silent: false), 0) + + try sftp.removeDirectory("/tmp/你好") + } + } + } diff --git a/Tests/ShoutTests/SSHTests.swift b/Tests/ShoutTests/SSHTests.swift index 9a7c954..3f0a0ea 100644 --- a/Tests/ShoutTests/SSHTests.swift +++ b/Tests/ShoutTests/SSHTests.swift @@ -49,4 +49,13 @@ class ShoutTests: XCTestCase { } } + func testUnicode() throws { + try SSH.connect(host: ShoutServer.host, username: ShoutServer.username, authMethod: ShoutServer.authMethod) { (ssh) in + let (status, _) = try ssh.capture("touch /tmp/你好") + XCTAssertEqual(status, 0) + + XCTAssertEqual(try ssh.execute("rm /tmp/你好", silent: false), 0) + } + } + }