Skip to content

Commit

Permalink
Upload is ok but download is not
Browse files Browse the repository at this point in the history
  • Loading branch information
fclairamb committed Jun 2, 2024
1 parent 8ebd068 commit 06a8c71
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 13 deletions.
6 changes: 2 additions & 4 deletions client_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -823,10 +823,8 @@ func (c *deflateConn) Write(p []byte) (int, error) {
}

func (c *deflateConn) Close() error {
errWriter := c.Writer.Flush()

if errWriter != nil {
return errWriter
if err := c.Writer.Close(); err != nil {
return fmt.Errorf("could not close deflate writer: %w", err)
}

return c.Conn.Close()
Expand Down
7 changes: 4 additions & 3 deletions driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ func (driver *TestServerDriver) Init() {
}

{
dir, _ := os.MkdirTemp("", "example")
if err := os.MkdirAll(dir, 0o750); err != nil {
driver.serverDir, _ = os.MkdirTemp("", "example")
if err := os.MkdirAll(driver.serverDir, 0o750); err != nil {
panic(err)
}

driver.fs = afero.NewBasePathFs(afero.NewOsFs(), dir)
driver.fs = afero.NewBasePathFs(afero.NewOsFs(), driver.serverDir)
}
}

Expand Down Expand Up @@ -126,6 +126,7 @@ type TestServerDriver struct {
CloseOnConnect bool // disconnect the client as soon as it connects

Settings *Settings // Settings
serverDir string
fs afero.Fs
clientMU sync.Mutex
Clients []ClientContext
Expand Down
42 changes: 36 additions & 6 deletions transfer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"math/rand"
"net"
"os"
"path"
"path/filepath"
"runtime"
"strconv"
Expand Down Expand Up @@ -116,6 +117,7 @@ func ftpDownloadAndHash(t *testing.T, ftp *goftp.Client, filename string) string

type ftpDownloadOptions struct {
deflateMode bool
otherWriter io.Writer
}

func ftpDownloadAndHashWithRawConnection(t *testing.T, raw goftp.RawConn, fileName string, options *ftpDownloadOptions) string {
Expand Down Expand Up @@ -143,7 +145,13 @@ func ftpDownloadAndHashWithRawConnection(t *testing.T, raw goftp.RawConn, fileNa
req.NoError(err)
}

_, err = io.Copy(hasher, dataConn)
var writer io.Writer = hasher

if options.otherWriter != nil {
writer = io.MultiWriter(writer, options.otherWriter)
}

_, err = io.Copy(writer, dataConn)
req.NoError(err)

err = dataConn.Close()
Expand Down Expand Up @@ -1260,12 +1268,14 @@ func getPortFromPASVResponse(t *testing.T, resp string) int {
}

func TestTransferModeDeflate(t *testing.T) {
s := NewTestServer(t, false)
driver := &TestServerDriver{Debug: true}
server := NewTestServerWithTestDriver(t, driver)

conf := goftp.Config{
User: authUser,
Password: authPass,
}
client, err := goftp.DialConfig(conf, s.Addr())
client, err := goftp.DialConfig(conf, server.Addr())
require.NoError(t, err, "Couldn't connect")

defer func() { require.NoError(t, client.Close()) }()
Expand All @@ -1281,6 +1291,7 @@ func TestTransferModeDeflate(t *testing.T) {
contents := []byte("line1\r\n\r\nline3\r\n,line4")
_, err = file.Write(contents)
require.NoError(t, err)
localHash := hashFile(t, file)

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

Expand All @@ -1297,7 +1308,26 @@ func TestTransferModeDeflate(t *testing.T) {
require.NoError(t, err)
require.Len(t, files, 1)

remoteHash := ftpDownloadAndHashWithRawConnection(t, raw, "file.txt", &ftpDownloadOptions{deflateMode: true})
localHash := hashFile(t, file)
require.Equal(t, localHash, remoteHash)
{ // Hash on server
fp, err := os.Open(path.Join(driver.serverDir, "file.txt"))
require.NoError(t, err)

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

readContents, err := io.ReadAll(fp)
require.NoError(t, err)
require.Equal(t, contents, readContents)
}

{ // Hash on standard connection
writer := bytes.NewBuffer(nil)
remoteHash := ftpDownloadAndHashWithRawConnection(t, raw, "file.txt", &ftpDownloadOptions{otherWriter: writer})
require.Equal(t, string(contents), writer.String())
require.Equal(t, localHash, remoteHash)
}

{ // Hash on deflate connection
remoteHash := ftpDownloadAndHashWithRawConnection(t, raw, "file.txt", &ftpDownloadOptions{deflateMode: true})
require.Equal(t, localHash, remoteHash)
}
}

0 comments on commit 06a8c71

Please sign in to comment.