ftp/client_test.go

172 lines
3.2 KiB
Go
Raw Normal View History

package ftp
import (
"bytes"
"io/ioutil"
"net/textproto"
"testing"
"time"
2017-02-04 12:42:50 +01:00
"github.com/stretchr/testify/require"
)
const (
testData = "Just some text"
2013-02-17 10:31:56 +01:00
testDir = "mydir"
)
2015-08-20 01:46:22 +02:00
func TestConnPASV(t *testing.T) {
testConn(t, true)
}
func TestConnEPSV(t *testing.T) {
testConn(t, false)
}
func testConn(t *testing.T, disableEPSV bool) {
2015-08-18 18:39:31 +02:00
if testing.Short() {
t.Skip("skipping test in short mode.")
}
2017-02-04 12:42:50 +01:00
require := require.New(t)
2015-08-18 18:39:31 +02:00
c, err := DialTimeout("localhost:21", 5*time.Second)
2017-02-04 12:42:50 +01:00
require.NoError(err)
if disableEPSV {
2015-08-20 01:46:22 +02:00
delete(c.features, "EPSV")
c.disableEPSV = true
2015-08-20 01:46:22 +02:00
}
err = c.Login("anonymous", "anonymous")
2017-02-04 12:42:50 +01:00
require.NoError(err)
err = c.NoOp()
2017-02-04 12:42:50 +01:00
require.NoError(err)
2015-08-18 19:34:22 +02:00
err = c.ChangeDir("incoming")
2017-02-04 12:42:50 +01:00
require.NoError(err)
2015-08-18 19:34:22 +02:00
data := bytes.NewBufferString(testData)
err = c.Stor("test", data)
2017-02-04 12:42:50 +01:00
require.NoError(err)
_, err = c.List(".")
2017-02-04 12:42:50 +01:00
require.NoError(err)
err = c.Rename("test", "tset")
2017-02-04 12:42:50 +01:00
require.NoError(err)
r, err := c.Retr("tset")
2017-02-04 12:42:50 +01:00
require.NoError(err)
buf, err := ioutil.ReadAll(r)
require.NoError(err)
require.Equal(testData, string(buf))
err = r.Close()
require.NoError(err)
2015-08-20 22:47:09 +02:00
r, err = c.RetrFrom("tset", 5)
2017-02-04 12:42:50 +01:00
require.NoError(err)
buf, err = ioutil.ReadAll(r)
require.NoError(err)
require.Equal(testData[5:], string(buf))
r.Close()
err = c.Delete("tset")
2017-02-04 12:42:50 +01:00
require.NoError(err)
err = c.MakeDir(testDir)
2017-02-04 12:42:50 +01:00
require.NoError(err)
err = c.ChangeDir(testDir)
2017-02-04 12:42:50 +01:00
require.NoError(err)
dir, err := c.CurrentDir()
2017-02-04 12:42:50 +01:00
require.NoError(err)
require.Equal("/incoming/"+testDir, dir)
err = c.ChangeDirToParent()
2017-02-04 12:42:50 +01:00
require.NoError(err)
2015-08-20 10:32:28 +02:00
entries, err := c.NameList("/")
2017-02-04 12:42:50 +01:00
require.NoError(err)
require.EqualValues([]string{"/incoming"}, entries)
2015-08-20 10:32:28 +02:00
err = c.RemoveDir(testDir)
2017-02-04 12:42:50 +01:00
require.NoError(err)
2013-05-19 21:15:23 +02:00
err = c.Logout()
if err != nil {
if protoErr := err.(*textproto.Error); protoErr != nil {
if protoErr.Code != StatusNotImplemented {
t.Error(err)
}
} else {
t.Error(err)
}
2013-05-19 21:15:23 +02:00
}
2017-02-04 12:42:50 +01:00
err = c.Quit()
require.NoError(err)
err = c.NoOp()
2017-02-04 12:42:50 +01:00
require.Error(err)
require.Regexp("write tcp .* use of closed network connection", err.Error())
}
2013-07-08 07:48:11 +02:00
func TestConnIPv6(t *testing.T) {
2015-08-18 19:02:23 +02:00
if testing.Short() {
t.Skip("skipping test in short mode.")
}
2017-02-04 12:42:50 +01:00
require := require.New(t)
2015-08-18 19:02:23 +02:00
c, err := DialTimeout("[::1]:21", 5*time.Second)
2017-02-04 12:42:50 +01:00
require.NoError(err)
err = c.Login("anonymous", "anonymous")
2017-02-04 12:42:50 +01:00
require.NoError(err)
_, err = c.List(".")
2017-02-04 12:42:50 +01:00
require.NoError(err)
2017-02-04 12:42:50 +01:00
err = c.Quit()
require.NoError(err)
}
2015-08-21 18:36:56 +02:00
// TestConnect tests the legacy Connect function
func TestConnect(t *testing.T) {
if testing.Short() {
t.Skip("skipping test in short mode.")
}
c, err := Connect("localhost:21")
2017-02-04 12:42:50 +01:00
require.NoError(t, err)
c.Quit()
}
2017-02-04 12:42:50 +01:00
func TestDialTimeout(t *testing.T) {
2015-08-21 18:36:56 +02:00
if testing.Short() {
t.Skip("skipping test in short mode.")
}
2017-02-04 12:42:50 +01:00
require := require.New(t)
2015-08-21 18:36:56 +02:00
2017-02-04 12:42:50 +01:00
_, err := DialTimeout("localhost:2121", 1*time.Second)
require.Error(err)
require.Regexp("dial tcp .* connection refused", err.Error())
2015-08-21 18:36:56 +02:00
}
func TestWrongLogin(t *testing.T) {
if testing.Short() {
t.Skip("skipping test in short mode.")
}
2017-02-04 12:42:50 +01:00
require := require.New(t)
2015-08-21 18:36:56 +02:00
c, err := DialTimeout("localhost:21", 5*time.Second)
2017-02-04 12:42:50 +01:00
require.NoError(err)
2015-08-21 18:36:56 +02:00
defer c.Quit()
err = c.Login("zoo2Shia", "fei5Yix9")
2017-02-04 12:42:50 +01:00
require.Error(err)
require.Regexp("(Login incorrect|anonymous only)", err.Error())
2015-08-21 18:36:56 +02:00
}