Files
Termind/TFMTests/SFTPRenameTests.swift
T
2026-09-17 13:50:06 +08:00

73 lines
2.4 KiB
Swift

import CSSH2
import Testing
@testable import TermindRemoteInfrastructure
@Suite struct SFTPRenameTests {
@Test func overwriteUsesPOSIXRenameWithoutFirstFailingStandardRename() {
var calls: [String] = []
let result = SFTPRenameOperation.perform(
overwrite: true,
posixRename: {
calls.append("posix")
return 0
},
standardRename: {
calls.append("standard")
return LIBSSH2_ERROR_SFTP_PROTOCOL
},
lastStatus: { UInt(LIBSSH2_FX_FAILURE) })
#expect(result == 0)
#expect(calls == ["posix"])
}
@Test func nonOverwritingRenameNeverUsesAnOverwritingExtension() {
var calls: [String] = []
let result = SFTPRenameOperation.perform(
overwrite: false,
posixRename: {
calls.append("posix")
return 0
},
standardRename: {
calls.append("standard")
return LIBSSH2_ERROR_SFTP_PROTOCOL
},
lastStatus: { UInt(LIBSSH2_FX_FAILURE) })
#expect(result == LIBSSH2_ERROR_SFTP_PROTOCOL)
#expect(calls == ["standard"])
}
@Test(arguments: [Int32(LIBSSH2_FX_OP_UNSUPPORTED), LIBSSH2_ERROR_SFTP_PROTOCOL])
func unsupportedExtensionFallsBackToStandardRename(_ result: Int32) {
var calls: [String] = []
#expect(
SFTPRenameOperation.perform(
overwrite: true,
posixRename: {
calls.append("posix")
return result
},
standardRename: {
calls.append("standard")
return 0
},
lastStatus: { UInt(LIBSSH2_FX_OP_UNSUPPORTED) }) == 0)
#expect(calls == ["posix", "standard"])
}
@Test(arguments: [LIBSSH2_ERROR_SFTP_PROTOCOL, LIBSSH2_ERROR_SOCKET_RECV, LIBSSH2_ERROR_EAGAIN])
func realFailuresDoNotFallBackOrRemoveTheOriginal(_ failure: Int32) {
var usedFallback = false
let result = SFTPRenameOperation.perform(
overwrite: true, posixRename: { failure },
standardRename: {
usedFallback = true
return 0
},
lastStatus: { UInt(LIBSSH2_FX_FAILURE) })
#expect(result == failure)
#expect(!usedFallback)
}
}