ftp/client_test.go

238 lines
3.6 KiB
Go
Raw Normal View History

package ftp
import (
"bytes"
"io/ioutil"
"net/textproto"
"testing"
"time"
)
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.")
}
c, err := DialTimeout("localhost:21", 5*time.Second)
if err != nil {
t.Fatal(err)
}
if disableEPSV {
2015-08-20 01:46:22 +02:00
delete(c.features, "EPSV")
2017-02-05 21:02:16 +01:00
c.DisableEPSV = true
2015-08-20 01:46:22 +02:00
}
err = c.Login("anonymous", "anonymous")
if err != nil {
t.Fatal(err)
}
err = c.NoOp()
if err != nil {
t.Error(err)
}
2015-08-18 19:34:22 +02:00
err = c.ChangeDir("incoming")
if err != nil {
t.Error(err)
}
data := bytes.NewBufferString(testData)
err = c.Stor("test", data)
if err != nil {
t.Error(err)
}
_, err = c.List(".")
if err != nil {
t.Error(err)
}
err = c.Rename("test", "tset")
if err != nil {
t.Error(err)
}
r, err := c.Retr("tset")
if err != nil {
t.Error(err)
} else {
buf, err := ioutil.ReadAll(r)
if err != nil {
t.Error(err)
}
if string(buf) != testData {
t.Errorf("'%s'", buf)
}
r.Close()
}
2015-08-20 22:47:09 +02:00
r, err = c.RetrFrom("tset", 5)
if err != nil {
t.Error(err)
} else {
2015-08-20 22:47:09 +02:00
buf, err := ioutil.ReadAll(r)
if err != nil {
t.Error(err)
}
expected := testData[5:]
if string(buf) != expected {
t.Errorf("read %q, expected %q", buf, expected)
}
r.Close()
}
fileSize, err := c.FileSize("tset")
if err != nil {
t.Error(err)
}
if fileSize != 14 {
t.Errorf("file size %q, expected %q", fileSize, 14)
}
fileSize, err = c.FileSize("not-found")
if err == nil {
t.Fatal("expected error, got nil")
}
err = c.Delete("tset")
if err != nil {
t.Error(err)
}
err = c.MakeDir(testDir)
if err != nil {
t.Error(err)
}
err = c.ChangeDir(testDir)
if err != nil {
t.Error(err)
}
dir, err := c.CurrentDir()
if err != nil {
t.Error(err)
} else {
2015-08-18 19:34:22 +02:00
if dir != "/incoming/"+testDir {
t.Error("Wrong dir: " + dir)
}
}
err = c.ChangeDirToParent()
if err != nil {
t.Error(err)
}
2015-08-20 10:32:28 +02:00
entries, err := c.NameList("/")
if err != nil {
t.Error(err)
}
if len(entries) != 1 || entries[0] != "/incoming" {
t.Errorf("Unexpected entries: %v", entries)
}
err = c.RemoveDir(testDir)
if err != nil {
t.Error(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
}
c.Quit()
err = c.NoOp()
if err == nil {
t.Error("Expected 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.")
}
c, err := DialTimeout("[::1]:21", 5*time.Second)
if err != nil {
t.Fatal(err)
}
err = c.Login("anonymous", "anonymous")
if err != nil {
t.Fatal(err)
}
_, err = c.List(".")
if err != nil {
t.Error(err)
}
c.Quit()
}
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")
if err != nil {
t.Fatal(err)
}
c.Quit()
}
2015-08-21 18:36:56 +02:00
func TestTimeout(t *testing.T) {
if testing.Short() {
t.Skip("skipping test in short mode.")
}
c, err := DialTimeout("localhost:2121", 1*time.Second)
if err == nil {
t.Fatal("expected timeout, got nil error")
c.Quit()
}
}
func TestWrongLogin(t *testing.T) {
if testing.Short() {
t.Skip("skipping test in short mode.")
}
c, err := DialTimeout("localhost:21", 5*time.Second)
if err != nil {
t.Fatal(err)
}
defer c.Quit()
err = c.Login("zoo2Shia", "fei5Yix9")
if err == nil {
t.Fatal("expected error, got nil")
}
}