Skip to content

Commit 361c288

Browse files
add rekey check to sftp client and use reletave path to file in test case
1 parent 462d77b commit 361c288

File tree

2 files changed

+44
-34
lines changed

2 files changed

+44
-34
lines changed

.github/workflows/windows-sftp.yml

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -318,16 +318,15 @@ jobs:
318318
}
319319
Write-Host "Basic SFTP test passed"
320320
321-
- name: Create 3GB test file and run SFTP get/put (large_rw)
322-
if: matrix.test_type == 'large_rw'
321+
- name: Create 3GB test file and run SFTP get/put
323322
working-directory: ${{ github.workspace }}\wolfssh
324323
shell: pwsh
325324
timeout-minutes: 25
326325
run: |
327326
$sftpPath = $env:SFTP_PATH
328-
$workDir = (Get-Location).Path
327+
$workDir = Join-Path $env:GITHUB_WORKSPACE "wolfssh"
329328
$largeFile = Join-Path $workDir "large_test.dat"
330-
$getDest = Join-Path $workDir "large_test_copy.dat"
329+
$getDestPath = Join-Path $workDir "large_test_copy.dat"
331330
332331
# Create 3GB file: one random 10MB chunk repeated 307x + 2MB
333332
Write-Host "Creating 3GB test file..."
@@ -346,57 +345,68 @@ jobs:
346345
$fs.Close()
347346
348347
$hash = Get-FileHash -Path $largeFile -Algorithm SHA256
349-
$hash.Hash | Out-File -FilePath large_test.dat.sha256
348+
$hash.Hash | Out-File -FilePath (Join-Path $workDir "large_test.dat.sha256")
350349
Write-Host "Created 3GB file, SHA256: $($hash.Hash)"
351350
352351
# SFTP PUT (upload)
352+
# Use a relative remote path (no leading /) so the server resolves it
353+
# under testuser's home directory. An absolute /large_test.dat maps to
354+
# the drive root (C:\) where testuser has no write permission; relative
355+
# large_test.dat is prefixed by workingDir (C:\Users\testuser) on the
356+
# client and becomes C:\Users\testuser\large_test.dat on the server.
353357
Write-Host "SFTP PUT 3GB file..."
354-
$putCommands = "put $largeFile /large_test.dat`nquit"
355-
$putCommands | Out-File -FilePath sftp_put_commands.txt -Encoding ASCII
358+
$putCommands = "put $largeFile large_test.dat`nquit"
359+
$putCommands | Out-File -FilePath (Join-Path $workDir "sftp_put_commands.txt") -Encoding ASCII
356360
$proc = Start-Process -FilePath $sftpPath `
357361
-ArgumentList "-u", "testuser", "-P", $env:TESTUSER_PASSWORD, "-h", "localhost", "-p", "${{env.TEST_PORT}}" `
358-
-RedirectStandardInput "sftp_put_commands.txt" `
359-
-RedirectStandardOutput "sftp_put_out.txt" `
360-
-RedirectStandardError "sftp_put_err.txt" `
362+
-WorkingDirectory $workDir `
363+
-RedirectStandardInput (Join-Path $workDir "sftp_put_commands.txt") `
364+
-RedirectStandardOutput (Join-Path $workDir "sftp_put_out.txt") `
365+
-RedirectStandardError (Join-Path $workDir "sftp_put_err.txt") `
361366
-Wait -NoNewWindow -PassThru
362367
363-
if ($proc.ExitCode -ne 0) {
364-
Get-Content sftp_put_out.txt
365-
Get-Content sftp_put_err.txt
368+
$putOut = Get-Content (Join-Path $workDir "sftp_put_out.txt") -Raw -ErrorAction SilentlyContinue
369+
Write-Host "=== SFTP PUT output ==="; Write-Host $putOut
370+
if ($proc.ExitCode -ne 0 -or $putOut -match "Error pushing file") {
371+
Get-Content (Join-Path $workDir "sftp_put_err.txt") -ErrorAction SilentlyContinue
366372
Write-Host "ERROR: SFTP PUT failed"
367373
exit 1
368374
}
369375
Write-Host "PUT succeeded"
370376
371-
# SFTP GET (download)
377+
# SFTP GET (download) - relative remote and local paths.
378+
# Remote large_test.dat resolves to C:\Users\testuser\large_test.dat.
379+
# Local large_test_copy.dat is relative to wolfsftp's CWD ($workDir).
372380
Write-Host "SFTP GET 3GB file..."
373-
$getCommands = "get /large_test.dat $getDest`nquit"
374-
$getCommands | Out-File -FilePath sftp_get_commands.txt -Encoding ASCII
381+
$getCommands = "get large_test.dat large_test_copy.dat`nquit"
382+
$getCommands | Out-File -FilePath (Join-Path $workDir "sftp_get_commands.txt") -Encoding ASCII
375383
$proc2 = Start-Process -FilePath $sftpPath `
376384
-ArgumentList "-u", "testuser", "-P", $env:TESTUSER_PASSWORD, "-h", "localhost", "-p", "${{env.TEST_PORT}}" `
377-
-RedirectStandardInput "sftp_get_commands.txt" `
378-
-RedirectStandardOutput "sftp_get_out.txt" `
379-
-RedirectStandardError "sftp_get_err.txt" `
385+
-WorkingDirectory $workDir `
386+
-RedirectStandardInput (Join-Path $workDir "sftp_get_commands.txt") `
387+
-RedirectStandardOutput (Join-Path $workDir "sftp_get_out.txt") `
388+
-RedirectStandardError (Join-Path $workDir "sftp_get_err.txt") `
380389
-Wait -NoNewWindow -PassThru
381390
382-
if ($proc2.ExitCode -ne 0) {
383-
Get-Content sftp_get_out.txt
384-
Get-Content sftp_get_err.txt
391+
$getOut = Get-Content (Join-Path $workDir "sftp_get_out.txt") -Raw -ErrorAction SilentlyContinue
392+
Write-Host "=== SFTP GET output ==="; Write-Host $getOut
393+
if ($proc2.ExitCode -ne 0 -or $getOut -match "Error getting file") {
394+
Get-Content (Join-Path $workDir "sftp_get_err.txt") -ErrorAction SilentlyContinue
385395
Write-Host "ERROR: SFTP GET failed"
386396
exit 1
387397
}
388398
Write-Host "GET succeeded"
389399
390-
# Verify integrity
391-
$expectedHash = (Get-Content large_test.dat.sha256).Trim()
392-
$actualHash = (Get-FileHash -Path $getDest -Algorithm SHA256).Hash
400+
# Verify integrity (file is in $workDir from GET with relative path)
401+
$expectedHash = (Get-Content (Join-Path $workDir "large_test.dat.sha256")).Trim()
402+
$actualHash = (Get-FileHash -Path $getDestPath -Algorithm SHA256).Hash
393403
if ($expectedHash -ne $actualHash) {
394404
Write-Host "ERROR: SHA256 mismatch - PUT/GET corruption"
395405
Write-Host "Expected: $expectedHash"
396406
Write-Host "Actual: $actualHash"
397407
exit 1
398408
}
399-
Write-Host "PASS: 3GB SFTP get/put with WOLFSSH_MAX_SFTP_RW=10485760 succeeded"
409+
Write-Host "PASS: 3GB SFTP get/put succeeded"
400410
401411
- name: Cleanup
402412
if: always()

examples/sftpclient/sftpclient.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -769,8 +769,8 @@ static int doCmds(func_args* args)
769769

770770
ret = wolfSSH_SFTP_STAT(ssh, pt, &atrb);
771771
err = wolfSSH_get_error(ssh);
772-
} while ((err == WS_WANT_READ || err == WS_WANT_WRITE)
773-
&& ret != WS_SUCCESS);
772+
} while ((err == WS_WANT_READ || err == WS_WANT_WRITE ||
773+
err == WS_REKEYING) && ret != WS_SUCCESS);
774774
if (ret != WS_SUCCESS) {
775775
if (SFTP_FPUTS(args, "Error changing directory\n") < 0) {
776776
err_msg("fputs error");
@@ -857,8 +857,8 @@ static int doCmds(func_args* args)
857857

858858
ret = wolfSSH_SFTP_CHMOD(ssh, pt, mode);
859859
err = wolfSSH_get_error(ssh);
860-
} while ((err == WS_WANT_READ || err == WS_WANT_WRITE)
861-
&& ret != WS_SUCCESS);
860+
} while ((err == WS_WANT_READ || err == WS_WANT_WRITE ||
861+
err == WS_REKEYING) && ret != WS_SUCCESS);
862862
if (ret != WS_SUCCESS) {
863863
if (SFTP_FPUTS(args, "Unable to change permissions of ") < 0) {
864864
err_msg("fputs error");
@@ -914,8 +914,8 @@ static int doCmds(func_args* args)
914914

915915
ret = wolfSSH_SFTP_RMDIR(ssh, pt);
916916
err = wolfSSH_get_error(ssh);
917-
} while ((err == WS_WANT_READ || err == WS_WANT_WRITE)
918-
&& ret != WS_SUCCESS);
917+
} while ((err == WS_WANT_READ || err == WS_WANT_WRITE ||
918+
err == WS_REKEYING) && ret != WS_SUCCESS);
919919
if (ret != WS_SUCCESS) {
920920
if (ret == WS_PERMISSIONS) {
921921
if (SFTP_FPUTS(args, "Insufficient permissions\n") < 0) {
@@ -967,8 +967,8 @@ static int doCmds(func_args* args)
967967

968968
ret = wolfSSH_SFTP_Remove(ssh, pt);
969969
err = wolfSSH_get_error(ssh);
970-
} while ((err == WS_WANT_READ || err == WS_WANT_WRITE)
971-
&& ret != WS_SUCCESS);
970+
} while ((err == WS_WANT_READ || err == WS_WANT_WRITE ||
971+
err == WS_REKEYING) && ret != WS_SUCCESS);
972972
if (ret != WS_SUCCESS) {
973973
if (ret == WS_PERMISSIONS) {
974974
if (SFTP_FPUTS(args, "Insufficient permissions\n") < 0) {

0 commit comments

Comments
 (0)