ftp_test.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package ftp
  2. import (
  3. "io/ioutil"
  4. "net/textproto"
  5. "strings"
  6. "testing"
  7. "time"
  8. )
  9. const (
  10. testData = "Just some text"
  11. )
  12. func TestConnPASV(t *testing.T) {
  13. testConn(t, true)
  14. }
  15. func TestConnEPSV(t *testing.T) {
  16. testConn(t, false)
  17. }
  18. func testConn(t *testing.T, disableEPSV bool) {
  19. mock, c := openConn(t, "127.0.0.1", DialWithTimeout(5*time.Second), DialWithDisabledEPSV(disableEPSV))
  20. err := c.Login("anonymous", "anonymous")
  21. if err != nil {
  22. t.Fatal(err)
  23. }
  24. // Read without deadline
  25. r, err := c.Retr("any_filename_ok")
  26. if err != nil {
  27. t.Error(err)
  28. } else {
  29. buf, err := ioutil.ReadAll(r)
  30. if err != nil {
  31. t.Error(err)
  32. }
  33. if string(buf) != testData {
  34. t.Errorf("'%s'", buf)
  35. }
  36. r.Close()
  37. r.Close() // test we can close two times
  38. }
  39. // Read with deadline
  40. r, err = c.Retr("any_filename_ok")
  41. if err != nil {
  42. t.Error(err)
  43. } else {
  44. r.SetDeadline(time.Now())
  45. _, err := ioutil.ReadAll(r)
  46. if err == nil {
  47. t.Error("deadline should have caused error")
  48. } else if !strings.HasSuffix(err.Error(), "i/o timeout") {
  49. t.Error(err)
  50. }
  51. r.Close()
  52. }
  53. // Read with offset
  54. r, err = c.RetrFrom("any_filename_ok", 5)
  55. if err != nil {
  56. t.Error(err)
  57. } else {
  58. buf, err := ioutil.ReadAll(r)
  59. if err != nil {
  60. t.Error(err)
  61. }
  62. expected := testData[5:]
  63. if string(buf) != expected {
  64. t.Errorf("read %q, expected %q", buf, expected)
  65. }
  66. r.Close()
  67. }
  68. err = c.Logout()
  69. if err != nil {
  70. if protoErr := err.(*textproto.Error); protoErr != nil {
  71. if protoErr.Code != StatusNotImplemented {
  72. t.Error(err)
  73. }
  74. } else {
  75. t.Error(err)
  76. }
  77. }
  78. if err := c.Quit(); err != nil {
  79. t.Fatal(err)
  80. }
  81. // Wait for the connection to close
  82. mock.Wait()
  83. }
  84. func TestDialtimeout(t *testing.T) {
  85. if testing.Short() {
  86. t.Skip("skipping test in short mode.")
  87. }
  88. c, err := DialTimeout("127.0.0.1:2121", 1*time.Second)
  89. if err == nil {
  90. t.Fatal("expected some error, got nil")
  91. c.Quit()
  92. }
  93. }
  94. func TestWrongLogin(t *testing.T) {
  95. mock, err := newFtpMock(t, "127.0.0.1")
  96. if err != nil {
  97. t.Fatal(err)
  98. }
  99. defer mock.Close()
  100. c, err := DialTimeout(mock.Addr(), 5*time.Second)
  101. if err != nil {
  102. t.Fatal(err)
  103. }
  104. defer c.Quit()
  105. err = c.Login("not_anonymous", "any_password_ok")
  106. if err == nil {
  107. t.Fatal("expected error, got nil")
  108. }
  109. }