123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- package ftp
- import (
- "io/ioutil"
- "net/textproto"
- "strings"
- "testing"
- "time"
- )
- const (
- testData = "Just some text"
- )
- func TestConnPASV(t *testing.T) {
- testConn(t, true)
- }
- func TestConnEPSV(t *testing.T) {
- testConn(t, false)
- }
- func testConn(t *testing.T, disableEPSV bool) {
- mock, c := openConn(t, "127.0.0.1", DialWithTimeout(5*time.Second), DialWithDisabledEPSV(disableEPSV))
- err := c.Login("anonymous", "anonymous")
- if err != nil {
- t.Fatal(err)
- }
- // Read without deadline
- r, err := c.Retr("any_filename_ok")
- 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()
- r.Close() // test we can close two times
- }
- // Read with deadline
- r, err = c.Retr("any_filename_ok")
- if err != nil {
- t.Error(err)
- } else {
- r.SetDeadline(time.Now())
- _, err := ioutil.ReadAll(r)
- if err == nil {
- t.Error("deadline should have caused error")
- } else if !strings.HasSuffix(err.Error(), "i/o timeout") {
- t.Error(err)
- }
- r.Close()
- }
- // Read with offset
- r, err = c.RetrFrom("any_filename_ok", 5)
- if err != nil {
- t.Error(err)
- } else {
- 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()
- }
- err = c.Logout()
- if err != nil {
- if protoErr := err.(*textproto.Error); protoErr != nil {
- if protoErr.Code != StatusNotImplemented {
- t.Error(err)
- }
- } else {
- t.Error(err)
- }
- }
- if err := c.Quit(); err != nil {
- t.Fatal(err)
- }
- // Wait for the connection to close
- mock.Wait()
- }
- func TestDialtimeout(t *testing.T) {
- if testing.Short() {
- t.Skip("skipping test in short mode.")
- }
- c, err := DialTimeout("127.0.0.1:2121", 1*time.Second)
- if err == nil {
- t.Fatal("expected some error, got nil")
- c.Quit()
- }
- }
- func TestWrongLogin(t *testing.T) {
- mock, err := newFtpMock(t, "127.0.0.1")
- if err != nil {
- t.Fatal(err)
- }
- defer mock.Close()
- c, err := DialTimeout(mock.Addr(), 5*time.Second)
- if err != nil {
- t.Fatal(err)
- }
- defer c.Quit()
- err = c.Login("not_anonymous", "any_password_ok")
- if err == nil {
- t.Fatal("expected error, got nil")
- }
- }
|