Skip to content

Commit

Permalink
Test case for QUIT command with a transfer in progress (#410)
Browse files Browse the repository at this point in the history
  • Loading branch information
drakkan authored Jul 19, 2023
1 parent 3fe6de2 commit 76e3b67
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions handle_misc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@ package ftpserver
import (
"crypto/tls"
"fmt"
"io"
"path/filepath"
"strings"
"testing"

"time"

"github.com/secsy/goftp"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -272,6 +275,63 @@ func TestQuit(t *testing.T) {
require.Equal(t, StatusClosingControlConn, rc)
}

func TestQuitWithTransferInProgress(t *testing.T) {
s := NewTestServerWithDriver(t, &TestServerDriver{
Debug: false,
})
conf := goftp.Config{
User: authUser,
Password: authPass,
}
c, err := goftp.DialConfig(conf, s.Addr())
require.NoError(t, err, "Couldn't connect")

defer func() { panicOnError(c.Close()) }()

raw, err := c.OpenRawConn()
require.NoError(t, err, "Couldn't open raw connection")

defer func() { require.NoError(t, raw.Close()) }()

ch := make(chan struct{}, 1)
go func() {
defer close(ch)

dcGetter, err := raw.PrepareDataConn() //nolint:govet
require.NoError(t, err)
file := createTemporaryFile(t, 256*1024)
fileName := filepath.Base(file.Name())
rc, response, err := raw.SendCommand(fmt.Sprintf("%s %s", "STOR", fileName))
require.NoError(t, err)
require.Equal(t, StatusFileStatusOK, rc, response)

dc, err := dcGetter()
assert.NoError(t, err)

ch <- struct{}{}
// wait some more time to be sure we send the QUIT command before starting the file copy
time.Sleep(100 * time.Millisecond)

_, err = io.Copy(dc, file)
assert.NoError(t, err)

err = dc.Close()
assert.NoError(t, err)
}()

// wait for the trasfer to start
<-ch
// we send a QUIT command after sending STOR and before the transfer ends.
// We expect the transfer close response and then the QUIT response
rc, _, err := raw.SendCommand("QUIT")
require.NoError(t, err)
require.Equal(t, StatusClosingDataConn, rc)

rc, _, err = raw.ReadResponse()
require.NoError(t, err)
require.Equal(t, StatusClosingControlConn, rc)
}

func TestTYPE(t *testing.T) {
s := NewTestServer(t, false)
conf := goftp.Config{
Expand Down

0 comments on commit 76e3b67

Please sign in to comment.